Skip to content
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

MultiHash support for libp2p-key, PeerId as base32 Cidv1 #26

Closed
Arlodotexe opened this issue Nov 20, 2023 · 1 comment · Fixed by #28
Closed

MultiHash support for libp2p-key, PeerId as base32 Cidv1 #26

Arlodotexe opened this issue Nov 20, 2023 · 1 comment · Fixed by #28
Assignees

Comments

@Arlodotexe
Copy link
Collaborator

Arlodotexe commented Nov 20, 2023

By default, Kubo will return "Self" as an IPNS key, using the identity of your own node. The problem is that turning a string into a MultiHash always tries to convert to Base58, which identity hashes are not.

To repro:

new MultiHash("k51qzi5uqu5dj38iif6tbbg7embsfa3l5yk4ld8e2c9n3obgk41n7ot7nepp6l");

Output:

...threw an exception of type 'System.InvalidOperationException'. "invalid character: l"
at SimpleBase.Base58Alphabet.get_Item(Char c)
at SimpleBase.Base58.Decode(String text)
at Ipfs.Base58.Decode(String s) in .\net-ipfs-core\src\Base58.cs:line 69
at Ipfs.Base58.FromBase58(String s) in .\net-ipfs-core\src\Base58.cs:line 84
at Ipfs.MultiHash..ctor(String s) in .\net-ipfs-core\src\MultiHash.cs:line 223

This is especially problematic because the default node will throw if you try to retrieve the ipns keys.

@Arlodotexe
Copy link
Collaborator Author

Arlodotexe commented Jan 13, 2024

I've re-identified this issue in Ipfs.Core where the peer id (libp2p-key) for Ipns addresses don't work for Cidv1. This was added to libp2p in 2019 in (see RFC 0001), after Richard (the original developer) stepped away from the project, so it hasn't been added.

When we add support for the libp2p-key MultiCodec (0x72), we'll be able to differentiate between these implicitly using Cidv1.

As a workaround, if you'd still like to store the IKey.Id multihash representing an IPNS key as a normal Cid, you can inline it with:

var ipnsPeerCid = new Cid { Hash = key.Id };

When resolving, you'd need to use the .Hash instead of the Cid itself:

var ipnsResResult = await client.Name.ResolveAsync($"/ipns/{ipnsPeerCid.Hash}", recursive: true, cancel: cancellationToken);

For now you'll need to manually differentiate between immutable Cids and mutable Ipns Cids, but generally it's future-safe to use Cid to represent either one in your data models.
Additional information: https://discuss.ipfs.tech/t/solved-how-do-i-get-the-cidv1-peer-id-of-a-key-pair/11707/2

@Arlodotexe Arlodotexe self-assigned this Jan 19, 2024
@Arlodotexe Arlodotexe changed the title MultiHash doesn't accept identity string MultiHash support for libp2p-key, PeerId as base32 Cidv1 Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant