@swapkit/wallet-keystore 1.0.16 → 1.1.0

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/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "dependencies": {
3
3
  "@scure/bip39": "1.3.0",
4
- "@swapkit/helpers": "1.3.0",
5
- "@swapkit/toolbox-cosmos": "1.0.16",
6
- "@swapkit/toolbox-evm": "1.1.9",
7
- "@swapkit/toolbox-radix": "1.0.14",
8
- "@swapkit/toolbox-solana": "1.0.14",
9
- "@swapkit/toolbox-substrate": "1.1.9",
10
- "@swapkit/toolbox-utxo": "1.0.14",
4
+ "@swapkit/helpers": "1.3.2",
5
+ "@swapkit/toolbox-cosmos": "1.0.18",
6
+ "@swapkit/toolbox-evm": "1.1.11",
7
+ "@swapkit/toolbox-radix": "1.0.16",
8
+ "@swapkit/toolbox-solana": "1.0.16",
9
+ "@swapkit/toolbox-substrate": "1.1.11",
10
+ "@swapkit/toolbox-utxo": "1.0.16",
11
11
  "blakejs": "1.2.1",
12
12
  "micro-key-producer": "0.7.0"
13
13
  },
@@ -35,5 +35,5 @@
35
35
  },
36
36
  "type": "module",
37
37
  "types": "./src/index.ts",
38
- "version": "1.0.16"
38
+ "version": "1.1.0"
39
39
  }
package/src/keystore.ts CHANGED
@@ -3,14 +3,17 @@ import {
3
3
  Chain,
4
4
  type ConnectWalletParams,
5
5
  DerivationPath,
6
+ type DerivationPathArray,
6
7
  RPCUrl,
7
8
  type WalletChain,
8
9
  WalletOption,
9
10
  type WalletTxParams,
10
11
  type Witness,
12
+ derivationPathToString,
13
+ ensureEVMApiKeys,
11
14
  setRequestClientConfig,
12
15
  } from "@swapkit/helpers";
