laconicd/crypto/ethsecp256k1/ethsecp256k1_test.go

123 lines
3.0 KiB
Go
Raw Normal View History

package ethsecp256k1
import (
2021-04-17 10:00:07 +00:00
"encoding/base64"
"testing"
"github.com/stretchr/testify/require"
2021-04-17 10:00:07 +00:00
"github.com/cosmos/cosmos-sdk/codec"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
2021-04-17 10:00:07 +00:00
"github.com/ethereum/go-ethereum/crypto/secp256k1"
2021-04-17 10:00:07 +00:00
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
2021-04-17 10:00:07 +00:00
func TestPrivKey(t *testing.T) {
// 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)
// validate inequality
privKey2, err := GenerateKey()
require.NoError(t, err)
require.False(t, privKey.Equals(privKey2))
// validate Ethereum address equality
addr := privKey.PubKey().Address()
expectedAddr := ethcrypto.PubkeyToAddress(privKey.ToECDSA().PublicKey)
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)
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) {
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)
// 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)
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)
})
}
}