package psbt import ( "encoding/hex" "errors" "testing" "github.com/btcsuite/btcd/btcec/v2" btcecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa" "git.smesh.lol/tinywallet/pkg/mnemonic" "git.smesh.lol/tinywallet/pkg/tx" "git.smesh.lol/tinywallet/pkg/wallet" ) const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" // walletKey derives the wallet's first receive address and the key that spends it. func walletKey(t *testing.T) (wallet.Address, Key) { t.Helper() seed, err := mnemonic.Seed(phrase, "") if err != nil { t.Fatal(err) } profile, err := wallet.ProfileFor("btc", "regtest") if err != nil { t.Fatal(err) } w, err := wallet.New(seed, profile) if err != nil { t.Fatal(err) } address, err := w.Address(false, 0) if err != nil { t.Fatal(err) } secret, err := w.Secret(false, 0) if err != nil { t.Fatal(err) } return address, Key{Secret: secret, Program: address.Program} } // unsignedPSBT describes a spend of the wallet's own output. func unsignedPSBT(t *testing.T, address wallet.Address, amount, pay uint64) []byte { t.Helper() transaction := tx.New() transaction.AddInput(tx.Outpoint{Hash: [32]byte{9}, Index: 0}, 0xfffffffd) 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}) raw, err := Write(transaction, []SegwitInput{{ Amount: amount, ScriptPubKey: address.Script(), }}) if err != nil { t.Fatalf("Write: %v", err) } return raw } // partialSig returns the first partial signature on input 0, keyed by pubkey. func partialSig(t *testing.T, raw []byte) ([]byte, []byte) { t.Helper() file, err := Read(raw) if err != nil { t.Fatalf("Read signed psbt: %v", err) } // Read does not keep partial sigs; find the record by scanning for key 0x02. for at := 0; at+2 < len(raw); at++ { if raw[at] == 0x22 && raw[at+1] == inputPartialSig { pubkey := raw[at+2 : at+2+33] length, next := raw[at+35], at+36 value := raw[next : next+int(length)] _ = file return pubkey, value } } t.Fatalf("no partial signature record in %x", raw) return nil, nil } // TestSignP2WPKHInput: the wallet's own input is signed, the signature verifies over the BIP143 // digest the transaction defines, and the witness program matches the key that signed. func TestSignP2WPKHInput(t *testing.T) { address, key := walletKey(t) raw := unsignedPSBT(t, address, 100_000, 90_000) signed, count, err := Sign(raw, map[string]Key{hex.EncodeToString(address.Script()): key}, Policy{ MaxSpend: 100_000, MaxFee: 10_000, }) if err != nil { t.Fatalf("Sign: %v", err) } if count != 1 { t.Fatalf("signed %d inputs, want 1", count) } pubkey, signature := partialSig(t, signed) if len(signature) < 9 || signature[len(signature)-1] != byte(tx.SighashAll) { t.Fatalf("signature is not DER plus SIGHASH_ALL: %x", signature) } parsed, err := btcec.ParsePubKey(pubkey) if err != nil { t.Fatalf("pubkey: %v", err) } if !bytesEqual(parsed.SerializeCompressed(), address.Pubkey) { t.Fatal("the partial signature is keyed by a different public key") } transaction := tx.New() transaction.AddInput(tx.Outpoint{Hash: [32]byte{9}, Index: 0}, 0xfffffffd) 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}) digest, err := transaction.SighashV0(0, tx.P2WPKHScriptCode(key.Program), 100_000, tx.SighashAll) if err != nil { t.Fatal(err) } parsedSig, err := btcecdsa.ParseDERSignature(signature[:len(signature)-1]) if err != nil { t.Fatalf("parse signature: %v", err) } if !parsedSig.Verify(digest[:], parsed) { t.Fatal("the partial signature does not verify over the digest") } } // TestSignRefusesWhatThePolicyForbids: a spend, a fee, or a PSBT with nothing of the wallet's // in it is refused, and the refusals are distinct. func TestSignRefusesWhatThePolicyForbids(t *testing.T) { address, key := walletKey(t) owned := map[string]Key{hex.EncodeToString(address.Script()): key} raw := unsignedPSBT(t, address, 100_000, 90_000) if _, _, err := Sign(raw, map[string]Key{}, Policy{}); !errors.Is(err, ErrNoOwnedInput) { t.Fatalf("no owned input: %v", err) } if _, _, err := Sign(raw, owned, Policy{MaxSpend: 99_999}); !errors.Is(err, ErrPolicy) { t.Fatalf("spend over the limit: %v", err) } if _, _, err := Sign(raw, owned, Policy{MaxFee: 9_999}); !errors.Is(err, ErrPolicy) { t.Fatalf("fee over the limit: %v", err) } // The limits are inclusive. if _, _, err := Sign(raw, owned, Policy{MaxSpend: 100_000, MaxFee: 10_000}); err != nil { t.Fatalf("a spend exactly at the limits: %v", err) } }