cosmos-sdk/crypto/ledger/encode_test.go
Marie Gauthier 90e9370bd8
Replace tmcrypto.PubKey by our own cryptotypes.PubKey (#7419)
* WIP on removing tm pub/privkey

* Fix part of crypto tests

* Add PrivKeyLedgerSecp256K1 proto type

* Use BasePrivKey for ledger priv key type

* Refacto continued

* First round

* x/staking

* Continue porting

* x/* done

* Make build pass

* More conversion

* Remove IntoTmPubKey

* Fix test

* Remove crypto.PubKey in some other places

* Revert ledger changes

* Fix comment

* Remove useless function

* Add To/FromTmPublicKey

* Add migrate tests

* Fix test

* Fix another test

* Rename tm conversion functions

* Less code

* Rename BasePrivKey to LedgerPrivKey

* Add changelog

* Rename functions

Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2020-11-09 16:01:43 +00:00

64 lines
1.7 KiB
Go

package ledger
import (
"os"
"testing"
"github.com/stretchr/testify/require"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
type byter interface {
Bytes() []byte
}
func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) {
// Marshal to JSON bytes.
js, err := cdc.MarshalJSON(src)
require.Nil(t, err, "%+v", err)
if isNil {
require.Equal(t, string(js), `null`)
} else {
require.Contains(t, string(js), `"type":`)
require.Contains(t, string(js), `"value":`)
}
// Unmarshal.
err = cdc.UnmarshalJSON(js, dst)
require.Nil(t, err, "%+v", err)
}
// nolint: govet
func ExamplePrintRegisteredTypes() {
cdc.PrintTypes(os.Stdout)
// | Type | Name | Prefix | Length | Notes |
// | ---- | ---- | ------ | ----- | ------ |
// | PrivKeyLedgerSecp256k1 | tendermint/PrivKeyLedgerSecp256k1 | 0x10CAB393 | variable | |
// | PubKey | tendermint/PubKeyEd25519 | 0x1624DE64 | variable | |
// | PubKey | tendermint/PubKeySr25519 | 0x0DFB1005 | variable | |
// | PubKey | tendermint/PubKeySecp256k1 | 0xEB5AE987 | variable | |
// | PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | |
// | PrivKey | tendermint/PrivKeyEd25519 | 0xA3288910 | variable | |
// | PrivKey | tendermint/PrivKeySr25519 | 0x2F82D78B | variable | |
// | PrivKey | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | variable | |
}
func TestNilEncodings(t *testing.T) {
// Check nil Signature.
var a, b []byte
checkAminoJSON(t, &a, &b, true)
require.EqualValues(t, a, b)
// Check nil PubKey.
var c, d cryptotypes.PubKey
checkAminoJSON(t, &c, &d, true)
require.EqualValues(t, c, d)
// Check nil PrivKey.
var e, f cryptotypes.PrivKey
checkAminoJSON(t, &e, &f, true)
require.EqualValues(t, e, f)
}