Skip to content

Commit 8b480d2

Browse files
committed
Implement remaining ipfs.key methods, including tests.
1 parent 9de63d6 commit 8b480d2

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,26 @@ public List<MultiAddress> update(Multihash existing, Multihash modified, boolean
185185
/* 'ipfs repo' is a plumbing command used to manipulate the repo.
186186
*/
187187
public class Key {
188-
public Object gen(String name, Optional<String> type, Optional<String> size) throws IOException {
189-
return retrieveAndParse("key/gen?arg=" + name + type.map(t -> "&type=" + t).orElse("") + size.map(s -> "&size=" + s).orElse(""));
188+
public KeyInfo gen(String name, Optional<String> type, Optional<String> size) throws IOException {
189+
return KeyInfo.fromJson(retrieveAndParse("key/gen?arg=" + name + type.map(t -> "&type=" + t).orElse("") + size.map(s -> "&size=" + s).orElse("")));
190+
}
191+
192+
public List<KeyInfo> list() throws IOException {
193+
return ((List<Object>)((Map)retrieveAndParse("key/list")).get("Keys"))
194+
.stream()
195+
.map(KeyInfo::fromJson)
196+
.collect(Collectors.toList());
197+
}
198+
199+
public Object rename(String name, String newName) throws IOException {
200+
return retrieveAndParse("key/rename?arg="+name + "&arg=" + newName);
201+
}
202+
203+
public List<KeyInfo> rm(String name) throws IOException {
204+
return ((List<Object>)((Map)retrieveAndParse("key/rm?arg=" + name)).get("Keys"))
205+
.stream()
206+
.map(KeyInfo::fromJson)
207+
.collect(Collectors.toList());
190208
}
191209
}
192210

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.ipfs.api;
2+
3+
import io.ipfs.cid.*;
4+
import io.ipfs.multihash.*;
5+
6+
import java.util.*;
7+
8+
public class KeyInfo {
9+
10+
public final String name;
11+
public final Multihash id;
12+
13+
public KeyInfo(String name, Multihash id) {
14+
this.name = name;
15+
this.id = id;
16+
}
17+
18+
public String toString() {
19+
return name + ": " + id;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
27+
KeyInfo keyInfo = (KeyInfo) o;
28+
29+
if (name != null ? !name.equals(keyInfo.name) : keyInfo.name != null) return false;
30+
return id != null ? id.equals(keyInfo.id) : keyInfo.id == null;
31+
}
32+
33+
@Override
34+
public int hashCode() {
35+
int result = name != null ? name.hashCode() : 0;
36+
result = 31 * result + (id != null ? id.hashCode() : 0);
37+
return result;
38+
}
39+
40+
public static KeyInfo fromJson(Object json) {
41+
Map<String, String> m = (Map) json;
42+
return new KeyInfo(m.get("Name"), Cid.decode(m.get("Id")));
43+
}
44+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ public void dagCbor() throws IOException {
5050
Assert.assertTrue("Correct cid returned", cid.equals(expected));
5151
}
5252

53-
@org.junit.Test
53+
@Test
54+
public void keys() throws IOException {
55+
List<KeyInfo> existing = ipfs.key.list();
56+
String name = "mykey" + System.nanoTime();
57+
KeyInfo gen = ipfs.key.gen(name, Optional.of("rsa"), Optional.of("2048"));
58+
String newName = "bob" + System.nanoTime();
59+
Object rename = ipfs.key.rename(name, newName);
60+
List<KeyInfo> rm = ipfs.key.rm(newName);
61+
List<KeyInfo> remaining = ipfs.key.list();
62+
Assert.assertTrue("removed key", remaining.equals(existing));
63+
}
64+
65+
@Test
5466
public void ipldNode() {
5567
Function<Stream<Pair<String, CborObject>>, CborObject.CborMap> map =
5668
s -> CborObject.CborMap.build(s.collect(Collectors.toMap(p -> p.left, p -> p.right)));

0 commit comments

Comments
 (0)