wallet: allow to create accounts from encrypted WIFs

This commit is contained in:
Evgenii Stratonikov 2020-02-20 12:22:40 +03:00
parent 02954285c1
commit d837eb3761
2 changed files with 26 additions and 0 deletions

View file

@ -150,6 +150,19 @@ func NewAccountFromWIF(wif string) (*Account, error) {
return newAccountFromPrivateKey(privKey), nil
}
// NewAccountFromEncryptedWIF creates a new Account from the given encrypted WIF.
func NewAccountFromEncryptedWIF(wif string, pass string) (*Account, error) {
priv, err := keys.NEP2Decrypt(wif, pass)
if err != nil {
return nil, err
}
a := newAccountFromPrivateKey(priv)
a.EncryptedWIF = wif
return a, nil
}
// newAccountFromPrivateKey creates a wallet from the given PrivateKey.
func newAccountFromPrivateKey(p *keys.PrivateKey) *Account {
pubKey := p.PublicKey()

View file

@ -49,6 +49,19 @@ func TestNewFromWif(t *testing.T) {
}
}
func TestNewAccountFromEncryptedWIF(t *testing.T) {
for _, tc := range keytestcases.Arr {
acc, err := NewAccountFromEncryptedWIF(tc.EncryptedWif, tc.Passphrase)
if tc.Invalid {
assert.Error(t, err)
continue
}
assert.NoError(t, err)
compareFields(t, tc, acc)
}
}
func TestContract_MarshalJSON(t *testing.T) {
var c Contract