Skip to content

Commit 1f4fcb8

Browse files
committed
Refactoring to support resolveAddress
1 parent 9dcdc23 commit 1f4fcb8

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

convex-restapi/src/main/java/convex/restapi/api/ABaseAPI.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package convex.restapi.api;
22

33
import convex.peer.Server;
4+
import convex.core.cvm.Address;
5+
import convex.core.data.AString;
6+
import convex.core.data.ACell;
7+
import convex.core.lang.Reader;
48
import convex.restapi.RESTServer;
9+
import convex.core.lang.RT;
510
import io.javalin.http.BadRequestResponse;
611
import io.javalin.http.Context;
12+
import convex.core.data.AVector;
713

814
/**
915
* Base class for API based services
@@ -132,5 +138,30 @@ protected long[] getPaginationRange(Context ctx, long maxIndex) {
132138
return range;
133139
}
134140

141+
/**
142+
* Attempt to resolve an address from an arbitrary object, including possible CNS lookups.
143+
* @param o Object to resolve an address from.
144+
* @return Address instance, or null if not a valid address.
145+
*/
146+
protected static Address resolveAddress(ACell o) {
147+
if (o instanceof Address a) {
148+
return a;
149+
}
150+
151+
if (o instanceof AVector v) {
152+
if (v.count() !=2) return null; // must be a scoped address
153+
return resolveAddress(v.get(0)); // resolve the base address
154+
}
135155

156+
try {
157+
// If it's a String, try to parse it as an address
158+
AString s = RT.ensureString(o);
159+
if (s != null) {
160+
return resolveAddress(Reader.read(s));
161+
}
162+
return null;
163+
} catch (Exception e) {
164+
throw new IllegalArgumentException("Unable to resolve address from object: "+o+" cause: "+e.getMessage(), e);
165+
}
166+
}
136167
}

convex-restapi/src/main/java/convex/restapi/api/McpAPI.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws Interr
401401
} catch (Exception e) {
402402
return toolError("Failed to parse query source: " + e.getMessage());
403403
}
404-
Address address = Address.parse(arguments.get(ARG_ADDRESS));
404+
Address address = resolveAddress(arguments.get(ARG_ADDRESS)); // OK if null
405405
Convex convex = restServer.getConvex();
406406
Result result = convex.querySync(form, address);
407407
return toolResult(result);
@@ -436,7 +436,12 @@ public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws Interr
436436
if ((seedBlob == null) || (seedBlob.count() != AKeyPair.SEED_LENGTH)) {
437437
return toolError("Seed must be 32-byte hex string");
438438
}
439-
Address address= Address.parse(addressCell.toString());
439+
Address address;
440+
try {
441+
address = resolveAddress(addressCell);
442+
} catch (IllegalArgumentException e) {
443+
return toolError("Invalid address format: " + e.getMessage());
444+
}
440445
if (address == null) {
441446
return toolError("Invalid address format");
442447
}
@@ -469,7 +474,12 @@ public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws Interr
469474
if (addressCell == null) {
470475
return protocolError(-32602, "Prepare requires 'address' string");
471476
}
472-
Address address = Address.parse(addressCell.toString());
477+
Address address;
478+
try {
479+
address = resolveAddress(addressCell);
480+
} catch (IllegalArgumentException e) {
481+
return toolError("Invalid address format: " + e.getMessage());
482+
}
473483
if (address == null) {
474484
return toolError("Invalid address format");
475485
}
@@ -875,7 +885,12 @@ private class DescribeAccountTool extends McpTool {
875885
@Override
876886
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
877887
try {
878-
Address address = Address.parse(RT.getIn(arguments,ARG_ADDRESS));
888+
Address address;
889+
try {
890+
address = resolveAddress(RT.getIn(arguments,ARG_ADDRESS));
891+
} catch (IllegalArgumentException e) {
892+
return toolError("Invalid address format: " + e.getMessage());
893+
}
879894
if (address == null) {
880895
return toolError("No valid address provided");
881896
}
@@ -929,7 +944,12 @@ public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws Interr
929944
if (addressCell == null) {
930945
return toolError("Lookup requires 'address' parameter, e.g. '#5675' or '@convex.core'");
931946
}
932-
Address address = Address.parse(addressCell);
947+
Address address;
948+
try {
949+
address = resolveAddress(addressCell);
950+
} catch (IllegalArgumentException e) {
951+
return toolError("Invalid address format: " + e.getMessage());
952+
}
933953
if (address == null) {
934954
return toolError("Invalid address format");
935955
}

0 commit comments

Comments
 (0)