Finding script signatures and their hash types working for p2sh script sigs

This commit is contained in:
Chris Stewart 2016-02-17 15:34:14 -06:00
parent d173ee20d2
commit 6bbcfa8cff
5 changed files with 34 additions and 14 deletions

View File

@ -15,12 +15,22 @@ trait RawBitcoinSerializer[T] {
*/
def read(hex : String) : T = read(ScalacoinUtil.decodeHex(hex))
/**
* Reads in bytes and transforms it into the approriate scala type T
* @param bytes
* @return
*/
def read(bytes : List[Byte]) : T
/**
* Reads in bytes and transforms it into the approriate scala type T
* @param bytes
* @return
*/
def read(bytes : Seq[Byte]) : T = read(bytes.toList)
/**
* Takes a type T and writes it into the appropriate type T
* Takes a type T and writes it into the appropriate hexadecimal serialization for type T
* @param t
* @return
*/

View File

@ -1,7 +1,7 @@
package org.scalacoin.protocol.script
import org.scalacoin.marshallers.transaction.TransactionElement
import org.scalacoin.script.constant.ScriptToken
import org.scalacoin.script.constant.{OP_0, ScriptToken}
import org.scalacoin.script.crypto.{HashType, HashTypeFactory}
import org.scalacoin.util.ScalacoinUtil
import org.slf4j.LoggerFactory
@ -15,11 +15,17 @@ trait ScriptSignature extends TransactionElement {
def asm : Seq[ScriptToken]
def hex : String
def signature : Seq[ScriptToken] = Seq(asm(1))
def signature : ScriptToken = {
if (asm.headOption.isDefined && asm.head == OP_0) {
//must be p2sh because of bug that forces p2sh scripts
//to begin with OP_0
asm(2)
} else asm(1)
}
def hashType : HashType = {
require(HashTypeFactory.fromByte(signature.head.bytes.last).isDefined,
"Hash type could not be read for this scriptSig: " + signature.head.hex)
HashTypeFactory.fromByte(signature.head.bytes.last).get
require(HashTypeFactory.fromByte(signature.bytes.last).isDefined,
"Hash type could not be read for this scriptSig: " + signature.hex)
HashTypeFactory.fromByte(signature.bytes.last).get
}
}

View File

@ -45,7 +45,7 @@ class ScriptParserTest extends FlatSpec with MustMatchers with ScriptParser with
}
it must "parse a p2sh input script from a byte array into script tokens" in {
val bytes = decodeHex(TestUtil.p2shInputScript)
val bytes = decodeHex(TestUtil.rawP2shInputScript)
parse(bytes) must be (TestUtil.p2shInputScriptAsm)
}

View File

@ -9,9 +9,9 @@ import org.scalatest.{FlatSpec, MustMatchers}
*/
class ScriptSignatureTest extends FlatSpec with MustMatchers {
"ScriptSignature" must "find the digital signature for the transaction inside of the script signature" in {
"ScriptSignature" must "find the digital signature for the transaction inside of a p2pkh script signature" in {
val scriptSig = ScriptSignatureFactory.factory(TestUtil.rawScriptSig)
scriptSig.signature.head.hex must be ("3045022100ad8e961fe3c22b2647d92b078f4c0cf81b3106ea5bf8b900ab8646aa4430216f022071d4edc2b5588be20ac4c2d07edd8ed069e10b2402d3dce2d3b835ccd075f28301")
scriptSig.signature.hex must be ("3045022100ad8e961fe3c22b2647d92b078f4c0cf81b3106ea5bf8b900ab8646aa4430216f022071d4edc2b5588be20ac4c2d07edd8ed069e10b2402d3dce2d3b835ccd075f28301")
}
it must "derive the signature hash type from the signature" in {
@ -20,11 +20,14 @@ class ScriptSignatureTest extends FlatSpec with MustMatchers {
}
it must "find the digital signature for a p2sh script signature" in {
val scriptSig = TestUtil.p2shInputScript
scriptSig.signature.hex must be ("304402207df6dd8dad22d49c3c83d8031733c32a53719278eb7985d3b35b375d776f84f102207054f9209a1e87d55feafc90aa04c33008e5bae9191da22aeaa16efde96f41f001")
}
/*it must "parse a p2pkh input script and derive it's hash type" in {
val scriptSig = ScriptSignatureFactory.factory(TestUtil.p2pkhInputScript)
scriptSig.hashType must be (SIGHASH_ALL)
}*/
it must "find the hash type for a p2sh script signature" in {
TestUtil.p2shInputScript.hashType must be (SIGHASH_ALL)
}
}

View File

@ -39,7 +39,8 @@ object TestUtil {
val p2shInputScriptNotParsedAsm =
"0 304402207df6dd8dad22d49c3c83d8031733c32a53719278eb7985d3b35b375d776f84f102207054f9209a1e87d55feafc90aa04c33008e5bae9191da22aeaa16efde96f41f001 512102b022902a0fdd71e831c37e4136c2754a59887be0618fb75336d7ab67e2982ff551ae"
val p2shInputScript = "0047304402207df6dd8dad22d49c3c83d8031733c32a53719278eb7985d3b35b375d776f84f102207054f9209a1e87d55feafc90aa04c33008e5bae9191da22aeaa16efde96f41f00125512102b022902a0fdd71e831c37e4136c2754a59887be0618fb75336d7ab67e2982ff551ae"
val rawP2shInputScript = "0047304402207df6dd8dad22d49c3c83d8031733c32a53719278eb7985d3b35b375d776f84f102207054f9209a1e87d55feafc90aa04c33008e5bae9191da22aeaa16efde96f41f00125512102b022902a0fdd71e831c37e4136c2754a59887be0618fb75336d7ab67e2982ff551ae"
val p2shInputScript = RawScriptSignatureParser.read(rawP2shInputScript)
val p2shInputScriptAsm = List(
OP_0,BytesToPushOntoStackImpl(71),
ScriptConstantImpl("304402207df6dd8dad22d49c3c83d8031733c32a53719278eb7985d3b35b375d776f84f102207054f9209a1e87d55feafc90aa04c33008e5bae9191da22aeaa16efde96f41f001"),