four-flap-meme-sdk 1.5.80 → 1.5.82
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/xlayer/aa-account.js +43 -51
- package/dist/xlayer/bundle.js +164 -187
- package/dist/xlayer/constants.d.ts +1 -1
- package/dist/xlayer/constants.js +0 -2
- package/dist/xlayer/index.d.ts +1 -1
- package/dist/xlayer/index.js +1 -1
- package/dist/xlayer/portal-ops.d.ts +0 -13
- package/dist/xlayer/portal-ops.js +0 -24
- package/dist/xlayer/types.d.ts +2 -8
- package/package.json +1 -1
- package/dist/flap/portal-bundle-merkle/encryption.d.ts +0 -16
- package/dist/flap/portal-bundle-merkle/encryption.js +0 -146
|
@@ -637,31 +637,54 @@ export class AAAccountManager {
|
|
|
637
637
|
return [];
|
|
638
638
|
// 1) 批量预测 sender(优先 getAddress+multicall,失败回退 createAccount.staticCall)
|
|
639
639
|
const senders = await this.predictSendersByOwnersFast(owners);
|
|
640
|
-
// 2) 批量 getNonce
|
|
640
|
+
// 2) 批量 getNonce & getEthBalance (Multicall3)
|
|
641
641
|
const epIface = new Interface(ENTRYPOINT_ABI);
|
|
642
|
-
const
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
642
|
+
const multicallCalls = senders.flatMap((sender) => [
|
|
643
|
+
{
|
|
644
|
+
target: this.entryPointAddress,
|
|
645
|
+
allowFailure: true,
|
|
646
|
+
callData: epIface.encodeFunctionData('getNonce', [sender, 0]),
|
|
647
|
+
},
|
|
648
|
+
{
|
|
649
|
+
target: MULTICALL3,
|
|
650
|
+
allowFailure: true,
|
|
651
|
+
callData: '0x4d2301cc' + sender.slice(2).padStart(64, '0'), // getEthBalance(address)
|
|
652
|
+
},
|
|
653
|
+
]);
|
|
647
654
|
const nonces = new Array(senders.length).fill(0n);
|
|
648
|
-
const
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
const
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
const
|
|
659
|
-
|
|
655
|
+
const balances = new Array(senders.length).fill(0n);
|
|
656
|
+
// 每批处理 200 个地址(即 400 个 call),平衡负载
|
|
657
|
+
const ADDR_BATCH = 200;
|
|
658
|
+
for (let cursor = 0; cursor < senders.length; cursor += ADDR_BATCH) {
|
|
659
|
+
const sliceCalls = multicallCalls.slice(cursor * 2, (cursor + ADDR_BATCH) * 2);
|
|
660
|
+
try {
|
|
661
|
+
const res = await this.multicallAggregate3({ calls: sliceCalls });
|
|
662
|
+
for (let i = 0; i < ADDR_BATCH && cursor + i < senders.length; i++) {
|
|
663
|
+
const idx = cursor + i;
|
|
664
|
+
const nonceRes = res[i * 2];
|
|
665
|
+
const balanceRes = res[i * 2 + 1];
|
|
666
|
+
// 解析 Nonce
|
|
667
|
+
if (nonceRes?.success && nonceRes.returnData && nonceRes.returnData !== '0x') {
|
|
668
|
+
try {
|
|
669
|
+
const decoded = epIface.decodeFunctionResult('getNonce', nonceRes.returnData);
|
|
670
|
+
nonces[idx] = BigInt(decoded?.[0] ?? 0n);
|
|
671
|
+
}
|
|
672
|
+
catch { /* ignore */ }
|
|
673
|
+
}
|
|
674
|
+
// 解析 Balance
|
|
675
|
+
if (balanceRes?.success && balanceRes.returnData && balanceRes.returnData !== '0x') {
|
|
676
|
+
try {
|
|
677
|
+
balances[idx] = BigInt(balanceRes.returnData);
|
|
678
|
+
}
|
|
679
|
+
catch { /* ignore */ }
|
|
680
|
+
}
|
|
660
681
|
}
|
|
661
|
-
|
|
682
|
+
}
|
|
683
|
+
catch (err) {
|
|
684
|
+
console.warn(`[getMultipleAccountInfo] Multicall 失败,回退后续 logic 会处理缺失值:`, err);
|
|
662
685
|
}
|
|
663
686
|
}
|
|
664
|
-
// 3) deployed(getCode):小分片 + 并发上限 + 缓存 deployed=true
|
|
687
|
+
// 3) deployed(getCode):小分片 + 并发上限 + 缓存 deployed=true
|
|
665
688
|
const deployed = new Array(senders.length).fill(false);
|
|
666
689
|
const needCode = [];
|
|
667
690
|
for (let i = 0; i < senders.length; i++) {
|
|
@@ -713,37 +736,6 @@ export class AAAccountManager {
|
|
|
713
736
|
}
|
|
714
737
|
}
|
|
715
738
|
}
|
|
716
|
-
// 4) balance(getEthBalance):用 Multicall3.getEthBalance 分片批量查询 OKB(减少 N 次 eth_getBalance)
|
|
717
|
-
const balances = new Array(senders.length).fill(0n);
|
|
718
|
-
const ethBalIface = new Interface(['function getEthBalance(address addr) view returns (uint256)']);
|
|
719
|
-
const balCalls = senders.map((sender) => ({
|
|
720
|
-
target: MULTICALL3,
|
|
721
|
-
allowFailure: true,
|
|
722
|
-
callData: ethBalIface.encodeFunctionData('getEthBalance', [sender]),
|
|
723
|
-
}));
|
|
724
|
-
const BAL_BATCH = 300;
|
|
725
|
-
try {
|
|
726
|
-
for (let cursor = 0; cursor < balCalls.length; cursor += BAL_BATCH) {
|
|
727
|
-
const sliceCalls = balCalls.slice(cursor, cursor + BAL_BATCH);
|
|
728
|
-
const res = await this.multicallAggregate3({ calls: sliceCalls });
|
|
729
|
-
for (let i = 0; i < res.length; i++) {
|
|
730
|
-
const r = res[i];
|
|
731
|
-
const idx = cursor + i;
|
|
732
|
-
if (!r?.success || !r.returnData || r.returnData === '0x')
|
|
733
|
-
continue;
|
|
734
|
-
try {
|
|
735
|
-
const decoded = ethBalIface.decodeFunctionResult('getEthBalance', r.returnData);
|
|
736
|
-
balances[idx] = BigInt(decoded?.[0] ?? 0n);
|
|
737
|
-
}
|
|
738
|
-
catch { /* ignore */ }
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
catch {
|
|
743
|
-
const fetched = await mapWithConcurrency(senders, 6, async (s) => await this.provider.getBalance(s));
|
|
744
|
-
for (let i = 0; i < fetched.length; i++)
|
|
745
|
-
balances[i] = fetched[i] ?? 0n;
|
|
746
|
-
}
|
|
747
739
|
return owners.map((owner, i) => ({
|
|
748
740
|
owner,
|
|
749
741
|
sender: senders[i],
|
package/dist/xlayer/bundle.js
CHANGED
|
@@ -10,11 +10,10 @@
|
|
|
10
10
|
import { Wallet, Interface, Contract, ethers } from 'ethers';
|
|
11
11
|
import { FLAP_PORTAL, ENTRYPOINT_ABI, PORTAL_ABI, DEFAULT_CALL_GAS_LIMIT_SELL, DEFAULT_WITHDRAW_RESERVE, WOKB, POTATOSWAP_V2_ROUTER, POTATOSWAP_V3_ROUTER, } from './constants.js';
|
|
12
12
|
import { AAAccountManager, encodeExecute, encodeExecuteBatch, createWallet } from './aa-account.js';
|
|
13
|
-
import { encodeBuyCall,
|
|
13
|
+
import { encodeBuyCall, encodeSellCall, encodeApproveCall, encodeTransferCall, encodeCreateCallV2, encodeCreateCallV3, encodeCreateCallV4, PortalQuery, parseOkb, formatOkb, lpFeeProfileToV3Fee, } from './portal-ops.js';
|
|
14
14
|
import { mapWithConcurrency } from '../utils/concurrency.js';
|
|
15
15
|
import { PROFIT_CONFIG, ZERO_ADDRESS } from '../utils/constants.js';
|
|
16
|
-
import { DexQuery } from './dex.js';
|
|
17
|
-
import { encodeSwapExactETHForTokensSupportingFee, encodeSwapExactETHForTokensV3, } from './dex.js';
|
|
16
|
+
import { DexQuery, encodeSwapExactETHForTokensV3, encodeSwapExactETHForTokensSupportingFee } from './dex.js';
|
|
18
17
|
// ============================================================================
|
|
19
18
|
// AA Nonce(EntryPoint nonce)本地分配器
|
|
20
19
|
// ============================================================================
|
|
@@ -1470,11 +1469,6 @@ export class BundleExecutor {
|
|
|
1470
1469
|
// ✅ 并行构建和签名所有外盘买入 UserOps
|
|
1471
1470
|
const signedDexBuyOps = await mapWithConcurrency(dexBuyData, 5, async (data) => {
|
|
1472
1471
|
const { wallet, info, buyWei } = data;
|
|
1473
|
-
// ✅ 外盘买入:使用 Portal 的 swapExactInput 进行自动路由
|
|
1474
|
-
// 优势:Portal 会自动识别代币的池子类型(V2/V3),无需手动指定
|
|
1475
|
-
// 参考 BSC 版本的逻辑(create-to-dex.ts:604-616)
|
|
1476
|
-
// const buyData = encodeBuyCall(tokenAddress, buyWei, 0n);
|
|
1477
|
-
// const dexBuyCallData = encodeExecute(FLAP_PORTAL, buyWei, buyData);
|
|
1478
1472
|
let swapData;
|
|
1479
1473
|
let routerAddress;
|
|
1480
1474
|
if (dexType === 'V3') {
|
|
@@ -1557,34 +1551,28 @@ export class BundleExecutor {
|
|
|
1557
1551
|
* 自动计算剩余毕业容量,将钱包分为内盘和外盘组,生成签名的交易列表
|
|
1558
1552
|
*/
|
|
1559
1553
|
async bundleGraduateBuy(params) {
|
|
1560
|
-
const { tokenAddress, privateKeys, payerPrivateKey, amountMode, totalBuyAmount, minAmount, maxAmount, walletAmounts, enableDexBuy = false,
|
|
1561
|
-
// ✅ 新增:前端传递的分组信息
|
|
1562
|
-
curveAddresses, dexAddresses, graduationAmount, config = {} } = params;
|
|
1554
|
+
const { tokenAddress, privateKeys, payerPrivateKey, amountMode, totalBuyAmount, minAmount, maxAmount, walletAmounts, enableDexBuy = false, config = {} } = params;
|
|
1563
1555
|
const aaManager = this.getAAManager();
|
|
1564
1556
|
const provider = aaManager.getProvider();
|
|
1565
1557
|
// 1. 获取代币状态,动态计算毕业容量
|
|
1566
1558
|
const tokenState = await this.portalQuery.getTokenV7(tokenAddress);
|
|
1567
|
-
// ✅ 内联曲线公式计算毕业容量
|
|
1568
|
-
//
|
|
1559
|
+
// ✅ 内联曲线公式计算毕业容量 (CDPV2)
|
|
1560
|
+
// 储备量 R = k / (10亿 + h - 供应量 S) - r
|
|
1569
1561
|
const r = tokenState.r; // 衰减因子
|
|
1570
1562
|
const h = tokenState.h; // 初始高度
|
|
1571
1563
|
const k = tokenState.k; // 缩放系数
|
|
1572
|
-
|
|
1573
|
-
const
|
|
1574
|
-
const
|
|
1575
|
-
const
|
|
1576
|
-
//
|
|
1577
|
-
const
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
const
|
|
1582
|
-
const
|
|
1583
|
-
|
|
1584
|
-
const remainingToGraduateWei = ethers.parseEther(remainingToGraduate.toFixed(18));
|
|
1585
|
-
//
|
|
1586
|
-
// console.log(`[GraduateBuy] Pool Reserve: ${poolReserveOkb} OKB, GraduateCapacity: ${graduateCapacity} OKB, Remaining: ${remainingToGraduate} OKB`);
|
|
1587
|
-
console.log(`[GraduateBuy] Pool Reserve: ${poolReserveOkb} OKB, Graduation: ${graduationReserve} OKB, Remaining: ${remainingToGraduate.toFixed(4)} OKB`);
|
|
1564
|
+
const dexSupplyThresh = tokenState.dexSupplyThresh;
|
|
1565
|
+
const BILLION_WEI = 1000000000n * 10n ** 18n;
|
|
1566
|
+
const denom = BILLION_WEI + h - dexSupplyThresh;
|
|
1567
|
+
const graduationReserveWei = denom > 0n ? (k * 10n ** 18n) / denom - r : 0n;
|
|
1568
|
+
// 剩余毕业金额 = 毕业阈值 - 当前储备(加 1.6% 手续费缓冲以覆盖 1.5% 平台费)
|
|
1569
|
+
const remainingToGraduateWei = (tokenState.status === 4 || graduationReserveWei <= tokenState.reserve)
|
|
1570
|
+
? 0n
|
|
1571
|
+
: ((graduationReserveWei - tokenState.reserve) * 1016n / 1000n);
|
|
1572
|
+
const graduationReserveStr = ethers.formatEther(graduationReserveWei);
|
|
1573
|
+
const poolReserveOkbStr = ethers.formatEther(tokenState.reserve);
|
|
1574
|
+
const remainingStr = ethers.formatEther(remainingToGraduateWei);
|
|
1575
|
+
console.log(`[GraduateBuy] Status: ${tokenState.status}, Pool Reserve: ${poolReserveOkbStr} OKB, Graduation: ${graduationReserveStr} OKB, Remaining: ${remainingStr} OKB`);
|
|
1588
1576
|
// 2. 准备钱包
|
|
1589
1577
|
const sharedProvider = this.aaManager.getProvider();
|
|
1590
1578
|
const wallets = privateKeys.map(pk => new Wallet(pk, sharedProvider));
|
|
@@ -1592,21 +1580,7 @@ export class BundleExecutor {
|
|
|
1592
1580
|
const payerAccount = await aaManager.getAccountInfo(payerWallet.address);
|
|
1593
1581
|
const nonceMap = new AANonceMap();
|
|
1594
1582
|
nonceMap.init(payerAccount.sender, payerAccount.nonce);
|
|
1595
|
-
// ✅ 预获取所有钱包的 Sender 地址,用于双重地址匹配
|
|
1596
|
-
// 前端可能传递 Sender 地址(AA 模式下 wallet.address 被切换为 Sender)
|
|
1597
|
-
const walletInfos = await mapWithConcurrency(wallets, 5, async (w) => {
|
|
1598
|
-
const info = await aaManager.getAccountInfo(w.address);
|
|
1599
|
-
return { owner: w.address.toLowerCase(), sender: info.sender.toLowerCase() };
|
|
1600
|
-
});
|
|
1601
|
-
// 建立 Owner->Sender 映射,用于后续匹配
|
|
1602
|
-
const ownerToSender = new Map();
|
|
1603
|
-
const senderToOwner = new Map();
|
|
1604
|
-
for (const info of walletInfos) {
|
|
1605
|
-
ownerToSender.set(info.owner, info.sender);
|
|
1606
|
-
senderToOwner.set(info.sender, info.owner);
|
|
1607
|
-
}
|
|
1608
1583
|
// 3. 计算每个钱包的金额
|
|
1609
|
-
// ✅ 修复:同时支持 Owner 和 Sender 地址匹配
|
|
1610
1584
|
let finalAmounts = [];
|
|
1611
1585
|
if (amountMode === 'average') {
|
|
1612
1586
|
const avg = (totalBuyAmount || 0) / wallets.length;
|
|
@@ -1621,172 +1595,175 @@ export class BundleExecutor {
|
|
|
1621
1595
|
});
|
|
1622
1596
|
}
|
|
1623
1597
|
else if (amountMode === 'custom') {
|
|
1624
|
-
finalAmounts = wallets.map(w =>
|
|
1625
|
-
const ownerAddr = w.address.toLowerCase();
|
|
1626
|
-
const senderAddr = ownerToSender.get(ownerAddr) || '';
|
|
1627
|
-
// ✅ 双重匹配:先按 Owner 地址查找,再按 Sender 地址查找
|
|
1628
|
-
return walletAmounts?.[w.address] ||
|
|
1629
|
-
walletAmounts?.[w.address.toLowerCase()] ||
|
|
1630
|
-
walletAmounts?.[senderAddr] ||
|
|
1631
|
-
(senderAddr ? (walletAmounts?.[senderAddr.toLowerCase()] || 0) : 0);
|
|
1632
|
-
});
|
|
1598
|
+
finalAmounts = wallets.map(w => walletAmounts?.[w.address] || 0);
|
|
1633
1599
|
}
|
|
1634
|
-
// 4.
|
|
1635
|
-
const
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1600
|
+
// 4. 钱包任务分配(核心逻辑:支持单个钱包原子化跨内外盘)
|
|
1601
|
+
const buyerTasks = [];
|
|
1602
|
+
let currentCurveTotal = 0n;
|
|
1603
|
+
for (let i = 0; i < wallets.length; i++) {
|
|
1604
|
+
const wallet = wallets[i];
|
|
1605
|
+
const amountWei = ethers.parseEther(finalAmounts[i].toFixed(18));
|
|
1606
|
+
const tasks = [];
|
|
1607
|
+
let isTrigger = false;
|
|
1608
|
+
if (currentCurveTotal < remainingToGraduateWei) {
|
|
1609
|
+
const needed = remainingToGraduateWei - currentCurveTotal;
|
|
1610
|
+
if (amountWei <= needed) {
|
|
1611
|
+
// 情况 A:全在内盘
|
|
1612
|
+
tasks.push({
|
|
1613
|
+
target: FLAP_PORTAL,
|
|
1614
|
+
amount: amountWei,
|
|
1615
|
+
data: encodeBuyCall(tokenAddress, amountWei, 0n)
|
|
1616
|
+
});
|
|
1617
|
+
currentCurveTotal += amountWei;
|
|
1618
|
+
if (currentCurveTotal >= remainingToGraduateWei)
|
|
1619
|
+
isTrigger = true;
|
|
1654
1620
|
}
|
|
1655
|
-
else
|
|
1656
|
-
|
|
1621
|
+
else {
|
|
1622
|
+
// 情况 B:跨内外盘(原子化拆分)
|
|
1623
|
+
tasks.push({
|
|
1624
|
+
target: FLAP_PORTAL,
|
|
1625
|
+
amount: needed,
|
|
1626
|
+
data: encodeBuyCall(tokenAddress, needed, 0n)
|
|
1627
|
+
});
|
|
1628
|
+
isTrigger = true; // 肯定是毕业触发者
|
|
1629
|
+
currentCurveTotal = remainingToGraduateWei;
|
|
1630
|
+
if (enableDexBuy) {
|
|
1631
|
+
const excess = amountWei - needed;
|
|
1632
|
+
const dexType = (tokenState.lpFeeProfile >= 0 && tokenState.tokenVersion >= 4) ? 'V3' : 'V2';
|
|
1633
|
+
const deadline = Math.floor(Date.now() / 1000) + 1200;
|
|
1634
|
+
let swapData;
|
|
1635
|
+
let routerAddress;
|
|
1636
|
+
if (dexType === 'V3') {
|
|
1637
|
+
routerAddress = POTATOSWAP_V3_ROUTER;
|
|
1638
|
+
swapData = encodeSwapExactETHForTokensV3({
|
|
1639
|
+
tokenIn: WOKB, tokenOut: tokenAddress,
|
|
1640
|
+
fee: lpFeeProfileToV3Fee(tokenState.lpFeeProfile),
|
|
1641
|
+
recipient: ZERO_ADDRESS, // AA 内部 call,最终由 execute/executeBatch 决定 recipient
|
|
1642
|
+
deadline, amountIn: excess, amountOutMinimum: 0n, sqrtPriceLimitX96: 0n
|
|
1643
|
+
});
|
|
1644
|
+
}
|
|
1645
|
+
else {
|
|
1646
|
+
routerAddress = POTATOSWAP_V2_ROUTER;
|
|
1647
|
+
swapData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], ZERO_ADDRESS, deadline);
|
|
1648
|
+
}
|
|
1649
|
+
tasks.push({ target: routerAddress, amount: excess, data: swapData });
|
|
1650
|
+
}
|
|
1657
1651
|
}
|
|
1658
|
-
// 如果钱包既不在 curveSet 也不在 dexSet,则跳过
|
|
1659
1652
|
}
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
}
|
|
1674
|
-
else {
|
|
1675
|
-
// 当前钱包金额超过了剩余容量,将其限制为 needed
|
|
1676
|
-
curveBuyers.push({ wallet, amount: needed });
|
|
1677
|
-
currentCurveTotal = remainingToGraduateWei;
|
|
1678
|
-
// 超出部分暂不处理(遵循单钱包单属性逻辑)
|
|
1679
|
-
}
|
|
1653
|
+
else if (enableDexBuy) {
|
|
1654
|
+
// 情况 C:全在外盘
|
|
1655
|
+
const dexType = (tokenState.lpFeeProfile >= 0 && tokenState.tokenVersion >= 4) ? 'V3' : 'V2';
|
|
1656
|
+
const deadline = Math.floor(Date.now() / 1000) + 1200;
|
|
1657
|
+
let swapData;
|
|
1658
|
+
let routerAddress;
|
|
1659
|
+
if (dexType === 'V3') {
|
|
1660
|
+
routerAddress = POTATOSWAP_V3_ROUTER;
|
|
1661
|
+
swapData = encodeSwapExactETHForTokensV3({
|
|
1662
|
+
tokenIn: WOKB, tokenOut: tokenAddress,
|
|
1663
|
+
fee: lpFeeProfileToV3Fee(tokenState.lpFeeProfile),
|
|
1664
|
+
recipient: ZERO_ADDRESS,
|
|
1665
|
+
deadline, amountIn: amountWei, amountOutMinimum: 0n, sqrtPriceLimitX96: 0n
|
|
1666
|
+
});
|
|
1680
1667
|
}
|
|
1681
|
-
else
|
|
1682
|
-
|
|
1668
|
+
else {
|
|
1669
|
+
routerAddress = POTATOSWAP_V2_ROUTER;
|
|
1670
|
+
swapData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], ZERO_ADDRESS, deadline);
|
|
1683
1671
|
}
|
|
1672
|
+
tasks.push({ target: routerAddress, amount: amountWei, data: swapData });
|
|
1673
|
+
}
|
|
1674
|
+
if (tasks.length > 0) {
|
|
1675
|
+
buyerTasks.push({ wallet, calls: tasks, isGraduationTrigger: isTrigger });
|
|
1684
1676
|
}
|
|
1685
1677
|
}
|
|
1686
|
-
// ✅ 统一计算内盘买入总金额(无论使用哪种分组方式)
|
|
1687
|
-
const curveTotalWei = curveBuyers.reduce((sum, b) => sum + b.amount, 0n);
|
|
1688
1678
|
// 5. 构建 UserOps
|
|
1689
1679
|
const effConfig = { ...(this.config ?? {}), ...(config ?? {}) };
|
|
1690
|
-
const signedTransactions = [];
|
|
1691
1680
|
const allOps = [];
|
|
1692
|
-
//
|
|
1693
|
-
const
|
|
1681
|
+
// 预获取所有发送者信息
|
|
1682
|
+
const buyerInfos = await mapWithConcurrency(buyerTasks.map(b => b.wallet), 5, async (w) => {
|
|
1694
1683
|
const info = await aaManager.getAccountInfo(w.address);
|
|
1695
1684
|
nonceMap.init(info.sender, info.nonce);
|
|
1696
1685
|
return info;
|
|
1697
1686
|
});
|
|
1698
|
-
const
|
|
1699
|
-
const { wallet,
|
|
1700
|
-
const info =
|
|
1701
|
-
//
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1687
|
+
const signedOps = await mapWithConcurrency(buyerTasks, 5, async (task, i) => {
|
|
1688
|
+
const { wallet, calls, isGraduationTrigger } = task;
|
|
1689
|
+
const info = buyerInfos[i];
|
|
1690
|
+
// 修正 Swap 接收地址为 Sender
|
|
1691
|
+
const finalCalls = calls.map(c => {
|
|
1692
|
+
let callData = c.data;
|
|
1693
|
+
// 简单处理:将 DEX swap 中的 recipient (ZERO_ADDRESS) 替换为 info.sender
|
|
1694
|
+
// 对 V2/V3 编码后的数据,地址通常在末尾或固定偏移处,但最稳妥是重新编码
|
|
1695
|
+
// 这里为了简洁,假设 swapData 在编码时已经包含了 recipient
|
|
1696
|
+
// 由于上面我们设置了 recipient: ZERO_ADDRESS,我们需要在打包前基于正确的 sender 重新编码
|
|
1697
|
+
// 为了绝对安全,我们在此时重新编码带有正确 sender 的 swapData
|
|
1698
|
+
if (c.target === POTATOSWAP_V3_ROUTER || c.target === POTATOSWAP_V2_ROUTER) {
|
|
1699
|
+
const dexType = (tokenState.lpFeeProfile >= 0 && tokenState.tokenVersion >= 4) ? 'V3' : 'V2';
|
|
1700
|
+
const deadline = Math.floor(Date.now() / 1000) + 1200;
|
|
1701
|
+
if (dexType === 'V3') {
|
|
1702
|
+
callData = encodeSwapExactETHForTokensV3({
|
|
1703
|
+
tokenIn: WOKB, tokenOut: tokenAddress,
|
|
1704
|
+
fee: lpFeeProfileToV3Fee(tokenState.lpFeeProfile),
|
|
1705
|
+
recipient: info.sender,
|
|
1706
|
+
deadline, amountIn: c.amount, amountOutMinimum: 0n, sqrtPriceLimitX96: 0n
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
else {
|
|
1710
|
+
callData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], info.sender, deadline);
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
return { ...c, data: callData };
|
|
1714
|
+
});
|
|
1715
|
+
let mainCallData;
|
|
1716
|
+
if (finalCalls.length === 1) {
|
|
1717
|
+
mainCallData = encodeExecute(finalCalls[0].target, finalCalls[0].amount, finalCalls[0].data);
|
|
1718
|
+
}
|
|
1719
|
+
else {
|
|
1720
|
+
mainCallData = encodeExecuteBatch(finalCalls.map(c => c.target), finalCalls.map(c => c.amount), finalCalls.map(c => c.data));
|
|
1721
|
+
}
|
|
1708
1722
|
const callGasLimit = isGraduationTrigger
|
|
1709
|
-
?
|
|
1710
|
-
: (effConfig.fixedGas?.callGasLimit ??
|
|
1711
|
-
const
|
|
1723
|
+
? 7000000n
|
|
1724
|
+
: (effConfig.fixedGas?.callGasLimit ?? 800000n);
|
|
1725
|
+
const opRes = await aaManager.buildUserOpWithFixedGas({
|
|
1712
1726
|
ownerWallet: wallet,
|
|
1713
1727
|
sender: info.sender,
|
|
1714
|
-
callData:
|
|
1728
|
+
callData: mainCallData,
|
|
1715
1729
|
nonce: nonceMap.next(info.sender),
|
|
1716
1730
|
initCode: info.deployed ? '0x' : (await aaManager.generateInitCode(wallet.address)),
|
|
1717
1731
|
deployed: info.deployed,
|
|
1718
1732
|
fixedGas: { callGasLimit }
|
|
1719
1733
|
});
|
|
1720
|
-
const
|
|
1721
|
-
return
|
|
1734
|
+
const signed = await aaManager.signUserOp(opRes.userOp, wallet);
|
|
1735
|
+
return signed.userOp;
|
|
1722
1736
|
});
|
|
1723
|
-
allOps.push(...
|
|
1724
|
-
//
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
const
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
let routerAddress;
|
|
1744
|
-
if (dexType === 'V3') {
|
|
1745
|
-
routerAddress = POTATOSWAP_V3_ROUTER;
|
|
1746
|
-
swapData = encodeSwapExactETHForTokensV3({
|
|
1747
|
-
tokenIn: WOKB,
|
|
1748
|
-
tokenOut: tokenAddress,
|
|
1749
|
-
fee: lpFeeProfileToV3Fee(tokenState.lpFeeProfile),
|
|
1750
|
-
recipient: info.sender,
|
|
1751
|
-
deadline,
|
|
1752
|
-
amountIn: amount,
|
|
1753
|
-
amountOutMinimum: 0n,
|
|
1754
|
-
sqrtPriceLimitX96: 0n
|
|
1755
|
-
});
|
|
1756
|
-
}
|
|
1757
|
-
else {
|
|
1758
|
-
routerAddress = POTATOSWAP_V2_ROUTER;
|
|
1759
|
-
swapData = encodeSwapExactETHForTokensSupportingFee(0n, [WOKB, tokenAddress], info.sender, deadline);
|
|
1760
|
-
}
|
|
1761
|
-
const dexBuyCallData = encodeExecute(routerAddress, amount, swapData);
|
|
1762
|
-
const dexBuyOpRes = await aaManager.buildUserOpWithFixedGas({
|
|
1763
|
-
ownerWallet: wallet,
|
|
1764
|
-
sender: info.sender,
|
|
1765
|
-
callData: dexBuyCallData,
|
|
1766
|
-
nonce: nonceMap.next(info.sender),
|
|
1767
|
-
initCode: info.deployed ? '0x' : (await aaManager.generateInitCode(wallet.address)),
|
|
1768
|
-
deployed: info.deployed,
|
|
1769
|
-
fixedGas: { callGasLimit: effConfig.fixedGas?.callGasLimit ?? 800000n } // 外盘 swap 需要更多 gas
|
|
1770
|
-
});
|
|
1771
|
-
const signedDexBuyOp = await aaManager.signUserOp(dexBuyOpRes.userOp, wallet);
|
|
1772
|
-
return signedDexBuyOp.userOp;
|
|
1737
|
+
allOps.push(...signedOps);
|
|
1738
|
+
// 6. 签名分批 handleOps 交易 (对齐用户分批需求,防止 gasLimit 爆表)
|
|
1739
|
+
const signedTransactions = [];
|
|
1740
|
+
const maxOpsPerBatch = effConfig.maxOpsPerHandleOps ? Math.floor(effConfig.maxOpsPerHandleOps) : allOps.length;
|
|
1741
|
+
const batches = [];
|
|
1742
|
+
for (let i = 0; i < allOps.length; i += maxOpsPerBatch) {
|
|
1743
|
+
batches.push(allOps.slice(i, i + maxOpsPerBatch));
|
|
1744
|
+
}
|
|
1745
|
+
let currentPayerNonce = params.payerStartNonce ?? (await provider.getTransactionCount(payerWallet.address, 'pending'));
|
|
1746
|
+
for (const batchOps of batches) {
|
|
1747
|
+
// 如果批次包含毕业触发 Op (7M), 则提高 gasLimit 预估
|
|
1748
|
+
const hasGraduation = batchOps.some(op => BigInt(op.callGasLimit) >= 7000000n);
|
|
1749
|
+
const batchGasLimit = hasGraduation ? 8000000n : effConfig.gasLimit;
|
|
1750
|
+
const signedBatchTx = await this.signHandleOpsTx({
|
|
1751
|
+
ops: batchOps,
|
|
1752
|
+
payerWallet: payerWallet,
|
|
1753
|
+
beneficiary: params.beneficiary ?? payerWallet.address,
|
|
1754
|
+
nonce: currentPayerNonce,
|
|
1755
|
+
gasLimit: batchGasLimit,
|
|
1756
|
+
gasPrice: effConfig.minGasPriceGwei ? ethers.parseUnits(effConfig.minGasPriceGwei.toString(), 'gwei') : undefined
|
|
1773
1757
|
});
|
|
1774
|
-
|
|
1758
|
+
signedTransactions.push(signedBatchTx);
|
|
1759
|
+
currentPayerNonce++;
|
|
1775
1760
|
}
|
|
1776
|
-
//
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
nonce: startNonce,
|
|
1783
|
-
gasLimit: effConfig.gasLimit,
|
|
1784
|
-
gasPrice: effConfig.minGasPriceGwei ? ethers.parseUnits(effConfig.minGasPriceGwei.toString(), 'gwei') : undefined
|
|
1785
|
-
});
|
|
1786
|
-
signedTransactions.push(signedMainTx);
|
|
1787
|
-
// 7. 计算并签利润提取交易 (EOA 转账,千分之 4)
|
|
1788
|
-
const totalBuyWei = curveTotalWei + totalDexBuyWei;
|
|
1789
|
-
const profitWei = (totalBuyWei * 4n) / 1000n; // 0.4%
|
|
1761
|
+
// 7. 计算利润 (0.4%)
|
|
1762
|
+
let totalDexBuyWei = 0n;
|
|
1763
|
+
buyerTasks.forEach(t => t.calls.forEach(c => { if (c.target !== FLAP_PORTAL)
|
|
1764
|
+
totalDexBuyWei += c.amount; }));
|
|
1765
|
+
const totalBuyWei = currentCurveTotal + totalDexBuyWei;
|
|
1766
|
+
const profitWei = (totalBuyWei * 4n) / 1000n;
|
|
1790
1767
|
if (profitWei > 0n) {
|
|
1791
1768
|
const profitRecipient = effConfig.profitRecipient || PROFIT_CONFIG.RECIPIENT;
|
|
1792
1769
|
const feeData = await provider.getFeeData();
|
|
@@ -1794,7 +1771,7 @@ export class BundleExecutor {
|
|
|
1794
1771
|
const profitTx = await payerWallet.signTransaction({
|
|
1795
1772
|
to: profitRecipient,
|
|
1796
1773
|
value: profitWei,
|
|
1797
|
-
nonce:
|
|
1774
|
+
nonce: currentPayerNonce, // 分批后最后一个 nonce
|
|
1798
1775
|
gasLimit: 21000n,
|
|
1799
1776
|
gasPrice,
|
|
1800
1777
|
chainId: effConfig.chainId ?? 196,
|
|
@@ -1805,9 +1782,9 @@ export class BundleExecutor {
|
|
|
1805
1782
|
return {
|
|
1806
1783
|
signedTransactions,
|
|
1807
1784
|
metadata: {
|
|
1808
|
-
curveCount:
|
|
1809
|
-
dexCount:
|
|
1810
|
-
curveTotalAmount: ethers.formatEther(
|
|
1785
|
+
curveCount: buyerTasks.filter(t => t.calls.some(c => c.target === FLAP_PORTAL)).length,
|
|
1786
|
+
dexCount: buyerTasks.filter(t => t.calls.some(c => c.target !== FLAP_PORTAL)).length,
|
|
1787
|
+
curveTotalAmount: ethers.formatEther(currentCurveTotal),
|
|
1811
1788
|
dexTotalAmount: ethers.formatEther(totalDexBuyWei),
|
|
1812
1789
|
tokenAddress
|
|
1813
1790
|
}
|
|
@@ -69,7 +69,7 @@ export declare const ENTRYPOINT_ABI: readonly ["function getNonce(address sender
|
|
|
69
69
|
/** SimpleAccount ABI */
|
|
70
70
|
export declare const SIMPLE_ACCOUNT_ABI: readonly ["function execute(address dest, uint256 value, bytes func) external", "function executeBatch(address[] calldata dest, bytes[] calldata func) external", "function executeBatch(address[] calldata dest, uint256[] calldata values, bytes[] calldata func) external"];
|
|
71
71
|
/** Flap Portal ABI */
|
|
72
|
-
export declare const PORTAL_ABI: readonly ["function swapExactInput((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData) params) payable returns (uint256)", "function
|
|
72
|
+
export declare const PORTAL_ABI: readonly ["function swapExactInput((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData) params) payable returns (uint256)", "function buy(address token, address to, uint256 minAmount) external payable returns (uint256)", "function sell(address token, uint256 amount, uint256 minEth) external returns (uint256)", "function quoteExactInput((address inputToken,address outputToken,uint256 inputAmount)) external returns (uint256)", "function previewBuy(address token, uint256 ethAmount) external view returns (uint256)", "function previewSell(address token, uint256 tokenAmount) external view returns (uint256)", "function getTokenV5(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32))", "function getTokenV7(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32,uint8,uint8))", "function newTokenV2((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData) params) external payable returns (address)", "function newTokenV3((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData,bytes32 extensionID,bytes extensionData) params) external payable returns (address)", "function newTokenV4((string name,string symbol,string meta,uint8 dexThresh,bytes32 salt,uint16 taxRate,uint8 migratorType,address quoteToken,uint256 quoteAmt,address beneficiary,bytes permitData,bytes32 extensionID,bytes extensionData,uint8 dexId,uint8 lpFeeProfile) params) external payable returns (address)"];
|
|
73
73
|
/** ERC20 ABI */
|
|
74
74
|
export declare const ERC20_ABI: readonly ["function balanceOf(address account) view returns (uint256)", "function allowance(address owner, address spender) view returns (uint256)", "function approve(address spender, uint256 amount) returns (bool)", "function transfer(address to, uint256 amount) returns (bool)", "function decimals() view returns (uint8)", "function symbol() view returns (string)", "function name() view returns (string)"];
|
|
75
75
|
/** PotatoSwap V2 Router ABI */
|
package/dist/xlayer/constants.js
CHANGED
|
@@ -107,8 +107,6 @@ export const SIMPLE_ACCOUNT_ABI = [
|
|
|
107
107
|
/** Flap Portal ABI */
|
|
108
108
|
export const PORTAL_ABI = [
|
|
109
109
|
'function swapExactInput((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData) params) payable returns (uint256)',
|
|
110
|
-
// ✅ swapExactInputV3: 支持 extensionData 扩展,用于买到毕业等场景
|
|
111
|
-
'function swapExactInputV3((address inputToken,address outputToken,uint256 inputAmount,uint256 minOutputAmount,bytes permitData,bytes extensionData)) external payable returns (uint256)',
|
|
112
110
|
'function buy(address token, address to, uint256 minAmount) external payable returns (uint256)',
|
|
113
111
|
'function sell(address token, uint256 amount, uint256 minEth) external returns (uint256)',
|
|
114
112
|
'function quoteExactInput((address inputToken,address outputToken,uint256 inputAmount)) external returns (uint256)',
|
package/dist/xlayer/index.d.ts
CHANGED
|
@@ -61,7 +61,7 @@ export * from './types.js';
|
|
|
61
61
|
import type { BundleSwapSignParams, BundleSwapSignResult, BundleBatchSwapSignParams, BundleBatchSwapSignResult } from './types.js';
|
|
62
62
|
export { BundlerClient, createBundlerClient, type BundlerConfig, type BundlerReceipt, } from './bundler.js';
|
|
63
63
|
export { AAAccountManager, createAAAccountManager, predictSender, createWallet, encodeExecute, encodeExecuteBatch, generateAAWallets, generateAAWalletsFromMnemonic, predictSendersFromPrivateKeys, type GeneratedAAWallet, type GenerateAAWalletsParams, type GenerateAAWalletsResult, } from './aa-account.js';
|
|
64
|
-
export { encodeBuyCall,
|
|
64
|
+
export { encodeBuyCall, encodeSellCall, encodeApproveCall, encodeTransferCall, PortalQuery, createPortalQuery, applySlippage, formatOkb, parseOkb, formatTokenAmount, parseTokenAmount, type PortalQueryConfig, } from './portal-ops.js';
|
|
65
65
|
export { BundleExecutor, createBundleExecutor, bundleBuy, bundleSell, bundleBuySell, bundleCreateBuySign, bundleCreateToDexSign, bundleGraduateBuy, bundlePreApprove, checkApprovalStatus, } from './bundle.js';
|
|
66
66
|
export { DexBundleExecutor, createDexBundleExecutor, dexBundleBuySell, type DexBundleConfig, type DexBundleBuySellParams, } from './dex-bundle.js';
|
|
67
67
|
export { AAPortalSwapExecutor, } from './portal-bundle-swap.js';
|
package/dist/xlayer/index.js
CHANGED
|
@@ -77,7 +77,7 @@ generateAAWallets, generateAAWalletsFromMnemonic, predictSendersFromPrivateKeys,
|
|
|
77
77
|
// ============================================================================
|
|
78
78
|
// Portal 操作
|
|
79
79
|
// ============================================================================
|
|
80
|
-
export { encodeBuyCall,
|
|
80
|
+
export { encodeBuyCall, encodeSellCall, encodeApproveCall, encodeTransferCall, PortalQuery, createPortalQuery, applySlippage, formatOkb, parseOkb, formatTokenAmount, parseTokenAmount, } from './portal-ops.js';
|
|
81
81
|
// ============================================================================
|
|
82
82
|
// 捆绑交易
|
|
83
83
|
// ============================================================================
|
|
@@ -16,19 +16,6 @@ import { JsonRpcProvider } from 'ethers';
|
|
|
16
16
|
* @param minOutputAmount 最小输出代币数量(默认 0)
|
|
17
17
|
*/
|
|
18
18
|
export declare function encodeBuyCall(tokenAddress: string, inputAmount: bigint, minOutputAmount?: bigint): string;
|
|
19
|
-
/**
|
|
20
|
-
* 编码买入调用 V3(swapExactInputV3: native -> token,支持 extensionData)
|
|
21
|
-
*
|
|
22
|
-
* 与 BSC 版本的 create-to-dex.ts 保持一致:
|
|
23
|
-
* - 统一优先使用 swapExactInputV3(支持 extensionData;无扩展时传 0x 即可)
|
|
24
|
-
* - 这里的 V3 指"交易接口版本(扩展支持)",不是"外盘 V3 池子"
|
|
25
|
-
*
|
|
26
|
-
* @param tokenAddress 目标代币地址
|
|
27
|
-
* @param inputAmount 输入 OKB 数量(wei)
|
|
28
|
-
* @param minOutputAmount 最小输出代币数量(默认 0)
|
|
29
|
-
* @param extensionData 扩展数据(默认 0x)
|
|
30
|
-
*/
|
|
31
|
-
export declare function encodeBuyCallV3(tokenAddress: string, inputAmount: bigint, minOutputAmount?: bigint, extensionData?: string): string;
|
|
32
19
|
/**
|
|
33
20
|
* 编码卖出调用(swapExactInput: token -> native)
|
|
34
21
|
*
|
|
@@ -33,30 +33,6 @@ export function encodeBuyCall(tokenAddress, inputAmount, minOutputAmount = 0n) {
|
|
|
33
33
|
},
|
|
34
34
|
]);
|
|
35
35
|
}
|
|
36
|
-
/**
|
|
37
|
-
* 编码买入调用 V3(swapExactInputV3: native -> token,支持 extensionData)
|
|
38
|
-
*
|
|
39
|
-
* 与 BSC 版本的 create-to-dex.ts 保持一致:
|
|
40
|
-
* - 统一优先使用 swapExactInputV3(支持 extensionData;无扩展时传 0x 即可)
|
|
41
|
-
* - 这里的 V3 指"交易接口版本(扩展支持)",不是"外盘 V3 池子"
|
|
42
|
-
*
|
|
43
|
-
* @param tokenAddress 目标代币地址
|
|
44
|
-
* @param inputAmount 输入 OKB 数量(wei)
|
|
45
|
-
* @param minOutputAmount 最小输出代币数量(默认 0)
|
|
46
|
-
* @param extensionData 扩展数据(默认 0x)
|
|
47
|
-
*/
|
|
48
|
-
export function encodeBuyCallV3(tokenAddress, inputAmount, minOutputAmount = 0n, extensionData = '0x') {
|
|
49
|
-
return portalIface.encodeFunctionData('swapExactInputV3', [
|
|
50
|
-
{
|
|
51
|
-
inputToken: ZERO_ADDRESS,
|
|
52
|
-
outputToken: tokenAddress,
|
|
53
|
-
inputAmount,
|
|
54
|
-
minOutputAmount,
|
|
55
|
-
permitData: '0x',
|
|
56
|
-
extensionData,
|
|
57
|
-
},
|
|
58
|
-
]);
|
|
59
|
-
}
|
|
60
36
|
/**
|
|
61
37
|
* 编码卖出调用(swapExactInput: token -> native)
|
|
62
38
|
*
|
package/dist/xlayer/types.d.ts
CHANGED
|
@@ -80,6 +80,8 @@ export interface XLayerConfig {
|
|
|
80
80
|
gasLimit?: bigint;
|
|
81
81
|
/** Payer handleOps 交易的最小 gasPrice (Gwei) */
|
|
82
82
|
minGasPriceGwei?: number;
|
|
83
|
+
/** 单个 handleOps 交易中最多包含的 UserOp 数量(默认 5) */
|
|
84
|
+
maxOpsPerHandleOps?: number;
|
|
83
85
|
/** 利润尾笔原生转账 gasLimit(默认 21000n) */
|
|
84
86
|
profitTailGasLimit?: bigint;
|
|
85
87
|
/**
|
|
@@ -860,16 +862,8 @@ export interface BundleGraduateBuyParams {
|
|
|
860
862
|
walletAmounts?: Record<string, number>;
|
|
861
863
|
/** 是否启用外盘买入(默认 false) */
|
|
862
864
|
enableDexBuy?: boolean;
|
|
863
|
-
/** 内盘钱包地址列表(可选,传入则使用前端分组,不传则 SDK 自动分组) */
|
|
864
|
-
curveAddresses?: string[];
|
|
865
|
-
/** 外盘钱包地址列表(可选) */
|
|
866
|
-
dexAddresses?: string[];
|
|
867
|
-
/** 动态毕业阈值(可选,不传则使用默认 73.38 OKB) */
|
|
868
|
-
graduationAmount?: number;
|
|
869
865
|
/** 配置覆盖 */
|
|
870
866
|
config?: Partial<XLayerConfig>;
|
|
871
|
-
/** 扩展数据(传递给 swapExactInputV3,默认 0x) */
|
|
872
|
-
extensionData?: string;
|
|
873
867
|
/** Payer 起始 nonce */
|
|
874
868
|
payerStartNonce?: number;
|
|
875
869
|
/** beneficiary(默认 payer 地址) */
|
package/package.json
CHANGED
|
@@ -1,16 +0,0 @@
|
|
|
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;
|
|
@@ -1,146 +0,0 @@
|
|
|
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
|
-
}
|