@ampersend_ai/ampersend-sdk 0.0.19 → 0.0.21

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 (123) hide show
  1. package/dist/ampersend/client.d.ts +4 -9
  2. package/dist/ampersend/client.d.ts.map +1 -1
  3. package/dist/ampersend/client.js +25 -21
  4. package/dist/ampersend/client.js.map +1 -1
  5. package/dist/ampersend/treasurer.d.ts +14 -79
  6. package/dist/ampersend/treasurer.d.ts.map +1 -1
  7. package/dist/ampersend/treasurer.js +34 -102
  8. package/dist/ampersend/treasurer.js.map +1 -1
  9. package/dist/ampersend/types.d.ts +342 -336
  10. package/dist/ampersend/types.d.ts.map +1 -1
  11. package/dist/ampersend/types.js +110 -149
  12. package/dist/ampersend/types.js.map +1 -1
  13. package/dist/ampersend/zod-bridge.d.ts +15 -0
  14. package/dist/ampersend/zod-bridge.d.ts.map +1 -0
  15. package/dist/ampersend/zod-bridge.js +29 -0
  16. package/dist/ampersend/zod-bridge.js.map +1 -0
  17. package/dist/cli/commands/fetch.d.ts.map +1 -1
  18. package/dist/cli/commands/fetch.js.map +1 -1
  19. package/dist/cli/commands/setup.d.ts.map +1 -1
  20. package/dist/cli/commands/setup.js +6 -1
  21. package/dist/cli/commands/setup.js.map +1 -1
  22. package/dist/index.d.ts +2 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +1 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/mcp/client/client.d.ts +0 -3
  27. package/dist/mcp/client/client.d.ts.map +1 -1
  28. package/dist/mcp/client/client.js +10 -14
  29. package/dist/mcp/client/client.js.map +1 -1
  30. package/dist/mcp/client/factory.d.ts +0 -2
  31. package/dist/mcp/client/factory.d.ts.map +1 -1
  32. package/dist/mcp/client/factory.js +0 -3
  33. package/dist/mcp/client/factory.js.map +1 -1
  34. package/dist/mcp/client/middleware.d.ts +0 -3
  35. package/dist/mcp/client/middleware.d.ts.map +1 -1
  36. package/dist/mcp/client/middleware.js +9 -12
  37. package/dist/mcp/client/middleware.js.map +1 -1
  38. package/dist/mcp/client/protocol.d.ts +81 -85
  39. package/dist/mcp/client/protocol.d.ts.map +1 -1
  40. package/dist/mcp/client/protocol.js +17 -15
  41. package/dist/mcp/client/protocol.js.map +1 -1
  42. package/dist/mcp/client/types.d.ts +6 -19
  43. package/dist/mcp/client/types.d.ts.map +1 -1
  44. package/dist/mcp/proxy/cli.d.ts.map +1 -1
  45. package/dist/mcp/proxy/cli.js +0 -1
  46. package/dist/mcp/proxy/cli.js.map +1 -1
  47. package/dist/mcp/proxy/env.d.ts +0 -9
  48. package/dist/mcp/proxy/env.d.ts.map +1 -1
  49. package/dist/mcp/proxy/env.js +0 -2
  50. package/dist/mcp/proxy/env.js.map +1 -1
  51. package/dist/mcp/proxy/factory.d.ts +0 -2
  52. package/dist/mcp/proxy/factory.d.ts.map +1 -1
  53. package/dist/mcp/proxy/factory.js +0 -3
  54. package/dist/mcp/proxy/factory.js.map +1 -1
  55. package/dist/mcp/server/fastmcp/middleware.d.ts +9 -29
  56. package/dist/mcp/server/fastmcp/middleware.d.ts.map +1 -1
  57. package/dist/mcp/server/fastmcp/middleware.js +10 -42
  58. package/dist/mcp/server/fastmcp/middleware.js.map +1 -1
  59. package/dist/version.d.ts +1 -1
  60. package/dist/version.js +1 -1
  61. package/dist/x402/envelopes.d.ts +62 -0
  62. package/dist/x402/envelopes.d.ts.map +1 -0
  63. package/dist/x402/envelopes.js +73 -0
  64. package/dist/x402/envelopes.js.map +1 -0
  65. package/dist/x402/http/client.d.ts +37 -0
  66. package/dist/x402/http/client.d.ts.map +1 -0
  67. package/dist/x402/http/client.js +159 -0
  68. package/dist/x402/http/client.js.map +1 -0
  69. package/dist/x402/http/factory.d.ts +11 -25
  70. package/dist/x402/http/factory.d.ts.map +1 -1
  71. package/dist/x402/http/factory.js +11 -22
  72. package/dist/x402/http/factory.js.map +1 -1
  73. package/dist/x402/http/index.d.ts +4 -1
  74. package/dist/x402/http/index.d.ts.map +1 -1
  75. package/dist/x402/http/index.js +2 -1
  76. package/dist/x402/http/index.js.map +1 -1
  77. package/dist/x402/index.d.ts +4 -1
  78. package/dist/x402/index.d.ts.map +1 -1
  79. package/dist/x402/index.js +3 -2
  80. package/dist/x402/index.js.map +1 -1
  81. package/dist/x402/treasurer.d.ts +22 -47
  82. package/dist/x402/treasurer.d.ts.map +1 -1
  83. package/dist/x402/treasurers/naive.d.ts +4 -40
  84. package/dist/x402/treasurers/naive.d.ts.map +1 -1
  85. package/dist/x402/treasurers/naive.js +7 -44
  86. package/dist/x402/treasurers/naive.js.map +1 -1
  87. package/dist/x402/wallet.d.ts +7 -32
  88. package/dist/x402/wallet.d.ts.map +1 -1
  89. package/dist/x402/wallet.js +0 -3
  90. package/dist/x402/wallet.js.map +1 -1
  91. package/dist/x402/wallets/account/wallet.d.ts +9 -30
  92. package/dist/x402/wallets/account/wallet.d.ts.map +1 -1
  93. package/dist/x402/wallets/account/wallet.js +30 -37
  94. package/dist/x402/wallets/account/wallet.js.map +1 -1
  95. package/dist/x402/wallets/index.d.ts +0 -1
  96. package/dist/x402/wallets/index.d.ts.map +1 -1
  97. package/dist/x402/wallets/index.js +0 -1
  98. package/dist/x402/wallets/index.js.map +1 -1
  99. package/dist/x402/wallets/smart-account/chain.d.ts +4 -0
  100. package/dist/x402/wallets/smart-account/chain.d.ts.map +1 -0
  101. package/dist/x402/wallets/smart-account/chain.js +12 -0
  102. package/dist/x402/wallets/smart-account/chain.js.map +1 -0
  103. package/dist/x402/wallets/smart-account/cosigned.d.ts +7 -33
  104. package/dist/x402/wallets/smart-account/cosigned.d.ts.map +1 -1
  105. package/dist/x402/wallets/smart-account/cosigned.js +28 -51
  106. package/dist/x402/wallets/smart-account/cosigned.js.map +1 -1
  107. package/dist/x402/wallets/smart-account/exact.d.ts +6 -20
  108. package/dist/x402/wallets/smart-account/exact.d.ts.map +1 -1
  109. package/dist/x402/wallets/smart-account/exact.js +33 -43
  110. package/dist/x402/wallets/smart-account/exact.js.map +1 -1
  111. package/dist/x402/wallets/smart-account/wallet.d.ts +6 -39
  112. package/dist/x402/wallets/smart-account/wallet.d.ts.map +1 -1
  113. package/dist/x402/wallets/smart-account/wallet.js +9 -36
  114. package/dist/x402/wallets/smart-account/wallet.js.map +1 -1
  115. package/package.json +4 -4
  116. package/dist/x402/http/adapter.d.ts +0 -38
  117. package/dist/x402/http/adapter.d.ts.map +0 -1
  118. package/dist/x402/http/adapter.js +0 -193
  119. package/dist/x402/http/adapter.js.map +0 -1
  120. package/dist/x402/http/v2-adapter.d.ts +0 -56
  121. package/dist/x402/http/v2-adapter.d.ts.map +0 -1
  122. package/dist/x402/http/v2-adapter.js +0 -94
  123. package/dist/x402/http/v2-adapter.js.map +0 -1
