package segwit import ( "bytes" "encoding/hex" "testing" ) func TestPrograms(t *testing.T) { h, _ := hex.DecodeString("751e76e8199196d454941c45d1b3a323f1433bd6") p, e := P2WPKH(h) if e != nil || hex.EncodeToString(p) != "0014751e76e8199196d454941c45d1b3a323f1433bd6" { t.Fatal(p, e) } c, _ := P2WPKHScriptCode(h) if hex.EncodeToString(c) != "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac" { t.Fatal(c) } if _, e = P2WPKH(nil); e == nil { t.Fatal("length") } s := []byte{0x51} if !MatchesWitnessScript(P2WSH(s), s) || MatchesWitnessScript(P2WSH(s), []byte{0x52}) { t.Fatal("commitment") } } func TestWitness(t *testing.T) { if !bytes.Equal(SerializeWitness([][]byte{nil, {1, 2}}), []byte{2, 0, 2, 1, 2}) { t.Fatal("encoding") } } func TestDigest(t *testing.T) { tx := Tx{Version: 2, Inputs: []Input{{Sequence: 0xfffffffd}}, Outputs: []Output{{Value: 100, Script: []byte{0x51}}}} a, e := SighashAll(tx, 0, []byte{0x51}, 200) if e != nil { t.Fatal(e) } b, _ := SighashAll(tx, 0, []byte{0x51}, 201) if a == b { t.Fatal("amount not committed") } tx.Outputs[0].Value++ b, _ = SighashAll(tx, 0, []byte{0x51}, 200) if a == b { t.Fatal("output not committed") } if _, e = SighashAll(tx, 1, nil, 200); e == nil { t.Fatal("index") } }