|
| 1 | +package keymanager_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/prysmaticlabs/prysm/validator/keymanager" |
| 10 | + keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4" |
| 11 | + nd "github.com/wealdtech/go-eth2-wallet-nd" |
| 12 | + filesystem "github.com/wealdtech/go-eth2-wallet-store-filesystem" |
| 13 | +) |
| 14 | + |
| 15 | +func SetupWallet(t *testing.T) string { |
| 16 | + path, err := ioutil.TempDir("", "") |
| 17 | + if err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + store := filesystem.New(filesystem.WithLocation(path)) |
| 21 | + encryptor := keystorev4.New() |
| 22 | + |
| 23 | + // Create wallets with keys. |
| 24 | + w1, err := nd.CreateWallet("Wallet 1", store, encryptor) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("Failed to create wallet: %v", err) |
| 27 | + } |
| 28 | + err = w1.Unlock(nil) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Failed to unlock wallet: %v", err) |
| 31 | + } |
| 32 | + _, err = w1.CreateAccount("Account 1", []byte("foo")) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("Failed to create account 1: %v", err) |
| 35 | + } |
| 36 | + _, err = w1.CreateAccount("Account 2", []byte("bar")) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("Failed to create account 2: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + return path |
| 42 | +} |
| 43 | + |
| 44 | +func wallet(t *testing.T, opts string) keymanager.KeyManager { |
| 45 | + km, _, err := keymanager.NewWallet(opts) |
| 46 | + if err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + return km |
| 50 | +} |
| 51 | + |
| 52 | +func TestMultiplePassphrases(t *testing.T) { |
| 53 | + path := SetupWallet(t) |
| 54 | + defer os.RemoveAll(path) |
| 55 | + tests := []struct { |
| 56 | + name string |
| 57 | + wallet keymanager.KeyManager |
| 58 | + accounts int |
| 59 | + }{ |
| 60 | + { |
| 61 | + name: "Neither", |
| 62 | + wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["neither"]}`, path)), |
| 63 | + accounts: 0, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Foo", |
| 67 | + wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["foo"]}`, path)), |
| 68 | + accounts: 1, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Bar", |
| 72 | + wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["bar"]}`, path)), |
| 73 | + accounts: 1, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "Both", |
| 77 | + wallet: wallet(t, fmt.Sprintf(`{"location":%q,"accounts":["Wallet 1"],"passphrases":["foo","bar"]}`, path)), |
| 78 | + accounts: 2, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, test := range tests { |
| 83 | + t.Run(test.name, func(t *testing.T) { |
| 84 | + keys, err := test.wallet.FetchValidatingKeys() |
| 85 | + if err != nil { |
| 86 | + t.Error(err) |
| 87 | + } |
| 88 | + if len(keys) != test.accounts { |
| 89 | + t.Errorf("Found %d keys; expected %d", len(keys), test.accounts) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
0 commit comments