@ampersend_ai/ampersend-sdk 0.0.16 → 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.
- package/dist/ampersend/approval.d.ts +8 -8
- package/dist/ampersend/approval.d.ts.map +1 -1
- package/dist/ampersend/approval.js +10 -15
- package/dist/ampersend/approval.js.map +1 -1
- package/dist/ampersend/client.d.ts +4 -9
- package/dist/ampersend/client.d.ts.map +1 -1
- package/dist/ampersend/client.js +25 -21
- package/dist/ampersend/client.js.map +1 -1
- package/dist/ampersend/treasurer.d.ts +14 -79
- package/dist/ampersend/treasurer.d.ts.map +1 -1
- package/dist/ampersend/treasurer.js +34 -102
- package/dist/ampersend/treasurer.js.map +1 -1
- package/dist/ampersend/types.d.ts +356 -338
- package/dist/ampersend/types.d.ts.map +1 -1
- package/dist/ampersend/types.js +120 -150
- package/dist/ampersend/types.js.map +1 -1
- package/dist/ampersend/zod-bridge.d.ts +15 -0
- package/dist/ampersend/zod-bridge.d.ts.map +1 -0
- package/dist/ampersend/zod-bridge.js +29 -0
- package/dist/ampersend/zod-bridge.js.map +1 -0
- package/dist/cli/commands/fetch.d.ts.map +1 -1
- package/dist/cli/commands/fetch.js.map +1 -1
- package/dist/cli/commands/setup.d.ts +4 -0
- package/dist/cli/commands/setup.d.ts.map +1 -1
- package/dist/cli/commands/setup.js +52 -7
- package/dist/cli/commands/setup.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp/client/client.d.ts +0 -3
- package/dist/mcp/client/client.d.ts.map +1 -1
- package/dist/mcp/client/client.js +10 -14
- package/dist/mcp/client/client.js.map +1 -1
- package/dist/mcp/client/factory.d.ts +0 -2
- package/dist/mcp/client/factory.d.ts.map +1 -1
- package/dist/mcp/client/factory.js +0 -3
- package/dist/mcp/client/factory.js.map +1 -1
- package/dist/mcp/client/middleware.d.ts +0 -3
- package/dist/mcp/client/middleware.d.ts.map +1 -1
- package/dist/mcp/client/middleware.js +9 -12
- package/dist/mcp/client/middleware.js.map +1 -1
- package/dist/mcp/client/protocol.d.ts +81 -85
- package/dist/mcp/client/protocol.d.ts.map +1 -1
- package/dist/mcp/client/protocol.js +17 -15
- package/dist/mcp/client/protocol.js.map +1 -1
- package/dist/mcp/client/types.d.ts +6 -19
- package/dist/mcp/client/types.d.ts.map +1 -1
- package/dist/mcp/proxy/cli.d.ts.map +1 -1
- package/dist/mcp/proxy/cli.js +0 -1
- package/dist/mcp/proxy/cli.js.map +1 -1
- package/dist/mcp/proxy/env.d.ts +0 -9
- package/dist/mcp/proxy/env.d.ts.map +1 -1
- package/dist/mcp/proxy/env.js +0 -2
- package/dist/mcp/proxy/env.js.map +1 -1
- package/dist/mcp/proxy/factory.d.ts +0 -2
- package/dist/mcp/proxy/factory.d.ts.map +1 -1
- package/dist/mcp/proxy/factory.js +0 -3
- package/dist/mcp/proxy/factory.js.map +1 -1
- package/dist/mcp/server/fastmcp/middleware.d.ts +9 -29
- package/dist/mcp/server/fastmcp/middleware.d.ts.map +1 -1
- package/dist/mcp/server/fastmcp/middleware.js +10 -42
- package/dist/mcp/server/fastmcp/middleware.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/x402/envelopes.d.ts +62 -0
- package/dist/x402/envelopes.d.ts.map +1 -0
- package/dist/x402/envelopes.js +73 -0
- package/dist/x402/envelopes.js.map +1 -0
- package/dist/x402/http/client.d.ts +37 -0
- package/dist/x402/http/client.d.ts.map +1 -0
- package/dist/x402/http/client.js +159 -0
- package/dist/x402/http/client.js.map +1 -0
- package/dist/x402/http/factory.d.ts +11 -25
- package/dist/x402/http/factory.d.ts.map +1 -1
- package/dist/x402/http/factory.js +11 -22
- package/dist/x402/http/factory.js.map +1 -1
- package/dist/x402/http/index.d.ts +4 -1
- package/dist/x402/http/index.d.ts.map +1 -1
- package/dist/x402/http/index.js +2 -1
- package/dist/x402/http/index.js.map +1 -1
- package/dist/x402/index.d.ts +4 -1
- package/dist/x402/index.d.ts.map +1 -1
- package/dist/x402/index.js +3 -2
- package/dist/x402/index.js.map +1 -1
- package/dist/x402/treasurer.d.ts +22 -47
- package/dist/x402/treasurer.d.ts.map +1 -1
- package/dist/x402/treasurers/naive.d.ts +4 -40
- package/dist/x402/treasurers/naive.d.ts.map +1 -1
- package/dist/x402/treasurers/naive.js +7 -44
- package/dist/x402/treasurers/naive.js.map +1 -1
- package/dist/x402/wallet.d.ts +7 -32
- package/dist/x402/wallet.d.ts.map +1 -1
- package/dist/x402/wallet.js +0 -3
- package/dist/x402/wallet.js.map +1 -1
- package/dist/x402/wallets/account/wallet.d.ts +9 -30
- package/dist/x402/wallets/account/wallet.d.ts.map +1 -1
- package/dist/x402/wallets/account/wallet.js +30 -37
- package/dist/x402/wallets/account/wallet.js.map +1 -1
- package/dist/x402/wallets/index.d.ts +0 -1
- package/dist/x402/wallets/index.d.ts.map +1 -1
- package/dist/x402/wallets/index.js +0 -1
- package/dist/x402/wallets/index.js.map +1 -1
- package/dist/x402/wallets/smart-account/chain.d.ts +4 -0
- package/dist/x402/wallets/smart-account/chain.d.ts.map +1 -0
- package/dist/x402/wallets/smart-account/chain.js +12 -0
- package/dist/x402/wallets/smart-account/chain.js.map +1 -0
- package/dist/x402/wallets/smart-account/cosigned.d.ts +7 -33
- package/dist/x402/wallets/smart-account/cosigned.d.ts.map +1 -1
- package/dist/x402/wallets/smart-account/cosigned.js +28 -51
- package/dist/x402/wallets/smart-account/cosigned.js.map +1 -1
- package/dist/x402/wallets/smart-account/exact.d.ts +6 -20
- package/dist/x402/wallets/smart-account/exact.d.ts.map +1 -1
- package/dist/x402/wallets/smart-account/exact.js +33 -43
- package/dist/x402/wallets/smart-account/exact.js.map +1 -1
- package/dist/x402/wallets/smart-account/wallet.d.ts +6 -39
- package/dist/x402/wallets/smart-account/wallet.d.ts.map +1 -1
- package/dist/x402/wallets/smart-account/wallet.js +9 -36
- package/dist/x402/wallets/smart-account/wallet.js.map +1 -1
- package/package.json +8 -6
- package/dist/x402/http/adapter.d.ts +0 -38
- package/dist/x402/http/adapter.d.ts.map +0 -1
- package/dist/x402/http/adapter.js +0 -199
- package/dist/x402/http/adapter.js.map +0 -1
- package/dist/x402/http/v2-adapter.d.ts +0 -56
- package/dist/x402/http/v2-adapter.d.ts.map +0 -1
- package/dist/x402/http/v2-adapter.js +0 -94
- package/dist/x402/http/v2-adapter.js.map +0 -1
|
@@ -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
|
-
*
|
|
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
|
-
|
|
37
|
-
|
|
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(
|
|
27
|
+
return await createCoSignedPayment(instruction, this.config, serverAuthorization);
|
|
51
28
|
}
|
|
52
|
-
|
|
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;
|
|
1
|
+
{"version":3,"file":"wallet.js","sourceRoot":"","sources":["../../../../src/x402/wallets/smart-account/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,MAAM,MAAM,CAAA;AAE7C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAA;AAC3F,OAAO,EAAE,UAAU,EAAsD,MAAM,oBAAoB,CAAA;AAEnG,OAAO,EAAE,WAAW,EAAmB,MAAM,iBAAiB,CAAA;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAW/C;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IACZ,MAAM,CAAuF;IAE9G,YAAY,MAA0B;QACpC,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,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,8 @@
|
|
|
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.
|
|
4
|
+
"version": "0.0.21",
|
|
5
|
+
"license": "Apache-2.0",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
7
8
|
"url": "https://github.com/edgeandnode/ampersend-sdk.git"
|
|
@@ -45,7 +46,8 @@
|
|
|
45
46
|
},
|
|
46
47
|
"files": [
|
|
47
48
|
"dist",
|
|
48
|
-
"README.md"
|
|
49
|
+
"README.md",
|
|
50
|
+
"LICENSE"
|
|
49
51
|
],
|
|
50
52
|
"scripts": {
|
|
51
53
|
"build": "tsc -b tsconfig.build.json --force",
|
|
@@ -62,16 +64,16 @@
|
|
|
62
64
|
"@modelcontextprotocol/sdk": "npm:@ampersend_ai/modelcontextprotocol-sdk@^1.20.1",
|
|
63
65
|
"@rhinestone/module-sdk": "^0.4.0",
|
|
64
66
|
"@types/express": "^5.0.6",
|
|
65
|
-
"@x402/core": "
|
|
66
|
-
"@x402/
|
|
67
|
+
"@x402/core": "2.9.0",
|
|
68
|
+
"@x402/evm": "2.9.0",
|
|
69
|
+
"@x402/fetch": "2.9.0",
|
|
67
70
|
"async-mutex": "^0.5.0",
|
|
68
71
|
"commander": "^12.1.0",
|
|
69
|
-
"effect": "^3.
|
|
72
|
+
"effect": "^3.21.0",
|
|
70
73
|
"express": "^5.2.1",
|
|
71
74
|
"fastmcp": "npm:@ampersend_ai/fastmcp@^1.0.0",
|
|
72
75
|
"siwe": "^3.0.0",
|
|
73
76
|
"viem": "^2.45.0",
|
|
74
|
-
"x402": "^0.6.6",
|
|
75
77
|
"zod": "^3.24.2"
|
|
76
78
|
},
|
|
77
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;AAyFnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,UAAU,CA6HnH"}
|
|
@@ -1,199 +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
|
-
* Detect if requirements are v2 format (has 'amount' field instead of 'maxAmountRequired').
|
|
53
|
-
*/
|
|
54
|
-
function isV2Requirements(requirements) {
|
|
55
|
-
return (typeof requirements === "object" &&
|
|
56
|
-
requirements !== null &&
|
|
57
|
-
"amount" in requirements &&
|
|
58
|
-
!("maxAmountRequired" in requirements));
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Wraps an x402Client to use an ampersend-sdk treasurer for payment decisions.
|
|
62
|
-
*
|
|
63
|
-
* This adapter integrates ampersend-sdk treasurers with Coinbase's x402 SDK,
|
|
64
|
-
* allowing you to use sophisticated payment authorization logic (budgets, policies,
|
|
65
|
-
* approvals) with the standard x402 HTTP client ecosystem.
|
|
66
|
-
*
|
|
67
|
-
* Supports both v1 and v2 x402 protocols. The underlying wallets produce v1 payment
|
|
68
|
-
* payloads which are automatically converted to v2 format when needed.
|
|
69
|
-
*
|
|
70
|
-
* @param client - The x402Client instance to wrap
|
|
71
|
-
* @param treasurer - The X402Treasurer that handles payment authorization
|
|
72
|
-
* @param networks - Array of v1 network names to register (e.g., 'base', 'base-sepolia')
|
|
73
|
-
* @returns The configured x402Client instance (same instance, mutated)
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* import { x402Client } from '@x402/core/client'
|
|
78
|
-
* import { wrapFetchWithPayment } from '@x402/fetch'
|
|
79
|
-
* import { wrapWithAmpersend, NaiveTreasurer, AccountWallet } from '@ampersend_ai/ampersend-sdk'
|
|
80
|
-
*
|
|
81
|
-
* const wallet = AccountWallet.fromPrivateKey('0x...')
|
|
82
|
-
* const treasurer = new NaiveTreasurer(wallet)
|
|
83
|
-
*
|
|
84
|
-
* const client = wrapWithAmpersend(
|
|
85
|
-
* new x402Client(),
|
|
86
|
-
* treasurer,
|
|
87
|
-
* ['base', 'base-sepolia']
|
|
88
|
-
* )
|
|
89
|
-
*
|
|
90
|
-
* const fetchWithPay = wrapFetchWithPayment(fetch, client)
|
|
91
|
-
* const response = await fetchWithPay('https://paid-api.com/endpoint')
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
export function wrapWithAmpersend(client, treasurer, networks) {
|
|
95
|
-
// Shared store for correlating payments between hooks and scheme clients
|
|
96
|
-
// Keyed by the original requirements object (v1 or v2)
|
|
97
|
-
const paymentStore = new WeakMap();
|
|
98
|
-
// Create scheme clients for both v1 and v2
|
|
99
|
-
const schemeClientV1 = new TreasurerSchemeClientV1(paymentStore);
|
|
100
|
-
const schemeClientV2 = new TreasurerSchemeClientV2(paymentStore);
|
|
101
|
-
// Register for both v1 and v2 protocols on each network
|
|
102
|
-
for (const network of networks) {
|
|
103
|
-
// v1: uses network names like "base-sepolia"
|
|
104
|
-
client.registerV1(network, schemeClientV1);
|
|
105
|
-
// v2: uses CAIP-2 format like "eip155:84532"
|
|
106
|
-
const caip2Network = v1NetworkToCaip2(network);
|
|
107
|
-
client.register(caip2Network, schemeClientV2);
|
|
108
|
-
}
|
|
109
|
-
// Track authorization for status updates (keyed by original requirements)
|
|
110
|
-
const authorizationByRequirements = new WeakMap();
|
|
111
|
-
// beforePaymentCreation: Consult treasurer for payment authorization
|
|
112
|
-
client.onBeforePaymentCreation(async (context) => {
|
|
113
|
-
const originalRequirements = context.selectedRequirements;
|
|
114
|
-
const paymentRequired = context.paymentRequired;
|
|
115
|
-
// Convert v2 requirements to v1 for treasurer (which speaks v1 internally)
|
|
116
|
-
let v1Requirements;
|
|
117
|
-
let storeEntry;
|
|
118
|
-
if (isV2Requirements(originalRequirements)) {
|
|
119
|
-
// v2 path: convert to v1 for treasurer
|
|
120
|
-
v1Requirements = v2RequirementsToV1(originalRequirements, paymentRequired.resource);
|
|
121
|
-
const authorization = await treasurer.onPaymentRequired([v1Requirements], {
|
|
122
|
-
method: "http",
|
|
123
|
-
params: {
|
|
124
|
-
resource: paymentRequired.resource.url,
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
if (!authorization) {
|
|
128
|
-
return { abort: true, reason: "Payment declined by treasurer" };
|
|
129
|
-
}
|
|
130
|
-
// Store v2 entry with context for payload conversion
|
|
131
|
-
storeEntry = {
|
|
132
|
-
version: 2,
|
|
133
|
-
authorization,
|
|
134
|
-
context: {
|
|
135
|
-
resource: paymentRequired.resource,
|
|
136
|
-
originalRequirements,
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
paymentStore.set(originalRequirements, storeEntry);
|
|
140
|
-
authorizationByRequirements.set(originalRequirements, authorization);
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
// v1 path: pass directly to treasurer
|
|
144
|
-
v1Requirements = originalRequirements;
|
|
145
|
-
const authorization = await treasurer.onPaymentRequired([v1Requirements], {
|
|
146
|
-
method: "http",
|
|
147
|
-
params: {
|
|
148
|
-
resource: paymentRequired.resource,
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
if (!authorization) {
|
|
152
|
-
return { abort: true, reason: "Payment declined by treasurer" };
|
|
153
|
-
}
|
|
154
|
-
// Store v1 entry
|
|
155
|
-
storeEntry = {
|
|
156
|
-
version: 1,
|
|
157
|
-
authorization,
|
|
158
|
-
};
|
|
159
|
-
paymentStore.set(originalRequirements, storeEntry);
|
|
160
|
-
authorizationByRequirements.set(originalRequirements, authorization);
|
|
161
|
-
}
|
|
162
|
-
return;
|
|
163
|
-
});
|
|
164
|
-
// afterPaymentCreation: Notify treasurer payment is being sent
|
|
165
|
-
client.onAfterPaymentCreation(async (context) => {
|
|
166
|
-
const paymentRequired = context.paymentRequired;
|
|
167
|
-
const authorization = authorizationByRequirements.get(context.selectedRequirements);
|
|
168
|
-
if (authorization) {
|
|
169
|
-
// Extract resource URL (v2 has resource.url, v1 has resource as string)
|
|
170
|
-
const resourceUrl = typeof paymentRequired.resource === "object" ? paymentRequired.resource.url : paymentRequired.resource;
|
|
171
|
-
await treasurer.onStatus("sending", authorization, {
|
|
172
|
-
method: "http",
|
|
173
|
-
params: {
|
|
174
|
-
resource: resourceUrl,
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
// onPaymentCreationFailure: Notify treasurer of error
|
|
180
|
-
client.onPaymentCreationFailure(async (context) => {
|
|
181
|
-
const paymentRequired = context.paymentRequired;
|
|
182
|
-
const authorization = authorizationByRequirements.get(context.selectedRequirements);
|
|
183
|
-
if (authorization) {
|
|
184
|
-
// Extract resource URL (v2 has resource.url, v1 has resource as string)
|
|
185
|
-
const resourceUrl = typeof paymentRequired.resource === "object" ? paymentRequired.resource.url : paymentRequired.resource;
|
|
186
|
-
await treasurer.onStatus("error", authorization, {
|
|
187
|
-
method: "http",
|
|
188
|
-
params: {
|
|
189
|
-
resource: resourceUrl,
|
|
190
|
-
error: context.error.message,
|
|
191
|
-
},
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
// Don't recover - let the error propagate
|
|
195
|
-
return;
|
|
196
|
-
});
|
|
197
|
-
return client;
|
|
198
|
-
}
|
|
199
|
-
//# 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,WAAmB,EACnB,YAAiC;QAEjC,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;QACjD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAA;QACpE,CAAC;QAED,2BAA2B;QAC3B,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;QAEtC,OAAO;YACL,WAAW;YACX,MAAM,EAAE,KAAK,CAAC,aAAa,CAAC,OAAO,CAAC,MAAM;YAC1C,OAAO,EAAE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AS,CAAC,QAAQ,CAAC,SAAS,EAAE,aAAa,EAAE;gBACjD,MAAM,EAAE,MAAM;gBACd,MAAM,EAAE;oBACN,QAAQ,EAAE,WAAW;iBACtB;aACF,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,sDAAsD;IACtD,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,OAAsC,EAAE,EAAE;QAC/E,MAAM,eAAe,GAAG,OAAO,CAAC,eAAoC,CAAA;QACpE,MAAM,aAAa,GAAG,2BAA2B,CAAC,GAAG,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAA;QACnF,IAAI,aAAa,EAAE,CAAC;YAClB,wEAAwE;YACxE,MAAM,WAAW,GACf,OAAO,eAAe,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,eAAe,CAAC,QAAQ,CAAA;YACxG,MAAM,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,aAAa,EAAE;gBAC/C,MAAM,EAAE,MAAM;gBACd,MAAM,EAAE;oBACN,QAAQ,EAAE,WAAW;oBACrB,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO;iBAC7B;aACF,CAAC,CAAA;QACJ,CAAC;QAED,0CAA0C;QAC1C,OAAM;IACR,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -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"}
|
|
@@ -1,94 +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 { ChainIdToNetwork, EvmNetworkToChainId } from "x402/types";
|
|
10
|
-
const DEFAULT_MAX_TIMEOUT_SECONDS = 300;
|
|
11
|
-
// ============================================================================
|
|
12
|
-
// Network Conversion
|
|
13
|
-
// ============================================================================
|
|
14
|
-
/**
|
|
15
|
-
* Convert a v1 network name to CAIP-2 format.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* v1NetworkToCaip2("base-sepolia") // => "eip155:84532"
|
|
19
|
-
* v1NetworkToCaip2("base") // => "eip155:8453"
|
|
20
|
-
*/
|
|
21
|
-
export function v1NetworkToCaip2(network) {
|
|
22
|
-
const chainId = EvmNetworkToChainId.get(network);
|
|
23
|
-
if (chainId === undefined) {
|
|
24
|
-
throw new Error(`Unknown v1 network: ${network}`);
|
|
25
|
-
}
|
|
26
|
-
return `eip155:${chainId}`;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Extract chain ID from a CAIP-2 identifier or passthrough.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* parseCaip2ChainId("eip155:8453") // => 8453
|
|
33
|
-
* parseCaip2ChainId("8453") // => 8453
|
|
34
|
-
*/
|
|
35
|
-
export function parseCaip2ChainId(network) {
|
|
36
|
-
const parts = network.split(":");
|
|
37
|
-
const chainIdStr = parts.length > 1 ? parts[1] : parts[0];
|
|
38
|
-
return parseInt(chainIdStr, 10);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Convert a CAIP-2 network identifier to v1 network name.
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* caip2ToV1Network("eip155:84532") // => "base-sepolia"
|
|
45
|
-
* caip2ToV1Network("eip155:8453") // => "base"
|
|
46
|
-
*/
|
|
47
|
-
export function caip2ToV1Network(network) {
|
|
48
|
-
const chainId = parseCaip2ChainId(network);
|
|
49
|
-
const v1Network = ChainIdToNetwork[chainId];
|
|
50
|
-
if (!v1Network) {
|
|
51
|
-
throw new Error(`Unknown chain ID: ${chainId}`);
|
|
52
|
-
}
|
|
53
|
-
return v1Network;
|
|
54
|
-
}
|
|
55
|
-
// ============================================================================
|
|
56
|
-
// Inbound: v2 → v1 Conversion
|
|
57
|
-
// ============================================================================
|
|
58
|
-
/**
|
|
59
|
-
* Convert v2 PaymentRequirements to v1 format.
|
|
60
|
-
*
|
|
61
|
-
* This allows the treasurer and wallet (which speak v1) to process v2 requirements.
|
|
62
|
-
*/
|
|
63
|
-
export function v2RequirementsToV1(v2Req, resource) {
|
|
64
|
-
const v1Network = caip2ToV1Network(v2Req.network);
|
|
65
|
-
return {
|
|
66
|
-
scheme: v2Req.scheme,
|
|
67
|
-
network: v1Network,
|
|
68
|
-
maxAmountRequired: v2Req.amount,
|
|
69
|
-
resource: resource.url,
|
|
70
|
-
description: resource.description || resource.url,
|
|
71
|
-
mimeType: resource.mimeType || "",
|
|
72
|
-
payTo: v2Req.payTo,
|
|
73
|
-
maxTimeoutSeconds: v2Req.maxTimeoutSeconds ?? DEFAULT_MAX_TIMEOUT_SECONDS,
|
|
74
|
-
asset: v2Req.asset,
|
|
75
|
-
extra: v2Req.extra ?? {},
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
// ============================================================================
|
|
79
|
-
// Outbound: v1 → v2 Conversion
|
|
80
|
-
// ============================================================================
|
|
81
|
-
/**
|
|
82
|
-
* Build a v2 PaymentPayload from a v1 payment payload.
|
|
83
|
-
*
|
|
84
|
-
* The accepted field is reconstructed from the original v2 requirements.
|
|
85
|
-
*/
|
|
86
|
-
export function v1PayloadToV2(v1Payload, context) {
|
|
87
|
-
return {
|
|
88
|
-
x402Version: 2,
|
|
89
|
-
resource: context.resource,
|
|
90
|
-
accepted: context.originalRequirements,
|
|
91
|
-
payload: v1Payload.payload,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
//# sourceMappingURL=v2-adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"v2-adapter.js","sourceRoot":"","sources":["../../../src/x402/http/v2-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAOH,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAiD,MAAM,YAAY,CAAA;AAEjH,MAAM,2BAA2B,GAAG,GAAG,CAAA;AAEvC,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC9C,MAAM,OAAO,GAAG,mBAAmB,CAAC,GAAG,CAAC,OAAwD,CAAC,CAAA;IACjG,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,uBAAuB,OAAO,EAAE,CAAC,CAAA;IACnD,CAAC;IACD,OAAO,UAAU,OAAO,EAAE,CAAA;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAe;IAC/C,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAChC,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAA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