Skip to content

Commit 037cf98

Browse files
fix: Implement gogoproto customtype to secp256r1 keys (#20027)
Co-authored-by: Marko <marko@baricevic.me>
1 parent ce373b6 commit 037cf98

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
113113
* [#19833](https://github.com/cosmos/cosmos-sdk/pull/19833) Fix some places in which we call Remove inside a Walk.
114114
* [#19851](https://github.com/cosmos/cosmos-sdk/pull/19851) Fix some places in which we call Remove inside a Walk (x/staking and x/gov).
115115
* (baseapp) [#19970](https://github.com/cosmos/cosmos-sdk/pull/19970) Fix default config values to use no-op mempool as default.
116+
* (crypto) [#20027](https://github.com/cosmos/cosmos-sdk/pull/20027) secp256r1 keys now implement gogoproto's customtype interface.
116117
* (x/bank) [#20028](https://github.com/cosmos/cosmos-sdk/pull/20028) Align query with multi denoms for send-enabled.
117118

118119
### API Breaking Changes

crypto/keys/secp256r1/privkey.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package secp256r1
22

33
import (
4+
"encoding/base64"
5+
46
"github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa"
57
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
68
)
79

10+
var _ customProtobufType = (*ecdsaSK)(nil)
11+
812
// GenPrivKey generates a new secp256r1 private key. It uses operating system randomness.
913
func GenPrivKey() (*PrivKey, error) {
1014
key, err := ecdsa.GenPrivKey(secp256r1)
@@ -52,6 +56,27 @@ type ecdsaSK struct {
5256
ecdsa.PrivKey
5357
}
5458

59+
// Marshal implements customProtobufType.
60+
func (sk ecdsaSK) Marshal() ([]byte, error) {
61+
return sk.PrivKey.Bytes(), nil
62+
}
63+
64+
// MarshalJSON implements customProtobufType.
65+
func (sk ecdsaSK) MarshalJSON() ([]byte, error) {
66+
b64 := base64.StdEncoding.EncodeToString(sk.PrivKey.Bytes())
67+
return []byte(b64), nil
68+
}
69+
70+
// UnmarshalJSON implements customProtobufType.
71+
func (sk *ecdsaSK) UnmarshalJSON(data []byte) error {
72+
bz, err := base64.StdEncoding.DecodeString(string(data))
73+
if err != nil {
74+
return err
75+
}
76+
77+
return sk.PrivKey.Unmarshal(bz, secp256r1, fieldSize)
78+
}
79+
5580
// Size implements proto.Marshaler interface
5681
func (sk *ecdsaSK) Size() int {
5782
if sk == nil {

crypto/keys/secp256r1/privkey_internal_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,14 @@ func (suite *SKSuite) TestSize() {
113113
var nilPk *ecdsaSK
114114
require.Equal(0, nilPk.Size(), "nil value must have zero size")
115115
}
116+
117+
func (suite *SKSuite) TestJson() {
118+
require := suite.Require()
119+
asd := suite.sk.(*PrivKey)
120+
bz, err := asd.Secret.MarshalJSON()
121+
require.NoError(err)
122+
123+
sk := &ecdsaSK{}
124+
require.NoError(sk.UnmarshalJSON(bz))
125+
require.Equal(suite.sk.(*PrivKey).Secret, sk)
126+
}

crypto/keys/secp256r1/pubkey.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
package secp256r1
22

33
import (
4+
"encoding/base64"
5+
46
cmtcrypto "github.com/cometbft/cometbft/crypto"
57
"github.com/cosmos/gogoproto/proto"
68

79
ecdsa "github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa"
810
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
911
)
1012

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+
1127
// String implements proto.Message interface.
1228
func (m *PubKey) String() string {
1329
return m.Key.String(name)
@@ -49,6 +65,27 @@ type ecdsaPK struct {
4965
ecdsa.PubKey
5066
}
5167

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+
5289
// Size implements proto.Marshaler interface
5390
func (pk *ecdsaPK) Size() int {
5491
if pk == nil {

crypto/keys/secp256r1/pubkey_internal_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,14 @@ func (suite *PKSuite) TestSize() {
126126
var nilPk *ecdsaPK
127127
require.Equal(0, nilPk.Size(), "nil value must have zero size")
128128
}
129+
130+
func (suite *PKSuite) TestJson() {
131+
require := suite.Require()
132+
133+
bz, err := suite.pk.Key.MarshalJSON()
134+
require.NoError(err)
135+
136+
pk := &ecdsaPK{}
137+
require.NoError(pk.UnmarshalJSON(bz))
138+
require.Equal(suite.pk.Key, pk)
139+
}

0 commit comments

Comments
 (0)