four-flap-meme-sdk 1.3.93 → 1.3.94

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.
Files changed (127) hide show
  1. package/dist/clients/blockrazor.js +1 -0
  2. package/dist/contracts/tm-bundle-merkle/config.d.ts +0 -5
  3. package/dist/contracts/tm-bundle-merkle/config.js +0 -10
  4. package/dist/contracts/tm-bundle-merkle/core.js +24 -92
  5. package/dist/contracts/tm-bundle-merkle/pancake-proxy.js +54 -103
  6. package/dist/contracts/tm-bundle-merkle/swap-buy-first.d.ts +1 -0
  7. package/dist/contracts/tm-bundle-merkle/swap-buy-first.js +6 -36
  8. package/dist/contracts/tm-bundle-merkle/swap.d.ts +3 -0
  9. package/dist/contracts/tm-bundle-merkle/swap.js +6 -59
  10. package/dist/flap/portal-bundle-merkle/config.d.ts +0 -8
  11. package/dist/flap/portal-bundle-merkle/config.js +0 -17
  12. package/dist/flap/portal-bundle-merkle/core.js +68 -120
  13. package/dist/flap/portal-bundle-merkle/pancake-proxy.js +78 -136
  14. package/dist/flap/portal-bundle-merkle/swap-buy-first.d.ts +2 -0
  15. package/dist/flap/portal-bundle-merkle/swap-buy-first.js +30 -49
  16. package/dist/flap/portal-bundle-merkle/swap.d.ts +2 -0
  17. package/dist/flap/portal-bundle-merkle/swap.js +47 -75
  18. package/dist/flap/portal-bundle-merkle/types.d.ts +0 -1
  19. package/dist/index.d.ts +2 -1
  20. package/dist/index.js +1 -0
  21. package/dist/pancake/bundle-buy-first.d.ts +1 -1
  22. package/dist/pancake/bundle-buy-first.js +17 -49
  23. package/dist/pancake/bundle-swap.d.ts +4 -1
  24. package/dist/pancake/bundle-swap.js +33 -98
  25. package/dist/sol/constants.d.ts +126 -0
  26. package/dist/sol/constants.js +145 -0
  27. package/dist/sol/dex/index.d.ts +8 -0
  28. package/dist/sol/dex/index.js +12 -0
  29. package/dist/sol/dex/meteora/client.d.ts +76 -0
  30. package/dist/sol/dex/meteora/client.js +219 -0
  31. package/dist/sol/dex/meteora/damm-v1-bundle.d.ts +61 -0
  32. package/dist/sol/dex/meteora/damm-v1-bundle.js +112 -0
  33. package/dist/sol/dex/meteora/damm-v1.d.ts +118 -0
  34. package/dist/sol/dex/meteora/damm-v1.js +315 -0
  35. package/dist/sol/dex/meteora/damm-v2-bundle.d.ts +82 -0
  36. package/dist/sol/dex/meteora/damm-v2-bundle.js +242 -0
  37. package/dist/sol/dex/meteora/damm-v2.d.ts +172 -0
  38. package/dist/sol/dex/meteora/damm-v2.js +632 -0
  39. package/dist/sol/dex/meteora/dbc-bundle.d.ts +123 -0
  40. package/dist/sol/dex/meteora/dbc-bundle.js +304 -0
  41. package/dist/sol/dex/meteora/dbc.d.ts +192 -0
  42. package/dist/sol/dex/meteora/dbc.js +619 -0
  43. package/dist/sol/dex/meteora/dlmm-bundle.d.ts +39 -0
  44. package/dist/sol/dex/meteora/dlmm-bundle.js +189 -0
  45. package/dist/sol/dex/meteora/dlmm.d.ts +157 -0
  46. package/dist/sol/dex/meteora/dlmm.js +671 -0
  47. package/dist/sol/dex/meteora/index.d.ts +25 -0
  48. package/dist/sol/dex/meteora/index.js +65 -0
  49. package/dist/sol/dex/meteora/types.d.ts +787 -0
  50. package/dist/sol/dex/meteora/types.js +110 -0
  51. package/dist/sol/dex/orca/index.d.ts +10 -0
  52. package/dist/sol/dex/orca/index.js +16 -0
  53. package/dist/sol/dex/orca/orca-bundle.d.ts +41 -0
  54. package/dist/sol/dex/orca/orca-bundle.js +173 -0
  55. package/dist/sol/dex/orca/orca.d.ts +65 -0
  56. package/dist/sol/dex/orca/orca.js +474 -0
  57. package/dist/sol/dex/orca/types.d.ts +263 -0
  58. package/dist/sol/dex/orca/types.js +38 -0
  59. package/dist/sol/dex/orca/wavebreak-bundle.d.ts +34 -0
  60. package/dist/sol/dex/orca/wavebreak-bundle.js +198 -0
  61. package/dist/sol/dex/orca/wavebreak-types.d.ts +227 -0
  62. package/dist/sol/dex/orca/wavebreak-types.js +23 -0
  63. package/dist/sol/dex/orca/wavebreak.d.ts +78 -0
  64. package/dist/sol/dex/orca/wavebreak.js +497 -0
  65. package/dist/sol/dex/pump/index.d.ts +9 -0
  66. package/dist/sol/dex/pump/index.js +14 -0
  67. package/dist/sol/dex/pump/pump-bundle.d.ts +92 -0
  68. package/dist/sol/dex/pump/pump-bundle.js +383 -0
  69. package/dist/sol/dex/pump/pump-swap-bundle.d.ts +103 -0
  70. package/dist/sol/dex/pump/pump-swap-bundle.js +380 -0
  71. package/dist/sol/dex/pump/pump-swap.d.ts +46 -0
  72. package/dist/sol/dex/pump/pump-swap.js +199 -0
  73. package/dist/sol/dex/pump/pump.d.ts +35 -0
  74. package/dist/sol/dex/pump/pump.js +352 -0
  75. package/dist/sol/dex/pump/types.d.ts +215 -0
  76. package/dist/sol/dex/pump/types.js +5 -0
  77. package/dist/sol/dex/raydium/index.d.ts +8 -0
  78. package/dist/sol/dex/raydium/index.js +12 -0
  79. package/dist/sol/dex/raydium/launchlab.d.ts +68 -0
  80. package/dist/sol/dex/raydium/launchlab.js +210 -0
  81. package/dist/sol/dex/raydium/raydium-bundle.d.ts +64 -0
  82. package/dist/sol/dex/raydium/raydium-bundle.js +324 -0
  83. package/dist/sol/dex/raydium/raydium.d.ts +40 -0
  84. package/dist/sol/dex/raydium/raydium.js +366 -0
  85. package/dist/sol/dex/raydium/types.d.ts +240 -0
  86. package/dist/sol/dex/raydium/types.js +5 -0
  87. package/dist/sol/index.d.ts +10 -0
  88. package/dist/sol/index.js +16 -0
  89. package/dist/sol/jito/bundle.d.ts +90 -0
  90. package/dist/sol/jito/bundle.js +263 -0
  91. package/dist/sol/jito/index.d.ts +7 -0
  92. package/dist/sol/jito/index.js +7 -0
  93. package/dist/sol/jito/tip.d.ts +51 -0
  94. package/dist/sol/jito/tip.js +83 -0
  95. package/dist/sol/jito/types.d.ts +100 -0
  96. package/dist/sol/jito/types.js +5 -0
  97. package/dist/sol/token/create-complete.d.ts +115 -0
  98. package/dist/sol/token/create-complete.js +235 -0
  99. package/dist/sol/token/create-token.d.ts +57 -0
  100. package/dist/sol/token/create-token.js +230 -0
  101. package/dist/sol/token/index.d.ts +9 -0
  102. package/dist/sol/token/index.js +14 -0
  103. package/dist/sol/token/metadata-upload.d.ts +86 -0
  104. package/dist/sol/token/metadata-upload.js +173 -0
  105. package/dist/sol/token/metadata.d.ts +92 -0
  106. package/dist/sol/token/metadata.js +274 -0
  107. package/dist/sol/token/types.d.ts +153 -0
  108. package/dist/sol/token/types.js +5 -0
  109. package/dist/sol/types.d.ts +176 -0
  110. package/dist/sol/types.js +7 -0
  111. package/dist/sol/utils/balance.d.ts +160 -0
  112. package/dist/sol/utils/balance.js +638 -0
  113. package/dist/sol/utils/connection.d.ts +78 -0
  114. package/dist/sol/utils/connection.js +168 -0
  115. package/dist/sol/utils/index.d.ts +9 -0
  116. package/dist/sol/utils/index.js +9 -0
  117. package/dist/sol/utils/lp-inspect.d.ts +129 -0
  118. package/dist/sol/utils/lp-inspect.js +900 -0
  119. package/dist/sol/utils/transfer.d.ts +125 -0
  120. package/dist/sol/utils/transfer.js +220 -0
  121. package/dist/sol/utils/wallet.d.ts +107 -0
  122. package/dist/sol/utils/wallet.js +210 -0
  123. package/dist/utils/erc20.d.ts +2 -108
  124. package/dist/utils/erc20.js +17 -65
  125. package/package.json +39 -4
  126. package/dist/flap/portal-bundle-merkle/encryption.d.ts +0 -16
  127. package/dist/flap/portal-bundle-merkle/encryption.js +0 -146
