forked from crypto-com/chain-indexing
/
tmpubkey.go
253 lines (205 loc) · 7.3 KB
/
tmpubkey.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package tmcosmosutils
import (
"bytes"
"fmt"
"sort"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/btcsuite/btcutil/bech32"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
)
func TmAddressFromTmPubKey(pubKey []byte) string {
key := make(tmed25519.PubKey, ed25519.PubKeySize)
copy(key, pubKey)
return key.Address().String()
}
func MustConsensusAddressFromTmPubKey(bech32Prefix string, pubKey []byte) string {
address, err := ConsensusNodeAddressFromTmPubKey(bech32Prefix, pubKey)
if err != nil {
panic(err)
}
return address
}
func ConsensusNodeAddressFromTmPubKey(bech32Prefix string, pubKey []byte) (string, error) {
cosmosPubKey := &ed25519.PubKey{
Key: pubKey,
}
conv, err := bech32.ConvertBits(cosmosPubKey.Address().Bytes(), 8, 5, true)
if err != nil {
return "", fmt.Errorf("error converting tendermint public key to bech32 bits: %v", err)
}
address, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus address: %v", err)
}
return address, nil
}
func MustConsensusNodePubKeyFromTmPubKey(bech32Prefix string, pubKey []byte) string {
address, err := ConsensusNodePubKeyFromTmPubKey(bech32Prefix, pubKey)
if err != nil {
panic(err)
}
return address
}
func ConsensusNodePubKeyFromTmPubKey(bech32Prefix string, pubKey []byte) (string, error) {
pkToMarshal := ed25519.PubKey{Key: pubKey}
conv, err := bech32.ConvertBits(legacy.Cdc.MustMarshal(pkToMarshal), 8, 5, true)
if err != nil {
return "", fmt.Errorf("error converting tendermint public key to bech32 bits: %v", err)
}
address, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus public key: %v", err)
}
return address, nil
}
func MustAccountAddressFromValidatorAddress(bech32Prefix string, srcAddress string) string {
return MustValidatorAddressFromAccountAddress(bech32Prefix, srcAddress)
}
func AccountAddressFromValidatorAddress(bech32Prefix string, srcAddress string) (string, error) {
return ValidatorAddressFromAccountAddress(bech32Prefix, srcAddress)
}
func MustValidatorAddressFromAccountAddress(bech32Prefix string, srcAddress string) string {
address, err := ValidatorAddressFromAccountAddress(bech32Prefix, srcAddress)
if err != nil {
panic(err)
}
return address
}
func ValidatorAddressFromAccountAddress(bech32Prefix string, srcAddress string) (string, error) {
_, conv, err := bech32.Decode(srcAddress)
if err != nil {
return "", fmt.Errorf("error Decoding provided address: %v", err)
}
validatorAddress, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus address: %v", err)
}
return validatorAddress, nil
}
func ConsensusNodeAddressFromConsensusNodePubKey(bech32Prefix string, consensusNodePubKey string) (string, error) {
_, conv, err := bech32.Decode(consensusNodePubKey)
if err != nil {
return "", fmt.Errorf("error converting consensus node pubkey to address")
}
pkToUnmarshal, err := bech32.ConvertBits(conv, 5, 8, false)
if err != nil {
return "", fmt.Errorf("error converting bech32 bits to tendermint public key: %v", err)
}
var pubKey cryptotypes.PubKey
legacy.Cdc.MustUnmarshal(pkToUnmarshal, &pubKey)
conv, err = bech32.ConvertBits(pubKey.Address().Bytes(), 8, 5, true)
if err != nil {
return "", fmt.Errorf("error converting tendermint public key to bech32 bits: %v", err)
}
address, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus address: %v", err)
}
return address, nil
}
func MustAccountAddressFromPubKey(bech32Prefix string, pubKey []byte) string {
address, err := AccountAddressFromPubKey(bech32Prefix, pubKey)
if err != nil {
panic(err)
}
return address
}
func AccountAddressFromPubKey(bech32Prefix string, pubKey []byte) (string, error) {
pkToMarshal := secp256k1.PubKey{Key: pubKey}
conv, err := bech32.ConvertBits(pkToMarshal.Address().Bytes(), 8, 5, true)
if err != nil {
return "", fmt.Errorf("error converting tendermint public key to bech32 bits: %v", err)
}
address, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus address: %v", err)
}
return address, nil
}
func AccountAddressFromBytes(bech32Prefix string, b []byte) (string, error) {
conv, err := bech32.ConvertBits(b, 8, 5, true)
if err != nil {
return "", fmt.Errorf("error converting tendermint public key to bech32 bits: %v", err)
}
address, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus address: %v", err)
}
return address, nil
}
func MustModuleAccountFromBytes(bech32Prefix string, b []byte) string {
address, err := ModuleAccountFromBytes(bech32Prefix, b)
if err != nil {
panic(err)
}
return address
}
func ModuleAccountFromBytes(bech32Prefix string, b []byte) (string, error) {
conv, err := bech32.ConvertBits(b, 8, 5, true)
if err != nil {
return "", fmt.Errorf("error converting tendermint public key to bech32 bits: %v", err)
}
address, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus address: %v", err)
}
return address, nil
}
func PubKeyFromCosmosPubKey(accountPubKey string) ([]byte, error) {
_, conv, err := bech32.Decode(accountPubKey)
if err != nil {
return nil, fmt.Errorf("error bech32 decoding Cosmos public key")
}
pkToUnmarshal, err := bech32.ConvertBits(conv, 5, 8, false)
if err != nil {
return nil, fmt.Errorf("error converting bech32 bits to public key: %v", err)
}
var pubKey cryptotypes.PubKey
legacy.Cdc.MustUnmarshal(pkToUnmarshal, &pubKey)
return pubKey.Bytes(), nil
}
func MustMultiSigAddressFromPubKeys(
bech32Prefix string,
pubKeys [][]byte,
threshold int,
sortPubKeys bool,
) string {
address, err := MultiSigAddressFromPubKeys(bech32Prefix, pubKeys, threshold, sortPubKeys)
if err != nil {
panic(err)
}
return address
}
func MultiSigAddressFromPubKeys(bech32Prefix string, pubKeys [][]byte, threshold int, sortPubKeys bool) (string, error) {
pks := make([]cryptotypes.PubKey, 0, len(pubKeys))
for _, pubKey := range pubKeys {
pks = append(pks, &secp256k1.PubKey{Key: pubKey})
}
if sortPubKeys {
sort.Slice(pks, func(i, j int) bool {
return bytes.Compare(pks[i].Address(), pks[j].Address()) < 0
})
}
aminoPubKey := multisig.NewLegacyAminoPubKey(threshold, pks)
conv, err := bech32.ConvertBits(aminoPubKey.Address().Bytes(), 8, 5, true)
if err != nil {
return "", fmt.Errorf("error converting tendermint public key to bech32 bits: %v", err)
}
address, err := bech32.Encode(bech32Prefix, conv)
if err != nil {
return "", fmt.Errorf("error encoding tendermint public key bits to consensus address: %v", err)
}
return address, nil
}
func IsValidCosmosAddress(address string) bool {
_, conv, err := bech32.Decode(address)
if err != nil {
return false
}
_, err = bech32.ConvertBits(conv, 5, 8, false)
return err == nil
}