22
33import io .ipfs .cid .*;
44import io .ipfs .multiaddr .*;
5+ import io .ipfs .multibase .Base58 ;
56import io .ipfs .multihash .*;
67
78import java .util .*;
89import java .util .function .*;
910
1011public class Peer {
1112 public final MultiAddress address ;
12- public final Multihash id ;
13+ public final Cid id ;
1314 public final long latency ;
1415 public final String muxer ;
1516 public final Object streams ;
1617
17- public Peer (MultiAddress address , Multihash id , long latency , String muxer , Object streams ) {
18+ public Peer (MultiAddress address , Cid id , long latency , String muxer , Object streams ) {
1819 this .address = address ;
1920 this .id = id ;
2021 this .latency = latency ;
@@ -27,17 +28,20 @@ public static Peer fromJSON(Object json) {
2728 throw new IllegalStateException ("Incorrect json for Peer: " + JSONParser .toString (json ));
2829 Map m = (Map ) json ;
2930 Function <String , String > val = key -> (String ) m .get (key );
30- String peerId = val .apply ("Peer" );
31- Multihash peer ; // Multihash bug? Throws IAEx Base1 not supported
32- try {
33- peer = Multihash .fromBase58 (peerId );
34- } catch (Exception e ) {
35- peer = Multihash .decode (peerId );
36- }
31+ Cid peer = decodePeerId (val .apply ("Peer" ));
3732 long latency = m .containsKey ("Latency" ) ? Long .parseLong (val .apply ("Latency" )) : -1 ;
3833 return new Peer (new MultiAddress (val .apply ("Addr" )), peer , latency , val .apply ("Muxer" ), val .apply ("Streams" ));
3934 }
4035
36+ // See https://github.com/Peergos/Peergos/blob/81064fdb2cdf6b6fe126cf6a20d4d40ecd148938/src/peergos/shared/io/ipfs/Cid.java#L148
37+ public static Cid decodePeerId (String peerId ) {
38+ if (peerId .startsWith ("1" )) {
39+ // convert base58 encoded identity multihash to cidV1
40+ Multihash hash = Multihash .deserialize (Base58 .decode (peerId ));
41+ return new Cid (1 , Cid .Codec .Libp2pKey , hash .getType (), hash .getHash ());
42+ }
43+ return Cid .decode (peerId );
44+ }
4145 @ Override
4246 public String toString () {
4347 return id + "@" + address ;
0 commit comments