mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 07:28:06 -05:00
Use the raw bytes, not the libp2p protobuf container for sepc256k1 private keys (#3445)
* use the raw bytes, not the libp2p protobuf container for sepc256k1 private keys * fix tests
This commit is contained in:
@@ -3,6 +3,7 @@ package p2p
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -21,17 +22,17 @@ func TestPrivateKeyLoading(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Could not generate key: %v", err)
|
||||
}
|
||||
marshalledKey, err := crypto.MarshalPrivateKey(key)
|
||||
raw, err := key.Raw()
|
||||
if err != nil {
|
||||
t.Fatalf("Could not marshal key %v", err)
|
||||
panic(err)
|
||||
}
|
||||
encodedKey := crypto.ConfigEncodeKey(marshalledKey)
|
||||
out := hex.EncodeToString(raw)
|
||||
|
||||
err = ioutil.WriteFile(file.Name(), []byte(encodedKey), 0600)
|
||||
err = ioutil.WriteFile(file.Name(), []byte(out), 0600)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not write key to file: %v", err)
|
||||
}
|
||||
log.WithField("file", file.Name()).WithField("key", encodedKey).Info("Wrote key to file")
|
||||
log.WithField("file", file.Name()).WithField("key", out).Info("Wrote key to file")
|
||||
cfg := &Config{
|
||||
PrivateKey: file.Name(),
|
||||
Encoding: "ssz",
|
||||
|
||||
@@ -3,11 +3,13 @@ package p2p
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/shared/iputils"
|
||||
)
|
||||
|
||||
@@ -40,18 +42,19 @@ func privKey(cfg *Config) (*ecdsa.PrivateKey, error) {
|
||||
convertedKey := convertFromInterfacePrivKey(priv)
|
||||
return convertedKey, nil
|
||||
}
|
||||
privateKey, err := ioutil.ReadFile(cfg.PrivateKey)
|
||||
src, err := ioutil.ReadFile(cfg.PrivateKey)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error reading private key from file")
|
||||
return nil, err
|
||||
}
|
||||
b, err := crypto.ConfigDecodeKey(string(privateKey))
|
||||
dst := make([]byte, hex.DecodedLen(len(src)))
|
||||
_, err = hex.Decode(dst, src)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, errors.Wrap(err, "failed to decode hex string")
|
||||
}
|
||||
unmarshalledKey, err := crypto.UnmarshalPrivateKey(b)
|
||||
unmarshalledKey, err := crypto.UnmarshalSecp256k1PrivateKey(dst)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
priv := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(unmarshalledKey.(*crypto.Secp256k1PrivateKey)))
|
||||
return priv, nil
|
||||
|
||||
@@ -12,6 +12,7 @@ package main
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -98,11 +99,11 @@ func createLocalNode(privKey *ecdsa.PrivateKey, ipAddr net.IP, port int) (*enode
|
||||
func extractPrivateKey() *ecdsa.PrivateKey {
|
||||
var privKey *ecdsa.PrivateKey
|
||||
if *privateKey != "" {
|
||||
b, err := crypto.ConfigDecodeKey(*privateKey)
|
||||
dst, err := hex.DecodeString(*privateKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
unmarshalledKey, err := crypto.UnmarshalPrivateKey(b)
|
||||
unmarshalledKey, err := crypto.UnmarshalSecp256k1PrivateKey(dst)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -115,12 +116,11 @@ func extractPrivateKey() *ecdsa.PrivateKey {
|
||||
}
|
||||
privKey = (*ecdsa.PrivateKey)((*btcec.PrivateKey)(privInterfaceKey.(*crypto.Secp256k1PrivateKey)))
|
||||
log.Warning("No private key was provided. Using default/random private key")
|
||||
|
||||
b, err := privInterfaceKey.Bytes()
|
||||
b, err := privInterfaceKey.Raw()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Debugf("Private key %s", crypto.ConfigEncodeKey(b))
|
||||
log.Debugf("Private key %x", b)
|
||||
}
|
||||
|
||||
return privKey
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -64,12 +65,12 @@ func TestPrivateKey_ParsesCorrectly(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
marshalledKey, err := crypto.MarshalPrivateKey(privKey)
|
||||
|
||||
pk, err := privKey.Raw()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encodedKey := crypto.ConfigEncodeKey(marshalledKey)
|
||||
*privateKey = encodedKey
|
||||
*privateKey = fmt.Sprintf("%x", pk)
|
||||
|
||||
extractedKey := extractPrivateKey()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user