four-flap-meme-sdk 1.6.76 → 1.6.79
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/flap/portal-bundle-merkle/encryption.d.ts +16 -0
- package/dist/flap/portal-bundle-merkle/encryption.js +146 -0
- package/dist/utils/quote-helpers.d.ts +1 -1
- package/dist/utils/quote-helpers.js +1 -1
- package/dist/xlayer/hop-wallet-manager.js +9 -22
- package/dist/xlayer/wash-ops.js +6 -8
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ECDH + AES-GCM 加密工具(浏览器兼容)
|
|
3
|
+
* 用于将签名交易用服务器公钥加密
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 用服务器公钥加密签名交易(ECDH + AES-GCM)
|
|
7
|
+
*
|
|
8
|
+
* @param signedTransactions 签名后的交易数组
|
|
9
|
+
* @param publicKeyBase64 服务器提供的公钥(Base64 格式)
|
|
10
|
+
* @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
|
|
11
|
+
*/
|
|
12
|
+
export declare function encryptWithPublicKey(signedTransactions: string[], publicKeyBase64: string): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* 验证公钥格式(Base64)
|
|
15
|
+
*/
|
|
16
|
+
export declare function validatePublicKey(publicKeyBase64: string): boolean;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ECDH + AES-GCM 加密工具(浏览器兼容)
|
|
3
|
+
* 用于将签名交易用服务器公钥加密
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 获取全局 crypto 对象(最简单直接的方式)
|
|
7
|
+
*/
|
|
8
|
+
function getCryptoAPI() {
|
|
9
|
+
// 尝试所有可能的全局对象,优先浏览器环境
|
|
10
|
+
const cryptoObj = (typeof window !== 'undefined' && window.crypto) ||
|
|
11
|
+
(typeof self !== 'undefined' && self.crypto) ||
|
|
12
|
+
(typeof global !== 'undefined' && global.crypto) ||
|
|
13
|
+
(typeof globalThis !== 'undefined' && globalThis.crypto);
|
|
14
|
+
if (!cryptoObj) {
|
|
15
|
+
const env = typeof window !== 'undefined' ? 'Browser' : 'Node.js';
|
|
16
|
+
const protocol = typeof location !== 'undefined' ? location.protocol : 'unknown';
|
|
17
|
+
throw new Error(`❌ Crypto API 不可用。环境: ${env}, 协议: ${protocol}. ` +
|
|
18
|
+
'请确保在 HTTPS 或 localhost 下运行');
|
|
19
|
+
}
|
|
20
|
+
return cryptoObj;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 获取 SubtleCrypto(用于加密操作)
|
|
24
|
+
*/
|
|
25
|
+
function getSubtleCrypto() {
|
|
26
|
+
const crypto = getCryptoAPI();
|
|
27
|
+
if (!crypto.subtle) {
|
|
28
|
+
const protocol = typeof location !== 'undefined' ? location.protocol : 'unknown';
|
|
29
|
+
const hostname = typeof location !== 'undefined' ? location.hostname : 'unknown';
|
|
30
|
+
throw new Error(`❌ SubtleCrypto API 不可用。协议: ${protocol}, 主机: ${hostname}. ` +
|
|
31
|
+
'请确保:1) 使用 HTTPS (或 localhost);2) 浏览器支持 Web Crypto API;' +
|
|
32
|
+
'3) 不在无痕/隐私浏览模式下');
|
|
33
|
+
}
|
|
34
|
+
return crypto.subtle;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Base64 转 ArrayBuffer(优先使用浏览器 API)
|
|
38
|
+
*/
|
|
39
|
+
function base64ToArrayBuffer(base64) {
|
|
40
|
+
// 浏览器环境(优先)
|
|
41
|
+
if (typeof atob !== 'undefined') {
|
|
42
|
+
const binaryString = atob(base64);
|
|
43
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
44
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
45
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
46
|
+
}
|
|
47
|
+
return bytes.buffer;
|
|
48
|
+
}
|
|
49
|
+
// Node.js 环境(fallback)
|
|
50
|
+
if (typeof Buffer !== 'undefined') {
|
|
51
|
+
return Buffer.from(base64, 'base64').buffer;
|
|
52
|
+
}
|
|
53
|
+
throw new Error('❌ Base64 解码不可用');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* ArrayBuffer 转 Base64(优先使用浏览器 API)
|
|
57
|
+
*/
|
|
58
|
+
function arrayBufferToBase64(buffer) {
|
|
59
|
+
// 浏览器环境(优先)
|
|
60
|
+
if (typeof btoa !== 'undefined') {
|
|
61
|
+
const bytes = new Uint8Array(buffer);
|
|
62
|
+
let binary = '';
|
|
63
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
64
|
+
binary += String.fromCharCode(bytes[i]);
|
|
65
|
+
}
|
|
66
|
+
return btoa(binary);
|
|
67
|
+
}
|
|
68
|
+
// Node.js 环境(fallback)
|
|
69
|
+
if (typeof Buffer !== 'undefined') {
|
|
70
|
+
return Buffer.from(buffer).toString('base64');
|
|
71
|
+
}
|
|
72
|
+
throw new Error('❌ Base64 编码不可用');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 生成随机 Hex 字符串
|
|
76
|
+
*/
|
|
77
|
+
function randomHex(length) {
|
|
78
|
+
const crypto = getCryptoAPI();
|
|
79
|
+
const array = new Uint8Array(length);
|
|
80
|
+
crypto.getRandomValues(array);
|
|
81
|
+
return Array.from(array)
|
|
82
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
83
|
+
.join('');
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 用服务器公钥加密签名交易(ECDH + AES-GCM)
|
|
87
|
+
*
|
|
88
|
+
* @param signedTransactions 签名后的交易数组
|
|
89
|
+
* @param publicKeyBase64 服务器提供的公钥(Base64 格式)
|
|
90
|
+
* @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
|
|
91
|
+
*/
|
|
92
|
+
export async function encryptWithPublicKey(signedTransactions, publicKeyBase64) {
|
|
93
|
+
try {
|
|
94
|
+
// 0. 获取 SubtleCrypto 和 Crypto API
|
|
95
|
+
const subtle = getSubtleCrypto();
|
|
96
|
+
const crypto = getCryptoAPI();
|
|
97
|
+
// 1. 准备数据
|
|
98
|
+
const payload = {
|
|
99
|
+
signedTransactions,
|
|
100
|
+
timestamp: Date.now(),
|
|
101
|
+
nonce: randomHex(8)
|
|
102
|
+
};
|
|
103
|
+
const plaintext = JSON.stringify(payload);
|
|
104
|
+
// 2. 生成临时 ECDH 密钥对
|
|
105
|
+
const ephemeralKeyPair = await subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveKey']);
|
|
106
|
+
// 3. 导入服务器公钥
|
|
107
|
+
const publicKeyBuffer = base64ToArrayBuffer(publicKeyBase64);
|
|
108
|
+
const publicKey = await subtle.importKey('raw', publicKeyBuffer, { name: 'ECDH', namedCurve: 'P-256' }, false, []);
|
|
109
|
+
// 4. 派生共享密钥(AES-256)
|
|
110
|
+
const sharedKey = await subtle.deriveKey({ name: 'ECDH', public: publicKey }, ephemeralKeyPair.privateKey, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
|
|
111
|
+
// 5. AES-GCM 加密
|
|
112
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
113
|
+
const encrypted = await subtle.encrypt({ name: 'AES-GCM', iv }, sharedKey, new TextEncoder().encode(plaintext));
|
|
114
|
+
// 6. 导出临时公钥
|
|
115
|
+
const ephemeralPublicKeyRaw = await subtle.exportKey('raw', ephemeralKeyPair.publicKey);
|
|
116
|
+
// 7. 返回加密包(JSON 格式)
|
|
117
|
+
return JSON.stringify({
|
|
118
|
+
e: arrayBufferToBase64(ephemeralPublicKeyRaw), // 临时公钥
|
|
119
|
+
i: arrayBufferToBase64(iv.buffer), // IV
|
|
120
|
+
d: arrayBufferToBase64(encrypted) // 密文
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
throw new Error(`加密失败: ${error?.message || String(error)}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 验证公钥格式(Base64)
|
|
129
|
+
*/
|
|
130
|
+
export function validatePublicKey(publicKeyBase64) {
|
|
131
|
+
try {
|
|
132
|
+
if (!publicKeyBase64)
|
|
133
|
+
return false;
|
|
134
|
+
// Base64 字符集验证
|
|
135
|
+
if (!/^[A-Za-z0-9+/=]+$/.test(publicKeyBase64))
|
|
136
|
+
return false;
|
|
137
|
+
// ECDH P-256 公钥固定长度 65 字节(未压缩)
|
|
138
|
+
// Base64 编码后约 88 字符
|
|
139
|
+
if (publicKeyBase64.length < 80 || publicKeyBase64.length > 100)
|
|
140
|
+
return false;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -25,7 +25,7 @@ export declare const QUOTE_CONFIG: {
|
|
|
25
25
|
};
|
|
26
26
|
readonly XLAYER: {
|
|
27
27
|
readonly v2Router: "0x881fb2f98c13d521009464e7d1cbf16e1b394e8e";
|
|
28
|
-
readonly v3Quoter: "";
|
|
28
|
+
readonly v3Quoter: "0x5A6f3723346aF54a4D0693bfC1718D64d4915C3e";
|
|
29
29
|
readonly wrappedNative: "0xe538905cf8410324e03a5a23c1c177a474d59b2b";
|
|
30
30
|
readonly stableCoins: readonly ["0x1E4a5963aBFD975d8c9021ce480b42188849D41d", "0x74b7f16337b8972027f6196a17a631ac6de26d22", "0x779ded0c9e1022225f8e0630b35a9b54be713736"];
|
|
31
31
|
};
|
|
@@ -35,7 +35,7 @@ export const QUOTE_CONFIG = {
|
|
|
35
35
|
},
|
|
36
36
|
XLAYER: {
|
|
37
37
|
v2Router: '0x881fb2f98c13d521009464e7d1cbf16e1b394e8e', // PotatoSwap V2
|
|
38
|
-
v3Quoter: '', //
|
|
38
|
+
v3Quoter: '0x5A6f3723346aF54a4D0693bfC1718D64d4915C3e', // ✅ PotatoSwap V3 QuoterV2
|
|
39
39
|
wrappedNative: '0xe538905cf8410324e03a5a23c1c177a474d59b2b', // WOKB
|
|
40
40
|
stableCoins: [
|
|
41
41
|
'0x1E4a5963aBFD975d8c9021ce480b42188849D41d', // USDT
|
|
@@ -345,9 +345,6 @@ export class HopWalletManager {
|
|
|
345
345
|
deployed: hopDeployedOnChain[i],
|
|
346
346
|
balance: ethers.formatEther(hopBalances[i]),
|
|
347
347
|
})));
|
|
348
|
-
// ✅ 记录 Payer nonce,避免冲突
|
|
349
|
-
let payerNonceCounter = await this.provider.getTransactionCount(payerWallet.address, 'pending');
|
|
350
|
-
const connectedPayer = payerWallet.connect(this.provider);
|
|
351
348
|
for (let i = 0; i < hopWallets.length; i++) {
|
|
352
349
|
const hop = hopWallets[i];
|
|
353
350
|
try {
|
|
@@ -366,24 +363,23 @@ export class HopWalletManager {
|
|
|
366
363
|
// 增加 20% buffer 确保足够
|
|
367
364
|
const estimatedPrefund = (totalGasNeeded * gasPrice * 120n) / 100n;
|
|
368
365
|
console.log(`[HopWalletManager] Hop ${i} prefund 估算: callGas=${callGasLimit}, verifyGas=${verificationGasLimit}, preVerify=${preVerificationGas}, total=${totalGasNeeded}, prefund=${ethers.formatEther(estimatedPrefund)} OKB, balance=${ethers.formatEther(balance)} OKB, deployed=${isDeployed}`);
|
|
369
|
-
// ✅
|
|
370
|
-
const minRefundAmount = gasPrice * 50000n; // ~0.00005 OKB
|
|
371
|
-
// ✅ 关键改进:如果余额不足 prefund + minRefund,先让 Payer 给 Hop 充值差额
|
|
366
|
+
// ✅ 关键改进:如果余额不足 prefund,先让 Payer 给 Hop 充值差额
|
|
372
367
|
let effectiveBalance = balance;
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const shortfall = minNeededForRefund - balance + (gasPrice * 20000n);
|
|
368
|
+
if (balance <= estimatedPrefund) {
|
|
369
|
+
// 计算需要充值的金额(差额 + 少量 buffer)
|
|
370
|
+
const shortfall = estimatedPrefund - balance + (gasPrice * 10000n); // 多充一点
|
|
377
371
|
console.log(`[HopWalletManager] Hop ${i} 余额不足,需要补充: ${ethers.formatEther(shortfall)} OKB`);
|
|
378
372
|
try {
|
|
373
|
+
// 使用 Payer 给 Hop 充值
|
|
374
|
+
const connectedPayer = payerWallet.connect(this.provider);
|
|
375
|
+
const payerNonce = await this.provider.getTransactionCount(payerWallet.address, 'pending');
|
|
379
376
|
const topUpTx = await connectedPayer.sendTransaction({
|
|
380
377
|
to: hop.senderAddress,
|
|
381
378
|
value: shortfall,
|
|
382
|
-
nonce:
|
|
379
|
+
nonce: payerNonce,
|
|
383
380
|
gasPrice,
|
|
384
381
|
gasLimit: 21055n,
|
|
385
382
|
});
|
|
386
|
-
payerNonceCounter++; // ✅ 递增 nonce
|
|
387
383
|
console.log(`[HopWalletManager] Hop ${i} 补充转账已发送: ${topUpTx.hash}`);
|
|
388
384
|
// 等待确认
|
|
389
385
|
const receipt = await topUpTx.wait();
|
|
@@ -405,16 +401,6 @@ export class HopWalletManager {
|
|
|
405
401
|
}
|
|
406
402
|
}
|
|
407
403
|
const refundAmount = effectiveBalance - estimatedPrefund;
|
|
408
|
-
// ✅ 安全检查:确保退款金额为正数
|
|
409
|
-
if (refundAmount <= 0n) {
|
|
410
|
-
console.warn(`[HopWalletManager] Hop ${i} 退款金额为 ${refundAmount},跳过`);
|
|
411
|
-
hopRefunds.push({
|
|
412
|
-
hopIndex: hop.index,
|
|
413
|
-
refundWei: 0n,
|
|
414
|
-
error: `退款金额不足: ${ethers.formatEther(refundAmount)} OKB`,
|
|
415
|
-
});
|
|
416
|
-
continue;
|
|
417
|
-
}
|
|
418
404
|
const initCode = isDeployed ? '0x' : hop.initCode;
|
|
419
405
|
// 构建 UserOp
|
|
420
406
|
const hopWallet = new Wallet(hop.privateKey, this.provider);
|
|
@@ -469,6 +455,7 @@ export class HopWalletManager {
|
|
|
469
455
|
});
|
|
470
456
|
// 发送 handleOps
|
|
471
457
|
try {
|
|
458
|
+
const connectedPayer = payerWallet.connect(this.provider);
|
|
472
459
|
const entryPointAddress = this.aaManager.getEntryPointAddress();
|
|
473
460
|
// ✅ 直接创建新 Contract 实例(跟其他地方一样)
|
|
474
461
|
const entryPointWithSigner = new ethers.Contract(entryPointAddress, ['function handleOps((address sender,uint256 nonce,bytes initCode,bytes callData,uint256 callGasLimit,uint256 verificationGasLimit,uint256 preVerificationGas,uint256 maxFeePerGas,uint256 maxPriorityFeePerGas,bytes paymasterAndData,bytes signature)[] ops, address payable beneficiary) external'], connectedPayer);
|
package/dist/xlayer/wash-ops.js
CHANGED
|
@@ -19,6 +19,7 @@ import { encodeSwapExactETHForTokensSupportingFee, encodeSwapExactTokensForETHSu
|
|
|
19
19
|
import { FLAP_PORTAL, WOKB, XLAYER_CHAIN_ID, DEFAULT_RPC_URL, ENTRYPOINT_V06, SIMPLE_ACCOUNT_FACTORY, PARTICLE_BUNDLER_URL, POTATOSWAP_V2_ROUTER, POTATOSWAP_V3_ROUTER, POTATOSWAP_V3_FACTORY, } from './constants.js';
|
|
20
20
|
import { PROFIT_CONFIG, ZERO_ADDRESS } from '../utils/constants.js';
|
|
21
21
|
import { mapWithConcurrency } from '../utils/concurrency.js';
|
|
22
|
+
import { quoteV3 } from '../utils/quote-helpers.js';
|
|
22
23
|
// ============================================================================
|
|
23
24
|
// V3 报价工具函数
|
|
24
25
|
// ============================================================================
|
|
@@ -266,18 +267,15 @@ export async function buildWashOps(params) {
|
|
|
266
267
|
});
|
|
267
268
|
}
|
|
268
269
|
else if (poolType === 'v3') {
|
|
269
|
-
// ✅ V3 报价:使用
|
|
270
|
+
// ✅ V3 报价:使用 V3 Quoter(和 BSC 一样,考虑价格影响,获取准确报价)
|
|
271
|
+
// PotatoSwap V3 QuoterV2: 0x5A6f3723346aF54a4D0693bfC1718D64d4915C3e
|
|
272
|
+
const provider = new ethers.JsonRpcProvider(config.rpcUrl, { chainId: config.chainId, name: 'xlayer' });
|
|
270
273
|
expectedTokenAmounts = await mapWithConcurrency(buyWeiList, 4, async (buyWei) => {
|
|
271
274
|
if (buyWei <= 0n)
|
|
272
275
|
return 0n;
|
|
273
276
|
try {
|
|
274
|
-
|
|
275
|
-
return
|
|
276
|
-
rpcUrl: config.rpcUrl,
|
|
277
|
-
tokenAddress: params.tokenAddress,
|
|
278
|
-
wokbAmount: buyWei,
|
|
279
|
-
fee: v3Fee,
|
|
280
|
-
});
|
|
277
|
+
const result = await quoteV3(provider, wokb, params.tokenAddress, buyWei, 'XLAYER', v3Fee);
|
|
278
|
+
return result.amountOut;
|
|
281
279
|
}
|
|
282
280
|
catch {
|
|
283
281
|
return 0n;
|