-
Notifications
You must be signed in to change notification settings - Fork 3.9k
op-node: Add p2p utils #3150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
op-node: Add p2p utils #3150
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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{ | ||
mslipper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.