Fix NEP2Encrypt add Unit Tests (#108)

* fix NEP2 add unit tests

* version update
This commit is contained in:
Sathvik Birudavolu 2018-12-05 06:30:13 -05:00 committed by Anthony De Meulemeester
parent e2f42e92a0
commit 5b57a10250
4 changed files with 66 additions and 6 deletions

View file

@ -1 +1 @@
0.45.11 0.45.12

View file

@ -12,11 +12,11 @@ import (
) )
var ( var (
errNoPath = errors.New("Target path where the wallet should be stored is mandatory and should be passed using (--path, -p) flags.") 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?") 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 { func NewCommand() cli.Command {
return cli.Command{ return cli.Command{
Name: "wallet", Name: "wallet",
@ -102,7 +102,7 @@ func createAccount(ctx *cli.Context, wall *wallet.Wallet) error {
) )
if phrase != phraseCheck { if phrase != phraseCheck {
return errPhraseMissmatch return errPhraseMismatch
} }
return wall.CreateAccount(name, phrase) return wall.CreateAccount(name, phrase)

View file

@ -58,7 +58,7 @@ func NEP2Encrypt(priv *PrivateKey, passphrase string) (s string, err error) {
derivedKey2 := derivedKey[32:] derivedKey2 := derivedKey[32:]
xr := xor(priv.Bytes(), derivedKey1) xr := xor(priv.Bytes(), derivedKey1)
encrypted, err := crypto.AESEncrypt(derivedKey2, xr) encrypted, err := crypto.AESEncrypt(xr, derivedKey2)
if err != nil { if err != nil {
return s, err return s, err
} }

60
pkg/wallet/nep2_test.go Normal file
View file

@ -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)
}
}
}