英文:
Can I keep the same ID in libp2p across restarts?
问题
我正在编写一个测试应用程序。为此,如果我的libp2p节点在重新启动后能保持它们的ID不变,那将是理想的情况 - 否则,我需要考虑一些中间服务,用于在测试期间跟踪节点的NodeID以进行连接。
我可以做到这一点吗?
目前,如果我只是运行https://docs.libp2p.io/guides/getting-started/go/教程,每次重新启动后,它都会打印出不同的节点ID。
❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWNK774CCm9b48H84qZu3mp2Jq4wvDBh9813omB712qqLW
^CReceived signal, shutting down...
❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWGLgDLUaNiuruX5umaExyFQJQBsf3yyXgC9MPy7cGS99F
^CReceived signal, shutting down...
英文:
I am writing a test app. For this, it would be ideal if my libp2p nodes would keep their IDs across restarts - otherwise I need to think of some intermediate service which keeps track of NodeIDs for the nodes to connect during a test.
Can I do this?
Currently, if I just run the https://docs.libp2p.io/guides/getting-started/go/ tutorial, after each restart, it prints a different Node ID.
❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWNK774CCm9b48H84qZu3mp2Jq4wvDBh9813omB712qqLW
^CReceived signal, shutting down...
❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWGLgDLUaNiuruX5umaExyFQJQBsf3yyXgC9MPy7cGS99F
^CReceived signal, shutting down...
答案1
得分: -2
这条评论似乎提供了答案:https://github.com/libp2p/go-libp2p/issues/1040#issuecomment-767695679
引用其中的内容:
一个 libp2p 节点无法连接到没有其 peerId 的远程节点。
你可以通过使用 Identity 选项,配置一个 libp2p 节点使用持久化的密钥来派生与之前相同的 peerId。
libp2p.New(context.Background(), libp2p.Identity(//在这里填入密钥))
要获取主机的密钥并将其编组,可以使用:
sk := h.Peerstore().PrivKey(h.ID())
bz, _ := crypto.MarshalPrivateKey(sk)
英文:
This comment seems to provide the answer: https://github.com/libp2p/go-libp2p/issues/1040#issuecomment-767695679
Quoting from there:
> A libp2p peer can't connect to a remote peer without it's peerId.
>
> You can configure a libp2p node to use a persisted secret key to
> derive the same peerId as before by using the Identity option.
libp2p.New(context.Background(), libp2p.Identity(//secret key here))
>
> To get a hosts's secret key and then marshal it, you can use:
>
>
sk := h.Peerstore().PrivKey(h.ID())
bz, _ := crypto.MarshalPrivateKey(sk)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论