2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/keytestcases"
|
2020-12-02 12:52:31 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2019-09-09 08:23:27 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-02 12:52:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-27 13:29:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPrivateKey(t *testing.T) {
|
|
|
|
for _, testCase := range keytestcases.Arr {
|
|
|
|
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)
|
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)
|
|
|
|
|
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)
|
2019-09-05 06:35:02 +00:00
|
|
|
pubKey := privKey.PublicKey()
|
2019-08-27 13:29:42 +00:00
|
|
|
assert.Equal(t, hex.EncodeToString(pubKey.Bytes()), testCase.PublicKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:52:31 +00:00
|
|
|
func TestNewPrivateKeyOnCurve(t *testing.T) {
|
|
|
|
msg := []byte{1, 2, 3}
|
|
|
|
h := hash.Sha256(msg).BytesBE()
|
|
|
|
t.Run("Secp256r1", func(t *testing.T) {
|
|
|
|
p, err := NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p.PublicKey().Verify(p.Sign(msg), h)
|
|
|
|
})
|
|
|
|
t.Run("Secp256k1", func(t *testing.T) {
|
|
|
|
p, err := NewSecp256k1PrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p.PublicKey().Verify(p.Sign(msg), h)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
func TestPrivateKeyFromWIF(t *testing.T) {
|
|
|
|
for _, testCase := range keytestcases.Arr {
|
|
|
|
key, err := NewPrivateKeyFromWIF(testCase.Wif)
|
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, key.String())
|
2019-08-27 13:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSigning(t *testing.T) {
|
|
|
|
// These were taken from the rfcPage:https://tools.ietf.org/html/rfc6979#page-33
|
|
|
|
// public key: U = xG
|
|
|
|
//Ux = 60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6
|
|
|
|
//Uy = 7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299
|
|
|
|
PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721")
|
|
|
|
|
2020-01-16 15:30:16 +00:00
|
|
|
data := PrivateKey.Sign([]byte("sample"))
|
2019-08-27 13:29:42 +00:00
|
|
|
|
|
|
|
r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716"
|
|
|
|
s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8"
|
|
|
|
assert.Equal(t, strings.ToLower(r+s), hex.EncodeToString(data))
|
|
|
|
}
|