mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 16:25:21 +00:00
97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
package vrf
|
|
|
|
import (
|
|
"testing"
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
|
|
"encoding/base64"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
)
|
|
|
|
func TestPrivKey(t *testing.T) {
|
|
// validate type and equality
|
|
privKey, err := GenerateKey()
|
|
require.NoError(t, err)
|
|
require.Implements(t, (*cryptotypes.PrivKey)(nil), privKey)
|
|
|
|
// validate inequality
|
|
privKey2, err := GenerateKey()
|
|
require.NoError(t, err)
|
|
require.False(t, privKey.Equals(privKey2))
|
|
}
|
|
|
|
func TestPrivKey_PubKey(t *testing.T) {
|
|
privKey, err := GenerateKey()
|
|
require.NoError(t, err)
|
|
|
|
// validate type and equality
|
|
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()))
|
|
}
|
|
|
|
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
|
|
}{
|
|
{
|
|
"vrf private key",
|
|
privKey,
|
|
&PrivKey{},
|
|
append([]byte{64}, privKey.Bytes()...), // Length-prefixed.
|
|
"\"" + base64.StdEncoding.EncodeToString(privKey.Bytes()) + "\"",
|
|
},
|
|
{
|
|
"vrf public key",
|
|
pubKey,
|
|
&PubKey{},
|
|
append([]byte{32}, 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)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.expBinary, bz)
|
|
|
|
err = aminoCdc.Unmarshal(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)
|
|
})
|
|
}
|
|
}
|