2020-10-06 18:57:55 +00:00
|
|
|
package ethsecp256k1
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"encoding/base64"
|
2018-12-18 16:10:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-07-02 15:19:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2018-12-18 16:10:04 +00:00
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
func TestPrivKey(t *testing.T) {
|
2018-12-18 16:10:04 +00:00
|
|
|
// validate type and equality
|
|
|
|
privKey, err := GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, privKey.Equals(privKey))
|
2021-04-17 10:00:07 +00:00
|
|
|
require.Implements(t, (*cryptotypes.PrivKey)(nil), privKey)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
// validate inequality
|
|
|
|
privKey2, err := GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, privKey.Equals(privKey2))
|
|
|
|
|
|
|
|
// validate Ethereum address equality
|
|
|
|
addr := privKey.PubKey().Address()
|
2019-08-11 14:42:46 +00:00
|
|
|
expectedAddr := ethcrypto.PubkeyToAddress(privKey.ToECDSA().PublicKey)
|
2018-12-18 16:10:04 +00:00
|
|
|
require.Equal(t, expectedAddr.Bytes(), addr.Bytes())
|
|
|
|
|
|
|
|
// validate we can sign some bytes
|
|
|
|
msg := []byte("hello world")
|
|
|
|
sigHash := ethcrypto.Keccak256Hash(msg)
|
2021-04-17 10:00:07 +00:00
|
|
|
expectedSig, err := secp256k1.Sign(sigHash.Bytes(), privKey.Bytes())
|
|
|
|
require.NoError(t, err)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
sig, err := privKey.Sign(msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedSig, sig)
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
func TestPrivKey_PubKey(t *testing.T) {
|
2018-12-18 16:10:04 +00:00
|
|
|
privKey, err := GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// validate type and equality
|
2021-04-17 10:00:07 +00:00
|
|
|
pubKey := &PubKey{
|
|
|
|
Key: privKey.PubKey().Bytes(),
|
|
|
|
}
|
|
|
|
require.Implements(t, (*cryptotypes.PubKey)(nil), pubKey)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
// validate inequality
|
|
|
|
privKey2, err := GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, pubKey.Equals(privKey2.PubKey()))
|
|
|
|
|
|
|
|
// validate signature
|
|
|
|
msg := []byte("hello world")
|
|
|
|
sig, err := privKey.Sign(msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
res := pubKey.VerifySignature(msg, sig)
|
2018-12-18 16:10:04 +00:00
|
|
|
require.True(t, res)
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
func TestMarshalAmino(t *testing.T) {
|
|
|
|
aminoCdc := codec.NewLegacyAmino()
|
|
|
|
privKey, err := GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pubKey := privKey.PubKey().(*PubKey)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
msg codec.AminoMarshaler
|
|
|
|
typ interface{}
|
|
|
|
expBinary []byte
|
|
|
|
expJSON string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"ethsecp256k1 private key",
|
|
|
|
privKey,
|
|
|
|
&PrivKey{},
|
|
|
|
append([]byte{32}, privKey.Bytes()...), // Length-prefixed.
|
|
|
|
"\"" + base64.StdEncoding.EncodeToString(privKey.Bytes()) + "\"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ethsecp256k1 public key",
|
|
|
|
pubKey,
|
|
|
|
&PubKey{},
|
|
|
|
append([]byte{33}, pubKey.Bytes()...), // Length-prefixed.
|
|
|
|
"\"" + base64.StdEncoding.EncodeToString(pubKey.Bytes()) + "\"",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
// Do a round trip of encoding/decoding binary.
|
|
|
|
bz, err := aminoCdc.MarshalBinaryBare(tc.msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expBinary, bz)
|
|
|
|
|
|
|
|
err = aminoCdc.UnmarshalBinaryBare(bz, tc.typ)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, tc.msg, tc.typ)
|
|
|
|
|
|
|
|
// Do a round trip of encoding/decoding JSON.
|
|
|
|
bz, err = aminoCdc.MarshalJSON(tc.msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expJSON, string(bz))
|
|
|
|
|
|
|
|
err = aminoCdc.UnmarshalJSON(bz, tc.typ)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, tc.msg, tc.typ)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|