File tree Expand file tree Collapse file tree 2 files changed +22
-7
lines changed
Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,16 @@ public byte[] toBytesV1() {
6262 return Arrays .copyOfRange (res , 0 , index + hashBytes .length );
6363 }
6464
65+ @ Override
66+ public String toString () {
67+ if (version == 0 ) {
68+ return hash .toString ();
69+ } else if (version == 1 ) {
70+ return Multibase .encode (Multibase .Base .Base58BTC , toBytesV1 ());
71+ }
72+ throw new IllegalStateException ("Unknown Cid version: " + version );
73+ }
74+
6575 @ Override
6676 public boolean equals (Object o ) {
6777 if (this == o ) return true ;
Original file line number Diff line number Diff line change 22
33import org .junit .*;
44import io .ipfs .multibase .*;
5+
6+ import java .io .*;
57import java .util .*;
68
79public class CidTest {
810
911 @ Test
10- public void base58Test () {
11- List <String > examples = Arrays .asList ("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB" ,
12- "QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy" );
12+ public void stringTest () throws IOException {
13+ List <String > examples = Arrays .asList (
14+ "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB" ,
15+ "QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy" ,
16+ "zdpuAyvkgEDQm9TenwGkd5eNaosSxjgEYd8QatfPetgB1CdEZ"
17+ );
1318 for (String example : examples ) {
14- byte [] output = Base58 .decode (example );
15- String encoded = Base58 . encode ( output );
16- if (!encoded .equals (encoded ))
17- throw new IllegalStateException ("Incorrect base58 ! " + example + " => " + encoded );
19+ Cid cid = Cid .decode (example );
20+ String encoded = cid . toString ( );
21+ if (!encoded .equals (example ))
22+ throw new IllegalStateException ("Incorrect cid string ! " + example + " => " + encoded );
1823 }
1924 }
2025}
You can’t perform that action at this time.
0 commit comments