four-flap-meme-sdk 1.3.1 → 1.3.3
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/contracts/tm-bundle-merkle/index.d.ts +2 -1
- package/dist/contracts/tm-bundle-merkle/index.js +2 -1
- package/dist/contracts/tm-bundle-merkle/submit.d.ts +10 -0
- package/dist/contracts/tm-bundle-merkle/submit.js +104 -0
- package/dist/flap/portal-bundle-merkle/core.js +34 -22
- package/dist/flap/portal-bundle-merkle/encryption.d.ts +16 -0
- package/dist/flap/portal-bundle-merkle/encryption.js +146 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
|
@@ -9,5 +9,6 @@ export { fourPrivateBuyMerkle, fourPrivateSellMerkle, fourBatchPrivateBuyMerkle,
|
|
|
9
9
|
export { disperseWithBundleMerkle, sweepWithBundleMerkle } from './utils.js';
|
|
10
10
|
export { fourPancakeProxyBatchBuyMerkle, fourPancakeProxyBatchSellMerkle, approveFourPancakeProxy, approveFourPancakeProxyBatch } from './pancake-proxy.js';
|
|
11
11
|
export { approveFourTokenManagerBatch, type ApproveFourTokenManagerBatchParams, type ApproveFourTokenManagerBatchResult } from './approve-tokenmanager.js';
|
|
12
|
-
export { submitBundleToMerkle, submitMultipleBundles, submitMultipleBundlesParallel, type MerkleSubmitConfig, type SubmitBundleResult, submitDirectToRpc,
|
|
12
|
+
export { submitBundleToMerkle, submitMultipleBundles, submitMultipleBundlesParallel, type MerkleSubmitConfig, type SubmitBundleResult, submitDirectToRpc, submitDirectToRpcSequential, // ✅ 新增:顺序广播并等待确认(用于多跳)
|
|
13
|
+
submitDirectToRpcParallel, type DirectSubmitConfig, type DirectSubmitResult, type DirectTxResult } from './submit.js';
|
|
13
14
|
export { fourBundleBuyFirstMerkle, type FourBundleBuyFirstSignParams, type FourBuyFirstSignConfig, type FourBuyFirstResult } from './swap-buy-first.js';
|
|
@@ -18,6 +18,7 @@ export { approveFourTokenManagerBatch } from './approve-tokenmanager.js';
|
|
|
18
18
|
// Bundle提交方法(服务器端使用)
|
|
19
19
|
export { submitBundleToMerkle, submitMultipleBundles, submitMultipleBundlesParallel,
|
|
20
20
|
// ✅ Monad 等链的逐笔广播方法
|
|
21
|
-
submitDirectToRpc,
|
|
21
|
+
submitDirectToRpc, submitDirectToRpcSequential, // ✅ 新增:顺序广播并等待确认(用于多跳)
|
|
22
|
+
submitDirectToRpcParallel } from './submit.js';
|
|
22
23
|
// 先买后卖换手方法
|
|
23
24
|
export { fourBundleBuyFirstMerkle } from './swap-buy-first.js';
|
|
@@ -163,6 +163,16 @@ export interface DirectSubmitResult {
|
|
|
163
163
|
* ```
|
|
164
164
|
*/
|
|
165
165
|
export declare function submitDirectToRpc(signedTransactions: string[], config: DirectSubmitConfig): Promise<DirectSubmitResult>;
|
|
166
|
+
/**
|
|
167
|
+
* ✅ 顺序广播并等待确认(用于多跳交易等需要顺序执行的场景)
|
|
168
|
+
*
|
|
169
|
+
* 每笔交易广播后会等待上链确认,确保后续交易能正确执行
|
|
170
|
+
*
|
|
171
|
+
* @param signedTransactions 签名后的交易数组
|
|
172
|
+
* @param config 直接广播配置
|
|
173
|
+
* @returns 广播结果
|
|
174
|
+
*/
|
|
175
|
+
export declare function submitDirectToRpcSequential(signedTransactions: string[], config: DirectSubmitConfig): Promise<DirectSubmitResult>;
|
|
166
176
|
/**
|
|
167
177
|
* 并行逐笔广播到 RPC(速度更快,但可能有 nonce 冲突)
|
|
168
178
|
*
|
|
@@ -223,6 +223,110 @@ export async function submitDirectToRpc(signedTransactions, config) {
|
|
|
223
223
|
errorSummary: errors.length > 0 ? errors.join('; ') : undefined
|
|
224
224
|
};
|
|
225
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* ✅ 顺序广播并等待确认(用于多跳交易等需要顺序执行的场景)
|
|
228
|
+
*
|
|
229
|
+
* 每笔交易广播后会等待上链确认,确保后续交易能正确执行
|
|
230
|
+
*
|
|
231
|
+
* @param signedTransactions 签名后的交易数组
|
|
232
|
+
* @param config 直接广播配置
|
|
233
|
+
* @returns 广播结果
|
|
234
|
+
*/
|
|
235
|
+
export async function submitDirectToRpcSequential(signedTransactions, config) {
|
|
236
|
+
const totalTransactions = signedTransactions?.length ?? 0;
|
|
237
|
+
if (!signedTransactions || signedTransactions.length === 0) {
|
|
238
|
+
return {
|
|
239
|
+
code: false,
|
|
240
|
+
totalTransactions: 0,
|
|
241
|
+
successCount: 0,
|
|
242
|
+
failedCount: 0,
|
|
243
|
+
txHashes: [],
|
|
244
|
+
results: [],
|
|
245
|
+
errorSummary: 'signedTransactions cannot be empty'
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if (!config.rpcUrl) {
|
|
249
|
+
return {
|
|
250
|
+
code: false,
|
|
251
|
+
totalTransactions,
|
|
252
|
+
successCount: 0,
|
|
253
|
+
failedCount: totalTransactions,
|
|
254
|
+
txHashes: [],
|
|
255
|
+
results: [],
|
|
256
|
+
errorSummary: 'rpcUrl is required in config'
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
const chainId = config.chainId ?? 143;
|
|
260
|
+
const chainName = config.chainName ?? 'MONAD';
|
|
261
|
+
const confirmationTimeout = config.confirmationTimeout ?? 60000; // 默认60秒超时
|
|
262
|
+
const provider = new ethers.JsonRpcProvider(config.rpcUrl, {
|
|
263
|
+
chainId,
|
|
264
|
+
name: chainName
|
|
265
|
+
});
|
|
266
|
+
const results = [];
|
|
267
|
+
const txHashes = [];
|
|
268
|
+
const errors = [];
|
|
269
|
+
// 顺序广播并等待确认
|
|
270
|
+
for (let i = 0; i < signedTransactions.length; i++) {
|
|
271
|
+
const signedTx = signedTransactions[i];
|
|
272
|
+
try {
|
|
273
|
+
console.log(`📤 [${chainName}] 广播交易 ${i + 1}/${totalTransactions}...`);
|
|
274
|
+
// 广播交易
|
|
275
|
+
const txResponse = await provider.broadcastTransaction(signedTx);
|
|
276
|
+
console.log(`✅ [${chainName}] 交易 ${i + 1} 已广播: ${txResponse.hash}`);
|
|
277
|
+
// ✅ 等待交易确认
|
|
278
|
+
console.log(`⏳ [${chainName}] 等待交易 ${i + 1} 确认...`);
|
|
279
|
+
const receipt = await provider.waitForTransaction(txResponse.hash, 1, // 等待1个确认
|
|
280
|
+
confirmationTimeout);
|
|
281
|
+
if (receipt && receipt.status === 1) {
|
|
282
|
+
results.push({
|
|
283
|
+
index: i,
|
|
284
|
+
success: true,
|
|
285
|
+
txHash: txResponse.hash
|
|
286
|
+
});
|
|
287
|
+
txHashes.push(txResponse.hash);
|
|
288
|
+
console.log(`✅ [${chainName}] 交易 ${i + 1} 已确认: ${txResponse.hash}`);
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const errorMsg = `交易执行失败(status=${receipt?.status})`;
|
|
292
|
+
results.push({
|
|
293
|
+
index: i,
|
|
294
|
+
success: false,
|
|
295
|
+
txHash: txResponse.hash,
|
|
296
|
+
error: errorMsg
|
|
297
|
+
});
|
|
298
|
+
errors.push(`交易 ${i + 1}: ${errorMsg}`);
|
|
299
|
+
console.error(`❌ [${chainName}] 交易 ${i + 1} 失败: ${errorMsg}`);
|
|
300
|
+
// ✅ 多跳场景:如果某笔失败,后续交易可能也会失败,继续尝试但标记
|
|
301
|
+
console.warn(`⚠️ [${chainName}] 交易 ${i + 1} 失败,继续尝试后续交易...`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
const errorMessage = error?.message || String(error);
|
|
306
|
+
results.push({
|
|
307
|
+
index: i,
|
|
308
|
+
success: false,
|
|
309
|
+
error: errorMessage
|
|
310
|
+
});
|
|
311
|
+
errors.push(`交易 ${i + 1}: ${errorMessage}`);
|
|
312
|
+
console.error(`❌ [${chainName}] 交易 ${i + 1}/${totalTransactions} 失败:`, errorMessage);
|
|
313
|
+
// ✅ 多跳场景:如果某笔失败,后续交易可能也会失败,继续尝试
|
|
314
|
+
console.warn(`⚠️ [${chainName}] 继续尝试后续交易...`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const successCount = txHashes.length;
|
|
318
|
+
const failedCount = totalTransactions - successCount;
|
|
319
|
+
console.log(`📊 [${chainName}] 顺序广播完成: ${successCount}/${totalTransactions} 成功`);
|
|
320
|
+
return {
|
|
321
|
+
code: successCount > 0,
|
|
322
|
+
totalTransactions,
|
|
323
|
+
successCount,
|
|
324
|
+
failedCount,
|
|
325
|
+
txHashes,
|
|
326
|
+
results,
|
|
327
|
+
errorSummary: errors.length > 0 ? errors.join('; ') : undefined
|
|
328
|
+
};
|
|
329
|
+
}
|
|
226
330
|
/**
|
|
227
331
|
* 并行逐笔广播到 RPC(速度更快,但可能有 nonce 冲突)
|
|
228
332
|
*
|
|
@@ -34,7 +34,6 @@ export async function createTokenWithBundleBuyMerkle(params) {
|
|
|
34
34
|
throw new Error(getErrorMessage('AMOUNT_MISMATCH', buyAmounts.length, privateKeys.length - 1));
|
|
35
35
|
}
|
|
36
36
|
const { provider, chainId } = createChainContext(chain, config.rpcUrl);
|
|
37
|
-
const gasPrice = await resolveGasPrice(provider, config);
|
|
38
37
|
const nonceManager = new NonceManager(provider);
|
|
39
38
|
const signedTxs = [];
|
|
40
39
|
const devWallet = new Wallet(privateKeys[0], provider);
|
|
@@ -42,8 +41,9 @@ export async function createTokenWithBundleBuyMerkle(params) {
|
|
|
42
41
|
const originalPortalAddr = FLAP_ORIGINAL_PORTAL_ADDRESSES[chain];
|
|
43
42
|
const portal = new ethers.Contract(originalPortalAddr, PORTAL_ABI, devWallet);
|
|
44
43
|
const useV3 = !!params.extensionID;
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
// ✅ 优化:并行获取 gasPrice、devWallet nonce 和 createTx
|
|
45
|
+
const createTxPromise = useV3
|
|
46
|
+
? portal.newTokenV3.populateTransaction({
|
|
47
47
|
name: tokenInfo.name,
|
|
48
48
|
symbol: tokenInfo.symbol,
|
|
49
49
|
meta: tokenInfo.meta,
|
|
@@ -58,7 +58,7 @@ export async function createTokenWithBundleBuyMerkle(params) {
|
|
|
58
58
|
extensionID: params.extensionID,
|
|
59
59
|
extensionData: params.extensionData ?? '0x'
|
|
60
60
|
})
|
|
61
|
-
:
|
|
61
|
+
: portal.newTokenV2.populateTransaction({
|
|
62
62
|
name: tokenInfo.name,
|
|
63
63
|
symbol: tokenInfo.symbol,
|
|
64
64
|
meta: tokenInfo.meta,
|
|
@@ -71,10 +71,15 @@ export async function createTokenWithBundleBuyMerkle(params) {
|
|
|
71
71
|
beneficiary: devWallet.address,
|
|
72
72
|
permitData: '0x'
|
|
73
73
|
});
|
|
74
|
+
const [gasPrice, createTxUnsigned, devNonce] = await Promise.all([
|
|
75
|
+
resolveGasPrice(provider, config),
|
|
76
|
+
createTxPromise,
|
|
77
|
+
nonceManager.getNextNonce(devWallet)
|
|
78
|
+
]);
|
|
74
79
|
const createTxRequest = {
|
|
75
80
|
...createTxUnsigned,
|
|
76
81
|
from: devWallet.address,
|
|
77
|
-
nonce:
|
|
82
|
+
nonce: devNonce,
|
|
78
83
|
gasLimit: getGasLimit(config),
|
|
79
84
|
gasPrice,
|
|
80
85
|
chainId,
|
|
@@ -99,8 +104,11 @@ export async function createTokenWithBundleBuyMerkle(params) {
|
|
|
99
104
|
console.log('🔍 createToken SDK 精度转换: 18 -> ', params.quoteTokenDecimals, ', 原始:', fundsList, ', 转换后:', adjustedFundsList);
|
|
100
105
|
}
|
|
101
106
|
console.log('🔍 createToken SDK - quoteToken:', params.quoteToken, 'inputToken:', inputToken, 'useNativeToken:', useNativeToken);
|
|
102
|
-
|
|
103
|
-
const buyerNonces = await
|
|
107
|
+
// ✅ 优化:并行获取 unsignedBuys 和 buyerNonces
|
|
108
|
+
const [unsignedBuys, buyerNonces] = await Promise.all([
|
|
109
|
+
populateBuyTransactionsWithQuote(buyers, portalAddr, tokenAddress, adjustedFundsList, inputToken, useNativeToken),
|
|
110
|
+
allocateBuyerNonces(buyers, extractProfit, maxFundsIndex, totalProfit, nonceManager)
|
|
111
|
+
]);
|
|
104
112
|
const signedBuys = await signBuyTransactions({
|
|
105
113
|
unsignedBuys,
|
|
106
114
|
buyers,
|
|
@@ -170,12 +178,12 @@ export async function batchBuyWithBundleMerkle(params) {
|
|
|
170
178
|
console.log('🔍 SDK inputToken 计算结果:', inputToken);
|
|
171
179
|
console.log('🔍 SDK useNativeToken:', useNativeToken);
|
|
172
180
|
console.log('🔍 SDK adjustedFundsList:', adjustedFundsList);
|
|
173
|
-
// ✅ 优化:并行执行 gasPrice 和
|
|
174
|
-
const [gasPrice, unsignedBuys] = await Promise.all([
|
|
181
|
+
// ✅ 优化:并行执行 gasPrice、populateBuyTransactions 和 allocateBuyerNonces(三个最耗时的 RPC 操作)
|
|
182
|
+
const [gasPrice, unsignedBuys, buyerNonces] = await Promise.all([
|
|
175
183
|
resolveGasPrice(provider, config),
|
|
176
|
-
populateBuyTransactionsWithQuote(buyers, FLAP_PORTAL_ADDRESSES[chain], tokenAddress, adjustedFundsList, inputToken, useNativeToken)
|
|
184
|
+
populateBuyTransactionsWithQuote(buyers, FLAP_PORTAL_ADDRESSES[chain], tokenAddress, adjustedFundsList, inputToken, useNativeToken),
|
|
185
|
+
allocateBuyerNonces(buyers, extractProfit, maxFundsIndex, totalProfit, nonceManager)
|
|
177
186
|
]);
|
|
178
|
-
const buyerNonces = await allocateBuyerNonces(buyers, extractProfit, maxFundsIndex, totalProfit, nonceManager);
|
|
179
187
|
const signedBuys = await signBuyTransactions({
|
|
180
188
|
unsignedBuys,
|
|
181
189
|
buyers,
|
|
@@ -343,19 +351,23 @@ function buildGasLimitList(length, config) {
|
|
|
343
351
|
const gasLimit = getGasLimit(config);
|
|
344
352
|
return new Array(length).fill(gasLimit);
|
|
345
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* ✅ 优化:并行获取所有钱包的 nonce
|
|
356
|
+
* 之前是串行循环,每个钱包一次 RPC 调用,非常慢
|
|
357
|
+
* 现在并行获取,大幅提升性能
|
|
358
|
+
*/
|
|
346
359
|
async function allocateBuyerNonces(buyers, extractProfit, maxIndex, totalProfit, nonceManager) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
360
|
+
// ✅ 并行获取所有钱包的初始 nonce
|
|
361
|
+
const initialNonces = await Promise.all(buyers.map(buyer => nonceManager.getNextNonce(buyer)));
|
|
362
|
+
// 如果需要提取利润,maxIndex 钱包需要额外一个 nonce(用于利润转账)
|
|
363
|
+
// 但这里只返回买入交易的 nonce,利润交易的 nonce 在 appendProfitTransaction 中处理
|
|
364
|
+
// NonceManager 内部会追踪已分配的 nonce
|
|
365
|
+
// 如果 maxIndex 钱包需要 2 个 nonce,提前预留
|
|
366
|
+
if (extractProfit && totalProfit > 0n && maxIndex >= 0 && maxIndex < buyers.length) {
|
|
367
|
+
// 再获取一个 nonce 给利润交易(NonceManager 内部会自增)
|
|
368
|
+
await nonceManager.getNextNonce(buyers[maxIndex]);
|
|
357
369
|
}
|
|
358
|
-
return
|
|
370
|
+
return initialNonces;
|
|
359
371
|
}
|
|
360
372
|
async function signBuyTransactions({ unsignedBuys, buyers, nonces, gasLimits, gasPrice, chainId, config, fundsList, useNativeToken = true // ✅ 默认使用原生代币
|
|
361
373
|
}) {
|
|
@@ -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
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -42,4 +42,5 @@ export { fourBundleBuyFirstMerkle, type FourBuyFirstConfig, type FourBundleBuyFi
|
|
|
42
42
|
export { flapBundleBuyFirstMerkle, type FlapBuyFirstSignConfig, type FlapBuyFirstConfig, type FlapBundleBuyFirstSignParams, type FlapBundleBuyFirstParams, type FlapBuyFirstResult } from './flap/portal-bundle-merkle/swap-buy-first.js';
|
|
43
43
|
export { pancakeBundleBuyFirstMerkle, type PancakeBuyFirstSignConfig, type PancakeBuyFirstConfig, type PancakeBundleBuyFirstSignParams, type PancakeBundleBuyFirstParams, type PancakeBuyFirstResult } from './pancake/bundle-buy-first.js';
|
|
44
44
|
export { PROFIT_CONFIG } from './utils/constants.js';
|
|
45
|
-
export { submitDirectToRpc,
|
|
45
|
+
export { submitDirectToRpc, submitDirectToRpcSequential, // ✅ 新增:顺序广播并等待确认(用于多跳)
|
|
46
|
+
submitDirectToRpcParallel, type DirectSubmitConfig, type DirectSubmitResult, type DirectTxResult } from './contracts/tm-bundle-merkle/submit.js';
|
package/dist/index.js
CHANGED
|
@@ -63,4 +63,5 @@ export { pancakeBundleBuyFirstMerkle } from './pancake/bundle-buy-first.js';
|
|
|
63
63
|
// ✅ 硬编码利润配置(统一管理)
|
|
64
64
|
export { PROFIT_CONFIG } from './utils/constants.js';
|
|
65
65
|
// ✅ Monad 等不支持 Bundle 的链:逐笔 RPC 广播方法(服务器端使用)
|
|
66
|
-
export { submitDirectToRpc,
|
|
66
|
+
export { submitDirectToRpc, submitDirectToRpcSequential, // ✅ 新增:顺序广播并等待确认(用于多跳)
|
|
67
|
+
submitDirectToRpcParallel } from './contracts/tm-bundle-merkle/submit.js';
|