@absol-labs/agent 0.11.0 → 0.12.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -51
- package/dist/capability/invocation-capability.d.ts +2 -2
- package/dist/capability/probe-capability.d.ts +225 -0
- package/dist/capability/probe-capability.d.ts.map +1 -0
- package/dist/capability/probe-capability.js +248 -0
- package/dist/capability/probe-capability.js.map +1 -0
- package/dist/cli/args.d.ts +11 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +23 -0
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/commands/claim.d.ts +32 -0
- package/dist/cli/commands/claim.d.ts.map +1 -1
- package/dist/cli/commands/claim.js +116 -28
- package/dist/cli/commands/claim.js.map +1 -1
- package/dist/cli/commands/close.d.ts.map +1 -1
- package/dist/cli/commands/close.js +35 -3
- package/dist/cli/commands/close.js.map +1 -1
- package/dist/cli/commands/invoke.d.ts +2 -0
- package/dist/cli/commands/invoke.d.ts.map +1 -1
- package/dist/cli/commands/invoke.js +2 -2
- package/dist/cli/commands/invoke.js.map +1 -1
- package/dist/cli/commands/mcp.d.ts.map +1 -1
- package/dist/cli/commands/mcp.js +15 -2
- package/dist/cli/commands/mcp.js.map +1 -1
- package/dist/cli/commands/open.d.ts.map +1 -1
- package/dist/cli/commands/open.js +58 -4
- package/dist/cli/commands/open.js.map +1 -1
- package/dist/cli/commands/pay.d.ts +34 -0
- package/dist/cli/commands/pay.d.ts.map +1 -0
- package/dist/cli/commands/pay.js +479 -0
- package/dist/cli/commands/pay.js.map +1 -0
- package/dist/cli/commands/reclaim-unverified.d.ts.map +1 -1
- package/dist/cli/commands/reclaim-unverified.js +3 -2
- package/dist/cli/commands/reclaim-unverified.js.map +1 -1
- package/dist/cli/commands/reclaim.d.ts +23 -2
- package/dist/cli/commands/reclaim.d.ts.map +1 -1
- package/dist/cli/commands/reclaim.js +204 -20
- package/dist/cli/commands/reclaim.js.map +1 -1
- package/dist/cli/commands/refund.d.ts +31 -0
- package/dist/cli/commands/refund.d.ts.map +1 -0
- package/dist/cli/commands/refund.js +397 -0
- package/dist/cli/commands/refund.js.map +1 -0
- package/dist/cli/commands/status.d.ts.map +1 -1
- package/dist/cli/commands/status.js +21 -2
- package/dist/cli/commands/status.js.map +1 -1
- package/dist/cli/commands/streams.js +8 -12
- package/dist/cli/commands/streams.js.map +1 -1
- package/dist/cli/commands/watch.d.ts.map +1 -1
- package/dist/cli/commands/watch.js +4 -1
- package/dist/cli/commands/watch.js.map +1 -1
- package/dist/cli/config.d.ts +44 -26
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +88 -38
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/context.d.ts +19 -0
- package/dist/cli/context.d.ts.map +1 -1
- package/dist/cli/deps.d.ts +53 -0
- package/dist/cli/deps.d.ts.map +1 -1
- package/dist/cli/deps.js +51 -1
- package/dist/cli/deps.js.map +1 -1
- package/dist/cli/errors.d.ts.map +1 -1
- package/dist/cli/errors.js +21 -1
- package/dist/cli/errors.js.map +1 -1
- package/dist/cli/escape-hatch.d.ts +6 -4
- package/dist/cli/escape-hatch.d.ts.map +1 -1
- package/dist/cli/escape-hatch.js +6 -2
- package/dist/cli/escape-hatch.js.map +1 -1
- package/dist/cli/listings.d.ts +18 -0
- package/dist/cli/listings.d.ts.map +1 -1
- package/dist/cli/listings.js +22 -12
- package/dist/cli/listings.js.map +1 -1
- package/dist/cli/preflight.d.ts +20 -14
- package/dist/cli/preflight.d.ts.map +1 -1
- package/dist/cli/preflight.js +34 -20
- package/dist/cli/preflight.js.map +1 -1
- package/dist/cli/privy.d.ts.map +1 -1
- package/dist/cli/privy.js +4 -2
- package/dist/cli/privy.js.map +1 -1
- package/dist/cli/progress.d.ts +13 -1
- package/dist/cli/progress.d.ts.map +1 -1
- package/dist/cli/progress.js +7 -1
- package/dist/cli/progress.js.map +1 -1
- package/dist/cli/reclaimability.d.ts +82 -0
- package/dist/cli/reclaimability.d.ts.map +1 -0
- package/dist/cli/reclaimability.js +80 -0
- package/dist/cli/reclaimability.js.map +1 -0
- package/dist/cli/run.d.ts +2 -1
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +62 -21
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/scan-range.d.ts +2 -2
- package/dist/cli/scan-range.js +2 -2
- package/dist/cli/status.d.ts +106 -1
- package/dist/cli/status.d.ts.map +1 -1
- package/dist/cli/status.js +325 -44
- package/dist/cli/status.js.map +1 -1
- package/dist/cli/wallet.d.ts +2 -2
- package/dist/cli/wallet.d.ts.map +1 -1
- package/dist/cli/wallet.js +29 -5
- package/dist/cli/wallet.js.map +1 -1
- package/dist/discovery/rate-band.d.ts +74 -0
- package/dist/discovery/rate-band.d.ts.map +1 -0
- package/dist/discovery/rate-band.js +114 -0
- package/dist/discovery/rate-band.js.map +1 -0
- package/dist/escrow-generation.d.ts +81 -0
- package/dist/escrow-generation.d.ts.map +1 -0
- package/dist/escrow-generation.js +76 -0
- package/dist/escrow-generation.js.map +1 -0
- package/dist/frameworks/agentkit.d.ts +8 -4
- package/dist/frameworks/agentkit.d.ts.map +1 -1
- package/dist/frameworks/agentkit.js +27 -10
- package/dist/frameworks/agentkit.js.map +1 -1
- package/dist/frameworks/eliza.d.ts +3 -3
- package/dist/frameworks/eliza.d.ts.map +1 -1
- package/dist/frameworks/eliza.js +15 -6
- package/dist/frameworks/eliza.js.map +1 -1
- package/dist/frameworks/langchain.d.ts.map +1 -1
- package/dist/frameworks/langchain.js +8 -0
- package/dist/frameworks/langchain.js.map +1 -1
- package/dist/gateway/caller-auth-gateway.d.ts +83 -31
- package/dist/gateway/caller-auth-gateway.d.ts.map +1 -1
- package/dist/gateway/caller-auth-gateway.js +180 -27
- package/dist/gateway/caller-auth-gateway.js.map +1 -1
- package/dist/gateway/http-server.d.ts +128 -44
- package/dist/gateway/http-server.d.ts.map +1 -1
- package/dist/gateway/http-server.js +506 -155
- package/dist/gateway/http-server.js.map +1 -1
- package/dist/gateway/probe-auth.d.ts +112 -0
- package/dist/gateway/probe-auth.d.ts.map +1 -0
- package/dist/gateway/probe-auth.js +127 -0
- package/dist/gateway/probe-auth.js.map +1 -0
- package/dist/gateway/signed-rate-bounds.d.ts +51 -0
- package/dist/gateway/signed-rate-bounds.d.ts.map +1 -0
- package/dist/gateway/signed-rate-bounds.js +94 -0
- package/dist/gateway/signed-rate-bounds.js.map +1 -0
- package/dist/gateway/x402-opener-mode.d.ts +252 -0
- package/dist/gateway/x402-opener-mode.d.ts.map +1 -0
- package/dist/gateway/x402-opener-mode.js +772 -0
- package/dist/gateway/x402-opener-mode.js.map +1 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -5
- package/dist/index.js.map +1 -1
- package/dist/mcp/server.d.ts +64 -1
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +247 -37
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp/x402-tools.d.ts +109 -0
- package/dist/mcp/x402-tools.d.ts.map +1 -0
- package/dist/mcp/x402-tools.js +554 -0
- package/dist/mcp/x402-tools.js.map +1 -0
- package/dist/network.d.ts +119 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +199 -0
- package/dist/network.js.map +1 -0
- package/dist/sdk/client.d.ts +13 -0
- package/dist/sdk/client.d.ts.map +1 -1
- package/dist/sdk/client.js +15 -0
- package/dist/sdk/client.js.map +1 -1
- package/dist/sdk/invoke.d.ts +4 -0
- package/dist/sdk/invoke.d.ts.map +1 -1
- package/dist/sdk/invoke.js +2 -5
- package/dist/sdk/invoke.js.map +1 -1
- package/dist/wallet/autonomous-wallet-broker.d.ts +3 -0
- package/dist/wallet/autonomous-wallet-broker.d.ts.map +1 -1
- package/dist/wallet/autonomous-wallet-broker.js +27 -23
- package/dist/wallet/autonomous-wallet-broker.js.map +1 -1
- package/dist/wallet/autonomous-wallet-protocol.d.ts +8 -2
- package/dist/wallet/autonomous-wallet-protocol.d.ts.map +1 -1
- package/dist/wallet/autonomous-wallet-protocol.js +9 -2
- package/dist/wallet/autonomous-wallet-protocol.js.map +1 -1
- package/dist/wallet/autonomous-wallet.d.ts.map +1 -1
- package/dist/wallet/autonomous-wallet.js +3 -2
- package/dist/wallet/autonomous-wallet.js.map +1 -1
- package/dist/wallet/broker-escrows.d.ts +222 -0
- package/dist/wallet/broker-escrows.d.ts.map +1 -0
- package/dist/wallet/broker-escrows.js +36 -0
- package/dist/wallet/broker-escrows.js.map +1 -0
- package/dist/wallet/lifecycle.d.ts +21 -5
- package/dist/wallet/lifecycle.d.ts.map +1 -1
- package/dist/wallet/lifecycle.js +32 -12
- package/dist/wallet/lifecycle.js.map +1 -1
- package/dist/wallet/privy-broker-server.d.ts.map +1 -1
- package/dist/wallet/privy-broker-server.js +31 -26
- package/dist/wallet/privy-broker-server.js.map +1 -1
- package/dist/wallet/privy-session-broker.d.ts +14 -5
- package/dist/wallet/privy-session-broker.d.ts.map +1 -1
- package/dist/wallet/privy-session-broker.js +29 -17
- package/dist/wallet/privy-session-broker.js.map +1 -1
- package/dist/wallet/privy-session-provider.d.ts +3 -1
- package/dist/wallet/privy-session-provider.d.ts.map +1 -1
- package/dist/wallet/privy-session-provider.js +5 -3
- package/dist/wallet/privy-session-provider.js.map +1 -1
- package/dist/wallet/provider.d.ts.map +1 -1
- package/dist/wallet/provider.js +15 -8
- package/dist/wallet/provider.js.map +1 -1
- package/dist/x402/facilitator.d.ts +3 -30
- package/dist/x402/facilitator.d.ts.map +1 -1
- package/dist/x402/facilitator.js +0 -27
- package/dist/x402/facilitator.js.map +1 -1
- package/dist/x402-buyer/guarded-fetch.d.ts +51 -0
- package/dist/x402-buyer/guarded-fetch.d.ts.map +1 -0
- package/dist/x402-buyer/guarded-fetch.js +147 -0
- package/dist/x402-buyer/guarded-fetch.js.map +1 -0
- package/dist/x402-buyer/index.d.ts +10 -0
- package/dist/x402-buyer/index.d.ts.map +1 -0
- package/dist/x402-buyer/index.js +10 -0
- package/dist/x402-buyer/index.js.map +1 -0
- package/dist/x402-buyer/pay.d.ts +157 -0
- package/dist/x402-buyer/pay.d.ts.map +1 -0
- package/dist/x402-buyer/pay.js +366 -0
- package/dist/x402-buyer/pay.js.map +1 -0
- package/dist/x402-opener/abi.d.ts +684 -0
- package/dist/x402-opener/abi.d.ts.map +1 -0
- package/dist/x402-opener/abi.js +880 -0
- package/dist/x402-opener/abi.js.map +1 -0
- package/dist/x402-opener/facilitator-entry.d.ts +3 -0
- package/dist/x402-opener/facilitator-entry.d.ts.map +1 -0
- package/dist/x402-opener/facilitator-entry.js +40 -0
- package/dist/x402-opener/facilitator-entry.js.map +1 -0
- package/dist/x402-opener/facilitator-server.d.ts +209 -0
- package/dist/x402-opener/facilitator-server.d.ts.map +1 -0
- package/dist/x402-opener/facilitator-server.js +762 -0
- package/dist/x402-opener/facilitator-server.js.map +1 -0
- package/dist/x402-opener/index.d.ts +14 -0
- package/dist/x402-opener/index.d.ts.map +1 -0
- package/dist/x402-opener/index.js +14 -0
- package/dist/x402-opener/index.js.map +1 -0
- package/dist/x402-opener/listing-resolver.d.ts +55 -0
- package/dist/x402-opener/listing-resolver.d.ts.map +1 -0
- package/dist/x402-opener/listing-resolver.js +125 -0
- package/dist/x402-opener/listing-resolver.js.map +1 -0
- package/dist/x402-opener/refund-relay.d.ts +266 -0
- package/dist/x402-opener/refund-relay.d.ts.map +1 -0
- package/dist/x402-opener/refund-relay.js +1341 -0
- package/dist/x402-opener/refund-relay.js.map +1 -0
- package/dist/x402-opener/relayer-queue.d.ts +14 -0
- package/dist/x402-opener/relayer-queue.d.ts.map +1 -0
- package/dist/x402-opener/relayer-queue.js +18 -0
- package/dist/x402-opener/relayer-queue.js.map +1 -0
- package/dist/x402-opener/scheme.d.ts +296 -0
- package/dist/x402-opener/scheme.d.ts.map +1 -0
- package/dist/x402-opener/scheme.js +1035 -0
- package/dist/x402-opener/scheme.js.map +1 -0
- package/dist/x402-opener/terms.d.ts +165 -0
- package/dist/x402-opener/terms.d.ts.map +1 -0
- package/dist/x402-opener/terms.js +304 -0
- package/dist/x402-opener/terms.js.map +1 -0
- package/dist/zktls/reclaim.d.ts +2 -2
- package/dist/zktls/t2-delivery-proof.d.ts +6 -6
- package/docs/agent-layer.md +3 -2
- package/docs/autonomous-privy-wallet.md +188 -0
- package/docs/cli.md +265 -34
- package/docs/langchain.md +5 -0
- package/docs/mcp-reference.md +91 -5
- package/docs/probe-capability.md +191 -0
- package/docs/quickstart.md +39 -4
- package/docs/threat-model.md +20 -0
- package/docs/x402.md +766 -593
- package/package.json +19 -17
- package/src/capability/probe-capability.ts +355 -0
- package/src/cli/args.ts +29 -0
- package/src/cli/commands/claim.ts +168 -33
- package/src/cli/commands/close.ts +36 -3
- package/src/cli/commands/invoke.ts +5 -2
- package/src/cli/commands/mcp.ts +18 -2
- package/src/cli/commands/open.ts +75 -4
- package/src/cli/commands/pay.ts +584 -0
- package/src/cli/commands/reclaim-unverified.ts +8 -2
- package/src/cli/commands/reclaim.ts +277 -19
- package/src/cli/commands/refund.ts +475 -0
- package/src/cli/commands/status.ts +28 -2
- package/src/cli/commands/streams.ts +10 -14
- package/src/cli/commands/watch.ts +11 -1
- package/src/cli/config.ts +139 -52
- package/src/cli/context.ts +16 -0
- package/src/cli/deps.ts +111 -1
- package/src/cli/errors.ts +24 -1
- package/src/cli/escape-hatch.ts +19 -5
- package/src/cli/listings.ts +43 -10
- package/src/cli/preflight.ts +36 -21
- package/src/cli/privy.ts +4 -2
- package/src/cli/progress.ts +21 -2
- package/src/cli/reclaimability.ts +154 -0
- package/src/cli/run.ts +77 -21
- package/src/cli/scan-range.ts +2 -2
- package/src/cli/status.ts +454 -44
- package/src/cli/wallet.ts +45 -8
- package/src/discovery/rate-band.ts +172 -0
- package/src/escrow-generation.ts +135 -0
- package/src/frameworks/agentkit.ts +45 -11
- package/src/frameworks/eliza.ts +27 -5
- package/src/frameworks/langchain.ts +14 -0
- package/src/gateway/caller-auth-gateway.ts +320 -68
- package/src/gateway/http-server.ts +717 -220
- package/src/gateway/probe-auth.ts +191 -0
- package/src/gateway/signed-rate-bounds.ts +164 -0
- package/src/gateway/x402-opener-mode.ts +1171 -0
- package/src/index.ts +82 -19
- package/src/mcp/server.ts +351 -42
- package/src/mcp/x402-tools.ts +785 -0
- package/src/network.ts +302 -0
- package/src/sdk/client.ts +25 -0
- package/src/sdk/invoke.ts +6 -5
- package/src/wallet/autonomous-wallet-broker.ts +46 -20
- package/src/wallet/autonomous-wallet-protocol.ts +11 -2
- package/src/wallet/autonomous-wallet.ts +4 -2
- package/src/wallet/broker-escrows.ts +66 -0
- package/src/wallet/lifecycle.ts +53 -18
- package/src/wallet/privy-broker-server.ts +35 -25
- package/src/wallet/privy-session-broker.ts +58 -25
- package/src/wallet/privy-session-provider.ts +7 -3
- package/src/wallet/provider.ts +20 -15
- package/src/x402/facilitator.ts +3 -43
- package/src/x402-buyer/guarded-fetch.ts +213 -0
- package/src/x402-buyer/index.ts +9 -0
- package/src/x402-buyer/pay.ts +555 -0
- package/src/x402-opener/abi.ts +881 -0
- package/src/x402-opener/facilitator-entry.ts +48 -0
- package/src/x402-opener/facilitator-server.ts +1114 -0
- package/src/x402-opener/index.ts +13 -0
- package/src/x402-opener/listing-resolver.ts +202 -0
- package/src/x402-opener/refund-relay.ts +1873 -0
- package/src/x402-opener/relayer-queue.ts +18 -0
- package/src/x402-opener/scheme.ts +1551 -0
- package/src/x402-opener/terms.ts +453 -0
- package/dist/gateway/x402-mode.d.ts +0 -123
- package/dist/gateway/x402-mode.d.ts.map +0 -1
- package/dist/gateway/x402-mode.js +0 -212
- package/dist/gateway/x402-mode.js.map +0 -1
- package/dist/x402/delivery-oracle.d.ts +0 -108
- package/dist/x402/delivery-oracle.d.ts.map +0 -1
- package/dist/x402/delivery-oracle.js +0 -174
- package/dist/x402/delivery-oracle.js.map +0 -1
- package/dist/x402/facilitator-server-entry.d.ts +0 -3
- package/dist/x402/facilitator-server-entry.d.ts.map +0 -1
- package/dist/x402/facilitator-server-entry.js +0 -27
- package/dist/x402/facilitator-server-entry.js.map +0 -1
- package/dist/x402/facilitator-server.d.ts +0 -73
- package/dist/x402/facilitator-server.d.ts.map +0 -1
- package/dist/x402/facilitator-server.js +0 -305
- package/dist/x402/facilitator-server.js.map +0 -1
- package/dist/x402/payment-requirements.d.ts +0 -106
- package/dist/x402/payment-requirements.d.ts.map +0 -1
- package/dist/x402/payment-requirements.js +0 -138
- package/dist/x402/payment-requirements.js.map +0 -1
- package/dist/x402/relayer.d.ts +0 -41
- package/dist/x402/relayer.d.ts.map +0 -1
- package/dist/x402/relayer.js +0 -64
- package/dist/x402/relayer.js.map +0 -1
- package/dist/x402/resource-index.d.ts +0 -75
- package/dist/x402/resource-index.d.ts.map +0 -1
- package/dist/x402/resource-index.js +0 -126
- package/dist/x402/resource-index.js.map +0 -1
- package/dist/x402/verified-delivery-facilitator.d.ts +0 -285
- package/dist/x402/verified-delivery-facilitator.d.ts.map +0 -1
- package/dist/x402/verified-delivery-facilitator.js +0 -423
- package/dist/x402/verified-delivery-facilitator.js.map +0 -1
- package/src/gateway/x402-mode.ts +0 -350
- package/src/x402/delivery-oracle.ts +0 -285
- package/src/x402/facilitator-server-entry.ts +0 -35
- package/src/x402/facilitator-server.ts +0 -445
- package/src/x402/payment-requirements.ts +0 -231
- package/src/x402/relayer.ts +0 -122
- package/src/x402/resource-index.ts +0 -228
- package/src/x402/verified-delivery-facilitator.ts +0 -744
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { lookup as dnsLookup } from "node:dns/promises";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
/** A request the guard refused; nothing was sent. */
|
|
4
|
+
export class OutboundRequestRefusedError extends Error {
|
|
5
|
+
constructor(reason) {
|
|
6
|
+
super(`outbound request refused: ${reason}`);
|
|
7
|
+
this.name = "OutboundRequestRefusedError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const defaultLookup = async (hostname) => await dnsLookup(hostname, { all: true, verbatim: true });
|
|
11
|
+
function ipv4ToInt(address) {
|
|
12
|
+
return address.split(".").reduce((acc, part) => acc * 256 + Number(part), 0);
|
|
13
|
+
}
|
|
14
|
+
const BLOCKED_V4 = [
|
|
15
|
+
["0.0.0.0", 8],
|
|
16
|
+
["10.0.0.0", 8],
|
|
17
|
+
["100.64.0.0", 10],
|
|
18
|
+
["127.0.0.0", 8],
|
|
19
|
+
["169.254.0.0", 16],
|
|
20
|
+
["172.16.0.0", 12],
|
|
21
|
+
["192.0.0.0", 24],
|
|
22
|
+
["192.0.2.0", 24],
|
|
23
|
+
["192.168.0.0", 16],
|
|
24
|
+
["198.18.0.0", 15],
|
|
25
|
+
["198.51.100.0", 24],
|
|
26
|
+
["203.0.113.0", 24],
|
|
27
|
+
["224.0.0.0", 4],
|
|
28
|
+
["240.0.0.0", 4],
|
|
29
|
+
];
|
|
30
|
+
function blockedV4(address) {
|
|
31
|
+
const value = ipv4ToInt(address);
|
|
32
|
+
return BLOCKED_V4.some(([base, bits]) => {
|
|
33
|
+
const mask = bits === 0 ? 0 : (~0 << (32 - bits)) >>> 0;
|
|
34
|
+
return (value & mask) >>> 0 === (ipv4ToInt(base) & mask) >>> 0;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function blockedV6(raw) {
|
|
38
|
+
const address = raw.toLowerCase().replace(/^\[|\]$/g, "");
|
|
39
|
+
if (address === "::" || address === "::1")
|
|
40
|
+
return true;
|
|
41
|
+
// IPv4-mapped / -compatible: judge the embedded IPv4 address.
|
|
42
|
+
const mapped = /^(?:::ffff:|::)(\d+\.\d+\.\d+\.\d+)$/.exec(address);
|
|
43
|
+
if (mapped?.[1] !== undefined)
|
|
44
|
+
return blockedV4(mapped[1]);
|
|
45
|
+
const hexMapped = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(address);
|
|
46
|
+
if (hexMapped?.[1] !== undefined && hexMapped[2] !== undefined) {
|
|
47
|
+
const hi = parseInt(hexMapped[1], 16);
|
|
48
|
+
const lo = parseInt(hexMapped[2], 16);
|
|
49
|
+
return blockedV4(`${hi >> 8}.${hi & 255}.${lo >> 8}.${lo & 255}`);
|
|
50
|
+
}
|
|
51
|
+
const first = parseInt(address.split(":")[0] || "0", 16);
|
|
52
|
+
return ((first & 0xfe00) === 0xfc00 || // fc00::/7 unique local
|
|
53
|
+
(first & 0xffc0) === 0xfe80 || // fe80::/10 link-local
|
|
54
|
+
(first & 0xffc0) === 0xfec0 || // fec0::/10 site-local (deprecated)
|
|
55
|
+
(first & 0xff00) === 0xff00 || // ff00::/8 multicast
|
|
56
|
+
address.startsWith("64:ff9b:") || // NAT64 (embeds IPv4)
|
|
57
|
+
address.startsWith("2001:db8:") // documentation
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
/** Whether an IP literal is in a range an outbound x402 request must not reach. */
|
|
61
|
+
export function isBlockedAddress(address) {
|
|
62
|
+
const bare = address.replace(/^\[|\]$/g, "");
|
|
63
|
+
const family = isIP(bare);
|
|
64
|
+
if (family === 4)
|
|
65
|
+
return blockedV4(bare);
|
|
66
|
+
if (family === 6)
|
|
67
|
+
return blockedV6(bare);
|
|
68
|
+
return true; // not an IP: never "safe" by default
|
|
69
|
+
}
|
|
70
|
+
function hostAllowed(hostname, allowed) {
|
|
71
|
+
const host = hostname.toLowerCase();
|
|
72
|
+
return allowed.some((entry) => {
|
|
73
|
+
const rule = entry.trim().toLowerCase();
|
|
74
|
+
if (rule.startsWith("*."))
|
|
75
|
+
return host.endsWith(rule.slice(1));
|
|
76
|
+
return host === rule;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** Validate one URL against the guard; throws {@link OutboundRequestRefusedError}. */
|
|
80
|
+
export async function assertOutboundUrlAllowed(raw, options = {}) {
|
|
81
|
+
let url;
|
|
82
|
+
try {
|
|
83
|
+
url = new URL(String(raw));
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
throw new OutboundRequestRefusedError(`"${String(raw)}" is not a URL`);
|
|
87
|
+
}
|
|
88
|
+
const allowPrivate = options.allowPrivateNetworks === true;
|
|
89
|
+
if (url.protocol !== "https:" &&
|
|
90
|
+
!(url.protocol === "http:" && allowPrivate)) {
|
|
91
|
+
throw new OutboundRequestRefusedError(`${url.protocol} is not allowed (https only)`);
|
|
92
|
+
}
|
|
93
|
+
if (url.username !== "" || url.password !== "") {
|
|
94
|
+
throw new OutboundRequestRefusedError("credentials in the URL");
|
|
95
|
+
}
|
|
96
|
+
const hostname = url.hostname.replace(/^\[|\]$/g, "");
|
|
97
|
+
if (options.allowedHosts !== undefined &&
|
|
98
|
+
options.allowedHosts.length > 0 &&
|
|
99
|
+
!hostAllowed(hostname, options.allowedHosts)) {
|
|
100
|
+
throw new OutboundRequestRefusedError(`${hostname} is not in METRIK_AGENT_X402_ALLOWED_HOSTS`);
|
|
101
|
+
}
|
|
102
|
+
if (allowPrivate)
|
|
103
|
+
return url;
|
|
104
|
+
if (isIP(hostname) !== 0) {
|
|
105
|
+
if (isBlockedAddress(hostname)) {
|
|
106
|
+
throw new OutboundRequestRefusedError(`${hostname} is a loopback, private, link-local or reserved address`);
|
|
107
|
+
}
|
|
108
|
+
return url;
|
|
109
|
+
}
|
|
110
|
+
if (hostname === "localhost" || hostname.endsWith(".localhost")) {
|
|
111
|
+
throw new OutboundRequestRefusedError(`${hostname} is loopback`);
|
|
112
|
+
}
|
|
113
|
+
let addresses;
|
|
114
|
+
try {
|
|
115
|
+
addresses = await (options.lookup ?? defaultLookup)(hostname);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
throw new OutboundRequestRefusedError(`${hostname} did not resolve`);
|
|
119
|
+
}
|
|
120
|
+
if (addresses.length === 0) {
|
|
121
|
+
throw new OutboundRequestRefusedError(`${hostname} did not resolve`);
|
|
122
|
+
}
|
|
123
|
+
const blocked = addresses.find((entry) => isBlockedAddress(entry.address));
|
|
124
|
+
if (blocked !== undefined) {
|
|
125
|
+
throw new OutboundRequestRefusedError(`${hostname} resolves to ${blocked.address}, a loopback, private, link-local or reserved address`);
|
|
126
|
+
}
|
|
127
|
+
return url;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A `fetch` that applies {@link assertOutboundUrlAllowed} to every request and
|
|
131
|
+
* never follows a redirect.
|
|
132
|
+
*/
|
|
133
|
+
export function createGuardedFetch(options = {}) {
|
|
134
|
+
const base = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
135
|
+
const guarded = async (input, init) => {
|
|
136
|
+
const target = input instanceof Request ? input.url : input;
|
|
137
|
+
await assertOutboundUrlAllowed(target, options);
|
|
138
|
+
const response = await base(input, { ...init, redirect: "manual" });
|
|
139
|
+
if (response.type === "opaqueredirect" ||
|
|
140
|
+
(response.status >= 300 && response.status < 400)) {
|
|
141
|
+
throw new OutboundRequestRefusedError(`${new URL(String(target)).host} answered with a redirect (${response.status}), which is never followed`);
|
|
142
|
+
}
|
|
143
|
+
return response;
|
|
144
|
+
};
|
|
145
|
+
return guarded;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=guarded-fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guarded-fetch.js","sourceRoot":"","sources":["../../src/x402-buyer/guarded-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAyChC,qDAAqD;AACrD,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IACpD,YAAY,MAAc;QACxB,KAAK,CAAC,6BAA6B,MAAM,EAAE,CAAC,CAAC;QAC7C,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;IAC5C,CAAC;CACF;AAED,MAAM,aAAa,GAAa,KAAK,EAAE,QAAQ,EAAE,EAAE,CACjD,MAAM,SAAS,CAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE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QAAQ,GAAG,MAAM,IAAI,CAAC,KAAK,EAAE,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;QACpE,IACE,QAAQ,CAAC,IAAI,KAAK,gBAAgB;YAClC,CAAC,QAAQ,CAAC,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC,EACjD,CAAC;YACD,MAAM,IAAI,2BAA2B,CACnC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,8BAA8B,QAAQ,CAAC,MAAM,4BAA4B,CACzG,CAAC;QACJ,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IACF,OAAO,OAAuB,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@absol-labs/agent/x402-buyer` — the BUYER side of Metrik x402: pay an x402
|
|
3
|
+
* v2 endpoint through `@absol-labs/sdk/x402`'s `payWithX402` with every SDK
|
|
4
|
+
* protection intact, gated by a spend mandate (`checkMandate()`) before
|
|
5
|
+
* anything is signed. Kept out of the root barrel so consumers that never use
|
|
6
|
+
* x402 do not load `@x402/*`. See docs/x402.md.
|
|
7
|
+
*/
|
|
8
|
+
export * from "./pay.js";
|
|
9
|
+
export * from "./guarded-fetch.js";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/x402-buyer/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAc,UAAU,CAAC;AACzB,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@absol-labs/agent/x402-buyer` — the BUYER side of Metrik x402: pay an x402
|
|
3
|
+
* v2 endpoint through `@absol-labs/sdk/x402`'s `payWithX402` with every SDK
|
|
4
|
+
* protection intact, gated by a spend mandate (`checkMandate()`) before
|
|
5
|
+
* anything is signed. Kept out of the root barrel so consumers that never use
|
|
6
|
+
* x402 do not load `@x402/*`. See docs/x402.md.
|
|
7
|
+
*/
|
|
8
|
+
export * from "./pay.js";
|
|
9
|
+
export * from "./guarded-fetch.js";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/x402-buyer/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAc,UAAU,CAAC;AACzB,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { MetrikNetwork } from "@absol-labs/sdk";
|
|
2
|
+
import { payWithX402, type PayWithX402Options, type PayWithX402Result, type SignedTransferAuthorization, type X402PayerSigner, type X402QuoteSummary } from "@absol-labs/sdk/x402";
|
|
3
|
+
import { type PublicClient } from "viem";
|
|
4
|
+
import { type HireRequest, type MandateDecision, type SignedSpendMandate } from "../mandates/mandate.js";
|
|
5
|
+
/**
|
|
6
|
+
* Buyer-side x402 payments under a spend mandate (metrik-agent#121).
|
|
7
|
+
*
|
|
8
|
+
* The golden rule of this package — no spend without a mandate — applies to an
|
|
9
|
+
* x402 payment exactly as it applies to `metrik open`, because a Metrik x402
|
|
10
|
+
* payment IS a stream open: the opener facilitator settles the payer's EIP-3009
|
|
11
|
+
* authorization with `factory.deployAndSettle`, which opens a `StreamEscrowV2`
|
|
12
|
+
* stream with `deposit = price`. So the payment is mapped onto the SAME
|
|
13
|
+
* {@link HireRequest} a stream open is checked as, from the quote's opener terms
|
|
14
|
+
* (which the SDK has already proven on-chain: `payTo == factory.predict(terms)`):
|
|
15
|
+
*
|
|
16
|
+
* | Mandate field | x402 quote field |
|
|
17
|
+
* | ---------------------- | ------------------------------------------------ |
|
|
18
|
+
* | `maxPerStreamUsdc` | `amount` (the deposit; the price of the call) |
|
|
19
|
+
* | `maxTotalUsdc` | `spentSoFar + amount` |
|
|
20
|
+
* | `maxRatePerSecondUsdc` | `ratePerSecond` (how fast the seller can earn) |
|
|
21
|
+
* | `maxDurationSeconds` | `maxDuration` (the stream's window) |
|
|
22
|
+
* | `allowedOperators` | `operator` (who the stream pays) |
|
|
23
|
+
*
|
|
24
|
+
* An UPFRONT ("minimum charge") opener quote is mapped the same way, with
|
|
25
|
+
* `budgetUsdc = price` — the WHOLE payment, of which `upfrontAmount` is
|
|
26
|
+
* non-refundable (paid to the seller at settlement) and only the rest is
|
|
27
|
+
* escrowed. So the per-stream and total caps always count the upfront part as
|
|
28
|
+
* spent, and nothing a refund returns is ever credited back. Its size is also
|
|
29
|
+
* capped separately, before signing, by the SDK's `maxUpfrontAmount` (default
|
|
30
|
+
* 0.10 USDC; pass {@link PayX402WithMandateInput.maxUpfrontAmount} to tighten
|
|
31
|
+
* it, `0n` to refuse every upfront charge).
|
|
32
|
+
*
|
|
33
|
+
* The check runs inside the SDK's `onQuote` hook — after every SDK protection
|
|
34
|
+
* (scheme, network, USDC domain, EIP-3009 only, registered factory, on-chain
|
|
35
|
+
* `predict`, authorization lifetime) and BEFORE anything is signed — so a
|
|
36
|
+
* denied payment never produces a signature. It is enforced twice: the mandate's
|
|
37
|
+
* caps are also handed to the SDK as `maxAmount` / `maxRatePerSecond`. A quote
|
|
38
|
+
* that is not a Metrik opener payment (no operator/rate/duration terms) is
|
|
39
|
+
* refused: there would be no stream to bound and nothing to recover.
|
|
40
|
+
*
|
|
41
|
+
* The mandate itself (signature, expiry, revocation) is verified FIRST, before
|
|
42
|
+
* any request reaches the seller.
|
|
43
|
+
*/
|
|
44
|
+
/** A payment refused by the spend mandate. Nothing was signed. */
|
|
45
|
+
export declare class X402MandateDeniedError extends Error {
|
|
46
|
+
readonly reason: NonNullable<MandateDecision["reason"]> | "not-an-opener-quote";
|
|
47
|
+
readonly quote: X402QuoteSummary | null;
|
|
48
|
+
constructor(reason: X402MandateDeniedError["reason"], quote: X402QuoteSummary | null, detail?: string);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The stream-open request an x402 quote would create, or a refusal reason when
|
|
52
|
+
* the quote does not carry Metrik opener terms.
|
|
53
|
+
*/
|
|
54
|
+
export declare function x402QuoteToHireRequest(quote: X402QuoteSummary): HireRequest | {
|
|
55
|
+
readonly refused: string;
|
|
56
|
+
};
|
|
57
|
+
export interface PayX402WithMandateInput {
|
|
58
|
+
readonly url: string | URL;
|
|
59
|
+
readonly init?: RequestInit;
|
|
60
|
+
/** The payer: signs the EIP-3009 authorization. Needs USDC, no gas. */
|
|
61
|
+
readonly signer: X402PayerSigner;
|
|
62
|
+
readonly network: MetrikNetwork;
|
|
63
|
+
readonly signedMandate: SignedSpendMandate;
|
|
64
|
+
/** USDC already spent under this mandate (the cumulative-cap input). */
|
|
65
|
+
readonly spentSoFarUsdc: bigint;
|
|
66
|
+
readonly nowSeconds: number;
|
|
67
|
+
readonly revokedMandateIds?: Iterable<`0x${string}`>;
|
|
68
|
+
/** An explicit price ceiling, tighter than the mandate (never looser). */
|
|
69
|
+
readonly maxAmount?: bigint;
|
|
70
|
+
/** An explicit rate ceiling, tighter than the mandate (never looser). */
|
|
71
|
+
readonly maxRatePerSecond?: bigint;
|
|
72
|
+
/** Refuse a quote the seller can fully earn in fewer verified seconds. */
|
|
73
|
+
readonly minDurationSeconds?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Ceiling on the NON-REFUNDABLE upfront part of an upfront-opener quote, base
|
|
76
|
+
* units (the SDK's `maxUpfrontAmount`). Default the SDK's 0.10 USDC; `0n`
|
|
77
|
+
* refuses any upfront charge. Never above the price ceiling in effect.
|
|
78
|
+
*/
|
|
79
|
+
readonly maxUpfrontAmount?: bigint;
|
|
80
|
+
/** Passed through to the SDK (transport, fetch, publicClient, ...). */
|
|
81
|
+
readonly sdkOptions?: Pick<PayWithX402Options, "publicClient" | "transport" | "fetch" | "allowInsecureHttp" | "maxAuthorizationSeconds" | "allowLegacyFactory">;
|
|
82
|
+
/** Test seam: the SDK payer. Default `payWithX402`. */
|
|
83
|
+
readonly payImpl?: typeof payWithX402;
|
|
84
|
+
/**
|
|
85
|
+
* Called once the mandate APPROVES a quote, BEFORE the SDK signs it. A
|
|
86
|
+
* caller that tracks cumulative spend reserves the payment here, so a
|
|
87
|
+
* failure after the signature is handed out can never leave it uncounted.
|
|
88
|
+
* Throwing refuses the quote (nothing is signed).
|
|
89
|
+
*/
|
|
90
|
+
readonly onApproved?: (approval: {
|
|
91
|
+
readonly request: HireRequest;
|
|
92
|
+
readonly quote: X402QuoteSummary;
|
|
93
|
+
}) => void | Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Chain reads used after a post-signature failure (USDC
|
|
96
|
+
* `authorizationState`, the opener's `StreamOpenedFor` log). Default: the
|
|
97
|
+
* SDK options' `publicClient`, else a client over their `transport`, else
|
|
98
|
+
* the network's first RPC.
|
|
99
|
+
*/
|
|
100
|
+
readonly chainReader?: Pick<PublicClient, "readContract" | "getLogs" | "getBlockNumber">;
|
|
101
|
+
}
|
|
102
|
+
/** What is known about a payment whose signature was handed out but that did not complete cleanly. */
|
|
103
|
+
export type X402PostSignatureState =
|
|
104
|
+
/** The authorization was used: the money left the payer. */
|
|
105
|
+
"used"
|
|
106
|
+
/** It could not be established either way. Treat as used. */
|
|
107
|
+
| "possibly-used"
|
|
108
|
+
/** Not used yet, but still valid until `validBefore`: a holder can still submit it. */
|
|
109
|
+
| "unused-but-valid"
|
|
110
|
+
/** Not used and past `validBefore`: it can never move funds. */
|
|
111
|
+
| "unused-expired";
|
|
112
|
+
/**
|
|
113
|
+
* The payment was APPROVED and its authorization signed and handed to the
|
|
114
|
+
* seller, then something failed (a malformed or missing PAYMENT-RESPONSE, a
|
|
115
|
+
* settlement the SDK could not confirm, a dropped connection). The money may
|
|
116
|
+
* have moved: {@link X402PostSignatureError.state} says what is known, and
|
|
117
|
+
* {@link X402PostSignatureError.releasable} is `true` only when the
|
|
118
|
+
* authorization provably can never be used. A spend reservation must be kept
|
|
119
|
+
* otherwise.
|
|
120
|
+
*/
|
|
121
|
+
export declare class X402PostSignatureError extends Error {
|
|
122
|
+
readonly state: X402PostSignatureState;
|
|
123
|
+
readonly approved: HireRequest;
|
|
124
|
+
readonly quote: X402QuoteSummary;
|
|
125
|
+
readonly authorization: SignedTransferAuthorization | null;
|
|
126
|
+
/** The stream the payment opened, when it could be identified. */
|
|
127
|
+
readonly streamId: `0x${string}` | null;
|
|
128
|
+
readonly opener: `0x${string}`;
|
|
129
|
+
/**
|
|
130
|
+
* `true` when the chain shows (or the signed quote implies) an UPFRONT-ONLY
|
|
131
|
+
* payment: no stream, the whole price was a non-refundable charge, nothing
|
|
132
|
+
* to recover.
|
|
133
|
+
*/
|
|
134
|
+
readonly upfrontOnly: boolean;
|
|
135
|
+
constructor(context: {
|
|
136
|
+
readonly state: X402PostSignatureState;
|
|
137
|
+
readonly approved: HireRequest;
|
|
138
|
+
readonly quote: X402QuoteSummary;
|
|
139
|
+
readonly authorization: SignedTransferAuthorization | null;
|
|
140
|
+
readonly streamId: `0x${string}` | null;
|
|
141
|
+
readonly upfrontOnly?: boolean;
|
|
142
|
+
readonly cause: unknown;
|
|
143
|
+
});
|
|
144
|
+
/** Only an authorization that provably can never move funds frees its reservation. */
|
|
145
|
+
get releasable(): boolean;
|
|
146
|
+
}
|
|
147
|
+
export interface PayX402WithMandateResult extends PayWithX402Result {
|
|
148
|
+
/** The stream-open request the mandate approved (null if nothing was paid). */
|
|
149
|
+
readonly approved: HireRequest | null;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Pay an x402 v2 endpoint through the SDK's `payWithX402`, gated by
|
|
153
|
+
* `checkMandate()`. Fail closed: a mandate that does not verify, a quote it
|
|
154
|
+
* denies, or a quote without opener terms is a hard stop with nothing signed.
|
|
155
|
+
*/
|
|
156
|
+
export declare function payX402WithMandate(input: PayX402WithMandateInput): Promise<PayX402WithMandateResult>;
|
|
157
|
+
//# sourceMapp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