moltspay 1.4.0 → 1.4.1
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/.env.example +14 -0
- package/README.md +140 -93
- package/dist/cli/index.js +59 -25
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/index.mjs +59 -25
- package/dist/cli/index.mjs.map +1 -1
- package/dist/client/index.d.mts +3 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +36 -18
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +36 -18
- package/dist/client/index.mjs.map +1 -1
- package/dist/index.js +40 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -20
- package/dist/index.mjs.map +1 -1
- package/dist/server/index.js +4 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +4 -2
- package/dist/server/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/client/index.mjs
CHANGED
|
@@ -398,11 +398,26 @@ var MoltsPayClient = class {
|
|
|
398
398
|
throw new Error("Client not initialized. Run: npx moltspay init");
|
|
399
399
|
}
|
|
400
400
|
console.log(`[MoltsPay] Requesting service: ${service}`);
|
|
401
|
-
|
|
401
|
+
let executeUrl = `${serverUrl}/execute`;
|
|
402
|
+
try {
|
|
403
|
+
const services = await this.getServices(serverUrl);
|
|
404
|
+
const svc = services.services?.find((s) => s.id === service);
|
|
405
|
+
if (svc?.endpoint) {
|
|
406
|
+
executeUrl = `${serverUrl}${svc.endpoint}`;
|
|
407
|
+
console.log(`[MoltsPay] Using service endpoint: ${svc.endpoint}`);
|
|
408
|
+
}
|
|
409
|
+
} catch {
|
|
410
|
+
}
|
|
411
|
+
let requestBody;
|
|
412
|
+
if (options.rawData) {
|
|
413
|
+
requestBody = { service, ...params };
|
|
414
|
+
} else {
|
|
415
|
+
requestBody = { service, params };
|
|
416
|
+
}
|
|
402
417
|
if (options.chain) {
|
|
403
418
|
requestBody.chain = options.chain;
|
|
404
419
|
}
|
|
405
|
-
const initialRes = await fetch(
|
|
420
|
+
const initialRes = await fetch(executeUrl, {
|
|
406
421
|
method: "POST",
|
|
407
422
|
headers: { "Content-Type": "application/json" },
|
|
408
423
|
body: JSON.stringify(requestBody)
|
|
@@ -418,7 +433,7 @@ var MoltsPayClient = class {
|
|
|
418
433
|
const paymentRequiredHeader = initialRes.headers.get(PAYMENT_REQUIRED_HEADER);
|
|
419
434
|
if (wwwAuthHeader && wwwAuthHeader.toLowerCase().includes("payment")) {
|
|
420
435
|
console.log("[MoltsPay] Detected MPP protocol, using Tempo flow...");
|
|
421
|
-
return await this.handleMPPPayment(
|
|
436
|
+
return await this.handleMPPPayment(executeUrl, service, params, wwwAuthHeader, options);
|
|
422
437
|
}
|
|
423
438
|
if (!paymentRequiredHeader) {
|
|
424
439
|
throw new Error("Missing payment header (x-payment-required or www-authenticate)");
|
|
@@ -479,7 +494,7 @@ Please specify: --chain <chain_name>`
|
|
|
479
494
|
if (!req2) {
|
|
480
495
|
throw new Error(`Failed to find payment requirement for ${selectedChain}`);
|
|
481
496
|
}
|
|
482
|
-
return await this.handleSolanaPayment(
|
|
497
|
+
return await this.handleSolanaPayment(executeUrl, service, params, req2, solanaChain, options);
|
|
483
498
|
}
|
|
484
499
|
const chainName = selectedChain;
|
|
485
500
|
const chain = getChain(chainName);
|
|
@@ -526,14 +541,14 @@ Please specify: --chain <chain_name>`
|
|
|
526
541
|
if (!bnbSpender) {
|
|
527
542
|
throw new Error("Server did not provide bnbSpender address. Server may not support BNB payments.");
|
|
528
543
|
}
|
|
529
|
-
return await this.handleBNBPayment(
|
|
544
|
+
return await this.handleBNBPayment(executeUrl, service, params, {
|
|
530
545
|
to: payTo2,
|
|
531
546
|
amount,
|
|
532
547
|
token,
|
|
533
548
|
chainName,
|
|
534
549
|
chain,
|
|
535
550
|
spender: bnbSpender
|
|
536
|
-
});
|
|
551
|
+
}, options);
|
|
537
552
|
}
|
|
538
553
|
const payTo = req.payTo || req.resource;
|
|
539
554
|
if (!payTo) {
|
|
@@ -564,11 +579,11 @@ Please specify: --chain <chain_name>`
|
|
|
564
579
|
};
|
|
565
580
|
const paymentHeader = Buffer.from(JSON.stringify(payload)).toString("base64");
|
|
566
581
|
console.log(`[MoltsPay] Sending request with payment...`);
|
|
567
|
-
const paidRequestBody = { service, params };
|
|
582
|
+
const paidRequestBody = options.rawData ? { service, ...params } : { service, params };
|
|
568
583
|
if (options.chain) {
|
|
569
584
|
paidRequestBody.chain = options.chain;
|
|
570
585
|
}
|
|
571
|
-
const paidRes = await fetch(
|
|
586
|
+
const paidRes = await fetch(executeUrl, {
|
|
572
587
|
method: "POST",
|
|
573
588
|
headers: {
|
|
574
589
|
"Content-Type": "application/json",
|
|
@@ -582,13 +597,13 @@ Please specify: --chain <chain_name>`
|
|
|
582
597
|
}
|
|
583
598
|
this.recordSpending(amount);
|
|
584
599
|
console.log(`[MoltsPay] Success! Payment: ${result.payment?.status || "claimed"}`);
|
|
585
|
-
return result.result;
|
|
600
|
+
return result.result || result;
|
|
586
601
|
}
|
|
587
602
|
/**
|
|
588
603
|
* Handle MPP (Machine Payments Protocol) payment flow
|
|
589
604
|
* Called when pay() detects WWW-Authenticate header in 402 response
|
|
590
605
|
*/
|
|
591
|
-
async handleMPPPayment(
|
|
606
|
+
async handleMPPPayment(executeUrl, service, params, wwwAuthHeader, options = {}) {
|
|
592
607
|
const { privateKeyToAccount } = await import("viem/accounts");
|
|
593
608
|
const { createWalletClient, createPublicClient, http } = await import("viem");
|
|
594
609
|
const { tempoModerato } = await import("viem/chains");
|
|
@@ -649,13 +664,14 @@ Please specify: --chain <chain_name>`
|
|
|
649
664
|
source: `did:pkh:eip155:${chainId}:${account.address}`
|
|
650
665
|
};
|
|
651
666
|
const credentialB64 = Buffer.from(JSON.stringify(credential)).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
652
|
-
const
|
|
667
|
+
const retryBody = options.rawData ? { service, ...params, chain: "tempo_moderato" } : { service, params, chain: "tempo_moderato" };
|
|
668
|
+
const paidRes = await fetch(executeUrl, {
|
|
653
669
|
method: "POST",
|
|
654
670
|
headers: {
|
|
655
671
|
"Content-Type": "application/json",
|
|
656
672
|
"Authorization": `Payment ${credentialB64}`
|
|
657
673
|
},
|
|
658
|
-
body: JSON.stringify(
|
|
674
|
+
body: JSON.stringify(retryBody)
|
|
659
675
|
});
|
|
660
676
|
const result = await paidRes.json();
|
|
661
677
|
if (!paidRes.ok) {
|
|
@@ -675,7 +691,7 @@ Please specify: --chain <chain_name>`
|
|
|
675
691
|
* 4. Server executes service
|
|
676
692
|
* 5. Server calls transferFrom if successful (pay-for-success)
|
|
677
693
|
*/
|
|
678
|
-
async handleBNBPayment(
|
|
694
|
+
async handleBNBPayment(executeUrl, service, params, paymentDetails, options = {}) {
|
|
679
695
|
const { to, amount, token, chainName, chain, spender } = paymentDetails;
|
|
680
696
|
const tokenConfig = chain.tokens[token];
|
|
681
697
|
const provider = new ethers.JsonRpcProvider(chain.rpc);
|
|
@@ -771,13 +787,14 @@ Run: npx moltspay approve --chain ${chainName} --spender ${spender}`
|
|
|
771
787
|
};
|
|
772
788
|
const paymentHeader = Buffer.from(JSON.stringify(payload)).toString("base64");
|
|
773
789
|
console.log(`[MoltsPay] Sending BNB payment request...`);
|
|
774
|
-
const
|
|
790
|
+
const bnbRequestBody = options.rawData ? { service, ...params, chain: chainName } : { service, params, chain: chainName };
|
|
791
|
+
const paidRes = await fetch(executeUrl, {
|
|
775
792
|
method: "POST",
|
|
776
793
|
headers: {
|
|
777
794
|
"Content-Type": "application/json",
|
|
778
795
|
"X-Payment": paymentHeader
|
|
779
796
|
},
|
|
780
|
-
body: JSON.stringify(
|
|
797
|
+
body: JSON.stringify(bnbRequestBody)
|
|
781
798
|
});
|
|
782
799
|
const result = await paidRes.json();
|
|
783
800
|
if (!paidRes.ok) {
|
|
@@ -794,7 +811,7 @@ Run: npx moltspay approve --chain ${chainName} --spender ${spender}`
|
|
|
794
811
|
* 1. Client creates and signs a transfer transaction
|
|
795
812
|
* 2. Server submits the transaction after service completes
|
|
796
813
|
*/
|
|
797
|
-
async handleSolanaPayment(
|
|
814
|
+
async handleSolanaPayment(executeUrl, service, params, requirements, chain, options = {}) {
|
|
798
815
|
const solanaWallet = loadSolanaWallet(this.configDir);
|
|
799
816
|
if (!solanaWallet) {
|
|
800
817
|
throw new Error("No Solana wallet found. Run: npx moltspay init --chain solana_devnet");
|
|
@@ -847,13 +864,14 @@ Run: npx moltspay approve --chain ${chainName} --spender ${spender}`
|
|
|
847
864
|
}
|
|
848
865
|
};
|
|
849
866
|
const paymentHeader = Buffer.from(JSON.stringify(payload)).toString("base64");
|
|
850
|
-
const
|
|
867
|
+
const solanaRequestBody = options.rawData ? { service, ...params, chain } : { service, params, chain };
|
|
868
|
+
const paidRes = await fetch(executeUrl, {
|
|
851
869
|
method: "POST",
|
|
852
870
|
headers: {
|
|
853
871
|
"Content-Type": "application/json",
|
|
854
872
|
"X-Payment": paymentHeader
|
|
855
873
|
},
|
|
856
|
-
body: JSON.stringify(
|
|
874
|
+
body: JSON.stringify(solanaRequestBody)
|
|
857
875
|
});
|
|
858
876
|
const result = await paidRes.json();
|
|
859
877
|
if (!paidRes.ok) {
|