Skip to content

Commit 55ff422

Browse files
committed
Update dag command to actually use dag over block
Make MerkleNode accept JS formatted Cids
1 parent 3186518 commit 55ff422

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public enum PinType {all, direct, indirect, recursive}
4040
public final Stats stats = new Stats();
4141
public final Name name = new Name();
4242
public final Pubsub pubsub = new Pubsub();
43-
43+
4444
public IPFS(String host, int port) {
4545
this(host, port, "/api/v0/", false);
4646
}
@@ -56,13 +56,13 @@ public IPFS(MultiAddress addr) {
5656
public IPFS(String host, int port, String version, boolean ssl) {
5757
this.host = host;
5858
this.port = port;
59-
59+
6060
if(ssl) {
6161
this.protocol = "https";
6262
} else {
6363
this.protocol = "http";
6464
}
65-
65+
6666
this.version = version;
6767
// Check IPFS is sufficiently recent
6868
try {
@@ -73,7 +73,7 @@ public IPFS(String host, int port, String version, boolean ssl) {
7373
throw new RuntimeException(e);
7474
}
7575
}
76-
76+
7777
public List<MerkleNode> add(NamedStreamable file) throws IOException {
7878
return add(file, false);
7979
}
@@ -493,7 +493,7 @@ public Map disconnect(String multiAddr) throws IOException {
493493

494494
public class Dag {
495495
public byte[] get(Cid cid) throws IOException {
496-
return retrieve("block/get?stream-channels=true&arg=" + cid);
496+
return retrieve("dag/get?stream-channels=true&arg=" + cid);
497497
}
498498

499499
public MerkleNode put(byte[] object) throws IOException {
@@ -509,9 +509,8 @@ public MerkleNode put(byte[] object, String outputFormat) throws IOException {
509509
}
510510

511511
public MerkleNode put(String inputFormat, byte[] object, String outputFormat) throws IOException {
512-
block.put(Arrays.asList(object));
513-
String prefix = protocol +"://" + host + ":" + port + version;
514-
Multipart m = new Multipart(prefix + "block/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat, "UTF-8");
512+
String prefix = protocol + "://" + host + ":" + port + version;
513+
Multipart m = new Multipart(prefix + "dag/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat, "UTF-8");
515514
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(object));
516515
String res = m.finish();
517516
return MerkleNode.fromJSON(JSONParser.parse(res));
@@ -724,7 +723,7 @@ private static final byte[] readFully(InputStream in) throws IOException {
724723
resp.write(buf, 0, r);
725724
return resp.toByteArray();
726725
}
727-
726+
728727
private static boolean detectSSL(MultiAddress multiaddress) {
729728
return multiaddress.toString().contains("/https");
730729
}

src/main/java/io/ipfs/api/MerkleNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public static MerkleNode fromJSON(Object rawjson) {
6060
String hash = (String)json.get("Hash");
6161
if (hash == null)
6262
hash = (String)json.get("Key");
63+
if (hash == null)
64+
hash = (String)(((Map)json.get("Cid")).get("/"));
6365
Optional<String> name = json.containsKey("Name") ?
6466
Optional.of((String) json.get("Name")) :
6567
Optional.empty();

src/test/java/io/ipfs/api/APITest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,27 @@ public void bulkBlockTest() throws IOException {
381381
System.out.println();
382382
}
383383

384+
@Test
385+
public void publish() throws Exception {
386+
// JSON document
387+
String json = "{\"name\":\"blogpost\",\"documents\":[]}";
388+
389+
// Add a DAG node to IPFS
390+
MerkleNode merkleNode = ipfs.dag.put("json", json.getBytes());
391+
Assert.assertEquals("expected to be zdpuAknRh1Kro2r2xBDKiXyTiwA3Nu5XcmvjRPA1VNjH41NF7" , "zdpuAknRh1Kro2r2xBDKiXyTiwA3Nu5XcmvjRPA1VNjH41NF7", merkleNode.hash.toString());
392+
393+
// Get a DAG node
394+
byte[] res = ipfs.dag.get((Cid) merkleNode.hash);
395+
Assert.assertEquals("Should be equals", JSONParser.parse(json), JSONParser.parse(new String(res)));
396+
397+
// Publish to IPNS
398+
Map result = ipfs.name.publish(merkleNode.hash);
399+
400+
// Resolve from IPNS
401+
String resolved = ipfs.name.resolve(Multihash.fromBase58((String) result.get("Name")));
402+
Assert.assertEquals("Should be equals", resolved, "/ipfs/" + merkleNode.hash.toString());
403+
}
404+
384405
@Test
385406
public void pubsubSynchronous() throws Exception {
386407
String topic = "topic" + System.nanoTime();

0 commit comments

Comments
 (0)