Skip to content

Commit 0983435

Browse files
authored
fix(client/validator): use correct secp256k1 key uncompression (#51)
1 parent 398936e commit 0983435

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

client/cmd/key_utils.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111

12+
"github.com/decred/dcrd/dcrec/secp256k1"
1213
"github.com/ethereum/go-ethereum/crypto"
1314

1415
"github.com/piplabs/story/lib/errors"
@@ -30,17 +31,16 @@ func decodeAndUncompressPubKey(compressedPubKeyBase64 string) (string, error) {
3031
if err != nil {
3132
return "", errors.Wrap(err, "failed to decode base64 public key")
3233
}
33-
if len(compressedPubKeyBytes) != 33 {
34+
if len(compressedPubKeyBytes) != secp256k1.PubKeyBytesLenCompressed {
3435
return "", fmt.Errorf("invalid compressed public key length: %d", len(compressedPubKeyBytes))
3536
}
3637

37-
curve := elliptic.P256()
38-
x, y := elliptic.UnmarshalCompressed(curve, compressedPubKeyBytes)
39-
if x == nil || y == nil {
40-
return "", errors.New("failed to unmarshal compressed public key")
38+
pubKey, err := secp256k1.ParsePubKey(compressedPubKeyBytes)
39+
if err != nil {
40+
return "", errors.Wrap(err, "failed to parse compressed public key")
4141
}
4242

43-
uncompressedPubKeyBytes := elliptic.Marshal(curve, x, y)
43+
uncompressedPubKeyBytes := pubKey.SerializeUncompressed()
4444
uncompressedPubKeyHex := hex.EncodeToString(uncompressedPubKeyBytes)
4545

4646
return uncompressedPubKeyHex, nil

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ require (
252252

253253
require (
254254
cosmossdk.io/x/upgrade v0.1.4
255+
github.com/decred/dcrd/dcrec/secp256k1 v1.0.4
255256
github.com/go-playground/validator/v10 v10.11.1
256257
github.com/joho/godotenv v1.5.1
257258
)
@@ -264,6 +265,7 @@ require (
264265
github.com/aws/aws-sdk-go v1.44.224 // indirect
265266
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
266267
github.com/chzyer/readline v1.5.1 // indirect
268+
github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0 // indirect
267269
github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 // indirect
268270
github.com/go-playground/locales v0.14.0 // indirect
269271
github.com/go-playground/universal-translator v0.18.0 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,15 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
425425
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
426426
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
427427
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
428+
github.com/decred/dcrd/chaincfg/chainhash v1.0.2 h1:rt5Vlq/jM3ZawwiacWjPa+smINyLRN07EO0cNBV6DGU=
429+
github.com/decred/dcrd/chaincfg/chainhash v1.0.2/go.mod h1:BpbrGgrPTr3YJYRN3Bm+D9NuaFd+zGyNeIKgrhCXK60=
430+
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
428431
github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y=
429432
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
433+
github.com/decred/dcrd/dcrec/secp256k1 v1.0.4 h1:0XErmfJBiVbl0NvyclGn4jr+1hIylDf5beFi9W0o7Fc=
434+
github.com/decred/dcrd/dcrec/secp256k1 v1.0.4/go.mod h1:00z7mJdugt+GBAzPN1QrDRGCXxyKUiexEHu6ukxEw3k=
435+
github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0 h1:3GIJYXQDAKpLEFriGFN8SbSffak10UXHGdIcFaMPykY=
436+
github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0/go.mod h1:3s92l0paYkZoIHuj4X93Teg/HB7eGM9x/zokGw+u4mY=
430437
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
431438
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
432439
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f h1:U5y3Y5UE0w7amNe7Z5G/twsBW0KEalRQXZzf8ufSh9I=

0 commit comments

Comments
 (0)