@@ -0,0 +1,159 @@
1
+ import { x402Client } from "@x402/core/client";
2
+ import { PaymentRequiredV1Schema, PaymentRequiredV2Schema, } from "@x402/core/schemas";
3
+ export class PaymentDeclinedError extends Error {
4
+ constructor() {
5
+ super("Payment declined by treasurer");
6
+ this.name = "PaymentDeclinedError";
7
+ }
8
+ }
9
+ export class UnsupportedProtocolError extends Error {
10
+ x402Version;
11
+ constructor(x402Version) {
12
+ super(`No scheme registered for x402 version ${x402Version}. ` +
13
+ `Register at least one network for this version via \`.withNetworks({ v${x402Version}: [...] })\`.`);
14
+ this.x402Version = x402Version;
15
+ this.name = "UnsupportedProtocolError";
16
+ }
17
+ }
18
+ /**
19
+ * `x402Client` whose `accepts[i]` selection is driven by an async treasurer.
20
+ *
21
+ * Upstream's selector is sync, so we override `createPaymentPayload` to run
22
+ * the treasurer first, stash `{ accepted → authorization }` in a WeakMap,
23
+ * and delegate to `super`. The installed selector returns the stashed entry
24
+ * by reference equality; our scheme client hands back the pre-signed payload.
25
+ * Hooks, policies, extensions, and failure recovery stay upstream's.
26
+ *
27
+ * Reference equality on `Authorization.accepted` is load-bearing — the
28
+ * treasurer must return the original `accepts[i]`, not a clone.
29
+ */
30
+ export class AmpersendX402Client extends x402Client {
31
+ #treasurer;
32
+ #authByAccepted;
33
+ #supportedVersions = new Set();
34
+ constructor(treasurer) {
35
+ const authByAccepted = new WeakMap();
36
+ super((_x402Version, accepts) => {
37
+ for (const accept of accepts) {
38
+ if (authByAccepted.has(accept))
39
+ return accept;
40
+ }
41
+ throw new Error("AmpersendX402Client: the treasurer's pick is not present in the filtered " +
42
+ "accepts[]. It was either filtered out by a registered policy or the scheme/" +
43
+ "network is not registered. Register the network with `.withNetworks()` or " +
44
+ "relax the policy so the treasurer's pick survives.");
45
+ });
46
+ this.#treasurer = treasurer;
47
+ this.#authByAccepted = authByAccepted;
48
+ }
49
+ withNetworks(networks) {
50
+ const schemeClient = new TreasurerSchemeClient(this.#authByAccepted);
51
+ if (networks.v1?.length)
52
+ this.#supportedVersions.add(1);
53
+ if (networks.v2?.length)
54
+ this.#supportedVersions.add(2);
55
+ for (const network of networks.v1 ?? []) {
56
+ this.registerV1(network, schemeClient);
57
+ }
58
+ for (const network of networks.v2 ?? []) {
59
+ // Upstream pattern-matches networks (including wildcards), but types
60
+ // them as CAIP-2 template literals.
61
+ this.register(network, schemeClient);
62
+ }
63
+ return this;
64
+ }
65
+ async createPaymentPayload(paymentRequired) {
66
+ // Fail before calling the treasurer — avoids wasting an API round-trip on an unsignable request.
67
+ if (!this.#supportedVersions.has(paymentRequired.x402Version)) {
68
+ throw new UnsupportedProtocolError(paymentRequired.x402Version);
69
+ }
70
+ const request = toPaymentRequest(paymentRequired);
71
+ let authorization;
72
+ try {
73
+ authorization = await this.#treasurer.onPaymentRequired(request, { method: "http" });
74
+ }
75
+ catch (error) {
76
+ throw new Error(`Treasurer failed: ${error instanceof Error ? error.message : String(error)}`);
77
+ }
78
+ if (!authorization)
79
+ throw new PaymentDeclinedError();
80
+ const accepted = authorization.accepted;
81
+ if (!paymentRequired.accepts.includes(authorization.accepted)) {
82
+ throw new Error("AmpersendX402Client: Authorization.accepted is not an element of " +
83
+ "paymentRequired.accepts (reference equality required). The treasurer must " +
84
+ "return the original accepts[i] reference, not a clone.");
85
+ }
86
+ this.#authByAccepted.set(accepted, authorization);
87
+ try {
88
+ const payload = await super.createPaymentPayload(paymentRequired);
89
+ await this.#treasurer.onStatus("sending", authorization, { method: "http" });
90
+ return payload;
91
+ }
92
+ catch (error) {
93
+ await this.#treasurer.onStatus("error", authorization, {
94
+ method: "http",
95
+ params: { error: error instanceof Error ? error.message : String(error) },
96
+ });
97
+ throw error;
98
+ }
99
+ finally {
100
+ this.#authByAccepted.delete(accepted);
101
+ }
102
+ }
103
+ }
104
+ /** Does not sign — returns the treasurer's pre-signed payload, looked up by `accepts[i]` reference. */
105
+ class TreasurerSchemeClient {
106
+ store;
107
+ scheme = "exact";
108
+ constructor(store) {
109
+ this.store = store;
110
+ }
111
+ async createPaymentPayload(x402Version, requirements) {
112
+ const authorization = this.store.get(requirements);
113
+ if (!authorization) {
114
+ throw new Error("TreasurerSchemeClient invoked without a stashed authorization. This " +
115
+ "indicates a call path that bypassed AmpersendX402Client.createPaymentPayload.");
116
+ }
117
+ const payment = authorization.payment;
118
+ if (x402Version === 1) {
119
+ if (payment.protocol !== "x402-v1") {
120
+ throw new Error(`Expected v1 authorization; got ${payment.protocol}`);
121
+ }
122
+ return payment.data;
123
+ }
124
+ if (payment.protocol !== "x402-v2") {
125
+ throw new Error(`Expected v2 authorization; got ${payment.protocol}`);
126
+ }
127
+ // Upstream reconstructs `resource` and `accepted` from paymentRequired,
128
+ // so we only return x402Version + payload + optional extensions.
129
+ const result = {
130
+ x402Version: payment.data.x402Version,
131
+ payload: payment.data.payload,
132
+ };
133
+ if (payment.data.extensions != null) {
134
+ result.extensions = payment.data.extensions;
135
+ }
136
+ return result;
137
+ }
138
+ }
139
+ // `data` keeps the original reference (not `r.data`) — reference-equality on
140
+ // `accepts[i]` is load-bearing for the WeakMap dispatch above.
141
+ function toPaymentRequest(paymentRequired) {
142
+ switch (paymentRequired.x402Version) {
143
+ case 1: {
144
+ const r = PaymentRequiredV1Schema.safeParse(paymentRequired);
145
+ if (!r.success)
146
+ throw new Error(`Invalid x402 v1 PaymentRequired: ${r.error.message}`);
147
+ return { protocol: "x402-v1", data: paymentRequired };
148
+ }
149
+ case 2: {
150
+ const r = PaymentRequiredV2Schema.safeParse(paymentRequired);
151
+ if (!r.success)
152
+ throw new Error(`Invalid x402 v2 PaymentRequired: ${r.error.message}`);
153
+ return { protocol: "x402-v2", data: paymentRequired };
154
+ }
155
+ default:
156
+ throw new Error(`Unsupported x402 version: ${paymentRequired.x402Version}`);
157
+ }
158
+ }
159
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/x402/http/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EACL,uBAAuB,EACvB,uBAAuB,GAIxB,MAAM,oBAAoB,CAAA;AAqB3B,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7C;QACE,KAAK,CAAC,+BAA+B,CAAC,CAAA;QACtC,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAA;IACpC,CAAC;CACF;AAED,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACrB;IAA5B,YAA4B,WAAmB;QAC7C,KAAK,CACH,yCAAyC,WAAW,IAAI;YACtD,yEAAyE,WAAW,eAAe,CACtG,CAAA;QAJyB,gBAAW,GAAX,WAAW,CAAQ;QAK7C,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAA;IACxC,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,mBAAoB,SAAQ,UAAU;IACxC,UAAU,CAAe;IACzB,eAAe,CAAgC;IAC/C,kBAAkB,GAAG,IAAI,GAAG,EAAU,CAAA;IAE/C,YAAY,SAAwB;QAClC,MAAM,cAAc,GAAG,IAAI,OAAO,EAAyB,CAAA;QAE3D,KAAK,CAAC,CAAC,YAAY,EAAE,OAAO,EAAE,EAAE;YAC9B,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AC,UAAU,IAAI,IAAI,EAAE,CAAC;YACpC,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,UAAU,CAAA;QAC7C,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAED,6EAA6E;AAC7E,+DAA+D;AAC/D,SAAS,gBAAgB,CAAC,eAAkC;IAC1D,QAAQ,eAAe,CAAC,WAAW,EAAE,CAAC;QACpC,KAAK,CAAC,CAAC,CAAC,CAAC;YACP,MAAM,CAAC,GAAG,uBAAuB,CAAC,SAAS,CAAC,eAAe,CAAC,CAAA;YAC5D,IAAI,CAAC,CAAC,CAAC,OAAO;gBAAE,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;YACtF,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,eAA+C,EAAE,CAAA;QACvF,CAAC;QACD,KAAK,CAAC,CAAC,CAAC,CAAC;YACP,MAAM,CAAC,GAAG,uBAAuB,CAAC,SAAS,CAAC,eAAe,CAAC,CAAA;YAC5D,IAAI,CAAC,CAAC,CAAC,OAAO;gBAAE,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;YACtF,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,eAA+C,EAAE,CAAA;QACvF,CAAC;QACD;YACE,MAAM,IAAI,KAAK,CAAC,6BAA6B,eAAe,CAAC,WAAW,EAAE,CAAC,CAAA;IAC/E,CAAC;AACH,CAAC"}
@@ -1,31 +1,22 @@
1
1
  /**
2
- * Simplified factory for Ampersend HTTP client.
3
- *
4
- * Provides one-liner setup for wrapping x402 HTTP clients with Ampersend payment support.
2
+ * Returns an `x402Client` subclass, so the result drops into
3
+ * `wrapFetchWithPayment` unchanged. For advanced setups, construct
4
+ * `AmpersendX402Client` directly.
5
5
  */
