four-flap-meme-sdk 1.5.5 → 1.5.6
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/abis/common.js +0 -2
- package/dist/contracts/tm-bundle-merkle/config.d.ts +1 -0
- package/dist/contracts/tm-bundle-merkle/config.js +1 -0
- package/dist/contracts/tm-bundle-merkle/core.js +9 -13
- package/dist/flap/portal-bundle-merkle/config.d.ts +1 -0
- package/dist/flap/portal-bundle-merkle/config.js +1 -0
- package/dist/flap/portal-bundle-merkle/create-to-dex.d.ts +2 -8
- package/dist/flap/portal-bundle-merkle/create-to-dex.js +126 -159
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/sol/constants.d.ts +150 -0
- package/dist/sol/constants.js +188 -0
- package/dist/sol/dex/blockrazor/client.d.ts +51 -0
- package/dist/sol/dex/blockrazor/client.js +96 -0
- package/dist/sol/dex/blockrazor/constants.d.ts +34 -0
- package/dist/sol/dex/blockrazor/constants.js +55 -0
- package/dist/sol/dex/blockrazor/geyser.d.ts +128 -0
- package/dist/sol/dex/blockrazor/geyser.js +530 -0
- package/dist/sol/dex/blockrazor/index.d.ts +18 -0
- package/dist/sol/dex/blockrazor/index.js +23 -0
- package/dist/sol/dex/blockrazor/send.d.ts +135 -0
- package/dist/sol/dex/blockrazor/send.js +254 -0
- package/dist/sol/dex/blockrazor/types.d.ts +191 -0
- package/dist/sol/dex/blockrazor/types.js +5 -0
- package/dist/sol/dex/index.d.ts +10 -0
- package/dist/sol/dex/index.js +16 -0
- package/dist/sol/dex/jup/client.d.ts +33 -0
- package/dist/sol/dex/jup/client.js +110 -0
- package/dist/sol/dex/jup/index.d.ts +16 -0
- package/dist/sol/dex/jup/index.js +148 -0
- package/dist/sol/dex/jup/legacy.d.ts +623 -0
- package/dist/sol/dex/jup/legacy.js +416 -0
- package/dist/sol/dex/jup/lend.d.ts +640 -0
- package/dist/sol/dex/jup/lend.js +603 -0
- package/dist/sol/dex/jup/portfolio.d.ts +362 -0
- package/dist/sol/dex/jup/portfolio.js +367 -0
- package/dist/sol/dex/jup/price.d.ts +173 -0
- package/dist/sol/dex/jup/price.js +220 -0
- package/dist/sol/dex/jup/recurring.d.ts +437 -0
- package/dist/sol/dex/jup/recurring.js +320 -0
- package/dist/sol/dex/jup/send.d.ts +282 -0
- package/dist/sol/dex/jup/send.js +295 -0
- package/dist/sol/dex/jup/studio.d.ts +457 -0
- package/dist/sol/dex/jup/studio.js +488 -0
- package/dist/sol/dex/jup/tokens.d.ts +767 -0
- package/dist/sol/dex/jup/tokens.js +697 -0
- package/dist/sol/dex/jup/trigger.d.ts +511 -0
- package/dist/sol/dex/jup/trigger.js +397 -0
- package/dist/sol/dex/jup/types.d.ts +433 -0
- package/dist/sol/dex/jup/types.js +5 -0
- package/dist/sol/dex/jup/ultra.d.ts +646 -0
- package/dist/sol/dex/jup/ultra.js +853 -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 +157 -0
- package/dist/sol/dex/meteora/dlmm.js +671 -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 +11 -0
- package/dist/sol/index.js +18 -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/nozomi/client.d.ts +63 -0
- package/dist/sol/nozomi/client.js +222 -0
- package/dist/sol/nozomi/index.d.ts +8 -0
- package/dist/sol/nozomi/index.js +8 -0
- package/dist/sol/nozomi/tip.d.ts +50 -0
- package/dist/sol/nozomi/tip.js +80 -0
- package/dist/sol/nozomi/types.d.ts +96 -0
- package/dist/sol/nozomi/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 +900 -0
- package/dist/sol/utils/transfer.d.ts +196 -0
- package/dist/sol/utils/transfer.js +307 -0
- package/dist/sol/utils/wallet.d.ts +107 -0
- package/dist/sol/utils/wallet.js +210 -0
- package/package.json +41 -5
- package/README.zh-CN.pdf +0 -0
- package/dist/flap/portal-bundle-merkle/encryption.d.ts +0 -16
- package/dist/flap/portal-bundle-merkle/encryption.js +0 -146
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solana 钱包工具
|
|
3
|
+
* @module sol/utils/wallet
|
|
4
|
+
*/
|
|
5
|
+
import { Keypair, PublicKey } from '@solana/web3.js';
|
|
6
|
+
import bs58 from 'bs58';
|
|
7
|
+
/**
|
|
8
|
+
* 生成新的 Solana 钱包
|
|
9
|
+
* @returns 钱包信息
|
|
10
|
+
*/
|
|
11
|
+
export function generateKeypair() {
|
|
12
|
+
const keypair = Keypair.generate();
|
|
13
|
+
return {
|
|
14
|
+
publicKey: keypair.publicKey.toBase58(),
|
|
15
|
+
secretKey: keypair.secretKey,
|
|
16
|
+
secretKeyBase58: bs58.encode(keypair.secretKey),
|
|
17
|
+
address: keypair.publicKey.toBase58(),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 批量生成钱包
|
|
22
|
+
* @param count 数量
|
|
23
|
+
* @returns 钱包列表
|
|
24
|
+
*/
|
|
25
|
+
export function generateKeypairs(count) {
|
|
26
|
+
const wallets = [];
|
|
27
|
+
for (let i = 0; i < count; i++) {
|
|
28
|
+
wallets.push(generateKeypair());
|
|
29
|
+
}
|
|
30
|
+
return wallets;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 批量生成钱包(增强版,返回更多信息)
|
|
34
|
+
* @param count 数量
|
|
35
|
+
* @returns 详细结果
|
|
36
|
+
*/
|
|
37
|
+
export function batchGenerateWallets(count) {
|
|
38
|
+
const wallets = [];
|
|
39
|
+
const addresses = [];
|
|
40
|
+
const secretKeys = [];
|
|
41
|
+
const keypairs = [];
|
|
42
|
+
for (let i = 0; i < count; i++) {
|
|
43
|
+
const keypair = Keypair.generate();
|
|
44
|
+
const address = keypair.publicKey.toBase58();
|
|
45
|
+
const secretKeyBase58 = bs58.encode(keypair.secretKey);
|
|
46
|
+
wallets.push({
|
|
47
|
+
publicKey: address,
|
|
48
|
+
secretKey: keypair.secretKey,
|
|
49
|
+
secretKeyBase58,
|
|
50
|
+
address,
|
|
51
|
+
});
|
|
52
|
+
addresses.push(address);
|
|
53
|
+
secretKeys.push(secretKeyBase58);
|
|
54
|
+
keypairs.push(keypair);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
wallets,
|
|
58
|
+
count,
|
|
59
|
+
addresses,
|
|
60
|
+
secretKeys,
|
|
61
|
+
keypairs,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 从私钥列表批量导入钱包
|
|
66
|
+
* @param secretKeys 私钥列表(base58 字符串)
|
|
67
|
+
* @returns 导入结果
|
|
68
|
+
*/
|
|
69
|
+
export function batchImportWallets(secretKeys) {
|
|
70
|
+
const keypairs = [];
|
|
71
|
+
const addresses = [];
|
|
72
|
+
const failed = [];
|
|
73
|
+
for (let i = 0; i < secretKeys.length; i++) {
|
|
74
|
+
try {
|
|
75
|
+
const keypair = keypairFromSecretKey(secretKeys[i]);
|
|
76
|
+
keypairs.push(keypair);
|
|
77
|
+
addresses.push(keypair.publicKey.toBase58());
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
failed.push({
|
|
81
|
+
index: i,
|
|
82
|
+
secretKey: secretKeys[i].slice(0, 8) + '...', // 只显示前8位
|
|
83
|
+
error: error.message,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
keypairs,
|
|
89
|
+
addresses,
|
|
90
|
+
failed,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 从私钥导入钱包
|
|
95
|
+
* @param secretKey 私钥(Uint8Array 或 base58 字符串)
|
|
96
|
+
* @returns Keypair
|
|
97
|
+
*/
|
|
98
|
+
export function keypairFromSecretKey(secretKey) {
|
|
99
|
+
try {
|
|
100
|
+
let keyArray;
|
|
101
|
+
if (typeof secretKey === 'string') {
|
|
102
|
+
// base58 格式
|
|
103
|
+
keyArray = bs58.decode(secretKey);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
keyArray = secretKey;
|
|
107
|
+
}
|
|
108
|
+
// 验证私钥长度
|
|
109
|
+
if (keyArray.length !== 64) {
|
|
110
|
+
throw new Error(`Invalid secret key length: ${keyArray.length}, expected 64`);
|
|
111
|
+
}
|
|
112
|
+
return Keypair.fromSecretKey(keyArray);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
throw new Error(`Failed to import keypair: ${error.message}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 导出私钥为 base58 格式
|
|
120
|
+
* @param keypair Keypair
|
|
121
|
+
* @returns base58 编码的私钥
|
|
122
|
+
*/
|
|
123
|
+
export function exportSecretKey(keypair) {
|
|
124
|
+
return bs58.encode(keypair.secretKey);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 导出私钥为 Uint8Array
|
|
128
|
+
* @param keypair Keypair
|
|
129
|
+
* @returns Uint8Array 私钥
|
|
130
|
+
*/
|
|
131
|
+
export function exportSecretKeyArray(keypair) {
|
|
132
|
+
return keypair.secretKey;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 验证 Solana 地址格式
|
|
136
|
+
* @param address 地址字符串
|
|
137
|
+
* @returns 是否有效
|
|
138
|
+
*/
|
|
139
|
+
export function isValidAddress(address) {
|
|
140
|
+
try {
|
|
141
|
+
new PublicKey(address);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 验证私钥格式
|
|
150
|
+
* @param secretKey 私钥(base58 字符串)
|
|
151
|
+
* @returns 是否有效
|
|
152
|
+
*/
|
|
153
|
+
export function isValidSecretKey(secretKey) {
|
|
154
|
+
try {
|
|
155
|
+
const decoded = bs58.decode(secretKey);
|
|
156
|
+
return decoded.length === 64;
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 从私钥获取公钥/地址
|
|
164
|
+
* @param secretKey 私钥(Uint8Array 或 base58 字符串)
|
|
165
|
+
* @returns 公钥地址
|
|
166
|
+
*/
|
|
167
|
+
export function getAddressFromSecretKey(secretKey) {
|
|
168
|
+
const keypair = keypairFromSecretKey(secretKey);
|
|
169
|
+
return keypair.publicKey.toBase58();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* 创建 PublicKey 对象
|
|
173
|
+
* @param address 地址字符串
|
|
174
|
+
* @returns PublicKey
|
|
175
|
+
*/
|
|
176
|
+
export function toPublicKey(address) {
|
|
177
|
+
if (!isValidAddress(address)) {
|
|
178
|
+
throw new Error(`Invalid Solana address: ${address}`);
|
|
179
|
+
}
|
|
180
|
+
return new PublicKey(address);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 比较两个地址是否相同
|
|
184
|
+
* @param addr1 地址1
|
|
185
|
+
* @param addr2 地址2
|
|
186
|
+
* @returns 是否相同
|
|
187
|
+
*/
|
|
188
|
+
export function isSameAddress(addr1, addr2) {
|
|
189
|
+
try {
|
|
190
|
+
const pk1 = new PublicKey(addr1);
|
|
191
|
+
const pk2 = new PublicKey(addr2);
|
|
192
|
+
return pk1.equals(pk2);
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 缩短地址显示
|
|
200
|
+
* @param address 完整地址
|
|
201
|
+
* @param prefixLength 前缀长度(默认4)
|
|
202
|
+
* @param suffixLength 后缀长度(默认4)
|
|
203
|
+
* @returns 缩短后的地址
|
|
204
|
+
*/
|
|
205
|
+
export function shortenAddress(address, prefixLength = 4, suffixLength = 4) {
|
|
206
|
+
if (address.length <= prefixLength + suffixLength + 3) {
|
|
207
|
+
return address;
|
|
208
|
+
}
|
|
209
|
+
return `${address.slice(0, prefixLength)}...${address.slice(-suffixLength)}`;
|
|
210
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "four-flap-meme-sdk",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.6",
|
|
4
4
|
"description": "SDK for Flap bonding curve and four.meme TokenManager",
|
|
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,33 @@
|
|
|
29
43
|
"node": ">=18"
|
|
30
44
|
},
|
|
31
45
|
"dependencies": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
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.4",
|
|
56
|
+
"@pump-fun/pump-sdk": "^1.24.0",
|
|
57
|
+
"@pump-fun/pump-swap-sdk": "^1.11.0",
|
|
58
|
+
"@raydium-io/raydium-sdk-v2": "^0.2.31-alpha",
|
|
59
|
+
"@solana/spl-token": "^0.4.14",
|
|
60
|
+
"@solana/web3.js": "^1.98.4",
|
|
61
|
+
"axios": "^1.13.2",
|
|
62
|
+
"ethers": "^6.16.0",
|
|
63
|
+
"pinata": "^2.5.1",
|
|
64
|
+
"tweetnacl": "^1.0.3"
|
|
65
|
+
},
|
|
66
|
+
"optionalDependencies": {
|
|
67
|
+
"@orca-so/wavebreak": "^1.1.7"
|
|
35
68
|
},
|
|
36
69
|
"devDependencies": {
|
|
37
|
-
"
|
|
70
|
+
"@types/bn.js": "^5.2.0",
|
|
71
|
+
"@types/bs58": "^4.0.4",
|
|
72
|
+
"decimal.js": "^10.6.0",
|
|
73
|
+
"typescript": "^5.9.3"
|
|
38
74
|
}
|
|
39
75
|
}
|
package/README.zh-CN.pdf
DELETED
|
Binary file
|
|
@@ -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
|
-
}
|