htlc_sign_test.go raw
1 package domain
2
3 import (
4 "crypto/sha256"
5 "errors"
6 "testing"
7
8 "github.com/btcsuite/btcd/btcec/v2"
9
10 "git.smesh.lol/tinywallet/pkg/psbt"
11 "git.smesh.lol/tinywallet/pkg/tx"
12 )
13
14 // TestSignPSBTSpendsAContract: a contract whose claimer is the wallet's own pubkey is spent by
15 // the wallet, with the preimage, without any key being passed in. This is the settlement path:
16 // the engine's contract names the wallet's key, so the wallet signs.
17 func TestSignPSBTSpendsAContract(t *testing.T) {
18 dom, _ := signDomain(t)
19
20 addresses, err := dom.Addresses("btc", false, 0, 1)
21
22 if err != nil {
23 t.Fatal(err)
24 }
25
26 refundKey, _ := btcec.PrivKeyFromBytes([]byte{31: 0x09})
27
28 preimage := claimPreimage()
29 sum := sha256.Sum256(preimage)
30
31 address, witness, _, err := dom.DeriveHTLC(
32 "btc", addresses[0].Pubkey, refundKey.PubKey().SerializeCompressed(), sum, 500,
33 )
34
35 if err != nil {
36 t.Fatalf("DeriveHTLC: %v", err)
37 }
38
39 if address == "" || len(witness) == 0 {
40 t.Fatal("the contract is empty")
41 }
42
43 transaction := tx.New()
44 transaction.AddInput(tx.Outpoint{Hash: [32]byte{4}, Index: 0}, 0xfffffffe)
45 transaction.AddOutput(60_000, []byte{0x00, 0x14, 2, 2, 2, 2})
46
47 raw, err := psbt.Write(transaction, []psbt.SegwitInput{{
48 Amount: 70_000,
49 ScriptPubKey: p2wsh(witness),
50 WitnessScript: witness,
51 Preimage: preimage,
52 SecretHash: sum,
53 }})
54
55 if err != nil {
56 t.Fatalf("Write: %v", err)
57 }
58
59 signed, count, err := dom.SignPSBT("btc", raw, psbt.Policy{MaxSpend: 70_000, MaxFee: 10_000})
60
61 if err != nil {
62 t.Fatalf("SignPSBT: %v", err)
63 }
64
65 if count != 1 {
66 t.Fatalf("signed %d inputs", count)
67 }
68
69 if string(signed) == string(raw) {
70 t.Fatal("the signed PSBT is the unsigned one")
71 }
72
73 // The wallet can reach the contract because its pubkey is the claim key, and it refused to
74 // sign without the preimage, which only the claim (or the counterparty) holds.
75 without := tx.New()
76 without.AddInput(tx.Outpoint{Hash: [32]byte{4}, Index: 0}, 0xfffffffe)
77 without.AddOutput(60_000, []byte{0x00, 0x14, 2, 2, 2, 2})
78
79 noPreimage, err := psbt.Write(without, []psbt.SegwitInput{{
80 Amount: 70_000,
81 ScriptPubKey: p2wsh(witness),
82 WitnessScript: witness,
83 }})
84
85 if err != nil {
86 t.Fatal(err)
87 }
88
89 if _, _, err := dom.SignPSBT("btc", noPreimage, psbt.Policy{}); err == nil {
90 t.Fatal("a claim without the preimage was signed")
91 }
92 }
93
94 // TestAShortPreimageIsRefused: the contract's claim branch is OP_SIZE 32, so a shorter preimage
95 // can never open it. Signing it anyway produces a transaction every node rejects, and the wallet
96 // refuses instead — the shape of the failure the rig found.
97 func TestAShortPreimageIsRefused(t *testing.T) {
98 dom, _ := signDomain(t)
99
100 addresses, err := dom.Addresses("btc", false, 0, 1)
101
102 if err != nil {
103 t.Fatal(err)
104 }
105
106 refundKey, _ := btcec.PrivKeyFromBytes([]byte{31: 0x0a})
107
108 short := []byte("the secret the claim reveals")
109 sum := sha256.Sum256(short)
110
111 _, witness, _, err := dom.DeriveHTLC(
112 "btc", addresses[0].Pubkey, refundKey.PubKey().SerializeCompressed(), sum, 500,
113 )
114
115 if err != nil {
116 t.Fatalf("DeriveHTLC: %v", err)
117 }
118
119 transaction := tx.New()
120 transaction.AddInput(tx.Outpoint{Hash: [32]byte{5}, Index: 0}, 0xfffffffe)
121 transaction.AddOutput(60_000, []byte{0x00, 0x14, 2, 2, 2, 2})
122
123 raw, err := psbt.Write(transaction, []psbt.SegwitInput{{
124 Amount: 70_000,
125 ScriptPubKey: p2wsh(witness),
126 WitnessScript: witness,
127 Preimage: short,
128 SecretHash: sum,
129 }})
130
131 if err != nil {
132 t.Fatalf("Write: %v", err)
133 }
134
135 // The preimage opens the hash, so the refusal is about its length alone.
136 if _, _, err := dom.SignPSBT("btc", raw, psbt.Policy{}); !errors.Is(err, tx.ErrPreimage) {
137 t.Fatalf("a %d-byte claim preimage gave %v", len(short), err)
138 }
139 }
140
141 // claimPreimage is a 32-byte secret, which is what the contract's OP_SIZE check requires.
142 func claimPreimage() []byte {
143 preimage := make([]byte, 32)
144 copy(preimage, []byte("the secret the claim reveals"))
145
146 return preimage
147 }
148
149 // p2wsh is the output script a witness script pays to.
150 func p2wsh(witness []byte) []byte {
151 sum := sha256.Sum256(witness)
152
153 out := make([]byte, 0, 34)
154 out = append(out, 0x00, 0x20)
155
156 return append(out, sum[:]...)
157 }
158