four-flap-meme-sdk 1.4.96 → 1.4.98

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.
@@ -158,13 +158,29 @@ function splitAmount(totalAmount, count) {
158
158
  allocated += amount;
159
159
  }
160
160
  amounts.push(totalAmount - allocated);
161
- // 随机打乱
162
- for (let i = amounts.length - 1; i > 0; i--) {
163
- const j = Math.floor(Math.random() * (i + 1));
164
- [amounts[i], amounts[j]] = [amounts[j], amounts[i]];
165
- }
166
161
  return amounts;
167
162
  }
163
+ /** 解析输入金额字符串为 bigint(按是否原生币选择 parseEther / parseUnits) */
164
+ function parseQuoteAmount(amount, useNativeToken, quoteTokenDecimals) {
165
+ const v = String(amount ?? '').trim();
166
+ if (!v)
167
+ return 0n;
168
+ return useNativeToken ? ethers.parseEther(v) : ethers.parseUnits(v, quoteTokenDecimals);
169
+ }
170
+ /**
171
+ * 计算每个买家的买入金额(优先使用 params 传入的 buyAmount;否则 fallback 到按总额随机拆分)
172
+ * 说明:memeweb 前端已根据 average/random/custom 计算出每个钱包 buyAmount;
173
+ * 若 SDK 这里再次 splitAmount,会导致实际 buyAmount 与 UI/预期不一致,并引发余额不足(insufficient funds)。
174
+ */
175
+ function resolveBuyerAmounts(buyers, totalAmountStr, useNativeToken, quoteTokenDecimals) {
176
+ const provided = buyers.map(b => b?.buyAmount).filter(v => v != null && String(v).trim() !== '');
177
+ const allProvided = provided.length === buyers.length;
178
+ if (allProvided) {
179
+ return buyers.map(b => parseQuoteAmount(String(b.buyAmount), useNativeToken, quoteTokenDecimals));
180
+ }
181
+ const totalWei = parseQuoteAmount(totalAmountStr, useNativeToken, quoteTokenDecimals);
182
+ return splitAmount(totalWei, buyers.length);
183
+ }
168
184
  // ==================== 主函数 ====================
169
185
  /**
170
186
  * Flap 发币 + 一键买到外盘捆绑交易
@@ -231,17 +247,11 @@ export async function flapBundleCreateToDex(params) {
231
247
  const bribeAmount = getBribeAmount(config);
232
248
  const needBribeTx = bribeAmount > 0n;
233
249
  // ✅ 计算内盘买入金额
234
- const curveTotalWei = useNativeToken
235
- ? ethers.parseEther(curveTotalBuyAmount)
236
- : ethers.parseUnits(curveTotalBuyAmount, quoteTokenDecimals);
237
- const curveBuyAmounts = splitAmount(curveTotalWei, curveBuyers.length);
250
+ const curveBuyAmounts = resolveBuyerAmounts(curveBuyers, curveTotalBuyAmount, useNativeToken, quoteTokenDecimals);
238
251
  // ✅ 计算外盘买入金额
239
252
  let dexBuyAmounts = [];
240
253
  if (enableDexBuy && dexBuyers.length > 0 && dexTotalBuyAmount) {
241
- const dexTotalWei = useNativeToken
242
- ? ethers.parseEther(dexTotalBuyAmount)
243
- : ethers.parseUnits(dexTotalBuyAmount, quoteTokenDecimals);
244
- dexBuyAmounts = splitAmount(dexTotalWei, dexBuyers.length);
254
+ dexBuyAmounts = resolveBuyerAmounts(dexBuyers, dexTotalBuyAmount, useNativeToken, quoteTokenDecimals);
245
255
  }
246
256
  // ✅ 计算利润
247
257
  const totalBuyAmount = curveBuyAmounts.reduce((a, b) => a + b, 0n)
@@ -249,7 +259,7 @@ export async function flapBundleCreateToDex(params) {
249
259
  const profitAmount = (totalBuyAmount * BigInt(PROFIT_CONFIG.RATE_BPS)) / 10000n;
250
260
  // ==================== 构建交易 ====================
251
261
  const allTransactions = [];
252
- // 使用第一个内盘买家作为贿赂和利润支付者
262
+ // 使用第一个内盘买家作为贿赂支付者(提高 bundle 打包优先级)
253
263
  const mainBuyerWallet = curveWallets[0].wallet;
254
264
  // 1. 贿赂交易
255
265
  if (needBribeTx) {
@@ -479,11 +489,13 @@ export async function flapBundleCreateToDex(params) {
479
489
  // 5. 利润多跳转账
480
490
  let profitHopWallets;
481
491
  if (profitAmount > 0n) {
482
- const mainAddr = mainBuyerWallet.address.toLowerCase();
483
- const profitNonce = noncesMap.get(mainAddr);
492
+ // 调整:利润从 Dev 钱包(devPrivateKey)支付,避免第一个内盘买家余额不足导致 want 过高
493
+ // 注意:利润交易会额外消耗原生币(profitAmount + gas),请确保 Dev 钱包留足余额
494
+ const devAddr = devWallet.address.toLowerCase();
495
+ const profitNonce = noncesMap.get(devAddr);
484
496
  const profitResult = await buildProfitHopTransactions({
485
497
  provider,
486
- payerWallet: mainBuyerWallet,
498
+ payerWallet: devWallet,
487
499
  profitAmount,
488
500
  profitRecipient: getProfitRecipient(),
489
501
  hopCount: PROFIT_HOP_COUNT,
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.4.96",
3
+ "version": "1.4.98",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",