Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Implement gogoproto customtype to secp256r1 keys (backport #20027) #20032

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (crypto) [#20027](https://github.com/cosmos/cosmos-sdk/pull/20027) secp256r1 keys now implement gogoproto's customtype interface.
* (x/gov) [#19725](https://github.com/cosmos/cosmos-sdk/pull/19725) Fetch a failed proposal tally from proposal.FinalTallyResult in the gprc query.
* (crypto) [#19691](https://github.com/cosmos/cosmos-sdk/pull/19746) Fix tx sign doesn't throw an error when incorrect Ledger is used.

Expand Down
25 changes: 25 additions & 0 deletions crypto/keys/secp256r1/privkey.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package secp256r1

import (
"encoding/base64"

"github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

var _ customProtobufType = (*ecdsaSK)(nil)

// GenPrivKey generates a new secp256r1 private key. It uses operating system randomness.
func GenPrivKey() (*PrivKey, error) {
key, err := ecdsa.GenPrivKey(secp256r1)
Expand Down Expand Up @@ -52,6 +56,27 @@ type ecdsaSK struct {
ecdsa.PrivKey
}

// Marshal implements customProtobufType.
func (sk ecdsaSK) Marshal() ([]byte, error) {
return sk.PrivKey.Bytes(), nil
}

// MarshalJSON implements customProtobufType.
func (sk ecdsaSK) MarshalJSON() ([]byte, error) {
b64 := base64.StdEncoding.EncodeToString(sk.PrivKey.Bytes())
return []byte(b64), nil
}

// UnmarshalJSON implements customProtobufType.
func (sk *ecdsaSK) UnmarshalJSON(data []byte) error {
bz, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return err
}

return sk.PrivKey.Unmarshal(bz, secp256r1, fieldSize)
}

// Size implements proto.Marshaler interface
func (sk *ecdsaSK) Size() int {
if sk == nil {
Expand Down
11 changes: 11 additions & 0 deletions crypto/keys/secp256r1/privkey_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,14 @@ func (suite *SKSuite) TestSize() {
var nilPk *ecdsaSK
require.Equal(0, nilPk.Size(), "nil value must have zero size")
}

func (suite *SKSuite) TestJson() {
require := suite.Require()
asd := suite.sk.(*PrivKey)
bz, err := asd.Secret.MarshalJSON()
require.NoError(err)

sk := &ecdsaSK{}
require.NoError(sk.UnmarshalJSON(bz))
require.Equal(suite.sk.(*PrivKey).Secret, sk)
}
41 changes: 41 additions & 0 deletions crypto/keys/secp256r1/pubkey.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
package secp256r1

import (
<<<<<<< HEAD

Check failure on line 4 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / split-test-files

expected 'STRING', found '<<'

Check failure on line 4 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found '<<' (typecheck)

Check failure on line 4 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 4 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 4 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path
tmcrypto "github.com/cometbft/cometbft/crypto"

Check failure on line 5 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

could not import github.com/cometbft/cometbft/crypto (no metadata for github.com/cometbft/cometbft/crypto)
=======

Check failure on line 6 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path
"encoding/base64"

cmtcrypto "github.com/cometbft/cometbft/crypto"
>>>>>>> 037cf98f7 (fix: Implement gogoproto customtype to secp256r1 keys (#20027))

Check failure on line 10 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

illegal character U+0023 '#' (typecheck)

Check failure on line 10 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 10 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected ';', found ':'

Check failure on line 10 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

illegal character U+0023 '#'
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cosmos/gogoproto/proto"

ecdsa "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 {

Check failure on line 19 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'type' (typecheck)

Check failure on line 19 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path
Marshal() ([]byte, error)

Check failure on line 20 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected ';', found '('
MarshalTo(data []byte) (n int, err error)
Unmarshal(data []byte) error
Size() int

MarshalJSON() ([]byte, error)
UnmarshalJSON(data []byte) error
}

var _ customProtobufType = (*ecdsaPK)(nil)

Check failure on line 29 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'var' (typecheck)

// String implements proto.Message interface.
func (m *PubKey) String() string {
return m.Key.String(name)

Check failure on line 33 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'return' (typecheck)
}

// Bytes implements SDK PubKey interface.
func (m *PubKey) Bytes() []byte {
if m == nil {

Check failure on line 38 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'if' (typecheck)
return nil

Check failure on line 39 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'return' (typecheck)
}
return m.Key.Bytes()

Check failure on line 41 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'return' (typecheck)
}

// Equals implements SDK PubKey interface.
func (m *PubKey) Equals(other cryptotypes.PubKey) bool {
pk2, ok := other.(*PubKey)
if !ok {

Check failure on line 47 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'if' (typecheck)
return false

Check failure on line 48 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found 'return' (typecheck)
}
return m.Key.Equal(&pk2.Key.PublicKey)
}
Expand All @@ -46,9 +66,30 @@
}

type ecdsaPK struct {
ecdsa.PubKey

Check failure on line 69 in crypto/keys/secp256r1/pubkey.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: ecdsa
}

// 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 {
bz, err := base64.StdEncoding.DecodeString(string(data))
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 {
Expand Down
11 changes: 11 additions & 0 deletions crypto/keys/secp256r1/pubkey_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,14 @@ func (suite *PKSuite) TestSize() {
var nilPk *ecdsaPK
require.Equal(0, nilPk.Size(), "nil value must have zero size")
}

func (suite *PKSuite) TestJson() {
require := suite.Require()

bz, err := suite.pk.Key.MarshalJSON()
require.NoError(err)

pk := &ecdsaPK{}
require.NoError(pk.UnmarshalJSON(bz))
require.Equal(suite.pk.Key, pk)
}
Loading