@absol-labs/agent 0.10.0 → 0.11.0

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.
Files changed (141) hide show
  1. package/README.md +107 -23
  2. package/dist/capability/invocation-capability.d.ts +18 -6
  3. package/dist/capability/invocation-capability.d.ts.map +1 -1
  4. package/dist/capability/invocation-capability.js.map +1 -1
  5. package/dist/cli/commands/claim.d.ts.map +1 -1
  6. package/dist/cli/commands/claim.js +20 -4
  7. package/dist/cli/commands/claim.js.map +1 -1
  8. package/dist/cli/commands/close.d.ts.map +1 -1
  9. package/dist/cli/commands/close.js +16 -4
  10. package/dist/cli/commands/close.js.map +1 -1
  11. package/dist/cli/commands/invoke.d.ts.map +1 -1
  12. package/dist/cli/commands/invoke.js +1 -0
  13. package/dist/cli/commands/invoke.js.map +1 -1
  14. package/dist/cli/commands/open.d.ts.map +1 -1
  15. package/dist/cli/commands/open.js +63 -11
  16. package/dist/cli/commands/open.js.map +1 -1
  17. package/dist/cli/commands/reclaim-unverified.d.ts.map +1 -1
  18. package/dist/cli/commands/reclaim-unverified.js +12 -4
  19. package/dist/cli/commands/reclaim-unverified.js.map +1 -1
  20. package/dist/cli/commands/reclaim.d.ts.map +1 -1
  21. package/dist/cli/commands/reclaim.js +16 -5
  22. package/dist/cli/commands/reclaim.js.map +1 -1
  23. package/dist/cli/commands/streams.js +1 -1
  24. package/dist/cli/commands/streams.js.map +1 -1
  25. package/dist/cli/deps.d.ts +53 -7
  26. package/dist/cli/deps.d.ts.map +1 -1
  27. package/dist/cli/deps.js +65 -15
  28. package/dist/cli/deps.js.map +1 -1
  29. package/dist/cli/privy.d.ts +83 -0
  30. package/dist/cli/privy.d.ts.map +1 -0
  31. package/dist/cli/privy.js +230 -0
  32. package/dist/cli/privy.js.map +1 -0
  33. package/dist/cli/spend-policy.d.ts +60 -0
  34. package/dist/cli/spend-policy.d.ts.map +1 -0
  35. package/dist/cli/spend-policy.js +65 -0
  36. package/dist/cli/spend-policy.js.map +1 -0
  37. package/dist/cli/wallet.d.ts +108 -39
  38. package/dist/cli/wallet.d.ts.map +1 -1
  39. package/dist/cli/wallet.js +108 -43
  40. package/dist/cli/wallet.js.map +1 -1
  41. package/dist/gateway/caller-auth-gateway.d.ts +38 -3
  42. package/dist/gateway/caller-auth-gateway.d.ts.map +1 -1
  43. package/dist/gateway/caller-auth-gateway.js +25 -1
  44. package/dist/gateway/caller-auth-gateway.js.map +1 -1
  45. package/dist/gateway/http-server.d.ts +53 -0
  46. package/dist/gateway/http-server.d.ts.map +1 -1
  47. package/dist/gateway/http-server.js +189 -4
  48. package/dist/gateway/http-server.js.map +1 -1
  49. package/dist/gateway/x402-mode.d.ts +123 -0
  50. package/dist/gateway/x402-mode.d.ts.map +1 -0
  51. package/dist/gateway/x402-mode.js +212 -0
  52. package/dist/gateway/x402-mode.js.map +1 -0
  53. package/dist/index.d.ts +6 -4
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +8 -3
  56. package/dist/index.js.map +1 -1
  57. package/dist/mandates/env.d.ts +3 -2
  58. package/dist/mandates/env.d.ts.map +1 -1
  59. package/dist/mandates/env.js +3 -2
  60. package/dist/mandates/env.js.map +1 -1
  61. package/dist/mcp/server.d.ts +49 -0
  62. package/dist/mcp/server.d.ts.map +1 -1
  63. package/dist/mcp/server.js +513 -24
  64. package/dist/mcp/server.js.map +1 -1
  65. package/dist/mcp/service-invoker.d.ts +97 -0
  66. package/dist/mcp/service-invoker.d.ts.map +1 -0
  67. package/dist/mcp/service-invoker.js +167 -0
  68. package/dist/mcp/service-invoker.js.map +1 -0
  69. package/dist/sdk/invoke.d.ts +19 -4
  70. package/dist/sdk/invoke.d.ts.map +1 -1
  71. package/dist/sdk/invoke.js +69 -5
  72. package/dist/sdk/invoke.js.map +1 -1
  73. package/dist/x402/delivery-oracle.d.ts +108 -0
  74. package/dist/x402/delivery-oracle.d.ts.map +1 -0
  75. package/dist/x402/delivery-oracle.js +174 -0
  76. package/dist/x402/delivery-oracle.js.map +1 -0
  77. package/dist/x402/facilitator-server-entry.d.ts +3 -0
  78. package/dist/x402/facilitator-server-entry.d.ts.map +1 -0
  79. package/dist/x402/facilitator-server-entry.js +27 -0
  80. package/dist/x402/facilitator-server-entry.js.map +1 -0
  81. package/dist/x402/facilitator-server.d.ts +73 -0
  82. package/dist/x402/facilitator-server.d.ts.map +1 -0
  83. package/dist/x402/facilitator-server.js +305 -0
  84. package/dist/x402/facilitator-server.js.map +1 -0
  85. package/dist/x402/facilitator.d.ts +69 -17
  86. package/dist/x402/facilitator.d.ts.map +1 -1
  87. package/dist/x402/facilitator.js +76 -33
  88. package/dist/x402/facilitator.js.map +1 -1
  89. package/dist/x402/payment-requirements.d.ts +106 -0
  90. package/dist/x402/payment-requirements.d.ts.map +1 -0
  91. package/dist/x402/payment-requirements.js +138 -0
  92. package/dist/x402/payment-requirements.js.map +1 -0
  93. package/dist/x402/relayer.d.ts +41 -0
  94. package/dist/x402/relayer.d.ts.map +1 -0
  95. package/dist/x402/relayer.js +64 -0
  96. package/dist/x402/relayer.js.map +1 -0
  97. package/dist/x402/resource-index.d.ts +75 -0
  98. package/dist/x402/resource-index.d.ts.map +1 -0
  99. package/dist/x402/resource-index.js +126 -0
  100. package/dist/x402/resource-index.js.map +1 -0
  101. package/dist/x402/verified-delivery-facilitator.d.ts +285 -0
  102. package/dist/x402/verified-delivery-facilitator.d.ts.map +1 -0
  103. package/dist/x402/verified-delivery-facilitator.js +423 -0
  104. package/dist/x402/verified-delivery-facilitator.js.map +1 -0
  105. package/dist/zktls/t2-delivery-proof.d.ts +6 -6
  106. package/docs/agent-layer.md +12 -8
  107. package/docs/cli.md +158 -22
  108. package/docs/mcp-hosted.md +4 -3
  109. package/docs/mcp-reference.md +175 -0
  110. package/docs/quickstart.md +18 -4
  111. package/docs/threat-model.md +4 -3
  112. package/docs/x402.md +648 -0
  113. package/package.json +18 -7
  114. package/src/capability/invocation-capability.ts +17 -1
  115. package/src/cli/commands/claim.ts +30 -4
  116. package/src/cli/commands/close.ts +20 -4
  117. package/src/cli/commands/invoke.ts +1 -0
  118. package/src/cli/commands/open.ts +76 -10
  119. package/src/cli/commands/reclaim-unverified.ts +16 -4
  120. package/src/cli/commands/reclaim.ts +22 -5
  121. package/src/cli/commands/streams.ts +1 -1
  122. package/src/cli/deps.ts +115 -24
  123. package/src/cli/privy.ts +396 -0
  124. package/src/cli/spend-policy.ts +139 -0
  125. package/src/cli/wallet.ts +188 -57
  126. package/src/gateway/caller-auth-gateway.ts +77 -6
  127. package/src/gateway/http-server.ts +280 -6
  128. package/src/gateway/x402-mode.ts +350 -0
  129. package/src/index.ts +40 -5
  130. package/src/mandates/env.ts +3 -2
  131. package/src/mcp/server.ts +681 -29
  132. package/src/mcp/service-invoker.ts +285 -0
  133. package/src/sdk/invoke.ts +90 -12
  134. package/src/x402/delivery-oracle.ts +285 -0
  135. package/src/x402/facilitator-server-entry.ts +35 -0
  136. package/src/x402/facilitator-server.ts +445 -0
  137. package/src/x402/facilitator.ts +125 -46
  138. package/src/x402/payment-requirements.ts +231 -0
  139. package/src/x402/relayer.ts +122 -0
  140. package/src/x402/resource-index.ts +228 -0
  141. package/src/x402/verified-delivery-facilitator.ts +744 -0
