@dcentralab/d402-client 0.3.3 → 0.3.5

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.mjs CHANGED
@@ -302,7 +302,6 @@ async function signD402Payment(params) {
302
302
  requestPath,
303
303
  d402Version = 1
304
304
  } = params;
305
- const consumerWallet = iatpWalletAddress || operatorAccount.address;
306
305
  let finalRequestPath = requestPath || paymentRequirement.resource || "/mcp";
307
306
  if (!finalRequestPath || finalRequestPath.trim() === "") {
308
307
  finalRequestPath = "/mcp";
@@ -319,10 +318,10 @@ async function signD402Payment(params) {
319
318
  name: walletName,
320
319
  version: walletVersion,
321
320
  chainId,
322
- verifyingContract: consumerWallet
321
+ verifyingContract: iatpWalletAddress
323
322
  };
324
323
  const message = {
325
- wallet: consumerWallet,
324
+ wallet: iatpWalletAddress,
326
325
  provider: paymentRequirement.payTo,
327
326
  token: paymentRequirement.asset,
328
327
  amount: BigInt(paymentRequirement.maxAmountRequired),
@@ -347,7 +346,7 @@ async function signD402Payment(params) {
347
346
  payload: {
348
347
  signature,
349
348
  authorization: {
350
- from: consumerWallet,
349
+ from: iatpWalletAddress,
351
350
  to: paymentRequirement.payTo,
352
351
  value: paymentRequirement.maxAmountRequired,
353
352
  validAfter,
@@ -5107,13 +5106,13 @@ async function createIATPWallet(params) {
5107
5106
  }
5108
5107
  async function getAvailableBalance(params) {
5109
5108
  const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
5110
- const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
5111
- if (!walletConfig) {
5112
- throw new Error(`IATPWallet contract not found for network: ${network}`);
5109
+ const walletAbi = getContractAbi("IATPWallet" /* IATP_WALLET */, network);
5110
+ if (!walletAbi) {
5111
+ throw new Error(`IATPWallet ABI not found for network: ${network}`);
5113
5112
  }
5114
5113
  const balance = await publicClient.readContract({
5115
5114
  address: walletAddress,
5116
- abi: walletConfig.abi,
5115
+ abi: walletAbi,
5117
5116
  functionName: "getAvailableBalance",
5118
5117
  args: [tokenAddress]
5119
5118
  });
@@ -5143,37 +5142,41 @@ async function getWalletsByOwner(params) {
5143
5142
  // src/wallet/withdrawals.ts
5144
5143
  async function getWithdrawalRequest(params) {
5145
5144
  const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
5146
- const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
5147
- if (!walletConfig) {
5148
- throw new Error(`IATPWallet contract not found for network: ${network}`);
5145
+ const walletAbi = getContractAbi("IATPWallet" /* IATP_WALLET */, network);
5146
+ if (!walletAbi) {
5147
+ throw new Error(`IATPWallet ABI not found for network: ${network}`);
5149
5148
  }
5150
5149
  const result = await publicClient.readContract({
5151
5150
  address: walletAddress,
5152
- abi: walletConfig.abi,
5151
+ abi: walletAbi,
5153
5152
  functionName: "getWithdrawalRequest",
5154
5153
  args: [tokenAddress]
5155
5154
  });
5156
- if (result[0] === 0n) {
5155
+ const [request, unlockTime] = result;
5156
+ if (request.amount === 0n) {
5157
5157
  return null;
5158
5158
  }
5159
- return result;
5159
+ return {
5160
+ request,
5161
+ unlockTime
5162
+ };
5160
5163
  }
5161
5164
  async function requestWithdrawal(params) {
5162
5165
  const { walletClient, publicClient, walletAddress, tokenAddress, amount, account, network = "sepolia" } = params;
5163
- const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
5164
- if (!walletConfig) {
5165
- throw new Error(`IATPWallet contract not found for network: ${network}`);
5166
+ const walletAbi = getContractAbi("IATPWallet" /* IATP_WALLET */, network);
5167
+ if (!walletAbi) {
5168
+ throw new Error(`IATPWallet ABI not found for network: ${network}`);
5166
5169
  }
5167
5170
  await publicClient.simulateContract({
5168
5171
  account,
5169
5172
  address: walletAddress,
5170
- abi: walletConfig.abi,
5173
+ abi: walletAbi,
5171
5174
  functionName: "requestWithdrawal",
5172
5175
  args: [tokenAddress, amount]
5173
5176
  });
5174
5177
  const estimatedGas = await publicClient.estimateContractGas({
5175
5178
  address: walletAddress,
5176
- abi: walletConfig.abi,
5179
+ abi: walletAbi,
5177
5180
  functionName: "requestWithdrawal",
5178
5181
  args: [tokenAddress, amount],
5179
5182
  account
@@ -5181,7 +5184,7 @@ async function requestWithdrawal(params) {
5181
5184
  const gasLimit = estimatedGas + estimatedGas / 5n;
5182
5185
  const hash = await walletClient.writeContract({
5183
5186
  address: walletAddress,
5184
- abi: walletConfig.abi,
5187
+ abi: walletAbi,
5185
5188
  functionName: "requestWithdrawal",
5186
5189
  args: [tokenAddress, amount],
5187
5190
  account,
@@ -5192,20 +5195,20 @@ async function requestWithdrawal(params) {
5192
5195
  }
5193
5196
  async function executeWithdrawal(params) {
5194
5197
  const { walletClient, publicClient, walletAddress, tokenAddress, account, network = "sepolia" } = params;
5195
- const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
5196
- if (!walletConfig) {
5197
- throw new Error(`IATPWallet contract not found for network: ${network}`);
5198
+ const walletAbi = getContractAbi("IATPWallet" /* IATP_WALLET */, network);
5199
+ if (!walletAbi) {
5200
+ throw new Error(`IATPWallet ABI not found for network: ${network}`);
5198
5201
  }
5199
5202
  await publicClient.simulateContract({
5200
5203
  account,
5201
5204
  address: walletAddress,
5202
- abi: walletConfig.abi,
5205
+ abi: walletAbi,
5203
5206
  functionName: "executeWithdrawal",
5204
5207
  args: [tokenAddress]
5205
5208
  });
5206
5209
  const estimatedGas = await publicClient.estimateContractGas({
5207
5210
  address: walletAddress,
5208
- abi: walletConfig.abi,
5211
+ abi: walletAbi,
5209
5212
  functionName: "executeWithdrawal",
5210
5213
  args: [tokenAddress],
5211
5214
  account
@@ -5213,7 +5216,7 @@ async function executeWithdrawal(params) {
5213
5216
  const gasLimit = estimatedGas + estimatedGas / 5n;
5214
5217
  const hash = await walletClient.writeContract({
5215
5218
  address: walletAddress,
5216
- abi: walletConfig.abi,
5219
+ abi: walletAbi,
5217
5220
  functionName: "executeWithdrawal",
5218
5221
  args: [tokenAddress],
5219
5222
  account,
@@ -5306,7 +5309,7 @@ var D402Client = class {
5306
5309
  *
5307
5310
  * @param publicClient - Viem PublicClient
5308
5311
  * @param tokenAddress - Token contract address
5309
- * @returns Withdrawal request: [amount, unlockTimestamp] or null if no request exists
5312
+ * @returns Withdrawal request details (request + unlockTime) or null if no request exists
5310
5313
  */
5311
5314
  async getWithdrawalRequest(publicClient, tokenAddress) {
5312
5315
  return getWithdrawalRequest({
@@ -5384,7 +5387,15 @@ var D402Client = class {
5384
5387
  const { parseAllPaymentRequirements: parseAllPaymentRequirements2 } = await Promise.resolve().then(() => (init_parser(), parser_exports));
5385
5388
  const { signD402Payment: signD402Payment2 } = await Promise.resolve().then(() => (init_signer(), signer_exports));
5386
5389
  const { encodePayment: encodePayment2 } = await Promise.resolve().then(() => (init_encoder(), encoder_exports));
5387
- let response = await fetch(url, init);
5390
+ const enhancedInit = {
5391
+ ...init,
5392
+ headers: {
5393
+ ...init?.headers,
5394
+ // Preserve existing Accept header or set default for MCP compatibility
5395
+ "Accept": init?.headers?.["Accept"] || "application/json, text/event-stream"
5396
+ }
5397
+ };
5398
+ let response = await fetch(url, enhancedInit);
5388
5399
  if (response.status !== 402) {
5389
5400
  return response;
5390
5401
  }
@@ -5398,9 +5409,9 @@ var D402Client = class {
5398
5409
  });
5399
5410
  const paymentHeader = encodePayment2(signedPayment);
5400
5411
  response = await fetch(url, {
5401
- ...init,
5412
+ ...enhancedInit,
5402
5413
  headers: {
5403
- ...init?.headers,
5414
+ ...enhancedInit.headers,
5404
5415
  "X-Payment": paymentHeader,
5405
5416
  "Access-Control-Expose-Headers": "X-Payment-Response"
5406
5417
  }