2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
2018-12-05 11:30:13 +00:00
|
|
|
|
2019-01-25 11:20:35 +00:00
|
|
|
import (
|
2018-12-05 11:30:13 +00:00
|
|
|
"testing"
|
2019-08-27 13:29:42 +00:00
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/keytestcases"
|
2019-08-27 14:06:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-05 11:30:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNEP2Encrypt(t *testing.T) {
|
2019-08-27 13:29:42 +00:00
|
|
|
for _, testCase := range keytestcases.Arr {
|
2018-12-05 11:30:13 +00:00
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey)
|
2019-11-24 15:40:07 +00:00
|
|
|
if testCase.Invalid {
|
|
|
|
assert.Error(t, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Nil(t, err)
|
2018-12-05 11:30:13 +00:00
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
encryptedWif, err := NEP2Encrypt(privKey, testCase.Passphrase)
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Nil(t, err)
|
2018-12-05 11:30:13 +00:00
|
|
|
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Equal(t, testCase.EncryptedWif, encryptedWif)
|
2018-12-05 11:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNEP2Decrypt(t *testing.T) {
|
2019-08-27 13:29:42 +00:00
|
|
|
for _, testCase := range keytestcases.Arr {
|
2020-01-09 14:27:12 +00:00
|
|
|
privKey, err := NEP2Decrypt(testCase.EncryptedWif, testCase.Passphrase)
|
2019-11-24 15:40:07 +00:00
|
|
|
if testCase.Invalid {
|
|
|
|
assert.Error(t, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, testCase.PrivateKey, privKey.String())
|
2018-12-05 11:30:13 +00:00
|
|
|
|
2019-09-05 06:35:02 +00:00
|
|
|
wif := privKey.WIF()
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Equal(t, testCase.Wif, wif)
|
2018-12-05 11:30:13 +00:00
|
|
|
|
2019-09-05 06:35:02 +00:00
|
|
|
address := privKey.Address()
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Equal(t, testCase.Address, address)
|
2018-12-05 11:30:13 +00:00
|
|
|
}
|
2019-01-25 11:20:35 +00:00
|
|
|
}
|
2020-01-09 15:06:03 +00:00
|
|
|
|
|
|
|
func TestNEP2DecryptErrors(t *testing.T) {
|
|
|
|
p := "qwerty"
|
|
|
|
|
|
|
|
// Not a base58-encoded value
|
|
|
|
s := "qazwsx"
|
|
|
|
_, err := NEP2Decrypt(s, p)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Valid base58, but not a NEP-2 format.
|
|
|
|
s = "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o"
|
|
|
|
_, err = NEP2Decrypt(s, p)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateNEP2Format(t *testing.T) {
|
|
|
|
// Wrong length.
|
|
|
|
s := []byte("gobbledygook")
|
|
|
|
assert.Error(t, validateNEP2Format(s))
|
|
|
|
|
|
|
|
// Wrong header 1.
|
|
|
|
s = []byte("gobbledygookgobbledygookgobbledygookgob")
|
|
|
|
assert.Error(t, validateNEP2Format(s))
|
|
|
|
|
|
|
|
// Wrong header 2.
|
|
|
|
s[0] = 0x01
|
|
|
|
assert.Error(t, validateNEP2Format(s))
|
|
|
|
|
|
|
|
// Wrong header 3.
|
|
|
|
s[1] = 0x42
|
|
|
|
assert.Error(t, validateNEP2Format(s))
|
|
|
|
|
|
|
|
// OK
|
|
|
|
s[2] = 0xe0
|
|
|
|
assert.NoError(t, validateNEP2Format(s))
|
|
|
|
}
|