diff --git a/pkg/wallet/account.go b/pkg/wallet/account.go index dc8e75b90..c1ddd1317 100644 --- a/pkg/wallet/account.go +++ b/pkg/wallet/account.go @@ -1,6 +1,8 @@ package wallet import ( + "errors" + "github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/util" ) @@ -60,14 +62,16 @@ func NewAccount() (*Account, error) { return newAccountFromPrivateKey(priv), nil } -// DecryptAccount decrypts the encryptedWIF with the given passphrase and -// return the decrypted Account. -func DecryptAccount(encryptedWIF, passphrase string) (*Account, error) { - key, err := keys.NEP2Decrypt(encryptedWIF, passphrase) - if err != nil { - return nil, err +// Decrypt decrypts the EncryptedWIF with the given passphrase returning error +// if anything goes wrong. +func (a *Account) Decrypt(passphrase string) error { + var err error + + if a.EncryptedWIF == "" { + return errors.New("no encrypted wif in the account") } - return newAccountFromPrivateKey(key), nil + a.privateKey, err = keys.NEP2Decrypt(a.EncryptedWIF, passphrase) + return err } // Encrypt encrypts the wallet's PrivateKey with the given passphrase diff --git a/pkg/wallet/account_test.go b/pkg/wallet/account_test.go index 343c993bc..b82af7e24 100644 --- a/pkg/wallet/account_test.go +++ b/pkg/wallet/account_test.go @@ -6,6 +6,7 @@ import ( "github.com/CityOfZion/neo-go/pkg/internal/keytestcases" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNewAccount(t *testing.T) { @@ -23,15 +24,21 @@ func TestNewAccount(t *testing.T) { func TestDecryptAccount(t *testing.T) { for _, testCase := range keytestcases.Arr { - acc, err := DecryptAccount(testCase.EncryptedWif, testCase.Passphrase) + acc := &Account{EncryptedWIF: testCase.EncryptedWif} + assert.Nil(t, acc.PrivateKey()) + err := acc.Decrypt(testCase.Passphrase) if testCase.Invalid { assert.Error(t, err) continue } assert.NoError(t, err) - compareFields(t, testCase, acc) + assert.NotNil(t, acc.PrivateKey()) + assert.Equal(t, testCase.PrivateKey, acc.privateKey.String()) } + // No encrypted key. + acc := &Account{} + require.Error(t, acc.Decrypt("qwerty")) } func TestNewFromWif(t *testing.T) {