Adding constants 1-75 as ScriptNumbers - the represent the amount of bytes to push onto the stack

This commit is contained in:
Chris Stewart 2016-01-09 16:05:40 -06:00
parent cb2bd86700
commit 1242ad83f3
7 changed files with 52 additions and 8 deletions

View File

@ -39,7 +39,8 @@ object ScriptOperationFactory extends ScriptOperationFactory[ScriptOperation] {
override def operations = StackOperationFactory.operations ++ LocktimeOperationFactory.operations ++
CryptoOperationFactory.operations ++ ControlOperationsFactory.operations ++ BitwiseOperationsFactory.operations ++
ArithmeticOperationsFactory.operations ++ Seq(OP_0,OP_1,OP_1NEGATE, OP_2,OP_3,OP_4,OP_5,OP_6,OP_7,OP_8,
ArithmeticOperationsFactory.operations ++ ScriptNumberFactory.operations ++
Seq(OP_0,OP_1,OP_1NEGATE, OP_2,OP_3,OP_4,OP_5,OP_6,OP_7,OP_8,
OP_9,OP_10,OP_11,OP_12,OP_13,OP_14,OP_15,OP_16,OP_FALSE,OP_PUSHDATA1, OP_PUSHDATA2,OP_PUSHDATA4,OP_TRUE)
}

View File

@ -17,13 +17,13 @@ trait ScriptOperation extends ScriptToken {
sealed trait ScriptConstant extends ScriptToken
/**
* Represent a pubkey or hash of a pub key on our stack
*
* @param str
*/
case class ScriptConstantImpl(str : String) extends ScriptConstant
/**
* An empty array of bytes is pushed onto the stack. (This is not a no-op: an item is added to the stack.)
*/

View File

@ -47,7 +47,7 @@ trait CryptoInterpreter extends ScalacoinUtil {
*/
private def sha256Hash160(hex : String) : ScriptConstant = {
val bytes = decodeHex(hex)
val hash = org.bitcoinj.core.Utils.sha256hash160(bytes)
val hash = org.bitcoinj.core.Utils.sha256hash160(bytes.toArray)
ScriptConstantImpl(encodeHex(hash))
}

View File

@ -31,10 +31,14 @@ trait ScriptParser {
/**
* Parses a byte array into a the asm operations for a script
* will throw an exception if it fails to parse a op code
* @param bytes
* @return
*/
def parse(bytes : List[Byte]) : List[ScriptToken] = {
???
val operations : List[ScriptToken] = for {
byte <- bytes
} yield ScriptOperationFactory.fromOpCode(byte).get
operations
}
}

View File

@ -0,0 +1,27 @@
package org.scalacoin.script.constant
import org.scalatest.{MustMatchers, FlatSpec}
/**
* Created by chris on 1/9/16.
*/
class ScriptNumberFactoryTest extends FlatSpec with MustMatchers with ScriptNumberFactory {
"ScriptNumberFactory" must "represent the number 1" in {
operations.exists(_ == ScriptNumberImpl(1)) must be (true)
operations.exists(_ == ScriptNumberImpl(75)) must be (true)
}
it must "not allow creation of the script number 0" in {
intercept[IllegalArgumentException] {
operations.exists(_ == ScriptNumberImpl(0)) must be (false)
}
}
it must "not allow creation of the script number 76" in {
intercept[IllegalArgumentException] {
operations.exists(_ == ScriptNumberImpl(76)) must be (false)
}
}
}

View File

@ -1,12 +1,15 @@
package org.scalacoin.script.parsing
import org.scalacoin.util.TestUtil
import org.scalacoin.script.constant.{ScriptNumberImpl, OP_14}
import org.scalacoin.script.crypto.OP_HASH160
import org.scalacoin.script.stack.OP_DUP
import org.scalacoin.util.{ScalacoinUtil, TestUtil}
import org.scalatest.{FlatSpec, MustMatchers}
/**
* Created by chris on 1/7/16.
*/
class ScriptParserTest extends FlatSpec with MustMatchers with ScriptParser {
class ScriptParserTest extends FlatSpec with MustMatchers with ScriptParser with ScalacoinUtil {
"ScriptParser" must "parse an input script" in {
@ -29,4 +32,10 @@ class ScriptParserTest extends FlatSpec with MustMatchers with ScriptParser {
parsedInput must be (TestUtil.p2shInputScriptAsm)
}
it must "parse a p2pkh input script from a byte array to script tokens" in {
/*val byteArray = TestUtil.p2pkhInputScript.getBytes.toList*/
val byteArray : List[Byte] = decodeHex("76a914")
parse(byteArray) must be (List(OP_DUP, OP_HASH160, ScriptNumberImpl(20)))
}
}

View File

@ -2,7 +2,7 @@ package org.scalacoin.util
import org.scalacoin.protocol.{AssetAddress, BitcoinAddress}
import org.scalacoin.script.bitwise.{OP_EQUAL, OP_EQUALVERIFY}
import org.scalacoin.script.constant.{OP_0, ScriptConstantImpl}
import org.scalacoin.script.constant.{ScriptToken, OP_0, ScriptConstantImpl}
import org.scalacoin.script.crypto.{OP_CHECKSIG, OP_HASH160}
import org.scalacoin.script.stack.OP_DUP
@ -17,12 +17,15 @@ object TestUtil {
val multiSigAddress = BitcoinAddress("342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey")
val assetAddress = AssetAddress("akJsoCcyh34FGPotxfEoSXGwFPCNAkyCgTA")
val p2pkhInputScript = "473044022016ffdbb7c57634903c5e018fcfc48d59f4e37dc4bc3bbc9ba4e6ee39150bca030220119c2241a931819bc1a75d3596e4029d803d1cd6de123bf8a1a1a2c3665e1fac012102af7dad03e682fcd0427b5c24140c220ac9d8abe286c15f8cf5bf77eed19c3652"
val p2pkhInputScriptNotParsedAsm =
"3044022016ffdbb7c57634903c5e018fcfc48d59f4e37dc4bc3bbc9ba4e6ee39150bca030220119c2241a931819bc1a75d3596e4029d803d1cd6de123bf8a1a1a2c3665e1fac01" +
" 02af7dad03e682fcd0427b5c24140c220ac9d8abe286c15f8cf5bf77eed19c3652"
val p2pkhInputScriptAsm = List(
val p2pkhInputScriptAsm : List[ScriptToken] = List(
ScriptConstantImpl("3044022016ffdbb7c57634903c5e018fcfc48d59f4e37dc4bc3bbc9ba4e6ee39150bca030220119c2241a931819bc1a75d3596e4029d803d1cd6de123bf8a1a1a2c3665e1fac01"),
ScriptConstantImpl("02af7dad03e682fcd0427b5c24140c220ac9d8abe286c15f8cf5bf77eed19c3652"))
val p2pkhOutputScript = "76a914e2e7c1ab3f807151e832dd1accb3d4f5d7d19b4b88ac"
val p2pkhOutputScriptNotParsedAsm = "OP_DUP OP_HASH160 e2e7c1ab3f807151e832dd1accb3d4f5d7d19b4b OP_EQUALVERIFY OP_CHECKSIG"
val p2pkhOutputScriptAsm = List(OP_DUP,OP_HASH160,ScriptConstantImpl("e2e7c1ab3f807151e832dd1accb3d4f5d7d19b4b"),OP_EQUALVERIFY, OP_CHECKSIG)