6
- import { x402Client } from "@x402/core/client";
7
6
  import type { Address, Hex } from "viem";
8
- import { type Network } from "x402/types";
9
- /**
10
- * Simplified options for Ampersend HTTP client wrapper.
11
- */
7
+ import { AmpersendX402Client } from "./client.ts";
12
8
  export interface SimpleHttpClientOptions {
13
- /** Smart account address */
9
+ /** Smart account address. */
14
10
  smartAccountAddress: Address;
15
- /** Session key private key for signing */
11
+ /** Session key private key for signing. */
16
12
  sessionKeyPrivateKey: Hex;
17
- /** The x402Client instance to wrap (created automatically if not provided) */
18
- client?: x402Client;
19
- /** Ampersend API URL (defaults to production) */
13
+ /** Ampersend API URL. Defaults to production. */
20
14
  apiUrl?: string;
21
- /** Network to use (defaults to "base"). Chain ID is inferred from this. */
22
- network?: Network;
15
+ /** v1 network name (e.g. `"base"`). v1 and v2 (CAIP-2) are both registered. */
16
+ network?: string;
23
17
  }
24
18
  /**
25
- * Create an x402 HTTP client with Ampersend payment support.
26
- *
27
- * This integrates ampersend-sdk with Coinbase's x402 SDK, allowing you to use
28
- * sophisticated payment authorization logic with the standard x402 HTTP client ecosystem.
19
+ * Create an `AmpersendX402Client` wired to the Ampersend API.
29
20
  *
30
21
  * @example
31
22
  * ```typescript
@@ -36,14 +27,9 @@ export interface SimpleHttpClientOptions {
36
27
  * smartAccountAddress: "0x...",
37
28
  * sessionKeyPrivateKey: "0x...",
38
29
  * })
39
- *
40
30
  * const fetchWithPay = wrapFetchWithPayment(fetch, client)
41
31
  * const response = await fetchWithPay("https://paid-api.com/endpoint")
42
32
  * ```
43
- *
44
- * @param options - Simplified HTTP client configuration
45
- * @returns The configured x402Client instance
46
33
  */
