Skip to content

Commit 0d89c71

Browse files
chore: use MultiHash instead of string for type safety
1 parent be057b5 commit 0d89c71

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

src/CoreApi/BlockApi.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ namespace Ipfs.Api
1818
public class BlockInfo
1919
{
2020
/// <summary>
21-
/// The unique ID of the block.
21+
/// The <see cref="MultiHash"/> ID of the block.
2222
/// </summary>
2323
/// <value>
24-
/// Typically, the string representation of a <see cref="MultiHash"/>.
24+
/// The unique ID of the block.
2525
/// </value>
26-
public string Key { get; set; }
26+
public MultiHash Key { get; set; }
2727

2828
/// <summary>
2929
/// The serialised size (in bytes) of the block.

src/CoreApi/DhtApi.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ internal DhtApi(IpfsClient ipfs)
3838
/// Information about an IPFS peer.
3939
/// </summary>
4040
/// <param name="id">
41-
/// The <see cref="string"/> ID of the IPFS peer.
41+
/// The <see cref="MultiHash"/> ID of the IPFS peer.
4242
/// </param>
4343
/// <param name="cancel">
4444
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
4545
/// </param>
46-
public Task<Peer> FindPeerAsync(string id, CancellationToken cancel = default(CancellationToken))
46+
public Task<Peer> FindPeerAsync(MultiHash id, CancellationToken cancel = default(CancellationToken))
4747
{
4848
return ipfs.IdAsync(id, cancel);
4949
}
@@ -58,16 +58,15 @@ internal DhtApi(IpfsClient ipfs)
5858
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
5959
/// </param>
6060
/// <returns>
61-
/// A sequence of IPFS peer IDs.
61+
/// A sequence of IPFS <see cref="Peer"/>.
6262
/// </returns>
63-
public async Task<IEnumerable<string>> FindProvidersAsync(string hash, CancellationToken cancel = default(CancellationToken))
63+
public async Task<IEnumerable<Peer>> FindProvidersAsync(string hash, CancellationToken cancel = default(CancellationToken))
6464
{
65-
var serializer = new JsonSerializer();
6665
var stream = await ipfs.PostDownloadAsync("dht/findprovs", cancel, hash);
6766
return ProviderFromStream(stream);
6867
}
6968

70-
IEnumerable<string> ProviderFromStream(Stream stream)
69+
IEnumerable<Peer> ProviderFromStream(Stream stream)
7170
{
7271
using (var sr = new StreamReader(stream))
7372
{
@@ -80,7 +79,7 @@ IEnumerable<string> ProviderFromStream(Stream stream)
8079
var r = JObject.Parse(json);
8180
var id = (string)r["ID"];
8281
if (id != String.Empty)
83-
yield return id;
82+
yield return new Peer { Id = new MultiHash(id) };
8483
else
8584
{
8685
var responses = (JArray)r["Responses"];
@@ -89,7 +88,8 @@ IEnumerable<string> ProviderFromStream(Stream stream)
8988
foreach (var response in responses)
9089
{
9190
var rid = (string)response["ID"];
92-
yield return rid;
91+
if (rid != String.Empty)
92+
yield return new Peer { Id = new MultiHash(rid) };
9393
}
9494
}
9595
}

src/CoreApi/KeyApi.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public class KeyApi
3434
public class KeyInfo
3535
{
3636
/// <summary>
37-
/// Unique identifier (multihash)
37+
/// Unique identifier.
3838
/// </summary>
3939
/// <value>
40-
/// This is the <see cref="MultiHash"/> of the key's public key.
40+
/// The <see cref="MultiHash"/> of the key's public key.
4141
/// </value>
42-
public string Id { get; set; }
42+
public MultiHash Id { get; set; }
4343

4444
/// <summary>
4545
/// The locally assigned name to the key.
@@ -50,6 +50,12 @@ public class KeyInfo
5050
/// </value>
5151
public string Name { get; set; }
5252

53+
/// <inheritdoc />
54+
public override string ToString()
55+
{
56+
return Name;
57+
}
58+
5359
}
5460
IpfsClient ipfs;
5561

@@ -78,8 +84,10 @@ internal KeyApi(IpfsClient ipfs)
7884
/// </returns>
7985
public async Task<KeyInfo> CreateAsync(string name, string keyType, int size, CancellationToken cancel = default(CancellationToken))
8086
{
81-
return await ipfs.DoCommandAsync<KeyInfo>("key/gen", cancel, name,
82-
$"type={keyType}", $"size={size}");
87+
return await ipfs.DoCommandAsync<KeyInfo>("key/gen", cancel,
88+
name,
89+
$"type={keyType}",
90+
$"size={size}");
8391
}
8492

8593
/// <summary>

test/CoreApi/BlockApiTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void Put_Block()
3434
[TestMethod]
3535
public void Get()
3636
{
37+
var _ = ipfs.Block.PutAsync(blob).Result;
3738
var block = ipfs.Block.GetAsync(hash).Result;
3839
Assert.AreEqual(hash, block.Hash);
3940
CollectionAssert.AreEqual(blob, block.DataBytes);
@@ -42,6 +43,7 @@ public void Get()
4243
[TestMethod]
4344
public void Stat()
4445
{
46+
var _ = ipfs.Block.PutAsync(blob).Result;
4547
var info = ipfs.Block.StatAsync(hash).Result;
4648
Assert.AreEqual(hash, info.Key);
4749
Assert.AreEqual(5, info.Size);
@@ -50,6 +52,7 @@ public void Stat()
5052
[TestMethod]
5153
public async Task Remove()
5254
{
55+
var _ = ipfs.Block.PutAsync(blob).Result;
5356
var removed = await ipfs.Block.RemoveAsync(hash);
5457
Assert.AreEqual(hash, removed);
5558
}

test/CoreApi/DhtApiTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task FindProviders()
3030
{
3131
var ipfs = TestFixture.Ipfs;
3232
var providers = await ipfs.Dht.FindProvidersAsync(helloWorldID);
33-
Assert.IsFalse(providers.Take(3).Contains(""));
33+
Assert.AreNotEqual(0, providers.Count());
3434
}
3535

3636
}

0 commit comments

Comments
 (0)