Skip to content

Commit be057b5

Browse files
fix: use MultiHash instead of string for type safety
1 parent a8f1212 commit be057b5

File tree

13 files changed

+139
-48
lines changed

13 files changed

+139
-48
lines changed

src/Block.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Block : IDataBlock
1313
byte[] dataBytes;
1414

1515
/// <inheritdoc />
16-
public string Hash { get; set; }
16+
public MultiHash Hash { get; set; }
1717

1818
/// <inheritdoc />
1919
public byte[] DataBytes

src/CoreApi/BlockApi.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ internal BlockApi(IpfsClient ipfs)
6666
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
6767
/// </param>
6868
/// <param name="hash">
69-
/// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>.
69+
/// The <see cref="MultiHash"/> of the block.
7070
/// </param>
71-
public async Task<Block> GetAsync(string hash, CancellationToken cancel = default(CancellationToken)) // TODO CID support
71+
public async Task<Block> GetAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken)) // TODO CID support
7272
{
73-
var data = await ipfs.DownloadBytesAsync("block/get", cancel, hash);
73+
var data = await ipfs.DownloadBytesAsync("block/get", cancel, hash.ToString());
7474
return new Block
7575
{
7676
DataBytes = data,
@@ -116,14 +116,14 @@ internal BlockApi(IpfsClient ipfs)
116116
/// Information on a raw <see cref="Block">IPFS block</see>.
117117
/// </summary>
118118
/// <param name="hash">
119-
/// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>.
119+
/// The <see cref="MultiHash"/> id of the block.
120120
/// </param>
121121
/// <param name="cancel">
122122
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
123123
/// </param>
124-
public Task<BlockInfo> StatAsync(string hash, CancellationToken cancel = default(CancellationToken))
124+
public Task<BlockInfo> StatAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken))
125125
{
126-
return ipfs.DoCommandAsync<BlockInfo>("block/stat", cancel, hash);
126+
return ipfs.DoCommandAsync<BlockInfo>("block/stat", cancel, hash.ToBase58());
127127
}
128128

129129
/// <summary>
@@ -133,7 +133,7 @@ internal BlockApi(IpfsClient ipfs)
133133
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
134134
/// </param>
135135
/// <param name="hash">
136-
/// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>.
136+
/// The <see cref="MultiHash"/> id of the block.
137137
/// </param>
138138
/// <param name="ignoreNonexistent">
139139
/// If <b>true</b> do not raise exception when <paramref name="hash"/> does not
@@ -144,9 +144,9 @@ internal BlockApi(IpfsClient ipfs)
144144
/// <see cref="string.Empty"/> if the hash does not exist and <paramref name="ignoreNonexistent"/>
145145
/// is <b>true</b>.
146146
/// </returns>
147-
public async Task<string> RemoveAsync(string hash, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
147+
public async Task<string> RemoveAsync(MultiHash hash, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
148148
{
149-
var json = await ipfs.DoCommandAsync("block/rm", cancel, hash, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
149+
var json = await ipfs.DoCommandAsync("block/rm", cancel, hash.ToBase58(), "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
150150
if (json.Length == 0)
151151
return "";
152152
var result = JObject.Parse(json);

src/CoreApi/FileSystemApi.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ internal FileSystemApi(IpfsClient ipfs)
144144
/// <param name="cancel">
145145
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
146146
/// </param>
147-
/// <returns></returns>
147+
/// <returns>
148+
/// The contents of the <paramref name="path"/> as a <see cref="string"/>.
149+
/// </returns>
148150
public async Task<String> ReadAllTextAsync(string path, CancellationToken cancel = default(CancellationToken))
149151
{
150152
using (var data = await ReadFileAsync(path, cancel))
@@ -154,6 +156,23 @@ internal FileSystemApi(IpfsClient ipfs)
154156
}
155157
}
156158

159+
/// <summary>
160+
/// Reads the content of an existing IPFS file as text.
161+
/// </summary>
162+
/// <param name="hash">
163+
/// The <see cref="MultiHash"/> id of the file.
164+
/// </param>
165+
/// <param name="cancel">
166+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
167+
/// </param>
168+
/// <returns>
169+
/// The contents of the <paramref name="hash"/> as a <see cref="string"/>.
170+
/// </returns>
171+
public Task<String> ReadAllTextAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken))
172+
{
173+
return ReadAllTextAsync(hash.ToBase58(), cancel);
174+
}
175+
157176
/// <summary>
158177
/// Opens an existing IPFS file for reading.
159178
/// </summary>
@@ -172,6 +191,23 @@ internal FileSystemApi(IpfsClient ipfs)
172191
return ipfs.DownloadAsync("cat", cancel, path);
173192
}
174193

194+
/// <summary>
195+
/// Opens an existing IPFS file for reading.
196+
/// </summary>
197+
/// <param name="hash">
198+
/// The <see cref="MultiHash"/> id of the file.
199+
/// </param>
200+
/// <param name="cancel">
201+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
202+
/// </param>
203+
/// <returns>
204+
/// A <see cref="Stream"/> to the file contents.
205+
/// </returns>
206+
public Task<Stream> ReadFileAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken))
207+
{
208+
return ipfs.DownloadAsync("cat", cancel, hash.ToBase58());
209+
}
210+
175211
/// <summary>
176212
/// Get information about the file or directory.
177213
/// </summary>

src/CoreApi/GenericApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public partial class IpfsClient
2525
/// <returns>
2626
/// Information on the peer node.
2727
/// </returns>
28-
public Task<Peer> IdAsync(string peer = null, CancellationToken cancel = default(CancellationToken))
28+
public Task<Peer> IdAsync(MultiHash peer = null, CancellationToken cancel = default(CancellationToken))
2929
{
30-
return DoCommandAsync<Peer>("id", cancel, peer);
30+
return DoCommandAsync<Peer>("id", cancel, peer?.ToString());
3131
}
3232

3333
/// <summary>

src/CoreApi/ObjectApi.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ internal ObjectApi(IpfsClient ipfs)
9797
/// Fetch a MerkleDAG node.
9898
/// </summary>
9999
/// <param name="hash">
100-
/// The <see cref="string"/> representation of an encoded <see cref="Ipfs.MultiHash"/>.
100+
/// The <see cref="MultiHash"/> to the node.
101101
/// </param>
102102
/// <param name="cancel">
103103
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
104104
/// </param>
105105
/// <returns></returns>
106-
public async Task<DagNode> GetAsync(string hash, CancellationToken cancel = default(CancellationToken))
106+
public async Task<DagNode> GetAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken))
107107
{
108-
var json = await ipfs.DoCommandAsync("object/get", cancel, hash);
108+
var json = await ipfs.DoCommandAsync("object/get", cancel, hash.ToBase58());
109109
return GetDagFromJson(json);
110110
}
111111

@@ -143,7 +143,7 @@ internal ObjectApi(IpfsClient ipfs)
143143
/// Get the data of a MerkleDAG node.
144144
/// </summary>
145145
/// <param name="hash">
146-
/// The <see cref="string"/> representation of an encoded <see cref="Ipfs.MultiHash"/>.
146+
/// The <see cref="MultiHash"/> id of the node.
147147
/// </param>
148148
/// <param name="cancel">
149149
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
@@ -152,40 +152,40 @@ internal ObjectApi(IpfsClient ipfs)
152152
/// <remarks>
153153
/// The caller must dispose the returned <see cref="Stream"/>.
154154
/// </remarks>
155-
public Task<Stream> DataAsync(string hash, CancellationToken cancel = default(CancellationToken))
155+
public Task<Stream> DataAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken))
156156
{
157-
return ipfs.DownloadAsync("object/data", cancel, hash);
157+
return ipfs.DownloadAsync("object/data", cancel, hash.ToBase58());
158158
}
159159

