Skip to content

Commit 0370250

Browse files
authored
Merge pull request #39 from vorburger/add-isvalid-method
Add isValid method to Multibase class
2 parents 6cb4cc0 + 4a5330f commit 0370250

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Simply clone this repo.
1717
byte[] data = ...
1818
String encoded = Multibase.encode(Multibase.Base.Base58BTC, data);
1919
byte[] decoded = Multibase.decode(encoded);
20+
boolean isValid = Multibase.isValid(encoded);
2021
```
2122

2223
## Dependency

src/main/java/io/ipfs/multibase/Multibase.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.ipfs.multibase;
22

33
import java.util.Map;
4+
import java.util.Optional;
45
import java.util.TreeMap;
56

67
import io.ipfs.multibase.binary.Base32;
@@ -45,14 +46,21 @@ public enum Base {
4546
lookup.put(b.prefix, b);
4647
}
4748

48-
public static Base lookup(String data) {
49+
private static Optional<Base> lookupOptional(String data) {
50+
if (data == null || data.isEmpty())
51+
return Optional.empty();
4952
String p = Character.toString(data.codePointAt(0));
5053
Base base = lookup.get(p);
5154
if (base != null)
52-
return base;
55+
return Optional.of(base);
5356
if (data.startsWith(Base256Emoji.prefix))
54-
return Base256Emoji;
55-
throw new IllegalArgumentException("Unknown Multibase type: " + p);
57+
return Optional.of(Base256Emoji);
58+
return Optional.empty();
59+
}
60+
61+
public static Base lookup(String data) {
62+
return lookupOptional(data)
63+
.orElseThrow(() -> new IllegalArgumentException("Unknown Multibase type: " + data));
5664
}
5765
}
5866

@@ -154,4 +162,22 @@ private static String safeSubstringFromIndexOne(String data) {
154162
int charIndex = data.offsetByCodePoints(0, 1);
155163
return data.substring(charIndex);
156164
}
165+
166+
/**
167+
* Check if the given data has a valid multibase prefix.
168+
*
169+
* <p>
170+
* Please note that "having a valid prefix" is NOT the same as "being an
171+
* entirely valid multibase string"; even if <tt>true</tt>, it's still entirely
172+
* possible for {@link #decode(String)} to throw an
173+
* <tt>IllegalArgumentException</tt>, if prefix is valid, but the following
174+
* data is not.
175+
*
176+
* @param data Multibase string to check.
177+
* @return true if the data has a valid multibase prefix, false otherwise; but
178+
* see above.
179+
*/
180+
public static boolean hasValidPrefix(String data) {
181+
return Base.lookupOptional(data).isPresent();
182+
}
157183
}

src/test/java/io/ipfs/multibase/MultibaseBadInputsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.ipfs.multibase;
22

3+
import static org.junit.jupiter.api.Assertions.assertFalse;
34
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.Arrays;
@@ -29,4 +30,17 @@ public void badInputTest(String input) {
2930
});
3031
}
3132

33+
public static Collection<String> invalidPrefix() {
34+
return Arrays.asList(
35+
"2", // '2' is not a valid encoding marker
36+
"", // Empty string is not a valid multibase
37+
"🫕🚀" // This not a valid Emoji Multibase (note how it's inverted)
38+
);
39+
}
40+
41+
@MethodSource("invalidPrefix")
42+
@ParameterizedTest(name = "{index}: \"{0}\"")
43+
public void invalidPrefix(String input) {
44+
assertFalse(Multibase.hasValidPrefix(input));
45+
}
3246
}

src/test/java/io/ipfs/multibase/MultibaseTest.java

100755100644
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import static org.junit.jupiter.api.Assertions.assertEquals;
1010
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
1112

1213
public class MultibaseTest {
1314

@@ -67,6 +68,12 @@ public void testDecode(Multibase.Base base, byte[] raw, String encoded) {
6768
assertArrayEquals(raw, output, String.format("Expected %s, but got %s", bytesToHex(raw), bytesToHex(output)));
6869
}
6970

71+
@MethodSource("data")
72+
@ParameterizedTest(name = "{index}: {0}, {2}")
73+
public void testHasValidPrefix(Multibase.Base base, byte[] raw, String encoded) {
74+
assertTrue(Multibase.hasValidPrefix(encoded));
75+
}
76+
7077
//Copied from https://stackoverflow.com/a/140861
7178
private static byte[] hexToBytes(String s) {
7279
int len = s.length();

0 commit comments

Comments
 (0)