@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
@@ -1,67 +1,57 @@
1
1
  import { toHex } from "viem";
2
2
  import { signERC3009Authorization } from "../../../smart-account/index.js";
3
- /**
4
- * Generates a random 32-byte nonce for use in authorization signatures
5
- */
3
+ import { acceptedOf, amountOf, buildAuthorization, } from "../../envelopes.js";
4
+ import { chainIdOf } from "./chain.js";
6
5
  function createNonce() {
7
6
  const cryptoObj = typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.getRandomValues === "function"
8
7
  ? globalThis.crypto
9
- : // Dynamic require is needed to support node.js
8
+ : // Dynamic require for node.js compatibility.
10
9
  // eslint-disable-next-line @typescript-eslint/no-require-imports
11
10
  require("crypto").webcrypto;
12
11
  return toHex(cryptoObj.getRandomValues(new Uint8Array(32)));
13
12
  }
14
13
  /**
15
- * Creates a payment payload using the "exact" scheme with ERC-3009 USDC authorization
16
- *
17
- * This implements the x402 "exact" payment scheme, which uses USDC's transferWithAuthorization
18
- * (ERC-3009) to create signed payment authorizations. The signature is created using ERC-1271
19
- * from a smart account via the OwnableValidator module.
20
- *
21
- * @param requirements - Payment requirements from the x402 server
22
- * @param config - Configuration for the smart account wallet
23
- * @returns Payment payload ready to send to x402 server
24
- * @throws Error if payment requirements are invalid or signing fails
14
+ * Sign an `exact` instruction: ERC-3009 `transferWithAuthorization` via
15
+ * ERC-1271 with OwnableValidator. {@link buildAuthorization} packages the
16
+ * signed body for v1 or v2.
25
17
  */
