Skip to content

Commit 1572b8e

Browse files
PercobainAlex | Interchain Labs
andauthored
feat: Add NewPubKeyFromBytes for secp256r1 to create PubKey from bytes (#24919)
Co-authored-by: Alex | Interchain Labs <[email protected]>
1 parent 131aa70 commit 1572b8e

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4949

5050
### Features
5151

52+
* (crypto) [#24919](https://github.com/cosmos/cosmos-sdk/pull/24919) add `NewPubKeyFromBytes` function to the `secp256r1` package to create `PubKey` from bytes
5253
* (server) [#24720](https://github.com/cosmos/cosmos-sdk/pull/24720) add `verbose_log_level` flag for configuring the log level when switching to verbose logging mode during sensitive operations (such as chain upgrades).
5354
* (crypto) [#24861](https://github.com/cosmos/cosmos-sdk/pull/24861) add `PubKeyFromCometTypeAndBytes` helper function to convert from `comet/v2` PubKeys to the `cryptotypes.Pubkey` interface.
5455

crypto/keys/secp256r1/pubkey.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
cmtcrypto "github.com/cometbft/cometbft/v2/crypto"
77
"github.com/cosmos/gogoproto/proto"
88

9+
errorsmod "cosmossdk.io/errors"
10+
911
ecdsa "github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa"
1012
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
13+
"github.com/cosmos/cosmos-sdk/types/errors"
1114
)
1215

1316
// customProtobufType is here to make sure that ecdsaPK and ecdsaSK implement the
@@ -99,3 +102,18 @@ func (pk *ecdsaPK) Size() int {
99102
func (pk *ecdsaPK) Unmarshal(bz []byte) error {
100103
return pk.PubKey.Unmarshal(bz, secp256r1, pubKeySize)
101104
}
105+
106+
// NewPubKeyFromBytes creates a secp256r1 PubKey from bytes.
107+
func NewPubKeyFromBytes(bytes []byte) (*PubKey, error) {
108+
if len(bytes) != pubKeySize {
109+
return nil, errorsmod.Wrapf(errors.ErrInvalidPubKey,
110+
"wrong secp256r1 pubkey size, expecting %d bytes, got %d",
111+
pubKeySize, len(bytes))
112+
}
113+
pk := &ecdsaPK{}
114+
err := pk.Unmarshal(bytes)
115+
if err != nil {
116+
return nil, errorsmod.Wrap(errors.ErrInvalidPubKey, err.Error())
117+
}
118+
return &PubKey{Key: pk}, nil
119+
}

crypto/keys/secp256r1/pubkey_internal_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,25 @@ func (suite *PKSuite) TestJson() {
137137
require.NoError(pk.UnmarshalJSON(bz))
138138
require.Equal(suite.pk.Key, pk)
139139
}
140+
141+
func (suite *PKSuite) TestNewPubKeyFromBytes() {
142+
require := suite.Require()
143+
144+
originalBytes := suite.pk.Bytes()
145+
newPk, err := NewPubKeyFromBytes(originalBytes)
146+
require.NoError(err)
147+
require.NotNil(newPk)
148+
require.True(newPk.Equals(suite.pk))
149+
require.Equal(originalBytes, newPk.Bytes())
150+
151+
_, err = NewPubKeyFromBytes([]byte{1, 2, 3})
152+
require.Error(err)
153+
154+
_, err = NewPubKeyFromBytes(nil)
155+
require.Error(err)
156+
157+
invalidBytes := make([]byte, pubKeySize)
158+
invalidBytes[0] = 0x04
159+
_, err = NewPubKeyFromBytes(invalidBytes)
160+
require.Error(err)
161+
}

0 commit comments

Comments
 (0)