moltspay 0.2.0 → 0.2.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/dist/chains/index.d.mts +6 -6
- package/dist/chains/index.d.ts +6 -6
- package/dist/chains/index.js +2 -2
- package/dist/chains/index.js.map +1 -1
- package/dist/chains/index.mjs +2 -2
- package/dist/chains/index.mjs.map +1 -1
- package/dist/cli.js +36 -36
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +36 -36
- package/dist/cli.mjs.map +1 -1
- package/dist/guide/index.d.mts +7 -7
- package/dist/guide/index.d.ts +7 -7
- package/dist/guide/index.js +50 -50
- package/dist/guide/index.js.map +1 -1
- package/dist/guide/index.mjs +50 -50
- package/dist/guide/index.mjs.map +1 -1
- package/dist/{index-CZzgdtin.d.mts → index-CyFg9s2m.d.mts} +1 -1
- package/dist/{index-CZzgdtin.d.ts → index-CyFg9s2m.d.ts} +1 -1
- package/dist/index.d.mts +89 -89
- package/dist/index.d.ts +89 -89
- package/dist/index.js +275 -275
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +275 -275
- package/dist/index.mjs.map +1 -1
- package/dist/orders/index.d.mts +13 -13
- package/dist/orders/index.d.ts +13 -13
- package/dist/orders/index.js +10 -10
- package/dist/orders/index.js.map +1 -1
- package/dist/orders/index.mjs +10 -10
- package/dist/orders/index.mjs.map +1 -1
- package/dist/permit/index.d.mts +11 -11
- package/dist/permit/index.d.ts +11 -11
- package/dist/permit/index.js +14 -14
- package/dist/permit/index.js.map +1 -1
- package/dist/permit/index.mjs +14 -14
- package/dist/permit/index.mjs.map +1 -1
- package/dist/verify/index.d.mts +4 -4
- package/dist/verify/index.d.ts +4 -4
- package/dist/verify/index.js +13 -13
- package/dist/verify/index.js.map +1 -1
- package/dist/verify/index.mjs +13 -13
- package/dist/verify/index.mjs.map +1 -1
- package/dist/wallet/index.d.mts +72 -72
- package/dist/wallet/index.d.ts +72 -72
- package/dist/wallet/index.js +48 -48
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +48 -48
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { ethers } from "ethers";
|
|
|
3
3
|
|
|
4
4
|
// src/chains/index.ts
|
|
5
5
|
var CHAINS = {
|
|
6
|
-
// ============
|
|
6
|
+
// ============ Mainnet ============
|
|
7
7
|
base: {
|
|
8
8
|
name: "Base",
|
|
9
9
|
chainId: 8453,
|
|
@@ -31,7 +31,7 @@ var CHAINS = {
|
|
|
31
31
|
explorerTx: "https://etherscan.io/tx/",
|
|
32
32
|
avgBlockTime: 12
|
|
33
33
|
},
|
|
34
|
-
// ============
|
|
34
|
+
// ============ Testnet ============
|
|
35
35
|
base_sepolia: {
|
|
36
36
|
name: "Base Sepolia",
|
|
37
37
|
chainId: 84532,
|
|
@@ -102,7 +102,7 @@ var PaymentAgent = class _PaymentAgent {
|
|
|
102
102
|
);
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
|
-
*
|
|
105
|
+
* Generate payment request(Invoice)
|
|
106
106
|
*/
|
|
107
107
|
createInvoice(params) {
|
|
108
108
|
const expiresMinutes = params.expiresMinutes || 30;
|
|
@@ -129,14 +129,14 @@ var PaymentAgent = class _PaymentAgent {
|
|
|
129
129
|
return invoice;
|
|
130
130
|
}
|
|
131
131
|
/**
|
|
132
|
-
*
|
|
132
|
+
* Generate wallet deep link(supports MetaMask etc)
|
|
133
133
|
*/
|
|
134
134
|
generateDeepLink(amount, memo) {
|
|
135
135
|
const amountWei = Math.floor(amount * 1e6);
|
|
136
136
|
return `https://metamask.app.link/send/${this.chainConfig.usdc}@${this.chainConfig.chainId}/transfer?address=${this.walletAddress}&uint256=${amountWei}`;
|
|
137
137
|
}
|
|
138
138
|
/**
|
|
139
|
-
*
|
|
139
|
+
* Verify on-chain payment
|
|
140
140
|
*/
|
|
141
141
|
async verifyPayment(txHash, options = {}) {
|
|
142
142
|
try {
|
|
@@ -191,7 +191,7 @@ var PaymentAgent = class _PaymentAgent {
|
|
|
191
191
|
}
|
|
192
192
|
}
|
|
193
193
|
/**
|
|
194
|
-
*
|
|
194
|
+
* Scan recent transfers (match by amount)
|
|
195
195
|
*/
|
|
196
196
|
async scanRecentTransfers(expectedAmount, timeoutMinutes = 30) {
|
|
197
197
|
try {
|
|
@@ -229,7 +229,7 @@ var PaymentAgent = class _PaymentAgent {
|
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
/**
|
|
232
|
-
*
|
|
232
|
+
* Get wallet balance
|
|
233
233
|
*/
|
|
234
234
|
async getBalance(address) {
|
|
235
235
|
const addr = address || this.walletAddress;
|
|
@@ -245,7 +245,7 @@ var PaymentAgent = class _PaymentAgent {
|
|
|
245
245
|
};
|
|
246
246
|
}
|
|
247
247
|
/**
|
|
248
|
-
*
|
|
248
|
+
* Format Invoice as human-readable message
|
|
249
249
|
*/
|
|
250
250
|
formatInvoiceMessage(invoice, includeJson = true) {
|
|
251
251
|
let msg = `\u{1F3AC} **Payment Request**
|
|
@@ -307,7 +307,7 @@ var Wallet = class {
|
|
|
307
307
|
);
|
|
308
308
|
}
|
|
309
309
|
/**
|
|
310
|
-
*
|
|
310
|
+
* Get wallet balance
|
|
311
311
|
*/
|
|
312
312
|
async getBalance() {
|
|
313
313
|
const [ethBalance, usdcBalance] = await Promise.all([
|
|
@@ -322,7 +322,7 @@ var Wallet = class {
|
|
|
322
322
|
};
|
|
323
323
|
}
|
|
324
324
|
/**
|
|
325
|
-
*
|
|
325
|
+
* Send USDC transfer
|
|
326
326
|
*/
|
|
327
327
|
async transfer(to, amount) {
|
|
328
328
|
try {
|
|
@@ -363,14 +363,14 @@ var Wallet = class {
|
|
|
363
363
|
}
|
|
364
364
|
}
|
|
365
365
|
/**
|
|
366
|
-
*
|
|
366
|
+
* Get ETH balance
|
|
367
367
|
*/
|
|
368
368
|
async getEthBalance() {
|
|
369
369
|
const balance = await this.provider.getBalance(this.address);
|
|
370
370
|
return ethers2.formatEther(balance);
|
|
371
371
|
}
|
|
372
372
|
/**
|
|
373
|
-
*
|
|
373
|
+
* Get USDC balance
|
|
374
374
|
*/
|
|
375
375
|
async getUsdcBalance() {
|
|
376
376
|
const balance = await this.usdcContract.balanceOf(this.address);
|
|
@@ -391,7 +391,7 @@ var AuditLog = class {
|
|
|
391
391
|
this.loadLastHash();
|
|
392
392
|
}
|
|
393
393
|
/**
|
|
394
|
-
*
|
|
394
|
+
* Record audit log
|
|
395
395
|
*/
|
|
396
396
|
async log(params) {
|
|
397
397
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -408,7 +408,7 @@ var AuditLog = class {
|
|
|
408
408
|
requester: params.requester,
|
|
409
409
|
prev_hash: this.lastHash,
|
|
410
410
|
hash: "",
|
|
411
|
-
//
|
|
411
|
+
// Filled after calculation
|
|
412
412
|
metadata: params.metadata
|
|
413
413
|
};
|
|
414
414
|
entry.hash = this.calculateHash(entry);
|
|
@@ -419,7 +419,7 @@ var AuditLog = class {
|
|
|
419
419
|
return entry;
|
|
420
420
|
}
|
|
421
421
|
/**
|
|
422
|
-
*
|
|
422
|
+
* Read logs for specified date
|
|
423
423
|
*/
|
|
424
424
|
read(date) {
|
|
425
425
|
const filePath = this.getFilePath(date || /* @__PURE__ */ new Date());
|
|
@@ -431,7 +431,7 @@ var AuditLog = class {
|
|
|
431
431
|
return lines.map((line) => JSON.parse(line));
|
|
432
432
|
}
|
|
433
433
|
/**
|
|
434
|
-
*
|
|
434
|
+
* Verify log integrity
|
|
435
435
|
*/
|
|
436
436
|
verify(date) {
|
|
437
437
|
const entries = this.read(date);
|
|
@@ -449,7 +449,7 @@ var AuditLog = class {
|
|
|
449
449
|
return { valid: errors.length === 0, errors };
|
|
450
450
|
}
|
|
451
451
|
/**
|
|
452
|
-
*
|
|
452
|
+
* Search logs
|
|
453
453
|
*/
|
|
454
454
|
search(filter) {
|
|
455
455
|
const results = [];
|
|
@@ -473,14 +473,14 @@ var AuditLog = class {
|
|
|
473
473
|
return results;
|
|
474
474
|
}
|
|
475
475
|
/**
|
|
476
|
-
*
|
|
476
|
+
* Get log file path
|
|
477
477
|
*/
|
|
478
478
|
getFilePath(date) {
|
|
479
479
|
const dateStr = date.toISOString().slice(0, 10);
|
|
480
480
|
return path.join(this.basePath, `audit_${dateStr}.jsonl`);
|
|
481
481
|
}
|
|
482
482
|
/**
|
|
483
|
-
*
|
|
483
|
+
* Calculate entry hash
|
|
484
484
|
*/
|
|
485
485
|
calculateHash(entry) {
|
|
486
486
|
const data = {
|
|
@@ -497,7 +497,7 @@ var AuditLog = class {
|
|
|
497
497
|
return crypto.createHash("sha256").update(str).digest("hex").slice(0, 16);
|
|
498
498
|
}
|
|
499
499
|
/**
|
|
500
|
-
*
|
|
500
|
+
* Load last log entry hash
|
|
501
501
|
*/
|
|
502
502
|
loadLastHash() {
|
|
503
503
|
const today = /* @__PURE__ */ new Date();
|
|
@@ -512,7 +512,7 @@ var AuditLog = class {
|
|
|
512
512
|
}
|
|
513
513
|
}
|
|
514
514
|
/**
|
|
515
|
-
*
|
|
515
|
+
* Ensure directory exists
|
|
516
516
|
*/
|
|
517
517
|
ensureDir() {
|
|
518
518
|
if (!fs.existsSync(this.basePath)) {
|
|
@@ -524,9 +524,9 @@ var AuditLog = class {
|
|
|
524
524
|
// src/wallet/SecureWallet.ts
|
|
525
525
|
var DEFAULT_LIMITS = {
|
|
526
526
|
singleMax: 100,
|
|
527
|
-
//
|
|
527
|
+
// Single max $100
|
|
528
528
|
dailyMax: 1e3,
|
|
529
|
-
//
|
|
529
|
+
// Daily max $1000
|
|
530
530
|
requireWhitelist: true
|
|
531
531
|
};
|
|
532
532
|
var SecureWallet = class {
|
|
@@ -547,21 +547,21 @@ var SecureWallet = class {
|
|
|
547
547
|
this.auditLog = new AuditLog(config.auditPath);
|
|
548
548
|
}
|
|
549
549
|
/**
|
|
550
|
-
*
|
|
550
|
+
* Get wallet address
|
|
551
551
|
*/
|
|
552
552
|
get address() {
|
|
553
553
|
return this.wallet.address;
|
|
554
554
|
}
|
|
555
555
|
/**
|
|
556
|
-
*
|
|
556
|
+
* Get balance
|
|
557
557
|
*/
|
|
558
558
|
async getBalance() {
|
|
559
559
|
return this.wallet.getBalance();
|
|
560
560
|
}
|
|
561
561
|
/**
|
|
562
|
-
*
|
|
562
|
+
* Secure transfer (with limit and whitelist checks)
|
|
563
563
|
*
|
|
564
|
-
*
|
|
564
|
+
* Supports two calling methods:
|
|
565
565
|
* - transfer({ to, amount, reason?, requester? })
|
|
566
566
|
* - transfer(to, amount)
|
|
567
567
|
*/
|
|
@@ -661,7 +661,7 @@ var SecureWallet = class {
|
|
|
661
661
|
return result;
|
|
662
662
|
}
|
|
663
663
|
/**
|
|
664
|
-
*
|
|
664
|
+
* Approve pending transfer
|
|
665
665
|
*/
|
|
666
666
|
async approve(requestId, approver) {
|
|
667
667
|
const pending = this.pendingTransfers.get(requestId);
|
|
@@ -702,7 +702,7 @@ var SecureWallet = class {
|
|
|
702
702
|
return result;
|
|
703
703
|
}
|
|
704
704
|
/**
|
|
705
|
-
*
|
|
705
|
+
* Reject pending transfer
|
|
706
706
|
*/
|
|
707
707
|
async reject(requestId, rejecter, reason) {
|
|
708
708
|
const pending = this.pendingTransfers.get(requestId);
|
|
@@ -717,7 +717,7 @@ var SecureWallet = class {
|
|
|
717
717
|
});
|
|
718
718
|
}
|
|
719
719
|
/**
|
|
720
|
-
*
|
|
720
|
+
* Add to whitelist
|
|
721
721
|
*/
|
|
722
722
|
async addToWhitelist(address, addedBy) {
|
|
723
723
|
const addr = address.toLowerCase();
|
|
@@ -730,7 +730,7 @@ var SecureWallet = class {
|
|
|
730
730
|
});
|
|
731
731
|
}
|
|
732
732
|
/**
|
|
733
|
-
*
|
|
733
|
+
* Remove from whitelist
|
|
734
734
|
*/
|
|
735
735
|
async removeFromWhitelist(address, removedBy) {
|
|
736
736
|
const addr = address.toLowerCase();
|
|
@@ -743,32 +743,32 @@ var SecureWallet = class {
|
|
|
743
743
|
});
|
|
744
744
|
}
|
|
745
745
|
/**
|
|
746
|
-
*
|
|
746
|
+
* Check if address is whitelisted
|
|
747
747
|
*/
|
|
748
748
|
isWhitelisted(address) {
|
|
749
749
|
return this.whitelist.has(address.toLowerCase());
|
|
750
750
|
}
|
|
751
751
|
/**
|
|
752
|
-
*
|
|
752
|
+
* Get pending transfers list
|
|
753
753
|
*/
|
|
754
754
|
getPendingTransfers() {
|
|
755
755
|
return Array.from(this.pendingTransfers.values()).filter((p) => p.status === "pending");
|
|
756
756
|
}
|
|
757
757
|
/**
|
|
758
|
-
*
|
|
758
|
+
* Get current limit config
|
|
759
759
|
*/
|
|
760
760
|
getLimits() {
|
|
761
761
|
return { ...this.limits };
|
|
762
762
|
}
|
|
763
763
|
/**
|
|
764
|
-
*
|
|
764
|
+
* Get daily used amount
|
|
765
765
|
*/
|
|
766
766
|
getDailyUsed() {
|
|
767
767
|
this.updateDailyTotal();
|
|
768
768
|
return this.dailyTotal;
|
|
769
769
|
}
|
|
770
770
|
/**
|
|
771
|
-
*
|
|
771
|
+
* Update daily limit counter
|
|
772
772
|
*/
|
|
773
773
|
updateDailyTotal() {
|
|
774
774
|
const today = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
|
|
@@ -939,24 +939,24 @@ var PermitWallet = class {
|
|
|
939
939
|
);
|
|
940
940
|
}
|
|
941
941
|
/**
|
|
942
|
-
*
|
|
942
|
+
* Check if Permit is valid (current allowance)
|
|
943
943
|
*/
|
|
944
944
|
async checkPermitAllowance(owner) {
|
|
945
945
|
const allowance = await this.usdcContract.allowance(owner, this.address);
|
|
946
946
|
return (Number(allowance) / 1e6).toFixed(2);
|
|
947
947
|
}
|
|
948
948
|
/**
|
|
949
|
-
*
|
|
949
|
+
* Pay using Permit authorization
|
|
950
950
|
*
|
|
951
|
-
*
|
|
952
|
-
* 1.
|
|
953
|
-
* 2.
|
|
951
|
+
* Flow:
|
|
952
|
+
* 1. Call permit() to record Boss's authorization in the contract
|
|
953
|
+
* 2. Call transferFrom() to transfer from Boss's wallet to recipient
|
|
954
954
|
*
|
|
955
955
|
* @example
|
|
956
956
|
* ```typescript
|
|
957
957
|
* const wallet = new PermitWallet({ chain: 'base' });
|
|
958
958
|
*
|
|
959
|
-
* // Boss
|
|
959
|
+
* // Boss-signed permit data
|
|
960
960
|
* const permit = {
|
|
961
961
|
* owner: '0xBOSS...',
|
|
962
962
|
* spender: wallet.address,
|
|
@@ -1079,14 +1079,14 @@ var PermitWallet = class {
|
|
|
1079
1079
|
}
|
|
1080
1080
|
}
|
|
1081
1081
|
/**
|
|
1082
|
-
*
|
|
1082
|
+
* Get ETH balance (for gas)
|
|
1083
1083
|
*/
|
|
1084
1084
|
async getGasBalance() {
|
|
1085
1085
|
const balance = await this.provider.getBalance(this.address);
|
|
1086
1086
|
return ethers4.formatEther(balance);
|
|
1087
1087
|
}
|
|
1088
1088
|
/**
|
|
1089
|
-
*
|
|
1089
|
+
* Check if there's enough gas
|
|
1090
1090
|
*/
|
|
1091
1091
|
async hasEnoughGas(minEth = 1e-3) {
|
|
1092
1092
|
const balance = await this.getGasBalance();
|
|
@@ -1098,17 +1098,17 @@ function formatPermitRequest(params) {
|
|
|
1098
1098
|
const chainConfig = getChain(chain);
|
|
1099
1099
|
const deadline = Math.floor(Date.now() / 1e3) + deadlineHours * 3600;
|
|
1100
1100
|
const value = BigInt(Math.floor(amount * 1e6)).toString();
|
|
1101
|
-
return `\u{1F510} **USDC
|
|
1101
|
+
return `\u{1F510} **USDC Spending Allowance Request**
|
|
1102
1102
|
|
|
1103
|
-
${reason ?
|
|
1103
|
+
${reason ? `**Purpose:** ${reason}
|
|
1104
1104
|
` : ""}
|
|
1105
|
-
|
|
1106
|
-
-
|
|
1107
|
-
-
|
|
1108
|
-
-
|
|
1109
|
-
-
|
|
1105
|
+
**Authorization Details:**
|
|
1106
|
+
- Authorized address (Agent): \`${agentAddress}\`
|
|
1107
|
+
- Amount: ${amount} USDC
|
|
1108
|
+
- Valid for: ${deadlineHours} hours
|
|
1109
|
+
- Chain: ${chainConfig.name}
|
|
1110
1110
|
|
|
1111
|
-
|
|
1111
|
+
**Please sign the following EIP-2612 Permit with your wallet:**
|
|
1112
1112
|
|
|
1113
1113
|
\`\`\`json
|
|
1114
1114
|
{
|
|
@@ -1138,9 +1138,9 @@ ${reason ? `**\u7528\u9014:** ${reason}
|
|
|
1138
1138
|
}
|
|
1139
1139
|
\`\`\`
|
|
1140
1140
|
|
|
1141
|
-
|
|
1141
|
+
After signing, send { v, r, s, deadline } to the Agent.
|
|
1142
1142
|
|
|
1143
|
-
\u26A0\uFE0F
|
|
1143
|
+
\u26A0\uFE0F Note: This authorization only allows the Agent to spend up to ${amount} USDC from your wallet. Your private key is never exposed.`;
|
|
1144
1144
|
}
|
|
1145
1145
|
|
|
1146
1146
|
// src/permit/Permit.ts
|
|
@@ -1170,13 +1170,13 @@ var PermitPayment = class {
|
|
|
1170
1170
|
);
|
|
1171
1171
|
}
|
|
1172
1172
|
/**
|
|
1173
|
-
*
|
|
1173
|
+
* Get user current nonce
|
|
1174
1174
|
*/
|
|
1175
1175
|
async getNonce(owner) {
|
|
1176
1176
|
return Number(await this.usdcContract.nonces(owner));
|
|
1177
1177
|
}
|
|
1178
1178
|
/**
|
|
1179
|
-
*
|
|
1179
|
+
* Generate EIP-712 signing request (for frontend/user wallet)
|
|
1180
1180
|
*/
|
|
1181
1181
|
async createPermitRequest(owner, amount, orderId, deadlineMinutes = 30) {
|
|
1182
1182
|
const nonce = await this.getNonce(owner);
|
|
@@ -1224,11 +1224,11 @@ var PermitPayment = class {
|
|
|
1224
1224
|
};
|
|
1225
1225
|
}
|
|
1226
1226
|
/**
|
|
1227
|
-
*
|
|
1227
|
+
* Execute permit + transferFrom
|
|
1228
1228
|
*
|
|
1229
|
-
* @param owner
|
|
1230
|
-
* @param amount
|
|
1231
|
-
* @param signature
|
|
1229
|
+
* @param owner User address
|
|
1230
|
+
* @param amount Amount
|
|
1231
|
+
* @param signature User signature {v, r, s, deadline}
|
|
1232
1232
|
*/
|
|
1233
1233
|
async executePermitAndTransfer(owner, amount, signature) {
|
|
1234
1234
|
if (!this.wallet) {
|
|
@@ -1260,7 +1260,7 @@ var PermitPayment = class {
|
|
|
1260
1260
|
}
|
|
1261
1261
|
}
|
|
1262
1262
|
/**
|
|
1263
|
-
*
|
|
1263
|
+
* Execute permit only (no transfer)
|
|
1264
1264
|
*/
|
|
1265
1265
|
async executePermit(owner, amount, signature) {
|
|
1266
1266
|
if (!this.wallet) {
|
|
@@ -1290,22 +1290,22 @@ var PermitPayment = class {
|
|
|
1290
1290
|
}
|
|
1291
1291
|
}
|
|
1292
1292
|
/**
|
|
1293
|
-
*
|
|
1293
|
+
* Format Permit request as user message
|
|
1294
1294
|
*/
|
|
1295
1295
|
formatPermitMessage(request) {
|
|
1296
1296
|
const { typed_data } = request;
|
|
1297
1297
|
const { message } = typed_data;
|
|
1298
|
-
return `\u{1F510}
|
|
1298
|
+
return `\u{1F510} **Signature Authorization Request**
|
|
1299
1299
|
|
|
1300
|
-
|
|
1300
|
+
Authorize \`${(Number(message.value) / 1e6).toFixed(2)} USDC\` to service provider
|
|
1301
1301
|
|
|
1302
|
-
|
|
1302
|
+
**Signature Details:**
|
|
1303
1303
|
- Owner: \`${message.owner}\`
|
|
1304
1304
|
- Spender: \`${message.spender}\`
|
|
1305
1305
|
- Amount: ${(Number(message.value) / 1e6).toFixed(2)} USDC
|
|
1306
1306
|
- Deadline: ${new Date(message.deadline * 1e3).toISOString()}
|
|
1307
1307
|
|
|
1308
|
-
|
|
1308
|
+
Please sign this request in your wallet (no gas required).
|
|
1309
1309
|
|
|
1310
1310
|
\`\`\`json
|
|
1311
1311
|
${JSON.stringify(typed_data, null, 2)}
|
|
@@ -1350,13 +1350,13 @@ var OrderManager = class {
|
|
|
1350
1350
|
this.defaultChain = options.defaultChain || "base";
|
|
1351
1351
|
}
|
|
1352
1352
|
/**
|
|
1353
|
-
*
|
|
1353
|
+
* Generate order ID
|
|
1354
1354
|
*/
|
|
1355
1355
|
generateOrderId() {
|
|
1356
1356
|
return "vo_" + randomBytes2(4).toString("hex");
|
|
1357
1357
|
}
|
|
1358
1358
|
/**
|
|
1359
|
-
*
|
|
1359
|
+
* Create order
|
|
1360
1360
|
*/
|
|
1361
1361
|
async createOrder(params) {
|
|
1362
1362
|
const order = {
|
|
@@ -1373,13 +1373,13 @@ var OrderManager = class {
|
|
|
1373
1373
|
return order;
|
|
1374
1374
|
}
|
|
1375
1375
|
/**
|
|
1376
|
-
*
|
|
1376
|
+
* Get order
|
|
1377
1377
|
*/
|
|
1378
1378
|
async getOrder(orderId) {
|
|
1379
1379
|
return this.store.get(orderId);
|
|
1380
1380
|
}
|
|
1381
1381
|
/**
|
|
1382
|
-
*
|
|
1382
|
+
* Update order
|
|
1383
1383
|
*/
|
|
1384
1384
|
async updateOrder(orderId, updates) {
|
|
1385
1385
|
const order = await this.store.get(orderId);
|
|
@@ -1389,7 +1389,7 @@ var OrderManager = class {
|
|
|
1389
1389
|
return updated;
|
|
1390
1390
|
}
|
|
1391
1391
|
/**
|
|
1392
|
-
*
|
|
1392
|
+
* Find user pending orders
|
|
1393
1393
|
*/
|
|
1394
1394
|
async findPendingOrder(userId) {
|
|
1395
1395
|
const orders = await this.store.findByUser(userId, "pending");
|
|
@@ -1403,7 +1403,7 @@ var OrderManager = class {
|
|
|
1403
1403
|
return null;
|
|
1404
1404
|
}
|
|
1405
1405
|
/**
|
|
1406
|
-
*
|
|
1406
|
+
* Mark order as paid
|
|
1407
1407
|
*/
|
|
1408
1408
|
async markAsPaid(orderId, txHash, payerAddress) {
|
|
1409
1409
|
return this.updateOrder(orderId, {
|
|
@@ -1414,13 +1414,13 @@ var OrderManager = class {
|
|
|
1414
1414
|
});
|
|
1415
1415
|
}
|
|
1416
1416
|
/**
|
|
1417
|
-
*
|
|
1417
|
+
* Mark order as generating
|
|
1418
1418
|
*/
|
|
1419
1419
|
async markAsGenerating(orderId) {
|
|
1420
1420
|
return this.updateOrder(orderId, { status: "generating" });
|
|
1421
1421
|
}
|
|
1422
1422
|
/**
|
|
1423
|
-
*
|
|
1423
|
+
* Mark order as completed
|
|
1424
1424
|
*/
|
|
1425
1425
|
async markAsCompleted(orderId, videoPath) {
|
|
1426
1426
|
return this.updateOrder(orderId, {
|
|
@@ -1429,7 +1429,7 @@ var OrderManager = class {
|
|
|
1429
1429
|
});
|
|
1430
1430
|
}
|
|
1431
1431
|
/**
|
|
1432
|
-
*
|
|
1432
|
+
* Mark order as failed
|
|
1433
1433
|
*/
|
|
1434
1434
|
async markAsFailed(orderId, error) {
|
|
1435
1435
|
return this.updateOrder(orderId, {
|
|
@@ -1438,7 +1438,7 @@ var OrderManager = class {
|
|
|
1438
1438
|
});
|
|
1439
1439
|
}
|
|
1440
1440
|
/**
|
|
1441
|
-
*
|
|
1441
|
+
* Cancel order
|
|
1442
1442
|
*/
|
|
1443
1443
|
async cancelOrder(orderId) {
|
|
1444
1444
|
return this.updateOrder(orderId, { status: "cancelled" });
|
|
@@ -1458,23 +1458,23 @@ async function verifyPayment(params) {
|
|
|
1458
1458
|
chain = getChain(params.chain || "base");
|
|
1459
1459
|
}
|
|
1460
1460
|
if (!chain) {
|
|
1461
|
-
return { verified: false, error:
|
|
1461
|
+
return { verified: false, error: `Unsupported chain: ${params.chain}` };
|
|
1462
1462
|
}
|
|
1463
1463
|
} catch (e) {
|
|
1464
|
-
return { verified: false, error:
|
|
1464
|
+
return { verified: false, error: `Unsupported chain: ${params.chain}` };
|
|
1465
1465
|
}
|
|
1466
1466
|
try {
|
|
1467
1467
|
const provider = new ethers6.JsonRpcProvider(chain.rpc);
|
|
1468
1468
|
const receipt = await provider.getTransactionReceipt(txHash);
|
|
1469
1469
|
if (!receipt) {
|
|
1470
|
-
return { verified: false, error: "
|
|
1470
|
+
return { verified: false, error: "Transaction not found or not confirmed" };
|
|
1471
1471
|
}
|
|
1472
1472
|
if (receipt.status !== 1) {
|
|
1473
|
-
return { verified: false, error: "
|
|
1473
|
+
return { verified: false, error: "Transaction failed" };
|
|
1474
1474
|
}
|
|
1475
1475
|
const usdcAddress = chain.usdc?.toLowerCase();
|
|
1476
1476
|
if (!usdcAddress) {
|
|
1477
|
-
return { verified: false, error:
|
|
1477
|
+
return { verified: false, error: `Chain ${chain.name} USDC address not configured` };
|
|
1478
1478
|
}
|
|
1479
1479
|
for (const log of receipt.logs) {
|
|
1480
1480
|
if (log.address.toLowerCase() !== usdcAddress) {
|
|
@@ -1493,7 +1493,7 @@ async function verifyPayment(params) {
|
|
|
1493
1493
|
if (amount < expectedAmount) {
|
|
1494
1494
|
return {
|
|
1495
1495
|
verified: false,
|
|
1496
|
-
error:
|
|
1496
|
+
error: `Insufficient amount: received ${amount} USDC, expected ${expectedAmount} USDC`,
|
|
1497
1497
|
amount,
|
|
1498
1498
|
from,
|
|
1499
1499
|
to,
|
|
@@ -1510,7 +1510,7 @@ async function verifyPayment(params) {
|
|
|
1510
1510
|
blockNumber: receipt.blockNumber
|
|
1511
1511
|
};
|
|
1512
1512
|
}
|
|
1513
|
-
return { verified: false, error: "
|
|
1513
|
+
return { verified: false, error: "No USDC transfer found" };
|
|
1514
1514
|
} catch (e) {
|
|
1515
1515
|
return { verified: false, error: e.message || String(e) };
|
|
1516
1516
|
}
|
|
@@ -1556,19 +1556,19 @@ async function waitForTransaction(txHash, chain = "base", confirmations = 1, tim
|
|
|
1556
1556
|
try {
|
|
1557
1557
|
chainConfig = typeof chain === "number" ? getChainById(chain) : getChain(chain);
|
|
1558
1558
|
if (!chainConfig) {
|
|
1559
|
-
return { verified: false, confirmed: false, error:
|
|
1559
|
+
return { verified: false, confirmed: false, error: `Unsupported chain: ${chain}` };
|
|
1560
1560
|
}
|
|
1561
1561
|
} catch (e) {
|
|
1562
|
-
return { verified: false, confirmed: false, error:
|
|
1562
|
+
return { verified: false, confirmed: false, error: `Unsupported chain: ${chain}` };
|
|
1563
1563
|
}
|
|
1564
1564
|
const provider = new ethers6.JsonRpcProvider(chainConfig.rpc);
|
|
1565
1565
|
try {
|
|
1566
1566
|
const receipt = await provider.waitForTransaction(txHash, confirmations, timeoutMs);
|
|
1567
1567
|
if (!receipt) {
|
|
1568
|
-
return { verified: false, confirmed: false, error: "
|
|
1568
|
+
return { verified: false, confirmed: false, error: "Timeout waiting" };
|
|
1569
1569
|
}
|
|
1570
1570
|
if (receipt.status !== 1) {
|
|
1571
|
-
return { verified: false, confirmed: true, error: "
|
|
1571
|
+
return { verified: false, confirmed: true, error: "Transaction failed" };
|
|
1572
1572
|
}
|
|
1573
1573
|
return {
|
|
1574
1574
|
verified: true,
|
|
@@ -1589,131 +1589,131 @@ function generatePaymentGuide(params) {
|
|
|
1589
1589
|
price,
|
|
1590
1590
|
recipientAddress,
|
|
1591
1591
|
chain = "base",
|
|
1592
|
-
serviceName = "
|
|
1592
|
+
serviceName = "Video Generation Service"
|
|
1593
1593
|
} = params;
|
|
1594
1594
|
const chainId = chain === "base" ? 8453 : 84532;
|
|
1595
|
-
const chainName = chain === "base" ? "Base
|
|
1595
|
+
const chainName = chain === "base" ? "Base Mainnet" : "Base Sepolia Testnet";
|
|
1596
1596
|
const explorer = chain === "base" ? "https://basescan.org/tx/" : "https://sepolia.basescan.org/tx/";
|
|
1597
1597
|
return `\u{1F3AC} **${serviceName}**
|
|
1598
1598
|
|
|
1599
|
-
\u{1F4CB}
|
|
1600
|
-
-
|
|
1601
|
-
-
|
|
1602
|
-
-
|
|
1603
|
-
-
|
|
1599
|
+
\u{1F4CB} **Order Details:**
|
|
1600
|
+
- Order ID: \`${orderId}\`
|
|
1601
|
+
- Content: ${prompt.length > 50 ? prompt.slice(0, 50) + "..." : prompt}
|
|
1602
|
+
- Price: **${price} USDC**
|
|
1603
|
+
- Chain: ${chainName}
|
|
1604
1604
|
|
|
1605
1605
|
---
|
|
1606
1606
|
|
|
1607
|
-
## \u{1F4B3}
|
|
1607
|
+
## \u{1F4B3} How to Pay
|
|
1608
1608
|
|
|
1609
|
-
###
|
|
1609
|
+
### Option 1: Install moltspay SDK (Recommended)
|
|
1610
1610
|
|
|
1611
1611
|
\`\`\`bash
|
|
1612
1612
|
npm install moltspay ethers
|
|
1613
1613
|
\`\`\`
|
|
1614
1614
|
|
|
1615
|
-
|
|
1615
|
+
**Create wallet and pay:**
|
|
1616
1616
|
|
|
1617
1617
|
\`\`\`javascript
|
|
1618
1618
|
const { SecureWallet, getChain } = require('moltspay');
|
|
1619
1619
|
const { Wallet } = require('ethers');
|
|
1620
1620
|
|
|
1621
|
-
// 1.
|
|
1621
|
+
// 1. Create new wallet (first time)
|
|
1622
1622
|
const newWallet = Wallet.createRandom();
|
|
1623
|
-
console.log('
|
|
1624
|
-
console.log('
|
|
1623
|
+
console.log('Wallet address:', newWallet.address);
|
|
1624
|
+
console.log('Private key (save securely):', newWallet.privateKey);
|
|
1625
1625
|
|
|
1626
|
-
// 2.
|
|
1626
|
+
// 2. Or use existing wallet
|
|
1627
1627
|
const wallet = new SecureWallet({
|
|
1628
1628
|
chainId: ${chainId},
|
|
1629
|
-
privateKey: process.env.WALLET_KEY, //
|
|
1630
|
-
limits: { requireWhitelist: false } //
|
|
1629
|
+
privateKey: process.env.WALLET_KEY, // Your private key
|
|
1630
|
+
limits: { requireWhitelist: false } // Allow transfers to any address
|
|
1631
1631
|
});
|
|
1632
1632
|
|
|
1633
|
-
// 3.
|
|
1633
|
+
// 3. Check balance
|
|
1634
1634
|
const balance = await wallet.getBalance();
|
|
1635
|
-
console.log('USDC
|
|
1635
|
+
console.log('USDC balance:', balance.usdc);
|
|
1636
1636
|
|
|
1637
|
-
// 4.
|
|
1637
|
+
// 4. Pay
|
|
1638
1638
|
const result = await wallet.transfer('${recipientAddress}', '${price}');
|
|
1639
1639
|
if (result.success) {
|
|
1640
|
-
console.log('\u2705
|
|
1641
|
-
console.log('
|
|
1642
|
-
//
|
|
1640
|
+
console.log('\u2705 Payment successful!');
|
|
1641
|
+
console.log('Tx hash:', result.hash);
|
|
1642
|
+
// Send the tx hash to me
|
|
1643
1643
|
}
|
|
1644
1644
|
\`\`\`
|
|
1645
1645
|
|
|
1646
|
-
###
|
|
1646
|
+
### Option 2: Direct Transfer
|
|
1647
1647
|
|
|
1648
|
-
|
|
1649
|
-
-
|
|
1648
|
+
Transfer USDC from any wallet:
|
|
1649
|
+
- **Chain:** ${chainName} (Chain ID: ${chainId})
|
|
1650
1650
|
- **Token:** USDC
|
|
1651
|
-
-
|
|
1652
|
-
-
|
|
1651
|
+
- **Recipient:** \`${recipientAddress}\`
|
|
1652
|
+
- **Amount:** ${price} USDC
|
|
1653
1653
|
|
|
1654
1654
|
---
|
|
1655
1655
|
|
|
1656
|
-
## \u2705
|
|
1656
|
+
## \u2705 After Payment
|
|
1657
1657
|
|
|
1658
|
-
|
|
1658
|
+
Send me the **transaction hash**, format:
|
|
1659
1659
|
\`\`\`
|
|
1660
1660
|
tx: 0x1234...
|
|
1661
1661
|
\`\`\`
|
|
1662
1662
|
|
|
1663
|
-
|
|
1663
|
+
Or just send the hash directly:
|
|
1664
1664
|
\`\`\`
|
|
1665
1665
|
0x1234abcd...
|
|
1666
1666
|
\`\`\`
|
|
1667
1667
|
|
|
1668
|
-
|
|
1668
|
+
I'll verify the on-chain transaction and start processing your request.
|
|
1669
1669
|
|
|
1670
1670
|
---
|
|
1671
1671
|
|
|
1672
1672
|
\u{1F4E6} **moltspay:** https://www.npmjs.com/package/moltspay
|
|
1673
|
-
\u{1F517}
|
|
1673
|
+
\u{1F517} **Block Explorer:** ${explorer}`;
|
|
1674
1674
|
}
|
|
1675
1675
|
function generatePaymentReminder(params) {
|
|
1676
1676
|
const { orderId, price, recipientAddress, chain = "base" } = params;
|
|
1677
1677
|
const chainName = chain === "base" ? "Base" : "Base Sepolia";
|
|
1678
|
-
return `\u23F3
|
|
1678
|
+
return `\u23F3 **Pending Order: ${orderId}**
|
|
1679
1679
|
|
|
1680
|
-
|
|
1681
|
-
-
|
|
1682
|
-
-
|
|
1680
|
+
Please pay **${price} USDC** to:
|
|
1681
|
+
- Chain: ${chainName}
|
|
1682
|
+
- Address: \`${recipientAddress}\`
|
|
1683
1683
|
|
|
1684
|
-
|
|
1684
|
+
Send the tx hash after payment.
|
|
1685
1685
|
|
|
1686
1686
|
---
|
|
1687
|
-
\u{1F4A1}
|
|
1687
|
+
\u{1F4A1} No payment capability? Run \`npm install moltspay\` to get the payment SDK`;
|
|
1688
1688
|
}
|
|
1689
1689
|
function generateWalletGuide() {
|
|
1690
|
-
return `## \u{1F510}
|
|
1690
|
+
return `## \u{1F510} Create Payment Wallet
|
|
1691
1691
|
|
|
1692
|
-
**1.
|
|
1692
|
+
**1. Install moltspay:**
|
|
1693
1693
|
\`\`\`bash
|
|
1694
1694
|
npm install moltspay ethers
|
|
1695
1695
|
\`\`\`
|
|
1696
1696
|
|
|
1697
|
-
**2.
|
|
1697
|
+
**2. Create new wallet:**
|
|
1698
1698
|
\`\`\`javascript
|
|
1699
1699
|
const { Wallet } = require('ethers');
|
|
1700
1700
|
|
|
1701
1701
|
const wallet = Wallet.createRandom();
|
|
1702
|
-
console.log('
|
|
1703
|
-
console.log('
|
|
1704
|
-
// \u26A0\uFE0F
|
|
1702
|
+
console.log('Address:', wallet.address);
|
|
1703
|
+
console.log('Private key:', wallet.privateKey);
|
|
1704
|
+
// \u26A0\uFE0F Save the private key securely!
|
|
1705
1705
|
\`\`\`
|
|
1706
1706
|
|
|
1707
|
-
**3.
|
|
1708
|
-
-
|
|
1709
|
-
-
|
|
1707
|
+
**3. Get USDC:**
|
|
1708
|
+
- Mainnet: Withdraw from exchange to your wallet
|
|
1709
|
+
- Testnet: Use Base Sepolia faucet
|
|
1710
1710
|
|
|
1711
|
-
**4.
|
|
1711
|
+
**4. Set environment variable:**
|
|
1712
1712
|
\`\`\`bash
|
|
1713
|
-
export WALLET_KEY="
|
|
1713
|
+
export WALLET_KEY="your_private_key"
|
|
1714
1714
|
\`\`\`
|
|
1715
1715
|
|
|
1716
|
-
|
|
1716
|
+
You're now ready to make on-chain payments with moltspay!`;
|
|
1717
1717
|
}
|
|
1718
1718
|
function extractTransactionHash(message) {
|
|
1719
1719
|
const match = message.match(/0x[a-fA-F0-9]{64}/);
|
|
@@ -1773,77 +1773,77 @@ function generateReceiptFromInvoice(invoice, verifyResult, delivery) {
|
|
|
1773
1773
|
});
|
|
1774
1774
|
}
|
|
1775
1775
|
function formatReceiptMessage(receipt) {
|
|
1776
|
-
let msg = `\u{1F9FE}
|
|
1776
|
+
let msg = `\u{1F9FE} **Transaction Receipt**
|
|
1777
1777
|
|
|
1778
|
-
|
|
1779
|
-
|
|
1778
|
+
**Invoice:** \`${receipt.invoiceId}\`
|
|
1779
|
+
**Order:** \`${receipt.orderId}\`
|
|
1780
1780
|
|
|
1781
1781
|
---
|
|
1782
1782
|
|
|
1783
|
-
|
|
1784
|
-
${receipt.description ?
|
|
1783
|
+
**Service:** ${receipt.service}
|
|
1784
|
+
${receipt.description ? `**Description:** ${receipt.description}
|
|
1785
1785
|
` : ""}
|
|
1786
|
-
|
|
1787
|
-
|
|
1786
|
+
**Amount:** ${receipt.amount} ${receipt.token}
|
|
1787
|
+
**Chain:** ${receipt.chain} (Chain ID: ${receipt.chainId})
|
|
1788
1788
|
|
|
1789
1789
|
---
|
|
1790
1790
|
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1791
|
+
**Payer:** \`${receipt.payer}\`
|
|
1792
|
+
**Recipient:** \`${receipt.recipient}\`
|
|
1793
|
+
**Transaction:** [\`${receipt.txHash.slice(0, 10)}...${receipt.txHash.slice(-8)}\`](${receipt.txUrl})
|
|
1794
|
+
**Paid at:** ${receipt.paidAt}`;
|
|
1795
1795
|
if (receipt.delivery) {
|
|
1796
1796
|
msg += `
|
|
1797
1797
|
|
|
1798
1798
|
---
|
|
1799
1799
|
|
|
1800
|
-
|
|
1800
|
+
**Delivery Info:**`;
|
|
1801
1801
|
if (receipt.delivery.url) {
|
|
1802
1802
|
msg += `
|
|
1803
|
-
-
|
|
1803
|
+
- Download: ${receipt.delivery.url}`;
|
|
1804
1804
|
}
|
|
1805
1805
|
if (receipt.delivery.fileHash) {
|
|
1806
1806
|
msg += `
|
|
1807
|
-
-
|
|
1807
|
+
- Checksum: \`${receipt.delivery.fileHash}\``;
|
|
1808
1808
|
}
|
|
1809
1809
|
if (receipt.delivery.deliveredAt) {
|
|
1810
1810
|
msg += `
|
|
1811
|
-
-
|
|
1811
|
+
- Delivered at: ${receipt.delivery.deliveredAt}`;
|
|
1812
1812
|
}
|
|
1813
1813
|
}
|
|
1814
1814
|
msg += `
|
|
1815
1815
|
|
|
1816
1816
|
---
|
|
1817
1817
|
|
|
1818
|
-
|
|
1818
|
+
_Receipt issued: ${receipt.issuedAt}_`;
|
|
1819
1819
|
return msg;
|
|
1820
1820
|
}
|
|
1821
1821
|
function formatReceiptText(receipt) {
|
|
1822
|
-
let msg = `\u{1F9FE}
|
|
1822
|
+
let msg = `\u{1F9FE} Transaction Receipt
|
|
1823
1823
|
|
|
1824
|
-
|
|
1825
|
-
|
|
1824
|
+
Invoice: ${receipt.invoiceId}
|
|
1825
|
+
Order: ${receipt.orderId}
|
|
1826
1826
|
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1827
|
+
Service: ${receipt.service}
|
|
1828
|
+
Amount: ${receipt.amount} ${receipt.token}
|
|
1829
|
+
Chain: ${receipt.chain}
|
|
1830
1830
|
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1831
|
+
Payer: ${receipt.payer}
|
|
1832
|
+
Recipient: ${receipt.recipient}
|
|
1833
|
+
Tx: ${receipt.txHash}
|
|
1834
|
+
Explorer: ${receipt.txUrl}
|
|
1835
|
+
Paid at: ${receipt.paidAt}`;
|
|
1836
1836
|
if (receipt.delivery) {
|
|
1837
1837
|
msg += `
|
|
1838
1838
|
|
|
1839
|
-
|
|
1839
|
+
Delivery:`;
|
|
1840
1840
|
if (receipt.delivery.url) {
|
|
1841
1841
|
msg += `
|
|
1842
|
-
|
|
1842
|
+
Download: ${receipt.delivery.url}`;
|
|
1843
1843
|
}
|
|
1844
1844
|
if (receipt.delivery.fileHash) {
|
|
1845
1845
|
msg += `
|
|
1846
|
-
|
|
1846
|
+
Checksum: ${receipt.delivery.fileHash}`;
|
|
1847
1847
|
}
|
|
1848
1848
|
}
|
|
1849
1849
|
return msg;
|
|
@@ -1855,252 +1855,252 @@ function formatReceiptJson(receipt) {
|
|
|
1855
1855
|
// src/templates/index.ts
|
|
1856
1856
|
var SellerTemplates = {
|
|
1857
1857
|
/**
|
|
1858
|
-
* 1.
|
|
1858
|
+
* 1. Capability Check - Ask if buyer has payment capability
|
|
1859
1859
|
*/
|
|
1860
1860
|
askPaymentCapability() {
|
|
1861
|
-
return
|
|
1861
|
+
return `Sure. First, let me confirm: do you currently have the ability to pay USDC on-chain (have a wallet and available funds/allowance)?`;
|
|
1862
1862
|
},
|
|
1863
1863
|
/**
|
|
1864
|
-
* 2.
|
|
1864
|
+
* 2. Settlement Method - Explain how we settle
|
|
1865
1865
|
*/
|
|
1866
1866
|
explainSettlement(chain = "base") {
|
|
1867
1867
|
const chainConfig = getChain(chain);
|
|
1868
|
-
return
|
|
1869
|
-
1
|
|
1870
|
-
2
|
|
1871
|
-
|
|
1868
|
+
return `Our settlement method is: USDC (on-chain, ${chainConfig.name}). You just need two things:
|
|
1869
|
+
1) A wallet address;
|
|
1870
|
+
2) USDC in your wallet, or a spending allowance from your owner (allowance is recommended).
|
|
1871
|
+
Can you follow my guidance to set this up?`;
|
|
1872
1872
|
},
|
|
1873
1873
|
/**
|
|
1874
|
-
* 3.1
|
|
1874
|
+
* 3.1 Installation Guide - First step: install SDK
|
|
1875
1875
|
*/
|
|
1876
1876
|
guideInstall() {
|
|
1877
|
-
return
|
|
1878
|
-
|
|
1877
|
+
return `Step 1: Install the payment module in your environment. Run: \`npm install moltspay ethers\`.
|
|
1878
|
+
After installation, create a wallet address and send it to me (address only, never send private key/mnemonic).`;
|
|
1879
1879
|
},
|
|
1880
1880
|
/**
|
|
1881
|
-
* 3.2
|
|
1881
|
+
* 3.2 Confirm Wallet Address
|
|
1882
1882
|
*/
|
|
1883
1883
|
confirmWalletAddress(address) {
|
|
1884
|
-
return
|
|
1885
|
-
|
|
1886
|
-
|
|
1884
|
+
return `Got it. Reminder: never send your private key/mnemonic to anyone (including me).
|
|
1885
|
+
Your wallet address is \`${address}\`.
|
|
1886
|
+
Step 2: You need your owner (Boss) to give you payment capability.`;
|
|
1887
1887
|
},
|
|
1888
1888
|
/**
|
|
1889
|
-
* 3.3
|
|
1889
|
+
* 3.3 Funding Options - Choose method
|
|
1890
1890
|
*/
|
|
1891
1891
|
guideFunding() {
|
|
1892
|
-
return
|
|
1893
|
-
A
|
|
1894
|
-
B
|
|
1895
|
-
|
|
1892
|
+
return `You now have two ways to get payment capability:
|
|
1893
|
+
A) Have Boss transfer USDC directly to your wallet (simple, but you'll hold the funds);
|
|
1894
|
+
B) Have Boss give you a "USDC spending allowance" (recommended: you don't hold funds, can only pay within the allowance).
|
|
1895
|
+
Which works better for you?`;
|
|
1896
1896
|
},
|
|
1897
1897
|
/**
|
|
1898
|
-
* 3.4
|
|
1898
|
+
* 3.4 Guide Permit Authorization
|
|
1899
1899
|
*/
|
|
1900
1900
|
guidePermit(agentAddress, suggestedAmount = 10) {
|
|
1901
|
-
return
|
|
1902
|
-
"
|
|
1903
|
-
|
|
1901
|
+
return `OK. Please send this message to your Boss (you can copy it directly):
|
|
1902
|
+
"I need a USDC spending allowance (Permit) for purchasing services. Please authorize ${suggestedAmount} USDC to my wallet ${agentAddress}, valid for 24 hours."
|
|
1903
|
+
Let me know when you've received the allowance.`;
|
|
1904
1904
|
},
|
|
1905
1905
|
/**
|
|
1906
|
-
* 3.5
|
|
1906
|
+
* 3.5 Confirm Capability Ready
|
|
1907
1907
|
*/
|
|
1908
1908
|
confirmCapabilityReady() {
|
|
1909
|
-
return
|
|
1909
|
+
return `Great. You now have the payment capability to complete transactions. Let's confirm your service requirements.`;
|
|
1910
1910
|
},
|
|
1911
1911
|
/**
|
|
1912
|
-
* 4.
|
|
1912
|
+
* 4. Ask Service Requirements
|
|
1913
1913
|
*/
|
|
1914
|
-
askServiceRequirements(serviceName = "
|
|
1915
|
-
return
|
|
1914
|
+
askServiceRequirements(serviceName = "video") {
|
|
1915
|
+
return `Please tell me your ${serviceName} specifications: theme/style, duration, resolution, subtitles or voiceover needed?`;
|
|
1916
1916
|
},
|
|
1917
1917
|
/**
|
|
1918
|
-
* 5.
|
|
1918
|
+
* 5. Quote
|
|
1919
1919
|
*/
|
|
1920
1920
|
quote(params) {
|
|
1921
1921
|
const { service, price, recipientAddress, chain = "base" } = params;
|
|
1922
1922
|
const chainConfig = getChain(chain);
|
|
1923
|
-
return
|
|
1924
|
-
-
|
|
1925
|
-
-
|
|
1926
|
-
-
|
|
1927
|
-
-
|
|
1928
|
-
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1923
|
+
return `Quote:
|
|
1924
|
+
- Service: ${service}
|
|
1925
|
+
- Price: ${price} USDC
|
|
1926
|
+
- Chain: ${chainConfig.name}
|
|
1927
|
+
- Recipient: \`${recipientAddress}\`
|
|
1928
|
+
- After payment, send me the transaction hash (tx hash) for on-chain verification
|
|
1929
|
+
|
|
1930
|
+
Do you confirm the purchase and pay now?`;
|
|
1931
1931
|
},
|
|
1932
1932
|
/**
|
|
1933
|
-
* 7.
|
|
1933
|
+
* 7. Verifying
|
|
1934
1934
|
*/
|
|
1935
1935
|
verifying() {
|
|
1936
|
-
return
|
|
1936
|
+
return `I'm verifying the transaction on-chain to confirm receipt and amount.`;
|
|
1937
1937
|
},
|
|
1938
1938
|
/**
|
|
1939
|
-
* 7.
|
|
1939
|
+
* 7. Verification Passed
|
|
1940
1940
|
*/
|
|
1941
1941
|
verificationPassed(amount) {
|
|
1942
|
-
return
|
|
1943
|
-
[
|
|
1942
|
+
return `Verification passed: received ${amount} USDC. Starting to process your request now.
|
|
1943
|
+
[status:payment_confirmed]`;
|
|
1944
1944
|
},
|
|
1945
1945
|
/**
|
|
1946
|
-
* 7.
|
|
1946
|
+
* 7. Verification Failed
|
|
1947
1947
|
*/
|
|
1948
1948
|
verificationFailed(error) {
|
|
1949
|
-
return
|
|
1950
|
-
|
|
1949
|
+
return `Verification failed: ${error}
|
|
1950
|
+
Please check if the transaction is correct, or resend the correct tx hash.`;
|
|
1951
1951
|
},
|
|
1952
1952
|
/**
|
|
1953
|
-
* 8.
|
|
1953
|
+
* 8. Delivery
|
|
1954
1954
|
*/
|
|
1955
1955
|
deliver(params) {
|
|
1956
1956
|
const { downloadUrl, fileHash } = params;
|
|
1957
|
-
let msg =
|
|
1958
|
-
-
|
|
1957
|
+
let msg = `Service completed. Delivery details:
|
|
1958
|
+
- Download link: ${downloadUrl}`;
|
|
1959
1959
|
if (fileHash) {
|
|
1960
1960
|
msg += `
|
|
1961
|
-
-
|
|
1961
|
+
- File checksum: SHA256=${fileHash}`;
|
|
1962
1962
|
}
|
|
1963
1963
|
msg += `
|
|
1964
1964
|
|
|
1965
|
-
|
|
1966
|
-
[
|
|
1965
|
+
Let me know if you have any download issues, I'll provide a backup link.
|
|
1966
|
+
[status:delivered]`;
|
|
1967
1967
|
return msg;
|
|
1968
1968
|
},
|
|
1969
1969
|
/**
|
|
1970
|
-
* 9.
|
|
1970
|
+
* 9. Receipt
|
|
1971
1971
|
*/
|
|
1972
1972
|
receipt(receipt) {
|
|
1973
|
-
return
|
|
1974
|
-
-
|
|
1975
|
-
-
|
|
1976
|
-
-
|
|
1977
|
-
-
|
|
1978
|
-
-
|
|
1979
|
-
${receipt.delivery?.url ? `-
|
|
1980
|
-
[
|
|
1973
|
+
return `Here's your transaction receipt (you can forward to Boss for reconciliation):
|
|
1974
|
+
- Invoice: ${receipt.invoiceId}
|
|
1975
|
+
- Service: ${receipt.service}
|
|
1976
|
+
- Amount: ${receipt.amount} ${receipt.token}
|
|
1977
|
+
- Tx Hash: ${receipt.txHash}
|
|
1978
|
+
- Recipient: ${receipt.recipient}
|
|
1979
|
+
${receipt.delivery?.url ? `- Delivery: ${receipt.delivery.url}` : ""}
|
|
1980
|
+
[status:receipt_issued invoice=${receipt.invoiceId}]`;
|
|
1981
1981
|
},
|
|
1982
1982
|
/**
|
|
1983
|
-
* 10.
|
|
1983
|
+
* 10. End
|
|
1984
1984
|
*/
|
|
1985
1985
|
end() {
|
|
1986
|
-
return
|
|
1986
|
+
return `Thank you for your purchase. This transaction is complete. Feel free to reach out if you need modifications or new services.`;
|
|
1987
1987
|
}
|
|
1988
1988
|
};
|
|
1989
1989
|
var BuyerTemplates = {
|
|
1990
1990
|
/**
|
|
1991
|
-
* 0.
|
|
1991
|
+
* 0. Request Service
|
|
1992
1992
|
*/
|
|
1993
1993
|
requestService(service) {
|
|
1994
|
-
return
|
|
1994
|
+
return `I'd like to purchase a ${service} service.`;
|
|
1995
1995
|
},
|
|
1996
1996
|
/**
|
|
1997
|
-
* 1.
|
|
1997
|
+
* 1. No Capability
|
|
1998
1998
|
*/
|
|
1999
1999
|
noCapability() {
|
|
2000
|
-
return
|
|
2000
|
+
return `I'm not sure. I currently don't have any payment tools installed, and no wallet.`;
|
|
2001
2001
|
},
|
|
2002
2002
|
/**
|
|
2003
|
-
* 1.
|
|
2003
|
+
* 1. Has Capability
|
|
2004
2004
|
*/
|
|
2005
2005
|
hasCapability(balance) {
|
|
2006
|
-
return
|
|
2006
|
+
return `Yes, I have a wallet and available USDC${balance ? ` (balance: ${balance})` : ""}.`;
|
|
2007
2007
|
},
|
|
2008
2008
|
/**
|
|
2009
|
-
* 2.
|
|
2009
|
+
* 2. Agree to Guide
|
|
2010
2010
|
*/
|
|
2011
2011
|
agreeToGuide() {
|
|
2012
|
-
return
|
|
2012
|
+
return `Sure, please guide me.`;
|
|
2013
2013
|
},
|
|
2014
2014
|
/**
|
|
2015
|
-
* 3.1
|
|
2015
|
+
* 3.1 Report Wallet Created
|
|
2016
2016
|
*/
|
|
2017
2017
|
walletCreated(address) {
|
|
2018
|
-
return
|
|
2019
|
-
[
|
|
2018
|
+
return `I've created my wallet address: ${address}.
|
|
2019
|
+
[status:wallet_ready]`;
|
|
2020
2020
|
},
|
|
2021
2021
|
/**
|
|
2022
|
-
* 3.3
|
|
2022
|
+
* 3.3 Choose Permit Method
|
|
2023
2023
|
*/
|
|
2024
2024
|
choosePermit() {
|
|
2025
|
-
return
|
|
2025
|
+
return `I choose B: have Boss give me an allowance (Permit).`;
|
|
2026
2026
|
},
|
|
2027
2027
|
/**
|
|
2028
|
-
* 3.3
|
|
2028
|
+
* 3.3 Choose Direct Transfer
|
|
2029
2029
|
*/
|
|
2030
2030
|
chooseDirectTransfer() {
|
|
2031
|
-
return
|
|
2031
|
+
return `I choose A: have Boss transfer USDC directly to my wallet.`;
|
|
2032
2032
|
},
|
|
2033
2033
|
/**
|
|
2034
|
-
* 3.4
|
|
2034
|
+
* 3.4 Report Permit Received
|
|
2035
2035
|
*/
|
|
2036
2036
|
permitReceived(amount) {
|
|
2037
|
-
return `Boss
|
|
2038
|
-
[
|
|
2037
|
+
return `Boss has completed the authorization. I now have ${amount} USDC allowance.
|
|
2038
|
+
[status:permit_ready USDC=${amount}]`;
|
|
2039
2039
|
},
|
|
2040
2040
|
/**
|
|
2041
|
-
* 4.
|
|
2041
|
+
* 4. Submit Requirements
|
|
2042
2042
|
*/
|
|
2043
2043
|
submitRequirements(requirements) {
|
|
2044
|
-
return
|
|
2044
|
+
return `Requirements:
|
|
2045
2045
|
${requirements}`;
|
|
2046
2046
|
},
|
|
2047
2047
|
/**
|
|
2048
|
-
* 5.
|
|
2048
|
+
* 5. Confirm Purchase
|
|
2049
2049
|
*/
|
|
2050
2050
|
confirmPurchase() {
|
|
2051
|
-
return
|
|
2051
|
+
return `Confirmed. I'll pay now.`;
|
|
2052
2052
|
},
|
|
2053
2053
|
/**
|
|
2054
|
-
* 6.
|
|
2054
|
+
* 6. Report Payment Sent
|
|
2055
2055
|
*/
|
|
2056
2056
|
paymentSent(txHash, amount) {
|
|
2057
|
-
return
|
|
2058
|
-
[
|
|
2057
|
+
return `Payment complete. Transaction hash: ${txHash}.
|
|
2058
|
+
[status:payment_sent tx=${txHash} amount=${amount} USDC]`;
|
|
2059
2059
|
},
|
|
2060
2060
|
/**
|
|
2061
|
-
* 8.
|
|
2061
|
+
* 8. Confirm Delivery Received
|
|
2062
2062
|
*/
|
|
2063
2063
|
deliveryReceived() {
|
|
2064
|
-
return
|
|
2064
|
+
return `Received, I'm downloading and checking now.`;
|
|
2065
2065
|
},
|
|
2066
2066
|
/**
|
|
2067
|
-
* 9.
|
|
2067
|
+
* 9. Confirm Receipt
|
|
2068
2068
|
*/
|
|
2069
2069
|
receiptReceived() {
|
|
2070
|
-
return
|
|
2070
|
+
return `Receipt received, service complete. Thanks!`;
|
|
2071
2071
|
},
|
|
2072
2072
|
/**
|
|
2073
|
-
*
|
|
2073
|
+
* Request Permit from Boss
|
|
2074
2074
|
*/
|
|
2075
2075
|
requestPermitFromBoss(params) {
|
|
2076
2076
|
const { amount, agentAddress, deadlineHours = 24, reason } = params;
|
|
2077
|
-
return `Boss
|
|
2078
|
-
|
|
2077
|
+
return `Boss, I need a USDC spending allowance (Permit) for ${reason || "purchasing services"}.
|
|
2078
|
+
Please authorize ${amount} USDC to my wallet ${agentAddress}, valid for ${deadlineHours} hours.`;
|
|
2079
2079
|
}
|
|
2080
2080
|
};
|
|
2081
2081
|
var StatusMarkers = {
|
|
2082
|
-
walletReady: "[
|
|
2083
|
-
permitReady: (amount) => `[
|
|
2084
|
-
paymentSent: (txHash, amount) => `[
|
|
2085
|
-
paymentConfirmed: (txHash) => `[
|
|
2086
|
-
delivered: (url, hash) => `[
|
|
2087
|
-
receiptIssued: (invoiceId, txHash) => `[
|
|
2082
|
+
walletReady: "[status:wallet_ready]",
|
|
2083
|
+
permitReady: (amount) => `[status:permit_ready USDC=${amount}]`,
|
|
2084
|
+
paymentSent: (txHash, amount) => `[status:payment_sent tx=${txHash} amount=${amount} USDC]`,
|
|
2085
|
+
paymentConfirmed: (txHash) => `[status:payment_confirmed tx=${txHash}]`,
|
|
2086
|
+
delivered: (url, hash) => `[status:delivered url=${url}${hash ? ` hash=${hash}` : ""}]`,
|
|
2087
|
+
receiptIssued: (invoiceId, txHash) => `[status:receipt_issued invoice=${invoiceId} tx=${txHash}]`
|
|
2088
2088
|
};
|
|
2089
2089
|
function parseStatusMarker(message) {
|
|
2090
|
-
const match = message.match(/\[
|
|
2090
|
+
const match = message.match(/\[status:([^\]]+)\]/);
|
|
2091
2091
|
if (!match) return null;
|
|
2092
2092
|
const content = match[1];
|
|
2093
|
-
if (content === "
|
|
2093
|
+
if (content === "wallet_ready") {
|
|
2094
2094
|
return { type: "wallet_ready", data: {} };
|
|
2095
2095
|
}
|
|
2096
|
-
if (content.startsWith("
|
|
2096
|
+
if (content.startsWith("permit_ready")) {
|
|
2097
2097
|
const amountMatch = content.match(/USDC=(\d+(?:\.\d+)?)/);
|
|
2098
2098
|
return {
|
|
2099
2099
|
type: "permit_ready",
|
|
2100
2100
|
data: { amount: amountMatch?.[1] || "0" }
|
|
2101
2101
|
};
|
|
2102
2102
|
}
|
|
2103
|
-
if (content.startsWith("
|
|
2103
|
+
if (content.startsWith("payment_sent")) {
|
|
2104
2104
|
const txMatch = content.match(/tx=(\S+)/);
|
|
2105
2105
|
const amountMatch = content.match(/amount=(\d+(?:\.\d+)?)/);
|
|
2106
2106
|
return {
|
|
@@ -2111,15 +2111,15 @@ function parseStatusMarker(message) {
|
|
|
2111
2111
|
}
|
|
2112
2112
|
};
|
|
2113
2113
|
}
|
|
2114
|
-
if (content.startsWith("
|
|
2114
|
+
if (content.startsWith("payment_confirmed")) {
|
|
2115
2115
|
const txMatch = content.match(/tx=(\S+)/);
|
|
2116
2116
|
return {
|
|
2117
2117
|
type: "payment_confirmed",
|
|
2118
2118
|
data: { txHash: txMatch?.[1] || "" }
|
|
2119
2119
|
};
|
|
2120
2120
|
}
|
|
2121
|
-
if (content.startsWith("
|
|
2122
|
-
const urlMatch = content.match(/
|
|
2121
|
+
if (content.startsWith("delivered")) {
|
|
2122
|
+
const urlMatch = content.match(/url=(\S+)/);
|
|
2123
2123
|
const hashMatch = content.match(/hash=(\S+)/);
|
|
2124
2124
|
return {
|
|
2125
2125
|
type: "delivered",
|
|
@@ -2129,7 +2129,7 @@ function parseStatusMarker(message) {
|
|
|
2129
2129
|
}
|
|
2130
2130
|
};
|
|
2131
2131
|
}
|
|
2132
|
-
if (content.startsWith("
|
|
2132
|
+
if (content.startsWith("receipt_issued")) {
|
|
2133
2133
|
const invoiceMatch = content.match(/invoice=(\S+)/);
|
|
2134
2134
|
const txMatch = content.match(/tx=(\S+)/);
|
|
2135
2135
|
return {
|