Skip to content

Commit

Permalink
Migrate to Cidv1 for ipns libp2p keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlodotexe committed Jan 24, 2024
1 parent 152f7be commit 856303f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/CoreApi/KeyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class KeyApi : IKeyApi
public class KeyInfo : IKey
{
/// <inheritdoc />
public MultiHash Id { get; set; }
public Cid Id { get; set; }

/// <inheritdoc />
public string Name { get; set; }
Expand All @@ -37,22 +37,22 @@ internal KeyApi(IpfsClient ipfs)

public async Task<IKey> CreateAsync(string name, string keyType, int size, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/gen", cancel, name, $"type={keyType}", $"size={size}", "ipns-base=base32");
var json = await ipfs.DoCommandAsync("key/gen", cancel, name, $"type={keyType}", $"size={size}", "ipns-base=base36");
var jobject = JObject.Parse(json);

string id = (string)jobject["Id"];
string apiName = (string)jobject["Name"];

return new KeyInfo
{
Id = Cid.Decode(id).Hash,
Id = id,
Name = apiName
};
}

public async Task<IEnumerable<IKey>> ListAsync(CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/list", cancel, null, "l=true", "ipns-base=base32");
var json = await ipfs.DoCommandAsync("key/list", cancel, null, "l=true", "ipns-base=base36");
var keys = (JArray)(JObject.Parse(json)["Keys"]);

return keys
Expand All @@ -63,15 +63,15 @@ internal KeyApi(IpfsClient ipfs)

return new KeyInfo
{
Id = Cid.Decode(id).Hash,
Id = id,
Name = name
};
});
}

public async Task<IKey> RemoveAsync(string name, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/rm", cancel, name, "ipns-base=base32");
var json = await ipfs.DoCommandAsync("key/rm", cancel, name, "ipns-base=base36");
var keys = JObject.Parse(json)["Keys"] as JArray;

return keys?
Expand All @@ -82,7 +82,7 @@ internal KeyApi(IpfsClient ipfs)

return new KeyInfo
{
Id = Cid.Decode(id).Hash,
Id = id,
Name = keyName
};
})
Expand All @@ -91,15 +91,15 @@ internal KeyApi(IpfsClient ipfs)

public async Task<IKey> RenameAsync(string oldName, string newName, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/rename", cancel, oldName, $"arg={newName}", "ipns-base=base32");
var json = await ipfs.DoCommandAsync("key/rename", cancel, oldName, $"arg={newName}", "ipns-base=base36");
var jobject = JObject.Parse(json);

string id = (string)jobject["Id"];
string currentName = (string)jobject["Now"];

return new KeyInfo
{
Id = Cid.Decode(id).Hash,
Id = id,
Name = currentName
};
}
Expand Down
8 changes: 5 additions & 3 deletions test/CoreApi/NameApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -24,18 +25,19 @@ public async Task Resolve()
}

[TestMethod]
[Ignore("takes forever")]
public async Task Publish()
{
var ipfs = TestFixture.Ipfs;
var cs = new CancellationTokenSource(TimeSpan.FromMinutes(5));
var content = await ipfs.FileSystem.AddTextAsync("hello world");
var key = await ipfs.Key.CreateAsync("name-publish-test", "rsa", 1024);
var key = await ipfs.Key.CreateAsync("name-publish-test", "rsa", 2048);

try
{
var result = await ipfs.Name.PublishAsync(content.Id, key.Name, cancel: cs.Token);
Assert.IsNotNull(result);
StringAssert.EndsWith(result.NamePath, key.Id.ToString());

StringAssert.EndsWith(result.NamePath, key.Id);
StringAssert.EndsWith(result.ContentPath, content.Id.Encode());
}
finally
Expand Down

0 comments on commit 856303f

Please sign in to comment.