47
- export declare function createAmpersendHttpClient(options: SimpleHttpClientOptions): x402Client;
48
- export { wrapWithAmpersend } from "./adapter.ts";
34
+ export declare function createAmpersendHttpClient(options: SimpleHttpClientOptions): AmpersendX402Client;
49
35
  //# sourceMappingURL=factory.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../../src/x402/http/factory.ts"],"names":[],"mappings":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ampersend HTTP client.
3
- *
4
- * Provides one-liner setup for wrapping x402 HTTP clients with Ampersend payment support.
2
+ * Returns an `x402Client` subclass, so the result drops into
3
+ * `wrapFetchWithPayment` unchanged. For advanced setups, construct
4
+ * `AmpersendX402Client` directly.
5
5
  */
6
- import { x402Client } from "@x402/core/client";
7
- import { EvmNetworkToChainId } from "x402/types";
6
+ import { EVM_NETWORK_CHAIN_ID_MAP } from "@x402/evm/v1";
8
7
  import { createAmpersendTreasurer } from "../../ampersend/treasurer.js";
9
- import { wrapWithAmpersend } from "./adapter.js";
10
- /** Default Ampersend API URL */
8
+ import { AmpersendX402Client } from "./client.js";
11
9
  const DEFAULT_API_URL = "https://api.ampersend.ai";
