diff --git a/pkg/wallet/account.go b/pkg/wallet/account.go index 591e1971e..25f9eae02 100644 --- a/pkg/wallet/account.go +++ b/pkg/wallet/account.go @@ -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() diff --git a/pkg/wallet/account_test.go b/pkg/wallet/account_test.go index 81eef739b..93847b991 100644 --- a/pkg/wallet/account_test.go +++ b/pkg/wallet/account_test.go @@ -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