final.go raw
1 package psbt
2
3 import (
4 "bytes"
5 "fmt"
6 )
7
8 // FinalWitness returns the final scriptWitness of the first input that carries one.
9 //
10 // A local wallet finalises its own contract inputs, so the witness is the answer a caller
11 // needs: it is applied to the unsigned transaction before the transaction is broadcast.
12 func FinalWitness(raw []byte) ([][]byte, error) {
13 if !bytes.HasPrefix(raw, magic) {
14 return nil, ErrNotPSBT
15 }
16
17 reader := &cursor{raw: raw, at: len(magic)}
18
19 // The global map is skipped: only input maps can carry a final witness.
20 for {
21 key, _, err := reader.record()
22
23 if err != nil {
24 return nil, err
25 }
26
27 if key == nil {
28 break
29 }
30 }
31
32 for {
33 key, value, err := reader.record()
34
35 if err != nil {
36 return nil, err
37 }
38
39 if key == nil {
40 // An input map ended without a final witness; try the next one.
41 if !reader.more() {
42 break
43 }
44
45 continue
46 }
47
48 if len(key) == 1 && key[0] == inputFinalWitness {
49 return decodeWitness(value)
50 }
51 }
52
53 return nil, fmt.Errorf("psbt: no final script witness")
54 }
55
56 // decodeWitness reads a BIP174 witness stack.
57 func decodeWitness(value []byte) ([][]byte, error) {
58 c := &witnessCursor{raw: value}
59
60 count, err := c.compactSize()
61
62 if err != nil {
63 return nil, err
64 }
65
66 if count == 0 || count > 16 {
67 return nil, fmt.Errorf("psbt: a %d-item witness", count)
68 }
69
70 items := make([][]byte, 0, count)
71
72 for i := 0; i < count; i++ {
73 item, err := c.bytes()
74
75 if err != nil {
76 return nil, err
77 }
78
79 items = append(items, item)
80 }
81
82 if c.at != len(value) {
83 return nil, fmt.Errorf("psbt: %d trailing bytes after a witness", len(value)-c.at)
84 }
85
86 return items, nil
87 }
88
89 // witnessCursor reads a witness stack, separate from the PSBT map cursor.
90 type witnessCursor struct {
91 raw []byte
92 at int
93 }
94
95 func (c *witnessCursor) u8() (byte, error) {
96 if c.at >= len(c.raw) {
97 return 0, fmt.Errorf("psbt: a witness ends inside a value")
98 }
99
100 b := c.raw[c.at]
101 c.at++
102
103 return b, nil
104 }
105
106 func (c *witnessCursor) compactSize() (int, error) {
107 first, err := c.u8()
108
109 if err != nil {
110 return 0, err
111 }
112
113 switch {
114 case first < 0xfd:
115 return int(first), nil
116 case first == 0xfd:
117 if c.at+2 > len(c.raw) {
118 return 0, fmt.Errorf("psbt: a short witness length")
119 }
120
121 value := int(c.raw[c.at]) | int(c.raw[c.at+1])<<8
122 c.at += 2
123
124 return value, nil
125 default:
126 return 0, fmt.Errorf("psbt: a witness length past 0xfd is not a witness")
127 }
128 }
129
130 func (c *witnessCursor) bytes() ([]byte, error) {
131 length, err := c.compactSize()
132
133 if err != nil {
134 return nil, err
135 }
136
137 if c.at+length > len(c.raw) {
138 return nil, fmt.Errorf("psbt: a witness item runs past the stack")
139 }
140
141 out := append([]byte(nil), c.raw[c.at:c.at+length]...)
142 c.at += length
143
144 return out, nil
145 }
146