sign_test.go raw

   1  package domain
   2  
   3  import (
   4  	"errors"
   5  	"testing"
   6  
   7  	"git.smesh.lol/tinywallet/pkg/psbt"
   8  	"git.smesh.lol/tinywallet/pkg/store"
   9  	"git.smesh.lol/tinywallet/pkg/tx"
  10  	"git.smesh.lol/tinywallet/pkg/wallet"
  11  )
  12  
  13  const signPhrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
  14  
  15  // signDomain is a domain with one restored btc chain and a memory store.
  16  func signDomain(t *testing.T) (*Domain, string) {
  17  	t.Helper()
  18  
  19  	cache, err := store.OpenManual(&memPersistence{items: map[string][]byte{}})
  20  
  21  	if err != nil {
  22  		t.Fatal(err)
  23  	}
  24  
  25  	t.Cleanup(func() { _ = cache.Close() })
  26  
  27  	dom, err := New(cache)
  28  
  29  	if err != nil {
  30  		t.Fatal(err)
  31  	}
  32  
  33  	profile, err := wallet.ProfileFor("btc", "regtest")
  34  
  35  	if err != nil {
  36  		t.Fatal(err)
  37  	}
  38  
  39  	if err := dom.Add("btc", Link{Profile: profile, Client: &fakeChain{}}); err != nil {
  40  		t.Fatal(err)
  41  	}
  42  
  43  	created, err := dom.Restore("btc", signPhrase, "sign-test")
  44  
  45  	if err != nil {
  46  		t.Fatal(err)
  47  	}
  48  
  49  	return dom, created.Address.Text
  50  }
  51  
  52  // TestSignPSBTSignsTheWalletsOwnInput: the wallet finds its own script among the inputs, signs
  53  // it inside the policy, and refuses a policy that does not cover the spend.
  54  func TestSignPSBTSignsTheWalletsOwnInput(t *testing.T) {
  55  	dom, addressText := signDomain(t)
  56  
  57  	derived, err := dom.Addresses("btc", false, 0, 1)
  58  
  59  	if err != nil {
  60  		t.Fatal(err)
  61  	}
  62  
  63  	script := derived[0].Script()
  64  
  65  	transaction := tx.New()
  66  	transaction.AddInput(tx.Outpoint{Hash: [32]byte{5}, Index: 1}, 0xfffffffd)
  67  	transaction.AddOutput(60_000, []byte{0x00, 0x14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
  68  
  69  	unsigned, err := psbt.Write(transaction, []psbt.SegwitInput{{
  70  		Amount:       70_000,
  71  		ScriptPubKey: script,
  72  	}})
  73  
  74  	if err != nil {
  75  		t.Fatal(err)
  76  	}
  77  
  78  	signed, count, err := dom.SignPSBT("btc", unsigned, psbt.Policy{MaxSpend: 70_000, MaxFee: 10_000})
  79  
  80  	if err != nil {
  81  		t.Fatalf("SignPSBT: %v", err)
  82  	}
  83  
  84  	if count != 1 {
  85  		t.Fatalf("signed %d inputs, want 1 (address %s)", count, addressText)
  86  	}
  87  
  88  	if string(signed) == string(unsigned) {
  89  		t.Fatal("the signed PSBT is the unsigned one")
  90  	}
  91  
  92  	if _, _, err := dom.SignPSBT("btc", unsigned, psbt.Policy{MaxFee: 9_999}); !errors.Is(err, psbt.ErrPolicy) {
  93  		t.Fatalf("a fee over the limit: %v", err)
  94  	}
  95  
  96  	if _, _, err := dom.SignPSBT("btc", unsigned, psbt.Policy{MaxSpend: 69_999}); !errors.Is(err, psbt.ErrPolicy) {
  97  		t.Fatalf("a spend over the limit: %v", err)
  98  	}
  99  
 100  	if _, _, err := dom.SignPSBT("blake", unsigned, psbt.Policy{}); err == nil {
 101  		t.Fatal("a chain the wallet does not drive was accepted")
 102  	}
 103  }
 104