diff --git a/src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java b/src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java index 46762f91..43e0cc08 100644 --- a/src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java +++ b/src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java @@ -112,22 +112,24 @@ public static List parseElements(String arrayJson, Set< } public static List parseElements(int flags, String arrayJson, Set types) { -// return ABIJSON.stream(flags, arrayJson, types).collect(Collectors.toList()); + try (final JsonReader reader = read(arrayJson)) { + return parseArray(reader, types, flags); + } catch (IOException io) { + throw new IllegalStateException(io); + } + } + + private static List parseArray(final JsonReader reader, Set types, int flags) throws IOException { final List list = new ArrayList<>(); + reader.beginArray(); final MessageDigest digest = Function.newDefaultDigest(); - final JsonReader jsonReader = read(arrayJson); - try { - jsonReader.beginArray(); - while (jsonReader.peek() != JsonToken.END_ARRAY) { - T e = tryParseStreaming(jsonReader, types, digest, flags); - if (e != null) { - list.add(e); - } + while (reader.peek() != JsonToken.END_ARRAY) { + T e = tryParseStreaming(reader, types, digest, flags); + if (e != null) { + list.add(e); } - jsonReader.endArray(); - } catch (IOException io) { - throw new IllegalStateException(io); } + reader.endArray(); return list; } @@ -525,4 +527,19 @@ public static TupleType parseTupleType(JsonReader reader, final boolean event private static JsonReader read(String json) { return new JsonReader(new StringReader(json)); } + + public static List parseABIField(int flags, String objectJson, Set types) { + try (final JsonReader reader = read(objectJson)) { + reader.beginObject(); + while (reader.peek() != JsonToken.END_OBJECT) { + if ("abi".equals(reader.nextName())) { + return parseArray(reader, types, flags); + } + reader.skipValue(); + } + } catch (IOException io) { + throw new IllegalStateException(io); + } + throw new IllegalStateException("abi key not found"); + } } diff --git a/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java b/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java index 72b47f05..f38706be 100644 --- a/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java +++ b/src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java @@ -897,4 +897,26 @@ public void testUserDefinedValueTypes() { assertEquals(json, f.toString()); } + + @Test + public void testParseABIField() { + final String json = "{\n" + + " \"abi\": [\n" + + " {\n" + + " \"name\": \"aller\",\n" + + " \"inputs\": [\n" + + " { \"name\": \"amt\", \"type\": \"uint256\" },\n" + + " { \"name\": \"balance\", \"type\": \"uint256\" },\n" + + " { \"name\": \"rate\", \"type\": \"uint256\" },\n" + + " { \"name\": \"dest\", \"type\": \"address\" },\n" + + " { \"name\": \"msg\", \"type\": \"string\" }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}"; + final List objects = ABIJSON.parseABIField(FLAGS_NONE, json, ABIJSON.ALL); + assertEquals(1, objects.size()); + assertEquals(TypeEnum.FUNCTION, objects.get(0).getType()); + assertEquals("aller(uint256,uint256,uint256,address,string)", objects.get(0).getCanonicalSignature()); + } }