Skip to content

Commit 02afb53

Browse files
authored
Remove suprious error messages in wallet keymanager (#5090)
* Handle multiple passphrases * Add tests
1 parent 0974c02 commit 02afb53

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

validator/keymanager/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ go_test(
3434
"direct_interop_test.go",
3535
"direct_test.go",
3636
"opts_test.go",
37+
"wallet_test.go",
3738
],
3839
embed = [":go_default_library"],
3940
deps = [
4041
"//shared/bls:go_default_library",
4142
"//shared/bytesutil:go_default_library",
43+
"@com_github_wealdtech_go_eth2_wallet_encryptor_keystorev4//:go_default_library",
44+
"@com_github_wealdtech_go_eth2_wallet_nd//:go_default_library",
45+
"@com_github_wealdtech_go_eth2_wallet_store_filesystem//:go_default_library",
4246
],
4347
)

validator/keymanager/wallet.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,22 @@ func NewWallet(input string) (KeyManager, string, error) {
8484
}
8585
re := regexp.MustCompile(accountSpecifier)
8686
for account := range wallet.Accounts() {
87+
log := log.WithField("account", fmt.Sprintf("%s/%s", wallet.Name(), account.Name()))
8788
if re.Match([]byte(account.Name())) {
8889
pubKey := bytesutil.ToBytes48(account.PublicKey().Marshal())
90+
unlocked := false
8991
for _, passphrase := range opts.Passphrases {
9092
if err := account.Unlock([]byte(passphrase)); err != nil {
91-
log.WithError(err).WithField("pubKey", fmt.Sprintf("%#x", pubKey)).Warn("Failed to unlock account with supplied passphrases; cannot validate")
93+
log.WithError(err).Trace("Failed to unlock account with one of the supplied passphrases")
9294
} else {
9395
km.accounts[pubKey] = account
96+
unlocked = true
97+
break
9498
}
9599
}
100+
if !unlocked {
101+
log.Warn("Failed to unlock account with any supplied passphrase; cannot validate with this key")
102+
}
96103
}
97104
}
98105
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)