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