@dcentralab/d402-client 0.1.0 → 0.1.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/README.md +37 -68
- package/dist/index.d.mts +13 -13
- package/dist/index.d.ts +13 -13
- package/dist/index.js +17 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +17 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -325,7 +325,9 @@ function normalizeAddress(address) {
|
|
|
325
325
|
}
|
|
326
326
|
function decodePaymentResponse(header) {
|
|
327
327
|
try {
|
|
328
|
-
const
|
|
328
|
+
const binString = atob(header);
|
|
329
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
330
|
+
const decoded = new TextDecoder().decode(bytes);
|
|
329
331
|
return JSON.parse(decoded);
|
|
330
332
|
} catch (error) {
|
|
331
333
|
throw new Error(`Failed to decode payment response: ${error}`);
|
|
@@ -347,11 +349,11 @@ async function signD402Payment(params) {
|
|
|
347
349
|
const {
|
|
348
350
|
operatorAccount,
|
|
349
351
|
paymentRequirement,
|
|
350
|
-
|
|
352
|
+
iatpWalletAddress,
|
|
351
353
|
requestPath,
|
|
352
354
|
d402Version = 1
|
|
353
355
|
} = params;
|
|
354
|
-
const consumerWallet =
|
|
356
|
+
const consumerWallet = iatpWalletAddress || operatorAccount.address;
|
|
355
357
|
let finalRequestPath = requestPath || paymentRequirement.resource || "/mcp";
|
|
356
358
|
if (!finalRequestPath || finalRequestPath.trim() === "") {
|
|
357
359
|
finalRequestPath = "/mcp";
|
|
@@ -425,11 +427,14 @@ __export(encoder_exports, {
|
|
|
425
427
|
safeBase64Encode: () => safeBase64Encode
|
|
426
428
|
});
|
|
427
429
|
function safeBase64Encode(data) {
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
+
const bytes = new TextEncoder().encode(data);
|
|
431
|
+
const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join("");
|
|
432
|
+
return btoa(binString);
|
|
430
433
|
}
|
|
431
434
|
function safeBase64Decode(data) {
|
|
432
|
-
|
|
435
|
+
const binString = atob(data);
|
|
436
|
+
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
437
|
+
return new TextDecoder().decode(bytes);
|
|
433
438
|
}
|
|
434
439
|
function encodePayment(payment) {
|
|
435
440
|
const jsonString = JSON.stringify(payment);
|
|
@@ -538,7 +543,7 @@ var D402Client = class {
|
|
|
538
543
|
*/
|
|
539
544
|
constructor(config) {
|
|
540
545
|
this.operatorAccount = config.operatorAccount;
|
|
541
|
-
this.
|
|
546
|
+
this.iatpWalletAddress = config.iatpWalletAddress || config.operatorAccount.address;
|
|
542
547
|
this.maxValue = config.maxValue;
|
|
543
548
|
this.networkFilter = config.networkFilter;
|
|
544
549
|
this.schemeFilter = config.schemeFilter || "exact";
|
|
@@ -560,12 +565,12 @@ var D402Client = class {
|
|
|
560
565
|
});
|
|
561
566
|
}
|
|
562
567
|
/**
|
|
563
|
-
* Get the wallet address used for payments.
|
|
568
|
+
* Get the IATP wallet address used for payments.
|
|
564
569
|
*
|
|
565
570
|
* @returns IATPWallet contract address or operator EOA address
|
|
566
571
|
*/
|
|
567
|
-
|
|
568
|
-
return this.
|
|
572
|
+
getIATPWalletAddress() {
|
|
573
|
+
return this.iatpWalletAddress;
|
|
569
574
|
}
|
|
570
575
|
/**
|
|
571
576
|
* Get the operator account used for signing.
|
|
@@ -602,7 +607,7 @@ var D402Client = class {
|
|
|
602
607
|
*
|
|
603
608
|
* @example
|
|
604
609
|
* ```ts
|
|
605
|
-
* const client = new D402Client({ operatorAccount,
|
|
610
|
+
* const client = new D402Client({ operatorAccount, iatpWalletAddress })
|
|
606
611
|
*
|
|
607
612
|
* const response = await client.fetch('http://api.example.com/analyze', {
|
|
608
613
|
* method: 'POST',
|
|
@@ -627,7 +632,7 @@ var D402Client = class {
|
|
|
627
632
|
const signedPayment = await signD402Payment2({
|
|
628
633
|
operatorAccount: this.operatorAccount,
|
|
629
634
|
paymentRequirement: selectedRequirement,
|
|
630
|
-
|
|
635
|
+
iatpWalletAddress: this.iatpWalletAddress
|
|
631
636
|
});
|
|
632
637
|
const paymentHeader = encodePayment2(signedPayment);
|
|
633
638
|
response = await fetch(url, {
|