mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-23 14:50:42 +01:00
Successfully parsing a byte array into an asm output script
This commit is contained in:
parent
1242ad83f3
commit
fdc502acba
2 changed files with 27 additions and 8 deletions
|
@ -1,14 +1,15 @@
|
||||||
package org.scalacoin.script.parsing
|
package org.scalacoin.script.parsing
|
||||||
|
|
||||||
import org.scalacoin.script._
|
import org.scalacoin.script._
|
||||||
import org.scalacoin.script.constant.{OP_0, ScriptConstantImpl, ScriptToken}
|
import org.scalacoin.script.constant._
|
||||||
|
import org.scalacoin.util.ScalacoinUtil
|
||||||
|
|
||||||
import scala.annotation.tailrec
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by chris on 1/7/16.
|
* Created by chris on 1/7/16.
|
||||||
*/
|
*/
|
||||||
trait ScriptParser {
|
trait ScriptParser extends ScalacoinUtil {
|
||||||
/**
|
/**
|
||||||
* Parses an output script of a transaction
|
* Parses an output script of a transaction
|
||||||
* @param str
|
* @param str
|
||||||
|
@ -36,9 +37,27 @@ trait ScriptParser {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
def parse(bytes : List[Byte]) : List[ScriptToken] = {
|
def parse(bytes : List[Byte]) : List[ScriptToken] = {
|
||||||
val operations : List[ScriptToken] = for {
|
|
||||||
byte <- bytes
|
@tailrec
|
||||||
} yield ScriptOperationFactory.fromOpCode(byte).get
|
def loop(bytes : List[Byte], accum : List[ScriptToken]) : List[ScriptToken] = {
|
||||||
operations
|
bytes match {
|
||||||
|
case h :: t =>
|
||||||
|
val op = ScriptOperationFactory.fromOpCode(h).get
|
||||||
|
//means that we need to push x amount of bytes on to the stack
|
||||||
|
if (ScriptNumberFactory.operations.contains(op)) {
|
||||||
|
val (constant,tail) = pushConstant(ScriptNumberImpl(op.opCode),t)
|
||||||
|
loop(tail, constant :: accum)
|
||||||
|
} else loop(t, op :: accum)
|
||||||
|
case Nil => accum
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
loop(bytes, List()).reverse
|
||||||
|
}
|
||||||
|
|
||||||
|
def pushConstant(op : ScriptNumber, bytes : List[Byte]) : (ScriptConstant, List[Byte]) = {
|
||||||
|
val finalIndex = op.opCode
|
||||||
|
val constant : ScriptConstantImpl = ScriptConstantImpl(encodeHex(bytes.slice(0,finalIndex)))
|
||||||
|
(constant, bytes.slice(finalIndex,bytes.size))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,8 +34,8 @@ class ScriptParserTest extends FlatSpec with MustMatchers with ScriptParser with
|
||||||
|
|
||||||
it must "parse a p2pkh input script from a byte array to script tokens" in {
|
it must "parse a p2pkh input script from a byte array to script tokens" in {
|
||||||
/*val byteArray = TestUtil.p2pkhInputScript.getBytes.toList*/
|
/*val byteArray = TestUtil.p2pkhInputScript.getBytes.toList*/
|
||||||
val byteArray : List[Byte] = decodeHex("76a914")
|
val bytes : List[Byte] = decodeHex(TestUtil.p2pkhOutputScript)
|
||||||
parse(byteArray) must be (List(OP_DUP, OP_HASH160, ScriptNumberImpl(20)))
|
parse(bytes) must be (TestUtil.p2pkhOutputScriptAsm)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue