Skip to content

Commit 6fe0bbe

Browse files
refactor: Use Cid instead of MultiHash
1 parent 546b2d9 commit 6fe0bbe

19 files changed

+337
-398
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 MultiHash Hash { get; set; }
16+
public Cid Id { get; set; }
1717

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

src/CoreApi/BlockApi.cs

Lines changed: 26 additions & 20 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 <see cref="MultiHash"/> ID of the block.
21+
/// The <see cref="Cid"/> of the block.
2222
/// </summary>
2323
/// <value>
2424
/// The unique ID of the block.
2525
/// </value>
26-
public MultiHash Key { get; set; }
26+
public Cid Id { get; set; }
2727

2828
/// <summary>
2929
/// The serialised size (in bytes) of the block.
@@ -65,16 +65,16 @@ internal BlockApi(IpfsClient ipfs)
6565
/// <param name="cancel">
6666
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
6767
/// </param>
68-
/// <param name="hash">
69-
/// The <see cref="MultiHash"/> of the block.
68+
/// <param name="id">
69+
/// The <see cref="Cid"/> of the block.
7070
/// </param>
71-
public async Task<Block> GetAsync(MultiHash hash, CancellationToken cancel = default(CancellationToken)) // TODO CID support
71+
public async Task<Block> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken)) // TODO CID support
7272
{
73-
var data = await ipfs.DownloadBytesAsync("block/get", cancel, hash.ToString());
73+
var data = await ipfs.DownloadBytesAsync("block/get", cancel, id);
7474
return new Block
7575
{
7676
DataBytes = data,
77-
Hash = hash
77+
Id = id
7878
};
7979
}
8080

@@ -89,12 +89,12 @@ internal BlockApi(IpfsClient ipfs)
8989
/// </param>
9090
public async Task<Block> PutAsync(byte[] data, CancellationToken cancel = default(CancellationToken))
9191
{
92-
var json = await ipfs.UploadAsync("block/put", cancel, data);
93-
var info = JsonConvert.DeserializeObject<BlockInfo>(json);
92+
var json = await ipfs.UploadAsync("block/put", cancel, data);
93+
var info = JObject.Parse(json);
9494
return new Block
9595
{
9696
DataBytes = data,
97-
Hash = info.Key
97+
Id = (string)info["Key"]
9898
};
9999
}
100100

@@ -115,15 +115,21 @@ internal BlockApi(IpfsClient ipfs)
115115
/// <summary>
116116
/// Information on a raw <see cref="Block">IPFS block</see>.
117117
/// </summary>
118-
/// <param name="hash">
119-
/// The <see cref="MultiHash"/> id of the block.
118+
/// <param name="id">
119+
/// The <see cref="Cid"/> 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(MultiHash hash, CancellationToken cancel = default(CancellationToken))
124+
public async Task<BlockInfo> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
125125
{
126-
return ipfs.DoCommandAsync<BlockInfo>("block/stat", cancel, hash.ToBase58());
126+
var json = await ipfs.DoCommandAsync("block/stat", cancel, id);
127+
var info = JObject.Parse(json);
128+
return new BlockInfo
129+
{
130+
Size = (long)info["Size"],
131+
Id = (string)info["Key"]
132+
};
127133
}
128134