package/src/cli/deps.ts CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  type StreamV2,
9
9
  type StreamProofTransactionResult,
10
10
  } from "@absol-labs/sdk";
11
- import { erc20Abi, getAbiItem, http, type LocalAccount } from "viem";
11
+ import { erc20Abi, getAbiItem, http, type Transport } from "viem";
12
12
 
13
13
  import {
14
14
  discoverServicesDetailed,
@@ -30,8 +30,10 @@ import {
30
30
  type ReclaimAuthorizedStreamInput,
31
31
  type ReclaimVerifiedStreamResult,
32
32
  } from "../sdk/client.js";
33
+ import type { SignedSpendMandate } from "../mandates/mandate.js";
33
34
 
34
35
  import type { CliConfig } from "./config.js";
36
+ import type { CliSignerAccount } from "./wallet.js";
35
37
  import { RPC_RETRY_COUNT, RPC_TIMEOUT_MS, withReadTimeout } from "./timeout.js";
36
38
 
37
39
  /**
@@ -149,17 +151,39 @@ export interface OpenedStream {
149
151
  readonly blockNumber: bigint;
150
152
  }
151
153
 
154
+ /**
155
+ * A fund-moving call, with the mandate made EXPLICITLY optional.
156
+ *
157
+ * `null` is not "no authorization" — it is "this wallet's ceiling is enforced
158
+ * by a broker policy the client cannot reach or raise, so a locally self-signed
159
+ * mandate would be the agent attesting to its own limits on top of a stronger
160
+ * limit". The CLI substitutes the real ceiling instead; see
161
+ * `./spend-policy.ts`, which also explains why re-adding the mandate here would
162
+ * be a regression rather than a tidy-up.
163
+ *
164
+ * A key-holding wallet (private key, CDP) always passes a real mandate, and its
165
+ * path through `VerifiedStreamAgentClient` is unchanged.
166
+ */
167
+ type MandateOptional<T extends { readonly signedMandate: SignedSpendMandate }> =
168
+ Omit<T, "signedMandate"> & {
169
+ readonly signedMandate: SignedSpendMandate | null;
170
+ };
171
+
172
+ export type CliOpenStreamInput = MandateOptional<OpenVerifiedStreamInput>;
173
+ export type CliStreamActionInput =
174
+ MandateOptional<MandateAuthorizedStreamActionInput>;
175
+ export type CliReclaimStreamInput =
176
+ MandateOptional<ReclaimAuthorizedStreamInput>;
177
+
152
178
  export interface CliWriteClient {
153
179
  /** Build + best-effort simulate the hire WITHOUT broadcasting (`--dry-run`). */
154
180
  buildHireTx(input: HireComputeInput): Promise<HireTxPreview>;
155
- openVerifiedStream(
156
- input: OpenVerifiedStreamInput,
157
- ): Promise<HireComputeResult>;
181
+ openVerifiedStream(input: CliOpenStreamInput): Promise<HireComputeResult>;
158
182
  closeStream(
159
- input: MandateAuthorizedStreamActionInput,
183
+ input: CliStreamActionInput,
160
184
  ): Promise<StreamProofTransactionResult>;
161
185
  reclaimStream(
162
- input: ReclaimAuthorizedStreamInput,
186
+ input: CliReclaimStreamInput,
163
187
  ): Promise<ReclaimVerifiedStreamResult>;
164
188
  /**
165
189
  * The no-proof escape hatch. Separate from {@link reclaimStream} because it is
@@ -167,14 +191,27 @@ export interface CliWriteClient {
167
191
  * — see `VerifiedStreamAgentClient.reclaimUnverifiedStream`.
168
192
  */
169
193
  reclaimUnverifiedStream(
170
- input: MandateAuthorizedStreamActionInput,
194
+ input: CliStreamActionInput,
171
195
  ): Promise<StreamProofTransactionResult>;
172
196
  /** Operator-only: redeem the verified-earned balance for a stream. */
173
197
  claimStream(
174
- input: MandateAuthorizedStreamActionInput,
198
+ input: CliStreamActionInput,
175
199
  ): Promise<StreamProofTransactionResult>;
176
200
  }
177
201
 
202
+ /**
203
+ * Per-wallet write-path overrides.
204
+ *
205
+ * `transport` exists for exactly one reason: a Privy wallet has no local
206
+ * `signTransaction`. Its EIP-1193 provider performs `eth_sendTransaction`
207
+ * itself, so the provider must BE the wallet client's transport rather than the
208
+ * plain `http(--rpc-url)` every local signer uses. Left unset — which is every
209
+ * private-key and CDP run — the transport is unchanged, byte for byte.
210
+ */
211
+ export interface CreateWriteClientOptions {
212
+ readonly transport?: Transport;
213
+ }
214
+
178
215
  export interface CliDependencies {
179
216
  readonly createReadClient: (config: CliConfig) => CliReadClient;
180
217
  /**
@@ -185,7 +222,8 @@ export interface CliDependencies {
185
222
  */
186
223
  readonly createWriteClient: (
187
224
  config: CliConfig,
188
- account: LocalAccount | null,
225
+ account: CliSignerAccount | null,
226
+ options?: CreateWriteClientOptions,
189
227
  ) => CliWriteClient;
190
228
  readonly discover: (
191
229
  options: DiscoverServicesOptions,
@@ -228,11 +266,12 @@ function rpcTransport(config: CliConfig) {
228
266
  /** Shared `MetrikClient` construction: one escrow, declared as V2 on both keys. */
229
267
  function createMetrikClient(
230
268
  config: CliConfig,
231
- account?: LocalAccount,
269
+ account?: CliSignerAccount,
270
+ transport?: Transport,
232
271
  ): MetrikClient {
233
272
  return new MetrikClient({
234
273
  chain: config.chain,
235
- transport: rpcTransport(config),
274
+ transport: transport ?? rpcTransport(config),
236
275
  escrow: config.escrow,
237
276
  // Declared on BOTH keys: without `escrowV2` the SDK routes reads and
238
277
  // settlement through the legacy V1 ABI, which reverts against
@@ -365,10 +404,13 @@ export const defaultCliDependencies: CliDependencies = {
365
404
  },
366
405
  };
367
406
  },
368
- createWriteClient(config, account) {
407
+ createWriteClient(config, account, options) {
408
+ // A Privy wallet's provider IS the transport (see CreateWriteClientOptions);
409
+ // every other wallet keeps the plain bounded HTTP transport.
410
+ const transport = options?.transport ?? rpcTransport(config);
369
411
  const clientConfig = {
370
412
  chain: config.chain,
371
- transport: rpcTransport(config),
413
+ transport,
372
414
  escrow: config.escrow,
373
415
  escrowV2: config.escrow,
374
416
  usdc: config.usdc,
@@ -382,26 +424,61 @@ export const defaultCliDependencies: CliDependencies = {
382
424
  const agentClient = new VerifiedStreamAgentClient(clientConfig);
383
425
  const metrik = createMetrikClient(
384
426
  config,
385
- ...(account === null ? [] : ([account] as const)),
427
+ account === null ? undefined : account,
428
+ transport,
386
429
  );
387
430
  return {
388
431
  async buildHireTx(input) {
389
432
  return await metrik.buildHireTx(input);
390
433
  },
391
- async openVerifiedStream(input) {
392
- return await agentClient.openVerifiedStream(input);
434
+ async openVerifiedStream({ signedMandate, ...input }) {
435
+ // No mandate means a policy-enforced wallet: the ceiling was already
436
+ // checked against the broker's own `/v1/config` before we got here, and
437
+ // the broker checks it AGAIN server-side on the transaction it signs.
438
+ // Going straight at the SDK client is the same path the SDK-driven
439
+ // (non-CLI) flow has always used.
440
+ if (signedMandate === null) {
441
+ return await metrik.hireVerifiedService({
442
+ operator: input.operator,
443
+ serviceRef: input.serviceRef,
444
+ budgetUsdc: input.budgetUsdc,
445
+ ratePerSecond: input.ratePerSecondUsdc,
446
+ maxDurationSeconds: input.maxDurationSeconds,
447
+ });
448
+ }
449
+ return await agentClient.openVerifiedStream({
450
+ ...input,
451
+ signedMandate,
452
+ });
393
453
  },
394
- async closeStream(input) {
395
- return await agentClient.closeStream(input);
454
+ async closeStream({ signedMandate, ...input }) {
455
+ if (signedMandate === null) return await metrik.close(input.streamId);
456
+ return await agentClient.closeStream({ ...input, signedMandate });
396
457
  },
397
- async reclaimStream(input) {
398
- return await agentClient.reclaimStream(input);
458
+ async reclaimStream({ signedMandate, ...input }) {
459
+ if (signedMandate === null) {
460
+ const closeResult = input.closeFirst
461
+ ? await metrik.close(input.streamId)
462
+ : null;
463
+ return {
464
+ closeResult,
465
+ reclaimResult: await metrik.reclaim(input.streamId),
466
+ };
467
+ }
468
+ return await agentClient.reclaimStream({ ...input, signedMandate });
399
469
  },
400
- async reclaimUnverifiedStream(input) {
401
- return await agentClient.reclaimUnverifiedStream(input);
470
+ async reclaimUnverifiedStream({ signedMandate, ...input }) {
471
+ if (signedMandate === null) {
472
+ return await metrik.reclaimUnverified(input.streamId);
473
+ }
474
+ return await agentClient.reclaimUnverifiedStream({
475
+ ...input,
476
+ signedMandate,
477
+ });
402
478
  },
403
- async claimStream(input) {
404
- return await agentClient.claimStream(input);
479
+ async claimStream({ signedMandate, ...input }) {
480
+ if (signedMandate === null) return await metrik.claim(input.streamId);
481
+ return await agentClient.claimStream({ ...input, signedMandate });
405
482
  },
406
483
  };
407
484
  },
@@ -434,4 +511,18 @@ export const defaultCliDependencies: CliDependencies = {
434
511
  },
435
512
  };
436
513
 
514
+ /**
515
+ * The write-client options a resolved wallet implies.
516
+ *
517
+ * Kept next to {@link CreateWriteClientOptions} so every fund-moving command
518
+ * spells the transport decision the same way, and so a wallet that needs a
519
+ * provider transport cannot be broadcast through a plain RPC by a command that
520
+ * simply forgot to pass it.
521
+ */
522
+ export function writeClientOptions(
523
+ wallet: { readonly transport?: Transport } | null,
524
+ ): CreateWriteClientOptions {
525
+ return wallet?.transport === undefined ? {} : { transport: wallet.transport };
526
+ }
527
+
437
528
  export type { DiscoverResult, ServiceListing };
@@ -0,0 +1,396 @@
1
+ import {
2
+ custom,
3
+ isAddress,
4
+ getAddress,
5
+ type Account,
6
+ type Transport,
7
+ } from "viem";
8
+
9
+ import {
10
+ AutonomousWalletError,
11
+ EncryptedFileCredentialStore,
12
+ HttpAutonomousWalletBroker,
13
+ METRIK_AUTONOMOUS_WALLET_BROKER_URL,
14
+ METRIK_WALLET_ENCRYPTION_KEY_ENV,
15
+ createAutonomousWalletProvider,
16
+ createDefaultAutonomousWalletStore,
17
+ type AutonomousWalletCredentialStore,
18
+ type AutonomousWalletRecord,
19
+ } from "../wallet/autonomous-wallet.js";
20
+ import { createPrivySessionProvider } from "../wallet/privy-session-provider.js";
21
+ import {
22
+ createPrivyEmbeddedAccount,
23
+ type PrivyEip1193Provider,
24
+ } from "../wallet/provider.js";
25
+ import { configError } from "./errors.js";
26
+
27
+ /**
28
+ * The CLI's two Privy wallet shapes.
29
+ *
30
+ * Privy differs from the private-key and CDP paths in ONE structural way: the
31
+ * account it yields has no local `signTransaction`. It signs and broadcasts by
32
+ * sending `eth_sendTransaction` / `eth_signTypedData_v4` to a provider, so the
33
+ * viem wallet client's TRANSPORT must be that provider rather than a plain HTTP
34
+ * RPC. That is the whole difference, and it is what `transport` below carries.
35
+ *
36
+ * Neither shape puts a wallet key on this machine. The signing key lives at
37
+ * Privy; the broker holds the Privy authorization key. What the CLI holds is
38
+ * either a short-lived session token (`session`) or a P-256 authorization key
39
+ * in the host credential store (`autonomous`) — a key that authorizes a
40
+ * policy-bounded request, not a key that can move funds by itself.
41
+ */
42
+ export type PrivyCliWalletSource = "privy-autonomous" | "privy-session";
43
+
44
+ /** The store key naming the agent-owned wallet in the host credential store. */
45
+ export const PRIVY_WALLET_KEY_ENV = "METRIK_AGENT_PRIVY_WALLET_KEY";
46
+ /** Broker base URL. Optional for `autonomous`, REQUIRED for `session`. */
47
+ export const PRIVY_BROKER_URL_ENV = "METRIK_AGENT_PRIVY_BROKER_URL";
48
+ /** A scoped session token minted by the broker's `POST /v1/sessions`. */
49
+ export const PRIVY_SESSION_TOKEN_ENV = "METRIK_AGENT_PRIVY_SESSION_TOKEN";
50
+ /** The embedded wallet address the session is bound to. */
51
+ export const PRIVY_ADDRESS_ENV = "METRIK_AGENT_PRIVY_ADDRESS";
52
+ /** The PUBLIC Privy app id. Not a secret — the broker serves it at `/v1/config`. */
53
+ export const PRIVY_APP_ID_ENV = "METRIK_AGENT_PRIVY_APP_ID";
54
+
55
+ /** Every variable either Privy shape reads. Named in errors, never printed. */
56
+ export const PRIVY_ENV_VARS = [
57
+ PRIVY_WALLET_KEY_ENV,
58
+ PRIVY_BROKER_URL_ENV,
59
+ PRIVY_SESSION_TOKEN_ENV,
60
+ PRIVY_ADDRESS_ENV,
61
+ PRIVY_APP_ID_ENV,
62
+ ] as const;
63
+
64
+ /** The variables the user-owned session shape needs, all of them. */
65
+ export const PRIVY_SESSION_ENV_VARS = [
66
+ PRIVY_BROKER_URL_ENV,
67
+ PRIVY_SESSION_TOKEN_ENV,
68
+ PRIVY_ADDRESS_ENV,
69
+ PRIVY_APP_ID_ENV,
70
+ ] as const;
71
+
72
+ /**
73
+ * The spend ceiling a broker enforces server-side, read from `GET /v1/config`.
74
+ *
75
+ * These are the SAME numbers the broker checks `openStream` against
76
+ * (`deposit <= maxTotalUsdc`, `ratePerSecond <= maxRatePerSecondUsdc`,
77
+ * `duration <= maxDurationSeconds`), which is why the CLI can use them to
78
+ * refuse an over-ceiling hire locally instead of paying gas to learn a knowable
79
+ * fact. It reads them rather than hardcoding them: the ceiling belongs to the
80
+ * broker, and a copy in this file would be a second source of truth that goes
81
+ * stale silently.
82
+ */
83
+ export interface PrivySpendPolicy {
84
+ readonly brokerUrl: string;
85
+ readonly chainId: number;
86
+ readonly maxTotalUsdc: bigint;
87
+ readonly maxRatePerSecondUsdc: bigint;
88
+ readonly maxDurationSeconds: number;
89
+ }
90
+
91
+ /** Reads a broker's public policy. No credential is sent; nothing secret is served. */
92
+ export async function fetchPrivySpendPolicy(
93
+ brokerUrl: string,
94
+ fetchImpl: typeof fetch = fetch,
95
+ ): Promise<PrivySpendPolicy> {
96
+ const base = brokerUrl.replace(/\/$/, "");
97
+ const response = await fetchImpl(`${base}/v1/config`, {
98
+ headers: { accept: "application/json" },
99
+ }).catch((error: unknown) => {
100
+ throw configError(
101
+ `the Privy broker at ${base} did not answer its public policy`,
102
+ `${describe(error)} The CLI reads GET /v1/config to know the ceiling your wallet is capped at; without it, it cannot tell you before broadcasting whether a hire fits.`,
103
+ );
104
+ });
105
+ const body = (await response.json().catch(() => null)) as Record<
106
+ string,
107
+ unknown
108
+ > | null;
109
+ if (!response.ok || body === null) {
110
+ throw configError(
111
+ `the Privy broker at ${base} rejected the public policy read (${response.status})`,
112
+ );
113
+ }
114
+ const chainId = Number(body.chainId);
115
+ const maxTotalUsdc = asBigint(body.maxTotalUsdcAtomic);
116
+ const maxRatePerSecondUsdc = asBigint(body.maxRatePerSecondAtomic);
117
+ const maxDurationSeconds = Number(body.maxDurationSeconds);
118
+ if (
119
+ !Number.isSafeInteger(chainId) ||
120
+ maxTotalUsdc === null ||
121
+ maxRatePerSecondUsdc === null ||
122
+ !Number.isSafeInteger(maxDurationSeconds)
123
+ ) {
124
+ throw configError(
125
+ `the Privy broker at ${base} returned a policy this CLI cannot read`,
126
+ "expected chainId, maxTotalUsdcAtomic, maxRatePerSecondAtomic and maxDurationSeconds.",
127
+ );
128
+ }
129
+ return {
130
+ brokerUrl: base,
131
+ chainId,
132
+ maxTotalUsdc,
133
+ maxRatePerSecondUsdc,
134
+ maxDurationSeconds,
135
+ };
136
+ }
137
+
138
+ function asBigint(value: unknown): bigint | null {
139
+ if (typeof value === "bigint") return value;
140
+ if (typeof value === "number" && Number.isSafeInteger(value))
141
+ return BigInt(value);
142
+ if (typeof value === "string" && /^[0-9]+$/.test(value)) return BigInt(value);
143
+ return null;
144
+ }
145
+
146
+ export interface ResolvedPrivyCliWallet {
147
+ readonly source: PrivyCliWalletSource;
148
+ /** A JSON-RPC account: `signTypedData` works, `signTransaction` does not exist. */
149
+ readonly account: Account;
150
+ /** The provider-as-transport. Without this every write goes to a plain RPC and fails. */
151
+ readonly transport: Transport;
152
+ /** One line naming the wallet's origin. Safe to print: no credential in it. */
153
+ readonly description: string;
154
+ /** Reads the broker ceiling this wallet is capped at. Bound to its broker URL. */
155
+ readonly readSpendPolicy: () => Promise<PrivySpendPolicy>;
156
+ }
157
+
158
+ export interface ResolvePrivyCliWalletOptions {
159
+ /** `--rpc-url`, so a Privy wallet's READS use the same endpoint as everything else. */
160
+ readonly rpcUrl?: string;
161
+ readonly note?: (line: string) => void;
162
+ /** Test seam. Defaults to the real credential store selected from `env`. */
163
+ readonly store?: AutonomousWalletCredentialStore;
164
+ /** Test seam for the broker HTTP contract. */
165
+ readonly fetchImpl?: typeof fetch;
166
+ }
167
+
168
+ /**
169
+ * Resolves a Privy wallet from the environment, or `null` when no Privy
170
+ * variable is set at all.
171
+ *
172
+ * A PARTIALLY configured Privy setup is an error, not a `null` — "you set the
173
+ * session token but not the address" and "you configured no Privy wallet" are
174
+ * different facts, and rendering the first as the second sends the reader
175
+ * looking in the wrong place. This mirrors the CDP path exactly.
176
+ */
177
+ export async function resolvePrivyCliWallet(
178
+ env: NodeJS.ProcessEnv,
179
+ options: ResolvePrivyCliWalletOptions = {},
180
+ ): Promise<ResolvedPrivyCliWallet | null> {
181
+ const walletKey = read(env, PRIVY_WALLET_KEY_ENV);
182
+ const brokerUrl = read(env, PRIVY_BROKER_URL_ENV);
183
+ const sessionToken = read(env, PRIVY_SESSION_TOKEN_ENV);
184
+ const address = read(env, PRIVY_ADDRESS_ENV);
185
+ const appId = read(env, PRIVY_APP_ID_ENV);
186
+
187
+ if (
188
+ walletKey === null &&
189
+ brokerUrl === null &&
190
+ sessionToken === null &&
191
+ address === null &&
192
+ appId === null
193
+ ) {
194
+ return null;
195
+ }
196
+
197
+ if (walletKey !== null) {
198
+ if (sessionToken !== null) {
199
+ options.note?.(
200
+ `warning: both ${PRIVY_WALLET_KEY_ENV} and ${PRIVY_SESSION_TOKEN_ENV} are set; using the agent-owned wallet named by ${PRIVY_WALLET_KEY_ENV}. Unset it to sign with the session token.`,
201
+ );
202
+ }
203
+ return await resolveAutonomous(walletKey, brokerUrl, env, options);
204
+ }
205
+
206
+ const missing = PRIVY_SESSION_ENV_VARS.filter(
207
+ (name) => read(env, name) === null,
208
+ );
209
+ if (missing.length > 0) {
210
+ throw configError(
211
+ `the Privy wallet is partially configured: ${missing.join(", ")} ${missing.length === 1 ? "is" : "are"} not set`,
212
+ `a Privy wallet is configured one of two ways: set ${PRIVY_WALLET_KEY_ENV} to the store key of an agent-owned wallet you provisioned with provisionMetrikAutonomousWallet(), or set all of ${PRIVY_SESSION_ENV_VARS.join(", ")} for a broker session. Set the missing one${missing.length === 1 ? "" : "s"}, or unset the others to use a different wallet.`,
213
+ );
214
+ }
215
+
216
+ return resolveSession({
217
+ brokerUrl: brokerUrl as string,
218
+ sessionToken: sessionToken as string,
219
+ address: address as string,
220
+ appId: appId as string,
221
+ ...(options.rpcUrl === undefined ? {} : { rpcUrl: options.rpcUrl }),
222
+ ...(options.fetchImpl === undefined
223
+ ? {}
224
+ : { fetchImpl: options.fetchImpl }),
225
+ });
226
+ }
227
+
228
+ async function resolveAutonomous(
229
+ walletKey: string,
230
+ brokerUrlOverride: string | null,
231
+ env: NodeJS.ProcessEnv,
232
+ options: ResolvePrivyCliWalletOptions,
233
+ ): Promise<ResolvedPrivyCliWallet> {
234
+ const store = options.store ?? selectCredentialStore(env);
235
+ let record: AutonomousWalletRecord | null;
236
+ try {
237
+ record = await store.load(walletKey);
238
+ } catch (error) {
239
+ throw configError(
240
+ `could not read the agent-owned Privy wallet "${walletKey}" from the credential store`,
241
+ `${describe(error)} On a headless host the OS credential store is often unavailable; set ${METRIK_WALLET_ENCRYPTION_KEY_ENV} to use the encrypted-file store instead.`,
242
+ );
243
+ }
244
+ if (record === null) {
245
+ throw configError(
246
+ `no agent-owned Privy wallet is stored under the key "${walletKey}"`,
247
+ `the CLI never provisions a wallet on your behalf — a fund-moving command is the wrong place to mint one. Provision it once with provisionMetrikAutonomousWallet({ storeKey: "${walletKey}" }) (see docs/autonomous-privy-wallet.md), then re-run this command.`,
248
+ );
249
+ }
250
+ if (record.walletId === undefined || record.address === undefined) {
251
+ throw configError(
252
+ `the agent-owned Privy wallet "${walletKey}" was never finished provisioning`,
253
+ "the local key exists but the broker never returned a wallet id and address. Re-run provisionMetrikAutonomousWallet() with the same store key — it retries with the same key and idempotency nonce.",
254
+ );
255
+ }
256
+
257
+ const brokerUrl = brokerUrlOverride ?? record.brokerUrl;
258
+ if (brokerUrlOverride !== null && brokerUrlOverride !== record.brokerUrl) {
259
+ options.note?.(
260
+ `note: ${PRIVY_BROKER_URL_ENV} (${brokerUrlOverride}) overrides the broker this wallet was provisioned against (${record.brokerUrl}); only a broker holding this wallet's Privy authorization key can authorize it.`,
261
+ );
262
+ }
263
+
264
+ let broker: HttpAutonomousWalletBroker;
265
+ try {
266
+ broker = new HttpAutonomousWalletBroker(brokerUrl, {
267
+ ...(options.fetchImpl === undefined
268
+ ? {}
269
+ : { fetchImpl: options.fetchImpl }),
270
+ });
271
+ } catch (error) {
272
+ throw configError(
273
+ `the autonomous wallet broker URL is not usable: ${describe(error)}`,
274
+ `set ${PRIVY_BROKER_URL_ENV} to an HTTPS broker you control (the default is ${METRIK_AUTONOMOUS_WALLET_BROKER_URL}).`,
275
+ );
276
+ }
277
+
278
+ const provider = createAutonomousWalletProvider({
279
+ wallet: record,
280
+ broker,
281
+ ...(options.rpcUrl === undefined ? {} : { rpcUrl: options.rpcUrl }),
282
+ ...(options.fetchImpl === undefined
283
+ ? {}
284
+ : { fetchImpl: options.fetchImpl }),
285
+ });
286
+ const account = await embeddedAccount(provider, record.appId, record.address);
287
+
288
+ return {
289
+ source: "privy-autonomous",
290
+ account,
291
+ transport: custom(provider),
292
+ description: `${record.address} (agent-owned Privy wallet "${walletKey}" via ${brokerUrl})`,
293
+ readSpendPolicy: async () =>
294
+ await fetchPrivySpendPolicy(
295
+ brokerUrl,
296
+ options.fetchImpl ?? globalThis.fetch.bind(globalThis),
297
+ ),
298
+ };
299
+ }
300
+
301
+ function resolveSession(input: {
302
+ readonly brokerUrl: string;
303
+ readonly sessionToken: string;
304
+ readonly address: string;
305
+ readonly appId: string;
306
+ readonly rpcUrl?: string;
307
+ readonly fetchImpl?: typeof fetch;
308
+ }): Promise<ResolvedPrivyCliWallet> {
309
+ if (!isAddress(input.address)) {
310
+ throw configError(
311
+ `${PRIVY_ADDRESS_ENV} must be a 0x-prefixed 20-byte address`,
312
+ );
313
+ }
314
+ const address = getAddress(input.address);
315
+ let provider: PrivyEip1193Provider;
316
+ try {
317
+ provider = createPrivySessionProvider({
318
+ brokerUrl: input.brokerUrl,
319
+ sessionToken: input.sessionToken,
320
+ address,
321
+ ...(input.rpcUrl === undefined ? {} : { rpcUrl: input.rpcUrl }),
322
+ ...(input.fetchImpl === undefined ? {} : { fetchImpl: input.fetchImpl }),
323
+ });
324
+ } catch (error) {
325
+ throw configError(
326
+ `the Privy session broker URL is not usable: ${describe(error)}`,
327
+ `set ${PRIVY_BROKER_URL_ENV} to the HTTPS broker that minted ${PRIVY_SESSION_TOKEN_ENV}. A session token is only valid against the broker that issued it.`,
328
+ );
329
+ }
330
+ return embeddedAccount(provider, input.appId, address).then((account) => ({
331
+ source: "privy-session" as const,
332
+ account,
333
+ transport: custom(provider),
334
+ description: `${address} (Privy broker session via ${input.brokerUrl})`,
335
+ readSpendPolicy: async () =>
336
+ await fetchPrivySpendPolicy(
337
+ input.brokerUrl,
338
+ input.fetchImpl ?? globalThis.fetch.bind(globalThis),
339
+ ),
340
+ }));
341
+ }
342
+
343
+ /**
344
+ * Wraps a provider in the package's existing Privy EOA adapter.
345
+ *
346
+ * Both CLI providers answer `eth_chainId` and `eth_accounts` locally, so the
347
+ * adapter's session assertion is a pure local consistency check here — no
348
+ * network call happens while the wallet is being resolved.
349
+ */
350
+ async function embeddedAccount(
351
+ provider: PrivyEip1193Provider,
352
+ appId: string,
353
+ address: `0x${string}`,
354
+ ): Promise<Account> {
355
+ try {
356
+ return await createPrivyEmbeddedAccount({
357
+ appId,
358
+ address,
359
+ provider,
360
+ chainId: 84532,
361
+ });
362
+ } catch (error) {
363
+ throw configError(
364
+ `the Privy wallet could not be prepared: ${describe(error)}`,
365
+ `check ${PRIVY_ENV_VARS.join(", ")}. Metrik Privy wallets are Base Sepolia (84532) only.`,
366
+ );
367
+ }
368
+ }
369
+
370
+ /**
371
+ * The credential store the autonomous wallet was provisioned into.
372
+ *
373
+ * Selection mirrors `provisionMetrikAutonomousWallet`'s documented options:
374
+ * setting `METRIK_WALLET_ENCRYPTION_KEY` means the encrypted-file store (the
375
+ * durable option for a headless host), otherwise the host OS store. Picking a
376
+ * different store than provisioning used simply finds no record, and the
377
+ * "no wallet is stored under this key" error above says so.
378
+ */
379
+ function selectCredentialStore(
380
+ env: NodeJS.ProcessEnv,
381
+ ): AutonomousWalletCredentialStore {
382
+ if (read(env, METRIK_WALLET_ENCRYPTION_KEY_ENV) !== null) {
383
+ return new EncryptedFileCredentialStore({ env });
384
+ }
385
+ return createDefaultAutonomousWalletStore();
386
+ }
387
+
388
+ function read(env: NodeJS.ProcessEnv, name: string): string | null {
389
+ const value = env[name];
390
+ return value === undefined || value.length === 0 ? null : value;
391
+ }
392
+
393
+ function describe(error: unknown): string {
394
+ if (error instanceof AutonomousWalletError) return error.message;
395
+ return error instanceof Error ? error.message : String(error);
396
+ }