Merge pull request #38 from Christewart/master

Adding helper function to P2PKHScriptPubKey
This commit is contained in:
Thomas McCabe 2016-09-23 10:38:26 -05:00 committed by GitHub
commit 3114b0a2c5
3 changed files with 26 additions and 1 deletions

View File

@ -50,6 +50,10 @@ object P2PKHScriptPubKey extends Factory[P2PKHScriptPubKey] {
def apply(pubKey : ECPublicKey) : P2PKHScriptPubKey = {
val hash = CryptoUtil.sha256Hash160(pubKey.bytes)
P2PKHScriptPubKey(hash)
}
def apply(hash: Sha256Hash160Digest): P2PKHScriptPubKey = {
val pushOps = BitcoinScriptUtil.calculatePushOp(hash.bytes)
val asm = Seq(OP_DUP, OP_HASH160) ++ pushOps ++ Seq(ScriptConstant(hash.bytes), OP_EQUALVERIFY, OP_CHECKSIG)
P2PKHScriptPubKey(asm)

View File

@ -1,6 +1,6 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.core.gen.ScriptGenerators
import org.bitcoins.core.gen.{CryptoGenerators, ScriptGenerators}
import org.scalacheck.{Prop, Properties}
/**
@ -11,6 +11,11 @@ class P2PKHScriptPubKeySpec extends Properties("P2PKHScriptPubKeySpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.p2pkhScriptPubKey) { case (p2pkhScriptPubKey, _) =>
P2PKHScriptPubKey(p2pkhScriptPubKey.hex) == p2pkhScriptPubKey
}
property("find pubkeyhash in scriptPubKey") =
Prop.forAll(CryptoGenerators.sha256Hash160Digest) { hash =>
val scriptPubKey = P2PKHScriptPubKey(hash)
scriptPubKey.pubKeyHash == hash
}
}

View File

@ -0,0 +1,16 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.core.gen.CryptoGenerators
import org.scalatest.{FlatSpec, MustMatchers}
/**
* Created by chris on 9/22/16.
*/
class P2PKHScriptPubKeyTest extends FlatSpec with MustMatchers {
"P2PKHScriptPubKey" must "return the pubkeyhash" in {
val hash = CryptoGenerators.sha256Hash160Digest.sample.get
val p2pkhScriptPubKey = P2PKHScriptPubKey(hash)
p2pkhScriptPubKey.pubKeyHash must be (hash)
}
}