laconicd-deprecated/crypto/ethsecp256k1/ethsecp256k1_test.go

126 lines
3.0 KiB
Go
Raw Permalink 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"
2021-09-03 18:06:36 +00:00
"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()
2021-09-07 17:29:24 +00:00
key, err := privKey.ToECDSA()
require.NoError(t, err)
expectedAddr := crypto.PubkeyToAddress(key.PublicKey)
require.Equal(t, expectedAddr.Bytes(), addr.Bytes())
// validate we can sign some bytes
msg := []byte("hello world")
2021-09-03 18:06:36 +00:00
sigHash := crypto.Keccak256Hash(msg)
2021-04-17 10:00:07 +00:00
expectedSig, err := secp256k1.Sign(sigHash.Bytes(), privKey.Bytes())
require.NoError(t, err)
2021-09-07 17:29:24 +00:00
sig, err := privKey.Sign(sigHash.Bytes())
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")
2021-09-07 17:29:24 +00:00
sigHash := crypto.Keccak256Hash(msg)
sig, err := privKey.Sign(sigHash.Bytes())
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.Marshal(tc.msg)
2021-04-17 10:00:07 +00:00
require.NoError(t, err)
require.Equal(t, tc.expBinary, bz)
err = aminoCdc.Unmarshal(bz, tc.typ)
2021-04-17 10:00:07 +00:00
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)
})
}
}