@absol-labs/agent 0.10.0 → 0.12.0-rc.1
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 +106 -30
- package/dist/capability/invocation-capability.d.ts +20 -8
- package/dist/capability/invocation-capability.d.ts.map +1 -1
- package/dist/capability/invocation-capability.js.map +1 -1
- 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.map +1 -1
- package/dist/cli/commands/claim.js +21 -5
- 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 +51 -7
- 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 +3 -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 +121 -15
- package/dist/cli/commands/open.js.map +1 -1
- package/dist/cli/commands/pay.d.ts +19 -0
- package/dist/cli/commands/pay.d.ts.map +1 -0
- package/dist/cli/commands/pay.js +355 -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 +15 -6
- 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 +200 -21
- package/dist/cli/commands/reclaim.js.map +1 -1
- package/dist/cli/commands/refund.d.ts +13 -0
- package/dist/cli/commands/refund.d.ts.map +1 -0
- package/dist/cli/commands/refund.js +224 -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 +10 -1
- package/dist/cli/commands/status.js.map +1 -1
- package/dist/cli/commands/streams.js +9 -13
- 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 +2 -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/deps.d.ts +86 -7
- package/dist/cli/deps.d.ts.map +1 -1
- package/dist/cli/deps.js +102 -16
- 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 +83 -0
- package/dist/cli/privy.d.ts.map +1 -0
- package/dist/cli/privy.js +232 -0
- package/dist/cli/privy.js.map +1 -0
- 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 +40 -11
- 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/spend-policy.d.ts +60 -0
- package/dist/cli/spend-policy.d.ts.map +1 -0
- package/dist/cli/spend-policy.js +65 -0
- package/dist/cli/spend-policy.js.map +1 -0
- package/dist/cli/status.d.ts +3 -1
- package/dist/cli/status.d.ts.map +1 -1
- package/dist/cli/status.js +15 -3
- package/dist/cli/status.js.map +1 -1
- package/dist/cli/wallet.d.ts +109 -40
- package/dist/cli/wallet.d.ts.map +1 -1
- package/dist/cli/wallet.js +109 -44
- 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 +91 -4
- package/dist/gateway/caller-auth-gateway.d.ts.map +1 -1
- package/dist/gateway/caller-auth-gateway.js +184 -7
- package/dist/gateway/caller-auth-gateway.js.map +1 -1
- package/dist/gateway/http-server.d.ts +121 -1
- package/dist/gateway/http-server.d.ts.map +1 -1
- package/dist/gateway/http-server.js +474 -5
- 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 +215 -0
- package/dist/gateway/x402-opener-mode.d.ts.map +1 -0
- package/dist/gateway/x402-opener-mode.js +639 -0
- package/dist/gateway/x402-opener-mode.js.map +1 -0
- package/dist/index.d.ts +11 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -5
- package/dist/index.js.map +1 -1
- package/dist/mandates/env.d.ts +3 -2
- package/dist/mandates/env.d.ts.map +1 -1
- package/dist/mandates/env.js +3 -2
- package/dist/mandates/env.js.map +1 -1
- package/dist/mcp/server.d.ts +100 -0
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +719 -39
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp/service-invoker.d.ts +97 -0
- package/dist/mcp/service-invoker.d.ts.map +1 -0
- package/dist/mcp/service-invoker.js +167 -0
- package/dist/mcp/service-invoker.js.map +1 -0
- package/dist/mcp/x402-tools.d.ts +104 -0
- package/dist/mcp/x402-tools.d.ts.map +1 -0
- package/dist/mcp/x402-tools.js +434 -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/invoke.d.ts +23 -4
- package/dist/sdk/invoke.d.ts.map +1 -1
- package/dist/sdk/invoke.js +71 -10
- 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 +42 -17
- package/dist/x402/facilitator.d.ts.map +1 -1
- package/dist/x402/facilitator.js +49 -33
- 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 +135 -0
- package/dist/x402-buyer/pay.d.ts.map +1 -0
- package/dist/x402-buyer/pay.js +336 -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 +37 -0
- package/dist/x402-opener/facilitator-entry.js.map +1 -0
- package/dist/x402-opener/facilitator-server.d.ts +180 -0
- package/dist/x402-opener/facilitator-server.d.ts.map +1 -0
- package/dist/x402-opener/facilitator-server.js +696 -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 +240 -0
- package/dist/x402-opener/refund-relay.d.ts.map +1 -0
- package/dist/x402-opener/refund-relay.js +1240 -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 +217 -0
- package/dist/x402-opener/scheme.d.ts.map +1 -0
- package/dist/x402-opener/scheme.js +749 -0
- package/dist/x402-opener/scheme.js.map +1 -0
- package/dist/x402-opener/terms.d.ts +105 -0
- package/dist/x402-opener/terms.d.ts.map +1 -0
- package/dist/x402-opener/terms.js +212 -0
- package/dist/x402-opener/terms.js.map +1 -0
- package/dist/zktls/reclaim.d.ts +2 -2
- package/docs/agent-layer.md +13 -8
- package/docs/autonomous-privy-wallet.md +188 -0
- package/docs/cli.md +342 -53
- package/docs/langchain.md +5 -0
- package/docs/mcp-hosted.md +4 -3
- package/docs/mcp-reference.md +245 -0
- package/docs/probe-capability.md +191 -0
- package/docs/quickstart.md +54 -5
- package/docs/threat-model.md +24 -3
- package/docs/x402.md +681 -0
- package/package.json +19 -6
- package/src/capability/invocation-capability.ts +17 -1
- package/src/capability/probe-capability.ts +355 -0
- package/src/cli/args.ts +29 -0
- package/src/cli/commands/claim.ts +31 -5
- package/src/cli/commands/close.ts +56 -7
- package/src/cli/commands/invoke.ts +6 -2
- package/src/cli/commands/mcp.ts +18 -2
- package/src/cli/commands/open.ts +151 -14
- package/src/cli/commands/pay.ts +442 -0
- package/src/cli/commands/reclaim-unverified.ts +24 -6
- package/src/cli/commands/reclaim.ts +274 -19
- package/src/cli/commands/refund.ts +277 -0
- package/src/cli/commands/status.ts +17 -1
- package/src/cli/commands/streams.ts +11 -15
- package/src/cli/commands/watch.ts +7 -1
- package/src/cli/config.ts +139 -52
- package/src/cli/deps.ts +189 -25
- 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 +398 -0
- package/src/cli/reclaimability.ts +154 -0
- package/src/cli/run.ts +54 -11
- package/src/cli/scan-range.ts +2 -2
- package/src/cli/spend-policy.ts +139 -0
- package/src/cli/status.ts +14 -1
- package/src/cli/wallet.ts +189 -58
- 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 +336 -13
- package/src/gateway/http-server.ts +690 -7
- package/src/gateway/probe-auth.ts +191 -0
- package/src/gateway/signed-rate-bounds.ts +164 -0
- package/src/gateway/x402-opener-mode.ts +974 -0
- package/src/index.ts +112 -14
- package/src/mandates/env.ts +3 -2
- package/src/mcp/server.ts +973 -44
- package/src/mcp/service-invoker.ts +285 -0
- package/src/mcp/x402-tools.ts +621 -0
- package/src/network.ts +302 -0
- package/src/sdk/invoke.ts +96 -17
- 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 +85 -46
- package/src/x402-buyer/guarded-fetch.ts +213 -0
- package/src/x402-buyer/index.ts +9 -0
- package/src/x402-buyer/pay.ts +517 -0
- package/src/x402-opener/abi.ts +881 -0
- package/src/x402-opener/facilitator-entry.ts +45 -0
- package/src/x402-opener/facilitator-server.ts +1014 -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 +1743 -0
- package/src/x402-opener/relayer-queue.ts +18 -0
- package/src/x402-opener/scheme.ts +1133 -0
- package/src/x402-opener/terms.ts +300 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { getAddress, isAddress, isHex, type Address, type Hex } from "viem";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shared vocabulary of the x402 v2 escrow-opener path (metrik-agent#116).
|
|
5
|
+
*
|
|
6
|
+
* A standard x402 v2 `exact` payment (an EIP-3009 `transferWithAuthorization`)
|
|
7
|
+
* is settled through the ownerless `X402StreamOpenerFactory`
|
|
8
|
+
* (metrik-contracts#48): the x402 `payTo` is the CREATE2 address of the opener
|
|
9
|
+
* for one immutable set of stream {@link OpenerTerms}, so the payer's signature
|
|
10
|
+
* (which binds `to`) commits to those terms, and the opener turns the payment
|
|
11
|
+
* into an ordinary `StreamEscrowV2` stream with `deposit = amount`.
|
|
12
|
+
*
|
|
13
|
+
* This module has no network or package side effects: it is the pure,
|
|
14
|
+
* dependency-free part shared by the facilitator and the gateway.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Scheme name. The standard x402 `exact` scheme, nothing Metrik-specific. */
|
|
18
|
+
export const OPENER_SCHEME = "exact" as const;
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* Network, USDC, escrow, factory and oracle defaults are NOT constants here:
|
|
22
|
+
* they come from the `@absol-labs/sdk` network registry (see `../network.ts`),
|
|
23
|
+
* so a new deployment is a registry change, never an edit to this module.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** Public docs for the x402 opener path, linked from the 402. */
|
|
27
|
+
export const DEFAULT_OPENER_DOCS_URL =
|
|
28
|
+
"https://github.com/Absol-Labs/metrik-agent/blob/main/docs/x402.md";
|
|
29
|
+
|
|
30
|
+
/** Longest informational link accepted in `extra.metrikStreamOpener`. */
|
|
31
|
+
const MAX_LINK_LENGTH = 2048;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Key under `PaymentRequirements.extra` that carries the opener terms. The
|
|
35
|
+
* facilitator reads the terms ONLY from here, recomputes the opener address
|
|
36
|
+
* on-chain and requires it to equal `payTo`, so a tampered value can never
|
|
37
|
+
* redirect funds: it just fails verification.
|
|
38
|
+
*/
|
|
39
|
+
export const OPENER_EXTRA_KEY = "metrikStreamOpener" as const;
|
|
40
|
+
|
|
41
|
+
/** `uint64` ceiling for `maxDuration`. */
|
|
42
|
+
const MAX_UINT64 = (1n << 64n) - 1n;
|
|
43
|
+
|
|
44
|
+
/** Immutable per-listing stream terms, mirroring the Solidity `Terms` struct. */
|
|
45
|
+
export interface OpenerTerms {
|
|
46
|
+
readonly operator: Address;
|
|
47
|
+
readonly serviceRef: Hex;
|
|
48
|
+
/** USDC base units per second. */
|
|
49
|
+
readonly ratePerSecond: bigint;
|
|
50
|
+
/** Seconds. */
|
|
51
|
+
readonly maxDuration: bigint;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** JSON-safe projection of {@link OpenerTerms} plus the factory that prices them. */
|
|
55
|
+
export interface OpenerTermsExtra {
|
|
56
|
+
readonly factory: Address;
|
|
57
|
+
readonly operator: Address;
|
|
58
|
+
readonly serviceRef: Hex;
|
|
59
|
+
readonly ratePerSecond: string;
|
|
60
|
+
readonly maxDuration: string;
|
|
61
|
+
/**
|
|
62
|
+
* Informational only (metrik-agent#119): where the payer gets unearned money
|
|
63
|
+
* back without ETH, as a template containing `{streamId}`. Never used for
|
|
64
|
+
* settlement; the facilitator validates it is an http(s) link and ignores it.
|
|
65
|
+
*/
|
|
66
|
+
readonly refundUrl?: string;
|
|
67
|
+
/** Informational only: docs for this payment path. */
|
|
68
|
+
readonly docsUrl?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** True for an absolute http(s) URL of sane length. */
|
|
72
|
+
export function isHttpLink(value: unknown): value is string {
|
|
73
|
+
if (typeof value !== "string" || value.length > MAX_LINK_LENGTH) return false;
|
|
74
|
+
try {
|
|
75
|
+
const url = new URL(value);
|
|
76
|
+
return url.protocol === "https:" || url.protocol === "http:";
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class InvalidOpenerTermsError extends Error {
|
|
83
|
+
constructor(message: string) {
|
|
84
|
+
super(message);
|
|
85
|
+
this.name = "InvalidOpenerTermsError";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Parses a non-negative base-10 integer string. Throws on anything else. */
|
|
90
|
+
export function parseUintString(value: unknown, field: string): bigint {
|
|
91
|
+
if (typeof value !== "string" || !/^(0|[1-9]\d*)$/.test(value)) {
|
|
92
|
+
throw new InvalidOpenerTermsError(
|
|
93
|
+
`${field} must be a base-10 integer string without sign or leading zeros`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return BigInt(value);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Validates terms the way the factory's `deploy` does (non-zero operator, rate
|
|
101
|
+
* and duration, `maxDuration` within `uint64`) plus the shape checks the
|
|
102
|
+
* contract cannot do for us (32-byte serviceRef).
|
|
103
|
+
*/
|
|
104
|
+
export function assertValidTerms(terms: OpenerTerms): void {
|
|
105
|
+
if (!isAddress(terms.operator, { strict: false })) {
|
|
106
|
+
throw new InvalidOpenerTermsError("operator must be an EVM address");
|
|
107
|
+
}
|
|
108
|
+
if (/^0x0{40}$/i.test(terms.operator)) {
|
|
109
|
+
throw new InvalidOpenerTermsError("operator must not be the zero address");
|
|
110
|
+
}
|
|
111
|
+
if (
|
|
112
|
+
!isHex(terms.serviceRef, { strict: true }) ||
|
|
113
|
+
terms.serviceRef.length !== 66
|
|
114
|
+
) {
|
|
115
|
+
throw new InvalidOpenerTermsError("serviceRef must be a 32-byte hex value");
|
|
116
|
+
}
|
|
117
|
+
if (terms.ratePerSecond <= 0n) {
|
|
118
|
+
throw new InvalidOpenerTermsError("ratePerSecond must be > 0");
|
|
119
|
+
}
|
|
120
|
+
if (terms.maxDuration <= 0n || terms.maxDuration > MAX_UINT64) {
|
|
121
|
+
throw new InvalidOpenerTermsError(
|
|
122
|
+
"maxDuration must be > 0 and fit in uint64",
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The largest deposit a stream on these terms can ever earn out: `rate * maxDuration`. */
|
|
128
|
+
export function maxEarnable(terms: OpenerTerms): bigint {
|
|
129
|
+
return terms.ratePerSecond * terms.maxDuration;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The price (= stream deposit) must be positive and must not exceed what the
|
|
134
|
+
* stream can ever accrue. A deposit above `rate * maxDuration` would lock money
|
|
135
|
+
* the seller can never earn, reclaimable only by a second buyer action.
|
|
136
|
+
*/
|
|
137
|
+
export function assertValidPrice(price: bigint, terms: OpenerTerms): void {
|
|
138
|
+
if (price <= 0n) {
|
|
139
|
+
throw new InvalidOpenerTermsError("price must be > 0");
|
|
140
|
+
}
|
|
141
|
+
if (price > maxEarnable(terms)) {
|
|
142
|
+
throw new InvalidOpenerTermsError(
|
|
143
|
+
`price ${price} exceeds ratePerSecond * maxDuration (${maxEarnable(terms)})`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function termsToExtra(
|
|
149
|
+
factory: Address,
|
|
150
|
+
terms: OpenerTerms,
|
|
151
|
+
): OpenerTermsExtra {
|
|
152
|
+
return {
|
|
153
|
+
factory: getAddress(factory),
|
|
154
|
+
operator: getAddress(terms.operator),
|
|
155
|
+
serviceRef: terms.serviceRef.toLowerCase() as Hex,
|
|
156
|
+
ratePerSecond: terms.ratePerSecond.toString(),
|
|
157
|
+
maxDuration: terms.maxDuration.toString(),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Reads opener terms from an untrusted `PaymentRequirements.extra`. Every field
|
|
163
|
+
* is re-validated; unknown keys under the opener object are rejected so the
|
|
164
|
+
* wire shape cannot drift silently.
|
|
165
|
+
*/
|
|
166
|
+
export function termsFromExtra(extra: unknown): {
|
|
167
|
+
readonly factory: Address;
|
|
168
|
+
readonly terms: OpenerTerms;
|
|
169
|
+
} {
|
|
170
|
+
if (typeof extra !== "object" || extra === null) {
|
|
171
|
+
throw new InvalidOpenerTermsError("requirements.extra is missing");
|
|
172
|
+
}
|
|
173
|
+
const raw = (extra as Record<string, unknown>)[OPENER_EXTRA_KEY];
|
|
174
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
175
|
+
throw new InvalidOpenerTermsError(
|
|
176
|
+
`requirements.extra.${OPENER_EXTRA_KEY} is missing`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
const record = raw as Record<string, unknown>;
|
|
180
|
+
const allowed = new Set([
|
|
181
|
+
"factory",
|
|
182
|
+
"operator",
|
|
183
|
+
"serviceRef",
|
|
184
|
+
"ratePerSecond",
|
|
185
|
+
"maxDuration",
|
|
186
|
+
"refundUrl",
|
|
187
|
+
"docsUrl",
|
|
188
|
+
]);
|
|
189
|
+
for (const key of Object.keys(record)) {
|
|
190
|
+
if (!allowed.has(key)) {
|
|
191
|
+
throw new InvalidOpenerTermsError(
|
|
192
|
+
`unknown key ${key} in requirements.extra.${OPENER_EXTRA_KEY}`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
for (const link of ["refundUrl", "docsUrl"] as const) {
|
|
197
|
+
if (record[link] !== undefined && !isHttpLink(record[link])) {
|
|
198
|
+
throw new InvalidOpenerTermsError(
|
|
199
|
+
`${link} in requirements.extra.${OPENER_EXTRA_KEY} must be an http(s) URL`,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const { factory, operator, serviceRef } = record;
|
|
204
|
+
if (typeof factory !== "string" || !isAddress(factory, { strict: false })) {
|
|
205
|
+
throw new InvalidOpenerTermsError("factory must be an EVM address");
|
|
206
|
+
}
|
|
207
|
+
if (typeof operator !== "string" || !isAddress(operator, { strict: false })) {
|
|
208
|
+
throw new InvalidOpenerTermsError("operator must be an EVM address");
|
|
209
|
+
}
|
|
210
|
+
if (typeof serviceRef !== "string") {
|
|
211
|
+
throw new InvalidOpenerTermsError("serviceRef must be a string");
|
|
212
|
+
}
|
|
213
|
+
const terms: OpenerTerms = {
|
|
214
|
+
operator: getAddress(operator),
|
|
215
|
+
serviceRef: serviceRef as Hex,
|
|
216
|
+
ratePerSecond: parseUintString(record.ratePerSecond, "ratePerSecond"),
|
|
217
|
+
maxDuration: parseUintString(record.maxDuration, "maxDuration"),
|
|
218
|
+
};
|
|
219
|
+
assertValidTerms(terms);
|
|
220
|
+
return { factory: getAddress(factory), terms };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Chain id encoded in an `eip155:<id>` CAIP-2 identifier. Throws otherwise. */
|
|
224
|
+
export function chainIdFromNetwork(network: string): number {
|
|
225
|
+
const match = /^eip155:([1-9]\d*)$/.exec(network);
|
|
226
|
+
if (match === null) {
|
|
227
|
+
throw new InvalidOpenerTermsError(
|
|
228
|
+
`network must be an eip155 CAIP-2 identifier, got ${network}`,
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
const chainId = Number(match[1]);
|
|
232
|
+
if (!Number.isSafeInteger(chainId)) {
|
|
233
|
+
throw new InvalidOpenerTermsError(`unsupported chain id in ${network}`);
|
|
234
|
+
}
|
|
235
|
+
return chainId;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Solidity-tuple view of terms for viem contract calls. */
|
|
239
|
+
export function termsTuple(terms: OpenerTerms): {
|
|
240
|
+
readonly operator: Address;
|
|
241
|
+
readonly serviceRef: Hex;
|
|
242
|
+
readonly ratePerSecond: bigint;
|
|
243
|
+
readonly maxDuration: bigint;
|
|
244
|
+
} {
|
|
245
|
+
return {
|
|
246
|
+
operator: getAddress(terms.operator),
|
|
247
|
+
serviceRef: terms.serviceRef,
|
|
248
|
+
ratePerSecond: terms.ratePerSecond,
|
|
249
|
+
maxDuration: terms.maxDuration,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** A bazaar-shaped `{info: {input, output?}, schema?}` declaration. */
|
|
254
|
+
export interface X402SchemaDeclaration {
|
|
255
|
+
readonly info: {
|
|
256
|
+
readonly input: { readonly type: "http" } & Record<string, unknown>;
|
|
257
|
+
readonly output?: Record<string, unknown>;
|
|
258
|
+
};
|
|
259
|
+
readonly schema?: Record<string, unknown>;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Largest accepted schema declaration, serialized bytes. */
|
|
263
|
+
export const MAX_SCHEMA_DECLARATION_BYTES = 16 * 1024;
|
|
264
|
+
|
|
265
|
+
/** Validates a bazaar-shaped schema declaration. Throws on anything else. */
|
|
266
|
+
export function assertSchemaDeclaration(
|
|
267
|
+
value: unknown,
|
|
268
|
+
): asserts value is X402SchemaDeclaration {
|
|
269
|
+
if (JSON.stringify(value ?? null).length > MAX_SCHEMA_DECLARATION_BYTES) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
`the schema declaration exceeds ${MAX_SCHEMA_DECLARATION_BYTES} bytes`,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
const record = value as Record<string, unknown> | null;
|
|
275
|
+
const info = record?.info as Record<string, unknown> | undefined;
|
|
276
|
+
const input = info?.input as Record<string, unknown> | undefined;
|
|
277
|
+
if (
|
|
278
|
+
typeof record !== "object" ||
|
|
279
|
+
record === null ||
|
|
280
|
+
Array.isArray(record) ||
|
|
281
|
+
typeof info !== "object" ||
|
|
282
|
+
info === null ||
|
|
283
|
+
typeof input !== "object" ||
|
|
284
|
+
input === null ||
|
|
285
|
+
input.type !== "http" ||
|
|
286
|
+
(info.output !== undefined &&
|
|
287
|
+
(typeof info.output !== "object" || info.output === null)) ||
|
|
288
|
+
(record.schema !== undefined &&
|
|
289
|
+
(typeof record.schema !== "object" || record.schema === null))
|
|
290
|
+
) {
|
|
291
|
+
throw new Error(
|
|
292
|
+
'the schema declaration must be {info: {input: {type: "http", ...}, output?}, schema?}',
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
for (const key of Object.keys(record)) {
|
|
296
|
+
if (key !== "info" && key !== "schema") {
|
|
297
|
+
throw new Error(`unknown key ${key} in the schema declaration`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|