forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
op-node: Add p2p utils (ethereum-optimism#3153)
* op-node: Add p2p utils * Update op-node/flags/flags.go Co-authored-by: Diederik Loerakker <[email protected]> Co-authored-by: Diederik Loerakker <[email protected]>
- Loading branch information
1 parent
0c5ad1d
commit 0e7184b
Showing
6 changed files
with
199 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package p2p | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func Priv2PeerID(r io.Reader) (string, error) { | ||
b, err := readHexData(r) | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
p, err := crypto.UnmarshalSecp256k1PrivateKey(b) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse priv key from %d bytes: %w", len(b), err) | ||
} | ||
|
||
pid, err := peer.IDFromPrivateKey(p) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse peer ID from private key: %w", err) | ||
} | ||
return pid.String(), nil | ||
} | ||
|
||
func Pub2PeerID(r io.Reader) (string, error) { | ||
b, err := readHexData(r) | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
p, err := crypto.UnmarshalSecp256k1PublicKey(b) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse pub key from %d bytes: %w", len(b), err) | ||
} | ||
|
||
pid, err := peer.IDFromPublicKey(p) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse peer ID from public key: %w", err) | ||
} | ||
|
||
return pid.String(), nil | ||
} | ||
|
||
func readHexData(r io.Reader) ([]byte, error) { | ||
data, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rawStr := strings.TrimSpace(string(data)) | ||
rawStr = strings.TrimPrefix(rawStr, "0x") | ||
b, err := hex.DecodeString(rawStr) | ||
if err != nil { | ||
return nil, fmt.Errorf("p2p key is not formatted in hex chars: %w", err) | ||
} | ||
return b, nil | ||
} | ||
|
||
var Subcommands = cli.Commands{ | ||
{ | ||
Name: "priv2id", | ||
Usage: "Reads a private key from STDIN, and returns a peer ID", | ||
Action: func(ctx *cli.Context) error { | ||
key, err := Priv2PeerID(os.Stdin) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(key) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "pub2id", | ||
Usage: "Reads a public key from STDIN, and returns a peer ID", | ||
Action: func(ctx *cli.Context) error { | ||
key, err := Pub2PeerID(os.Stdin) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(key) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "genkey", | ||
Usage: "Generates a private key", | ||
Action: func(ctx *cli.Context) error { | ||
buf := make([]byte, 32) | ||
if _, err := rand.Read(buf); err != nil { | ||
return fmt.Errorf("failed to get entropy: %w", err) | ||
} | ||
fmt.Println(hex.EncodeToString(buf)) | ||
return nil | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package p2p | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPrivPub2PeerID(t *testing.T) { | ||
priv, pub, err := crypto.GenerateKeyPair(crypto.Secp256k1, 32) | ||
require.NoError(t, err) | ||
privRaw, err := priv.Raw() | ||
require.NoError(t, err) | ||
pubRaw, err := pub.Raw() | ||
require.NoError(t, err) | ||
|
||
t.Run("with a private key", func(t *testing.T) { | ||
privPidLib, err := peer.IDFromPrivateKey(priv) | ||
require.NoError(t, err) | ||
privPidImpl, err := Priv2PeerID(bytes.NewReader([]byte(hex.EncodeToString(privRaw)))) | ||
require.NoError(t, err) | ||
require.Equal(t, privPidLib.String(), privPidImpl) | ||
}) | ||
t.Run("with a public key", func(t *testing.T) { | ||
pubPidLib, err := peer.IDFromPublicKey(pub) | ||
require.NoError(t, err) | ||
pubPidImpl, err := Pub2PeerID(bytes.NewReader([]byte(hex.EncodeToString(pubRaw)))) | ||
require.NoError(t, err) | ||
require.Equal(t, pubPidLib.String(), pubPidImpl) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters