four-flap-meme-sdk 1.5.21 → 1.5.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/contracts/tm-bundle-merkle/index.d.ts +1 -1
- package/dist/contracts/tm-bundle-merkle/index.js +1 -1
- package/dist/contracts/tm-bundle-merkle/types.d.ts +24 -0
- package/dist/contracts/tm-bundle-merkle/utils.d.ts +8 -1
- package/dist/contracts/tm-bundle-merkle/utils.js +287 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/xlayer/aa-account.d.ts +32 -1
- package/dist/xlayer/aa-account.js +184 -46
- package/dist/xlayer/bundle.js +149 -53
- package/dist/xlayer/bundler.js +30 -11
- package/dist/xlayer/portal-ops.d.ts +1 -0
- package/dist/xlayer/portal-ops.js +125 -8
- package/dist/xlayer/types.d.ts +20 -0
- 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
package/dist/xlayer/bundler.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* 与 Particle Bundler 交互,提供 ERC-4337 相关 RPC 方法
|
|
5
5
|
*/
|
|
6
6
|
import { PARTICLE_BUNDLER_URL, XLAYER_CHAIN_ID, ENTRYPOINT_V06, } from './constants.js';
|
|
7
|
+
import { mapWithConcurrency } from '../utils/concurrency.js';
|
|
7
8
|
// ============================================================================
|
|
8
9
|
// Bundler 客户端类
|
|
9
10
|
// ============================================================================
|
|
@@ -151,17 +152,35 @@ export class BundlerClient {
|
|
|
151
152
|
async estimateUserOperationGasBatch(userOps) {
|
|
152
153
|
if (userOps.length === 0)
|
|
153
154
|
return [];
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
155
|
+
const maxBatchSize = 30;
|
|
156
|
+
const maxSingleConcurrency = 4;
|
|
157
|
+
const estimateChunk = async (chunk, batchSize) => {
|
|
158
|
+
if (chunk.length === 0)
|
|
159
|
+
return [];
|
|
160
|
+
if (batchSize <= 1 || chunk.length === 1) {
|
|
161
|
+
// 最终兜底:受控并发的单请求(避免 Promise.all 突发)
|
|
162
|
+
return await mapWithConcurrency(chunk, maxSingleConcurrency, async (op) => await this.estimateUserOperationGas(op));
|
|
163
|
+
}
|
|
164
|
+
const out = [];
|
|
165
|
+
for (let cursor = 0; cursor < chunk.length; cursor += batchSize) {
|
|
166
|
+
const slice = chunk.slice(cursor, cursor + batchSize);
|
|
167
|
+
try {
|
|
168
|
+
const res = await this.rpcBatch(slice.map((op) => ({
|
|
169
|
+
method: 'eth_estimateUserOperationGas',
|
|
170
|
+
params: [op, this.entryPoint],
|
|
171
|
+
})));
|
|
172
|
+
out.push(...res);
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
// 降级:拆分为更小 batch(直到 1)
|
|
176
|
+
const next = Math.max(1, Math.floor(batchSize / 2));
|
|
177
|
+
const res = await estimateChunk(slice, next);
|
|
178
|
+
out.push(...res);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
};
|
|
183
|
+
return await estimateChunk(userOps, Math.min(maxBatchSize, userOps.length));
|
|
165
184
|
}
|
|
166
185
|
/**
|
|
167
186
|
* 发送 UserOperation
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
* - 代币转账
|
|
9
9
|
*/
|
|
10
10
|
import { ethers, Interface, Contract, JsonRpcProvider } from 'ethers';
|
|
11
|
-
import { FLAP_PORTAL, ZERO_ADDRESS, PORTAL_ABI, ERC20_ABI, DEFAULT_RPC_URL, XLAYER_CHAIN_ID, } from './constants.js';
|
|
11
|
+
import { FLAP_PORTAL, ZERO_ADDRESS, PORTAL_ABI, ERC20_ABI, MULTICALL3, DEFAULT_RPC_URL, XLAYER_CHAIN_ID, } from './constants.js';
|
|
12
|
+
import { mapWithConcurrency } from '../utils/concurrency.js';
|
|
12
13
|
// ============================================================================
|
|
13
14
|
// Portal 操作编码器
|
|
14
15
|
// ============================================================================
|
|
@@ -81,10 +82,22 @@ export class PortalQuery {
|
|
|
81
82
|
constructor(config = {}) {
|
|
82
83
|
const rpcUrl = config.rpcUrl ?? DEFAULT_RPC_URL;
|
|
83
84
|
const chainId = config.chainId ?? XLAYER_CHAIN_ID;
|
|
84
|
-
this.provider = new JsonRpcProvider(rpcUrl, { chainId, name: 'xlayer' }
|
|
85
|
+
this.provider = new JsonRpcProvider(rpcUrl, { chainId, name: 'xlayer' }, {
|
|
86
|
+
batchMaxCount: 20,
|
|
87
|
+
batchStallTime: 30,
|
|
88
|
+
});
|
|
85
89
|
this.portalAddress = config.portalAddress ?? FLAP_PORTAL;
|
|
86
90
|
this.portal = new Contract(this.portalAddress, PORTAL_ABI, this.provider);
|
|
87
91
|
}
|
|
92
|
+
async multicallAggregate3(params) {
|
|
93
|
+
const mcIface = new Interface([
|
|
94
|
+
'function aggregate3((address target,bool allowFailure,bytes callData)[] calls) view returns ((bool success,bytes returnData)[] returnData)',
|
|
95
|
+
]);
|
|
96
|
+
const data = mcIface.encodeFunctionData('aggregate3', [params.calls]);
|
|
97
|
+
const raw = await this.provider.call({ to: MULTICALL3, data });
|
|
98
|
+
const decoded = mcIface.decodeFunctionResult('aggregate3', raw)?.[0];
|
|
99
|
+
return decoded || [];
|
|
100
|
+
}
|
|
88
101
|
/**
|
|
89
102
|
* 获取 Provider
|
|
90
103
|
*/
|
|
@@ -171,8 +184,42 @@ export class PortalQuery {
|
|
|
171
184
|
*/
|
|
172
185
|
async getMultipleTokenBalances(tokenAddress, accounts) {
|
|
173
186
|
const balances = new Map();
|
|
174
|
-
const
|
|
175
|
-
|
|
187
|
+
const list = accounts.map((a) => String(a || '').trim()).filter(Boolean);
|
|
188
|
+
if (list.length === 0)
|
|
189
|
+
return balances;
|
|
190
|
+
const calls = list.map((acc) => ({
|
|
191
|
+
target: tokenAddress,
|
|
192
|
+
allowFailure: true,
|
|
193
|
+
callData: erc20Iface.encodeFunctionData('balanceOf', [acc]),
|
|
194
|
+
}));
|
|
195
|
+
const BATCH = 350;
|
|
196
|
+
try {
|
|
197
|
+
for (let cursor = 0; cursor < calls.length; cursor += BATCH) {
|
|
198
|
+
const sliceCalls = calls.slice(cursor, cursor + BATCH);
|
|
199
|
+
const res = await this.multicallAggregate3({ calls: sliceCalls });
|
|
200
|
+
for (let i = 0; i < res.length; i++) {
|
|
201
|
+
const r = res[i];
|
|
202
|
+
const idx = cursor + i;
|
|
203
|
+
const acc = list[idx];
|
|
204
|
+
if (!r?.success || !r.returnData || r.returnData === '0x') {
|
|
205
|
+
balances.set(acc, 0n);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
const decoded = erc20Iface.decodeFunctionResult('balanceOf', r.returnData);
|
|
210
|
+
balances.set(acc, BigInt(decoded?.[0] ?? 0n));
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
balances.set(acc, 0n);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return balances;
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
const results = await mapWithConcurrency(list, 8, async (acc) => this.getTokenBalance(tokenAddress, acc));
|
|
221
|
+
list.forEach((acc, i) => balances.set(acc, results[i] ?? 0n));
|
|
222
|
+
}
|
|
176
223
|
return balances;
|
|
177
224
|
}
|
|
178
225
|
/**
|
|
@@ -180,8 +227,43 @@ export class PortalQuery {
|
|
|
180
227
|
*/
|
|
181
228
|
async getMultipleOkbBalances(accounts) {
|
|
182
229
|
const balances = new Map();
|
|
183
|
-
const
|
|
184
|
-
|
|
230
|
+
const list = accounts.map((a) => String(a || '').trim()).filter(Boolean);
|
|
231
|
+
if (list.length === 0)
|
|
232
|
+
return balances;
|
|
233
|
+
const ethBalIface = new Interface(['function getEthBalance(address addr) view returns (uint256)']);
|
|
234
|
+
const calls = list.map((acc) => ({
|
|
235
|
+
target: MULTICALL3,
|
|
236
|
+
allowFailure: true,
|
|
237
|
+
callData: ethBalIface.encodeFunctionData('getEthBalance', [acc]),
|
|
238
|
+
}));
|
|
239
|
+
const BATCH = 350;
|
|
240
|
+
try {
|
|
241
|
+
for (let cursor = 0; cursor < calls.length; cursor += BATCH) {
|
|
242
|
+
const sliceCalls = calls.slice(cursor, cursor + BATCH);
|
|
243
|
+
const res = await this.multicallAggregate3({ calls: sliceCalls });
|
|
244
|
+
for (let i = 0; i < res.length; i++) {
|
|
245
|
+
const r = res[i];
|
|
246
|
+
const idx = cursor + i;
|
|
247
|
+
const acc = list[idx];
|
|
248
|
+
if (!r?.success || !r.returnData || r.returnData === '0x') {
|
|
249
|
+
balances.set(acc, 0n);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
const decoded = ethBalIface.decodeFunctionResult('getEthBalance', r.returnData);
|
|
254
|
+
balances.set(acc, BigInt(decoded?.[0] ?? 0n));
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
balances.set(acc, 0n);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return balances;
|
|
262
|
+
}
|
|
263
|
+
catch {
|
|
264
|
+
const results = await mapWithConcurrency(list, 8, async (acc) => this.getOkbBalance(acc));
|
|
265
|
+
list.forEach((acc, i) => balances.set(acc, results[i] ?? 0n));
|
|
266
|
+
}
|
|
185
267
|
return balances;
|
|
186
268
|
}
|
|
187
269
|
/**
|
|
@@ -189,8 +271,43 @@ export class PortalQuery {
|
|
|
189
271
|
*/
|
|
190
272
|
async getMultipleAllowances(tokenAddress, owners, spender) {
|
|
191
273
|
const allowances = new Map();
|
|
192
|
-
const
|
|
193
|
-
|
|
274
|
+
const list = owners.map((a) => String(a || '').trim()).filter(Boolean);
|
|
275
|
+
if (list.length === 0)
|
|
276
|
+
return allowances;
|
|
277
|
+
const useSpender = spender ?? this.portalAddress;
|
|
278
|
+
const calls = list.map((owner) => ({
|
|
279
|
+
target: tokenAddress,
|
|
280
|
+
allowFailure: true,
|
|
281
|
+
callData: erc20Iface.encodeFunctionData('allowance', [owner, useSpender]),
|
|
282
|
+
}));
|
|
283
|
+
const BATCH = 350;
|
|
284
|
+
try {
|
|
285
|
+
for (let cursor = 0; cursor < calls.length; cursor += BATCH) {
|
|
286
|
+
const sliceCalls = calls.slice(cursor, cursor + BATCH);
|
|
287
|
+
const res = await this.multicallAggregate3({ calls: sliceCalls });
|
|
288
|
+
for (let i = 0; i < res.length; i++) {
|
|
289
|
+
const r = res[i];
|
|
290
|
+
const idx = cursor + i;
|
|
291
|
+
const owner = list[idx];
|
|
292
|
+
if (!r?.success || !r.returnData || r.returnData === '0x') {
|
|
293
|
+
allowances.set(owner, 0n);
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
try {
|
|
297
|
+
const decoded = erc20Iface.decodeFunctionResult('allowance', r.returnData);
|
|
298
|
+
allowances.set(owner, BigInt(decoded?.[0] ?? 0n));
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
allowances.set(owner, 0n);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return allowances;
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
const results = await mapWithConcurrency(list, 8, async (owner) => this.getAllowance(tokenAddress, owner, useSpender));
|
|
309
|
+
list.forEach((owner, i) => allowances.set(owner, results[i] ?? 0n));
|
|
310
|
+
}
|
|
194
311
|
return allowances;
|
|
195
312
|
}
|
|
196
313
|
}
|
package/dist/xlayer/types.d.ts
CHANGED
|
@@ -41,6 +41,17 @@ export interface GasEstimate {
|
|
|
41
41
|
maxFeePerGas?: HexString;
|
|
42
42
|
maxPriorityFeePerGas?: HexString;
|
|
43
43
|
}
|
|
44
|
+
export type GasPolicy = 'fixed' | 'localEstimate' | 'bundlerEstimate';
|
|
45
|
+
export interface FixedGasConfig {
|
|
46
|
+
/** callGasLimit(若不提供,SDK 会使用较保守的默认值) */
|
|
47
|
+
callGasLimit?: bigint;
|
|
48
|
+
/** 已部署 sender 的 verificationGasLimit(默认用 SDK 常量) */
|
|
49
|
+
verificationGasLimitDeployed?: bigint;
|
|
50
|
+
/** 未部署 sender 的 verificationGasLimit(默认用 SDK 常量) */
|
|
51
|
+
verificationGasLimitUndeployed?: bigint;
|
|
52
|
+
/** preVerificationGas(默认用 SDK 常量) */
|
|
53
|
+
preVerificationGas?: bigint;
|
|
54
|
+
}
|
|
44
55
|
/**
|
|
45
56
|
* XLayer SDK 基础配置
|
|
46
57
|
*/
|
|
@@ -65,6 +76,15 @@ export interface XLayerConfig {
|
|
|
65
76
|
timeoutMs?: number;
|
|
66
77
|
/** Gas 估算安全余量倍数 */
|
|
67
78
|
gasLimitMultiplier?: number;
|
|
79
|
+
/**
|
|
80
|
+
* AA Gas 策略(用于大规模地址时减少 RPC)
|
|
81
|
+
* - fixed:固定 gas(不 estimate)
|
|
82
|
+
* - localEstimate:eth_estimateGas(不走 bundler)
|
|
83
|
+
* - bundlerEstimate:eth_estimateUserOperationGas(最慢但最稳)
|
|
84
|
+
*/
|
|
85
|
+
gasPolicy?: GasPolicy;
|
|
86
|
+
/** fixed 策略的默认 gas 配置(可被每次调用覆盖) */
|
|
87
|
+
fixedGas?: FixedGasConfig;
|
|
68
88
|
}
|
|
69
89
|
/**
|
|
70
90
|
* AA 账户信息
|
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
|
-
}
|