github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/wallet/seed_test.go (about)

     1  package wallet
     2  
     3  import (
     4  	"bytes"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/NebulousLabs/Sia/build"
     9  	"github.com/NebulousLabs/Sia/crypto"
    10  	"github.com/NebulousLabs/Sia/modules"
    11  	"github.com/NebulousLabs/Sia/types"
    12  )
    13  
    14  // TestPrimarySeed checks that the correct seed is returned when calling
    15  // PrimarySeed.
    16  func TestPrimarySeed(t *testing.T) {
    17  	if testing.Short() {
    18  		t.SkipNow()
    19  	}
    20  	// Start with a blank wallet tester.
    21  	wt, err := createBlankWalletTester("TestPrimarySeed")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	// Create a seed and unlock the wallet.
    27  	seed, err := wt.wallet.Encrypt(crypto.TwofishKey{})
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	err = wt.wallet.Unlock(crypto.TwofishKey(crypto.HashObject(seed)))
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	// Try getting an address, see that the seed advances correctly.
    37  	primarySeed, progress, err := wt.wallet.PrimarySeed()
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if !bytes.Equal(primarySeed[:], seed[:]) {
    42  		t.Error("PrimarySeed is returning a value inconsitent with the seed returned by Encrypt")
    43  	}
    44  	if progress != 0 {
    45  		t.Error("primary seed is returning the wrong progress")
    46  	}
    47  	_, err = wt.wallet.NextAddress()
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	_, progress, err = wt.wallet.PrimarySeed()
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if progress != 1 {
    56  		t.Error("primary seed is returning the wrong progress")
    57  	}
    58  
    59  	// Lock then unlock the wallet and check the responses.
    60  	err = wt.wallet.Lock()
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	_, _, err = wt.wallet.PrimarySeed()
    65  	if err != modules.ErrLockedWallet {
    66  		t.Error("unexpected err:", err)
    67  	}
    68  	err = wt.wallet.Unlock(crypto.TwofishKey(crypto.HashObject(seed)))
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	primarySeed, progress, err = wt.wallet.PrimarySeed()
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if !bytes.Equal(primarySeed[:], seed[:]) {
    77  		t.Error("PrimarySeed is returning a value inconsitent with the seed returned by Encrypt")
    78  	}
    79  	if progress != 1 {
    80  		t.Error("progress reporting an unexpected value")
    81  	}
    82  }
    83  
    84  // TestLoadSeed checks that a seed can be successfully recovered from a wallet,
    85  // and then remain available on subsequent loads of the wallet.
    86  func TestLoadSeed(t *testing.T) {
    87  	if testing.Short() {
    88  		t.SkipNow()
    89  	}
    90  	wt, err := createWalletTester("TestLoadSeed")
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	seed, _, err := wt.wallet.PrimarySeed()
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	allSeeds, err := wt.wallet.AllSeeds()
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	if len(allSeeds) != 1 {
   103  		t.Error("AllSeeds should be returning the primary seed.")
   104  	}
   105  	if allSeeds[0] != seed {
   106  		t.Error("AllSeeds returned the wrong seed")
   107  	}
   108  
   109  	dir := filepath.Join(build.TempDir(modules.WalletDir, "TestLoadSeed - 0"), modules.WalletDir)
   110  	w, err := New(wt.cs, wt.tpool, dir)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	newSeed, err := w.Encrypt(crypto.TwofishKey{})
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	err = w.Unlock(crypto.TwofishKey(crypto.HashObject(newSeed)))
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	// Balance of wallet should be 0.
   123  	siacoinBal, _, _ := w.ConfirmedBalance()
   124  	if siacoinBal.Cmp(types.NewCurrency64(0)) != 0 {
   125  		t.Error("fresh wallet should not have a balance")
   126  	}
   127  	err = w.LoadSeed(crypto.TwofishKey(crypto.HashObject(newSeed)), seed)
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  	allSeeds, err = w.AllSeeds()
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	if len(allSeeds) != 2 {
   136  		t.Error("AllSeeds should be returning the primary seed with the recovery seed.")
   137  	}
   138  	if !bytes.Equal(allSeeds[0][:], newSeed[:]) {
   139  		t.Error("AllSeeds returned the wrong seed")
   140  	}
   141  	if !bytes.Equal(allSeeds[1][:], seed[:]) {
   142  		t.Error("AllSeeds returned the wrong seed")
   143  	}
   144  
   145  	// Rather than worry about a rescan, which isn't implemented and has
   146  	// synchronization difficulties, just load a new wallet from the same
   147  	// settings file - the same effect is achieved without the difficulties.
   148  	w2, err := New(wt.cs, wt.tpool, dir)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  	err = w2.Unlock(crypto.TwofishKey(crypto.HashObject(newSeed)))
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  	siacoinBal2, _, _ := w2.ConfirmedBalance()
   157  	if siacoinBal2.Cmp(types.NewCurrency64(0)) <= 0 {
   158  		t.Error("wallet failed to load a seed with money in it")
   159  	}
   160  	allSeeds, err = w2.AllSeeds()
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   164  	if len(allSeeds) != 2 {
   165  		t.Error("AllSeeds should be returning the primary seed with the recovery seed.")
   166  	}
   167  	if !bytes.Equal(allSeeds[0][:], newSeed[:]) {
   168  		t.Error("AllSeeds returned the wrong seed")
   169  	}
   170  	if !bytes.Equal(allSeeds[1][:], seed[:]) {
   171  		t.Error("AllSeeds returned the wrong seed")
   172  	}
   173  }