package tx import ( "encoding/hex" "testing" ) // The published BIP143 native P2WPKH example, from bip-0143.mediawiki. The digest below is the // specification's own, so this pins the preimage rather than checking it against itself. const ( bip143Hash = "c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670" // The second input's witness program is 1d0f172a…a1, which is what the scriptCode wraps. bip143Program = "1d0f172a0ecb48aee1be1f2687d2963ae33f71a1" // The first input's outpoint, and both inputs' outpoints in order. bip143Prev1 = "fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f" bip143Prev2 = "ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a" ) // TestThePublishedBIP143Vector: the digest this package computes is the specification's. func TestThePublishedBIP143Vector(t *testing.T) { transaction := New() transaction.Version = 1 for _, prev := range []struct { txid string index uint32 sequence uint32 }{ {bip143Prev1, 0, 0xffffffee}, {bip143Prev2, 1, 0xffffffff}, } { hash, err := hex.DecodeString(prev.txid) if err != nil { t.Fatal(err) } var outpoint [32]byte copy(outpoint[:], hash) transaction.AddInput(Outpoint{Hash: outpoint, Index: prev.index}, prev.sequence) } for _, output := range []struct { value uint64 script string }{ {112340000, "76a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac"}, {223450000, "76a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac"}, } { script, err := hex.DecodeString(output.script) if err != nil { t.Fatal(err) } transaction.AddOutput(output.value, script) } transaction.LockTime = 17 programRaw, err := hex.DecodeString(bip143Program) if err != nil { t.Fatal(err) } var program [20]byte copy(program[:], programRaw) // The second input is the P2WPKH one, and it spends 6 BTC. digest, err := transaction.SighashV0(1, P2WPKHScriptCode(program), 600000000, SighashAll) if err != nil { t.Fatal(err) } if got := hex.EncodeToString(digest[:]); got != bip143Hash { t.Fatalf("the digest is %s, the specification says %s", got, bip143Hash) } }