@atxp/polygon 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +497 -0
- package/dist/cache.d.ts +19 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +11 -0
- package/dist/cache.js.map +1 -0
- package/dist/directWalletPaymentMaker.d.ts +35 -0
- package/dist/directWalletPaymentMaker.d.ts.map +1 -0
- package/dist/directWalletPaymentMaker.js +143 -0
- package/dist/directWalletPaymentMaker.js.map +1 -0
- package/dist/index.cjs +458 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +145 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +446 -0
- package/dist/index.js.map +1 -0
- package/dist/polygonBrowserAccount.d.ts +40 -0
- package/dist/polygonBrowserAccount.d.ts.map +1 -0
- package/dist/polygonBrowserAccount.js +70 -0
- package/dist/polygonBrowserAccount.js.map +1 -0
- package/dist/polygonServerAccount.d.ts +35 -0
- package/dist/polygonServerAccount.d.ts.map +1 -0
- package/dist/polygonServerAccount.js +78 -0
- package/dist/polygonServerAccount.js.map +1 -0
- package/dist/serverPaymentMaker.d.ts +29 -0
- package/dist/serverPaymentMaker.d.ts.map +1 -0
- package/dist/serverPaymentMaker.js +173 -0
- package/dist/serverPaymentMaker.js.map +1 -0
- package/dist/smartWalletHelpers.d.ts +18 -0
- package/dist/smartWalletHelpers.d.ts.map +1 -0
- package/dist/smartWalletHelpers.js +93 -0
- package/dist/smartWalletHelpers.js.map +1 -0
- package/dist/smartWalletPaymentMaker.d.ts +29 -0
- package/dist/smartWalletPaymentMaker.d.ts.map +1 -0
- package/dist/smartWalletPaymentMaker.js +172 -0
- package/dist/smartWalletPaymentMaker.js.map +1 -0
- package/dist/spendPermissionShim.d.ts +19 -0
- package/dist/spendPermissionShim.d.ts.map +1 -0
- package/dist/spendPermissionShim.js +129 -0
- package/dist/spendPermissionShim.js.map +1 -0
- package/dist/testHelpers.d.ts +17 -0
- package/dist/testHelpers.d.ts.map +1 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ConsoleLogger, ChainEnum, WalletTypeEnum } from '@atxp/common';
|
|
2
|
+
import { DirectWalletPaymentMaker } from './directWalletPaymentMaker.js';
|
|
3
|
+
import { polygon } from 'viem/chains';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Polygon browser account implementation using Direct Wallet mode.
|
|
7
|
+
*
|
|
8
|
+
* Direct Wallet mode:
|
|
9
|
+
* - User signs each transaction with their wallet
|
|
10
|
+
* - User pays gas fees in POL
|
|
11
|
+
* - No smart wallet or gasless transactions
|
|
12
|
+
*
|
|
13
|
+
* Note: Smart Wallet mode is not supported on Polygon because Coinbase CDP
|
|
14
|
+
* does not provide Paymaster services for Polygon mainnet.
|
|
15
|
+
*/
|
|
16
|
+
class PolygonBrowserAccount {
|
|
17
|
+
static async initialize(config) {
|
|
18
|
+
const logger = config.logger || new ConsoleLogger();
|
|
19
|
+
const chainId = config.chainId || polygon.id; // Default to Polygon mainnet
|
|
20
|
+
// Warn if deprecated smart wallet parameters are provided
|
|
21
|
+
if (config.useEphemeralWallet === true) {
|
|
22
|
+
logger.warn('Smart Wallet mode (useEphemeralWallet=true) is not supported on Polygon. Using Direct Wallet mode instead.');
|
|
23
|
+
}
|
|
24
|
+
if (config.allowance !== undefined || config.periodInDays !== undefined) {
|
|
25
|
+
logger.warn('allowance and periodInDays parameters are ignored in Direct Wallet mode.');
|
|
26
|
+
}
|
|
27
|
+
if (config.coinbaseCdpApiKey !== undefined) {
|
|
28
|
+
logger.warn('coinbaseCdpApiKey parameter is ignored in Direct Wallet mode.');
|
|
29
|
+
}
|
|
30
|
+
// Some wallets don't support wallet_connect, so
|
|
31
|
+
// will just continue if it fails
|
|
32
|
+
try {
|
|
33
|
+
await config.provider.request({ method: 'wallet_connect' });
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
// Continue if wallet_connect is not supported
|
|
37
|
+
logger.warn(`wallet_connect not supported, continuing with initialization. ${error}`);
|
|
38
|
+
}
|
|
39
|
+
logger.info(`Initializing Polygon account in Direct Wallet mode for address: ${config.walletAddress}`);
|
|
40
|
+
return new PolygonBrowserAccount(config.walletAddress, config.provider, logger, chainId);
|
|
41
|
+
}
|
|
42
|
+
constructor(walletAddress, provider, logger, chainId = polygon.id) {
|
|
43
|
+
this.walletAddress = walletAddress;
|
|
44
|
+
this.chainId = chainId;
|
|
45
|
+
// Format accountId as network:address
|
|
46
|
+
this.accountId = `polygon:${walletAddress}`;
|
|
47
|
+
this.paymentMakers = [
|
|
48
|
+
new DirectWalletPaymentMaker(walletAddress, provider, logger, chainId)
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
async getSources() {
|
|
52
|
+
// For Polygon, we support both mainnet (137) and Amoy testnet (80002)
|
|
53
|
+
const chain = ChainEnum.Polygon;
|
|
54
|
+
return [{
|
|
55
|
+
address: this.walletAddress,
|
|
56
|
+
chain,
|
|
57
|
+
walletType: WalletTypeEnum.EOA
|
|
58
|
+
}];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Clear cached data (no-op in Direct Wallet mode, kept for backward compatibility)
|
|
62
|
+
* @deprecated This method is a no-op in Direct Wallet mode
|
|
63
|
+
*/
|
|
64
|
+
static clearAllCachedData(_userWalletAddress, _cache) {
|
|
65
|
+
// No-op: Direct Wallet mode doesn't cache any data
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { PolygonBrowserAccount };
|
|
70
|
+
//# sourceMappingURL=polygonBrowserAccount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polygonBrowserAccount.js","sources":["../src/polygonBrowserAccount.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAOA;;;;;;;;;;AAUG;MACU,qBAAqB,CAAA;AAMhC,IAAA,a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AAa,GAAG;YACnB,IAAI,wBAAwB,CAAC,aAAa,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO;SACtE;IACH;AAEA,IAAA,MAAM,UAAU,GAAA;;AAEd,QAAA,MAAM,KAAK,GAAG,SAAS,CAAC,OAAO;AAE/B,QAAA,OAAO,CAAC;gBACN,OAAO,EAAE,IAAI,CAAC,aAAa;gBAC3B,KAAK;gBACL,UAAU,EAAE,cAAc,CAAC;AAC5B,aAAA,CAAC;IACJ;AAEA;;;AAGG;AACH,IAAA,OAAO,kBAAkB,CAAC,kBAA0B,EAAE,MAAgB,EAAA;;IAEtE;AACD;;;;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Account, PaymentMaker, Source } from '@atxp/common';
|
|
2
|
+
import type { AccountId } from '@atxp/common';
|
|
3
|
+
/**
|
|
4
|
+
* Polygon account for server-side/CLI usage
|
|
5
|
+
*
|
|
6
|
+
* This account type works without browser providers and uses direct private key signing.
|
|
7
|
+
* It uses direct wallet signing (similar to BaseAccount) rather than ephemeral wallets
|
|
8
|
+
* and spend permissions.
|
|
9
|
+
*
|
|
10
|
+
* For browser-based applications with wallet providers, use PolygonBrowserAccount.initialize() instead.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Server-side usage
|
|
15
|
+
* const account = new PolygonServerAccount(
|
|
16
|
+
* 'https://polygon-rpc.com',
|
|
17
|
+
* '0x_your_private_key',
|
|
18
|
+
* 137 // Polygon mainnet
|
|
19
|
+
* );
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare class PolygonServerAccount implements Account {
|
|
23
|
+
accountId: AccountId;
|
|
24
|
+
paymentMakers: PaymentMaker[];
|
|
25
|
+
private walletClient;
|
|
26
|
+
private account;
|
|
27
|
+
private chainId;
|
|
28
|
+
constructor(polygonRPCUrl: string, sourceSecretKey: string, chainId?: number);
|
|
29
|
+
private getChain;
|
|
30
|
+
/**
|
|
31
|
+
* Get sources for this account
|
|
32
|
+
*/
|
|
33
|
+
getSources(): Promise<Source[]>;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=polygonServerAccount.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polygonServerAccount.d.ts","sourceRoot":"","sources":["../src/polygonServerAccount.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAQ9C;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,oBAAqB,YAAW,OAAO;IAClD,SAAS,EAAE,SAAS,CAAC;IACrB,aAAa,EAAE,YAAY,EAAE,CAAC;IAC9B,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,OAAO,CAAoB;IACnC,OAAO,CAAC,OAAO,CAAS;gBAEZ,aAAa,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,OAAO,GAAE,MAAY;IAgCjF,OAAO,CAAC,QAAQ;IAWhB;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAUtC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ChainEnum, WalletTypeEnum } from '@atxp/common';
|
|
2
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
3
|
+
import { ServerPaymentMaker } from './serverPaymentMaker.js';
|
|
4
|
+
import { createWalletClient, http } from 'viem';
|
|
5
|
+
import { polygonAmoy, polygon } from 'viem/chains';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Polygon account for server-side/CLI usage
|
|
9
|
+
*
|
|
10
|
+
* This account type works without browser providers and uses direct private key signing.
|
|
11
|
+
* It uses direct wallet signing (similar to BaseAccount) rather than ephemeral wallets
|
|
12
|
+
* and spend permissions.
|
|
13
|
+
*
|
|
14
|
+
* For browser-based applications with wallet providers, use PolygonBrowserAccount.initialize() instead.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Server-side usage
|
|
19
|
+
* const account = new PolygonServerAccount(
|
|
20
|
+
* 'https://polygon-rpc.com',
|
|
21
|
+
* '0x_your_private_key',
|
|
22
|
+
* 137 // Polygon mainnet
|
|
23
|
+
* );
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class PolygonServerAccount {
|
|
27
|
+
constructor(polygonRPCUrl, sourceSecretKey, chainId = 137) {
|
|
28
|
+
if (!polygonRPCUrl) {
|
|
29
|
+
throw new Error('Polygon RPC URL is required');
|
|
30
|
+
}
|
|
31
|
+
if (!sourceSecretKey) {
|
|
32
|
+
throw new Error('Source secret key is required');
|
|
33
|
+
}
|
|
34
|
+
if (!chainId) {
|
|
35
|
+
throw new Error('Chain ID is required');
|
|
36
|
+
}
|
|
37
|
+
this.chainId = chainId;
|
|
38
|
+
this.account = privateKeyToAccount(sourceSecretKey);
|
|
39
|
+
// Determine network name for accountId
|
|
40
|
+
const networkName = chainId === 137 ? 'polygon' : 'polygon_amoy';
|
|
41
|
+
this.accountId = `${networkName}:${this.account.address}`;
|
|
42
|
+
// Get the appropriate chain configuration
|
|
43
|
+
const chain = this.getChain(chainId);
|
|
44
|
+
this.walletClient = createWalletClient({
|
|
45
|
+
account: this.account,
|
|
46
|
+
chain,
|
|
47
|
+
transport: http(polygonRPCUrl),
|
|
48
|
+
});
|
|
49
|
+
this.paymentMakers = [
|
|
50
|
+
new ServerPaymentMaker(polygonRPCUrl, this.walletClient, chainId)
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
getChain(chainId) {
|
|
54
|
+
switch (chainId) {
|
|
55
|
+
case 137:
|
|
56
|
+
return polygon;
|
|
57
|
+
case 80002:
|
|
58
|
+
return polygonAmoy;
|
|
59
|
+
default:
|
|
60
|
+
throw new Error(`Unsupported Polygon chain ID: ${chainId}. Supported: 137 (mainnet), 80002 (Amoy testnet)`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get sources for this account
|
|
65
|
+
*/
|
|
66
|
+
async getSources() {
|
|
67
|
+
// Determine chain enum value
|
|
68
|
+
const chain = this.chainId === 137 ? ChainEnum.Polygon : ChainEnum.PolygonAmoy;
|
|
69
|
+
return [{
|
|
70
|
+
address: this.account.address,
|
|
71
|
+
chain,
|
|
72
|
+
walletType: WalletTypeEnum.EOA
|
|
73
|
+
}];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { PolygonServerAccount };
|
|
78
|
+
//# sourceMappingURL=polygonServerAccount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polygonServerAccount.js","sources":["../src/polygonServerAccount.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;AASA;;;;;;;;;;;;;;;;;;AAkBG;MACU,oBAAoB,CAAA;AAO/B,IAAA,WAAA,CAAY,aAAqB,EAAE,eAAuB,EAAE,UAAkB,GAAG,EAAA;QAC/E,IAAI,CAAC,aAAa,EAAE;AAClB,YAAA,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;QAChD;QACA,IAAI,CAAC,eAAe,EAAE;AACpB,YAAA,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC;QAClD;QACA,IAAI,CAAC,OAAO,EAAE;AACZ,YAAA,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC;QACzC;AAEA,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO;AACtB,QAAA,IAAI,CAAC,OAAO,GAAG,mBAAmB,CAAC,eAAsB,CAAC;;AAG1D,QAAA,MAAM,WAAW,GAAG,OAAO,KAAK,GAAG,GAAG,SAAS,GAAG,cAAc;AAChE,QAAA,IAAI,CAAC,SAAS,GAAG,CAAA,EAAG,WAAW,CAAA,CAAA,EAAI,IAAI,CAAC,OAAO,CAAC,OAAO,CAAA,CAAe;;QAGtE,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;AAEpC,QAAA,IAAI,CAAC,YAAY,GAAG,kBAAkB,CAAC;YACrC,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,KAAK;AACL,YAAA,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC;AAC/B,SAAA,CAAC;QAEF,IAAI,CAAC,aAAa,GAAG;YACnB,IAAI,kBAAkB,CAAC,aAAa,EAAE,IAAI,CAAC,YAAY,EAAE,OAAO;SACjE;IACH;AAEQ,IAAA,QAAQ,CAAC,OAAe,EAAA;QAC9B,QAAQ,OAAO;AACb,YAAA,KAAK,GAAG;AACN,gBAAA,OAAO,OAAO;AAChB,YAAA,KAAK,KAAK;AACR,gBAAA,OAAO,WAAW;AACpB,YAAA;AACE,gBAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,OAAO,CAAA,gDAAA,CAAkD,CAAC;;IAEjH;AAEA;;AAEG;AACH,IAAA,MAAM,UAAU,GAAA;;AAEd,QAAA,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,KAAK,GAAG,GAAG,SAAS,CAAC,OAAO,GAAG,SAAS,CAAC,WAAW;AAE9E,QAAA,OAAO,CAAC;AACN,gBAAA,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO;gBAC7B,KAAK;gBACL,UAAU,EAAE,cAAc,CAAC;AAC5B,aAAA,CAAC;IACJ;AACD;;;;"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { PaymentMaker } from '@atxp/common';
|
|
2
|
+
import { Logger, Currency, AccountId, PaymentIdentifier, Destination } from '@atxp/common';
|
|
3
|
+
import { WalletClient, PublicActions } from "viem";
|
|
4
|
+
type ExtendedWalletClient = WalletClient & PublicActions;
|
|
5
|
+
/**
|
|
6
|
+
* Server-side Polygon payment maker for CLI/backend usage
|
|
7
|
+
* Uses direct private key signing without browser providers.
|
|
8
|
+
* Similar to BasePaymentMaker but for Polygon network.
|
|
9
|
+
*/
|
|
10
|
+
export declare class ServerPaymentMaker implements PaymentMaker {
|
|
11
|
+
protected signingClient: ExtendedWalletClient;
|
|
12
|
+
protected logger: Logger;
|
|
13
|
+
protected chainId: number;
|
|
14
|
+
constructor(polygonRPCUrl: string, walletClient: WalletClient, chainId: number, logger?: Logger);
|
|
15
|
+
getSourceAddress(_params: {
|
|
16
|
+
amount: BigNumber;
|
|
17
|
+
currency: Currency;
|
|
18
|
+
receiver: string;
|
|
19
|
+
memo: string;
|
|
20
|
+
}): string;
|
|
21
|
+
generateJWT({ paymentRequestId, codeChallenge, accountId }: {
|
|
22
|
+
paymentRequestId: string;
|
|
23
|
+
codeChallenge: string;
|
|
24
|
+
accountId?: AccountId | null;
|
|
25
|
+
}): Promise<string>;
|
|
26
|
+
makePayment(destinations: Destination[], _memo: string, _paymentRequestId?: string): Promise<PaymentIdentifier | null>;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=serverPaymentMaker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverPaymentMaker.d.ts","sourceRoot":"","sources":["../src/serverPaymentMaker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAG3F,OAAO,EAGL,YAAY,EACZ,aAAa,EAEd,MAAM,MAAM,CAAC;AAId,KAAK,oBAAoB,GAAG,YAAY,GAAG,aAAa,CAAC;AA6CzD;;;;GAIG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACrD,SAAS,CAAC,aAAa,EAAE,oBAAoB,CAAC;IAC9C,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;gBAEd,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;IAmB/F,gBAAgB,CAAC,OAAO,EAAE;QAAC,MAAM,EAAE,SAAS,CAAC;QAAC,QAAQ,EAAE,QAAQ,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAC,GAAG,MAAM;IAIpG,WAAW,CAAC,EAAC,gBAAgB,EAAE,aAAa,EAAE,SAAS,EAAC,EAAE;QAAC,gBAAgB,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,SAAS,GAAG,IAAI,CAAA;KAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IAwC3J,WAAW,CAAC,YAAY,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,iBAAiB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;CA8E7H"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { ConsoleLogger } from '@atxp/common';
|
|
2
|
+
import { getPolygonUSDCAddress } from '@atxp/client';
|
|
3
|
+
import { publicActions, encodeFunctionData } from 'viem';
|
|
4
|
+
|
|
5
|
+
// Helper function to convert to base64url that works in both Node.js and browsers
|
|
6
|
+
function toBase64Url(data) {
|
|
7
|
+
// Convert string to base64
|
|
8
|
+
const base64 = typeof Buffer !== 'undefined'
|
|
9
|
+
? Buffer.from(data).toString('base64')
|
|
10
|
+
: btoa(data);
|
|
11
|
+
// Convert base64 to base64url
|
|
12
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
13
|
+
}
|
|
14
|
+
const USDC_DECIMALS = 6;
|
|
15
|
+
const ERC20_ABI = [
|
|
16
|
+
{
|
|
17
|
+
constant: false,
|
|
18
|
+
inputs: [
|
|
19
|
+
{ name: "_to", type: "address" },
|
|
20
|
+
{ name: "_value", type: "uint256" },
|
|
21
|
+
],
|
|
22
|
+
name: "transfer",
|
|
23
|
+
outputs: [{ name: "", type: "bool" }],
|
|
24
|
+
type: "function",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"constant": true,
|
|
28
|
+
"inputs": [
|
|
29
|
+
{
|
|
30
|
+
"name": "_owner",
|
|
31
|
+
"type": "address"
|
|
32
|
+
}
|
|
33
|
+
],
|
|
34
|
+
"name": "balanceOf",
|
|
35
|
+
"outputs": [
|
|
36
|
+
{
|
|
37
|
+
"name": "balance",
|
|
38
|
+
"type": "uint256"
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
"payable": false,
|
|
42
|
+
"stateMutability": "view",
|
|
43
|
+
"type": "function"
|
|
44
|
+
}
|
|
45
|
+
];
|
|
46
|
+
/**
|
|
47
|
+
* Server-side Polygon payment maker for CLI/backend usage
|
|
48
|
+
* Uses direct private key signing without browser providers.
|
|
49
|
+
* Similar to BasePaymentMaker but for Polygon network.
|
|
50
|
+
*/
|
|
51
|
+
class ServerPaymentMaker {
|
|
52
|
+
constructor(polygonRPCUrl, walletClient, chainId, logger) {
|
|
53
|
+
if (!polygonRPCUrl) {
|
|
54
|
+
throw new Error('polygonRPCUrl was empty');
|
|
55
|
+
}
|
|
56
|
+
if (!walletClient) {
|
|
57
|
+
throw new Error('walletClient was empty');
|
|
58
|
+
}
|
|
59
|
+
if (!walletClient.account) {
|
|
60
|
+
throw new Error('walletClient.account was empty');
|
|
61
|
+
}
|
|
62
|
+
if (!chainId) {
|
|
63
|
+
throw new Error('chainId was empty');
|
|
64
|
+
}
|
|
65
|
+
this.signingClient = walletClient.extend(publicActions);
|
|
66
|
+
this.logger = logger ?? new ConsoleLogger();
|
|
67
|
+
this.chainId = chainId;
|
|
68
|
+
}
|
|
69
|
+
getSourceAddress(_params) {
|
|
70
|
+
return this.signingClient.account.address;
|
|
71
|
+
}
|
|
72
|
+
async generateJWT({ paymentRequestId, codeChallenge, accountId }) {
|
|
73
|
+
const headerObj = { alg: 'ES256K' };
|
|
74
|
+
const payloadObj = {
|
|
75
|
+
sub: this.signingClient.account.address,
|
|
76
|
+
iss: 'accounts.atxp.ai',
|
|
77
|
+
aud: 'https://auth.atxp.ai',
|
|
78
|
+
iat: Math.floor(Date.now() / 1000),
|
|
79
|
+
exp: Math.floor(Date.now() / 1000) + 60 * 60,
|
|
80
|
+
...(codeChallenge ? { code_challenge: codeChallenge } : {}),
|
|
81
|
+
...(paymentRequestId ? { payment_request_id: paymentRequestId } : {}),
|
|
82
|
+
...(accountId ? { account_id: accountId } : {}),
|
|
83
|
+
};
|
|
84
|
+
const header = toBase64Url(JSON.stringify(headerObj));
|
|
85
|
+
const payload = toBase64Url(JSON.stringify(payloadObj));
|
|
86
|
+
const message = `${header}.${payload}`;
|
|
87
|
+
// Convert message to bytes
|
|
88
|
+
const messageBytes = typeof Buffer !== 'undefined'
|
|
89
|
+
? Buffer.from(message, 'utf8')
|
|
90
|
+
: new TextEncoder().encode(message);
|
|
91
|
+
// Sign the message with raw bytes
|
|
92
|
+
const signResult = await this.signingClient.signMessage({
|
|
93
|
+
account: this.signingClient.account,
|
|
94
|
+
message: { raw: messageBytes },
|
|
95
|
+
});
|
|
96
|
+
// For ES256K, signature is typically 65 bytes (r,s,v)
|
|
97
|
+
// Server expects the hex signature string (with 0x prefix) to be base64url encoded
|
|
98
|
+
// This creates: base64url("0x6eb2565...") not base64url(rawBytes)
|
|
99
|
+
// Pass the hex string directly to toBase64Url which will UTF-8 encode and base64url it
|
|
100
|
+
const signature = toBase64Url(signResult);
|
|
101
|
+
const jwt = `${header}.${payload}.${signature}`;
|
|
102
|
+
this.logger.info(`Generated ES256K JWT: ${jwt}`);
|
|
103
|
+
return jwt;
|
|
104
|
+
}
|
|
105
|
+
async makePayment(destinations, _memo, _paymentRequestId) {
|
|
106
|
+
this.logger.info(`Making payment with ${destinations.length} destination(s)`);
|
|
107
|
+
// Filter to polygon chain destinations
|
|
108
|
+
const polygonDestinations = destinations.filter(d => d.chain === 'polygon');
|
|
109
|
+
if (polygonDestinations.length === 0) {
|
|
110
|
+
this.logger.debug('SimplePolygonPaymentMaker: No polygon destinations found, cannot handle payment');
|
|
111
|
+
return null; // Cannot handle these destinations
|
|
112
|
+
}
|
|
113
|
+
// Pick first polygon destination
|
|
114
|
+
const destination = polygonDestinations[0];
|
|
115
|
+
// Validate currency
|
|
116
|
+
if (destination.currency !== 'USDC') {
|
|
117
|
+
throw new Error(`Unsupported currency: ${destination.currency}. Only USDC is supported on Polygon.`);
|
|
118
|
+
}
|
|
119
|
+
// Get USDC contract address for this chain
|
|
120
|
+
const usdcAddress = getPolygonUSDCAddress(this.chainId);
|
|
121
|
+
// Convert amount to smallest unit (USDC has 6 decimals)
|
|
122
|
+
const amountInSmallestUnit = destination.amount.multipliedBy(10 ** USDC_DECIMALS);
|
|
123
|
+
this.logger.info(`Transferring ${destination.amount.toString()} USDC to ${destination.address}`);
|
|
124
|
+
this.logger.info(`Amount in smallest unit: ${amountInSmallestUnit.toString()}`);
|
|
125
|
+
try {
|
|
126
|
+
// Check balance first
|
|
127
|
+
const balance = await this.signingClient.readContract({
|
|
128
|
+
address: usdcAddress,
|
|
129
|
+
abi: ERC20_ABI,
|
|
130
|
+
functionName: 'balanceOf',
|
|
131
|
+
args: [this.signingClient.account.address],
|
|
132
|
+
});
|
|
133
|
+
this.logger.info(`Current USDC balance: ${balance.toString()}`);
|
|
134
|
+
if (balance < BigInt(amountInSmallestUnit.toFixed(0))) {
|
|
135
|
+
throw new Error(`Insufficient USDC balance. Have: ${balance.toString()}, Need: ${amountInSmallestUnit.toString()}`);
|
|
136
|
+
}
|
|
137
|
+
// Encode the transfer function call
|
|
138
|
+
const data = encodeFunctionData({
|
|
139
|
+
abi: ERC20_ABI,
|
|
140
|
+
functionName: 'transfer',
|
|
141
|
+
args: [destination.address, BigInt(amountInSmallestUnit.toFixed(0))],
|
|
142
|
+
});
|
|
143
|
+
// Send the transaction
|
|
144
|
+
const hash = await this.signingClient.sendTransaction({
|
|
145
|
+
account: this.signingClient.account,
|
|
146
|
+
to: usdcAddress,
|
|
147
|
+
data,
|
|
148
|
+
chain: this.signingClient.chain,
|
|
149
|
+
});
|
|
150
|
+
this.logger.info(`Transaction sent: ${hash}`);
|
|
151
|
+
// Wait for confirmation
|
|
152
|
+
const receipt = await this.signingClient.waitForTransactionReceipt({ hash });
|
|
153
|
+
if (receipt.status === 'success') {
|
|
154
|
+
this.logger.info(`Payment successful! Transaction: ${hash}`);
|
|
155
|
+
return {
|
|
156
|
+
transactionId: hash,
|
|
157
|
+
chain: 'polygon',
|
|
158
|
+
currency: 'USDC',
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
throw new Error(`Transaction failed: ${hash}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
this.logger.error(`Payment failed: ${error}`);
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export { ServerPaymentMaker };
|
|
173
|
+
//# sourceMappingURL=serverPaymentMaker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverPaymentMaker.js","sources":["../src/serverPaymentMaker.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAgBA;AACA,SAAS,WAAW,CAAC,IAAY,EAAA;;AAE/B,IAAA,MAAM,MAAM,GAAG,OAAO,MAAM,KAAK;UAC7B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,QAAQ;AACrC,UAAE,IAAI,CAAC,IAAI,CAAC;;IAEd,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;AACzE;AAEA,MAAM,aAAa,GAAG,CAAC;AACvB,MAAM,SAAS,GAAG;AAChB,IAAA;AACE,QAAA,QAAQ,E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CAAC,WAAW,CAAC,OAAkB,EAAE,MAAM,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AAChF,aAAA,CAAC;;YAGF,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,eAAe,CAAC;AACpD,gBAAA,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,OAAQ;AACpC,gBAAA,EAAE,EAAE,WAAsB;gBAC1B,IAAI;AACJ,gBAAA,KAAK,EAAE,IAAI,CAAC,aAAa,CAAC,KAAK;AAChC,aAAA,CAAC;YAEF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA,kBAAA,EAAqB,IAAI,CAAA,CAAE,CAAC;;AAG7C,YAAA,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC,EAAE,IAAI,EAAE,CAAC;AAE5E,YAAA,IAAI,OAAO,CAAC,MAAM,KAAK,SAAS,EAAE;gBAChC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA,iCAAA,EAAoC,IAAI,CAAA,CAAE,CAAC;gBAC5D,OAAO;AACL,oBAAA,aAAa,EAAE,IAAI;AACnB,oBAAA,KAAK,EAAE,SAAS;AAChB,oBAAA,QAAQ,EAAE,MAAM;iBACjB;YACH;iBAAO;AACL,gBAAA,MAAM,IAAI,KAAK,CAAC,uBAAuB,IAAI,CAAA,CAAE,CAAC;YAChD;QACF;QAAE,OAAO,KAAK,EAAE;YACd,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA,gBAAA,EAAmB,KAAK,CAAA,CAAE,CAAC;AAC7C,YAAA,MAAM,KAAK;QACb;IACF;AACD;;;;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Account, type Address, type Hex } from 'viem';
|
|
2
|
+
import { type BundlerClient, type SmartAccount } from 'viem/account-abstraction';
|
|
3
|
+
export interface EphemeralSmartWallet {
|
|
4
|
+
address: Address;
|
|
5
|
+
client: BundlerClient;
|
|
6
|
+
account: SmartAccount;
|
|
7
|
+
signer: Account;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates an ephemeral smart wallet with paymaster support
|
|
11
|
+
* @param privateKey - Private key for the wallet signer
|
|
12
|
+
* @param chainId - Chain ID (defaults to 137 for Polygon mainnet, or 80002 for Amoy testnet)
|
|
13
|
+
*
|
|
14
|
+
* NOTE: This implementation assumes Coinbase CDP supports Polygon.
|
|
15
|
+
* Verify support and correct endpoint URLs before using in production.
|
|
16
|
+
*/
|
|
17
|
+
export declare function toEphemeralSmartWallet(privateKey: Hex, chainId?: number): Promise<EphemeralSmartWallet>;
|
|
18
|
+
//# sourceMappingURL=smartWalletHelpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smartWalletHelpers.d.ts","sourceRoot":"","sources":["../src/smartWalletHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,OAAO,EACZ,KAAK,OAAO,EACZ,KAAK,GAAG,EACT,MAAM,MAAM,CAAC;AAId,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,YAAY,EAClB,MAAM,0BAA0B,CAAC;AAiDlC,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,aAAa,CAAC;IACtB,OAAO,EAAE,YAAY,CAAC;IACtB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,UAAU,EAAE,GAAG,EACf,OAAO,GAAE,MAAmB,GAC3B,OAAO,CAAC,oBAAoB,CAAC,CAqC/B"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createPublicClient, http } from 'viem';
|
|
2
|
+
import { polygon, polygonAmoy } from 'viem/chains';
|
|
3
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
4
|
+
import { toCoinbaseSmartAccount, createBundlerClient } from 'viem/account-abstraction';
|
|
5
|
+
|
|
6
|
+
// Coinbase CDP API Key
|
|
7
|
+
const COINBASE_API_KEY = 'snPdXqIzOGhRkGNJvEHM5bl9Hm3yRO3m';
|
|
8
|
+
/**
|
|
9
|
+
* Get Coinbase CDP Bundler URL for a given chain
|
|
10
|
+
* NOTE: Verify Coinbase CDP support for Polygon before using in production
|
|
11
|
+
*/
|
|
12
|
+
function getCoinbaseBundlerUrl(chainId) {
|
|
13
|
+
switch (chainId) {
|
|
14
|
+
case 137: // Polygon mainnet
|
|
15
|
+
return 'https://api.developer.coinbase.com/rpc/v1/polygon-mainnet';
|
|
16
|
+
case 80002: // Polygon Amoy testnet
|
|
17
|
+
return 'https://api.developer.coinbase.com/rpc/v1/polygon-amoy';
|
|
18
|
+
default:
|
|
19
|
+
throw new Error(`Unsupported chain ID for Coinbase bundler: ${chainId}. Supported: 137 (mainnet), 80002 (Amoy testnet)`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get Coinbase CDP Paymaster URL for a given chain
|
|
24
|
+
* NOTE: Verify Coinbase CDP support for Polygon before using in production
|
|
25
|
+
*/
|
|
26
|
+
function getCoinbasePaymasterUrl(chainId) {
|
|
27
|
+
switch (chainId) {
|
|
28
|
+
case 137: // Polygon mainnet
|
|
29
|
+
return 'https://api.developer.coinbase.com/rpc/v1/polygon-mainnet';
|
|
30
|
+
case 80002: // Polygon Amoy testnet
|
|
31
|
+
return 'https://api.developer.coinbase.com/rpc/v1/polygon-amoy';
|
|
32
|
+
default:
|
|
33
|
+
throw new Error(`Unsupported chain ID for Coinbase paymaster: ${chainId}. Supported: 137 (mainnet), 80002 (Amoy testnet)`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get Polygon chain configuration by chain ID
|
|
38
|
+
*/
|
|
39
|
+
function getPolygonChain(chainId) {
|
|
40
|
+
switch (chainId) {
|
|
41
|
+
case 137:
|
|
42
|
+
return polygon;
|
|
43
|
+
case 80002:
|
|
44
|
+
return polygonAmoy;
|
|
45
|
+
default:
|
|
46
|
+
throw new Error(`Unsupported Polygon chain ID: ${chainId}. Supported: 137 (mainnet), 80002 (Amoy testnet)`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Creates an ephemeral smart wallet with paymaster support
|
|
51
|
+
* @param privateKey - Private key for the wallet signer
|
|
52
|
+
* @param chainId - Chain ID (defaults to 137 for Polygon mainnet, or 80002 for Amoy testnet)
|
|
53
|
+
*
|
|
54
|
+
* NOTE: This implementation assumes Coinbase CDP supports Polygon.
|
|
55
|
+
* Verify support and correct endpoint URLs before using in production.
|
|
56
|
+
*/
|
|
57
|
+
async function toEphemeralSmartWallet(privateKey, chainId = polygon.id) {
|
|
58
|
+
const apiKey = COINBASE_API_KEY;
|
|
59
|
+
const signer = privateKeyToAccount(privateKey);
|
|
60
|
+
const chain = getPolygonChain(chainId);
|
|
61
|
+
const bundlerUrl = getCoinbaseBundlerUrl(chainId);
|
|
62
|
+
const paymasterUrl = getCoinbasePaymasterUrl(chainId);
|
|
63
|
+
const publicClient = createPublicClient({
|
|
64
|
+
chain,
|
|
65
|
+
transport: http(`${bundlerUrl}/${apiKey}`)
|
|
66
|
+
});
|
|
67
|
+
// Create the Coinbase smart wallet
|
|
68
|
+
const account = await toCoinbaseSmartAccount({
|
|
69
|
+
client: publicClient,
|
|
70
|
+
owners: [signer],
|
|
71
|
+
version: '1'
|
|
72
|
+
});
|
|
73
|
+
// Create bundler client with paymaster support
|
|
74
|
+
const bundlerClient = createBundlerClient({
|
|
75
|
+
account,
|
|
76
|
+
client: publicClient,
|
|
77
|
+
transport: http(`${bundlerUrl}/${apiKey}`),
|
|
78
|
+
chain,
|
|
79
|
+
paymaster: true, // Enable paymaster sponsorship
|
|
80
|
+
paymasterContext: {
|
|
81
|
+
transport: http(`${paymasterUrl}/${apiKey}`)
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
address: account.address,
|
|
86
|
+
client: bundlerClient,
|
|
87
|
+
account,
|
|
88
|
+
signer,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { toEphemeralSmartWallet };
|
|
93
|
+
//# sourceMappingURL=smartWalletHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smartWalletHelpers.js","sources":["../src/smartWalletHelpers.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAiBA;AACA,MAAM,gBAAgB,GAAG,kCAAkC;AAE3D;;;AAGG;AACH,SAAS,qBAAqB,CAAC,OAAe,EAAA;IAC5C,QAAQ,OAAO;QACb,KAAK,GAAG;AACN,YAAA,OAAO,2DAA2D;QACpE,KAAK,KAAK;AACR,YAAA,OAAO,wDAAwD;AACjE,QAAA;AACE,YAAA,MAAM,IAAI,KAAK,CAAC,8CAA8C,OAAO,CAAA,gDAAA,CAAkD,CAAC;;AAE9H;AAEA;;;AAGG;AACH,SAAS,uBAAuB,CAAC,OAAe,EAAA;IAC9C,QAAQ,OAAO;QACb,KAAK,GAAG;AACN,YAAA,OAAO,2DAA2D;QACpE,KAAK,KAAK;AACR,YAAA,OAAO,wDAAwD;AACjE,QAAA;AACE,YAAA,MAAM,IAAI,KAAK,CAAC,gDAAgD,OAAO,CAAA,gDAAA,CAAkD,CAAC;;AAEhI;AAEA;;AAEG;AACH,SAAS,eAAe,CAAC,OAAe,EAAA;IACtC,QAAQ,OAAO;AACb,QAAA,KAAK,GAAG;AACN,YAAA,OAAO,OAAO;AAChB,QAAA,KAAK,KAAK;AACR,YAAA,OAAO,WAAW;AACpB,QAAA;AACE,YAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,OAAO,CAAA,gDAAA,CAAkD,CAAC;;AAEjH;AASA;;;;;;;AAOG;AACI,eAAe,sBAAsB,CAC1C,UAAe,EACf,OAAA,GAAkB,OAAO,CAAC,EAAE,EAAA;IAE5B,MAAM,MAAM,GAAG,gBAAgB;AAC/B,IAAA,MAAM,MAAM,GAAG,mBAAmB,CAAC,UAAU,CAAC;AAC9C,IAAA,MAAM,KAAK,GAAG,eAAe,CAAC,OAAO,CAAC;AACtC,IAAA,MAAM,UAAU,GAAG,qBAAqB,CAAC,OAAO,CAAC;AACjD,IAAA,MAAM,YAAY,GAAG,uBAAuB,CAAC,OAAO,CAAC;IAErD,MAAM,YAAY,GAAG,kBAAkB,CAAC;QACtC,KAAK;QACL,SAAS,EAAE,IAAI,CAAC,CAAA,EAAG,UAAU,CAAA,CAAA,EAAI,MAAM,EAAE;AAC1C,KAAA,CAAC;;AAGF,IAAA,MAAM,OAAO,GAAG,MAAM,sBAAsB,CAAC;AAC3C,QAAA,MAAM,EAAE,YAAY;QACpB,MAAM,EAAE,CAAC,MAAM,CAAC;AAChB,QAAA,OAAO,EAAE;AACV,KAAA,CAAC;;IAGF,MAAM,aAAa,GAAG,mBAAmB,CAAC;QACxC,OAAO;AACP,QAAA,MAAM,EAAE,YAAY;QACpB,SAAS,EAAE,IAAI,CAAC,CAAA,EAAG,UAAU,CAAA,CAAA,EAAI,MAAM,EAAE,CAAC;QAC1C,KAAK;QACL,SAAS,EAAE,IAAI;AACf,QAAA,gBAAgB,EAAE;YAChB,SAAS,EAAE,IAAI,CAAC,CAAA,EAAG,YAAY,CAAA,CAAA,EAAI,MAAM,EAAE;AAC5C;AACF,KAAA,CAAC;IAEF,OAAO;QACL,OAAO,EAAE,OAAO,CAAC,OAAO;AACxB,QAAA,MAAM,EAAE,aAAa;QACrB,OAAO;QACP,MAAM;KACP;AACH;;;;"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Logger, Currency, PaymentMaker, AccountId, PaymentIdentifier, Destination } from '@atxp/common';
|
|
2
|
+
import BigNumber from 'bignumber.js';
|
|
3
|
+
import { SpendPermission } from './types.js';
|
|
4
|
+
import { type EphemeralSmartWallet } from './smartWalletHelpers.js';
|
|
5
|
+
/**
|
|
6
|
+
* Browser-based payment maker using ephemeral smart wallets with account abstraction.
|
|
7
|
+
* Uses Coinbase CDP for gasless transactions and spend permissions.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SmartWalletPaymentMaker implements PaymentMaker {
|
|
10
|
+
private logger;
|
|
11
|
+
private spendPermission;
|
|
12
|
+
private smartWallet;
|
|
13
|
+
private chainId;
|
|
14
|
+
private usdcAddress;
|
|
15
|
+
constructor(spendPermission: SpendPermission, smartWallet: EphemeralSmartWallet, logger?: Logger, chainId?: number);
|
|
16
|
+
getSourceAddress(_params: {
|
|
17
|
+
amount: BigNumber;
|
|
18
|
+
currency: Currency;
|
|
19
|
+
receiver: string;
|
|
20
|
+
memo: string;
|
|
21
|
+
}): string;
|
|
22
|
+
generateJWT({ paymentRequestId, codeChallenge, accountId }: {
|
|
23
|
+
paymentRequestId: string;
|
|
24
|
+
codeChallenge: string;
|
|
25
|
+
accountId?: AccountId | null;
|
|
26
|
+
}): Promise<string>;
|
|
27
|
+
makePayment(destinations: Destination[], memo: string, _paymentRequestId?: string): Promise<PaymentIdentifier | null>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=smartWalletPaymentMaker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smartWalletPaymentMaker.d.ts","sourceRoot":"","sources":["../src/smartWalletPaymentMaker.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAiB,YAAY,EAAE,SAAS,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAExH,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAiDpE;;;GAGG;AACH,qBAAa,uBAAwB,YAAW,YAAY;IAC1D,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,WAAW,CAAuB;IAC1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