wallet: make DecryptAccount a method of Account

Otherwise it almost duplicates keys.NEP2Decrypt().
This commit is contained in:
Roman Khimov 2020-01-09 18:30:25 +03:00
parent 951ee383e9
commit 9bb68d7025
2 changed files with 20 additions and 9 deletions

View file

@ -1,6 +1,8 @@
package wallet package wallet
import ( import (
"errors"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
) )
@ -60,14 +62,16 @@ func NewAccount() (*Account, error) {
return newAccountFromPrivateKey(priv), nil return newAccountFromPrivateKey(priv), nil
} }
// DecryptAccount decrypts the encryptedWIF with the given passphrase and // Decrypt decrypts the EncryptedWIF with the given passphrase returning error
// return the decrypted Account. // if anything goes wrong.
func DecryptAccount(encryptedWIF, passphrase string) (*Account, error) { func (a *Account) Decrypt(passphrase string) error {
key, err := keys.NEP2Decrypt(encryptedWIF, passphrase) var err error
if err != nil {
return nil, err 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 // Encrypt encrypts the wallet's PrivateKey with the given passphrase

View file

@ -6,6 +6,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/internal/keytestcases" "github.com/CityOfZion/neo-go/pkg/internal/keytestcases"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestNewAccount(t *testing.T) { func TestNewAccount(t *testing.T) {
@ -23,15 +24,21 @@ func TestNewAccount(t *testing.T) {
func TestDecryptAccount(t *testing.T) { func TestDecryptAccount(t *testing.T) {
for _, testCase := range keytestcases.Arr { 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 { if testCase.Invalid {
assert.Error(t, err) assert.Error(t, err)
continue continue
} }
assert.NoError(t, err) 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) { func TestNewFromWif(t *testing.T) {