mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-24 15:02:17 +01:00
Adding a wrapper scriptSig/scriptPubKey from core test cases
This commit is contained in:
parent
26d827cde9
commit
4843ee5c5e
6 changed files with 85 additions and 22 deletions
|
@ -47,6 +47,7 @@ trait ScriptParser extends Factory[List[ScriptToken]] {
|
|||
val scriptTokens : List[ScriptToken] = parse(str)
|
||||
//check if we have a redeem script that needs to be parsed
|
||||
if (scriptTokens.size > 0 && isRedeemScript(scriptTokens.last)) {
|
||||
logger.debug("Last token was redeem script")
|
||||
val redeemScript : Try[List[ScriptToken]] = parseRedeemScript(scriptTokens.last)
|
||||
redeemScript match {
|
||||
case Success(redeemScript : List[ScriptToken]) => scriptTokens.reverse.tail.reverse ++ redeemScript
|
||||
|
|
|
@ -15,7 +15,7 @@ trait NumberUtil extends BitcoinSLogger {
|
|||
* @param hex
|
||||
* @return
|
||||
*/
|
||||
def toLong(hex : String) : Long = toLong(ScalacoinUtil.decodeHex(hex))
|
||||
def toLong(hex : String) : Long = toLong(BitcoinSUtil.decodeHex(hex))
|
||||
|
||||
/**
|
||||
* Takes a list of bytes and converts it in to signed number inside of bitcoins
|
||||
|
@ -50,12 +50,12 @@ trait NumberUtil extends BitcoinSLogger {
|
|||
def longToHex(long : Long) : String = {
|
||||
if (long > -1) {
|
||||
val bytes = toByteList(long)
|
||||
ScalacoinUtil.encodeHex(bytes)
|
||||
BitcoinSUtil.encodeHex(bytes)
|
||||
} else {
|
||||
val bytes = toByteList(long.abs)
|
||||
//add sign bit
|
||||
val negativeNumberBytes : List[Byte] = changeSignBitToNegative(bytes)
|
||||
val hex = ScalacoinUtil.encodeHex(negativeNumberBytes.reverse)
|
||||
val hex = BitcoinSUtil.encodeHex(negativeNumberBytes.reverse)
|
||||
hex
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ trait NumberUtil extends BitcoinSLogger {
|
|||
* @param hex
|
||||
* @return
|
||||
*/
|
||||
def isPositive(hex : String) : Boolean = isPositive(ScalacoinUtil.decodeHex(hex))
|
||||
def isPositive(hex : String) : Boolean = isPositive(BitcoinSUtil.decodeHex(hex))
|
||||
|
||||
/**
|
||||
* Determines if a byte array is a positive or negative number
|
||||
|
@ -91,17 +91,17 @@ trait NumberUtil extends BitcoinSLogger {
|
|||
newByte :: bytes.tail
|
||||
}
|
||||
|
||||
def changeSignBitToPositive(hex : String) : List[Byte] = changeSignBitToPositive(ScalacoinUtil.decodeHex(hex))
|
||||
def changeSignBitToPositive(hex : String) : List[Byte] = changeSignBitToPositive(BitcoinSUtil.decodeHex(hex))
|
||||
|
||||
def changeSignBitToNegative(bytes : List[Byte]) : List[Byte] = {
|
||||
val newByte = (bytes.head | 0x80).toByte
|
||||
(newByte :: bytes.tail)
|
||||
}
|
||||
|
||||
def changeSignBitToNegative(hex : String) : List[Byte] = changeSignBitToNegative(ScalacoinUtil.decodeHex(hex))
|
||||
def changeSignBitToNegative(hex : String) : List[Byte] = changeSignBitToNegative(BitcoinSUtil.decodeHex(hex))
|
||||
|
||||
|
||||
def firstByteAllZeros(hex : String) : Boolean = firstByteAllZeros(ScalacoinUtil.decodeHex(hex))
|
||||
def firstByteAllZeros(hex : String) : Boolean = firstByteAllZeros(BitcoinSUtil.decodeHex(hex))
|
||||
|
||||
def firstByteAllZeros(bytes : List[Byte]) : Boolean = {
|
||||
val lastByte = bytes.head
|
||||
|
@ -118,7 +118,7 @@ trait NumberUtil extends BitcoinSLogger {
|
|||
* @param hex
|
||||
* @return
|
||||
*/
|
||||
def parseCompactSizeUInt(hex : String) : CompactSizeUInt = parseCompactSizeUInt(ScalacoinUtil.decodeHex(hex))
|
||||
def parseCompactSizeUInt(hex : String) : CompactSizeUInt = parseCompactSizeUInt(BitcoinSUtil.decodeHex(hex))
|
||||
|
||||
/**
|
||||
* Parses a CompactSizeUInt from a sequence of bytes
|
||||
|
@ -192,7 +192,7 @@ trait NumberUtil extends BitcoinSLogger {
|
|||
|
||||
private def parseLong(hex : String) : Long = java.lang.Long.parseLong(hex,16)
|
||||
|
||||
private def parseLong(bytes : List[Byte]) : Long = parseLong(ScalacoinUtil.encodeHex(bytes))
|
||||
private def parseLong(bytes : List[Byte]) : Long = parseLong(BitcoinSUtil.encodeHex(bytes))
|
||||
|
||||
private def parseLong(byte : Byte) : Long = parseLong(List(byte))
|
||||
|
||||
|
|
|
@ -21,15 +21,16 @@ import spray.json._
|
|||
class ScriptInterpreterTest extends FlatSpec with MustMatchers with ScriptInterpreter with BitcoinSLogger {
|
||||
|
||||
"ScriptInterpreter" must "evaluate a valid script to true" in {
|
||||
//this is in asm format, not hex
|
||||
/* //this is in asm format, not hex
|
||||
val inputScript = TestUtil.p2pkhInputScriptAsm
|
||||
//this is asm format, not hex
|
||||
val outputScript : List[ScriptToken] = TestUtil.p2pkhOutputScriptAsm
|
||||
val result = run(inputScript, outputScript,TestUtil.transaction)
|
||||
result must be (true)
|
||||
result must be (true)*/
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
it must "evaluate a script that asks to push 20 bytes onto the stack correctly" in {
|
||||
val stack = List(ScriptConstantImpl("68ca4fec736264c13b859bac43d5173df6871682"))
|
||||
val script = List(BytesToPushOntoStackImpl(20), ScriptConstantImpl("68ca4fec736264c13b859bac43d5173df6871682"), OP_EQUAL)
|
||||
|
@ -50,6 +51,7 @@ class ScriptInterpreterTest extends FlatSpec with MustMatchers with ScriptInterp
|
|||
run(stack,script,TestUtil.transaction) must equal (true)
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
@ -60,13 +62,12 @@ class ScriptInterpreterTest extends FlatSpec with MustMatchers with ScriptInterp
|
|||
val source = scala.io.Source.fromFile("src/test/scala/org/scalacoin/script/interpreter/script_valid.json")
|
||||
|
||||
//use this to represent a single test case from script_valid.json
|
||||
/*
|
||||
val lines =
|
||||
/* val lines =
|
||||
"""
|
||||
|
|
||||
|[["0", "0x21 0x02865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac0 CHECKSIG NOT", "STRICTENC"]]
|
||||
""".stripMargin
|
||||
*/
|
||||
|[["0x4b 0x417a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a",
|
||||
| "'Azzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' EQUAL", "P2SH,STRICTENC", "push 75 bytes"]]
|
||||
""".stripMargin*/
|
||||
|
||||
val lines = try source.getLines.filterNot(_.isEmpty).map(_.trim) mkString "\n" finally source.close()
|
||||
val json = lines.parseJson
|
||||
|
@ -76,7 +77,8 @@ class ScriptInterpreterTest extends FlatSpec with MustMatchers with ScriptInterp
|
|||
|
||||
for {
|
||||
testCase <- testCases
|
||||
tx = TransactionTestUtil.buildSpendingTransaction(testCase.scriptSig, TransactionTestUtil.buildCreditingTransaction(testCase.scriptPubKey))
|
||||
tx = TransactionTestUtil.buildSpendingTransaction(testCase.scriptSig.scriptSignature,
|
||||
TransactionTestUtil.buildCreditingTransaction(testCase.scriptPubKey.scriptPubKey))
|
||||
} yield {
|
||||
logger.info("Raw test case: " + testCase.raw)
|
||||
logger.info("Parsed ScriptSig: " + testCase.scriptSig)
|
||||
|
@ -84,7 +86,7 @@ class ScriptInterpreterTest extends FlatSpec with MustMatchers with ScriptInterp
|
|||
logger.info("Flags: " + testCase.flags)
|
||||
logger.info("Comments: " + testCase.comments)
|
||||
withClue(testCase.raw) {
|
||||
ScriptInterpreter.run(testCase.scriptSig, testCase.scriptPubKey, tx) must equal (true)
|
||||
ScriptInterpreter.run(testCase.scriptSig.asm, testCase.scriptPubKey.asm, tx) must equal (true)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,12 +9,12 @@ import org.scalacoin.protocol.script.{ScriptPubKey, ScriptSignature}
|
|||
* files.
|
||||
*/
|
||||
trait CoreTestCase {
|
||||
def scriptSig : ScriptSignature
|
||||
def scriptPubKey : ScriptPubKey
|
||||
def scriptSig : ScriptSignatureCoreTestCase
|
||||
def scriptPubKey : ScriptPubKeyCoreTestCase
|
||||
def flags : String
|
||||
def comments : String
|
||||
def raw : String
|
||||
}
|
||||
|
||||
case class CoreTestCaseImpl(scriptSig : ScriptSignature,
|
||||
scriptPubKey: ScriptPubKey, flags : String, comments : String, raw : String) extends CoreTestCase
|
||||
case class CoreTestCaseImpl(scriptSig : ScriptSignatureCoreTestCase,
|
||||
scriptPubKey: ScriptPubKeyCoreTestCase, flags : String, comments : String, raw : String) extends CoreTestCase
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package org.scalacoin.script.interpreter.testprotocol
|
||||
|
||||
import org.scalacoin.protocol.script.ScriptPubKey
|
||||
import org.scalacoin.script.constant.ScriptToken
|
||||
|
||||
/**
|
||||
* Created by chris on 3/14/16.
|
||||
*/
|
||||
trait ScriptPubKeyCoreTestCase {
|
||||
/**
|
||||
* The parsed asm representation for the core test case
|
||||
* this will be different than the asm representation
|
||||
* inside of scriptPubKey
|
||||
* @return
|
||||
*/
|
||||
def asm : Seq[ScriptToken]
|
||||
|
||||
/**
|
||||
* This is the underlying scriptPubKey that is parsed from the core test case
|
||||
* this is needed because there is no ubiquitous formats for scriptPubKeys
|
||||
* inside of script_valid.json. Normal scriptPubKeys have their asm representation
|
||||
* parsed from the underlying hex/byte representation every time which won't work
|
||||
* for core test cases.
|
||||
* @return
|
||||
*/
|
||||
def scriptPubKey : ScriptPubKey
|
||||
}
|
||||
|
||||
|
||||
case class ScriptPubKeyCoreTestCaseImpl(asm : Seq[ScriptToken], scriptPubKey : ScriptPubKey) extends ScriptPubKeyCoreTestCase
|
|
@ -0,0 +1,30 @@
|
|||
package org.scalacoin.script.interpreter.testprotocol
|
||||
|
||||
import org.scalacoin.protocol.script.{ScriptSignature, ScriptPubKey}
|
||||
import org.scalacoin.script.constant.ScriptToken
|
||||
|
||||
/**
|
||||
* Created by chris on 3/14/16.
|
||||
*/
|
||||
trait ScriptSignatureCoreTestCase {
|
||||
/**
|
||||
* The parsed asm representation for the core test case
|
||||
* this will be different than the asm representation
|
||||
* inside of scriptSignature
|
||||
* @return
|
||||
*/
|
||||
def asm : Seq[ScriptToken]
|
||||
|
||||
/**
|
||||
* This is the underlying scriptSignature that is parsed from the core test case
|
||||
* this is needed because there is no ubiquitous formats for scriptSignatures
|
||||
* inside of script_valid.json. Normal scriptSignatures have their asm representation
|
||||
* parsed from the underlying hex/byte representation every time which won't work
|
||||
* for core test cases.
|
||||
* @return
|
||||
*/
|
||||
def scriptSignature : ScriptSignature
|
||||
|
||||
}
|
||||
|
||||
case class ScriptSignatureCoreTestCaseImpl(asm : Seq[ScriptToken], scriptSignature : ScriptSignature) extends ScriptSignatureCoreTestCase
|
Loading…
Add table
Reference in a new issue