@dcentralab/d402-client 0.2.7 → 0.3.2
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/LICENSE +22 -0
- package/README.md +58 -553
- package/dist/index.d.mts +474 -283
- package/dist/index.d.ts +474 -283
- package/dist/index.js +112 -100
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +102 -98
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { http, createPublicClient, createWalletClient, decodeEventLog,
|
|
1
|
+
import { http, createPublicClient, createWalletClient, decodeEventLog, isAddress } from 'viem';
|
|
2
2
|
import { sepolia } from 'viem/chains';
|
|
3
3
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
@@ -11,10 +11,10 @@ var __export = (target, all) => {
|
|
|
11
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
// src/errors.ts
|
|
14
|
+
// src/core/errors.ts
|
|
15
15
|
var PaymentError, PaymentAmountExceededError, MissingRequestConfigError, PaymentAlreadyAttemptedError, UnsupportedSchemeError, PaymentVerificationError, Invalid402ResponseError, UnsupportedNetworkError;
|
|
16
16
|
var init_errors = __esm({
|
|
17
|
-
"src/errors.ts"() {
|
|
17
|
+
"src/core/errors.ts"() {
|
|
18
18
|
PaymentError = class _PaymentError extends Error {
|
|
19
19
|
constructor(message) {
|
|
20
20
|
super(message);
|
|
@@ -81,7 +81,7 @@ var init_errors = __esm({
|
|
|
81
81
|
}
|
|
82
82
|
});
|
|
83
83
|
|
|
84
|
-
// src/parser.ts
|
|
84
|
+
// src/payment/parser.ts
|
|
85
85
|
var parser_exports = {};
|
|
86
86
|
__export(parser_exports, {
|
|
87
87
|
parseAllPaymentRequirements: () => parseAllPaymentRequirements,
|
|
@@ -187,29 +187,18 @@ async function parseAllPaymentRequirements(response) {
|
|
|
187
187
|
return requirements;
|
|
188
188
|
}
|
|
189
189
|
var init_parser = __esm({
|
|
190
|
-
"src/parser.ts"() {
|
|
190
|
+
"src/payment/parser.ts"() {
|
|
191
191
|
init_errors();
|
|
192
192
|
}
|
|
193
193
|
});
|
|
194
194
|
|
|
195
|
-
// src/constants.ts
|
|
196
|
-
var
|
|
195
|
+
// src/core/constants.ts
|
|
196
|
+
var TOKEN_ADDRESSES, EIP712_TYPES;
|
|
197
197
|
var init_constants = __esm({
|
|
198
|
-
"src/constants.ts"() {
|
|
199
|
-
CHAIN_IDS = {
|
|
200
|
-
sepolia: 11155111
|
|
201
|
-
};
|
|
202
|
-
NETWORKS = {
|
|
203
|
-
sepolia: {
|
|
204
|
-
chainId: 11155111,
|
|
205
|
-
name: "Sepolia Testnet",
|
|
206
|
-
nativeCurrency: { name: "Sepolia Ether", symbol: "SEP", decimals: 18 }
|
|
207
|
-
}
|
|
208
|
-
};
|
|
198
|
+
"src/core/constants.ts"() {
|
|
209
199
|
TOKEN_ADDRESSES = {
|
|
210
200
|
sepolia: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
|
|
211
201
|
};
|
|
212
|
-
DEFAULT_VALIDITY_WINDOW_SECONDS = 300;
|
|
213
202
|
EIP712_TYPES = {
|
|
214
203
|
PullFundsForSettlement: [
|
|
215
204
|
{ name: "wallet", type: "address" },
|
|
@@ -223,7 +212,7 @@ var init_constants = __esm({
|
|
|
223
212
|
}
|
|
224
213
|
});
|
|
225
214
|
|
|
226
|
-
// src/utils.ts
|
|
215
|
+
// src/core/utils.ts
|
|
227
216
|
function parseMoney(amount, decimals) {
|
|
228
217
|
if (typeof amount === "bigint") {
|
|
229
218
|
return amount;
|
|
@@ -276,13 +265,6 @@ function getChainId(network) {
|
|
|
276
265
|
}
|
|
277
266
|
return chainId;
|
|
278
267
|
}
|
|
279
|
-
function findMatchingPaymentRequirement(requirements, scheme = "exact", network) {
|
|
280
|
-
return requirements.find((req) => {
|
|
281
|
-
const schemeMatches = req.scheme === scheme;
|
|
282
|
-
const networkMatches = !network || req.network === network;
|
|
283
|
-
return schemeMatches && networkMatches;
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
268
|
function usdToUsdc(usdAmount, decimals = 6) {
|
|
287
269
|
return parseMoney(usdAmount, decimals);
|
|
288
270
|
}
|
|
@@ -300,24 +282,14 @@ function isValidAddress(address) {
|
|
|
300
282
|
function normalizeAddress(address) {
|
|
301
283
|
return address.toLowerCase();
|
|
302
284
|
}
|
|
303
|
-
function decodePaymentResponse(header) {
|
|
304
|
-
try {
|
|
305
|
-
const binString = atob(header);
|
|
306
|
-
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
307
|
-
const decoded = new TextDecoder().decode(bytes);
|
|
308
|
-
return JSON.parse(decoded);
|
|
309
|
-
} catch (error) {
|
|
310
|
-
throw new Error(`Failed to decode payment response: ${error}`);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
285
|
var init_utils = __esm({
|
|
314
|
-
"src/utils.ts"() {
|
|
286
|
+
"src/core/utils.ts"() {
|
|
315
287
|
init_constants();
|
|
316
288
|
init_errors();
|
|
317
289
|
}
|
|
318
290
|
});
|
|
319
291
|
|
|
320
|
-
// src/signer.ts
|
|
292
|
+
// src/payment/signer.ts
|
|
321
293
|
var signer_exports = {};
|
|
322
294
|
__export(signer_exports, {
|
|
323
295
|
signD402Payment: () => signD402Payment
|
|
@@ -388,17 +360,17 @@ async function signD402Payment(params) {
|
|
|
388
360
|
return signedPayment;
|
|
389
361
|
}
|
|
390
362
|
var init_signer = __esm({
|
|
391
|
-
"src/signer.ts"() {
|
|
392
|
-
init_utils();
|
|
363
|
+
"src/payment/signer.ts"() {
|
|
393
364
|
init_utils();
|
|
394
365
|
init_constants();
|
|
395
366
|
}
|
|
396
367
|
});
|
|
397
368
|
|
|
398
|
-
// src/encoder.ts
|
|
369
|
+
// src/payment/encoder.ts
|
|
399
370
|
var encoder_exports = {};
|
|
400
371
|
__export(encoder_exports, {
|
|
401
372
|
decodePayment: () => decodePayment,
|
|
373
|
+
decodePaymentResponse: () => decodePaymentResponse,
|
|
402
374
|
encodePayment: () => encodePayment,
|
|
403
375
|
safeBase64Decode: () => safeBase64Decode,
|
|
404
376
|
safeBase64Encode: () => safeBase64Encode
|
|
@@ -421,12 +393,20 @@ function decodePayment(encodedPayment) {
|
|
|
421
393
|
const jsonString = safeBase64Decode(encodedPayment);
|
|
422
394
|
return JSON.parse(jsonString);
|
|
423
395
|
}
|
|
396
|
+
function decodePaymentResponse(header) {
|
|
397
|
+
try {
|
|
398
|
+
const jsonString = safeBase64Decode(header);
|
|
399
|
+
return JSON.parse(jsonString);
|
|
400
|
+
} catch (error) {
|
|
401
|
+
throw new Error(`Failed to decode payment response: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
424
404
|
var init_encoder = __esm({
|
|
425
|
-
"src/encoder.ts"() {
|
|
405
|
+
"src/payment/encoder.ts"() {
|
|
426
406
|
}
|
|
427
407
|
});
|
|
428
408
|
|
|
429
|
-
// src/selector.ts
|
|
409
|
+
// src/payment/selector.ts
|
|
430
410
|
init_errors();
|
|
431
411
|
function selectPaymentRequirement(requirements, options = {}) {
|
|
432
412
|
const {
|
|
@@ -508,6 +488,13 @@ function sortPaymentRequirements(requirements, preferredNetwork) {
|
|
|
508
488
|
return 0;
|
|
509
489
|
});
|
|
510
490
|
}
|
|
491
|
+
function findMatchingPaymentRequirement(requirements, scheme = "exact", network) {
|
|
492
|
+
return requirements.find((req) => {
|
|
493
|
+
const schemeMatches = req.scheme === scheme;
|
|
494
|
+
const networkMatches = !network || req.network === network;
|
|
495
|
+
return schemeMatches && networkMatches;
|
|
496
|
+
});
|
|
497
|
+
}
|
|
511
498
|
|
|
512
499
|
// src/contracts/abis/sepolia.json
|
|
513
500
|
var sepolia_default = {
|
|
@@ -5013,7 +5000,14 @@ var implementations_default = {
|
|
|
5013
5000
|
}
|
|
5014
5001
|
};
|
|
5015
5002
|
|
|
5016
|
-
// src/contracts.ts
|
|
5003
|
+
// src/contracts/index.ts
|
|
5004
|
+
var ContractName = /* @__PURE__ */ ((ContractName2) => {
|
|
5005
|
+
ContractName2["IATP_WALLET"] = "IATPWallet";
|
|
5006
|
+
ContractName2["IATP_WALLET_FACTORY"] = "IATPWalletFactory";
|
|
5007
|
+
ContractName2["IATP_SETTLEMENT_LAYER"] = "IATPSettlementLayer";
|
|
5008
|
+
ContractName2["ROLE_MANAGER"] = "RoleManager";
|
|
5009
|
+
return ContractName2;
|
|
5010
|
+
})(ContractName || {});
|
|
5017
5011
|
var ABIS = {
|
|
5018
5012
|
sepolia: sepolia_default.sepolia || {}
|
|
5019
5013
|
};
|
|
@@ -5043,7 +5037,7 @@ function getContractConfig(contractName, network = "sepolia") {
|
|
|
5043
5037
|
return { address, abi };
|
|
5044
5038
|
}
|
|
5045
5039
|
|
|
5046
|
-
// src/wallet.ts
|
|
5040
|
+
// src/wallet/creation.ts
|
|
5047
5041
|
async function createIATPWallet(params) {
|
|
5048
5042
|
const { ownerAccount, network = "sepolia", rpcUrl } = params;
|
|
5049
5043
|
const factoryConfig = getContractConfig("IATPWalletFactory" /* IATP_WALLET_FACTORY */, network);
|
|
@@ -5111,6 +5105,20 @@ async function createIATPWallet(params) {
|
|
|
5111
5105
|
chainId: chain.id
|
|
5112
5106
|
};
|
|
5113
5107
|
}
|
|
5108
|
+
async function getAvailableBalance(params) {
|
|
5109
|
+
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}`);
|
|
5113
|
+
}
|
|
5114
|
+
const balance = await publicClient.readContract({
|
|
5115
|
+
address: walletAddress,
|
|
5116
|
+
abi: walletConfig.abi,
|
|
5117
|
+
functionName: "getAvailableBalance",
|
|
5118
|
+
args: [tokenAddress]
|
|
5119
|
+
});
|
|
5120
|
+
return balance;
|
|
5121
|
+
}
|
|
5114
5122
|
async function getWalletsByOwner(params) {
|
|
5115
5123
|
const { ownerAddress, network = "sepolia", rpcUrl } = params;
|
|
5116
5124
|
const factoryConfig = getContractConfig("IATPWalletFactory" /* IATP_WALLET_FACTORY */, network);
|
|
@@ -5131,20 +5139,8 @@ async function getWalletsByOwner(params) {
|
|
|
5131
5139
|
});
|
|
5132
5140
|
return wallets;
|
|
5133
5141
|
}
|
|
5134
|
-
|
|
5135
|
-
|
|
5136
|
-
const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
|
|
5137
|
-
if (!walletConfig) {
|
|
5138
|
-
throw new Error(`IATPWallet contract not found for network: ${network}`);
|
|
5139
|
-
}
|
|
5140
|
-
const balance = await publicClient.readContract({
|
|
5141
|
-
address: walletAddress,
|
|
5142
|
-
abi: walletConfig.abi,
|
|
5143
|
-
functionName: "getAvailableBalance",
|
|
5144
|
-
args: [tokenAddress]
|
|
5145
|
-
});
|
|
5146
|
-
return balance;
|
|
5147
|
-
}
|
|
5142
|
+
|
|
5143
|
+
// src/wallet/withdrawals.ts
|
|
5148
5144
|
async function getWithdrawalRequest(params) {
|
|
5149
5145
|
const { publicClient, walletAddress, tokenAddress, network = "sepolia" } = params;
|
|
5150
5146
|
const walletConfig = getContractConfig("IATPWallet" /* IATP_WALLET */, network);
|
|
@@ -5168,26 +5164,27 @@ async function requestWithdrawal(params) {
|
|
|
5168
5164
|
if (!walletConfig) {
|
|
5169
5165
|
throw new Error(`IATPWallet contract not found for network: ${network}`);
|
|
5170
5166
|
}
|
|
5171
|
-
|
|
5167
|
+
await publicClient.simulateContract({
|
|
5168
|
+
account,
|
|
5169
|
+
address: walletAddress,
|
|
5172
5170
|
abi: walletConfig.abi,
|
|
5173
5171
|
functionName: "requestWithdrawal",
|
|
5174
5172
|
args: [tokenAddress, amount]
|
|
5175
5173
|
});
|
|
5176
|
-
const estimatedGas = await publicClient.
|
|
5177
|
-
account: account.address,
|
|
5178
|
-
to: walletAddress,
|
|
5179
|
-
data
|
|
5180
|
-
});
|
|
5181
|
-
const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
|
|
5182
|
-
const { request } = await publicClient.simulateContract({
|
|
5183
|
-
account,
|
|
5174
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5184
5175
|
address: walletAddress,
|
|
5185
5176
|
abi: walletConfig.abi,
|
|
5186
5177
|
functionName: "requestWithdrawal",
|
|
5187
|
-
args: [tokenAddress, amount]
|
|
5178
|
+
args: [tokenAddress, amount],
|
|
5179
|
+
account
|
|
5188
5180
|
});
|
|
5181
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5189
5182
|
const hash = await walletClient.writeContract({
|
|
5190
|
-
|
|
5183
|
+
address: walletAddress,
|
|
5184
|
+
abi: walletConfig.abi,
|
|
5185
|
+
functionName: "requestWithdrawal",
|
|
5186
|
+
args: [tokenAddress, amount],
|
|
5187
|
+
account,
|
|
5191
5188
|
gas: gasLimit
|
|
5192
5189
|
});
|
|
5193
5190
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
@@ -5199,33 +5196,34 @@ async function executeWithdrawal(params) {
|
|
|
5199
5196
|
if (!walletConfig) {
|
|
5200
5197
|
throw new Error(`IATPWallet contract not found for network: ${network}`);
|
|
5201
5198
|
}
|
|
5202
|
-
|
|
5199
|
+
await publicClient.simulateContract({
|
|
5200
|
+
account,
|
|
5201
|
+
address: walletAddress,
|
|
5203
5202
|
abi: walletConfig.abi,
|
|
5204
5203
|
functionName: "executeWithdrawal",
|
|
5205
5204
|
args: [tokenAddress]
|
|
5206
5205
|
});
|
|
5207
|
-
const estimatedGas = await publicClient.
|
|
5208
|
-
account: account.address,
|
|
5209
|
-
to: walletAddress,
|
|
5210
|
-
data
|
|
5211
|
-
});
|
|
5212
|
-
const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
|
|
5213
|
-
const { request } = await publicClient.simulateContract({
|
|
5214
|
-
account,
|
|
5206
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5215
5207
|
address: walletAddress,
|
|
5216
5208
|
abi: walletConfig.abi,
|
|
5217
5209
|
functionName: "executeWithdrawal",
|
|
5218
|
-
args: [tokenAddress]
|
|
5210
|
+
args: [tokenAddress],
|
|
5211
|
+
account
|
|
5219
5212
|
});
|
|
5213
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5220
5214
|
const hash = await walletClient.writeContract({
|
|
5221
|
-
|
|
5215
|
+
address: walletAddress,
|
|
5216
|
+
abi: walletConfig.abi,
|
|
5217
|
+
functionName: "executeWithdrawal",
|
|
5218
|
+
args: [tokenAddress],
|
|
5219
|
+
account,
|
|
5222
5220
|
gas: gasLimit
|
|
5223
5221
|
});
|
|
5224
5222
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
5225
5223
|
return hash;
|
|
5226
5224
|
}
|
|
5227
5225
|
|
|
5228
|
-
// src/client.ts
|
|
5226
|
+
// src/client/D402Client.ts
|
|
5229
5227
|
var D402Client = class {
|
|
5230
5228
|
/**
|
|
5231
5229
|
* Create a new D402 Client.
|
|
@@ -5418,9 +5416,8 @@ var D402Client = class {
|
|
|
5418
5416
|
init_signer();
|
|
5419
5417
|
init_parser();
|
|
5420
5418
|
init_encoder();
|
|
5421
|
-
|
|
5422
|
-
|
|
5423
|
-
init_constants();
|
|
5419
|
+
|
|
5420
|
+
// src/settlement/queries.ts
|
|
5424
5421
|
async function getLockedBalanceForProvider(params) {
|
|
5425
5422
|
const { publicClient, providerAddress, tokenAddress, network = "sepolia" } = params;
|
|
5426
5423
|
const settlementConfig = getContractConfig("IATPSettlementLayer" /* IATP_SETTLEMENT_LAYER */, network);
|
|
@@ -5449,6 +5446,8 @@ async function getUnlockedBalanceForProvider(params) {
|
|
|
5449
5446
|
});
|
|
5450
5447
|
return balance;
|
|
5451
5448
|
}
|
|
5449
|
+
|
|
5450
|
+
// src/settlement/operations.ts
|
|
5452
5451
|
async function withdrawAllAvailableEpochs(params) {
|
|
5453
5452
|
const { walletClient, publicClient, tokenAddress, network = "sepolia" } = params;
|
|
5454
5453
|
if (!walletClient?.account) {
|
|
@@ -5459,32 +5458,37 @@ async function withdrawAllAvailableEpochs(params) {
|
|
|
5459
5458
|
if (!settlementConfig) {
|
|
5460
5459
|
throw new Error(`IATPSettlementLayer contract not found for network: ${network}`);
|
|
5461
5460
|
}
|
|
5462
|
-
|
|
5461
|
+
await publicClient.simulateContract({
|
|
5462
|
+
account,
|
|
5463
|
+
address: settlementConfig.address,
|
|
5463
5464
|
abi: settlementConfig.abi,
|
|
5464
5465
|
functionName: "withdrawAllAvailableEpochs",
|
|
5465
5466
|
args: [tokenAddress]
|
|
5466
5467
|
});
|
|
5467
|
-
const estimatedGas = await publicClient.
|
|
5468
|
-
account: account.address,
|
|
5469
|
-
to: settlementConfig.address,
|
|
5470
|
-
data
|
|
5471
|
-
});
|
|
5472
|
-
const gasLimit = estimatedGas + estimatedGas * BigInt(20) / BigInt(100);
|
|
5473
|
-
const { request } = await publicClient.simulateContract({
|
|
5474
|
-
account,
|
|
5468
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
5475
5469
|
address: settlementConfig.address,
|
|
5476
5470
|
abi: settlementConfig.abi,
|
|
5477
5471
|
functionName: "withdrawAllAvailableEpochs",
|
|
5478
|
-
args: [tokenAddress]
|
|
5472
|
+
args: [tokenAddress],
|
|
5473
|
+
account
|
|
5479
5474
|
});
|
|
5475
|
+
const gasLimit = estimatedGas + estimatedGas / 5n;
|
|
5480
5476
|
const hash = await walletClient.writeContract({
|
|
5481
|
-
|
|
5477
|
+
address: settlementConfig.address,
|
|
5478
|
+
abi: settlementConfig.abi,
|
|
5479
|
+
functionName: "withdrawAllAvailableEpochs",
|
|
5480
|
+
args: [tokenAddress],
|
|
5481
|
+
account,
|
|
5482
5482
|
gas: gasLimit
|
|
5483
5483
|
});
|
|
5484
5484
|
await publicClient.waitForTransactionReceipt({ hash });
|
|
5485
5485
|
return hash;
|
|
5486
5486
|
}
|
|
5487
5487
|
|
|
5488
|
-
|
|
5488
|
+
// src/index.ts
|
|
5489
|
+
init_utils();
|
|
5490
|
+
init_errors();
|
|
5491
|
+
|
|
5492
|
+
export { ContractName, D402Client, Invalid402ResponseError, MissingRequestConfigError, PaymentAlreadyAttemptedError, PaymentAmountExceededError, PaymentError, PaymentVerificationError, UnsupportedNetworkError, UnsupportedSchemeError, createIATPWallet, createPaymentSelector, decodePayment, decodePaymentResponse, encodePayment, executeWithdrawal, findMatchingPaymentRequirement, formatMoney, generateNonce, getAvailableBalance, getChainId, getContractAbi, getContractAddress, getContractConfig, getCurrentTimestamp, getLockedBalanceForProvider, getUnlockedBalanceForProvider, getUsdcAddress, getWalletsByOwner, getWithdrawalRequest, isValidAddress, normalizeAddress, parseAllPaymentRequirements, parseMoney, parsePaymentRequirement, requestWithdrawal, selectPaymentRequirement, signD402Payment, sortPaymentRequirements, usdToUsdc, withdrawAllAvailableEpochs };
|
|
5489
5493
|
//# sourceMappingURL=index.mjs.map
|
|
5490
5494
|
//# sourceMappingURL=index.mjs.map
|