Skip to content

Commit

Permalink
Start factoring out duplicate test keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Nov 2, 2022
1 parent 97abb29 commit 983c87c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
59 changes: 59 additions & 0 deletions testutil/crypto/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package crypto

import (
"encoding/binary"

"github.com/cosmos/ibc-go/v3/testing/mock"

cryptoEd25519 "crypto/ed25519"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"

sdkcryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdkcryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

tmcrypto "github.com/tendermint/tendermint/crypto"
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
)

type Validator struct {
mock.PV
}

func NewValidatorFromBytesSeed(seed []byte) Validator {
//lint:ignore SA1019 We don't care because this is only a test.
privKey := mock.PV{PrivKey: &ed25519.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)}}
return Validator{PV: privKey}
}

func NewValidatorFromIntSeed(i int) Validator {
iUint64 := uint64(i)
seed := []byte("AAAAAAAAabcdefghijklmnopqrstuvwx") // 8+24 bytes
binary.LittleEndian.PutUint64(seed[:8], iUint64)
return NewValidatorFromBytesSeed(seed)
}

func (v *Validator) TMProtoCryptoPublicKey() tmprotocrypto.PublicKey {
ret, err := sdkcryptocodec.ToTmProtoPublicKey(v.SDKPubKey())
if err != nil {
panic(err)
}
return ret
}

func (v *Validator) TMCryptoPubKey() tmcrypto.PubKey {
ret, err := v.GetPubKey()
if err != nil {
panic(err)
}
return ret
}

func (v *Validator) SDKPubKey() sdkcryptotypes.PubKey {
tmcryptoPubKey := v.TMCryptoPubKey()
ret, err := sdkcryptocodec.FromTmPubKeyInterface(tmcryptoPubKey)
if err != nil {
panic(err)
}
return ret
}
33 changes: 0 additions & 33 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package sample

import (
"encoding/binary"

cryptoEd25519 "crypto/ed25519"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v3/testing/mock"

tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
)

// AccAddress returns a sample account address
Expand All @@ -20,28 +12,3 @@ func AccAddress() string {
addr := pk.Address()
return sdk.AccAddress(addr).String()
}

func GetTMCryptoPublicKeyFromSeed(i uint64) (mock.PV, tmprotocrypto.PublicKey) {

fromSeed := func(seed []byte) (mock.PV, tmprotocrypto.PublicKey) {
//lint:ignore SA1019 We don't care because this is only a test.
privKey := mock.PV{PrivKey: &ed25519.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)}}
pubKey, err := privKey.GetPubKey()
if err != nil {
panic(err)
}
sdkKey, err := cryptocodec.FromTmPubKeyInterface(pubKey)
if err != nil {
panic(err)
}
tmProtoKey, err := cryptocodec.ToTmProtoPublicKey(sdkKey)
if err != nil {
panic(err)
}
return privKey, tmProtoKey
}

seed := []byte("AAAAAAAAabcdefghijklmnopqrstuvwx") // 8+24 bytes
binary.LittleEndian.PutUint64(seed[:8], i)
return fromSeed(seed)
}

0 comments on commit 983c87c

Please sign in to comment.