Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Cryptographic Identity utility for working with various keys ect #470

Merged
merged 4 commits into from
Nov 15, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions testutil/crypto/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package crypto

import (
"encoding/binary"

"github.com/cosmos/ibc-go/v3/testing/mock"
danwt marked this conversation as resolved.
Show resolved Hide resolved

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())
}