26
- export async function createExactPayment(requirements, config) {
27
- // Generate nonce and validity timestamps
18
+ export async function createExactPayment(instruction, config) {
19
+ const accepted = acceptedOf(instruction);
20
+ const payTo = accepted.payTo;
21
+ const asset = accepted.asset;
28
22
  const nonce = createNonce();
29
- const validAfter = BigInt(Math.floor(Date.now() / 1000) - 600); // 10 minutes before
30
- const validBefore = BigInt(Math.floor(Date.now() / 1000) + requirements.maxTimeoutSeconds);
31
- // Prepare authorization data for ERC-3009 signing
23
+ const now = Math.floor(Date.now() / 1000);
24
+ const validAfter = BigInt(now - 600); // 10 minutes of clock-skew tolerance
25
+ const validBefore = BigInt(now + accepted.maxTimeoutSeconds);
32
26
  const authData = {
33
27
  from: config.smartAccountAddress,
34
- to: requirements.payTo,
35
- value: BigInt(requirements.maxAmountRequired),
28
+ to: payTo,
29
+ value: BigInt(amountOf(instruction)),
36
30
  validAfter,
37
31
  validBefore,
38
32
  nonce,
39
33
  };
40
- // Get domain params from requirements.extra (provided by server)
41
- const domainName = requirements.extra?.name;
42
- const domainVersion = requirements.extra?.version;
34
+ const domainName = accepted.extra?.name;
35
+ const domainVersion = accepted.extra?.version;
43
36
  if (!domainName || !domainVersion) {
44
- throw new Error("requirements.extra must contain 'name' and 'version' for EIP-712 domain");
37
+ throw new Error("accepted.extra must contain 'name' and 'version' for EIP-712 domain");
38
+ }
39
+ const chainId = chainIdOf(instruction);
40
+ if (chainId === null) {
41
+ throw new Error(`Unsupported network "${accepted.network}" — use a known v1 name or CAIP-2 "eip155:N".`);
45
42
  }
46
- // Sign using ERC-1271 with OwnableValidator
47
- const signature = await signERC3009Authorization(config.sessionKeyPrivateKey, config.smartAccountAddress, authData, requirements.asset, config.chainId, config.validatorAddress, domainName, domainVersion);
48
- // Construct payment payload matching x402 exact scheme format
49
- const paymentPayload = {
50
- x402Version: 1,
51
- scheme: "exact",
52
- network: requirements.network,
53
- payload: {
54
- signature: signature,
55
- authorization: {
56
- from: config.smartAccountAddress,
57
- to: requirements.payTo,
58
- value: requirements.maxAmountRequired,
59
- validAfter: validAfter.toString(),
60
- validBefore: validBefore.toString(),
61
- nonce: nonce,
62
- },
43
+ const signature = await signERC3009Authorization(config.sessionKeyPrivateKey, config.smartAccountAddress, authData, asset, chainId, config.validatorAddress, domainName, domainVersion);
44
+ const signedPayload = {
45
+ signature: signature,
46
+ authorization: {
47
+ from: config.smartAccountAddress,
48
+ to: payTo,
49
+ value: amountOf(instruction),
50
+ validAfter: validAfter.toString(),
51
+ validBefore: validBefore.toString(),
52
+ nonce: nonce,
63
53
  },
64
54
  };
65
- return paymentPayload;
55
+ return buildAuthorization(instruction, signedPayload);
66
56
  }
67
57
  //# sourceMappingURL=exact.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"exact.js","sourceRoot":"","sources":["../../../../src/x402/wallets/smart-account/exact.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAA0B,MAAM,MAAM,CAAA;AAGpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAA;AAE1E;;GAEG;AACH,SAAS,WAAW;IAClB,MAAM,SAAS,GACb,OAAO,UAAU,CAAC,MAAM,KAAK,WAAW,IAAI,OAAO,UAAU,CAAC,MAAM,CAAC,eAAe,KAAK,UAAU;QACjG,CAAC,CAAC,UAAU,CAAC,MAAM;QACnB,CAAC,CAAC,+CAA+C;YAC/C,iEAAiE;YACjE,OAAO,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAA;IACjC,OAAO,KAAK,CAAC,SAAS,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;AAC7D,CAAC;AAgBD;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,YAAiC,EACjC,MAA0B;IAE1B,yCAAyC;IACzC,MAAM,KAA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AC5B,UAAU,EAAE,UAAU,CAAC,QAAQ,EAAE;YACjC,WAAW,EAAE,WAAW,CAAC,QAAQ,EAAE;YACnC,KAAK,EAAE,KAAe;SACvB;KACF,CAAA;IAED,OAAO,kBAAkB,CAAC,WAAW,EAAE,aAAa,CAAC,CAAA;AACvD,CAAC"}
@@ -1,56 +1,23 @@
1
1
  import { type Address, type Hex } from "viem";
2
- import type { PaymentPayload, PaymentRequirements } from "x402/types";
2
+ import { type PaymentAuthorization, type PaymentInstruction } from "../../envelopes.ts";
3
3
  import type { ServerAuthorizationData } from "../../types.ts";
4
4
  import { type X402Wallet } from "../../wallet.ts";
5
- /**
6
- * Configuration for SmartAccountWallet
7
- */
8
5
  export interface SmartAccountConfig {
9
- /** Smart account address */
10
6
  smartAccountAddress: Address;
11
- /** Session key private key for signing */
12
7
  sessionKeyPrivateKey: Hex;
13
- /** Chain ID for the blockchain network */
14
- chainId: number;
15
- /** OwnableValidator address (defaults to standard OwnableValidator) */
8
+ /** OwnableValidator address. Defaults to the standard OwnableValidator. */
16
9
  validatorAddress?: Address;
17
- /** CoSignerValidator address (defaults to standard CoSignerValidator) */
10
+ /** CoSignerValidator address. Defaults to the standard CoSignerValidator. */
18
11
  coSignerValidatorAddress?: Address;
19
12
  }
20
13
  /**
21
- * SmartAccountWallet - Smart account wallet implementation using ERC-1271
22
- *
23
- * Creates payment payloads signed by a smart account using ERC-1271 standard.
24
- * Supports Safe accounts with OwnableValidator module.
25
- * Only supports the "exact" payment scheme with ERC-3009 (USDC) authorizations.
26
- *
27
- * @example
28
- * ```typescript
29
- * const wallet = new SmartAccountWallet({
30
- * smartAccountAddress: "0x...", // Smart account address
31
- * sessionKeyPrivateKey: "0x...", // Session key
32
- * chainId: 84532, // Base Sepolia
33
- * validatorAddress: "0x..." // OwnableValidator (optional, defaults to standard validator)
34
- * })
35
- *
36
- * const payment = await wallet.createPayment(requirements)
37
- * ```
14
+ * Smart account wallet using ERC-1271. Supports Safe accounts with the
15
+ * OwnableValidator module and only the `exact` scheme (ERC-3009 / USDC).
38
16
  */
39
17
  export declare class SmartAccountWallet implements X402Wallet {
40
18
  private readonly config;
41
19
  constructor(config: SmartAccountConfig);
42
- /**
43
- * Creates a payment payload from requirements.
44
- * Only supports "exact" payment scheme with ERC-3009 authorizations.
45
- *
46
- * @param requirements Payment requirements from x402
47
- * @param serverAuthorization Optional server co-signature data for co-signed keys
48
- * @returns Payment payload ready to submit
49
- */
50
- createPayment(requirements: PaymentRequirements, serverAuthorization?: ServerAuthorizationData): Promise<PaymentPayload>;
51
- /**
52
- * Returns the smart account address
53
- */
20
+ createPayment(instruction: PaymentInstruction, serverAuthorization?: ServerAuthorizationData): Promise<PaymentAuthorization>;
54
21
  get address(): Address;
55
22
  }
56
23
  //# sourceMappingURL=wallet.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../../../../src/x402/wallets/smart-account/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,MAAM,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAGrE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAC7D,OAAO,EAAe,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI9D;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,4BAA4B;IAC5B,mBAAmB,EAAE,OAAO,CAAA;IAC5B,0CAA0C;IAC1C,oBAAoB,EAAE,GAAG,CAAA;IACzB,0CAA0C;IAC1C,OAAO,EAAE,MAAM,CAAA;IACf,uEAAuE;IACvE,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,yEAAyE;IACzE,wBAAwB,CAAC,EAAE,OAAO,CAAA;CACnC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,kBAAmB,YAAW,UAAU;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuF;gBAElG,MAAM,EAAE,kBAAkB;IAStC;;;;;;;OAOG;IACG,aAAa,CACjB,YAAY,EAAE,mBAAmB,EACjC,mBAAmB,CAAC,EAAE,uBAAuB,GAC5C,OAAO,CAAC,cAAc,CAAC;IAuB1B;;OAEG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;CACF"}
1
+ {"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../../../../src/x402/wallets/smart-account/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,MAAM,CAAA;AAG7C,OAAO,EAAc,KAAK,oBAAoB,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AACnG,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAC7D,OAAO,EAAe,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI9D,MAAM,WAAW,kBAAkB;IACjC,mBAAmB,EAAE,OAAO,CAAA;IAC5B,oBAAoB,EAAE,GAAG,CAAA;IACzB,2EAA2E;IAC3E,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,6EAA6E;IAC7E,wBAAwB,CAAC,EAAE,OAAO,CAAA;CACnC;AAED;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,UAAU;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuF;gBAElG,MAAM,EAAE,kBAAkB;IAQhC,aAAa,CACjB,WAAW,EAAE,kBAAkB,EAC/B,mBAAmB,CAAC,EAAE,uBAAuB,GAC5C,OAAO,CAAC,oBAAoB,CAAC;IAmBhC,IAAI,OAAO,IAAI,OAAO,CAErB;CACF"}
@@ -1,64 +1,37 @@
1
1
  import {} from "viem";
2
2
  import { COSIGNER_VALIDATOR, OWNABLE_VALIDATOR } from "../../../smart-account/constants.js";
3
+ import { acceptedOf } from "../../envelopes.js";
3
4
  import { WalletError } from "../../wallet.js";
4
5
  import { createCoSignedPayment } from "./cosigned.js";
5
6
  import { createExactPayment } from "./exact.js";
6
7
  /**
7
- * SmartAccountWallet - Smart account wallet implementation using ERC-1271
8
- *
9
- * Creates payment payloads signed by a smart account using ERC-1271 standard.
10
- * Supports Safe accounts with OwnableValidator module.
11
- * Only supports the "exact" payment scheme with ERC-3009 (USDC) authorizations.
12
- *
13
- * @example
14
- * ```typescript
15
- * const wallet = new SmartAccountWallet({
16
- * smartAccountAddress: "0x...", // Smart account address
17
- * sessionKeyPrivateKey: "0x...", // Session key
18
- * chainId: 84532, // Base Sepolia
19
- * validatorAddress: "0x..." // OwnableValidator (optional, defaults to standard validator)
20
- * })
21
- *
22
- * const payment = await wallet.createPayment(requirements)
23
- * ```
8
+ * Smart account wallet using ERC-1271. Supports Safe accounts with the
9
+ * OwnableValidator module and only the `exact` scheme (ERC-3009 / USDC).
24
10
  */
25
11
  export class SmartAccountWallet {
26
12
  config;
27
13
  constructor(config) {
28
- // Apply default validator addresses if not provided
29
14
  this.config = {
30
15
  ...config,
31
16
  validatorAddress: config.validatorAddress ?? OWNABLE_VALIDATOR,
32
17
  coSignerValidatorAddress: config.coSignerValidatorAddress ?? COSIGNER_VALIDATOR,
33
18
  };
34
19
  }
35
- /**
36
- * Creates a payment payload from requirements.
37
- * Only supports "exact" payment scheme with ERC-3009 authorizations.
38
- *
39
- * @param requirements Payment requirements from x402
40
- * @param serverAuthorization Optional server co-signature data for co-signed keys
41
- * @returns Payment payload ready to submit
42
- */
43
- async createPayment(requirements, serverAuthorization) {
44
- if (requirements.scheme !== "exact") {
45
- throw new WalletError(`Unsupported payment scheme: ${requirements.scheme}. SmartAccountWallet only supports "exact".`);
20
+ async createPayment(instruction, serverAuthorization) {
21
+ const accepted = acceptedOf(instruction);
22
+ if (accepted.scheme !== "exact") {
23
+ throw new WalletError(`Unsupported payment scheme: ${accepted.scheme}. SmartAccountWallet only supports "exact".`);
46
24
  }
47
25
  try {
48
- // If server authorization provided, use co-signed path
49
26
  if (serverAuthorization) {
50
- return await createCoSignedPayment(requirements, this.config, serverAuthorization);
27
+ return await createCoSignedPayment(instruction, this.config, serverAuthorization);
51
28
  }
52
- // Otherwise use direct signing (full-access keys)
53
- return await createExactPayment(requirements, this.config);
29
+ return await createExactPayment(instruction, this.config);
54
30
  }
55
31
  catch (error) {
56
32
  throw new WalletError(`Failed to create smart account payment: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? error : undefined);
57
33
  }
58
34
  }
59
- /**
60
- * Returns the smart account address
61
- */
62
35
  get address() {
63
36
  return this.config.smartAccountAddress;
64
37
  }
@@ -1 +1 @@
1
- {"version":3,"file":"wallet.js","sourceRoot":"","sources":["../../../../src/x402/wallets/smart-account/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,MAAM,MAAM,CAAA;AAG7C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAA;AAE3F,OAAO,EAAE,WAAW,EAAmB,MAAM,iBAAiB,CAAA;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAkB/C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,kBAAkB;IACZ,MAAM,CAAuF;IAE9G,YAAY,MAA0B;QACpC,oDAAoD;QACpD,IAAI,CAAC,MAAM,GAAG;YACZ,GAAG,MAAM;YACT,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,IAAI,iBAAiB;YAC9D,wBAAwB,EAAE,MAAM,CAAC,wBAAwB,IAAI,kBAAkB;SAChF,CAAA;IACH,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,aAAa,CACjB,YAAiC,EACjC,mBAA6C;QAE7C,IAAI,YAAY,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;YACpC,MAAM,IAAI,WAAW,CACnB,+BAA+B,YAAY,CAAC,MAAM,6CAA6C,CAChG,CAAA;Q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AC9D,wBAAwB,EAAE,MAAM,CAAC,wBAAwB,IAAI,kBAAkB;SAChF,CAAA;IACH,CAAC;IAED,KAAK,CAAC,aAAa,CACjB,WAA+B,EAC/B,mBAA6C;QAE7C,MAAM,QAAQ,GAAG,UAAU,CAAC,WAAW,CAAC,CAAA;QACxC,IAAI,QAAQ,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;YAChC,MAAM,IAAI,WAAW,CAAC,+BAA+B,QAAQ,CAAC,MAAM,6CAA6C,CAAC,CAAA;QACpH,CAAC;QAED,IAAI,CAAC;YACH,IAAI,mBAAmB,EAAE,CAAC;gBACxB,OAAO,MAAM,qBAAqB,CAAC,WAAW,EAAE,IAAI,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAA;YACnF,CAAC;YACD,OAAO,MAAM,kBAAkB,CAAC,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;QAC3D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,WAAW,CACnB,2CAA2C,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EACnG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAC3C,CAAA;QACH,CAAC;IACH,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAA;IACxC,CAAC;CACF"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ampersend_ai/ampersend-sdk",
3
3
  "description": "Tooling for building applications with x402 payment capabilities. Supports buyer and seller roles.",
4
- "version": "0.0.19",
4
+ "version": "0.0.21",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "type": "git",
@@ -64,8 +64,9 @@
64
64
  "@modelcontextprotocol/sdk": "npm:@ampersend_ai/modelcontextprotocol-sdk@^1.20.1",
65
65
  "@rhinestone/module-sdk": "^0.4.0",
66
66
  "@types/express": "^5.0.6",
67
- "@x402/core": "^2.5.0",
68
- "@x402/fetch": "^2.5.0",
67
+ "@x402/core": "2.9.0",
68
+ "@x402/evm": "2.9.0",
69
+ "@x402/fetch": "2.9.0",
69
70
  "async-mutex": "^0.5.0",
70
71
  "commander": "^12.1.0",
71
72
  "effect": "^3.21.0",
@@ -73,7 +74,6 @@
73
74
  "fastmcp": "npm:@ampersend_ai/fastmcp@^1.0.0",
74
75
  "siwe": "^3.0.0",
75
76
  "viem": "^2.45.0",
76
- "x402": "^0.6.6",
77
77
  "zod": "^3.24.2"
78
78
  },
79
79
  "devDependencies": {
@@ -1,38 +0,0 @@
1
- import type { x402Client } from "@x402/core/client";
2
- import type { X402Treasurer } from "../treasurer.ts";
3
- /**
4
- * Wraps an x402Client to use an ampersend-sdk treasurer for payment decisions.
5
- *
6
- * This adapter integrates ampersend-sdk treasurers with Coinbase's x402 SDK,
7
- * allowing you to use sophisticated payment authorization logic (budgets, policies,
8
- * approvals) with the standard x402 HTTP client ecosystem.
9
- *
10
- * Supports both v1 and v2 x402 protocols. The underlying wallets produce v1 payment
11
- * payloads which are automatically converted to v2 format when needed.
12
- *
13
- * @param client - The x402Client instance to wrap
14
- * @param treasurer - The X402Treasurer that handles payment authorization
15
- * @param networks - Array of v1 network names to register (e.g., 'base', 'base-sepolia')
16
- * @returns The configured x402Client instance (same instance, mutated)
17
- *
18
- * @example
19
- * ```typescript
20
- * import { x402Client } from '@x402/core/client'
21
- * import { wrapFetchWithPayment } from '@x402/fetch'
22
- * import { wrapWithAmpersend, NaiveTreasurer, AccountWallet } from '@ampersend_ai/ampersend-sdk'
23
- *
24
- * const wallet = AccountWallet.fromPrivateKey('0x...')
25
- * const treasurer = new NaiveTreasurer(wallet)
26
- *
27
- * const client = wrapWithAmpersend(
28
- * new x402Client(),
29
- * treasurer,
30
- * ['base', 'base-sepolia']
31
- * )
32
- *
33
- * const fetchWithPay = wrapFetchWithPayment(fetch, client)
34
- * const response = await fetchWithPay('https://paid-api.com/endpoint')
35
- * ```
36
- */
37
- export declare function wrapWithAmpersend(client: x402Client, treasurer: X402Treasurer, networks: Array<string>): x402Client;
38
- //# sourceMappingURL=adapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../../src/x402/http/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,UAAU,EACX,MAAM,mBAAmB,CAAA;AAQ1B,OAAO,KAAK,EAAiB,aAAa,EAAE,MAAM,iBAAiB,CAAA;AA6EnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,UAAU,CAiInH"}
@@ -1,193 +0,0 @@
1
- import { v1NetworkToCaip2, v1PayloadToV2, v2RequirementsToV1 } from "./v2-adapter.js";
2
- /**
3
- * Scheme client that retrieves payments from the treasurer via a shared WeakMap.
4
- * Compatible with @x402/core's SchemeNetworkClient interface for v1 protocol.
5
- *
6
- * Note: We don't implement SchemeNetworkClient directly because @x402/core
7
- * exports v2 types, but registerV1() passes v1 types at runtime.
8
- */
9
- class TreasurerSchemeClientV1 {
10
- paymentStore;
11
- scheme = "exact";
12
- constructor(paymentStore) {
13
- this.paymentStore = paymentStore;
14
- }
15
- async createPaymentPayload(x402Version, requirements) {
16
- const entry = this.paymentStore.get(requirements);
17
- if (!entry) {
18
- throw new Error("No payment authorization found for requirements");
19
- }
20
- // Clean up after retrieval
21
- this.paymentStore.delete(requirements);
22
- return {
23
- x402Version,
24
- scheme: entry.authorization.payment.scheme,
25
- network: entry.authorization.payment.network,
26
- payload: entry.authorization.payment.payload,
27
- };
28
- }
29
- }
30
- /**
31
- * Scheme client for v2 protocol.
32
- * Converts v1 payment payloads to v2 format using stored context.
33
- */
34
- class TreasurerSchemeClientV2 {
35
- paymentStore;
36
- scheme = "exact";
37
- constructor(paymentStore) {
38
- this.paymentStore = paymentStore;
39
- }
40
- async createPaymentPayload(x402Version, requirements) {
41
- const entry = this.paymentStore.get(requirements);
42
- if (!entry || entry.version !== 2) {
43
- throw new Error("No v2 payment authorization found for requirements");
44
- }
45
- // Clean up after retrieval
46
- this.paymentStore.delete(requirements);
47
- // Convert v1 payment to v2 format
48
- return v1PayloadToV2(entry.authorization.payment, entry.context);
49
- }
50
- }
51
- /**
52
- * Wraps an x402Client to use an ampersend-sdk treasurer for payment decisions.
53
- *
54
- * This adapter integrates ampersend-sdk treasurers with Coinbase's x402 SDK,
55
- * allowing you to use sophisticated payment authorization logic (budgets, policies,
56
- * approvals) with the standard x402 HTTP client ecosystem.
57
- *
58
- * Supports both v1 and v2 x402 protocols. The underlying wallets produce v1 payment
59
- * payloads which are automatically converted to v2 format when needed.
60
- *
61
- * @param client - The x402Client instance to wrap
62
- * @param treasurer - The X402Treasurer that handles payment authorization
63
- * @param networks - Array of v1 network names to register (e.g., 'base', 'base-sepolia')
64
- * @returns The configured x402Client instance (same instance, mutated)
65
- *
66
- * @example
67
- * ```typescript
68
- * import { x402Client } from '@x402/core/client'
69
- * import { wrapFetchWithPayment } from '@x402/fetch'
70
- * import { wrapWithAmpersend, NaiveTreasurer, AccountWallet } from '@ampersend_ai/ampersend-sdk'
71
- *
72
- * const wallet = AccountWallet.fromPrivateKey('0x...')
73
- * const treasurer = new NaiveTreasurer(wallet)
74
- *
75
- * const client = wrapWithAmpersend(
76
- * new x402Client(),
77
- * treasurer,
78
- * ['base', 'base-sepolia']
79
- * )
80
- *
81
- * const fetchWithPay = wrapFetchWithPayment(fetch, client)
82
- * const response = await fetchWithPay('https://paid-api.com/endpoint')
83
- * ```
84
- */
85
- export function wrapWithAmpersend(client, treasurer, networks) {
86
- // Shared store for correlating payments between hooks and scheme clients
87
- // Keyed by the original requirements object (v1 or v2)
88
- const paymentStore = new WeakMap();
89
- // Create scheme clients for both v1 and v2
90
- const schemeClientV1 = new TreasurerSchemeClientV1(paymentStore);
91
- const schemeClientV2 = new TreasurerSchemeClientV2(paymentStore);
92
- // Register for both v1 and v2 protocols on each network
93
- for (const network of networks) {
94
- // v1: uses network names like "base-sepolia"
95
- client.registerV1(network, schemeClientV1);
96
- // v2: uses CAIP-2 format like "eip155:84532"
97
- const caip2Network = v1NetworkToCaip2(network);
98
- client.register(caip2Network, schemeClientV2);
99
- }
100
- // Track authorization for status updates (keyed by original requirements)
101
- const authorizationByRequirements = new WeakMap();
102
- // beforePaymentCreation: Consult treasurer for payment authorization
103
- client.onBeforePaymentCreation(async (context) => {
104
- const originalRequirements = context.selectedRequirements;
105
- const paymentRequired = context.paymentRequired;
106
- if (paymentRequired.x402Version !== 1 && paymentRequired.x402Version !== 2) {
107
- throw new Error(`Unsupported x402 version: ${paymentRequired.x402Version}`);
108
- }
109
- // Convert v2 requirements to v1 for treasurer (which speaks v1 internally)
110
- let v1Requirements;
111
- let storeEntry;
112
- if (paymentRequired.x402Version === 2) {
113
- // v2 path: convert to v1 for treasurer
114
- v1Requirements = v2RequirementsToV1(originalRequirements, paymentRequired.resource);
115
- const authorization = await treasurer.onPaymentRequired([v1Requirements], {
116
- method: "http",
117
- params: {
118
- resource: paymentRequired.resource.url,
119
- },
120
- });
121
- if (!authorization) {
122
- return { abort: true, reason: "Payment declined by treasurer" };
123
- }
124
- // Store v2 entry with context for payload conversion
125
- storeEntry = {
126
- version: 2,
127
- authorization,
128
- context: {
129
- resource: paymentRequired.resource,
130
- originalRequirements,
131
- },
132
- };
133
- paymentStore.set(originalRequirements, storeEntry);
134
- authorizationByRequirements.set(originalRequirements, authorization);
135
- }
136
- else {
137
- // v1 path: pass directly to treasurer
138
- v1Requirements = originalRequirements;
139
- const authorization = await treasurer.onPaymentRequired([v1Requirements], {
140
- method: "http",
141
- params: {
142
- resource: paymentRequired.resource,
143
- },
144
- });
145
- if (!authorization) {
146
- return { abort: true, reason: "Payment declined by treasurer" };
147
- }
148
- // Store v1 entry
149
- storeEntry = {
150
- version: 1,
151
- authorization,
152
- };
153
- paymentStore.set(originalRequirements, storeEntry);
154
- authorizationByRequirements.set(originalRequirements, authorization);
155
- }
156
- return;
157
- });
158
- // afterPaymentCreation: Notify treasurer payment is being sent
159
- client.onAfterPaymentCreation(async (context) => {
160
- const paymentRequired = context.paymentRequired;
161
- const authorization = authorizationByRequirements.get(context.selectedRequirements);
162
- if (authorization) {
163
- // Extract resource URL (v2 has resource.url, v1 has resource as string)
164
- const resourceUrl = typeof paymentRequired.resource === "object" ? paymentRequired.resource.url : paymentRequired.resource;
165
- await treasurer.onStatus("sending", authorization, {
166
- method: "http",
167
- params: {
168
- resource: resourceUrl,
169
- },
170
- });
171
- }
172
- });
173
- // onPaymentCreationFailure: Notify treasurer of error
174
- client.onPaymentCreationFailure(async (context) => {
175
- const paymentRequired = context.paymentRequired;
176
- const authorization = authorizationByRequirements.get(context.selectedRequirements);
177
- if (authorization) {
178
- // Extract resource URL (v2 has resource.url, v1 has resource as string)
179
- const resourceUrl = typeof paymentRequired.resource === "object" ? paymentRequired.resource.url : paymentRequired.resource;
180
- await treasurer.onStatus("error", authorization, {
181
- method: "http",
182
- params: {
183
- resource: resourceUrl,
184
- error: context.error.message,
185
- },
186
- });
187
- }
188
- // Don't recover - let the error propagate
189
- return;
190
- });
191
- return client;
192
- }
193
- //# sourceMappingURL=adapter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../../src/x402/http/adapter.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,kBAAkB,EAAyB,MAAM,iBAAiB,CAAA;AAiB5G;;;;;;GAMG;AACH,MAAM,uBAAuB;IAGE;IAFpB,MAAM,GAAG,OAAO,CAAA;IAEzB,YAA6B,YAAyC;QAAzC,iBAAY,GAAZ,YAAY,CAA6B;IAAG,CAAC;IAE1E,KAAK,CAAC,oBAAoB,CACxB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C"}
@@ -1,56 +0,0 @@
1
- /**
2
- * Transport-agnostic v2 ↔ v1 protocol adapter.
3
- *
4
- * The x402 v2 protocol uses CAIP-2 network identifiers, a different field
5
- * layout (amount vs maxAmountRequired, resource/accepted envelope), and the
6
- * same underlying ERC-3009 signatures. Internally the SDK speaks v1 everywhere
7
- * (treasurer, wallet, types), so this module converts at the protocol boundary.
8
- */
9
- import type { PaymentPayload as V2PaymentPayload, PaymentRequired as V2PaymentRequired, PaymentRequirements as V2PaymentRequirements } from "@x402/core/types";
10
- import { type PaymentPayload, type PaymentRequirements } from "x402/types";
11
- /**
12
- * Convert a v1 network name to CAIP-2 format.
13
- *
14
- * @example
15
- * v1NetworkToCaip2("base-sepolia") // => "eip155:84532"
16
- * v1NetworkToCaip2("base") // => "eip155:8453"
17
- */
18
- export declare function v1NetworkToCaip2(network: string): `eip155:${number}`;
19
- /**
20
- * Extract chain ID from a CAIP-2 identifier or passthrough.
21
- *
22
- * @example
23
- * parseCaip2ChainId("eip155:8453") // => 8453
24
- * parseCaip2ChainId("8453") // => 8453
25
- */
26
- export declare function parseCaip2ChainId(network: string): number;
27
- /**
28
- * Convert a CAIP-2 network identifier to v1 network name.
29
- *
30
- * @example
31
- * caip2ToV1Network("eip155:84532") // => "base-sepolia"
32
- * caip2ToV1Network("eip155:8453") // => "base"
33
- */
34
- export declare function caip2ToV1Network(network: string): string;
35
- /**
36
- * Original v2 data preserved for building the outbound payment.
37
- */
38
- export interface V2PaymentContext {
39
- /** The resource info from the v2 payment required response */
40
- resource: V2PaymentRequired["resource"];
41
- /** The original v2 requirements for reconstructing the accepted field */
42
- originalRequirements: V2PaymentRequirements;
43
- }
44
- /**
45
- * Convert v2 PaymentRequirements to v1 format.
46
- *
47
- * This allows the treasurer and wallet (which speak v1) to process v2 requirements.
48
- */
49
- export declare function v2RequirementsToV1(v2Req: V2PaymentRequirements, resource: V2PaymentRequired["resource"]): PaymentRequirements;
50
- /**
51
- * Build a v2 PaymentPayload from a v1 payment payload.
52
- *
53
- * The accepted field is reconstructed from the original v2 requirements.
54
- */
55
- export declare function v1PayloadToV2(v1Payload: PaymentPayload, context: V2PaymentContext): Pick<V2PaymentPayload, "x402Version" | "payload"> & Partial<V2PaymentPayload>;
56
- //# sourceMappingURL=v2-adapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"v2-adapter.d.ts","sourceRoot":"","sources":["../../../src/x402/http/v2-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,cAAc,IAAI,gBAAgB,EAClC,eAAe,IAAI,iBAAiB,EACpC,mBAAmB,IAAI,qBAAqB,EAC7C,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAyC,KAAK,cAAc,EAAE,KAAK,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAQjH;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,MAAM,EAAE,CAMpE;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAIzD;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAOxD;AAMD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,8DAA8D;IAC9D,QAAQ,EAAE,iBAAiB,CAAC,UAAU,CAAC,CAAA;IACvC,yEAAyE;IACzE,oBAAoB,EAAE,qBAAqB,CAAA;CAC5C;AAMD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,qBAAqB,EAC5B,QAAQ,EAAE,iBAAiB,CAAC,UAAU,CAAC,GACtC,mBAAmB,CAerB;AAMD;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,cAAc,EACzB,OAAO,EAAE,gBAAgB,GACxB,IAAI,CAAC,gBAAgB,EAAE,aAAa,GAAG,SAAS,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAO/E"}