sign_test.go raw
1 package psbt
2
3 import (
4 "encoding/hex"
5 "errors"
6 "testing"
7
8 "github.com/btcsuite/btcd/btcec/v2"
9 btcecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa"
10
11 "git.smesh.lol/tinywallet/pkg/mnemonic"
12 "git.smesh.lol/tinywallet/pkg/tx"
13 "git.smesh.lol/tinywallet/pkg/wallet"
14 )
15
16 const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
17
18 // walletKey derives the wallet's first receive address and the key that spends it.
19 func walletKey(t *testing.T) (wallet.Address, Key) {
20 t.Helper()
21
22 seed, err := mnemonic.Seed(phrase, "")
23
24 if err != nil {
25 t.Fatal(err)
26 }
27
28 profile, err := wallet.ProfileFor("btc", "regtest")
29
30 if err != nil {
31 t.Fatal(err)
32 }
33
34 w, err := wallet.New(seed, profile)
35
36 if err != nil {
37 t.Fatal(err)
38 }
39
40 address, err := w.Address(false, 0)
41
42 if err != nil {
43 t.Fatal(err)
44 }
45
46 secret, err := w.Secret(false, 0)
47
48 if err != nil {
49 t.Fatal(err)
50 }
51
52 return address, Key{Secret: secret, Program: address.Program}
53 }
54
55 // unsignedPSBT describes a spend of the wallet's own output.
56 func unsignedPSBT(t *testing.T, address wallet.Address, amount, pay uint64) []byte {
57 t.Helper()
58
59 transaction := tx.New()
60 transaction.AddInput(tx.Outpoint{Hash: [32]byte{9}, Index: 0}, 0xfffffffd)
61 transaction.AddOutput(pay, []byte{0x00, 0x14, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7})
62
63 raw, err := Write(transaction, []SegwitInput{{
64 Amount: amount,
65 ScriptPubKey: address.Script(),
66 }})
67
68 if err != nil {
69 t.Fatalf("Write: %v", err)
70 }
71
72 return raw
73 }
74
75 // partialSig returns the first partial signature on input 0, keyed by pubkey.
76 func partialSig(t *testing.T, raw []byte) ([]byte, []byte) {
77 t.Helper()
78
79 file, err := Read(raw)
80
81 if err != nil {
82 t.Fatalf("Read signed psbt: %v", err)
83 }
84
85 // Read does not keep partial sigs; find the record by scanning for key 0x02.
86 for at := 0; at+2 < len(raw); at++ {
87 if raw[at] == 0x22 && raw[at+1] == inputPartialSig {
88 pubkey := raw[at+2 : at+2+33]
89 length, next := raw[at+35], at+36
90 value := raw[next : next+int(length)]
91
92 _ = file
93
94 return pubkey, value
95 }
96 }
97
98 t.Fatalf("no partial signature record in %x", raw)
99
100 return nil, nil
101 }
102
103 // TestSignP2WPKHInput: the wallet's own input is signed, the signature verifies over the BIP143
104 // digest the transaction defines, and the witness program matches the key that signed.
105 func TestSignP2WPKHInput(t *testing.T) {
106 address, key := walletKey(t)
107 raw := unsignedPSBT(t, address, 100_000, 90_000)
108
109 signed, count, err := Sign(raw, map[string]Key{hex.EncodeToString(address.Script()): key}, Policy{
110 MaxSpend: 100_000,
111 MaxFee: 10_000,
112 })
113
114 if err != nil {
115 t.Fatalf("Sign: %v", err)
116 }
117
118 if count != 1 {
119 t.Fatalf("signed %d inputs, want 1", count)
120 }
121
122 pubkey, signature := partialSig(t, signed)
123
124 if len(signature) < 9 || signature[len(signature)-1] != byte(tx.SighashAll) {
125 t.Fatalf("signature is not DER plus SIGHASH_ALL: %x", signature)
126 }
127
128 parsed, err := btcec.ParsePubKey(pubkey)
129
130 if err != nil {
131 t.Fatalf("pubkey: %v", err)
132 }
133
134 if !bytesEqual(parsed.SerializeCompressed(), address.Pubkey) {
135 t.Fatal("the partial signature is keyed by a different public key")
136 }
137
138 transaction := tx.New()
139 transaction.AddInput(tx.Outpoint{Hash: [32]byte{9}, Index: 0}, 0xfffffffd)
140 transaction.AddOutput(90_000, []byte{0x00, 0x14, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7})
141
142 digest, err := transaction.SighashV0(0, tx.P2WPKHScriptCode(key.Program), 100_000, tx.SighashAll)
143
144 if err != nil {
145 t.Fatal(err)
146 }
147
148 parsedSig, err := btcecdsa.ParseDERSignature(signature[:len(signature)-1])
149
150 if err != nil {
151 t.Fatalf("parse signature: %v", err)
152 }
153
154 if !parsedSig.Verify(digest[:], parsed) {
155 t.Fatal("the partial signature does not verify over the digest")
156 }
157 }
158
159 // TestSignRefusesWhatThePolicyForbids: a spend, a fee, or a PSBT with nothing of the wallet's
160 // in it is refused, and the refusals are distinct.
161 func TestSignRefusesWhatThePolicyForbids(t *testing.T) {
162 address, key := walletKey(t)
163 owned := map[string]Key{hex.EncodeToString(address.Script()): key}
164 raw := unsignedPSBT(t, address, 100_000, 90_000)
165
166 if _, _, err := Sign(raw, map[string]Key{}, Policy{}); !errors.Is(err, ErrNoOwnedInput) {
167 t.Fatalf("no owned input: %v", err)
168 }
169
170 if _, _, err := Sign(raw, owned, Policy{MaxSpend: 99_999}); !errors.Is(err, ErrPolicy) {
171 t.Fatalf("spend over the limit: %v", err)
172 }
173
174 if _, _, err := Sign(raw, owned, Policy{MaxFee: 9_999}); !errors.Is(err, ErrPolicy) {
175 t.Fatalf("fee over the limit: %v", err)
176 }
177
178 // The limits are inclusive.
179 if _, _, err := Sign(raw, owned, Policy{MaxSpend: 100_000, MaxFee: 10_000}); err != nil {
180 t.Fatalf("a spend exactly at the limits: %v", err)
181 }
182 }
183