four-flap-meme-sdk 1.3.89 → 1.3.90
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/clients/blockrazor.js +1 -0
- package/dist/contracts/tm-bundle-merkle/core.js +3 -6
- package/dist/contracts/tm-bundle-merkle/pancake-proxy.js +38 -30
- package/dist/contracts/tm-bundle-merkle/swap-buy-first.d.ts +1 -0
- package/dist/contracts/tm-bundle-merkle/swap-buy-first.js +3 -4
- package/dist/contracts/tm-bundle-merkle/swap.d.ts +3 -0
- package/dist/contracts/tm-bundle-merkle/swap.js +2 -2
- package/dist/flap/portal-bundle-merkle/core.js +6 -2
- package/dist/flap/portal-bundle-merkle/pancake-proxy.js +35 -55
- package/dist/flap/portal-bundle-merkle/swap-buy-first.d.ts +2 -0
- package/dist/flap/portal-bundle-merkle/swap-buy-first.js +11 -6
- package/dist/flap/portal-bundle-merkle/swap.d.ts +2 -0
- package/dist/flap/portal-bundle-merkle/swap.js +22 -10
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/pancake/bundle-buy-first.d.ts +1 -0
- package/dist/pancake/bundle-buy-first.js +9 -4
- package/dist/pancake/bundle-swap.d.ts +4 -0
- package/dist/pancake/bundle-swap.js +14 -7
- package/dist/sol/constants.d.ts +126 -0
- package/dist/sol/constants.js +145 -0
- package/dist/sol/dex/index.d.ts +8 -0
- package/dist/sol/dex/index.js +12 -0
- package/dist/sol/dex/meteora/client.d.ts +76 -0
- package/dist/sol/dex/meteora/client.js +219 -0
- package/dist/sol/dex/meteora/damm-v1-bundle.d.ts +61 -0
- package/dist/sol/dex/meteora/damm-v1-bundle.js +112 -0
- package/dist/sol/dex/meteora/damm-v1.d.ts +118 -0
- package/dist/sol/dex/meteora/damm-v1.js +315 -0
- package/dist/sol/dex/meteora/damm-v2-bundle.d.ts +82 -0
- package/dist/sol/dex/meteora/damm-v2-bundle.js +242 -0
- package/dist/sol/dex/meteora/damm-v2.d.ts +172 -0
- package/dist/sol/dex/meteora/damm-v2.js +632 -0
- package/dist/sol/dex/meteora/dbc-bundle.d.ts +123 -0
- package/dist/sol/dex/meteora/dbc-bundle.js +304 -0
- package/dist/sol/dex/meteora/dbc.d.ts +192 -0
- package/dist/sol/dex/meteora/dbc.js +619 -0
- package/dist/sol/dex/meteora/dlmm-bundle.d.ts +39 -0
- package/dist/sol/dex/meteora/dlmm-bundle.js +189 -0
- package/dist/sol/dex/meteora/dlmm.d.ts +146 -0
- package/dist/sol/dex/meteora/dlmm.js +593 -0
- package/dist/sol/dex/meteora/index.d.ts +25 -0
- package/dist/sol/dex/meteora/index.js +65 -0
- package/dist/sol/dex/meteora/types.d.ts +787 -0
- package/dist/sol/dex/meteora/types.js +110 -0
- package/dist/sol/dex/orca/index.d.ts +10 -0
- package/dist/sol/dex/orca/index.js +16 -0
- package/dist/sol/dex/orca/orca-bundle.d.ts +41 -0
- package/dist/sol/dex/orca/orca-bundle.js +173 -0
- package/dist/sol/dex/orca/orca.d.ts +65 -0
- package/dist/sol/dex/orca/orca.js +474 -0
- package/dist/sol/dex/orca/types.d.ts +263 -0
- package/dist/sol/dex/orca/types.js +38 -0
- package/dist/sol/dex/orca/wavebreak-bundle.d.ts +34 -0
- package/dist/sol/dex/orca/wavebreak-bundle.js +198 -0
- package/dist/sol/dex/orca/wavebreak-types.d.ts +227 -0
- package/dist/sol/dex/orca/wavebreak-types.js +23 -0
- package/dist/sol/dex/orca/wavebreak.d.ts +78 -0
- package/dist/sol/dex/orca/wavebreak.js +497 -0
- package/dist/sol/dex/pump/index.d.ts +9 -0
- package/dist/sol/dex/pump/index.js +14 -0
- package/dist/sol/dex/pump/pump-bundle.d.ts +92 -0
- package/dist/sol/dex/pump/pump-bundle.js +383 -0
- package/dist/sol/dex/pump/pump-swap-bundle.d.ts +103 -0
- package/dist/sol/dex/pump/pump-swap-bundle.js +380 -0
- package/dist/sol/dex/pump/pump-swap.d.ts +46 -0
- package/dist/sol/dex/pump/pump-swap.js +199 -0
- package/dist/sol/dex/pump/pump.d.ts +35 -0
- package/dist/sol/dex/pump/pump.js +352 -0
- package/dist/sol/dex/pump/types.d.ts +215 -0
- package/dist/sol/dex/pump/types.js +5 -0
- package/dist/sol/dex/raydium/index.d.ts +8 -0
- package/dist/sol/dex/raydium/index.js +12 -0
- package/dist/sol/dex/raydium/launchlab.d.ts +68 -0
- package/dist/sol/dex/raydium/launchlab.js +210 -0
- package/dist/sol/dex/raydium/raydium-bundle.d.ts +64 -0
- package/dist/sol/dex/raydium/raydium-bundle.js +324 -0
- package/dist/sol/dex/raydium/raydium.d.ts +40 -0
- package/dist/sol/dex/raydium/raydium.js +366 -0
- package/dist/sol/dex/raydium/types.d.ts +240 -0
- package/dist/sol/dex/raydium/types.js +5 -0
- package/dist/sol/index.d.ts +10 -0
- package/dist/sol/index.js +16 -0
- package/dist/sol/jito/bundle.d.ts +90 -0
- package/dist/sol/jito/bundle.js +263 -0
- package/dist/sol/jito/index.d.ts +7 -0
- package/dist/sol/jito/index.js +7 -0
- package/dist/sol/jito/tip.d.ts +51 -0
- package/dist/sol/jito/tip.js +83 -0
- package/dist/sol/jito/types.d.ts +100 -0
- package/dist/sol/jito/types.js +5 -0
- package/dist/sol/token/create-complete.d.ts +115 -0
- package/dist/sol/token/create-complete.js +235 -0
- package/dist/sol/token/create-token.d.ts +57 -0
- package/dist/sol/token/create-token.js +230 -0
- package/dist/sol/token/index.d.ts +9 -0
- package/dist/sol/token/index.js +14 -0
- package/dist/sol/token/metadata-upload.d.ts +86 -0
- package/dist/sol/token/metadata-upload.js +173 -0
- package/dist/sol/token/metadata.d.ts +92 -0
- package/dist/sol/token/metadata.js +274 -0
- package/dist/sol/token/types.d.ts +153 -0
- package/dist/sol/token/types.js +5 -0
- package/dist/sol/types.d.ts +176 -0
- package/dist/sol/types.js +7 -0
- package/dist/sol/utils/balance.d.ts +160 -0
- package/dist/sol/utils/balance.js +638 -0
- package/dist/sol/utils/connection.d.ts +78 -0
- package/dist/sol/utils/connection.js +168 -0
- package/dist/sol/utils/index.d.ts +9 -0
- package/dist/sol/utils/index.js +9 -0
- package/dist/sol/utils/lp-inspect.d.ts +129 -0
- package/dist/sol/utils/lp-inspect.js +521 -0
- package/dist/sol/utils/transfer.d.ts +125 -0
- package/dist/sol/utils/transfer.js +220 -0
- package/dist/sol/utils/wallet.d.ts +107 -0
- package/dist/sol/utils/wallet.js +210 -0
- package/dist/utils/erc20.d.ts +2 -108
- package/dist/utils/erc20.js +17 -65
- package/package.json +39 -4
- package/dist/flap/portal-bundle-merkle/encryption.d.ts +0 -16
- package/dist/flap/portal-bundle-merkle/encryption.js +0 -146
package/dist/utils/erc20.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Contract, Wallet, JsonRpcProvider
|
|
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
|
|
492
|
+
const { chain, platform, rpcUrl, privateKeys, tokenAddress, amounts } = params;
|
|
487
493
|
const spenderAddress = resolveSpenderAddress(chain, platform);
|
|
488
|
-
return approveTokenBatchRaw({ rpcUrl, privateKeys, tokenAddress, spenderAddress, amounts
|
|
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
|
|
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.
|
|
4
|
-
"description": "SDK for Flap bonding curve
|
|
3
|
+
"version": "1.3.90",
|
|
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
|
-
}
|