13
- import type { DepositParam, ThorchainToolboxType, TransferParams } from "@swapkit/toolbox-cosmos";
16
+ import type { DepositParam, TransferParams } from "@swapkit/toolbox-cosmos";
14
17
  import type {
15
18
  Psbt,
16
19
  TransactionType,
@@ -30,7 +33,7 @@ type Params = KeystoreOptions & {
30
33
  rpcUrl?: string;
31
34
  chain: Chain;
32
35
  phrase: string;
33
- index: number;
36
+ derivationPath: string;
34
37
  };
35
38
 
36
39
  const getWalletMethodsForChain = async ({
@@ -41,11 +44,9 @@ const getWalletMethodsForChain = async ({
41
44
  ethplorerApiKey,
42
45
  covalentApiKey,
43
46
  blockchairApiKey,
44
- index,
47
+ derivationPath,
45
48
  stagenet,
46
49
  }: Params) => {
47
- const derivationPath = `${DerivationPath[chain] as string}/${index}`;
48
-
49
50
  switch (chain) {
50
51
  case Chain.BinanceSmartChain:
51
52
  case Chain.Arbitrum:
@@ -53,38 +54,30 @@ const getWalletMethodsForChain = async ({
53
54
  case Chain.Polygon:
54
55
  case Chain.Avalanche:
55
56
  case Chain.Ethereum: {
56
- if (chain === Chain.Ethereum && !ethplorerApiKey) {
57
- throw new Error("Ethplorer API key not found");
58
- // biome-ignore lint/style/noUselessElse: This is a valid use case
59
- } else if (!covalentApiKey) {
60
- throw new Error("Covalent API key not found");
61
- }
62
-
63
57
  const { HDNodeWallet, getProvider, getToolboxByChain } = await import("@swapkit/toolbox-evm");
64
58
 
59
+ const keys = ensureEVMApiKeys({ chain, covalentApiKey, ethplorerApiKey });
65
60
  const provider = getProvider(chain, rpcUrl);
66
61
  const wallet = HDNodeWallet.fromPhrase(phrase).connect(provider);
67
- const params = { api, provider, signer: wallet };
68
-
69
- const toolbox = getToolboxByChain(chain)({
70
- ...params,
71
- covalentApiKey,
72
- ethplorerApiKey: ethplorerApiKey as string,
73
- });
62
+ const params = { ...keys, api, provider, signer: wallet };
74
63
 
75
- return { address: wallet.address, walletMethods: toolbox };
64
+ return { address: wallet.address, walletMethods: getToolboxByChain(chain)(params) };
76
65
  }
77
66
 
78
67
  case Chain.BitcoinCash: {
79
68
  const { BCHToolbox } = await import("@swapkit/toolbox-utxo");
80
- const toolbox = BCHToolbox({
81
- rpcUrl,
82
- apiKey: blockchairApiKey,
83
- apiClient: api,
84
- });
69
+ const toolbox = BCHToolbox({ rpcUrl, apiKey: blockchairApiKey, apiClient: api });
85
70
  const keys = await toolbox.createKeysForPath({ phrase, derivationPath });
86
71
  const address = toolbox.getAddressFromKeys(keys);
87
72
 
73
+ function signTransaction({ builder, utxos }: Awaited<ReturnType<typeof toolbox.buildBCHTx>>) {
74
+ utxos.forEach((utxo, index) => {
75
+ builder.sign(index, keys, undefined, 0x41, (utxo.witnessUtxo as Witness).value);
76
+ });
77
+
78
+ return builder.build();
79
+ }
80
+
88
81
  const walletMethods = {
89
82
  ...toolbox,
90
83
  transfer: (
@@ -92,21 +85,7 @@ const getWalletMethodsForChain = async ({
92
85
  Awaited<ReturnType<typeof toolbox.buildBCHTx>>,
93
86
  TransactionType
94
87
  >,
95
- ) =>
96
- toolbox.transfer({
97
- ...params,
98
- from: address,
99
- signTransaction: ({
100
- builder,
101
- utxos,
102
- }: Awaited<ReturnType<typeof toolbox.buildBCHTx>>) => {
103
- utxos.forEach((utxo, index) => {
104
- builder.sign(index, keys, undefined, 0x41, (utxo.witnessUtxo as Witness).value);
105
- });
106
-
107
- return builder.build();
108
- },
109
- }),
88
+ ) => toolbox.transfer({ ...params, from: address, signTransaction }),
110
89
  };
111
90
 
112
91
  return { address, walletMethods };
@@ -135,10 +114,7 @@ const getWalletMethodsForChain = async ({
135
114
  toolbox.transfer({
136
115
  ...params,
137
116
  from: address,
138
- signTransaction: (psbt: Psbt) => {
139
- psbt.signAllInputs(keys);
140
- return psbt;
141
- },
117
+ signTransaction: (psbt: Psbt) => psbt.signAllInputs(keys),
142
118
  }),
143
119
  },
144
120
  };
@@ -161,40 +137,20 @@ const getWalletMethodsForChain = async ({
161
137
  const signer = await toolbox.getSigner(phrase);
162
138
  const address = await toolbox.getAddressFromMnemonic(phrase);
163
139
 
164
- const transfer = async ({ assetValue, recipient, memo }: TransferParams) =>
165
- toolbox.transfer({
166
- from: address,
167
- recipient,
168
- assetValue,
169
- memo,
170
- signer,
171
- });
172
-
173
- const deposit =
174
- "deposit" in toolbox
175
- ? ({ assetValue, memo }: DepositParam) => {
176
- return (toolbox as ThorchainToolboxType).deposit({
177
- assetValue,
178
- memo,
179
- from: address,
180
- signer,
181
- });
182
- }
183
- : undefined;
184
-
185
- const signMessage = async (message: string) => {
186
- const privateKey = await toolbox.createPrivateKeyFromPhrase(phrase);
187
- return toolbox.signWithPrivateKey({ privateKey, message });
188
- };
189
-
190
- const walletMethods = {
191
- ...toolbox,
192
- deposit,
193
- transfer,
194
- signMessage,
140
+ return {
141
+ address,
142
+ walletMethods: {
143
+ ...toolbox,
144
+ deposit: ({ assetValue, memo }: DepositParam) =>
145
+ toolbox.deposit({ assetValue, memo, from: address, signer }),
146
+ transfer: (params: TransferParams) =>
147
+ toolbox.transfer({ ...params, from: address, signer }),
148
+ signMessage: async (message: string) => {
149
+ const privateKey = await toolbox.createPrivateKeyFromPhrase(phrase);
150
+ return toolbox.signWithPrivateKey({ privateKey, message });
151
+ },
152
+ },
195
153
  };
196
-
197
- return { address, walletMethods };
198
154
  }
199
155
 
200
156
  case Chain.Polkadot:
@@ -218,7 +174,7 @@ const getWalletMethodsForChain = async ({
218
174
  const signer = await createPrivateKey(phrase);
219
175
  const toolbox = await RadixToolbox({ api, signer });
220
176
 
221
- return { address: await toolbox.getAddress(), walletMethods: toolbox };
177
+ return { address: toolbox.getAddress(), walletMethods: toolbox };
222
178
  }
223
179
 
224
180
  case Chain.Solana: {
@@ -247,14 +203,23 @@ function connectKeystore({
247
203
  rpcUrls,
248
204
  config: { thorswapApiKey, covalentApiKey, ethplorerApiKey, blockchairApiKey, stagenet },
249
205
  }: ConnectWalletParams) {
250
- return async function connectKeystore(chains: WalletChain[], phrase: string, index = 0) {
206
+ return async function connectKeystore(
207
+ chains: WalletChain[],
208
+ phrase: string,
209
+ derivationPathMapOrIndex?: { [chain in Chain]?: DerivationPathArray } | number,
210
+ ) {
251
211
  setRequestClientConfig({ apiKey: thorswapApiKey });
252
212
 
253
213
  const promises = chains.map(async (chain) => {
214
+ const derivationPath =
215
+ typeof derivationPathMapOrIndex === "object" && derivationPathMapOrIndex[chain]
216
+ ? derivationPathToString(derivationPathMapOrIndex[chain])
217
+ : `${DerivationPath[chain]}/${derivationPathMapOrIndex}`;
218
+
254
219
  const { address, walletMethods } = await getWalletMethodsForChain({
255
- index,
220
+ derivationPath,
256
221
  chain,
257
- api: apis[chain as Chain.Avalanche],
222
+ api: apis[chain],
258
223
  rpcUrl: rpcUrls[chain],
259
224
  covalentApiKey,
260
225
  ethplorerApiKey,