160160
/// <summary>
161161
/// Get the links of a MerkleDAG node.
162162
/// </summary>
163163
/// <param name="hash">
164-
/// The <see cref="string"/> representation of an encoded <see cref="Ipfs.MultiHash"/>.
164+
/// The <see cref="MultiHash"/> id of the node.
165165
/// </param>
166166
/// <param name="cancel">
167167
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
168168
/// </param>
169169
/// <returns>A sequence of links</returns>
170-
public async Task<IEnumerable<IMerkleLink>> LinksAsync(string hash, CancellationToken cancel = default(CancellationToken))
170+
public async Task<IEnumerable<IMerkleLink>> LinksAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken))
171171
{
172-
var json = await ipfs.DoCommandAsync("object/links", cancel, hash);
172+
var json = await ipfs.DoCommandAsync("object/links", cancel, hash.ToBase58());
173173
return GetDagFromJson(json).Links;
174174
}
175175

176176
/// <summary>
177177
/// Get the statistics of a MerkleDAG node.
178178
/// </summary>
179179
/// <param name="hash">
180-
/// The <see cref="string"/> representation of an encoded <see cref="Ipfs.MultiHash"/>.
180+
/// The <see cref="MultiHash"/> of the node.
181181
/// </param>
182182
/// <param name="cancel">
183183
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
184184
/// </param>
185185
/// <returns></returns>
186-
public Task<DagInfo> StatAsync(string hash, CancellationToken cancel = default(CancellationToken))
186+
public Task<DagInfo> StatAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken))
187187
{
188-
return ipfs.DoCommandAsync<DagInfo>("object/stat", cancel, hash);
188+
return ipfs.DoCommandAsync<DagInfo>("object/stat", cancel, hash.ToBase58());
189189
}
190190

191191
// TOOD: patch sub API

src/CoreApi/PinApi.cs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ internal PinApi(IpfsClient ipfs)
3232
/// <summary>
3333
/// Adds an IPFS object to the pinset and also stores it to the IPFS repo. pinset is the set of hashes currently pinned (not gc'able).
3434
/// </summary>
35-
/// <param name="hash">
36-
/// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>.
35+
/// <param name="path">
36+
/// A path to an existing object, such as "QmXarR6rgkQ2fDSHjSY5nM2kuCXKYGViky5nohtwgF65Ec/about"
37+
/// or "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
3738
/// </param>
3839
/// <param name="recursive">
3940
/// <b>true</b> to recursively pin links of object; otherwise, <b>false</b> to only pin
@@ -42,15 +43,33 @@ internal PinApi(IpfsClient ipfs)
4243
/// <param name="cancel">
4344
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
4445
/// </param>
45-
public async Task<PinnedObject[]> AddAsync(string hash, bool recursive = true, CancellationToken cancel = default(CancellationToken))
46+
public async Task<PinnedObject[]> AddAsync(string path, bool recursive = true, CancellationToken cancel = default(CancellationToken))
4647
{
4748
var opts = "recursive=" + recursive.ToString().ToLowerInvariant();
48-
var json = await ipfs.DoCommandAsync("pin/add", cancel, hash, opts);
49+
var json = await ipfs.DoCommandAsync("pin/add", cancel, path, opts);
4950
return ((JArray)JObject.Parse(json)["Pins"])
5051
.Select(p => new PinnedObject { Id = (string)p })
5152
.ToArray();
5253
}
5354