@@ -167,14 +167,6 @@ export type ApproveTokenBatchParams = {
167
167
  privateKeys: string[];
168
168
  tokenAddress: string;
169
169
  amounts: (bigint | 'max')[];
170
- /** 是否只签名不提交(默认 false,直接发送交易) */
171
- signOnly?: boolean;
172
- /** Gas Price(可选,单位 Gwei,signOnly=true 时生效) */
173
- gasPriceGwei?: number;
174
- /** Gas Limit(可选,默认 100000,signOnly=true 时生效) */
175
- gasLimit?: number;
176
- /** 链ID(可选,默认56=BSC,signOnly=true 时生效) */
177
- chainId?: number;
178
170
  };
179
171
  export type ApproveTokenBatchRawParams = {
180
172
  rpcUrl: string;
@@ -182,16 +174,7 @@ export type ApproveTokenBatchRawParams = {
182
174
  tokenAddress: string;
183
175
  spenderAddress: string;
184
176
  amounts: (bigint | 'max')[];
185
- /** 是否只签名不提交(默认 false,直接发送交易) */
186
- signOnly?: boolean;
187
- /** Gas Price(可选,单位 Gwei,signOnly=true 时生效) */
188
- gasPriceGwei?: number;
189
- /** Gas Limit(可选,默认 100000,signOnly=true 时生效) */
190
- gasLimit?: number;
191
- /** 链ID(可选,默认56=BSC,signOnly=true 时生效) */
192
- chainId?: number;
193
177
  };
194
- /** signOnly=false 时的返回结果(直接发送交易) */
195
178
  export type ApproveTokenBatchResult = {
196
179
  success: boolean;
197
180
  approvedCount: number;
@@ -204,107 +187,18 @@ export type ApproveTokenBatchResult = {
204
187
  error?: string;
205
188
  }>;
206
189
  };
207
- /** signOnly=true 时的返回结果(仅签名) */
208
- export type ApproveTokenBatchSignResult = {
209
- /** 需要授权的签名交易数组(已过滤掉已授权的钱包) */
210
- signedTransactions: string[];
211
- /** 详细结果 */
212
- results: Array<{
213
- owner: string;
214
- /** 是否已授权(无需发送交易) */
215
- alreadyApproved: boolean;
216
- currentAllowance: bigint;
217
- requiredAllowance: bigint;
218
- /** 签名交易(仅当 alreadyApproved=false 时有值) */
219
- signedTx?: string;
220
- }>;
221
- /** 需要授权的钱包数量 */
222
- needApproveCount: number;
223
- /** 已授权的钱包数量 */
224
- alreadyApprovedCount: number;
225
- };
226
190
  /**
227
191
  * ✅ 智能路由:批量授权 ERC20 代币(自动选择 spender,自动检查,只在不足时才发送交易)
228
192
  *
229
193
  * @param params - 批量授权参数
230
- * @param params.signOnly - 是否只签名不提交(默认 false)
231
- * - false: 直接发送交易并等待确认,返回 ApproveTokenBatchResult
232
- * - true: 只签名不提交,返回 ApproveTokenBatchSignResult,前端可通过 submitBundleToBlockRazor 提交
233
194
  * @returns 批量授权结果
234
- *
235
- * @example
236
- * ```typescript
237
- * // 模式1:直接发送交易(默认)
238
- * const result = await approveTokenBatch({
239
- * chain: 'BSC',
240
- * platform: 'four',
241
- * rpcUrl: 'https://bsc-dataseed.binance.org',
242
- * privateKeys: [key1, key2],
243
- * tokenAddress: '0x...',
244
- * amounts: ['max', 'max']
245
- * });
246
- *
247
- * // 模式2:只签名,前端提交
248
- * const signResult = await approveTokenBatch({
249
- * chain: 'BSC',
250
- * platform: 'four',
251
- * rpcUrl: 'https://bsc-dataseed.binance.org',
252
- * privateKeys: [key1, key2],
253
- * tokenAddress: '0x...',
254
- * amounts: ['max', 'max'],
255
- * signOnly: true
256
- * });
257
- * // 提交到 BlockRazor
258
- * if (signResult.signedTransactions.length > 0) {
259
- * await submitBundleToBlockRazor(signResult.signedTransactions, { blockOffset: 10 });
260
- * }
261
- * ```
262
195
  */
263
- export declare function approveTokenBatch(params: ApproveTokenBatchParams & {
264
- signOnly: true;
265
- }): Promise<ApproveTokenBatchSignResult>;
266
- export declare function approveTokenBatch(params: ApproveTokenBatchParams & {
267
- signOnly?: false;
268
- }): Promise<ApproveTokenBatchResult>;
196
+ export declare function approveTokenBatch(params: ApproveTokenBatchParams): Promise<ApproveTokenBatchResult>;
269
197
  /**
270
198
  * ✅ 底层方法:批量授权 ERC20 代币(手动指定 spender)
271
199
  * 适用于任意 spender 地址(Flap、Four、PancakeSwap V2/V3 等)
272
200
  *
273
201
  * @param params - 批量授权参数
274
- * @param params.signOnly - 是否只签名不提交(默认 false)
275
- * - false: 直接发送交易并等待确认,返回 ApproveTokenBatchResult
276
- * - true: 只签名不提交,返回 ApproveTokenBatchSignResult,前端可通过 submitBundleToBlockRazor 提交
277
202
  * @returns 批量授权结果
278
- *
279
- * @example
280
- * ```typescript
281
- * // 模式1:直接发送交易(默认)
282
- * const result = await approveTokenBatchRaw({
283
- * rpcUrl: 'https://bsc-dataseed.binance.org',
284
- * privateKeys: [key1, key2],
285
- * tokenAddress: '0x...',
286
- * spenderAddress: '0x...',
287
- * amounts: ['max', 'max']
288
- * });
289
- *
290
- * // 模式2:只签名,前端提交
291
- * const signResult = await approveTokenBatchRaw({
292
- * rpcUrl: 'https://bsc-dataseed.binance.org',
293
- * privateKeys: [key1, key2],
294
- * tokenAddress: '0x...',
295
- * spenderAddress: '0x...',
296
- * amounts: ['max', 'max'],
297
- * signOnly: true
298
- * });
299
- * // 提交到 BlockRazor
300
- * if (signResult.signedTransactions.length > 0) {
301
- * await submitBundleToBlockRazor(signResult.signedTransactions, { blockOffset: 10 });
302
- * }
303
- * ```
304
203
  */
305
- export declare function approveTokenBatchRaw(params: ApproveTokenBatchRawParams & {
306
- signOnly: true;
307
- }): Promise<ApproveTokenBatchSignResult>;
308
- export declare function approveTokenBatchRaw(params: ApproveTokenBatchRawParams & {
309
- signOnly?: false;
310
- }): Promise<ApproveTokenBatchResult>;
204
+ export declare function approveTokenBatchRaw(params: ApproveTokenBatchRawParams): Promise<ApproveTokenBatchResult>;
@@ -1,4 +1,4 @@
1
- import { Contract, Wallet, JsonRpcProvider, Interface, parseUnits } from 'ethers';
1
+ import { Contract, Wallet, JsonRpcProvider } from 'ethers';
2
2
  import { ADDRESSES } from './constants.js';
3
3
  const ERC20_ABI = [
4
4
  { "constant": false, "inputs": [{ "name": "spender", "type": "address" }, { "name": "value", "type": "uint256" }], "name": "approve", "outputs": [{ "name": "", "type": "bool" }], "type": "function" },
@@ -482,13 +482,26 @@ export async function checkAllowanceBatchRaw(rpcUrl, tokenAddress, ownerAddresse
482
482
  await validateContractAddress(provider, normalizedSpender, 'Spender');
483
483
  return batchCheckAllowances(provider, normalizedToken, normalizedOwners, normalizedSpender);
484
484
  }
485
+ /**
486
+ * ✅ 智能路由:批量授权 ERC20 代币(自动选择 spender,自动检查,只在不足时才发送交易)
487
+ *
488
+ * @param params - 批量授权参数
489
+ * @returns 批量授权结果
490
+ */
485
491
  export async function approveTokenBatch(params) {
486
- const { chain, platform, rpcUrl, privateKeys, tokenAddress, amounts, signOnly, gasPriceGwei, gasLimit, chainId } = params;
492
+ const { chain, platform, rpcUrl, privateKeys, tokenAddress, amounts } = params;
487
493
  const spenderAddress = resolveSpenderAddress(chain, platform);
488
- return approveTokenBatchRaw({ rpcUrl, privateKeys, tokenAddress, spenderAddress, amounts, signOnly, gasPriceGwei, gasLimit, chainId });
494
+ return approveTokenBatchRaw({ rpcUrl, privateKeys, tokenAddress, spenderAddress, amounts });
489
495
  }
496
+ /**
497
+ * ✅ 底层方法:批量授权 ERC20 代币(手动指定 spender)
498
+ * 适用于任意 spender 地址(Flap、Four、PancakeSwap V2/V3 等)
499
+ *
500
+ * @param params - 批量授权参数
501
+ * @returns 批量授权结果
502
+ */
490
503
  export async function approveTokenBatchRaw(params) {
491
- const { rpcUrl, privateKeys, tokenAddress, spenderAddress, amounts, signOnly, gasPriceGwei, gasLimit, chainId = 56 } = params;
504
+ const { rpcUrl, privateKeys, tokenAddress, spenderAddress, amounts } = params;
492
505
  if (privateKeys.length === 0 || amounts.length !== privateKeys.length) {
493
506
  throw new Error('❌ 私钥数量和授权数量必须匹配');
494
507
  }
@@ -505,67 +518,6 @@ export async function approveTokenBatchRaw(params) {
505
518
  const requiredAmounts = amounts.map(amount => amount === 'max'
506
519
  ? BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
507
520
  : amount);
508
- // ==================== signOnly=true:只签名不提交 ====================
509
- if (signOnly) {
510
- // ✅ 并行获取:当前授权额度 + nonces + gasPrice
511
- const [currentAllowances, nonces, fetchedGasPrice] = await Promise.all([
512
- batchCheckAllowances(provider, normalizedToken, ownerAddresses, normalizedSpender),
513
- Promise.all(wallets.map(w => provider.getTransactionCount(w.address, 'latest'))),
514
- gasPriceGwei ? Promise.resolve(parseUnits(gasPriceGwei.toString(), 'gwei')) : provider.getFeeData().then(fee => fee.gasPrice || parseUnits('3', 'gwei'))
515
- ]);
516
- const finalGasPrice = fetchedGasPrice;
517
- const finalGasLimit = BigInt(gasLimit || 100000);
518
- // ✅ ERC20 approve 函数的 ABI 编码
519
- const erc20Interface = new Interface(ERC20_ABI);
520
- // ✅ 并行签名所有需要授权的交易
521
- const signPromises = wallets.map(async (wallet, i) => {
522
- const ownerAddress = ownerAddresses[i];
523
- const currentAllowance = currentAllowances[i];
524
- const requiredAmount = requiredAmounts[i];
525
- // 如果已经授权足够,跳过
526
- if (currentAllowance >= requiredAmount) {
527
- return {
528
- owner: ownerAddress,
529
- alreadyApproved: true,
530
- currentAllowance,
531
- requiredAllowance: requiredAmount,
532
- signedTx: undefined
533
- };
534
- }
535
- // 构建并签名交易
536
- const txData = erc20Interface.encodeFunctionData('approve', [normalizedSpender, requiredAmount]);
537
- const signedTx = await wallet.signTransaction({
538
- to: normalizedToken,
539
- data: txData,
540
- nonce: nonces[i],
541
- gasLimit: finalGasLimit,
542
- gasPrice: finalGasPrice,
543
- chainId,
544
- type: 0 // Legacy 交易
545
- });
546
- return {
547
- owner: ownerAddress,
548
- alreadyApproved: false,
549
- currentAllowance,
550
- requiredAllowance: requiredAmount,
551
- signedTx
552
- };
553
- });
554
- const results = await Promise.all(signPromises);
555
- // ✅ 提取所有签名交易(过滤掉已授权的)
556
- const signedTransactions = results
557
- .filter(r => !r.alreadyApproved && r.signedTx)
558
- .map(r => r.signedTx);
559
- const alreadyApprovedCount = results.filter(r => r.alreadyApproved).length;
560
- const needApproveCount = results.filter(r => !r.alreadyApproved).length;
561
- return {
562
- signedTransactions,
563
- results,
564
- needApproveCount,
565
- alreadyApprovedCount
566
- };
567
- }
568
- // ==================== signOnly=false(默认):直接发送交易 ====================
569
521
  // ✅ 优化:并行检查所有钱包的授权额度
570
522
  const currentAllowances = await batchCheckAllowances(provider, normalizedToken, ownerAddresses, normalizedSpender);
571
523
  // ✅ 优化:并行发送所有需要授权的交易
package/package.json CHANGED
@@ -1,10 +1,24 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.3.93",
4
- "description": "SDK for Flap bonding curve and four.meme TokenManager",
3
+ "version": "1.3.94",
4
+ "description": "SDK for Flap bonding curve, four.meme TokenManager, and Solana DEX",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./sol": {
14
+ "types": "./dist/sol/index.d.ts",
15
+ "import": "./dist/sol/index.js"
16
+ },
17
+ "./sol/*": {
18
+ "types": "./dist/sol/*.d.ts",
19
+ "import": "./dist/sol/*.js"
20
+ }
21
+ },
8
22
  "files": [
9
23
  "dist",
10
24
  "src/abis/*.json"
@@ -29,11 +43,32 @@
29
43
  "node": ">=18"
30
44
  },
31
45
  "dependencies": {
46
+ "@metaplex-foundation/mpl-token-metadata": "^3.4.0",
47
+ "@metaplex-foundation/umi": "^1.4.1",
48
+ "@metaplex-foundation/umi-bundle-defaults": "^1.4.1",
49
+ "@metaplex-foundation/umi-web3js-adapters": "^1.4.1",
50
+ "@meteora-ag/cp-amm-sdk": "^1.2.6",
51
+ "@meteora-ag/dlmm": "^1.9.0",
52
+ "@meteora-ag/dynamic-amm-sdk": "^1.4.1",
53
+ "@meteora-ag/dynamic-bonding-curve-sdk": "^1.4.9",
54
+ "@orca-so/common-sdk": "^0.7.0",
55
+ "@orca-so/whirlpools-sdk": "^0.17.0",
56
+ "@pump-fun/pump-sdk": "^1.23.0",
57
+ "@pump-fun/pump-swap-sdk": "^1.11.0",
58
+ "@raydium-io/raydium-sdk-v2": "^0.2.30-alpha",
59
+ "@solana/spl-token": "^0.4.14",
60
+ "@solana/web3.js": "^1.98.4",
32
61
  "axios": "^1.12.2",
33
62
  "ethers": "^6.11.0",
34
- "pinata": "^1.10.1"
63
+ "pinata": "^1.10.1",
64
+ "tweetnacl": "^1.0.3"
65
+ },
66
+ "optionalDependencies": {
67
+ "@orca-so/wavebreak": "^1.1.7"
35
68
  },
36
69
  "devDependencies": {
70
+ "@types/bn.js": "^5.2.0",
71
+ "@types/bs58": "^4.0.4",
37
72
  "typescript": "^5.6.3"
38
73
  }
39
- }
74
+ }
@@ -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
- }