four-flap-meme-sdk 1.2.34 → 1.2.36

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.
@@ -222,9 +222,9 @@ export async function batchBuyWithBundleMerkleEncrypted(params) {
222
222
  config
223
223
  });
224
224
  console.log('✅ 交易签名完成,准备加密...');
225
- // 2. 用服务器公钥加密签名交易
226
- const encryptedPayload = encryptWithPublicKey(signResult.signedTransactions, publicKey);
227
- console.log('✅ 已用服务器公钥加密,前端无法解密');
225
+ // 2. 用服务器公钥加密签名交易(ECDH + AES-GCM)
226
+ const encryptedPayload = await encryptWithPublicKey(signResult.signedTransactions, publicKey);
227
+ console.log('✅ 已用服务器公钥加密(ECDH + AES-GCM),前端无法解密');
228
228
  // 3. 返回加密结果
229
229
  return {
230
230
  sessionId,
@@ -1,16 +1,16 @@
1
1
  /**
2
- * RSA 加密工具
2
+ * ECDH + AES-GCM 加密工具(浏览器兼容)
3
3
  * 用于将签名交易用服务器公钥加密
4
4
  */
5
5
  /**
6
- * 用服务器公钥加密签名交易
6
+ * 用服务器公钥加密签名交易(ECDH + AES-GCM)
7
7
  *
8
8
  * @param signedTransactions 签名后的交易数组
9
- * @param publicKeyPem 服务器提供的公钥(PEM格式)
10
- * @returns Base64 编码的密文
9
+ * @param publicKeyBase64 服务器提供的公钥(Base64 格式)
10
+ * @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
11
11
  */
12
- export declare function encryptWithPublicKey(signedTransactions: string[], publicKeyPem: string): string;
12
+ export declare function encryptWithPublicKey(signedTransactions: string[], publicKeyBase64: string): Promise<string>;
13
13
  /**
14
- * 解析公钥(验证格式)
14
+ * 验证公钥格式(Base64)
15
15
  */
16
- export declare function validatePublicKey(publicKeyPem: string): boolean;
16
+ export declare function validatePublicKey(publicKeyBase64: string): boolean;
@@ -1,47 +1,145 @@
1
1
  /**
2
- * RSA 加密工具
2
+ * ECDH + AES-GCM 加密工具(浏览器兼容)
3
3
  * 用于将签名交易用服务器公钥加密
4
4
  */
5
- import { publicEncrypt, constants, randomBytes } from 'crypto';
6
5
  /**
7
- * 用服务器公钥加密签名交易
6
+ * 获取 crypto 对象(兼容浏览器和 Node.js)
7
+ */
8
+ function getCrypto() {
9
+ if (typeof globalThis !== 'undefined' && globalThis.crypto) {
10
+ return globalThis.crypto;
11
+ }
12
+ if (typeof window !== 'undefined' && window.crypto) {
13
+ return window.crypto;
14
+ }
15
+ if (typeof self !== 'undefined' && self.crypto) {
16
+ return self.crypto;
17
+ }
18
+ // Node.js 环境
19
+ if (typeof require !== 'undefined') {
20
+ try {
21
+ const { webcrypto } = require('crypto');
22
+ return webcrypto;
23
+ }
24
+ catch {
25
+ // ignore
26
+ }
27
+ }
28
+ throw new Error('当前环境不支持 Web Crypto API');
29
+ }
30
+ /**
31
+ * Base64 转 ArrayBuffer
32
+ */
33
+ function base64ToArrayBuffer(base64) {
34
+ if (typeof Buffer !== 'undefined') {
35
+ // Node.js 环境
36
+ return Buffer.from(base64, 'base64').buffer;
37
+ }
38
+ else {
39
+ // 浏览器环境
40
+ const binaryString = atob(base64);
41
+ const bytes = new Uint8Array(binaryString.length);
42
+ for (let i = 0; i < binaryString.length; i++) {
43
+ bytes[i] = binaryString.charCodeAt(i);
44
+ }
45
+ return bytes.buffer;
46
+ }
47
+ }
48
+ /**
49
+ * ArrayBuffer 转 Base64
50
+ */
51
+ function arrayBufferToBase64(buffer) {
52
+ if (typeof Buffer !== 'undefined') {
53
+ // Node.js 环境
54
+ return Buffer.from(buffer).toString('base64');
55
+ }
56
+ else {
57
+ // 浏览器环境
58
+ const bytes = new Uint8Array(buffer);
59
+ let binary = '';
60
+ for (let i = 0; i < bytes.length; i++) {
61
+ binary += String.fromCharCode(bytes[i]);
62
+ }
63
+ return btoa(binary);
64
+ }
65
+ }
66
+ /**
67
+ * 生成随机 Hex 字符串
68
+ */
69
+ function randomHex(length) {
70
+ const array = new Uint8Array(length);
71
+ try {
72
+ const crypto = getCrypto();
73
+ crypto.getRandomValues(array);
74
+ }
75
+ catch {
76
+ // Fallback to Node.js crypto
77
+ if (typeof require !== 'undefined') {
78
+ const { randomBytes } = require('crypto');
79
+ const buffer = randomBytes(length);
80
+ for (let i = 0; i < length; i++) {
81
+ array[i] = buffer[i];
82
+ }
83
+ }
84
+ }
85
+ return Array.from(array)
86
+ .map(b => b.toString(16).padStart(2, '0'))
87
+ .join('');
88
+ }
89
+ /**
90
+ * 用服务器公钥加密签名交易(ECDH + AES-GCM)
8
91
  *
9
92
  * @param signedTransactions 签名后的交易数组
10
- * @param publicKeyPem 服务器提供的公钥(PEM格式)
11
- * @returns Base64 编码的密文
93
+ * @param publicKeyBase64 服务器提供的公钥(Base64 格式)
94
+ * @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
12
95
  */
13
- export function encryptWithPublicKey(signedTransactions, publicKeyPem) {
14
- // 1. 准备数据(包含签名交易和元数据)
15
- const payload = {
16
- signedTransactions,
17
- timestamp: Date.now(),
18
- nonce: randomBytes(8).toString('hex')
19
- };
20
- const plaintext = JSON.stringify(payload);
21
- // 2. 用服务器公钥加密(RSA-OAEP)
96
+ export async function encryptWithPublicKey(signedTransactions, publicKeyBase64) {
22
97
  try {
23
- const encrypted = publicEncrypt({
24
- key: publicKeyPem,
25
- padding: constants.RSA_PKCS1_OAEP_PADDING,
26
- oaepHash: 'sha256'
27
- }, Buffer.from(plaintext, 'utf-8'));
28
- // 3. 返回 Base64 编码的密文
29
- return encrypted.toString('base64');
98
+ // 0. 获取 crypto 对象
99
+ const crypto = getCrypto();
100
+ // 1. 准备数据
101
+ const payload = {
102
+ signedTransactions,
103
+ timestamp: Date.now(),
104
+ nonce: randomHex(8)
105
+ };
106
+ const plaintext = JSON.stringify(payload);
107
+ // 2. 生成临时 ECDH 密钥对
108
+ const ephemeralKeyPair = await crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveKey']);
109
+ // 3. 导入服务器公钥
110
+ const publicKeyBuffer = base64ToArrayBuffer(publicKeyBase64);
111
+ const publicKey = await crypto.subtle.importKey('raw', publicKeyBuffer, { name: 'ECDH', namedCurve: 'P-256' }, false, []);
112
+ // 4. 派生共享密钥(AES-256)
113
+ const sharedKey = await crypto.subtle.deriveKey({ name: 'ECDH', public: publicKey }, ephemeralKeyPair.privateKey, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
114
+ // 5. AES-GCM 加密
115
+ const iv = crypto.getRandomValues(new Uint8Array(12));
116
+ const encrypted = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, sharedKey, new TextEncoder().encode(plaintext));
117
+ // 6. 导出临时公钥
118
+ const ephemeralPublicKeyRaw = await crypto.subtle.exportKey('raw', ephemeralKeyPair.publicKey);
119
+ // 7. 返回加密包(JSON 格式)
120
+ return JSON.stringify({
121
+ e: arrayBufferToBase64(ephemeralPublicKeyRaw), // 临时公钥
122
+ i: arrayBufferToBase64(iv.buffer), // IV
123
+ d: arrayBufferToBase64(encrypted) // 密文
124
+ });
30
125
  }
31
126
  catch (error) {
32
127
  throw new Error(`加密失败: ${error?.message || String(error)}`);
33
128
  }
34
129
  }
35
130
  /**
36
- * 解析公钥(验证格式)
131
+ * 验证公钥格式(Base64)
37
132
  */
38
- export function validatePublicKey(publicKeyPem) {
133
+ export function validatePublicKey(publicKeyBase64) {
39
134
  try {
40
- if (!publicKeyPem)
135
+ if (!publicKeyBase64)
41
136
  return false;
42
- if (!publicKeyPem.includes('-----BEGIN PUBLIC KEY-----'))
137
+ // Base64 字符集验证
138
+ if (!/^[A-Za-z0-9+/=]+$/.test(publicKeyBase64))
43
139
  return false;
44
- if (!publicKeyPem.includes('-----END PUBLIC KEY-----'))
140
+ // ECDH P-256 公钥固定长度 65 字节(未压缩)
141
+ // Base64 编码后约 88 字符
142
+ if (publicKeyBase64.length < 80 || publicKeyBase64.length > 100)
45
143
  return false;
46
144
  return true;
47
145
  }
@@ -280,7 +280,7 @@ export type PancakeProxyApprovalBatchResult = {
280
280
  };
281
281
  /**
282
282
  * 批量买入参数(加密版)
283
- * 使用服务器提供的公钥加密签名交易
283
+ * 使用服务器提供的公钥加密签名交易(ECDH + AES-GCM)
284
284
  */
285
285
  export type FlapBatchBuyEncryptedParams = {
286
286
  sessionId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.2.34",
3
+ "version": "1.2.36",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",