12
- /** Default network (Base mainnet for production) */
13
10
  const DEFAULT_NETWORK = "base";
14
11
  /**
15
- * Create an x402 HTTP client with Ampersend payment support.
16
- *
17
- * This integrates ampersend-sdk with Coinbase's x402 SDK, allowing you to use
18
- * sophisticated payment authorization logic with the standard x402 HTTP client ecosystem.
12
+ * Create an `AmpersendX402Client` wired to the Ampersend API.
19
13
  *
20
14
  * @example
21
15
  * ```typescript
@@ -26,17 +20,13 @@ const DEFAULT_NETWORK = "base";
26
20
  * smartAccountAddress: "0x...",
27
21
  * sessionKeyPrivateKey: "0x...",
28
22
  * })
29
- *
30
23
  * const fetchWithPay = wrapFetchWithPayment(fetch, client)
31
24
  * const response = await fetchWithPay("https://paid-api.com/endpoint")
32
25
  * ```
33
- *
34
- * @param options - Simplified HTTP client configuration
35
- * @returns The configured x402Client instance
36
26
  */
37
27
  export function createAmpersendHttpClient(options) {
38
28
  const network = options.network ?? DEFAULT_NETWORK;
39
- const chainId = EvmNetworkToChainId.get(network);
29
+ const chainId = EVM_NETWORK_CHAIN_ID_MAP[network];
40
30
  if (chainId === undefined) {
41
31
  throw new Error(`Unknown network: ${network}`);
42
32
  }
@@ -44,11 +34,10 @@ export function createAmpersendHttpClient(options) {
44
34
  smartAccountAddress: options.smartAccountAddress,
45
35
  sessionKeyPrivateKey: options.sessionKeyPrivateKey,
46
36
  apiUrl: options.apiUrl ?? DEFAULT_API_URL,
47
- chainId,
48
37
  });
49
- const client = options.client ?? new x402Client();
50
- return wrapWithAmpersend(client, treasurer, [network]);
38
+ return new AmpersendX402Client(treasurer).withNetworks({
39
+ v1: [network],
40
+ v2: [`eip155:${chainId}`],
41
+ });
51
42
  }
52
- // Re-export original for advanced use cases
53
- export { wrapWithAmpersend } from "./adapter.js";
54
43
  //# sourceMappingURL=factory.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/x402/http/factory.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE9C,OAAO,EAAE,mBAAmB,EAAgB,MAAM,YAAY,CAAA;AAE9D,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAA;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEhD,gCAAgC;AAChC,MAAM,eAAe,GAAG,0BAA0B,CAAA;AAElD,oDAAoD;AACpD,MAAM,eAAe,GAAY,MAAM,CAAA;AAkBvC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAgC;IACxE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,eAAe,CAAA;IAClD,MAAM,OAAO,GAAG,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;IAEhD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,oBAAoB,OAAO,EAAE,CAAC,CAAA;IAChD,CAAC;IAED,MAAM,SAAS,GAAG,wBAAwB,CAAC;QACzC,mBAAmB,EAAE,OAAO,CAAC,mBAAmB;QAChD,oBAAoB,EAAE,OAAO,CAAC,oBAAoB;QAClD,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,eAAe;QACzC,OAAO;K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"}
