From 5b57a10250d415bda55a25ae01565bb4ace9e927 Mon Sep 17 00:00:00 2001 From: Sathvik Birudavolu Date: Wed, 5 Dec 2018 06:30:13 -0500 Subject: [PATCH] Fix NEP2Encrypt add Unit Tests (#108) * fix NEP2 add unit tests * version update --- VERSION | 2 +- cli/wallet/wallet.go | 8 +++--- pkg/wallet/nep2.go | 2 +- pkg/wallet/nep2_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 pkg/wallet/nep2_test.go diff --git a/VERSION b/VERSION index a1cf49f6e..cbb488ee2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.45.11 +0.45.12 diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 209e6e340..13b2d5455 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -12,11 +12,11 @@ import ( ) var ( - errNoPath = errors.New("Target path where the wallet should be stored is mandatory and should be passed using (--path, -p) flags.") - errPhraseMissmatch = errors.New("The entered passphrases do not match. Maybe you have misspelled them?") + errNoPath = errors.New("target path where the wallet should be stored is mandatory and should be passed using (--path, -p) flags") + errPhraseMismatch = errors.New("the entered pass-phrases do not match. Maybe you have misspelled them") ) -// NewComand creates a new Wallet command. +// NewCommand creates a new Wallet command. func NewCommand() cli.Command { return cli.Command{ Name: "wallet", @@ -102,7 +102,7 @@ func createAccount(ctx *cli.Context, wall *wallet.Wallet) error { ) if phrase != phraseCheck { - return errPhraseMissmatch + return errPhraseMismatch } return wall.CreateAccount(name, phrase) diff --git a/pkg/wallet/nep2.go b/pkg/wallet/nep2.go index 135049c30..b9409fc03 100644 --- a/pkg/wallet/nep2.go +++ b/pkg/wallet/nep2.go @@ -58,7 +58,7 @@ func NEP2Encrypt(priv *PrivateKey, passphrase string) (s string, err error) { derivedKey2 := derivedKey[32:] xr := xor(priv.Bytes(), derivedKey1) - encrypted, err := crypto.AESEncrypt(derivedKey2, xr) + encrypted, err := crypto.AESEncrypt(xr, derivedKey2) if err != nil { return s, err } diff --git a/pkg/wallet/nep2_test.go b/pkg/wallet/nep2_test.go new file mode 100644 index 000000000..e00a0cae1 --- /dev/null +++ b/pkg/wallet/nep2_test.go @@ -0,0 +1,60 @@ +package wallet + +import( + "testing" +) + +func TestNEP2Encrypt(t *testing.T) { + for _, testCase := range testKeyCases { + + privKey, err := NewPrivateKeyFromHex(testCase.privateKey) + if err != nil { + t.Fatal(err) + } + + encryptedWif, err := NEP2Encrypt(privKey, testCase.passphrase) + if err != nil { + t.Fatal(err) + } + + if want, have:= testCase.encryptedWif, encryptedWif; want != have { + t.Fatalf("expected %s got %s", want, have) + } + } +} + +func TestNEP2Decrypt(t *testing.T) { + for _, testCase := range testKeyCases { + + privKeyString, err := NEP2Decrypt(testCase.encryptedWif, testCase.passphrase) + + if err != nil { + t.Fatal(err) + } + + privKey, err := NewPrivateKeyFromWIF(privKeyString) + if err != nil { + t.Fatal(err) + } + + if want, have := testCase.privateKey, privKey.String(); want != have { + t.Fatalf("expected %s got %s", want, have) + } + + wif, err := privKey.WIF() + if err != nil{ + t.Fatal(err) + } + if want, have := testCase.wif, wif; want != have { + t.Fatalf("expected %s got %s", want, have) + } + + address, err := privKey.Address() + if err != nil{ + t.Fatal(err) + } + if want, have := testCase.address, address; want != have { + t.Fatalf("expected %s got %s", want, have) + } + } +} \ No newline at end of file