htlc.go raw
1 package psbt
2
3 import (
4 "crypto/sha256"
5 "errors"
6 "fmt"
7 )
8
9 // ErrNotContract is a witness script that is not a swap contract of this layout.
10 var ErrNotContract = errors.New("psbt: the witness script is not a swap contract")
11
12 // HTLC is a contract's terms, as its witness script states them.
13 type HTLC struct {
14 // Claimer and Refunder are the two compressed public keys, in script order.
15 Claimer []byte
16 Refunder []byte
17 // SecretHash is the SHA-256 the claim branch compares the preimage against.
18 SecretHash [32]byte
19 // Locktime is the refund branch's absolute locktime.
20 Locktime uint32
21 }
22
23 // ParseHTLC reads the terms out of a witness script this wallet compiles.
24 //
25 // It checks the whole shape rather than searching for public keys: a script that merely
26 // contains two of them is not this contract, and signing something else is how a wallet loses
27 // a coin. The layout is the one pkg/script pins to the node's own compilation.
28 func ParseHTLC(witness []byte) (HTLC, error) {
29 var terms HTLC
30
31 const pushKey = 0x21 // a 33-byte push
32
33 head := []byte{0x63, pushKey} // OP_IF <C>
34 middle := []byte{0xad, 0x82, 0x01, 0x20, 0x88, 0xa8, 0x20} // CHECKSIGVERIFY SIZE 1 32 EQUALVERIFY SHA256
35 mark := []byte{0x87, 0x67} // EQUAL ELSE
36 after := []byte{0xb1, 0x69, pushKey} // CLTV VERIFY <R>
37 tail := []byte{0xac, 0x68} // CHECKSIG ENDIF
38
39 if len(witness) < len(head)+33+len(middle)+32+len(mark)+1+len(after)+33+len(tail) {
40 return terms, ErrNotContract
41 }
42
43 at := 0
44
45 next := func(want []byte) bool {
46 if at+len(want) > len(witness) || string(witness[at:at+len(want)]) != string(want) {
47 return false
48 }
49
50 at += len(want)
51
52 return true
53 }
54
55 if !next(head) {
56 return terms, ErrNotContract
57 }
58
59 terms.Claimer = append([]byte(nil), witness[at:at+33]...)
60 at += 33
61
62 if !next(middle) {
63 return terms, ErrNotContract
64 }
65
66 copy(terms.SecretHash[:], witness[at:at+32])
67 at += 32
68
69 if !next(mark) {
70 return terms, ErrNotContract
71 }
72
73 // A script number is signed, so a locktime whose top byte has its high bit set carries a
74 // zero pad: up to five bytes for a 32-bit value.
75 if at >= len(witness) || witness[at] < 1 || witness[at] > 5 {
76 return terms, ErrNotContract
77 }
78
79 length := int(witness[at])
80 at++
81
82 if at+length > len(witness) {
83 return terms, ErrNotContract
84 }
85
86 raw := witness[at : at+length]
87
88 if length == 5 {
89 if raw[4] != 0x00 || raw[3]&0x80 == 0 {
90 return terms, ErrNotContract
91 }
92
93 raw = raw[:4]
94 }
95
96 var lock [4]byte
97 copy(lock[:], raw)
98
99 terms.Locktime = binaryLittleUint32(lock[:])
100 at += length
101
102 if !next(after) {
103 return terms, ErrNotContract
104 }
105
106 terms.Refunder = append([]byte(nil), witness[at:at+33]...)
107 at += 33
108
109 if at+len(tail) != len(witness) || !next(tail) {
110 return terms, ErrNotContract
111 }
112
113 return terms, nil
114 }
115
116 // binaryLittleUint32 reads a little-endian 32-bit number from four bytes.
117 func binaryLittleUint32(raw []byte) uint32 {
118 return uint32(raw[0]) | uint32(raw[1])<<8 | uint32(raw[2])<<16 | uint32(raw[3])<<24
119 }
120
121 // inputFinalWitness is BIP174's final scriptWitness record.
122 const inputFinalWitness byte = 0x08
123
124 // p2wsh is the output script a witness script pays to.
125 func p2wsh(witness []byte) []byte {
126 sum := sha256.Sum256(witness)
127
128 out := make([]byte, 0, 34)
129 out = append(out, 0x00, 0x20)
130
131 return append(out, sum[:]...)
132 }
133
134 // opensContract returns the preimage that opens secretHash, or nil.
135 func opensContract(preimages []Preimage, secretHash [32]byte) []byte {
136 for _, preimage := range preimages {
137 if preimage.SecretHash == secretHash && bytesEqual(sha256Of(preimage.Preimage), secretHash[:]) {
138 return preimage.Preimage
139 }
140 }
141
142 return nil
143 }
144
145 // witnessRecord serialises a witness stack the way BIP174 carries a final scriptWitness: a
146 // count, then each item length-prefixed.
147 func witnessRecord(stack [][]byte) ([]byte, error) {
148 if len(stack) == 0 {
149 return nil, fmt.Errorf("psbt: an empty witness")
150 }
151
152 out := compactSize(len(stack))
153
154 for _, item := range stack {
155 if len(item) > 0xffff {
156 return nil, fmt.Errorf("psbt: a %d-byte witness item", len(item))
157 }
158
159 out = append(out, compactSize(len(item))...)
160 out = append(out, item...)
161 }
162
163 return out, nil
164 }
165
166 // bytesEqual compares two byte strings.
167 func bytesEqual(a, b []byte) bool {
168 if len(a) != len(b) {
169 return false
170 }
171
172 for i := range a {
173 if a[i] != b[i] {
174 return false
175 }
176 }
177
178 return true
179 }
180