forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmt.go
92 lines (78 loc) · 2.83 KB
/
cmt.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
package codec
import (
cmtcrypto "github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/encoding"
cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
"github.com/verzth/cosmos-sdk/errors"
"github.com/verzth/cosmos-sdk/crypto/keys/ed25519"
"github.com/verzth/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/verzth/cosmos-sdk/crypto/types"
sdkerrors "github.com/verzth/cosmos-sdk/types/errors"
)
// FromCmtProtoPublicKey converts a CMT's cmtprotocrypto.PublicKey into our own PubKey.
func FromCmtProtoPublicKey(protoPk cmtprotocrypto.PublicKey) (cryptotypes.PubKey, error) {
switch protoPk := protoPk.Sum.(type) {
case *cmtprotocrypto.PublicKey_Ed25519:
return &ed25519.PubKey{
Key: protoPk.Ed25519,
}, nil
case *cmtprotocrypto.PublicKey_Secp256K1:
return &secp256k1.PubKey{
Key: protoPk.Secp256K1,
}, nil
default:
return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from Tendermint public key", protoPk)
}
}
// ToCmtProtoPublicKey converts our own PubKey to Cmt's cmtprotocrypto.PublicKey.
func ToCmtProtoPublicKey(pk cryptotypes.PubKey) (cmtprotocrypto.PublicKey, error) {
switch pk := pk.(type) {
case *ed25519.PubKey:
return cmtprotocrypto.PublicKey{
Sum: &cmtprotocrypto.PublicKey_Ed25519{
Ed25519: pk.Key,
},
}, nil
case *secp256k1.PubKey:
return cmtprotocrypto.PublicKey{
Sum: &cmtprotocrypto.PublicKey_Secp256K1{
Secp256K1: pk.Key,
},
}, nil
default:
return cmtprotocrypto.PublicKey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to Tendermint public key", pk)
}
}
// FromCmtPubKeyInterface converts CMT's cmtcrypto.PubKey to our own PubKey.
func FromCmtPubKeyInterface(tmPk cmtcrypto.PubKey) (cryptotypes.PubKey, error) {
tmProtoPk, err := encoding.PubKeyToProto(tmPk)
if err != nil {
return nil, err
}
return FromCmtProtoPublicKey(tmProtoPk)
}
// ToCmtPubKeyInterface converts our own PubKey to CMT's cmtcrypto.PubKey.
func ToCmtPubKeyInterface(pk cryptotypes.PubKey) (cmtcrypto.PubKey, error) {
tmProtoPk, err := ToCmtProtoPublicKey(pk)
if err != nil {
return nil, err
}
return encoding.PubKeyFromProto(tmProtoPk)
}
// ----------------------
// Deprecated: use FromCmtProtoPublicKey instead.
func FromTmProtoPublicKey(protoPk cmtprotocrypto.PublicKey) (cryptotypes.PubKey, error) {
return FromCmtProtoPublicKey(protoPk)
}
// Deprecated: use ToCmtProtoPublicKey instead.
func ToTmProtoPublicKey(pk cryptotypes.PubKey) (cmtprotocrypto.PublicKey, error) {
return ToCmtProtoPublicKey(pk)
}
// Deprecated: use FromCmtPubKeyInterface instead.
func FromTmPubKeyInterface(tmPk cmtcrypto.PubKey) (cryptotypes.PubKey, error) {
return FromCmtPubKeyInterface(tmPk)
}
// Deprecated: use ToCmtPubKeyInterface instead.
func ToTmPubKeyInterface(pk cryptotypes.PubKey) (cmtcrypto.PubKey, error) {
return ToCmtPubKeyInterface(pk)
}