Skip to content

Commit e602820

Browse files
committed
Made IPFS constructor able to take a compatible Multiaddress
1 parent 61987cc commit e602820

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/org/ipfs/IPFS.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public IPFS(String host, int port) {
3131
this(host, port, "/api/v0/");
3232
}
3333

34+
public IPFS(MultiAddress addr) {
35+
this(addr.getHost(), addr.getTCPPort(), "/api/v0/");
36+
}
37+
3438
public IPFS(String host, int port, String version) {
3539
this.host = host;
3640
this.port = port;

src/org/ipfs/MultiAddress.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ public byte[] getBytes() {
2020
return Arrays.copyOfRange(raw, 0, raw.length);
2121
}
2222

23+
public boolean isTCPIP() {
24+
String[] parts = toString().substring(1).split("/");
25+
if (parts.length != 4)
26+
return false;
27+
if (!parts[0].startsWith("ip"))
28+
return false;
29+
if (!parts[2].equals("tcp"))
30+
return false;
31+
return true;
32+
}
33+
34+
public String getHost() {
35+
String[] parts = toString().substring(1).split("/");
36+
if (parts[0].startsWith("ip"))
37+
return parts[1];
38+
throw new IllegalStateException("This multiaddress doesn't have a host: "+toString());
39+
}
40+
41+
public int getTCPPort() {
42+
String[] parts = toString().substring(1).split("/");
43+
if (parts[2].startsWith("tcp"))
44+
return Integer.parseInt(parts[3]);
45+
throw new IllegalStateException("This multiaddress doesn't have a tcp port: "+toString());
46+
}
47+
2348
private static byte[] decodeFromString(String addr) {
2449
while (addr.endsWith("/"))
2550
addr = addr.substring(0, addr.length()-1);

test/org/ipfs/Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class Test {
77

8-
IPFS ipfs = new IPFS("127.0.0.1", 5001);
8+
IPFS ipfs = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001"));
99
@org.junit.Test
1010
public void base58Test() {
1111
String input = "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB";

0 commit comments

Comments
 (0)