129135
/// <summary>
@@ -132,21 +138,21 @@ internal BlockApi(IpfsClient ipfs)
132138
/// <param name="cancel">
133139
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
134140
/// </param>
135-
/// <param name="hash">
136-
/// The <see cref="MultiHash"/> id of the block.
141+
/// <param name="id">
142+
/// The <see cref="Cid"/> of the block.
137143
/// </param>
138144
/// <param name="ignoreNonexistent">
139-
/// If <b>true</b> do not raise exception when <paramref name="hash"/> does not
145+
/// If <b>true</b> do not raise exception when <paramref name="id"/> does not
140146
/// exist. Default value is <b>false</b>.
141147
/// </param>
142148
/// <returns>
143-
/// The awaited Task will return the deleted <paramref name="hash"/> or
149+
/// The awaited Task will return the deleted <paramref name="id"/> or
144150
/// <see cref="string.Empty"/> if the hash does not exist and <paramref name="ignoreNonexistent"/>
145151
/// is <b>true</b>.
146152
/// </returns>
147-
public async Task<string> RemoveAsync(MultiHash hash, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
153+
public async Task<string> RemoveAsync(Cid id, bool ignoreNonexistent = false, CancellationToken cancel = default(CancellationToken)) // TODO CID support
148154
{
149-
var json = await ipfs.DoCommandAsync("block/rm", cancel, hash.ToBase58(), "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
155+
var json = await ipfs.DoCommandAsync("block/rm", cancel, id, "force=" + ignoreNonexistent.ToString().ToLowerInvariant());
150156
if (json.Length == 0)
151157
return "";
152158
var result = JObject.Parse(json);

src/CoreApi/DhtApi.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ internal DhtApi(IpfsClient ipfs)
5151
/// <summary>
5252
/// Find the providers for content that is addressed by a hash.
5353
/// </summary>
54-
/// <param name="hash">
55-
/// The <see cref="string"/> representation of a base58 encoded <see cref="Ipfs.MultiHash"/>.
54+
/// <param name="id">
55+
/// The <see cref="Cid"/> of the content.
5656
/// </param>
5757
/// <param name="cancel">
5858
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
5959
/// </param>
6060
/// <returns>
6161
/// A sequence of IPFS <see cref="Peer"/>.
6262
/// </returns>
63-
public async Task<IEnumerable<Peer>> FindProvidersAsync(string hash, CancellationToken cancel = default(CancellationToken))
63+
public async Task<IEnumerable<Peer>> FindProvidersAsync(Cid id, CancellationToken cancel = default(CancellationToken))
6464
{
65-
var stream = await ipfs.PostDownloadAsync("dht/findprovs", cancel, hash);
65+
var stream = await ipfs.PostDownloadAsync("dht/findprovs", cancel, id);
6666
return ProviderFromStream(stream);
6767
}
6868

src/CoreApi/FileSystemApi.cs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ internal FileSystemApi(IpfsClient ipfs)
7676
var r = JObject.Parse(json);
7777
var fsn = new FileSystemNode
7878
{
79-
Hash = (string)r["Hash"],
79+
Id = (string)r["Hash"],
8080
Size = long.Parse((string)r["Size"]),
8181
IsDirectory = false,
8282
Name = name,
8383
IpfsClient = ipfs
8484
};
8585
if (log.IsDebugEnabled)
86-
log.Debug("added " + fsn.Hash + " " + fsn.Name);
86+
log.Debug("added " + fsn.Id + " " + fsn.Name);
8787
return fsn;
8888
}
8989

@@ -121,10 +121,10 @@ internal FileSystemApi(IpfsClient ipfs)
121121
var directory = await ipfs.Object.PutAsync(folder, cancel);
122122

123123
if (log.IsDebugEnabled)
124-
log.Debug("added " + directory.Hash + " " + Path.GetFileName(path));
124+
log.Debug("added " + directory.Id + " " + Path.GetFileName(path));
125125
return new FileSystemNode
126126
{
127-
Hash = directory.Hash,
127+
Id = directory.Id,
128128
Name = Path.GetFileName(path),
129129
Links = links,
130130
IsDirectory = true,
@@ -156,22 +156,6 @@ internal FileSystemApi(IpfsClient ipfs)
156156
}
157157
}
158158

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-
}
175159

176160
/// <summary>
177161
/// Opens an existing IPFS file for reading.
@@ -191,22 +175,6 @@ internal FileSystemApi(IpfsClient ipfs)
191175
return ipfs.DownloadAsync("cat", cancel, path);
192176
}
193177

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-
}
210178

