|
1 | 1 | package secp256r1 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/base64" |
| 5 | + |
4 | 6 | cmtcrypto "github.com/cometbft/cometbft/crypto" |
5 | 7 | "github.com/cosmos/gogoproto/proto" |
6 | 8 |
|
7 | 9 | ecdsa "github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa" |
8 | 10 | cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" |
9 | 11 | ) |
10 | 12 |
|
| 13 | +// customProtobufType is here to make sure that ecdsaPK and ecdsaSK implement the |
| 14 | +// gogoproto customtype interface. |
| 15 | +type customProtobufType interface { |
| 16 | + Marshal() ([]byte, error) |
| 17 | + MarshalTo(data []byte) (n int, err error) |
| 18 | + Unmarshal(data []byte) error |
| 19 | + Size() int |
| 20 | + |
| 21 | + MarshalJSON() ([]byte, error) |
| 22 | + UnmarshalJSON(data []byte) error |
| 23 | +} |
| 24 | + |
| 25 | +var _ customProtobufType = (*ecdsaPK)(nil) |
| 26 | + |
11 | 27 | // String implements proto.Message interface. |
12 | 28 | func (m *PubKey) String() string { |
13 | 29 | return m.Key.String(name) |
@@ -49,6 +65,27 @@ type ecdsaPK struct { |
49 | 65 | ecdsa.PubKey |
50 | 66 | } |
51 | 67 |
|
| 68 | +// Marshal implements customProtobufType. |
| 69 | +func (pk ecdsaPK) Marshal() ([]byte, error) { |
| 70 | + return pk.PubKey.Bytes(), nil |
| 71 | +} |
| 72 | + |
| 73 | +// MarshalJSON implements customProtobufType. |
| 74 | +func (pk ecdsaPK) MarshalJSON() ([]byte, error) { |
| 75 | + b64 := base64.StdEncoding.EncodeToString(pk.PubKey.Bytes()) |
| 76 | + return []byte(b64), nil |
| 77 | +} |
| 78 | + |
| 79 | +// UnmarshalJSON implements customProtobufType. |
| 80 | +func (pk *ecdsaPK) UnmarshalJSON(data []byte) error { |
| 81 | + bz, err := base64.StdEncoding.DecodeString(string(data)) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + return pk.PubKey.Unmarshal(bz, secp256r1, pubKeySize) |
| 87 | +} |
| 88 | + |
52 | 89 | // Size implements proto.Marshaler interface |
53 | 90 | func (pk *ecdsaPK) Size() int { |
54 | 91 | if pk == nil { |
|
0 commit comments