segwit_test.go
TEXT FILE1.2 KB · Public source code for developers, search crawlers and AI agents
1package segwit
2
3import (
4 "bytes"
5 "encoding/hex"
6 "testing"
7)
8
9func TestPrograms(t *testing.T) {
10 h, _ := hex.DecodeString("751e76e8199196d454941c45d1b3a323f1433bd6")
11 p, e := P2WPKH(h)
12 if e != nil || hex.EncodeToString(p) != "0014751e76e8199196d454941c45d1b3a323f1433bd6" {
13 t.Fatal(p, e)
14 }
15 c, _ := P2WPKHScriptCode(h)
16 if hex.EncodeToString(c) != "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac" {
17 t.Fatal(c)
18 }
19 if _, e = P2WPKH(nil); e == nil {
20 t.Fatal("length")
21 }
22 s := []byte{0x51}
23 if !MatchesWitnessScript(P2WSH(s), s) || MatchesWitnessScript(P2WSH(s), []byte{0x52}) {
24 t.Fatal("commitment")
25 }
26}
27func TestWitness(t *testing.T) {
28 if !bytes.Equal(SerializeWitness([][]byte{nil, {1, 2}}), []byte{2, 0, 2, 1, 2}) {
29 t.Fatal("encoding")
30 }
31}
32func TestDigest(t *testing.T) {
33 tx := Tx{Version: 2, Inputs: []Input{{Sequence: 0xfffffffd}}, Outputs: []Output{{Value: 100, Script: []byte{0x51}}}}
34 a, e := SighashAll(tx, 0, []byte{0x51}, 200)
35 if e != nil {
36 t.Fatal(e)
37 }
38 b, _ := SighashAll(tx, 0, []byte{0x51}, 201)
39 if a == b {
40 t.Fatal("amount not committed")
41 }
42 tx.Outputs[0].Value++
43 b, _ = SighashAll(tx, 0, []byte{0x51}, 200)
44 if a == b {
45 t.Fatal("output not committed")
46 }
47 if _, e = SighashAll(tx, 1, nil, 200); e == nil {
48 t.Fatal("index")
49 }
50}Read-only source · Select a line number to link directly · Raw download above
