-
Notifications
You must be signed in to change notification settings - Fork 108
/
crypto.go
115 lines (95 loc) · 4.05 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package crypto
import (
cryptoEd25519 "crypto/ed25519"
"encoding/binary"
sdkcryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdkcryptoEd25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdkcryptoSecp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
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/cometbft/cometbft/crypto"
tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
tmtypes "github.com/cometbft/cometbft/types"
providertypes "github.com/cosmos/interchain-security/v3/x/ccv/provider/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' seed.
type CryptoIdentity struct {
// private key for validators to run consensus
consensus sdkcryptotypes.PrivKey
// key for validator operator account
operator sdkcryptotypes.PrivKey
}
func NewCryptoIdentityFromBytesSeed(seed []byte) *CryptoIdentity {
//lint:ignore SA1019 We don't care because this is only a test.
consKey := &sdkcryptoEd25519.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)} //nolint:staticcheck // SA1019: sdkcryptoEd25519.PrivKey is deprecated: PrivKey defines a ed25519 private key. NOTE: ed25519 keys must not be used in SDK apps except in a tendermint validator context.
opKey := sdkcryptoSecp256k1.GenPrivKeyFromSecret(seed)
return &CryptoIdentity{
consensus: consKey,
operator: opKey,
}
}
func NewCryptoIdentityFromIntSeed(i int) *CryptoIdentity {
iUint64 := uint64(i)
seed := []byte("AAAAAAAAabcdefghijklmnopqrstuvwx") // 8+24 bytes
binary.LittleEndian.PutUint64(seed[:8], iUint64)
return NewCryptoIdentityFromBytesSeed(seed)
}
// GenMultipleCryptoIds generates and returns multiple CryptoIdentities from a starting int seed.
func GenMultipleCryptoIds(num, fromIntSeed int) []*CryptoIdentity {
ids := make([]*CryptoIdentity, num)
for i := 0; i < num; i++ {
ids[i] = NewCryptoIdentityFromIntSeed(fromIntSeed + i)
}
return ids
}
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.ConsensusSDKPubKey())
if err != nil {
panic(err)
}
return ret
}
func (v *CryptoIdentity) TMCryptoPubKey() tmcrypto.PubKey {
ret, err := sdkcryptocodec.ToTmPubKeyInterface(v.ConsensusSDKPubKey())
if err != nil {
panic(err)
}
return ret
}
func (v *CryptoIdentity) SDKStakingValidator() sdkstakingtypes.Validator {
ret, err := sdkstakingtypes.NewValidator(v.SDKValOpAddress(), v.ConsensusSDKPubKey(), sdkstakingtypes.Description{})
if err != nil {
panic(err)
}
return ret
}
func (v *CryptoIdentity) ConsensusSDKPubKey() sdkcryptotypes.PubKey {
return v.consensus.PubKey()
}
func (v *CryptoIdentity) OperatorSDKPubKey() sdkcryptotypes.PubKey {
return v.operator.PubKey()
}
func (v *CryptoIdentity) SDKValOpAddress() sdktypes.ValAddress {
return sdktypes.ValAddress(v.OperatorSDKPubKey().Address())
}
func (v *CryptoIdentity) SDKValConsAddress() sdktypes.ConsAddress {
return sdktypes.ConsAddress(v.ConsensusSDKPubKey().Address())
}
// Returns the cons address of the crypto identity as a ProviderConsAddress.
// In most cases, one crypto identity should NOT be casted to both a ProviderConsAddress and ConsumerConsAddress.
// However, test intention is left to the caller.
func (v *CryptoIdentity) ProviderConsAddress() providertypes.ProviderConsAddress {
return providertypes.NewProviderConsAddress(v.SDKValConsAddress())
}
// Returns the cons address of the crypto identity as a ConsumerConsAddress.
// In most cases, one crypto identity should NOT be casted to both a ProviderConsAddress and ConsumerConsAddress.
// However, test intention is left to the caller.
func (v *CryptoIdentity) ConsumerConsAddress() providertypes.ConsumerConsAddress {
return providertypes.NewConsumerConsAddress(v.SDKValConsAddress())
}