@atxp/base 0.10.7 → 0.10.8

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PaymentMaker, Logger, Currency, AccountId, Destination, PaymentIdentifier, JsonCache, Account as Account$1, ICache, Source } from '@atxp/common';
1
+ import { PaymentMaker, Logger, Currency, AccountId, Destination, PaymentIdentifier, JsonCache, Account as Account$1, ICache, Source, AuthorizeParams, AuthorizeResult } from '@atxp/common';
2
2
  export { ATXPAccount, BrowserCache, ICache, MemoryCache } from '@atxp/common';
3
3
  import BigNumber$1 from 'bignumber.js';
4
4
  import { Address, Account, LocalAccount, WalletClient, PublicActions } from 'viem';
@@ -132,6 +132,10 @@ declare class BaseAppAccount implements Account$1 {
132
132
  * BaseAppAccount doesn't support spend permissions, so this returns null.
133
133
  */
134
134
  createSpendPermission(_resourceUrl: string): Promise<string | null>;
135
+ /**
136
+ * Authorize a payment through the appropriate channel for Base browser accounts.
137
+ */
138
+ authorize(params: AuthorizeParams): Promise<AuthorizeResult>;
135
139
  }
136
140
 
137
141
  declare class BaseAppPaymentMaker implements PaymentMaker {
@@ -179,6 +183,10 @@ declare class BaseAccount implements Account$2 {
179
183
  * Base accounts don't support spend permissions, so this returns null.
180
184
  */
181
185
  createSpendPermission(_resourceUrl: string): Promise<string | null>;
186
+ /**
187
+ * Authorize a payment through the appropriate channel for Base accounts.
188
+ */
189
+ authorize(params: AuthorizeParams): Promise<AuthorizeResult>;
182
190
  }
183
191
 
184
192
  type ExtendedWalletClient = WalletClient & PublicActions;
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import { ConsoleLogger, constructEIP1271Message, createEIP1271AuthData, createEIP1271JWT, JsonCache, BrowserCache, ChainEnum, WalletTypeEnum } from '@atxp/common';
2
2
  export { ATXPAccount, BrowserCache, MemoryCache } from '@atxp/common';
3
+ import { BigNumber } from 'bignumber.js';
3
4
  import { baseSepolia, base } from 'viem/chains';
4
5
  import { createWalletClient, custom as custom$1, encodeFunctionData, parseEther, toHex, BaseError as BaseError$1, isHex, http, createTransport, createPublicClient, publicActions } from 'viem';
5
6
  import { privateKeyToAccount, generatePrivateKey } from 'viem/accounts';
6
7
  import { toCoinbaseSmartAccount, createBundlerClient } from 'viem/account-abstraction';
7
8
  import { UnsupportedCurrencyError, InsufficientFundsError, TransactionRevertedError, ATXPPaymentError, UserRejectedError, GasEstimationError, RpcError, PaymentNetworkError } from '@atxp/client';
8
- import { BigNumber } from 'bignumber.js';
9
9
 
10
10
  const USDC_CONTRACT_ADDRESS_BASE = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"; // USDC on Base mainnet
11
11
  const USDC_CONTRACT_ADDRESS_BASE_SEPOLIA = "0x036CbD53842c5426634e7929541eC2318f3dCF7e"; // USDC on Base Sepolia testnet
@@ -5503,6 +5503,34 @@ class BaseAppAccount {
5503
5503
  async createSpendPermission(_resourceUrl) {
5504
5504
  return null;
5505
5505
  }
5506
+ /**
5507
+ * Authorize a payment through the appropriate channel for Base browser accounts.
5508
+ */
5509
+ async authorize(params) {
5510
+ const { protocol } = params;
5511
+ switch (protocol) {
5512
+ case 'atxp': {
5513
+ const chain = this.chainId === 84532 ? ChainEnum.BaseSepolia : ChainEnum.Base;
5514
+ const destination = {
5515
+ chain,
5516
+ currency: 'USDC',
5517
+ address: params.destination,
5518
+ amount: new BigNumber(params.amount),
5519
+ };
5520
+ const result = await this.paymentMakers[0].makePayment([destination], params.memo || '');
5521
+ if (!result) {
5522
+ throw new Error('BaseAppAccount: payment execution returned no result');
5523
+ }
5524
+ return { protocol, credential: JSON.stringify(result) };
5525
+ }
5526
+ case 'x402':
5527
+ throw new Error('BaseAppAccount does not support x402 protocol');
5528
+ case 'mpp':
5529
+ throw new Error('BaseAppAccount does not support MPP protocol');
5530
+ default:
5531
+ throw new Error(`BaseAppAccount: unsupported protocol '${protocol}'`);
5532
+ }
5533
+ }
5506
5534
  }
5507
5535
 
5508
5536
  // Helper function to convert to base64url that works in both Node.js and browsers
@@ -5742,6 +5770,42 @@ class BaseAccount {
5742
5770
  async createSpendPermission(_resourceUrl) {
5743
5771
  return null;
5744
5772
  }
5773
+ /**
5774
+ * Authorize a payment through the appropriate channel for Base accounts.
5775
+ */
5776
+ async authorize(params) {
5777
+ const { protocol } = params;
5778
+ switch (protocol) {
5779
+ case 'x402': {
5780
+ const { createPaymentHeader } = await import('x402/client');
5781
+ if (!params.paymentRequirements) {
5782
+ throw new Error('BaseAccount: x402 authorize requires paymentRequirements');
5783
+ }
5784
+ const reqs = params.paymentRequirements;
5785
+ const x402Version = reqs.x402Version || 1;
5786
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5787
+ const paymentHeader = await createPaymentHeader(this.getLocalAccount(), x402Version, reqs);
5788
+ return { protocol, credential: paymentHeader };
5789
+ }
5790
+ case 'atxp': {
5791
+ const destination = {
5792
+ chain: 'base',
5793
+ currency: 'USDC',
5794
+ address: params.destination,
5795
+ amount: new BigNumber(params.amount),
5796
+ };
5797
+ const result = await this.paymentMakers[0].makePayment([destination], params.memo || '');
5798
+ if (!result) {
5799
+ throw new Error('BaseAccount: payment execution returned no result');
5800
+ }
5801
+ return { protocol, credential: JSON.stringify({ transactionId: result.transactionId, chain: result.chain, currency: result.currency }) };
5802
+ }
5803
+ case 'mpp':
5804
+ throw new Error('BaseAccount does not support MPP protocol');
5805
+ default:
5806
+ throw new Error(`BaseAccount: unsupported protocol '${protocol}'`);
5807
+ }
5808
+ }
5745
5809
  }
5746
5810
 
5747
5811
  export { BaseAccount, BaseAppAccount, BaseAppPaymentMaker, BasePaymentMaker, MainWalletPaymentMaker, IntermediaryCache as PermissionCache, USDC_CONTRACT_ADDRESS_BASE, USDC_CONTRACT_ADDRESS_BASE_SEPOLIA, getBaseUSDCAddress };