github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/crypto/encode_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	tcrypto "github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
    10  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519"
    11  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/secp256k1"
    12  )
    13  
    14  type byter interface {
    15  	Bytes() []byte
    16  }
    17  
    18  func checkAminoBinary(t *testing.T, src, dst interface{}, size int) {
    19  	// Marshal to binary bytes.
    20  	bz, err := cdc.MarshalBinaryBare(src)
    21  	require.Nil(t, err, "%+v", err)
    22  	if byterSrc, ok := src.(byter); ok {
    23  		// Make sure this is compatible with current (Bytes()) encoding.
    24  		require.Equal(t, byterSrc.Bytes(), bz, "Amino binary vs Bytes() mismatch")
    25  	}
    26  	// Make sure we have the expected length.
    27  	if size != -1 {
    28  		require.Equal(t, size, len(bz), "Amino binary size mismatch")
    29  	}
    30  	// Unmarshal.
    31  	err = cdc.UnmarshalBinaryBare(bz, dst)
    32  	require.Nil(t, err, "%+v", err)
    33  }
    34  
    35  func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) {
    36  	// Marshal to JSON bytes.
    37  	js, err := cdc.MarshalJSON(src)
    38  	require.Nil(t, err, "%+v", err)
    39  	if isNil {
    40  		require.Equal(t, string(js), `null`)
    41  	} else {
    42  		require.Contains(t, string(js), `"type":`)
    43  		require.Contains(t, string(js), `"value":`)
    44  	}
    45  	// Unmarshal.
    46  	err = cdc.UnmarshalJSON(js, dst)
    47  	require.Nil(t, err, "%+v", err)
    48  }
    49  
    50  // nolint: govet
    51  func ExamplePrintRegisteredTypes() {
    52  	cdc.PrintTypes(os.Stdout)
    53  	// Output: | Type | Name | Prefix | Length | Notes |
    54  	//| ---- | ---- | ------ | ----- | ------ |
    55  	//| PrivKeyLedgerSecp256k1 | tendermint/PrivKeyLedgerSecp256k1 | 0x10CAB393 | variable |  |
    56  	//| PubKeyEd25519 | tendermint/PubKeyEd25519 | 0x1624DE64 | 0x20 |  |
    57  	//| PubKeySr25519 | tendermint/PubKeySr25519 | 0x0DFB1005 | 0x20 |  |
    58  	//| PubKeySecp256k1 | tendermint/PubKeySecp256k1 | 0xEB5AE987 | 0x21 |  |
    59  	//| PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable |  |
    60  	//| PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 |  |
    61  	//| PrivKeySr25519 | tendermint/PrivKeySr25519 | 0x2F82D78B | 0x20 |  |
    62  	//| PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 |  |
    63  }
    64  
    65  func TestKeyEncodings(t *testing.T) {
    66  	cases := []struct {
    67  		privKey           tcrypto.PrivKey
    68  		privSize, pubSize int // binary sizes with the amino overhead
    69  	}{
    70  		{
    71  			privKey:  ed25519.GenPrivKey(),
    72  			privSize: 69,
    73  			pubSize:  37,
    74  		},
    75  		{
    76  			privKey:  secp256k1.GenPrivKey(),
    77  			privSize: 37,
    78  			pubSize:  38,
    79  		},
    80  	}
    81  
    82  	for _, tc := range cases {
    83  
    84  		// Check (de/en)codings of PrivKeys.
    85  		var priv2, priv3 tcrypto.PrivKey
    86  		checkAminoBinary(t, tc.privKey, &priv2, tc.privSize)
    87  		require.EqualValues(t, tc.privKey, priv2)
    88  		checkAminoJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes.
    89  		require.EqualValues(t, tc.privKey, priv3)
    90  
    91  		// Check (de/en)codings of Signatures.
    92  		var sig1, sig2 []byte
    93  		sig1, err := tc.privKey.Sign([]byte("something"))
    94  		require.NoError(t, err)
    95  		checkAminoBinary(t, sig1, &sig2, -1) // Signature size changes for Secp anyways.
    96  		require.EqualValues(t, sig1, sig2)
    97  
    98  		// Check (de/en)codings of PubKeys.
    99  		pubKey := tc.privKey.PubKey()
   100  		var pub2, pub3 tcrypto.PubKey
   101  		checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
   102  		require.EqualValues(t, pubKey, pub2)
   103  		checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
   104  		require.EqualValues(t, pubKey, pub3)
   105  	}
   106  }
   107  
   108  func TestNilEncodings(t *testing.T) {
   109  
   110  	// Check nil Signature.
   111  	var a, b []byte
   112  	checkAminoJSON(t, &a, &b, true)
   113  	require.EqualValues(t, a, b)
   114  
   115  	// Check nil PubKey.
   116  	var c, d tcrypto.PubKey
   117  	checkAminoJSON(t, &c, &d, true)
   118  	require.EqualValues(t, c, d)
   119  
   120  	// Check nil PrivKey.
   121  	var e, f tcrypto.PrivKey
   122  	checkAminoJSON(t, &e, &f, true)
   123  	require.EqualValues(t, e, f)
   124  
   125  }