Skip to content

Commit

Permalink
add annotate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Oct 18, 2024
1 parent c930ba6 commit 0138b30
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/java/com/esaulpaugh/headlong/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ static String eval(String[] args) {
case "-e": return encodeABI(args, false, false);
case "-ef": return encodeABI(args, false, true);
case "-efform": return Function.formatCall(Strings.decode(encodeABI(args, false, true)));
case "-efann": return encodeAnnotated(args, false, true);
case "-ep": return encodeABIPacked(args, false);
case "-eann": return encodeAnnotated(args, false, false);
case "-mep": return encodeABIPacked(args, true);
case "-d": return decodeABI(args, false, false, false);
case "-dc": return decodeABI(args, false, false, true);
Expand All @@ -104,7 +106,9 @@ static String eval(String[] args) {
case "-dp": return decodeABIPacked(args, false);
case "-dpc": return decodeABIPacked(args, true);
case "-me": return encodeABI(args, true, false);
case "-meann": return encodeAnnotated(args, true, false);
case "-mef": return encodeABI(args, true, true);
case "-mefann": return encodeAnnotated(args, true, true);
case "-md": return decodeABI(args, true, false, false);
case "-mdf": return decodeABI(args, true, true, false);
case "-re": return encodeRLP(args);
Expand Down Expand Up @@ -295,14 +299,26 @@ private static String formatFunctionCall(String[] args) {
return Function.formatCall(Strings.decode(DATA_FIRST.from(args)));
}

private static String encodeAnnotated(String[] args, boolean machine, boolean function) {
final String signature = DATA_FIRST.from(args);
final String values = parseVals(DATA_SECOND.from(args), machine, true);
if (function) {
Function f = Function.parse(signature);
return f.annotateCall(SuperSerial.deserialize(f.getInputs(), values, machine));
} else {
TupleType<Tuple> tt = TupleType.parse(signature);
return tt.annotate(SuperSerial.deserialize(tt, values, machine));
}
}

private static String parseAbiJson(String[] args) {
final String json = DATA_FIRST.from(args);
if(json.startsWith("[")) {
if (json.startsWith("[")) {
return ABIJSON.parseElements(json)
.stream()
.map(Main::describe)
.collect(Collectors.joining("\n"));
} else if(json.startsWith("{")) {
} else if (json.startsWith("{")) {
return describe(ABIObject.fromJson(json));
} else {
throw new IllegalArgumentException("json must start with '[' or '{'");
Expand All @@ -315,12 +331,12 @@ private static String jsonToSignature(String[] args) {
}

private static String describe(ABIObject o) {
if(o.isFunction()) {
if (o.isFunction()) {
Function foo = o.asFunction();
return foo.getType().name() + " " + o.getCanonicalSignature() + getParamNames(o.getInputs()) + " returns: " + foo.getOutputs().getCanonicalType() + " stateMutability: " + foo.getStateMutability();
} else if (o.isEvent()) {
return "event " + o.getCanonicalSignature() + getParamNames(o.getInputs()) + " indexed:" + Arrays.toString(o.asEvent().getIndexManifest());
} else if(o.isContractError()) {
} else if (o.isContractError()) {
return "error " + o.getCanonicalSignature() + getParamNames(o.getInputs());
}
throw new AssertionError();
Expand All @@ -332,7 +348,7 @@ private static String getParamNames(TupleType params) {
for (int i = 0; i < size; i++) {
hasName |= params.getElementName(i) != null;
}
if(!hasName) return " ";
if (!hasName) return " ";
StringBuilder sb = new StringBuilder(" names:(");
for (int i = 0; i < size; i++) {
sb.append(params.getElementName(i))
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/esaulpaugh/headlong/cli/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,57 @@ public void testEIP55() {
assertEquals(new BigInteger(expected.substring(2), 16), Address.wrap(expected).value());
}

@Test
public void testAnnotate() {
assertEquals(
"foo:\n" +
"ID 9d879739\n" +
" 0 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80\t[0] int8\n" +
" 20 0000000000000000000000000000000000000000000000000000000000000040\t[1] bytes offset\n" +
" 40 0000000000000000000000000000000000000000000000000000000000000006\t[1] bytes length\n" +
" 60 25486c0a2d2b0000000000000000000000000000000000000000000000000000\t[1] bytes",
Main.eval(new String[] { "-efann", "foo(int8,bytes)",
"(\n" +
" '80',\n" +
" '25486c0a2d2b'\n" +
")"
})
);
assertEquals(
" 0 0000000000000000000000000000000000000000000000000000000000000040\t[0] bytes offset\n" +
" 20 0000000000000000000000000000000000000000000000000000000000000080\t[1] uint8\n" +
" 40 0000000000000000000000000000000000000000000000000000000000000006\t[0] bytes length\n" +
" 60 25486c0a2d2b0000000000000000000000000000000000000000000000000000\t[0] bytes",
Main.eval(new String[] { "-eann", "(bytes,uint8)",
"(\n" +
" '25486c0a2d2b',\n" +
" '80'\n" +
")"
})
);

assertEquals(
"foo:\n" +
"ID 9d879739\n" +
" 0 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80\t[0] int8\n" +
" 20 0000000000000000000000000000000000000000000000000000000000000040\t[1] bytes offset\n" +
" 40 0000000000000000000000000000000000000000000000000000000000000006\t[1] bytes length\n" +
" 60 25486c0a2d2b0000000000000000000000000000000000000000000000000000\t[1] bytes",
Main.eval(new String[] { "-mefann", "foo(int8,bytes)",
"81808625486c0a2d2b"
})
);
assertEquals(
" 0 0000000000000000000000000000000000000000000000000000000000000040\t[0] bytes offset\n" +
" 20 0000000000000000000000000000000000000000000000000000000000000080\t[1] uint8\n" +
" 40 0000000000000000000000000000000000000000000000000000000000000006\t[0] bytes length\n" +
" 60 25486c0a2d2b0000000000000000000000000000000000000000000000000000\t[0] bytes",
Main.eval(new String[] { "-meann", "(bytes,uint8)",
"8625486c0a2d2b8180"
})
);
}

@FunctionalInterface
public interface CustomRunnable {
void run() throws Throwable;
Expand Down

0 comments on commit 0138b30

Please sign in to comment.