@swapkit/wallet-keystore 1.0.0-rc.13 → 1.0.0-rc.130
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/index.js +18397 -0
- package/dist/index.js.map +13 -0
- package/package.json +32 -53
- package/src/helpers.ts +66 -84
- package/src/index.ts +2 -2
- package/src/keystore.ts +115 -130
- package/LICENSE +0 -201
- package/dist/index.cjs +0 -2069
- package/dist/index.d.ts +0 -62
- package/dist/index.es.js +0 -17692
package/package.json
CHANGED
|
@@ -1,38 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
-
"author": "swapkit-oss-team",
|
|
3
2
|
"dependencies": {
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"@swapkit/types": "1.0.0-rc.5"
|
|
3
|
+
"@scure/bip39": "1.3.0",
|
|
4
|
+
"@swapkit/helpers": "1.0.0-rc.95",
|
|
5
|
+
"blakejs": "1.2.1"
|
|
8
6
|
},
|
|
9
|
-
"description": "SwapKit
|
|
7
|
+
"description": "SwapKit Wallet - Keystore",
|
|
10
8
|
"devDependencies": {
|
|
11
9
|
"@rollup/plugin-node-resolve": "15.2.3",
|
|
12
|
-
"@
|
|
13
|
-
"@
|
|
14
|
-
"@
|
|
15
|
-
"@
|
|
16
|
-
"
|
|
17
|
-
"bitcoinjs-lib": "5.2.0",
|
|
18
|
-
"ethers": "6.7.1",
|
|
19
|
-
"vite": "4.4.9",
|
|
20
|
-
"vitest": "0.34.4",
|
|
21
|
-
"@internal/config": "0.0.0-internal.0",
|
|
22
|
-
"@swapkit/helpers": "1.0.0-rc.7",
|
|
23
|
-
"@swapkit/toolbox-cosmos": "1.0.0-rc.9",
|
|
24
|
-
"@swapkit/toolbox-evm": "1.0.0-rc.8",
|
|
25
|
-
"@swapkit/toolbox-utxo": "1.0.0-rc.8"
|
|
26
|
-
},
|
|
27
|
-
"eslintConfig": {
|
|
28
|
-
"extends": "../../../internal/eslint-config"
|
|
29
|
-
},
|
|
30
|
-
"exports": {
|
|
31
|
-
".": {
|
|
32
|
-
"import": "./dist/index.es.js",
|
|
33
|
-
"require": "./dist/index.cjs",
|
|
34
|
-
"types": "./dist/index.d.ts"
|
|
35
|
-
}
|
|
10
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.116",
|
|
11
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.101",
|
|
12
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.30",
|
|
13
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.104",
|
|
14
|
+
"bun-types": "1.1.2"
|
|
36
15
|
},
|
|
37
16
|
"files": [
|
|
38
17
|
"src/",
|
|
@@ -40,31 +19,31 @@
|
|
|
40
19
|
],
|
|
41
20
|
"homepage": "https://github.com/thorswap/SwapKit",
|
|
42
21
|
"license": "Apache-2.0",
|
|
43
|
-
"main": "./dist/index.
|
|
44
|
-
"module": "./dist/index.es.js",
|
|
22
|
+
"main": "./dist/index.js",
|
|
45
23
|
"name": "@swapkit/wallet-keystore",
|
|
46
24
|
"peerDependencies": {
|
|
47
|
-
"@scure/bip39": "1.
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"@swapkit/
|
|
51
|
-
"@swapkit/toolbox-
|
|
52
|
-
"@swapkit/toolbox-
|
|
53
|
-
"
|
|
54
|
-
},
|
|
55
|
-
"publishConfig": {
|
|
56
|
-
"access": "public"
|
|
25
|
+
"@scure/bip39": "1.3.0",
|
|
26
|
+
"@swapkit/helpers": "1.0.0-rc.95",
|
|
27
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.116",
|
|
28
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.101",
|
|
29
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.30",
|
|
30
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.104",
|
|
31
|
+
"blakejs": "1.2.1"
|
|
57
32
|
},
|
|
58
33
|
"react-native": "./src/index.ts",
|
|
59
|
-
"repository":
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/thorswap/SwapKit.git"
|
|
37
|
+
},
|
|
63
38
|
"scripts": {
|
|
64
|
-
"build": "
|
|
65
|
-
"clean": "rm -rf
|
|
66
|
-
"lint": "
|
|
67
|
-
"test": "echo '
|
|
68
|
-
"test:coverage": "echo '
|
|
69
|
-
|
|
70
|
-
}
|
|
39
|
+
"build": "bun run ./build.ts",
|
|
40
|
+
"clean": "rm -rf .turbo dist node_modules *.tsbuildinfo",
|
|
41
|
+
"lint": "biome check --apply ./src",
|
|
42
|
+
"test": "echo 'bun test'",
|
|
43
|
+
"test:coverage": "echo 'bun test --coverage'",
|
|
44
|
+
"type-check": "tsc --noEmit"
|
|
45
|
+
},
|
|
46
|
+
"type": "module",
|
|
47
|
+
"types": "./src/index.ts",
|
|
48
|
+
"version": "1.0.0-rc.130"
|
|
49
|
+
}
|
package/src/helpers.ts
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
6
|
-
|
|
7
|
-
const cipher = 'aes-128-ctr';
|
|
8
|
-
const kdf = 'pbkdf2';
|
|
9
|
-
const prf = 'hmac-sha256';
|
|
10
|
-
const dklen = 32;
|
|
11
|
-
const c = 262144;
|
|
12
|
-
const hashFunction = 'sha256';
|
|
13
|
-
const meta = 'xchain-keystore';
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { generateMnemonic, validateMnemonic } from "@scure/bip39";
|
|
3
|
+
import { wordlist } from "@scure/bip39/wordlists/english";
|
|
4
|
+
import { blake2bFinal, blake2bInit, blake2bUpdate } from "blakejs";
|
|
14
5
|
|
|
15
6
|
export type Keystore = {
|
|
16
7
|
crypto: {
|
|
@@ -28,7 +19,6 @@ export type Keystore = {
|
|
|
28
19
|
};
|
|
29
20
|
mac: string;
|
|
30
21
|
};
|
|
31
|
-
id: string;
|
|
32
22
|
version: number;
|
|
33
23
|
meta: string;
|
|
34
24
|
};
|
|
@@ -36,114 +26,106 @@ export type Keystore = {
|
|
|
36
26
|
/**
|
|
37
27
|
* taken from `foundry-primitives` and modified
|
|
38
28
|
*/
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
const _isNode = () => typeof window === 'undefined';
|
|
29
|
+
const blake256 = (initData: Buffer | string): string => {
|
|
30
|
+
let data = initData;
|
|
42
31
|
|
|
43
|
-
/**
|
|
44
|
-
* Gets data's 256 bit blake hash.
|
|
45
|
-
* @param data buffer or hexadecimal string
|
|
46
|
-
* @returns 32 byte hexadecimal string
|
|
47
|
-
*/
|
|
48
|
-
export const blake256 = (data: Buffer | string): string => {
|
|
49
32
|
if (!(data instanceof Buffer)) {
|
|
50
|
-
data = Buffer.from(data,
|
|
33
|
+
data = Buffer.from(data, "hex");
|
|
51
34
|
}
|
|
35
|
+
|
|
52
36
|
const context = blake2bInit(32);
|
|
53
37
|
blake2bUpdate(context, data);
|
|
54
|
-
|
|
38
|
+
|
|
39
|
+
return Array.from(blake2bFinal(context))
|
|
40
|
+
.map((byte) => (byte < 0x10 ? `0${byte.toString(16)}` : byte.toString(16)))
|
|
41
|
+
.join("");
|
|
55
42
|
};
|
|
56
43
|
|
|
57
|
-
const pbkdf2Async =
|
|
58
|
-
passphrase: string | Buffer
|
|
59
|
-
salt: string | Buffer
|
|
44
|
+
const pbkdf2Async = (
|
|
45
|
+
passphrase: string | Buffer,
|
|
46
|
+
salt: string | Buffer,
|
|
60
47
|
iterations: number,
|
|
61
48
|
keylen: number,
|
|
62
49
|
digest: string,
|
|
63
|
-
) =>
|
|
64
|
-
|
|
65
|
-
crypto.pbkdf2(passphrase, salt, iterations, keylen, digest, (
|
|
66
|
-
if (
|
|
67
|
-
reject(
|
|
50
|
+
) =>
|
|
51
|
+
new Promise<Buffer>((resolve, reject) => {
|
|
52
|
+
crypto.pbkdf2(passphrase, salt, iterations, keylen, digest, (error, drived) => {
|
|
53
|
+
if (error) {
|
|
54
|
+
reject(error);
|
|
68
55
|
} else {
|
|
69
56
|
resolve(drived);
|
|
70
57
|
}
|
|
71
58
|
});
|
|
72
59
|
});
|
|
73
|
-
};
|
|
74
60
|
|
|
75
61
|
export const encryptToKeyStore = async (phrase: string, password: string) => {
|
|
76
|
-
const ID = _isNode() ? require('uuid').v4() : uuidv4();
|
|
77
62
|
const salt = crypto.randomBytes(32);
|
|
78
63
|
const iv = crypto.randomBytes(16);
|
|
79
|
-
const kdfParams = {
|
|
80
|
-
|
|
81
|
-
dklen,
|
|
82
|
-
salt: salt.toString('hex'),
|
|
83
|
-
c,
|
|
84
|
-
};
|
|
85
|
-
const cipherParams = {
|
|
86
|
-
iv: iv.toString('hex'),
|
|
87
|
-
};
|
|
64
|
+
const kdfParams = { c: 262144, prf: "hmac-sha256", dklen: 32, salt: salt.toString("hex") };
|
|
65
|
+
const cipher = "aes-128-ctr";
|
|
88
66
|
|
|
89
67
|
const derivedKey = await pbkdf2Async(
|
|
90
68
|
Buffer.from(password),
|
|
91
69
|
salt,
|
|
92
70
|
kdfParams.c,
|
|
93
71
|
kdfParams.dklen,
|
|
94
|
-
|
|
72
|
+
"sha256",
|
|
95
73
|
);
|
|
96
|
-
const cipherIV = crypto.createCipheriv(cipher, derivedKey.
|
|
97
|
-
const
|
|
98
|
-
cipherIV.update(Buffer.from(phrase,
|
|
74
|
+
const cipherIV = crypto.createCipheriv(cipher, derivedKey.subarray(0, 16), iv);
|
|
75
|
+
const ciphertext = Buffer.concat([
|
|
76
|
+
cipherIV.update(Buffer.from(phrase, "utf8")),
|
|
99
77
|
cipherIV.final(),
|
|
100
78
|
]);
|
|
101
|
-
const mac = blake256(Buffer.concat([derivedKey.slice(16, 32), Buffer.from(cipherText)]));
|
|
102
|
-
|
|
103
|
-
const cryptoStruct = {
|
|
104
|
-
cipher: cipher,
|
|
105
|
-
ciphertext: cipherText.toString('hex'),
|
|
106
|
-
cipherparams: cipherParams,
|
|
107
|
-
kdf: kdf,
|
|
108
|
-
kdfparams: kdfParams,
|
|
109
|
-
mac: mac,
|
|
110
|
-
};
|
|
111
79
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
id: ID,
|
|
80
|
+
return {
|
|
81
|
+
meta: "xchain-keystore",
|
|
115
82
|
version: 1,
|
|
116
|
-
|
|
83
|
+
crypto: {
|
|
84
|
+
cipher,
|
|
85
|
+
cipherparams: { iv: iv.toString("hex") },
|
|
86
|
+
ciphertext: ciphertext.toString("hex"),
|
|
87
|
+
kdf: "pbkdf2",
|
|
88
|
+
kdfparams: kdfParams,
|
|
89
|
+
mac: blake256(Buffer.concat([derivedKey.subarray(16, 32), Buffer.from(ciphertext)])),
|
|
90
|
+
},
|
|
117
91
|
};
|
|
92
|
+
};
|
|
118
93
|
|
|
119
|
-
|
|
94
|
+
export const generatePhrase = (size: 12 | 24 = 12) => {
|
|
95
|
+
return generateMnemonic(wordlist, size === 12 ? 128 : 256);
|
|
120
96
|
};
|
|
121
97
|
|
|
122
|
-
export const
|
|
123
|
-
|
|
124
|
-
return generateMnemonic(wordlist, entropy);
|
|
98
|
+
export const validatePhrase = (phrase: string) => {
|
|
99
|
+
return validateMnemonic(phrase, wordlist);
|
|
125
100
|
};
|
|
126
101
|
|
|
127
102
|
export const decryptFromKeystore = async (keystore: Keystore, password: string) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
103
|
+
switch (keystore.version) {
|
|
104
|
+
case 1: {
|
|
105
|
+
const kdfparams = keystore.crypto.kdfparams;
|
|
106
|
+
const derivedKey = await pbkdf2Async(
|
|
107
|
+
Buffer.from(password),
|
|
108
|
+
Buffer.from(kdfparams.salt, "hex"),
|
|
109
|
+
kdfparams.c,
|
|
110
|
+
kdfparams.dklen,
|
|
111
|
+
"sha256",
|
|
112
|
+
);
|
|
136
113
|
|
|
137
|
-
|
|
138
|
-
|
|
114
|
+
const ciphertext = Buffer.from(keystore.crypto.ciphertext, "hex");
|
|
115
|
+
const mac = blake256(Buffer.concat([derivedKey.subarray(16, 32), ciphertext]));
|
|
139
116
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
117
|
+
if (mac !== keystore.crypto.mac) throw new Error("Invalid password");
|
|
118
|
+
const decipher = crypto.createDecipheriv(
|
|
119
|
+
keystore.crypto.cipher,
|
|
120
|
+
derivedKey.subarray(0, 16),
|
|
121
|
+
Buffer.from(keystore.crypto.cipherparams.iv, "hex"),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const phrase = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
125
|
+
return phrase.toString("utf8");
|
|
126
|
+
}
|
|
146
127
|
|
|
147
|
-
|
|
148
|
-
|
|
128
|
+
default:
|
|
129
|
+
throw new Error("Unsupported keystore version");
|
|
130
|
+
}
|
|
149
131
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export { keystoreWallet } from
|
|
1
|
+
export * from "./helpers.ts";
|
|
2
|
+
export { keystoreWallet } from "./keystore.ts";
|
package/src/keystore.ts
CHANGED
|
@@ -1,22 +1,33 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
Chain,
|
|
3
|
+
type ConnectWalletParams,
|
|
4
|
+
DerivationPath,
|
|
5
|
+
WalletOption,
|
|
6
|
+
type Witness,
|
|
7
|
+
setRequestClientConfig,
|
|
8
|
+
} from "@swapkit/helpers";
|
|
2
9
|
import type {
|
|
10
|
+
BinanceToolboxType,
|
|
11
|
+
DepositParam,
|
|
12
|
+
ThorchainToolboxType,
|
|
13
|
+
TransferParams,
|
|
14
|
+
} from "@swapkit/toolbox-cosmos";
|
|
15
|
+
import type {
|
|
16
|
+
Psbt,
|
|
3
17
|
TransactionType,
|
|
4
18
|
UTXOTransferParams,
|
|
5
19
|
UTXOWalletTransferParams,
|
|
6
|
-
} from
|
|
7
|
-
import type { ConnectWalletParams, Witness } from '@swapkit/types';
|
|
8
|
-
import { Chain, DerivationPath, WalletOption } from '@swapkit/types';
|
|
9
|
-
import type { Psbt } from 'bitcoinjs-lib';
|
|
20
|
+
} from "@swapkit/toolbox-utxo";
|
|
10
21
|
|
|
11
22
|
type KeystoreOptions = {
|
|
12
23
|
ethplorerApiKey?: string;
|
|
13
|
-
|
|
24
|
+
blockchairApiKey?: string;
|
|
14
25
|
covalentApiKey?: string;
|
|
15
26
|
stagenet?: boolean;
|
|
16
27
|
};
|
|
17
28
|
|
|
18
29
|
type Params = KeystoreOptions & {
|
|
19
|
-
api?:
|
|
30
|
+
api?: Todo;
|
|
20
31
|
rpcUrl?: string;
|
|
21
32
|
chain: Chain;
|
|
22
33
|
phrase: string;
|
|
@@ -30,51 +41,51 @@ const getWalletMethodsForChain = async ({
|
|
|
30
41
|
phrase,
|
|
31
42
|
ethplorerApiKey,
|
|
32
43
|
covalentApiKey,
|
|
33
|
-
|
|
44
|
+
blockchairApiKey,
|
|
34
45
|
index,
|
|
35
46
|
stagenet,
|
|
36
47
|
}: Params) => {
|
|
37
|
-
const derivationPath = `${DerivationPath[chain]}/${index}`;
|
|
48
|
+
const derivationPath = `${DerivationPath[chain] as string}/${index}`;
|
|
38
49
|
|
|
39
50
|
switch (chain) {
|
|
40
51
|
case Chain.BinanceSmartChain:
|
|
52
|
+
case Chain.Arbitrum:
|
|
53
|
+
case Chain.Optimism:
|
|
54
|
+
case Chain.Polygon:
|
|
41
55
|
case Chain.Avalanche:
|
|
42
56
|
case Chain.Ethereum: {
|
|
43
57
|
if (chain === Chain.Ethereum && !ethplorerApiKey) {
|
|
44
|
-
throw new Error(
|
|
58
|
+
throw new Error("Ethplorer API key not found");
|
|
59
|
+
// biome-ignore lint/style/noUselessElse: This is a valid use case
|
|
45
60
|
} else if (!covalentApiKey) {
|
|
46
|
-
throw new Error(
|
|
61
|
+
throw new Error("Covalent API key not found");
|
|
47
62
|
}
|
|
48
63
|
|
|
49
|
-
const { HDNodeWallet } = await import(
|
|
50
|
-
const { getProvider, ETHToolbox, AVAXToolbox, BSCToolbox } = await import(
|
|
51
|
-
'@swapkit/toolbox-evm'
|
|
52
|
-
);
|
|
64
|
+
const { HDNodeWallet, getProvider, getToolboxByChain } = await import("@swapkit/toolbox-evm");
|
|
53
65
|
|
|
54
66
|
const provider = getProvider(chain, rpcUrl);
|
|
55
67
|
const wallet = HDNodeWallet.fromPhrase(phrase).connect(provider);
|
|
56
68
|
const params = { api, provider, signer: wallet };
|
|
57
69
|
|
|
58
|
-
const toolbox =
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
: BSCToolbox({ ...params, covalentApiKey: covalentApiKey! });
|
|
70
|
+
const toolbox = getToolboxByChain(chain)({
|
|
71
|
+
...params,
|
|
72
|
+
covalentApiKey,
|
|
73
|
+
ethplorerApiKey: ethplorerApiKey as string,
|
|
74
|
+
});
|
|
64
75
|
|
|
65
76
|
return {
|
|
66
77
|
address: wallet.address,
|
|
67
|
-
walletMethods: {
|
|
68
|
-
...toolbox,
|
|
69
|
-
getAddress: () => wallet.address,
|
|
70
|
-
},
|
|
78
|
+
walletMethods: { ...toolbox },
|
|
71
79
|
};
|
|
72
80
|
}
|
|
73
81
|
|
|
74
82
|
case Chain.BitcoinCash: {
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
83
|
+
const { BCHToolbox } = await import("@swapkit/toolbox-utxo");
|
|
84
|
+
const toolbox = BCHToolbox({
|
|
85
|
+
rpcUrl,
|
|
86
|
+
apiKey: blockchairApiKey,
|
|
87
|
+
apiClient: api,
|
|
88
|
+
});
|
|
78
89
|
const keys = await toolbox.createKeysForPath({ phrase, derivationPath });
|
|
79
90
|
const address = toolbox.getAddressFromKeys(keys);
|
|
80
91
|
|
|
@@ -91,7 +102,6 @@ const getWalletMethodsForChain = async ({
|
|
|
91
102
|
|
|
92
103
|
const walletMethods = {
|
|
93
104
|
...toolbox,
|
|
94
|
-
getAddress: () => address,
|
|
95
105
|
transfer: (
|
|
96
106
|
params: UTXOWalletTransferParams<
|
|
97
107
|
Awaited<ReturnType<typeof toolbox.buildBCHTx>>,
|
|
@@ -104,126 +114,100 @@ const getWalletMethodsForChain = async ({
|
|
|
104
114
|
}
|
|
105
115
|
|
|
106
116
|
case Chain.Bitcoin:
|
|
117
|
+
case Chain.Dash:
|
|
107
118
|
case Chain.Dogecoin:
|
|
108
119
|
case Chain.Litecoin: {
|
|
109
|
-
const
|
|
120
|
+
const { getToolboxByChain } = await import("@swapkit/toolbox-utxo");
|
|
110
121
|
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
: chain === Chain.Litecoin
|
|
117
|
-
? LTCToolbox(params)
|
|
118
|
-
: DOGEToolbox(params);
|
|
122
|
+
const toolbox = getToolboxByChain(chain)({
|
|
123
|
+
rpcUrl,
|
|
124
|
+
apiKey: blockchairApiKey,
|
|
125
|
+
apiClient: api,
|
|
126
|
+
});
|
|
119
127
|
|
|
120
128
|
const keys = await toolbox.createKeysForPath({ phrase, derivationPath });
|
|
121
129
|
const address = toolbox.getAddressFromKeys(keys);
|
|
122
130
|
|
|
123
|
-
const signTransaction = async (psbt: Psbt) => {
|
|
124
|
-
psbt.signAllInputs(keys);
|
|
125
|
-
|
|
126
|
-
return psbt;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
131
|
return {
|
|
130
132
|
address,
|
|
131
133
|
walletMethods: {
|
|
132
134
|
...toolbox,
|
|
133
|
-
getAddress: () => address,
|
|
134
135
|
transfer: (params: UTXOTransferParams) =>
|
|
135
|
-
toolbox.transfer({
|
|
136
|
+
toolbox.transfer({
|
|
137
|
+
...params,
|
|
138
|
+
from: address,
|
|
139
|
+
signTransaction: (psbt: Psbt) => {
|
|
140
|
+
psbt.signAllInputs(keys);
|
|
141
|
+
return psbt;
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
136
144
|
},
|
|
137
145
|
};
|
|
138
146
|
}
|
|
139
147
|
|
|
140
|
-
case Chain.Binance:
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const transfer = ({ assetValue, recipient, memo }: TransferParams) =>
|
|
147
|
-
toolbox.transfer({
|
|
148
|
-
from: address,
|
|
149
|
-
recipient,
|
|
150
|
-
assetValue,
|
|
151
|
-
privkey,
|
|
152
|
-
memo,
|
|
153
|
-
});
|
|
148
|
+
case Chain.Binance:
|
|
149
|
+
case Chain.Cosmos:
|
|
150
|
+
case Chain.Kujira:
|
|
151
|
+
case Chain.Maya:
|
|
152
|
+
case Chain.THORChain: {
|
|
153
|
+
const { getToolboxByChain } = await import("@swapkit/toolbox-cosmos");
|
|
154
154
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
155
|
+
const toolbox = getToolboxByChain(chain)({ server: api, stagenet });
|
|
156
|
+
const additionalParams =
|
|
157
|
+
chain === Chain.Binance
|
|
158
|
+
? {
|
|
159
|
+
privkey: await (toolbox as BinanceToolboxType).createPrivateKeyFromPhrase(phrase),
|
|
160
|
+
}
|
|
161
|
+
: { signer: await toolbox.getSigner(phrase) };
|
|
160
162
|
|
|
161
|
-
case Chain.Cosmos: {
|
|
162
|
-
const { GaiaToolbox } = await import('@swapkit/toolbox-cosmos');
|
|
163
|
-
const toolbox = GaiaToolbox({ server: api });
|
|
164
|
-
const signer = await toolbox.getSigner(phrase);
|
|
165
163
|
const address = await toolbox.getAddressFromMnemonic(phrase);
|
|
166
164
|
|
|
167
|
-
const transfer = ({ assetValue, recipient, memo }: TransferParams) =>
|
|
165
|
+
const transfer = async ({ assetValue, recipient, memo }: TransferParams) =>
|
|
168
166
|
toolbox.transfer({
|
|
169
167
|
from: address,
|
|
170
168
|
recipient,
|
|
171
|
-
signer,
|
|
172
169
|
assetValue,
|
|
173
170
|
memo,
|
|
171
|
+
...additionalParams,
|
|
174
172
|
});
|
|
175
173
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
174
|
+
const deposit =
|
|
175
|
+
"deposit" in toolbox
|
|
176
|
+
? ({ assetValue, memo }: DepositParam) => {
|
|
177
|
+
return (toolbox as ThorchainToolboxType).deposit({
|
|
178
|
+
assetValue,
|
|
179
|
+
memo,
|
|
180
|
+
from: address,
|
|
181
|
+
...additionalParams,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
: undefined;
|
|
185
|
+
|
|
186
|
+
const signMessage = async (message: string) => {
|
|
187
|
+
const privateKey = await toolbox.createPrivateKeyFromPhrase(phrase);
|
|
188
|
+
return (toolbox as ThorchainToolboxType).signMessage(privateKey, message);
|
|
179
189
|
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
case Chain.Kujira: {
|
|
183
|
-
const { KujiraToolbox } = await import('@swapkit/toolbox-cosmos');
|
|
184
|
-
const toolbox = KujiraToolbox({ server: api });
|
|
185
|
-
const signer = await toolbox.getSigner(phrase);
|
|
186
|
-
const address = await toolbox.getAddressFromMnemonic(phrase);
|
|
187
|
-
|
|
188
|
-
const transfer = ({ assetValue, recipient, memo }: TransferParams) =>
|
|
189
|
-
toolbox.transfer({
|
|
190
|
-
from: address,
|
|
191
|
-
recipient,
|
|
192
|
-
signer,
|
|
193
|
-
assetValue,
|
|
194
|
-
memo,
|
|
195
|
-
});
|
|
196
190
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
191
|
+
const walletMethods = {
|
|
192
|
+
...toolbox,
|
|
193
|
+
deposit,
|
|
194
|
+
transfer,
|
|
195
|
+
signMessage,
|
|
200
196
|
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
case Chain.Maya:
|
|
204
|
-
case Chain.THORChain: {
|
|
205
|
-
const { MayaToolbox, ThorchainToolbox } = await import('@swapkit/toolbox-cosmos');
|
|
206
|
-
const toolbox =
|
|
207
|
-
chain === Chain.THORChain ? ThorchainToolbox({ stagenet }) : MayaToolbox({ stagenet });
|
|
208
|
-
const address = await toolbox.getAddressFromMnemonic(phrase);
|
|
209
|
-
const signer = await toolbox.getSigner(phrase);
|
|
210
197
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
from: address,
|
|
214
|
-
recipient,
|
|
215
|
-
signer,
|
|
216
|
-
assetValue,
|
|
217
|
-
memo,
|
|
218
|
-
});
|
|
198
|
+
return { address, walletMethods };
|
|
199
|
+
}
|
|
219
200
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
201
|
+
case Chain.Polkadot:
|
|
202
|
+
case Chain.Chainflip: {
|
|
203
|
+
const { Network, getToolboxByChain, createKeyring } = await import(
|
|
204
|
+
"@swapkit/toolbox-substrate"
|
|
205
|
+
);
|
|
223
206
|
|
|
224
|
-
const
|
|
207
|
+
const signer = await createKeyring(phrase, Network[chain].prefix);
|
|
208
|
+
const toolbox = await getToolboxByChain(chain, { signer });
|
|
225
209
|
|
|
226
|
-
return { address, walletMethods };
|
|
210
|
+
return { address: signer.address, walletMethods: toolbox };
|
|
227
211
|
}
|
|
228
212
|
|
|
229
213
|
default:
|
|
@@ -231,14 +215,15 @@ const getWalletMethodsForChain = async ({
|
|
|
231
215
|
}
|
|
232
216
|
};
|
|
233
217
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
218
|
+
function connectKeystore({
|
|
219
|
+
addChain,
|
|
220
|
+
apis,
|
|
221
|
+
rpcUrls,
|
|
222
|
+
config: { thorswapApiKey, covalentApiKey, ethplorerApiKey, blockchairApiKey, stagenet },
|
|
223
|
+
}: ConnectWalletParams) {
|
|
224
|
+
return async function connectKeystore(chains: Chain[], phrase: string, index = 0) {
|
|
225
|
+
setRequestClientConfig({ apiKey: thorswapApiKey });
|
|
226
|
+
|
|
242
227
|
const promises = chains.map(async (chain) => {
|
|
243
228
|
const { address, walletMethods } = await getWalletMethodsForChain({
|
|
244
229
|
index,
|
|
@@ -248,14 +233,16 @@ const connectKeystore =
|
|
|
248
233
|
covalentApiKey,
|
|
249
234
|
ethplorerApiKey,
|
|
250
235
|
phrase,
|
|
251
|
-
|
|
236
|
+
blockchairApiKey,
|
|
252
237
|
stagenet,
|
|
253
238
|
});
|
|
254
239
|
|
|
255
240
|
addChain({
|
|
256
241
|
chain,
|
|
257
|
-
|
|
258
|
-
|
|
242
|
+
address,
|
|
243
|
+
balance: [],
|
|
244
|
+
walletType: WalletOption.KEYSTORE,
|
|
245
|
+
...walletMethods,
|
|
259
246
|
});
|
|
260
247
|
});
|
|
261
248
|
|
|
@@ -263,8 +250,6 @@ const connectKeystore =
|
|
|
263
250
|
|
|
264
251
|
return true;
|
|
265
252
|
};
|
|
253
|
+
}
|
|
266
254
|
|
|
267
|
-
export const keystoreWallet = {
|
|
268
|
-
connectMethodName: 'connectKeystore' as const,
|
|
269
|
-
connect: connectKeystore,
|
|
270
|
-
};
|
|
255
|
+
export const keystoreWallet = { connectKeystore } as const;
|