@@ -1,2 +1,5 @@
1
- export { wrapWithAmpersend } from "./adapter.ts";
1
+ export { AmpersendX402Client, PaymentDeclinedError, UnsupportedProtocolError } from "./client.ts";
2
+ export type { AmpersendNetworks } from "./client.ts";
3
+ export { createAmpersendHttpClient } from "./factory.ts";
4
+ export type { SimpleHttpClientOptions } from "./factory.ts";
2
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/x402/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/x402/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AACjG,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA;AACxD,YAAY,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA"}
@@ -1,2 +1,3 @@
1
- export { wrapWithAmpersend } from "./adapter.js";
1
+ export { AmpersendX402Client, PaymentDeclinedError, UnsupportedProtocolError } from "./client.js";
2
+ export { createAmpersendHttpClient } from "./factory.js";
2
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/x402/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/x402/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AAEjG,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA"}
@@ -1,8 +1,11 @@
1
+ export type { PaymentAuthorization, PaymentInstruction, PaymentRequest, Protocol, SchemeSpecificPayload, SettlementResult, } from "./envelopes.ts";
2
+ export { acceptedOf, amountOf, buildAuthorization, firstInstructionOf, resourceUrlOf } from "./envelopes.ts";
1
3
  export type { Authorization, PaymentContext, PaymentStatus, X402Treasurer } from "./treasurer.ts";
2
4
  export type { ERC3009AuthorizationData, ServerAuthorizationData } from "./types.ts";
3
5
  export { WalletError } from "./wallet.ts";
4
6
  export type { X402Wallet } from "./wallet.ts";
5
7
  export { AccountWallet, SmartAccountWallet, createWalletFromConfig } from "./wallets/index.ts";
6
8
  export type { SmartAccountConfig, WalletConfig, EOAWalletConfig, SmartAccountWalletConfig } from "./wallets/index.ts";
7
- export { wrapWithAmpersend } from "./http/index.ts";
9
+ export { AmpersendX402Client, PaymentDeclinedError, UnsupportedProtocolError, createAmpersendHttpClient, } from "./http/index.ts";
10
+ export type { AmpersendNetworks, SimpleHttpClientOptions } from "./http/index.ts";
8
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/x402/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AACjG,YAAY,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AACnF,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAG7C,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAC9F,YAAY,EAAE,kBAAkB,EAAE,YAAY,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAGrH,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/x402/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,oBAAoB,EACpB,kBAAkB,EAClB,cAAc,EACd,QAAQ,EACR,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAG5G,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AACjG,YAAY,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AACnF,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAG7C,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAC9F,YAAY,EAAE,kBAAkB,EAAE,YAAY,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAGrH,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACpB,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,iBAAiB,CAAA;AACxB,YAAY,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAA"}
@@ -1,6 +1,7 @@
1
+ export { acceptedOf, amountOf, buildAuthorization, firstInstructionOf, resourceUrlOf } from "./envelopes.js";
1
2
  export { WalletError } from "./wallet.js";
2
3
  // Wallet implementations
3
4
  export { AccountWallet, SmartAccountWallet, createWalletFromConfig } from "./wallets/index.js";
4
- // HTTP adapter
5
- export { wrapWithAmpersend } from "./http/index.js";
5
+ // HTTP integration
6
+ export { AmpersendX402Client, PaymentDeclinedError, UnsupportedProtocolError, createAmpersendHttpClient, } from "./http/index.js";
6
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/x402/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAGzC,yBAAyB;AACzB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAG9F,eAAe;AACf,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/x402/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAK5G,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAGzC,yBAAyB;AACzB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAG9F,mBAAmB;AACnB,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACpB,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,iBAAiB,CAAA"}
@@ -1,64 +1,39 @@
1
- import type { PaymentPayload, PaymentRequirements } from "x402/types";
1
+ import type { PaymentRequirementsV1, PaymentRequirementsV2 } from "@x402/core/schemas";
2
+ import type { PaymentAuthorization, PaymentRequest } from "./envelopes.ts";
2
3
  /**
3
- * Context information for payment decisions
4
+ * Loose caller context which protocol triggered the flow and any debugging
5
+ * metadata. NOT payment details; those live on the {@link PaymentRequest}.
4
6
  */
5
7
  export interface PaymentContext {
6
8
  method: string;
7
- params: any;
9
+ params?: any;
8
10
  metadata?: Record<string, unknown>;
9
11
  }
10
- /**
11
- * Authorization linking a payment with a tracking ID
12
- */
13
12
  export interface Authorization {
14
- payment: PaymentPayload;
13
+ payment: PaymentAuthorization;
15
14
  authorizationId: string;
15
+ /**
16
+ * The `accepts[i]` the wallet signed against. Must be `===` to an element
17
+ * of the original `PaymentRequest.data.accepts` — reference equality is
18
+ * load-bearing for downstream integrations (e.g. the `x402Client` subclass).
19
+ */
20
+ accepted: PaymentRequirementsV1 | PaymentRequirementsV2;
16
21
  }
17
- /**
18
- * Payment status types for tracking payment lifecycle
19
- */
20
22
  export type PaymentStatus = "sending" | "accepted" | "rejected" | "declined" | "error";