55+
/// <summary>
56+
/// Adds an IPFS object to the pinset and also stores it to the IPFS repo. pinset is the set of hashes currently pinned (not gc'able).
57+
/// </summary>
58+
/// <param name="hash">
59+
/// A <see cref="MultiHash"/> id to an existing object.
60+
/// </param>
61+
/// <param name="recursive">
62+
/// <b>true</b> to recursively pin links of object; otherwise, <b>false</b> to only pin
63+
/// the specified object. Default is <b>true</b>.
64+
/// </param>
65+
/// <param name="cancel">
66+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
67+
/// </param>
68+
public Task<PinnedObject[]> AddAsync(MultiHash hash, bool recursive = true, CancellationToken cancel = default(CancellationToken))
69+
{
70+
return AddAsync(hash.ToBase58(), recursive, cancel);
71+
}
72+
5473
/// <summary>
5574
/// List all the objects pinned to local storage.
5675
/// </summary>
@@ -79,8 +98,9 @@ internal PinApi(IpfsClient ipfs)
7998
/// <summary>
8099
/// Unpin an object.
81100
/// </summary>
82-
/// <param name="hash">
83-
/// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>.
101+
/// <param name="path">
102+
/// A path to an existing object, such as "QmXarR6rgkQ2fDSHjSY5nM2kuCXKYGViky5nohtwgF65Ec/about"
103+
/// or "QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V"
84104
/// </param>
85105
/// <param name="recursive">
86106
/// <b>true</b> to recursively unpin links of object; otherwise, <b>false</b> to only unpin
@@ -89,14 +109,32 @@ internal PinApi(IpfsClient ipfs)
89109
/// <param name="cancel">
90110
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
91111
/// </param>
92-
public async Task<PinnedObject[]> RemoveAsync(string hash, bool recursive = true, CancellationToken cancel = default(CancellationToken))
112+
public async Task<PinnedObject[]> RemoveAsync(string path, bool recursive = true, CancellationToken cancel = default(CancellationToken))
93113
{
94114
var opts = "recursive=" + recursive.ToString().ToLowerInvariant();
95-
var json = await ipfs.DoCommandAsync("pin/rm", cancel, hash, opts);
115+
var json = await ipfs.DoCommandAsync("pin/rm", cancel, path, opts);
96116
return ((JArray)JObject.Parse(json)["Pins"])
97117
.Select(p => new PinnedObject { Id = (string)p })
98118
.ToArray();
99119
}
120+
121+
/// <summary>
122+
/// Unpin an object.
123+
/// </summary>
124+
/// <param name="hash">
125+
/// A <see cref="MultiHash"/> id to an existing object.
126+
/// </param>
127+
/// <param name="recursive">
128+
/// <b>true</b> to recursively unpin links of object; otherwise, <b>false</b> to only unpin
129+
/// the specified object. Default is <b>true</b>.
130+
/// </param>
131+
/// <param name="cancel">
132+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
133+
/// </param>
134+
public Task<PinnedObject[]> RemoveAsync(MultiHash hash, bool recursive = true, CancellationToken cancel = default(CancellationToken))
135+
{
136+
return RemoveAsync(hash.ToBase58(), recursive, cancel);
137+
}
100138
}
101139

102140
}

src/FileSystemLink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class FileSystemLink : IMerkleLink
1010
public string Name { get; set; }
1111

1212
/// <inheritdoc />
13-
public string Hash { get; set; }
13+
public MultiHash Hash { get; set; }
1414

1515
/// <inheritdoc />
1616
public long Size { get; set; }

src/FileSystemNode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public Stream DataStream
3434
{
3535
get
3636
{
37-
return IpfsClient.FileSystem.ReadFileAsync(Hash).Result;
37+
return IpfsClient.FileSystem.ReadFileAsync(Hash.ToBase58()).Result;
3838
}
3939
}
4040
/// <inheritdoc />
41-
public string Hash { get; set; }
41+
public MultiHash Hash { get; set; }
4242

4343
/// <inheritdoc />
4444
public IEnumerable<FileSystemLink> Links {
@@ -103,7 +103,7 @@ public FileSystemLink ToLink(string name = null)
103103
{
104104
return new FileSystemLink
105105
{
106-
Name = name != null ? name : Name,
106+
Name = name ?? Name,
107107
Hash = Hash,
108108
Size = Size,
109109
IsDirectory = IsDirectory
@@ -131,7 +131,7 @@ internal IpfsClient IpfsClient
131131

132132
void GetInfo()
133133
{
134-
var node = IpfsClient.FileSystem.ListFileAsync(Hash).Result;
134+
var node = IpfsClient.FileSystem.ListFileAsync(Hash.ToBase58()).Result;
135135
this.IsDirectory = node.IsDirectory;
136136
this.Links = node.Links;
137137
this.Size = node.Size;

src/IpfsApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</PropertyGroup>
2929

3030
<ItemGroup>
31-
<PackageReference Include="Ipfs.Core" Version="0.13.0" />
31+
<PackageReference Include="Ipfs.Core" Version="0.14.0" />
3232
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
3333
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'netstandard14'" />
3434
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'net45'" />

0 commit comments

Comments
 (0)