-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathpubkey.go
101 lines (82 loc) · 2.38 KB
/
pubkey.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
package secp256r1
import (
"encoding/base64"
cmtcrypto "github.com/cometbft/cometbft/crypto"
"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
// customProtobufType is here to make sure that ecdsaPK and ecdsaSK implement the
// gogoproto customtype interface.
type customProtobufType interface {
Marshal() ([]byte, error)
MarshalTo(data []byte) (n int, err error)
Unmarshal(data []byte) error
Size() int
MarshalJSON() ([]byte, error)
UnmarshalJSON(data []byte) error
}
var _ customProtobufType = (*ecdsaPK)(nil)
// String implements proto.Message interface.
func (m *PubKey) String() string {
return m.Key.String(name)
}
// Bytes implements SDK PubKey interface.
func (m *PubKey) Bytes() []byte {
if m == nil {
return nil
}
return m.Key.Bytes()
}
// Equals implements SDK PubKey interface.
func (m *PubKey) Equals(other cryptotypes.PubKey) bool {
pk2, ok := other.(*PubKey)
if !ok {
return false
}
return m.Key.Equal(&pk2.Key.PublicKey)
}
// Address implements SDK PubKey interface.
func (m *PubKey) Address() cmtcrypto.Address {
return m.Key.Address(proto.MessageName(m))
}
// Type returns key type name. Implements SDK PubKey interface.
func (m *PubKey) Type() string {
return name
}
// VerifySignature implements SDK PubKey interface.
func (m *PubKey) VerifySignature(msg, sig []byte) bool {
return m.Key.VerifySignature(msg, sig)
}
type ecdsaPK struct {
ecdsa.PubKey
}
// Marshal implements customProtobufType.
func (pk ecdsaPK) Marshal() ([]byte, error) {
return pk.PubKey.Bytes(), nil
}
// MarshalJSON implements customProtobufType.
func (pk ecdsaPK) MarshalJSON() ([]byte, error) {
b64 := base64.StdEncoding.EncodeToString(pk.PubKey.Bytes())
return []byte("\"" + b64 + "\""), nil
}
// UnmarshalJSON implements customProtobufType.
func (pk *ecdsaPK) UnmarshalJSON(data []byte) error {
// the string is quoted so we need to remove them
bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
if err != nil {
return err
}
return pk.PubKey.Unmarshal(bz, secp256r1, pubKeySize)
}
// Size implements proto.Marshaler interface
func (pk *ecdsaPK) Size() int {
if pk == nil {
return 0
}
return pubKeySize
}
// Unmarshal implements proto.Marshaler interface
func (pk *ecdsaPK) Unmarshal(bz []byte) error {
return pk.PubKey.Unmarshal(bz, secp256r1, pubKeySize)
}