accounts_test.go raw

   1  package domain
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.smesh.lol/tinywallet/pkg/wallet"
   7  )
   8  
   9  // TestRestoredSeedFindsCoinsAtTheStandardAccount: the blake wallet derives new addresses from
  10  // account 1, but a seed restored from any other wallet has its coins at account 0. A sync that
  11  // searched only its own account would report "no coins" for a funded wallet, which is the one lie
  12  // a restore must never tell.
  13  func TestRestoredSeedFindsCoinsAtTheStandardAccount(t *testing.T) {
  14  	const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
  15  
  16  	blakeProfile, err := wallet.ProfileFor("blake", "regtest")
  17  
  18  	if err != nil {
  19  		t.Fatal(err)
  20  	}
  21  
  22  	if blakeProfile.Account != wallet.AccountBlake {
  23  		t.Fatalf("blake derives from account %d", blakeProfile.Account)
  24  	}
  25  
  26  	if blakeProfile.Account == wallet.AccountBTC {
  27  		t.Fatal("this test is pointless if blake and btc use one account")
  28  	}
  29  
  30  	derived, err := derive(phrase, blakeProfile)
  31  
  32  	if err != nil {
  33  		t.Fatal(err)
  34  	}
  35  
  36  	if err != nil {
  37  		t.Fatal(err)
  38  	}
  39  
  40  	standard, err := derived.ForAccount(wallet.AccountBTC)
  41  
  42  	if err != nil {
  43  		t.Fatal(err)
  44  	}
  45  
  46  	coin, err := standard.Address(false, 3)
  47  
  48  	if err != nil {
  49  		t.Fatal(err)
  50  	}
  51  
  52  	meta := Meta{Account: blakeProfile.Account, Receive: 4}
  53  	scripts, err := scriptsFor(derived, meta, 10, nil)
  54  
  55  	if err != nil {
  56  		t.Fatal(err)
  57  	}
  58  
  59  	found := false
  60  
  61  	for _, script := range scripts {
  62  		if string(script) == string(coin.Script()) {
  63  			found = true
  64  		}
  65  	}
  66  
  67  	if !found {
  68  		t.Fatal("the standard account's address is not searched, so its coins would be invisible")
  69  	}
  70  
  71  	// The spend path must know that script too, or the coin can be found but never signed.
  72  	keys, err := (&Domain{}).spendKeys(derived, meta)
  73  
  74  	if err != nil {
  75  		t.Fatal(err)
  76  	}
  77  
  78  	key, ok := keys[string(coin.Script())]
  79  
  80  	if !ok {
  81  		t.Fatal("the standard account's script has no spend key")
  82  	}
  83  
  84  	if key.account != wallet.AccountBTC {
  85  		t.Fatalf("spend key is for account %d", key.account)
  86  	}
  87  }
  88