21
23
  /**
22
- * X402Treasurer interface - Separates payment decision logic from payment creation
23
- *
24
- * An X402Treasurer decides whether to approve or reject payment requests,
25
- * tracks payment status, and delegates actual payment creation to an X402Wallet.
24
+ * Separates payment *decisions* from payment *creation*. Receives the seller's
25
+ * full 402 body, returns a signed {@link Authorization} or `null` to decline.
26
+ * Internally picks an index into `request.data.accepts[]` and hands the
27
+ * resulting `PaymentInstruction` to a wallet; use {@link firstInstructionOf}
28
+ * for the trivial "take the first option" case.
26
29
  *
27
- * @example
28
- * ```typescript
29
- * class BudgetTreasurer implements X402Treasurer {
30
- * constructor(private wallet: X402Wallet, private dailyLimit: number) {}
31
- *
32
- * async onPaymentRequired(requirements, context) {
33
- * if (this.wouldExceedBudget(requirements[0])) {
34
- * return null // Decline
35
- * }
36
- * const payment = await this.wallet.createPayment(requirements[0])
37
- * return { payment, authorizationId: crypto.randomUUID() }
38
- * }
39
- *
40
- * async onStatus(status, authorization, context) {
41
- * console.log(`Payment ${authorization.authorizationId}: ${status}`)
42
- * }
43
- * }
44
- * ```
30
+ * Return `null` only for domain-level declines (budget exhausted, user
31
+ * rejected, policy said no). Infrastructure failures — network errors, auth
32
+ * failures, wallet signing errors — must **throw**, so callers can distinguish
33
+ * "you can't have this payment" from "something is broken."
45
34
  */
46
35
  export interface X402Treasurer {
47
- /**
48
- * Called when payment is required.
49
- *
50
- * @param requirements - Array of payment requirements from seller (typically use first)
51
- * @param context - Optional context about the request requiring payment
52
- * @returns Authorization to proceed with payment, or null to decline
53
- */
54
- onPaymentRequired(requirements: ReadonlyArray<PaymentRequirements>, context?: PaymentContext): Promise<Authorization | null>;
55
- /**
56
- * Called with payment status updates throughout the payment lifecycle.
57
- *
58
- * @param status - Current payment status
59
- * @param authorization - The authorization returned from onPaymentRequired
60
- * @param context - Optional context about the status update
61
- */
36
+ onPaymentRequired(request: PaymentRequest, context?: PaymentContext): Promise<Authorization | null>;
62
37
  onStatus(status: PaymentStatus, authorization: Authorization, context?: PaymentContext): Promise<void>;
63
38
  }
64
39
  //# sourceMappingURL=treasurer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"treasurer.d.ts","sourceRoot":"","sources":["../../src/x402/treasurer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAErE;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,GAAG,CAAA;IACX,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,cAAc,CAAA;IACvB,eAAe,EAAE,MAAM,CAAA;CACxB;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,SAAS,GACT,UAAU,GACV,UAAU,GACV,UAAU,GACV,OAAO,CAAA;AAEX;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;OAMG;IACH,iBAAiB,CACf,YAAY,EAAE,aAAa,CAAC,mBAAmB,CAAC,EAChD,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IAEhC;;;;;;OAMG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACvG"}
