@swapkit/wallets 3.0.0-beta.0 → 3.0.0-beta.2
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/chunk-czhtd6cy.js +4 -0
- package/dist/chunk-czhtd6cy.js.map +10 -0
- package/dist/{chunk-5m175wfy.js → chunk-p1kdg37m.js} +2 -2
- package/dist/{chunk-5m175wfy.js.map → chunk-p1kdg37m.js.map} +1 -1
- package/dist/{chunk-f76hj57k.js → chunk-q81hzyra.js} +2 -2
- package/dist/{chunk-f76hj57k.js.map → chunk-q81hzyra.js.map} +1 -1
- package/dist/chunk-qadd75nn.js +3 -0
- package/dist/chunk-qadd75nn.js.map +10 -0
- package/dist/src/bitget/index.cjs +2 -2
- package/dist/src/bitget/index.cjs.map +5 -5
- package/dist/src/bitget/index.js +2 -2
- package/dist/src/bitget/index.js.map +5 -5
- package/dist/src/coinbase/index.cjs +2 -2
- package/dist/src/coinbase/index.cjs.map +4 -4
- package/dist/src/coinbase/index.js +2 -3
- package/dist/src/coinbase/index.js.map +4 -4
- package/dist/src/ctrl/index.cjs +2 -2
- package/dist/src/ctrl/index.cjs.map +5 -5
- package/dist/src/ctrl/index.js +2 -2
- package/dist/src/ctrl/index.js.map +5 -5
- package/dist/src/evm-extensions/index.cjs +2 -2
- package/dist/src/evm-extensions/index.cjs.map +3 -3
- package/dist/src/evm-extensions/index.js +2 -3
- package/dist/src/evm-extensions/index.js.map +3 -3
- package/dist/src/exodus/index.cjs +2 -2
- package/dist/src/exodus/index.cjs.map +3 -3
- package/dist/src/exodus/index.js +2 -3
- package/dist/src/exodus/index.js.map +3 -3
- package/dist/src/index.cjs +2 -2
- package/dist/src/index.cjs.map +3 -4
- package/dist/src/index.js +2 -2
- package/dist/src/index.js.map +3 -4
- package/dist/src/keepkey/index.cjs +2 -2
- package/dist/src/keepkey/index.cjs.map +7 -7
- package/dist/src/keepkey/index.js +2 -3
- package/dist/src/keepkey/index.js.map +7 -7
- package/dist/src/keepkey-bex/index.cjs +2 -2
- package/dist/src/keepkey-bex/index.cjs.map +4 -4
- package/dist/src/keepkey-bex/index.js +2 -3
- package/dist/src/keepkey-bex/index.js.map +4 -4
- package/dist/src/keplr/index.cjs +2 -2
- package/dist/src/keplr/index.cjs.map +3 -3
- package/dist/src/keplr/index.js +2 -3
- package/dist/src/keplr/index.js.map +3 -3
- package/dist/src/keystore/index.cjs +2 -2
- package/dist/src/keystore/index.cjs.map +4 -4
- package/dist/src/keystore/index.js +2 -3
- package/dist/src/keystore/index.js.map +4 -4
- package/dist/src/ledger/index.cjs +3 -3
- package/dist/src/ledger/index.cjs.map +8 -8
- package/dist/src/ledger/index.js +3 -4
- package/dist/src/ledger/index.js.map +8 -8
- package/dist/src/okx/index.cjs +2 -2
- package/dist/src/okx/index.cjs.map +4 -4
- package/dist/src/okx/index.js +2 -3
- package/dist/src/okx/index.js.map +4 -4
- package/dist/src/onekey/index.cjs +3 -0
- package/dist/src/onekey/index.cjs.map +10 -0
- package/dist/src/onekey/index.js +3 -0
- package/dist/src/onekey/index.js.map +10 -0
- package/dist/src/phantom/index.cjs +2 -2
- package/dist/src/phantom/index.cjs.map +3 -3
- package/dist/src/phantom/index.js +2 -3
- package/dist/src/phantom/index.js.map +3 -3
- package/dist/src/polkadotjs/index.cjs +2 -2
- package/dist/src/polkadotjs/index.cjs.map +3 -3
- package/dist/src/polkadotjs/index.js +2 -3
- package/dist/src/polkadotjs/index.js.map +3 -3
- package/dist/src/radix/index.cjs +2 -2
- package/dist/src/radix/index.cjs.map +3 -3
- package/dist/src/radix/index.js +2 -3
- package/dist/src/radix/index.js.map +3 -3
- package/dist/src/talisman/index.cjs +2 -2
- package/dist/src/talisman/index.cjs.map +3 -3
- package/dist/src/talisman/index.js +2 -3
- package/dist/src/talisman/index.js.map +3 -3
- package/dist/src/trezor/index.cjs +2 -2
- package/dist/src/trezor/index.cjs.map +3 -3
- package/dist/src/trezor/index.js +2 -3
- package/dist/src/trezor/index.js.map +3 -3
- package/dist/src/walletconnect/index.cjs +2 -2
- package/dist/src/walletconnect/index.cjs.map +5 -5
- package/dist/src/walletconnect/index.js +2 -3
- package/dist/src/walletconnect/index.js.map +5 -5
- package/package.json +31 -26
- package/src/bitget/helpers.ts +33 -93
- package/src/bitget/index.ts +32 -54
- package/src/coinbase/index.ts +2 -2
- package/src/coinbase/signer.ts +46 -42
- package/src/ctrl/index.ts +157 -22
- package/src/ctrl/walletHelpers.ts +45 -94
- package/src/evm-extensions/index.ts +21 -40
- package/src/exodus/index.ts +18 -28
- package/src/index.ts +2 -37
- package/src/keepkey/chains/cosmos.ts +10 -7
- package/src/keepkey/chains/mayachain.ts +24 -24
- package/src/keepkey/chains/thorchain.ts +19 -13
- package/src/keepkey/chains/utxo.ts +36 -48
- package/src/keepkey/index.ts +9 -23
- package/src/keepkey-bex/index.ts +25 -23
- package/src/keepkey-bex/walletHelpers.ts +8 -36
- package/src/keplr/index.ts +6 -53
- package/src/keystore/helpers.ts +34 -55
- package/src/keystore/index.ts +15 -183
- package/src/ledger/clients/evm.ts +3 -7
- package/src/ledger/clients/thorchain/lib.ts +0 -19
- package/src/ledger/clients/utxo.ts +3 -2
- package/src/ledger/helpers/getLedgerClient.ts +2 -2
- package/src/ledger/index.ts +20 -21
- package/src/ledger/types.ts +1 -1
- package/src/okx/helpers.ts +23 -33
- package/src/okx/index.ts +4 -47
- package/src/onekey/evmSigner.ts +133 -0
- package/src/onekey/index.ts +213 -0
- package/src/phantom/index.ts +18 -42
- package/src/polkadotjs/index.ts +5 -7
- package/src/radix/index.ts +31 -47
- package/src/talisman/index.ts +15 -32
- package/src/trezor/index.ts +46 -45
- package/src/types.ts +199 -0
- package/src/utils.ts +54 -0
- package/src/walletconnect/constants.ts +1 -1
- package/src/walletconnect/helpers.ts +2 -2
- package/src/walletconnect/index.ts +36 -37
- package/dist/chunk-ha78se5g.js +0 -4
- package/dist/chunk-ha78se5g.js.map +0 -10
- package/dist/chunk-yvbvzc0q.js +0 -3
- package/dist/chunk-yvbvzc0q.js.map +0 -10
- package/src/bitget/bitgetWallet.ts +0 -32
- package/src/ctrl/ctrlWallet.ts +0 -175
- package/src/helpers.ts +0 -8
- package/src/ledger/ledgerLive.ts +0 -429
package/src/keepkey-bex/index.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AssetValue,
|
|
3
3
|
Chain,
|
|
4
|
+
ChainIdToChain,
|
|
4
5
|
SwapKitError,
|
|
5
6
|
WalletOption,
|
|
6
7
|
createWallet,
|
|
7
8
|
filterSupportedChains,
|
|
8
9
|
} from "@swapkit/helpers";
|
|
9
|
-
import type { NonETHToolbox } from "@swapkit/toolboxes/evm";
|
|
10
10
|
import type { Eip1193Provider } from "ethers";
|
|
11
|
-
import { getWalletSupportedChains } from "../
|
|
11
|
+
import { getWalletSupportedChains } from "../utils";
|
|
12
12
|
import {
|
|
13
13
|
type WalletTxParams,
|
|
14
|
-
cosmosTransfer,
|
|
15
14
|
getKEEPKEYAddress,
|
|
16
15
|
getKEEPKEYMethods,
|
|
17
16
|
getKEEPKEYProvider,
|
|
@@ -50,7 +49,7 @@ export const keepkeyBexWallet = createWallet({
|
|
|
50
49
|
const address = await getKEEPKEYAddress(chain);
|
|
51
50
|
const walletMethods = await getWalletMethods(chain);
|
|
52
51
|
|
|
53
|
-
addChain({ ...walletMethods, address,
|
|
52
|
+
addChain({ ...walletMethods, address, chain, walletType });
|
|
54
53
|
}),
|
|
55
54
|
);
|
|
56
55
|
|
|
@@ -64,15 +63,15 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
|
|
|
64
63
|
switch (chain) {
|
|
65
64
|
case Chain.Maya:
|
|
66
65
|
case Chain.THORChain: {
|
|
67
|
-
const {
|
|
66
|
+
const { getCosmosToolbox, THORCHAIN_GAS_VALUE, MAYA_GAS_VALUE } = await import(
|
|
68
67
|
"@swapkit/toolboxes/cosmos"
|
|
69
68
|
);
|
|
70
69
|
|
|
71
70
|
const gasLimit = chain === Chain.Maya ? MAYA_GAS_VALUE : THORCHAIN_GAS_VALUE;
|
|
72
|
-
const toolbox =
|
|
71
|
+
const toolbox = getCosmosToolbox(chain);
|
|
73
72
|
|
|
74
73
|
return {
|
|
75
|
-
...toolbox
|
|
74
|
+
...toolbox,
|
|
76
75
|
deposit: (tx: WalletTxParams) => walletTransfer({ ...tx, recipient: "" }, "deposit"),
|
|
77
76
|
transfer: (tx: WalletTxParams) => walletTransfer({ ...tx, gasLimit }, "transfer"),
|
|
78
77
|
};
|
|
@@ -80,10 +79,19 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
|
|
|
80
79
|
|
|
81
80
|
case Chain.Cosmos:
|
|
82
81
|
case Chain.Kujira: {
|
|
83
|
-
const {
|
|
84
|
-
const toolbox = getToolboxByChain(chain)();
|
|
82
|
+
const { getCosmosToolbox } = await import("@swapkit/toolboxes/cosmos");
|
|
85
83
|
|
|
86
|
-
|
|
84
|
+
// @ts-expect-error assumed available connection
|
|
85
|
+
const signer = window.keepkey?.cosmos?.getOfflineSignerOnlyAmino(ChainIdToChain[chain]);
|
|
86
|
+
if (!signer) throw new Error("Could not load signer");
|
|
87
|
+
const toolbox = getCosmosToolbox(chain, { signer });
|
|
88
|
+
|
|
89
|
+
const accounts = await signer.getAccounts();
|
|
90
|
+
if (!accounts?.[0]?.address) throw new Error("No accounts found");
|
|
91
|
+
|
|
92
|
+
const [{ address }] = accounts;
|
|
93
|
+
|
|
94
|
+
return { ...toolbox, address };
|
|
87
95
|
}
|
|
88
96
|
|
|
89
97
|
case Chain.Dash:
|
|
@@ -91,8 +99,8 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
|
|
|
91
99
|
case Chain.BitcoinCash:
|
|
92
100
|
case Chain.Dogecoin:
|
|
93
101
|
case Chain.Litecoin: {
|
|
94
|
-
const {
|
|
95
|
-
const toolbox =
|
|
102
|
+
const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
|
|
103
|
+
const toolbox = await getUtxoToolbox(chain);
|
|
96
104
|
|
|
97
105
|
const getBalance = async () => {
|
|
98
106
|
try {
|
|
@@ -120,7 +128,7 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
|
|
|
120
128
|
case Chain.Polygon:
|
|
121
129
|
case Chain.Avalanche: {
|
|
122
130
|
const { prepareNetworkSwitch, switchEVMWalletNetwork } = await import("@swapkit/helpers");
|
|
123
|
-
const {
|
|
131
|
+
const { getEvmToolbox } = await import("@swapkit/toolboxes/evm");
|
|
124
132
|
const { BrowserProvider } = await import("ethers");
|
|
125
133
|
const ethereumWindowProvider = getKEEPKEYProvider(chain) as Eip1193Provider;
|
|
126
134
|
|
|
@@ -130,12 +138,12 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
|
|
|
130
138
|
|
|
131
139
|
const provider = new BrowserProvider(ethereumWindowProvider, "any");
|
|
132
140
|
const signer = await provider.getSigner();
|
|
133
|
-
const toolbox =
|
|
134
|
-
const keepkeyMethods = getKEEPKEYMethods(provider);
|
|
141
|
+
const toolbox = await getEvmToolbox(chain, { provider, signer });
|
|
142
|
+
const keepkeyMethods = getKEEPKEYMethods(provider, chain);
|
|
135
143
|
|
|
136
144
|
try {
|
|
137
145
|
if (chain !== Chain.Ethereum) {
|
|
138
|
-
const networkParams =
|
|
146
|
+
const networkParams = toolbox.getNetworkParams();
|
|
139
147
|
await switchEVMWalletNetwork(provider, chain, networkParams);
|
|
140
148
|
}
|
|
141
149
|
} catch (_error) {
|
|
@@ -148,13 +156,7 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
|
|
|
148
156
|
return prepareNetworkSwitch({
|
|
149
157
|
provider,
|
|
150
158
|
chain,
|
|
151
|
-
toolbox: {
|
|
152
|
-
...toolbox,
|
|
153
|
-
...keepkeyMethods,
|
|
154
|
-
// Overwrite getBalance due to race conditions
|
|
155
|
-
getBalance: (address: string, potentialScamFilter?: boolean) =>
|
|
156
|
-
getBalance({ chain, provider: getProvider(chain), address, potentialScamFilter }),
|
|
157
|
-
},
|
|
159
|
+
toolbox: { ...toolbox, ...keepkeyMethods },
|
|
158
160
|
});
|
|
159
161
|
}
|
|
160
162
|
|
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type AssetValue,
|
|
3
3
|
Chain,
|
|
4
|
-
ChainToChainId,
|
|
5
|
-
type CosmosChain,
|
|
6
4
|
type EVMChain,
|
|
7
5
|
EVMChains,
|
|
8
6
|
type FeeOption,
|
|
9
|
-
SKConfig,
|
|
10
7
|
SwapKitError,
|
|
11
8
|
WalletOption,
|
|
12
9
|
} from "@swapkit/helpers";
|
|
13
10
|
import { erc20ABI } from "@swapkit/helpers/contracts";
|
|
14
|
-
import type { TransferParams } from "@swapkit/toolboxes/cosmos";
|
|
15
11
|
import type { ApproveParams, CallParams, EVMTxParams } from "@swapkit/toolboxes/evm";
|
|
16
12
|
import type { BrowserProvider, Eip1193Provider } from "ethers";
|
|
17
13
|
|
|
@@ -188,33 +184,7 @@ export async function walletTransfer(
|
|
|
188
184
|
return transaction({ method, params, chain: assetValue.chain });
|
|
189
185
|
}
|
|
190
186
|
|
|
191
|
-
export function
|
|
192
|
-
const chainId = ChainToChainId[chain];
|
|
193
|
-
return async ({ from, recipient, assetValue }: TransferParams) => {
|
|
194
|
-
const { getMsgSendDenom, createSigningStargateClient } = await import(
|
|
195
|
-
"@swapkit/toolboxes/cosmos"
|
|
196
|
-
);
|
|
197
|
-
// @ts-expect-error assumed available connection
|
|
198
|
-
const offlineSigner = window.keepkey?.cosmos?.getOfflineSignerOnlyAmino(chainId);
|
|
199
|
-
const cosmJS = await createSigningStargateClient(SKConfig.get("rpcUrls")[chain], offlineSigner);
|
|
200
|
-
|
|
201
|
-
const coins = [
|
|
202
|
-
{
|
|
203
|
-
denom: getMsgSendDenom(assetValue.symbol).toLowerCase(),
|
|
204
|
-
amount: assetValue.getBaseValue("string"),
|
|
205
|
-
},
|
|
206
|
-
];
|
|
207
|
-
|
|
208
|
-
try {
|
|
209
|
-
const { transactionHash } = await cosmJS.sendTokens(from, recipient, coins, 2);
|
|
210
|
-
return transactionHash;
|
|
211
|
-
} catch (error) {
|
|
212
|
-
throw new SwapKitError("core_transaction_failed", { error });
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export function getKEEPKEYMethods(provider: BrowserProvider) {
|
|
187
|
+
export function getKEEPKEYMethods(provider: BrowserProvider, chain: EVMChain) {
|
|
218
188
|
return {
|
|
219
189
|
call: async <T>({
|
|
220
190
|
contractAddress,
|
|
@@ -226,13 +196,14 @@ export function getKEEPKEYMethods(provider: BrowserProvider) {
|
|
|
226
196
|
if (!contractAddress) {
|
|
227
197
|
throw new SwapKitError("wallet_keepkey_contract_address_not_provided");
|
|
228
198
|
}
|
|
229
|
-
const { createContract,
|
|
199
|
+
const { createContract, getCreateContractTxObject, isStateChangingCall, toHexString } =
|
|
230
200
|
await import("@swapkit/toolboxes/evm");
|
|
231
201
|
|
|
232
|
-
const isStateChanging = isStateChangingCall(abi, funcName);
|
|
202
|
+
const isStateChanging = isStateChangingCall({ abi, funcName });
|
|
233
203
|
|
|
234
204
|
if (isStateChanging) {
|
|
235
|
-
const
|
|
205
|
+
const createTx = getCreateContractTxObject({ provider, chain });
|
|
206
|
+
const { value, from, to, data } = await createTx({
|
|
236
207
|
contractAddress,
|
|
237
208
|
abi,
|
|
238
209
|
funcName,
|
|
@@ -251,11 +222,12 @@ export function getKEEPKEYMethods(provider: BrowserProvider) {
|
|
|
251
222
|
return typeof result?.hash === "string" ? result?.hash : result;
|
|
252
223
|
},
|
|
253
224
|
approve: async ({ assetAddress, spenderAddress, amount, from }: ApproveParams) => {
|
|
254
|
-
const { MAX_APPROVAL,
|
|
225
|
+
const { MAX_APPROVAL, getCreateContractTxObject, toHexString } = await import(
|
|
255
226
|
"@swapkit/toolboxes/evm"
|
|
256
227
|
);
|
|
257
228
|
|
|
258
|
-
const
|
|
229
|
+
const createTx = getCreateContractTxObject({ provider, chain });
|
|
230
|
+
const { value, to, data } = await createTx({
|
|
259
231
|
contractAddress: assetAddress,
|
|
260
232
|
abi: erc20ABI,
|
|
261
233
|
funcName: "approve",
|
package/src/keplr/index.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { Keplr } from "@keplr-wallet/types";
|
|
2
1
|
import {
|
|
3
|
-
type AssetValue,
|
|
4
2
|
Chain,
|
|
5
3
|
ChainId,
|
|
6
4
|
ChainToChainId,
|
|
@@ -8,16 +6,8 @@ import {
|
|
|
8
6
|
createWallet,
|
|
9
7
|
filterSupportedChains,
|
|
10
8
|
} from "@swapkit/helpers";
|
|
11
|
-
import type { CosmosToolboxType } from "@swapkit/toolboxes/cosmos";
|
|
12
9
|
import { chainRegistry } from "./chainRegistry";
|
|
13
10
|
|
|
14
|
-
declare global {
|
|
15
|
-
interface Window {
|
|
16
|
-
keplr: Keplr;
|
|
17
|
-
leap: Keplr;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
11
|
const keplrSupportedChainIds = [ChainId.Cosmos, ChainId.Kujira, ChainId.THORChain] as const;
|
|
22
12
|
|
|
23
13
|
export const keplrWallet = createWallet({
|
|
@@ -33,7 +23,6 @@ export const keplrWallet = createWallet({
|
|
|
33
23
|
const keplrClient = window[extensionKey];
|
|
34
24
|
|
|
35
25
|
await Promise.all(
|
|
36
|
-
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: refactor/split
|
|
37
26
|
filteredChains.map(async (chain) => {
|
|
38
27
|
const chainId = ChainToChainId[chain] as (typeof keplrSupportedChainIds)[number];
|
|
39
28
|
|
|
@@ -45,54 +34,18 @@ export const keplrWallet = createWallet({
|
|
|
45
34
|
}
|
|
46
35
|
|
|
47
36
|
keplrClient?.enable(chainId);
|
|
48
|
-
const
|
|
49
|
-
if (!
|
|
37
|
+
const signer = keplrClient?.getOfflineSignerOnlyAmino(chainId);
|
|
38
|
+
if (!signer) throw new Error("Could not load signer");
|
|
50
39
|
|
|
51
|
-
const {
|
|
40
|
+
const { getCosmosToolbox } = await import("@swapkit/toolboxes/cosmos");
|
|
52
41
|
|
|
53
|
-
const accounts = await
|
|
42
|
+
const accounts = await signer.getAccounts();
|
|
54
43
|
if (!accounts?.[0]?.address) throw new Error("No accounts found");
|
|
55
44
|
|
|
56
45
|
const [{ address }] = accounts;
|
|
57
|
-
const toolbox =
|
|
58
|
-
|
|
59
|
-
const transfer = (params: {
|
|
60
|
-
from?: string;
|
|
61
|
-
recipient: string;
|
|
62
|
-
assetValue: AssetValue;
|
|
63
|
-
memo?: string;
|
|
64
|
-
}) =>
|
|
65
|
-
toolbox.transfer({
|
|
66
|
-
...params,
|
|
67
|
-
signer: offlineSigner,
|
|
68
|
-
fee: 2,
|
|
69
|
-
from: params.from || address,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const deposit =
|
|
73
|
-
chain === Chain.THORChain
|
|
74
|
-
? (params: {
|
|
75
|
-
from?: string;
|
|
76
|
-
assetValue: AssetValue;
|
|
77
|
-
memo?: string;
|
|
78
|
-
}) =>
|
|
79
|
-
(toolbox as ReturnType<CosmosToolboxType["THOR"]>).deposit({
|
|
80
|
-
...params,
|
|
81
|
-
signer: offlineSigner,
|
|
82
|
-
from: params.from || address,
|
|
83
|
-
memo: params.memo || "",
|
|
84
|
-
})
|
|
85
|
-
: undefined;
|
|
46
|
+
const toolbox = getCosmosToolbox(chain, { signer });
|
|
86
47
|
|
|
87
|
-
addChain({
|
|
88
|
-
...toolbox,
|
|
89
|
-
deposit,
|
|
90
|
-
chain,
|
|
91
|
-
transfer,
|
|
92
|
-
address,
|
|
93
|
-
balance: [],
|
|
94
|
-
walletType,
|
|
95
|
-
});
|
|
48
|
+
addChain({ ...toolbox, chain, address, walletType });
|
|
96
49
|
}),
|
|
97
50
|
);
|
|
98
51
|
|
package/src/keystore/helpers.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import crypto from "crypto";
|
|
2
1
|
import { generateMnemonic, validateMnemonic } from "@scure/bip39";
|
|
3
2
|
import { wordlist } from "@scure/bip39/wordlists/english";
|
|
4
|
-
import { blake2bFinal, blake2bInit, blake2bUpdate } from "blakejs";
|
|
5
3
|
|
|
6
4
|
export type Keystore = {
|
|
7
5
|
version: number;
|
|
@@ -16,10 +14,8 @@ export type Keystore = {
|
|
|
16
14
|
};
|
|
17
15
|
};
|
|
18
16
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*/
|
|
22
|
-
const blake256 = (initData: Buffer | string): string => {
|
|
17
|
+
async function blake256(initData: Buffer | string) {
|
|
18
|
+
const { blake2bFinal, blake2bInit, blake2bUpdate } = await import("blakejs");
|
|
23
19
|
let data = initData;
|
|
24
20
|
|
|
25
21
|
if (!(data instanceof Buffer)) {
|
|
@@ -33,43 +29,24 @@ const blake256 = (initData: Buffer | string): string => {
|
|
|
33
29
|
return Array.from(blake2bFinal(context))
|
|
34
30
|
.map((byte) => (byte < 0x10 ? `0${byte.toString(16)}` : byte.toString(16)))
|
|
35
31
|
.join("");
|
|
36
|
-
}
|
|
32
|
+
}
|
|
37
33
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
salt: string | Buffer,
|
|
41
|
-
iterations: number,
|
|
42
|
-
keylen: number,
|
|
43
|
-
digest: string,
|
|
44
|
-
) =>
|
|
45
|
-
new Promise<Buffer>((resolve, reject) => {
|
|
46
|
-
crypto.pbkdf2(passphrase, salt, iterations, keylen, digest, (error, drived) => {
|
|
47
|
-
if (error) {
|
|
48
|
-
reject(error);
|
|
49
|
-
} else {
|
|
50
|
-
resolve(drived);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
});
|
|
34
|
+
export async function encryptToKeyStore(phrase: string, password: string) {
|
|
35
|
+
const { pbkdf2Sync, randomBytes, createCipheriv } = await import("node:crypto");
|
|
54
36
|
|
|
55
|
-
export const encryptToKeyStore = async (phrase: string, password: string) => {
|
|
56
|
-
const salt = crypto.randomBytes(32);
|
|
57
|
-
const iv = crypto.randomBytes(16);
|
|
58
|
-
const kdfParams = { c: 262144, prf: "hmac-sha256", dklen: 32, salt: salt.toString("hex") };
|
|
59
37
|
const cipher = "aes-128-ctr";
|
|
38
|
+
const iv = randomBytes(16);
|
|
39
|
+
const salt = randomBytes(32);
|
|
40
|
+
const kdfParams = { c: 262144, prf: "hmac-sha256", dklen: 32, salt: salt.toString("hex") };
|
|
60
41
|
|
|
61
|
-
const derivedKey =
|
|
62
|
-
|
|
63
|
-
salt,
|
|
64
|
-
kdfParams.c,
|
|
65
|
-
kdfParams.dklen,
|
|
66
|
-
"sha256",
|
|
67
|
-
);
|
|
68
|
-
const cipherIV = crypto.createCipheriv(cipher, derivedKey.subarray(0, 16), iv);
|
|
42
|
+
const derivedKey = pbkdf2Sync(password, salt, kdfParams.c, kdfParams.dklen, "sha256");
|
|
43
|
+
const cipherIV = createCipheriv(cipher, derivedKey.subarray(0, 16), iv);
|
|
69
44
|
const ciphertext = Buffer.concat([
|
|
70
45
|
cipherIV.update(Buffer.from(phrase, "utf8")),
|
|
71
46
|
cipherIV.final(),
|
|
72
47
|
]);
|
|
48
|
+
const initData = Buffer.concat([derivedKey.subarray(16, 32), Buffer.from(ciphertext)]);
|
|
49
|
+
const mac = await blake256(initData);
|
|
73
50
|
|
|
74
51
|
return {
|
|
75
52
|
meta: "xchain-keystore",
|
|
@@ -80,36 +57,38 @@ export const encryptToKeyStore = async (phrase: string, password: string) => {
|
|
|
80
57
|
ciphertext: ciphertext.toString("hex"),
|
|
81
58
|
kdf: "pbkdf2",
|
|
82
59
|
kdfparams: kdfParams,
|
|
83
|
-
mac
|
|
60
|
+
mac,
|
|
84
61
|
},
|
|
85
62
|
};
|
|
86
|
-
}
|
|
63
|
+
}
|
|
87
64
|
|
|
88
|
-
export
|
|
65
|
+
export function generatePhrase(size: 12 | 24 = 12) {
|
|
89
66
|
return generateMnemonic(wordlist, size === 12 ? 128 : 256);
|
|
90
|
-
}
|
|
67
|
+
}
|
|
91
68
|
|
|
92
|
-
export
|
|
69
|
+
export function validatePhrase(phrase: string) {
|
|
93
70
|
return validateMnemonic(phrase, wordlist);
|
|
94
|
-
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function decryptFromKeystore(keystore: Keystore, password: string) {
|
|
74
|
+
const { createDecipheriv, pbkdf2Sync } = await import("node:crypto");
|
|
75
|
+
const { SwapKitError } = await import("@swapkit/helpers");
|
|
95
76
|
|
|
96
|
-
export const decryptFromKeystore = async (keystore: Keystore, password: string) => {
|
|
97
77
|
switch (keystore.version) {
|
|
98
78
|
case 1: {
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
Buffer.from(kdfparams.salt, "hex"),
|
|
103
|
-
kdfparams.c,
|
|
104
|
-
kdfparams.dklen,
|
|
105
|
-
"sha256",
|
|
106
|
-
);
|
|
79
|
+
const kdfParams = keystore.crypto.kdfparams;
|
|
80
|
+
const salt = Buffer.from(kdfParams.salt, "hex");
|
|
81
|
+
const derivedKey = pbkdf2Sync(password, salt, kdfParams.c, kdfParams.dklen, "sha256");
|
|
107
82
|
|
|
108
83
|
const ciphertext = Buffer.from(keystore.crypto.ciphertext, "hex");
|
|
109
|
-
const
|
|
84
|
+
const initData = Buffer.concat([derivedKey.subarray(16, 32), ciphertext]);
|
|
85
|
+
const mac = await blake256(initData);
|
|
110
86
|
|
|
111
|
-
if (mac !== keystore.crypto.mac)
|
|
112
|
-
|
|
87
|
+
if (mac !== keystore.crypto.mac) {
|
|
88
|
+
throw new SwapKitError("wallet_keystore_invalid_password");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const decipher = createDecipheriv(
|
|
113
92
|
keystore.crypto.cipher,
|
|
114
93
|
derivedKey.subarray(0, 16),
|
|
115
94
|
Buffer.from(keystore.crypto.cipherparams.iv, "hex"),
|
|
@@ -120,6 +99,6 @@ export const decryptFromKeystore = async (keystore: Keystore, password: string)
|
|
|
120
99
|
}
|
|
121
100
|
|
|
122
101
|
default:
|
|
123
|
-
throw new
|
|
102
|
+
throw new SwapKitError("wallet_keystore_unsupported_version");
|
|
124
103
|
}
|
|
125
|
-
}
|
|
104
|
+
}
|
package/src/keystore/index.ts
CHANGED
|
@@ -1,176 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
type AssetValue,
|
|
3
2
|
Chain,
|
|
4
3
|
CosmosChains,
|
|
5
4
|
type DerivationPathArray,
|
|
6
5
|
EVMChains,
|
|
7
6
|
NetworkDerivationPath,
|
|
8
|
-
SKConfig,
|
|
9
7
|
UTXOChains,
|
|
10
8
|
WalletOption,
|
|
11
|
-
type WalletTxParams,
|
|
12
9
|
createWallet,
|
|
13
|
-
derivationPathToString,
|
|
14
10
|
filterSupportedChains,
|
|
15
|
-
|
|
11
|
+
updateDerivationPath,
|
|
16
12
|
} from "@swapkit/helpers";
|
|
17
|
-
import
|
|
18
|
-
import type {
|
|
19
|
-
Psbt,
|
|
20
|
-
TransactionType,
|
|
21
|
-
UTXOTransferParams,
|
|
22
|
-
UTXOWalletTransferParams,
|
|
23
|
-
} from "@swapkit/toolboxes/utxo";
|
|
24
|
-
import { getWalletSupportedChains } from "../helpers";
|
|
25
|
-
|
|
26
|
-
type Params = {
|
|
27
|
-
chain: Chain;
|
|
28
|
-
phrase: string;
|
|
29
|
-
derivationPath: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const getWalletMethods = async ({ chain, phrase, derivationPath }: Params) => {
|
|
33
|
-
const rpcUrl = SKConfig.get("rpcUrls")[chain];
|
|
34
|
-
|
|
35
|
-
switch (chain) {
|
|
36
|
-
case Chain.Arbitrum:
|
|
37
|
-
case Chain.Avalanche:
|
|
38
|
-
case Chain.Base:
|
|
39
|
-
case Chain.BinanceSmartChain:
|
|
40
|
-
case Chain.Ethereum:
|
|
41
|
-
case Chain.Optimism:
|
|
42
|
-
case Chain.Polygon: {
|
|
43
|
-
const { getProvider, getToolboxByChain } = await import("@swapkit/toolboxes/evm");
|
|
44
|
-
const { HDNodeWallet } = await import("ethers");
|
|
45
|
-
|
|
46
|
-
const provider = getProvider(chain, rpcUrl);
|
|
47
|
-
const wallet = HDNodeWallet.fromPhrase(phrase).connect(provider);
|
|
48
|
-
const toolbox = getToolboxByChain(chain)({ provider, signer: wallet });
|
|
49
|
-
|
|
50
|
-
return { address: wallet.address, walletMethods: toolbox };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
case Chain.BitcoinCash: {
|
|
54
|
-
const { BCHToolbox } = await import("@swapkit/toolboxes/utxo");
|
|
55
|
-
const toolbox = BCHToolbox();
|
|
56
|
-
const keys = await toolbox.createKeysForPath({ phrase, derivationPath });
|
|
57
|
-
const address = toolbox.getAddressFromKeys(keys);
|
|
58
|
-
|
|
59
|
-
async function signTransaction({
|
|
60
|
-
builder,
|
|
61
|
-
utxos,
|
|
62
|
-
}: Awaited<ReturnType<typeof toolbox.buildBCHTx>>) {
|
|
63
|
-
utxos.forEach((utxo, index) => {
|
|
64
|
-
builder.sign(index, keys, undefined, 0x41, utxo.witnessUtxo?.value);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return builder.build();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const walletMethods = {
|
|
71
|
-
...toolbox,
|
|
72
|
-
transfer: (
|
|
73
|
-
params: UTXOWalletTransferParams<
|
|
74
|
-
Awaited<ReturnType<typeof toolbox.buildBCHTx>>,
|
|
75
|
-
TransactionType
|
|
76
|
-
>,
|
|
77
|
-
) => toolbox.transfer({ ...params, from: address, signTransaction }),
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
return { address, walletMethods };
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
case Chain.Bitcoin:
|
|
84
|
-
case Chain.Dash:
|
|
85
|
-
case Chain.Dogecoin:
|
|
86
|
-
case Chain.Litecoin: {
|
|
87
|
-
const { getToolboxByChain } = await import("@swapkit/toolboxes/utxo");
|
|
88
|
-
|
|
89
|
-
const toolbox = getToolboxByChain(chain)();
|
|
90
|
-
|
|
91
|
-
const keys = await toolbox.createKeysForPath({ phrase, derivationPath });
|
|
92
|
-
const address = toolbox.getAddressFromKeys(keys);
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
address,
|
|
96
|
-
walletMethods: {
|
|
97
|
-
...toolbox,
|
|
98
|
-
transfer: (params: UTXOTransferParams) =>
|
|
99
|
-
toolbox.transfer({
|
|
100
|
-
...params,
|
|
101
|
-
from: address,
|
|
102
|
-
signTransaction: async (psbt: Psbt) => psbt.signAllInputs(keys),
|
|
103
|
-
}),
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
case Chain.Cosmos:
|
|
109
|
-
case Chain.Kujira: {
|
|
110
|
-
const { getToolboxByChain } = await import("@swapkit/toolboxes/cosmos");
|
|
111
|
-
const toolbox = getToolboxByChain(chain)();
|
|
112
|
-
const address = await toolbox.getAddressFromMnemonic(phrase);
|
|
113
|
-
const signer = await toolbox.getSigner(phrase);
|
|
114
|
-
|
|
115
|
-
const transfer = (params: TransferParams) => toolbox.transfer({ ...params, signer });
|
|
116
|
-
|
|
117
|
-
return { address, walletMethods: { ...toolbox, transfer } };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
case Chain.Maya:
|
|
121
|
-
case Chain.THORChain: {
|
|
122
|
-
const { getToolboxByChain } = await import("@swapkit/toolboxes/cosmos");
|
|
123
|
-
|
|
124
|
-
const toolbox = getToolboxByChain(chain)();
|
|
125
|
-
const signer = await toolbox.getSigner(phrase);
|
|
126
|
-
const address = await toolbox.getAddressFromMnemonic(phrase);
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
address,
|
|
130
|
-
walletMethods: {
|
|
131
|
-
...toolbox,
|
|
132
|
-
deposit: (params: DepositParam) => toolbox.deposit({ ...params, from: address, signer }),
|
|
133
|
-
transfer: (params: TransferParams) =>
|
|
134
|
-
toolbox.transfer({ ...params, from: address, signer }),
|
|
135
|
-
signMessage: async (message: string) => {
|
|
136
|
-
const privateKey = await toolbox.createPrivateKeyFromPhrase(phrase);
|
|
137
|
-
return toolbox.signWithPrivateKey({ privateKey, message });
|
|
138
|
-
},
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
case Chain.Polkadot:
|
|
144
|
-
case Chain.Chainflip: {
|
|
145
|
-
const { Network, getToolboxByChain, createKeyring } = await import(
|
|
146
|
-
"@swapkit/toolboxes/substrate"
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
const signer = await createKeyring(phrase, Network[chain].prefix);
|
|
150
|
-
const toolbox = await getToolboxByChain(chain, { signer });
|
|
151
|
-
|
|
152
|
-
return { address: signer.address, walletMethods: toolbox };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
case Chain.Solana: {
|
|
156
|
-
const { SOLToolbox } = await import("@swapkit/toolboxes/solana");
|
|
157
|
-
const toolbox = SOLToolbox();
|
|
158
|
-
const keypair = await toolbox.createKeysForPath({ phrase, derivationPath });
|
|
159
|
-
|
|
160
|
-
return {
|
|
161
|
-
address: toolbox.getAddressFromKeys(keypair),
|
|
162
|
-
walletMethods: {
|
|
163
|
-
...toolbox,
|
|
164
|
-
transfer: (params: WalletTxParams & { assetValue: AssetValue }) =>
|
|
165
|
-
toolbox.transfer({ ...params, fromKeypair: keypair }),
|
|
166
|
-
},
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
default:
|
|
171
|
-
throw new Error(`Unsupported chain ${chain}`);
|
|
172
|
-
}
|
|
173
|
-
};
|
|
13
|
+
import { getWalletSupportedChains } from "../utils";
|
|
174
14
|
|
|
175
15
|
export const keystoreWallet = createWallet({
|
|
176
16
|
name: "connectKeystore",
|
|
@@ -181,6 +21,7 @@ export const keystoreWallet = createWallet({
|
|
|
181
21
|
...CosmosChains,
|
|
182
22
|
Chain.Polkadot,
|
|
183
23
|
Chain.Chainflip,
|
|
24
|
+
Chain.Ripple,
|
|
184
25
|
Chain.Solana,
|
|
185
26
|
],
|
|
186
27
|
connect: ({ addChain, supportedChains, walletType }) =>
|
|
@@ -201,32 +42,23 @@ export const keystoreWallet = createWallet({
|
|
|
201
42
|
? derivationPathMapOrIndex[chain]
|
|
202
43
|
: undefined;
|
|
203
44
|
|
|
204
|
-
const
|
|
45
|
+
const derivationArrayToUpdate = NetworkDerivationPath[chain].slice(
|
|
46
|
+
0,
|
|
47
|
+
chain === Chain.Solana ? 4 : 5,
|
|
48
|
+
) as DerivationPathArray;
|
|
205
49
|
|
|
206
|
-
const
|
|
50
|
+
const derivationPath: DerivationPathArray =
|
|
207
51
|
derivationPathFromMap ||
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
: [first, second, third, fourth, fifth],
|
|
212
|
-
derivationPathIndex,
|
|
213
|
-
);
|
|
52
|
+
updateDerivationPath(derivationArrayToUpdate, { index: derivationPathIndex });
|
|
53
|
+
|
|
54
|
+
const { getToolbox } = await import("@swapkit/toolboxes");
|
|
214
55
|
|
|
215
|
-
const
|
|
56
|
+
const toolbox = await getToolbox(chain, { phrase, derivationPath });
|
|
57
|
+
const address = (await toolbox.getAddress()) || "";
|
|
216
58
|
|
|
217
|
-
const {
|
|
218
|
-
chain,
|
|
219
|
-
derivationPath,
|
|
220
|
-
phrase,
|
|
221
|
-
});
|
|
59
|
+
const wallet = { ...toolbox, address };
|
|
222
60
|
|
|
223
|
-
addChain({
|
|
224
|
-
...walletMethods,
|
|
225
|
-
address,
|
|
226
|
-
balance: [],
|
|
227
|
-
chain,
|
|
228
|
-
walletType: WalletOption.KEYSTORE,
|
|
229
|
-
});
|
|
61
|
+
addChain({ ...wallet, chain, walletType: WalletOption.KEYSTORE });
|
|
230
62
|
}),
|
|
231
63
|
);
|
|
232
64
|
|