211179
/// <summary>
212180
/// Get information about the file or directory.
@@ -227,7 +195,7 @@ internal FileSystemApi(IpfsClient ipfs)
227195
var o = (JObject)r["Objects"][hash];
228196
var node = new FileSystemNode()
229197
{
230-
Hash = (string)o["Hash"],
198+
Id = (string)o["Hash"],
231199
Size = (long)o["Size"],
232200
IsDirectory = (string)o["Type"] == "Directory",
233201
Links = new FileSystemLink[0]
@@ -239,7 +207,7 @@ internal FileSystemApi(IpfsClient ipfs)
239207
.Select(l => new FileSystemLink()
240208
{
241209
Name = (string)l["Name"],
242-
Hash = (string)l["Hash"],
210+
Id = (string)l["Hash"],
243211
Size = (long)l["Size"],
244212
IsDirectory = (string)l["Type"] == "Directory",
245213
})

src/CoreApi/ObjectApi.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ internal ObjectApi(IpfsClient ipfs)
9696
/// <summary>
9797
/// Fetch a MerkleDAG node.
9898
/// </summary>
99-
/// <param name="hash">
100-
/// The <see cref="MultiHash"/> to the node.
99+
/// <param name="id">
100+
/// The <see cref="Cid"/> 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(MultiHash hash, CancellationToken cancel = default(CancellationToken))
106+
public async Task<DagNode> GetAsync(Cid id, CancellationToken cancel = default(CancellationToken))
107107
{
108-
var json = await ipfs.DoCommandAsync("object/get", cancel, hash.ToBase58());
108+
var json = await ipfs.DoCommandAsync("object/get", cancel, id);
109109
return GetDagFromJson(json);
110110
}
111111

@@ -142,8 +142,8 @@ internal ObjectApi(IpfsClient ipfs)
142142
/// <summary>
143143
/// Get the data of a MerkleDAG node.
144144
/// </summary>
145-
/// <param name="hash">
146-
/// The <see cref="MultiHash"/> id of the node.
145+
/// <param name="id">
146+
/// The <see cref="Cid"/> 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(MultiHash hash, CancellationToken cancel = default(CancellationToken))
155+
public Task<Stream> DataAsync(Cid id, CancellationToken cancel = default(CancellationToken))
156156
{
157-
return ipfs.DownloadAsync("object/data", cancel, hash.ToBase58());
157+
return ipfs.DownloadAsync("object/data", cancel, id);
158158
}
159159

160160
/// <summary>
161161
/// Get the links of a MerkleDAG node.
162162
/// </summary>
163-
/// <param name="hash">
164-
/// The <see cref="MultiHash"/> id of the node.
163+
/// <param name="id">
164+
/// The <see cref="Cid"/> 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(MultiHash hash, CancellationToken cancel = default(CancellationToken))
170+
public async Task<IEnumerable<IMerkleLink>> LinksAsync(Cid id, CancellationToken cancel = default(CancellationToken))
171171
{
172-
var json = await ipfs.DoCommandAsync("object/links", cancel, hash.ToBase58());
172+
var json = await ipfs.DoCommandAsync("object/links", cancel, id);
173173
return GetDagFromJson(json).Links;
174174
}
175175

176176
/// <summary>
177177
/// Get the statistics of a MerkleDAG node.
178178
/// </summary>
179-
/// <param name="hash">
180-
/// The <see cref="MultiHash"/> of the node.
179+
/// <param name="id">
180+
/// The <see cref="Cid"/> 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(MultiHash hash, CancellationToken cancel = default(CancellationToken))
186+
public Task<DagInfo> StatAsync(Cid id, CancellationToken cancel = default(CancellationToken))
187187
{
188-
return ipfs.DoCommandAsync<DagInfo>("object/stat", cancel, hash.ToBase58());
188+
return ipfs.DoCommandAsync<DagInfo>("object/stat", cancel, id);
189189
}
190190

191191
// TOOD: patch sub API

src/CoreApi/PinApi.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,6 @@ internal PinApi(IpfsClient ipfs)
5252
.ToArray();
5353
}
5454

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-
7355
/// <summary>
7456
/// List all the objects pinned to local storage.
7557
/// </summary>
@@ -118,23 +100,6 @@ internal PinApi(IpfsClient ipfs)
118100
.ToArray();
119101
}
120102

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-
}
138103
}
139104

140105
}

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 MultiHash Hash { get; set; }
13+
public Cid Id { get; set; }
1414

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

0 commit comments

Comments
 (0)