1
+ {"version":3,"file":"treasurer.d.ts","sourceRoot":"","sources":["../../src/x402/treasurer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAEtF,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAE1E;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,GAAG,CAAA;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACnC;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,oBAAoB,CAAA;IAC7B,eAAe,EAAE,MAAM,CAAA;IACvB;;;;OAIG;IACH,QAAQ,EAAE,qBAAqB,GAAG,qBAAqB,CAAA;CACxD;AAED,MAAM,MAAM,aAAa,GACrB,SAAS,GACT,UAAU,GACV,UAAU,GACV,UAAU,GACV,OAAO,CAAA;AAEX;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,aAAa;IAC5B,iBAAiB,CAAC,OAAO,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IACnG,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACvG"}
@@ -1,52 +1,16 @@
1
- import type { PaymentRequirements } from "x402/types";
1
+ import { type PaymentRequest } from "../envelopes.ts";
2
2
  import type { Authorization, PaymentContext, PaymentStatus, X402Treasurer } from "../treasurer.ts";
3
3
  import type { X402Wallet } from "../wallet.ts";
4
4
  import { type WalletConfig } from "../wallets/index.ts";
5
5
  /**
6
- * NaiveTreasurer - Auto-approves all payment requests
7
- *
8
- * This treasurer automatically approves all payment requests without
9
- * any budget checks or user confirmation. Useful for:
10
- * - Testing and development
11
- * - Trusted sellers where all requests should be paid
12
- * - Simple use cases without budget limits
13
- *
14
- * @example
15
- * ```typescript
16
- * const wallet = new AccountWallet(account)
17
- * const treasurer = new NaiveTreasurer(wallet)
18
- *
19
- * // Auto-approves all payments
20
- * const client = new X402Client({ treasurer })
21
- * ```
6
+ * Auto-approves any request by signing its first `accepts[]` entry. No budget
7
+ * or policy checks; for testing and development only.
22
8
  */
23
9
  export declare class NaiveTreasurer implements X402Treasurer {
24
10
  private wallet;
25
11
  constructor(wallet: X402Wallet);
26
- /**
27
- * Always approves payment by creating payment with the wallet.
28
- * Uses the first requirement from the array.
29
- */
30
- onPaymentRequired(requirements: Array<PaymentRequirements>, _context?: PaymentContext): Promise<Authorization | null>;
31
- /**
32
- * Logs payment status to console for debugging.
33
- */
12
+ onPaymentRequired(request: PaymentRequest, _context?: PaymentContext): Promise<Authorization | null>;
34
13
  onStatus(status: PaymentStatus, authorization: Authorization, _context?: PaymentContext): Promise<void>;
35
14
  }
36
- /**
37
- * Creates a naive treasurer that automatically approves all payment requests.
38
- * This treasurer selects the first payment requirement and creates a payment immediately.
39
- *
40
- * @param walletConfig - Configuration for the wallet to use
41
- * @returns An X402Treasurer implementation
42
- *
43
- * @example
44
- * ```typescript
45
- * const treasurer = createNaiveTreasurer({
46
- * type: 'eoa',
47
- * privateKey: '0x...'
48
- * })
49
- * ```
50
- */
51
15
  export declare function createNaiveTreasurer(walletConfig: WalletConfig): X402Treasurer;
52
16
  //# sourceMappingURL=naive.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"naive.d.ts","sourceRoot":"","sources":["../../../src/x402/treasurers/naive.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAErD,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAClG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAC9C,OAAO,EAA0B,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAE/E;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAe,YA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ionOf } from "../envelopes.js";
1
2
  import { createWalletFromConfig } from "../wallets/index.js";
2
3
  /**
3
- * NaiveTreasurer - Auto-approves all payment requests
4
- *
5
- * This treasurer automatically approves all payment requests without
6
- * any budget checks or user confirmation. Useful for:
7
- * - Testing and development
8
- * - Trusted sellers where all requests should be paid
9
- * - Simple use cases without budget limits
10
- *
11
- * @example
12
- * ```typescript
13
- * const wallet = new AccountWallet(account)
14
- * const treasurer = new NaiveTreasurer(wallet)
15
- *
16
- * // Auto-approves all payments
17
- * const client = new X402Client({ treasurer })
18
- * ```
4
+ * Auto-approves any request by signing its first `accepts[]` entry. No budget
5
+ * or policy checks; for testing and development only.
19
6
  */
20
7
  export class NaiveTreasurer {
21
8
  wallet;
22
9
  constructor(wallet) {
23
10
  this.wallet = wallet;
24
11
  }
25
- /**
26
- * Always approves payment by creating payment with the wallet.
27
- * Uses the first requirement from the array.
28
- */
29
- async onPaymentRequired(requirements, _context) {
30
- if (requirements.length === 0) {
31
- return null;
32
- }
33
- // Create payment using the wallet
34
- const payment = await this.wallet.createPayment(requirements[0]);
12
+ async onPaymentRequired(request, _context) {
13
+ const instruction = firstInstructionOf(request);
14
+ const payment = await this.wallet.createPayment(instruction);
35
15
  return {
36
16
  payment,
37
17
  authorizationId: crypto.randomUUID(),
18
+ accepted: acceptedOf(instruction),
38
19
  };
39
20
  }
40
- /**
41
- * Logs payment status to console for debugging.
42
- */
43
21
  async onStatus(status, authorization, _context) {
44
22
  console.log(`[NaiveTreasurer] Payment ${authorization.authorizationId}: ${status}`);
45
23
  }
46
24
  }
47
- /**
48
- * Creates a naive treasurer that automatically approves all payment requests.
49
- * This treasurer selects the first payment requirement and creates a payment immediately.
50
- *
51
- * @param walletConfig - Configuration for the wallet to use
52
- * @returns An X402Treasurer implementation
53
- *
54
- * @example
55
- * ```typescript
56
- * const treasurer = createNaiveTreasurer({
57
- * type: 'eoa',
58
- * privateKey: '0x...'
59
- * })
60
- * ```
61
- */
62
25
  export function createNaiveTreasurer(walletConfig) {
63
26
  const wallet = createWalletFromConfig(walletConfig);
64
27
  return new NaiveTreasurer(wallet);
@@ -1 +1 @@
1
- {"version":3,"file":"naive.js","sourceRoot":"","sources":["../../../src/x402/treasurers/naive.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,sBAAsB,EAAqB,MAAM,qBAAqB,CAAA;AAE/E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,cAAc;IACL;IAApB,YAAoB,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;IAAG,CAAC;IAE1C;;;OAGG;IACH,KAAK,CAAC,iBAAiB,CACrB,YAAwC,EACxC,QAAyB;QAEzB,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAA;QACb,CAAC;QAED,kCAAkC;QAClC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAA;QAEhE,OAAO;YACL,OAAO;YACP,eAAe,EAAE,MAAM,CAAC,UAAU,EAAE;SACrC,CAAA;IACH,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,QAAQ,CAAC,MAAqB,EAAE,aAA4B,EAAE,QAAyB;QAC3F,OAAO,CAAC,GAAG,CAAC,4BAA4B,aAAa,CAAC,eAAe,KAAK,MAAM,EAAE,CAAC,CAAA;IACrF,CAAC;CACF;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,oBAAoB,CAAC,YAA0B;IAC7D,MAAM,MAAM,GAAG,sBAAsB,CAAC,YAAY,CAAC,CAAA;IACnD,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,CAAA;AACnC,CAAC"}
1
+ {"version":3,"file":"naive.js","sourceRoot":"","sources":["../../../src/x402/treasurers/naive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAuB,MAAM,iBAAiB,CAAA;AAGrF,OAAO,EAAE,sBAAsB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