From fb0cef150179f65ee7c7d7435cb49eac6f2d611d Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 11 Nov 2022 11:58:38 +0000 Subject: [PATCH 1/2] Adds crypto.go Cryptographic Identity util --- testutil/crypto/crypto.go | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 testutil/crypto/crypto.go diff --git a/testutil/crypto/crypto.go b/testutil/crypto/crypto.go new file mode 100644 index 0000000000..3551a97e65 --- /dev/null +++ b/testutil/crypto/crypto.go @@ -0,0 +1,96 @@ +package crypto + +import ( + "encoding/binary" + + "github.com/cosmos/ibc-go/v3/testing/mock" + + cryptoEd25519 "crypto/ed25519" + + sdkcryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdkcryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + sdkcryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdktypes "github.com/cosmos/cosmos-sdk/types" + sdkstakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + tmcrypto "github.com/tendermint/tendermint/crypto" + tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto" + tmtypes "github.com/tendermint/tendermint/types" +) + +// CryptoIdentity is a test helper for generating keys and addresses of +// various interfaces and types used by the SDK and Tendermint from a single +// 'root' private key. +type CryptoIdentity struct { + mock.PV +} + +func NewCryptoIdentityFromBytesSeed(seed []byte) CryptoIdentity { + //lint:ignore SA1019 We don't care because this is only a test. + privKey := mock.PV{PrivKey: &sdkcryptokeys.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)}} + return CryptoIdentity{PV: privKey} +} + +func NewCryptoIdentityFromIntSeed(i int) CryptoIdentity { + iUint64 := uint64(i) + seed := []byte("AAAAAAAAabcdefghijklmnopqrstuvwx") // 8+24 bytes + binary.LittleEndian.PutUint64(seed[:8], iUint64) + return NewCryptoIdentityFromBytesSeed(seed) +} + +func (v *CryptoIdentity) ABCIAddressBytes() []byte { + return v.SDKPubKey().Address() +} + +func (v *CryptoIdentity) TMValidator(power int64) *tmtypes.Validator { + return tmtypes.NewValidator(v.TMCryptoPubKey(), power) +} + +func (v *CryptoIdentity) TMProtoCryptoPublicKey() tmprotocrypto.PublicKey { + ret, err := sdkcryptocodec.ToTmProtoPublicKey(v.SDKPubKey()) + if err != nil { + panic(err) + } + return ret +} + +func (v *CryptoIdentity) TMCryptoPubKey() tmcrypto.PubKey { + ret, err := v.GetPubKey() + if err != nil { + panic(err) + } + return ret +} + +func (v *CryptoIdentity) SDKStakingValidator() sdkstakingtypes.Validator { + ret, err := sdkstakingtypes.NewValidator(v.SDKValAddress(), v.SDKPubKey(), sdkstakingtypes.Description{}) + if err != nil { + panic(err) + } + return ret +} + +func (v *CryptoIdentity) SDKPubKey() sdkcryptotypes.PubKey { + tmcryptoPubKey := v.TMCryptoPubKey() + ret, err := sdkcryptocodec.FromTmPubKeyInterface(tmcryptoPubKey) + if err != nil { + panic(err) + } + return ret +} + +func (v *CryptoIdentity) SDKValAddressString() string { + return v.TMCryptoPubKey().Address().String() +} + +func (v *CryptoIdentity) SDKValAddress() sdktypes.ValAddress { + ret, err := sdktypes.ValAddressFromHex(v.SDKValAddressString()) + if err != nil { + panic(err) + } + return ret +} + +func (v *CryptoIdentity) SDKConsAddress() sdktypes.ConsAddress { + return sdktypes.GetConsAddress(v.SDKPubKey()) +} From 654684eb4cea7384a8a92177fc3e7d45960ec881 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 14 Nov 2022 13:36:55 +0000 Subject: [PATCH 2/2] Changes mock to ibcmock --- testutil/crypto/crypto.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testutil/crypto/crypto.go b/testutil/crypto/crypto.go index 3551a97e65..5e77e65e40 100644 --- a/testutil/crypto/crypto.go +++ b/testutil/crypto/crypto.go @@ -3,7 +3,7 @@ package crypto import ( "encoding/binary" - "github.com/cosmos/ibc-go/v3/testing/mock" + ibcmock "github.com/cosmos/ibc-go/v3/testing/mock" cryptoEd25519 "crypto/ed25519" @@ -22,12 +22,12 @@ import ( // various interfaces and types used by the SDK and Tendermint from a single // 'root' private key. type CryptoIdentity struct { - mock.PV + ibcmock.PV } func NewCryptoIdentityFromBytesSeed(seed []byte) CryptoIdentity { //lint:ignore SA1019 We don't care because this is only a test. - privKey := mock.PV{PrivKey: &sdkcryptokeys.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)}} + privKey := ibcmock.PV{PrivKey: &sdkcryptokeys.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)}} return CryptoIdentity{PV: privKey} }