package domain import ( "crypto/sha256" "errors" "testing" "github.com/btcsuite/btcd/btcec/v2" "git.smesh.lol/tinywallet/pkg/psbt" "git.smesh.lol/tinywallet/pkg/tx" ) // TestSignPSBTSpendsAContract: a contract whose claimer is the wallet's own pubkey is spent by // the wallet, with the preimage, without any key being passed in. This is the settlement path: // the engine's contract names the wallet's key, so the wallet signs. func TestSignPSBTSpendsAContract(t *testing.T) { dom, _ := signDomain(t) addresses, err := dom.Addresses("btc", false, 0, 1) if err != nil { t.Fatal(err) } refundKey, _ := btcec.PrivKeyFromBytes([]byte{31: 0x09}) preimage := claimPreimage() sum := sha256.Sum256(preimage) address, witness, _, err := dom.DeriveHTLC( "btc", addresses[0].Pubkey, refundKey.PubKey().SerializeCompressed(), sum, 500, ) if err != nil { t.Fatalf("DeriveHTLC: %v", err) } if address == "" || len(witness) == 0 { t.Fatal("the contract is empty") } transaction := tx.New() transaction.AddInput(tx.Outpoint{Hash: [32]byte{4}, Index: 0}, 0xfffffffe) transaction.AddOutput(60_000, []byte{0x00, 0x14, 2, 2, 2, 2}) raw, err := psbt.Write(transaction, []psbt.SegwitInput{{ Amount: 70_000, ScriptPubKey: p2wsh(witness), WitnessScript: witness, Preimage: preimage, SecretHash: sum, }}) if err != nil { t.Fatalf("Write: %v", err) } signed, count, err := dom.SignPSBT("btc", raw, psbt.Policy{MaxSpend: 70_000, MaxFee: 10_000}) if err != nil { t.Fatalf("SignPSBT: %v", err) } if count != 1 { t.Fatalf("signed %d inputs", count) } if string(signed) == string(raw) { t.Fatal("the signed PSBT is the unsigned one") } // The wallet can reach the contract because its pubkey is the claim key, and it refused to // sign without the preimage, which only the claim (or the counterparty) holds. without := tx.New() without.AddInput(tx.Outpoint{Hash: [32]byte{4}, Index: 0}, 0xfffffffe) without.AddOutput(60_000, []byte{0x00, 0x14, 2, 2, 2, 2}) noPreimage, err := psbt.Write(without, []psbt.SegwitInput{{ Amount: 70_000, ScriptPubKey: p2wsh(witness), WitnessScript: witness, }}) if err != nil { t.Fatal(err) } if _, _, err := dom.SignPSBT("btc", noPreimage, psbt.Policy{}); err == nil { t.Fatal("a claim without the preimage was signed") } } // TestAShortPreimageIsRefused: the contract's claim branch is OP_SIZE 32, so a shorter preimage // can never open it. Signing it anyway produces a transaction every node rejects, and the wallet // refuses instead — the shape of the failure the rig found. func TestAShortPreimageIsRefused(t *testing.T) { dom, _ := signDomain(t) addresses, err := dom.Addresses("btc", false, 0, 1) if err != nil { t.Fatal(err) } refundKey, _ := btcec.PrivKeyFromBytes([]byte{31: 0x0a}) short := []byte("the secret the claim reveals") sum := sha256.Sum256(short) _, witness, _, err := dom.DeriveHTLC( "btc", addresses[0].Pubkey, refundKey.PubKey().SerializeCompressed(), sum, 500, ) if err != nil { t.Fatalf("DeriveHTLC: %v", err) } transaction := tx.New() transaction.AddInput(tx.Outpoint{Hash: [32]byte{5}, Index: 0}, 0xfffffffe) transaction.AddOutput(60_000, []byte{0x00, 0x14, 2, 2, 2, 2}) raw, err := psbt.Write(transaction, []psbt.SegwitInput{{ Amount: 70_000, ScriptPubKey: p2wsh(witness), WitnessScript: witness, Preimage: short, SecretHash: sum, }}) if err != nil { t.Fatalf("Write: %v", err) } // The preimage opens the hash, so the refusal is about its length alone. if _, _, err := dom.SignPSBT("btc", raw, psbt.Policy{}); !errors.Is(err, tx.ErrPreimage) { t.Fatalf("a %d-byte claim preimage gave %v", len(short), err) } } // claimPreimage is a 32-byte secret, which is what the contract's OP_SIZE check requires. func claimPreimage() []byte { preimage := make([]byte, 32) copy(preimage, []byte("the secret the claim reveals")) return preimage } // p2wsh is the output script a witness script pays to. func p2wsh(witness []byte) []byte { sum := sha256.Sum256(witness) out := make([]byte, 0, 34) out = append(out, 0x00, 0x20) return append(out, sum[:]...) }