mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-22 03:30:35 +00:00
Fix Failures With Prysm Starting Up (#11103)
This commit is contained in:
parent
5a4edf897f
commit
a7c9c76b18
@ -25,7 +25,14 @@ func ConvertFromInterfacePrivKey(privkey crypto.PrivKey) (*ecdsa.PrivateKey, err
|
||||
}
|
||||
|
||||
func ConvertToInterfacePrivkey(privkey *ecdsa.PrivateKey) (crypto.PrivKey, error) {
|
||||
return crypto.UnmarshalSecp256k1PrivateKey(privkey.D.Bytes())
|
||||
privBytes := privkey.D.Bytes()
|
||||
// In the event the number of bytes outputted by the big-int are less than 32,
|
||||
// we append bytes to the start of the sequence for the missing most significant
|
||||
// bytes.
|
||||
if len(privBytes) < 32 {
|
||||
privBytes = append(make([]byte, 32-len(privBytes)), privBytes...)
|
||||
}
|
||||
return crypto.UnmarshalSecp256k1PrivateKey(privBytes)
|
||||
}
|
||||
|
||||
func ConvertToInterfacePubkey(pubkey *ecdsa.PublicKey) (crypto.PubKey, error) {
|
||||
|
@ -2,6 +2,8 @@ package ecdsa
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
@ -26,3 +28,20 @@ func TestConvertToInterfacePubkey(t *testing.T) {
|
||||
origRawKey := gcrypto.FromECDSAPub(pubkey)
|
||||
assert.DeepEqual(t, origRawKey, rawKey)
|
||||
}
|
||||
|
||||
func TestConvertToInterfacePrivkey_HandlesShorterKeys(t *testing.T) {
|
||||
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
||||
assert.NoError(t, err)
|
||||
rawBytes, err := priv.Raw()
|
||||
assert.NoError(t, err)
|
||||
// Zero-out most significant byte so that the big int normalizes
|
||||
// it by removing it.
|
||||
rawBytes[0] = 0
|
||||
privKey := new(ecdsa.PrivateKey)
|
||||
k := new(big.Int).SetBytes(rawBytes)
|
||||
privKey.D = k
|
||||
privKey.Curve = gcrypto.S256()
|
||||
privKey.X, privKey.Y = gcrypto.S256().ScalarBaseMult(rawBytes)
|
||||
_, err = ConvertToInterfacePrivkey(privKey)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user