Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Added support for loading keypair as file used in authentication in direct mode#118

Open
andreasbros wants to merge 2 commits intomxinden:masterfrom
andreasbros:feature/keypair-file
Open

Added support for loading keypair as file used in authentication in direct mode#118
andreasbros wants to merge 2 commits intomxinden:masterfrom
andreasbros:feature/keypair-file

Conversation

@andreasbros
Copy link

There is a need to have ability to load existing keypair file used for authentication with P2P nodes which only allow known PeerIds to connect (https://docs.rs/libp2p/latest/libp2p/allow_block_list/index.html#structs).

Proposed change adds additional parameter in "direct" mode to load keypair from given file path.
This only applies to direct mode connecting to a given node which requires authentication with "whitelisted" PeerId.
Currently it only supports base58 encoded files.

Example:
libp2p-lookup direct --address /dns/node-1/tcp/55123 --keypair-path ~/path/to/key.base58

@mxinden
Copy link
Owner

mxinden commented Jun 26, 2023

There is a need to have ability to load existing keypair file used for authentication with P2P nodes which only allow known PeerIds to connect (https://docs.rs/libp2p/latest/libp2p/allow_block_list/index.html#structs).

//CC @thomaseizinger since you might be excited to see your work being used.

Copy link
Owner

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see libp2p-lookup used more widely and contributions flowing back. Thank you!

Overall I am in favor of this patch. I just have a couple of suggestions.

Comment on lines +139 to +145
let local_key = if let Some(path) = keypair_path {
let keys_serialized_58 = std::fs::read_to_string(path).expect("Invalid keypair file path").trim().to_string();
Keypair::from_protobuf_encoding(&keys_serialized_58.from_base58().expect("Keypair file is not base58 encoded"))
.expect("Invalid keypair file")
} else {
Keypair::generate_ed25519()
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a preference on the file format? Asked differently, why did you choose base58?

If you don't have a strong preference, what do you think of following IPFS' format, which is followed by many other projects.

You can simply copy the code from here:

https://github.com/libp2p/rust-libp2p/blob/master/misc/keygen/src/config.rs

Comment on lines +43 to +45
/// Keypair used for authentication.
#[structopt(long)]
keypair_path: Option<PathBuf>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only make this available to direct calls? Wouldn't the dht subcommand also benefit from this?

Asking ChatGPT, a simple approach would be a struct SharedOptions { keypair_path: Option<PathBuf> } that is then added to each subcommand via #[structopt(flatten)].

What do you think?

Comment on lines +2 to +5

# IntelliJ IDEA
.idea
*.iml
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this in your global .gitignore in your home directory.

https://gist.github.com/subfuzion/db7f57fff2fb6998a16c

Comment on lines +69 to +101
#### Lookup peer by [address][multiaddr] and keypair file

If your network nodes only allow certain PeerId's to connect, then you can provide your own keypair file (base58 encoded) to authenticate with the node.

```
$ libp2p-lookup direct --address /dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa --keypair-path ./path/to/keypair.base58

Lookup for peer with id PeerId("QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa") succeeded.

Protocol version: "ipfs/0.1.0"
Agent version: "go-ipfs/0.8.0/48f94e2"
Observed address: "/ip4/2.200.106.157/tcp/56136"
Listen addresses:
- "/ip4/147.75.77.187/tcp/4001"
- "/ip6/2604:1380:0:c100::1/tcp/4001"
- "/ip4/147.75.77.187/udp/4001/quic"
- "/ip6/2604:1380:0:c100::1/udp/4001/quic"
Protocols:
- "/p2p/id/delta/1.0.0"
- "/ipfs/id/1.0.0"
- "/ipfs/id/push/1.0.0"
- "/ipfs/ping/1.0.0"
- "/libp2p/circuit/relay/0.1.0"
- "/ipfs/kad/1.0.0"
- "/ipfs/lan/kad/1.0.0"
- "/libp2p/autonat/1.0.0"
- "/ipfs/bitswap/1.2.0"
- "/ipfs/bitswap/1.1.0"
- "/ipfs/bitswap/1.0.0"
- "/ipfs/bitswap"
- "/x/"
```

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs an entire new section. Instead I suggest adding a note to #### Lookup peer by [address][multiaddr] only.

@andreasbros
Copy link
Author

Great to see libp2p-lookup used more widely and contributions flowing back. Thank you!

Overall I am in favor of this patch. I just have a couple of suggestions.

Thanks @mxinden your suggestions make sense, will get them ready.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments