@ecency/wallets 1.0.18 → 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/dist/ecency-wallets.es.d.ts +1 -1
- package/dist/ecency-wallets.es.js +282 -256
- package/package.json +1 -1
|
@@ -75,7 +75,7 @@ export declare function useHiveKeysQuery(username: string): UseQueryResult<Ecenc
|
|
|
75
75
|
|
|
76
76
|
export declare function useImportWallet(username: string, currency: EcencyWalletCurrency): UseMutationResult< {
|
|
77
77
|
privateKey: string;
|
|
78
|
-
address:
|
|
78
|
+
address: any;
|
|
79
79
|
publicKey: string;
|
|
80
80
|
}, Error, Payload_2, unknown>;
|
|
81
81
|
|
|
@@ -1,64 +1,76 @@
|
|
|
1
|
-
import { useCallback
|
|
2
|
-
import { useQuery
|
|
3
|
-
import { BtcWallet
|
|
4
|
-
import { EthWallet
|
|
5
|
-
import { TrxWallet
|
|
6
|
-
import { TonWallet
|
|
7
|
-
import { SolWallet
|
|
8
|
-
import { AtomWallet
|
|
9
|
-
import { AptosWallet
|
|
10
|
-
import
|
|
11
|
-
import { LRUCache
|
|
12
|
-
import { PrivateKey
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useQuery, useQueryClient, useMutation } from "@tanstack/react-query";
|
|
3
|
+
import { BtcWallet } from "@okxweb3/coin-bitcoin";
|
|
4
|
+
import { EthWallet } from "@okxweb3/coin-ethereum";
|
|
5
|
+
import { TrxWallet } from "@okxweb3/coin-tron";
|
|
6
|
+
import { TonWallet } from "@okxweb3/coin-ton";
|
|
7
|
+
import { SolWallet } from "@okxweb3/coin-solana";
|
|
8
|
+
import { AtomWallet } from "@okxweb3/coin-cosmos";
|
|
9
|
+
import { AptosWallet } from "@okxweb3/coin-aptos";
|
|
10
|
+
import bip39, { mnemonicToSeedSync } from "bip39";
|
|
11
|
+
import { LRUCache } from "lru-cache";
|
|
12
|
+
import { PrivateKey } from "@hiveio/dhive";
|
|
13
|
+
var EcencyWalletCurrency = /* @__PURE__ */ ((EcencyWalletCurrency2) => {
|
|
14
|
+
EcencyWalletCurrency2["BTC"] = "btc";
|
|
15
|
+
EcencyWalletCurrency2["ETH"] = "eth";
|
|
16
|
+
EcencyWalletCurrency2["APT"] = "aptos";
|
|
17
|
+
EcencyWalletCurrency2["ATOM"] = "cosmos";
|
|
18
|
+
EcencyWalletCurrency2["TON"] = "ton";
|
|
19
|
+
EcencyWalletCurrency2["TRON"] = "tron";
|
|
20
|
+
EcencyWalletCurrency2["SOL"] = "solana";
|
|
21
|
+
return EcencyWalletCurrency2;
|
|
22
|
+
})(EcencyWalletCurrency || {});
|
|
23
|
+
function delay(ms) {
|
|
24
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
16
25
|
}
|
|
17
|
-
function
|
|
18
|
-
switch (
|
|
19
|
-
case
|
|
20
|
-
return new
|
|
21
|
-
case
|
|
22
|
-
return new
|
|
23
|
-
case
|
|
24
|
-
return new
|
|
25
|
-
case
|
|
26
|
-
return new
|
|
27
|
-
case
|
|
28
|
-
return new
|
|
29
|
-
case
|
|
30
|
-
return new
|
|
31
|
-
case
|
|
32
|
-
return new
|
|
26
|
+
function getWallet(currency) {
|
|
27
|
+
switch (currency) {
|
|
28
|
+
case EcencyWalletCurrency.BTC:
|
|
29
|
+
return new BtcWallet();
|
|
30
|
+
case EcencyWalletCurrency.ETH:
|
|
31
|
+
return new EthWallet();
|
|
32
|
+
case EcencyWalletCurrency.TRON:
|
|
33
|
+
return new TrxWallet();
|
|
34
|
+
case EcencyWalletCurrency.TON:
|
|
35
|
+
return new TonWallet();
|
|
36
|
+
case EcencyWalletCurrency.SOL:
|
|
37
|
+
return new SolWallet();
|
|
38
|
+
case EcencyWalletCurrency.ATOM:
|
|
39
|
+
return new AtomWallet();
|
|
40
|
+
case EcencyWalletCurrency.APT:
|
|
41
|
+
return new AptosWallet();
|
|
33
42
|
default:
|
|
34
|
-
return;
|
|
43
|
+
return void 0;
|
|
35
44
|
}
|
|
36
45
|
}
|
|
37
|
-
function
|
|
38
|
-
return
|
|
46
|
+
function mnemonicToSeedBip39(value) {
|
|
47
|
+
return mnemonicToSeedSync(value).toString("hex");
|
|
39
48
|
}
|
|
40
|
-
function
|
|
41
|
-
return
|
|
42
|
-
queryKey: ["ecency-wallets", "external-wallet-balance",
|
|
49
|
+
function useGetExternalWalletBalanceQuery(currency, address) {
|
|
50
|
+
return useQuery({
|
|
51
|
+
queryKey: ["ecency-wallets", "external-wallet-balance", currency, address],
|
|
43
52
|
queryFn: async () => {
|
|
44
|
-
switch (
|
|
45
|
-
case
|
|
46
|
-
const
|
|
47
|
-
`https://mempool.space/api/address/${
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
53
|
+
switch (currency) {
|
|
54
|
+
case EcencyWalletCurrency.BTC:
|
|
55
|
+
const btcResponse = await fetch(
|
|
56
|
+
`https://mempool.space/api/address/${address}`
|
|
57
|
+
);
|
|
58
|
+
const btcResponseData = await btcResponse.json();
|
|
59
|
+
return (btcResponseData.chain_stats.funded_txo_sum - btcResponseData.chain_stats.spent_txo_sum) / 1e8;
|
|
60
|
+
case EcencyWalletCurrency.ETH:
|
|
61
|
+
const ethResponse = await fetch("https://eth.llamarpc.com", {
|
|
52
62
|
method: "POST",
|
|
53
63
|
body: JSON.stringify({
|
|
54
64
|
jsonrpc: "2.0",
|
|
55
65
|
id: "1",
|
|
56
66
|
method: "eth_getBalance",
|
|
57
|
-
params: [
|
|
67
|
+
params: [address, "latest"]
|
|
58
68
|
})
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
return
|
|
69
|
+
});
|
|
70
|
+
const ethResponseData = await ethResponse.json();
|
|
71
|
+
return +ethResponseData.result / 1e18;
|
|
72
|
+
case EcencyWalletCurrency.SOL:
|
|
73
|
+
const solResponse = await fetch(
|
|
62
74
|
"https://api.mainnet-beta.solana.com",
|
|
63
75
|
{
|
|
64
76
|
method: "POST",
|
|
@@ -66,193 +78,222 @@ function ce(e, t) {
|
|
|
66
78
|
jsonrpc: "2.0",
|
|
67
79
|
id: "1",
|
|
68
80
|
method: "getBalance",
|
|
69
|
-
params: [
|
|
81
|
+
params: [address]
|
|
70
82
|
})
|
|
71
83
|
}
|
|
72
|
-
)).json()).result.value / 1e9;
|
|
73
|
-
case o.TRON:
|
|
74
|
-
return (await (await fetch(
|
|
75
|
-
`https://api.trongrid.io/v1/accounts/${t}`
|
|
76
|
-
)).json()).data[0].balance / 1e6;
|
|
77
|
-
case o.TON:
|
|
78
|
-
return (await (await fetch(
|
|
79
|
-
`https://tonapi.io/v1/blockchain/getAccount?account=${t}`
|
|
80
|
-
)).json()).balance / 1e9;
|
|
81
|
-
case o.APT:
|
|
82
|
-
const y = (await (await fetch(
|
|
83
|
-
`https://fullnode.mainnet.aptoslabs.com/v1/accounts/${t}/resources`
|
|
84
|
-
)).json()).find(
|
|
85
|
-
(v) => v.type.includes("coin::CoinStore")
|
|
86
84
|
);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
const solResponseData = await solResponse.json();
|
|
86
|
+
return solResponseData.result.value / 1e9;
|
|
87
|
+
case EcencyWalletCurrency.TRON:
|
|
88
|
+
const tronResponse = await fetch(
|
|
89
|
+
`https://api.trongrid.io/v1/accounts/${address}`
|
|
90
|
+
);
|
|
91
|
+
const tronResponseData = await tronResponse.json();
|
|
92
|
+
return tronResponseData.data[0].balance / 1e6;
|
|
93
|
+
case EcencyWalletCurrency.TON:
|
|
94
|
+
const tonResponse = await fetch(
|
|
95
|
+
`https://tonapi.io/v1/blockchain/getAccount?account=${address}`
|
|
96
|
+
);
|
|
97
|
+
const tonResponseData = await tonResponse.json();
|
|
98
|
+
return tonResponseData.balance / 1e9;
|
|
99
|
+
case EcencyWalletCurrency.APT:
|
|
100
|
+
const aptResponse = await fetch(
|
|
101
|
+
`https://fullnode.mainnet.aptoslabs.com/v1/accounts/${address}/resources`
|
|
102
|
+
);
|
|
103
|
+
const aptResponseData = await aptResponse.json();
|
|
104
|
+
const coinStore = aptResponseData.find(
|
|
105
|
+
(resource) => resource.type.includes("coin::CoinStore")
|
|
106
|
+
);
|
|
107
|
+
if (!coinStore) return 0;
|
|
108
|
+
return parseInt(coinStore.data.coin.value) / 1e8;
|
|
109
|
+
case EcencyWalletCurrency.ATOM:
|
|
110
|
+
const atomResponse = await fetch(
|
|
111
|
+
`https://rest.cosmos.directory/cosmoshub/auth/accounts/${address}`
|
|
112
|
+
);
|
|
113
|
+
const atomResponseData = await atomResponse.json();
|
|
114
|
+
return +atomResponseData.result.value.coins[0].amount / 1e6;
|
|
92
115
|
}
|
|
93
116
|
}
|
|
94
117
|
});
|
|
95
118
|
}
|
|
96
|
-
function
|
|
97
|
-
return
|
|
98
|
-
queryKey: ["ecency-wallets", "seed",
|
|
99
|
-
queryFn: async () =>
|
|
119
|
+
function useSeedPhrase(username) {
|
|
120
|
+
return useQuery({
|
|
121
|
+
queryKey: ["ecency-wallets", "seed", username],
|
|
122
|
+
queryFn: async () => bip39.generateMnemonic(128)
|
|
100
123
|
});
|
|
101
124
|
}
|
|
102
|
-
const
|
|
125
|
+
const options = {
|
|
103
126
|
max: 500,
|
|
104
127
|
// how long to live in ms
|
|
105
128
|
ttl: 1e3 * 60 * 5,
|
|
106
129
|
// return stale items before removing from cache?
|
|
107
|
-
allowStale:
|
|
108
|
-
updateAgeOnGet:
|
|
109
|
-
updateAgeOnHas:
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
130
|
+
allowStale: false,
|
|
131
|
+
updateAgeOnGet: false,
|
|
132
|
+
updateAgeOnHas: false
|
|
133
|
+
};
|
|
134
|
+
const cache = new LRUCache(options);
|
|
135
|
+
const undefinedValue = Symbol("undefined");
|
|
136
|
+
const cacheSet = (key, value) => cache.set(key, value === void 0 ? undefinedValue : value);
|
|
137
|
+
const cacheGet = (key) => {
|
|
138
|
+
const v = cache.get(key);
|
|
139
|
+
return v === undefinedValue ? void 0 : v;
|
|
113
140
|
};
|
|
114
|
-
function
|
|
115
|
-
return
|
|
116
|
-
queryKey: ["ecency-wallets", "coingecko-price",
|
|
141
|
+
function useCoinGeckoPriceQuery(currency) {
|
|
142
|
+
return useQuery({
|
|
143
|
+
queryKey: ["ecency-wallets", "coingecko-price", currency],
|
|
117
144
|
queryFn: async () => {
|
|
118
|
-
let
|
|
119
|
-
switch (
|
|
120
|
-
case
|
|
121
|
-
|
|
145
|
+
let curr = currency;
|
|
146
|
+
switch (currency) {
|
|
147
|
+
case EcencyWalletCurrency.BTC:
|
|
148
|
+
curr = "binance-wrapped-btc";
|
|
122
149
|
break;
|
|
123
|
-
case
|
|
124
|
-
|
|
150
|
+
case EcencyWalletCurrency.ETH:
|
|
151
|
+
curr = "ethereum";
|
|
125
152
|
break;
|
|
126
|
-
case
|
|
127
|
-
|
|
153
|
+
case EcencyWalletCurrency.SOL:
|
|
154
|
+
curr = "solana";
|
|
128
155
|
break;
|
|
129
|
-
case
|
|
130
|
-
|
|
156
|
+
case EcencyWalletCurrency.TON:
|
|
157
|
+
curr = "trx";
|
|
131
158
|
break;
|
|
132
159
|
default:
|
|
133
|
-
|
|
160
|
+
curr = currency;
|
|
134
161
|
}
|
|
135
|
-
let
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
162
|
+
let rate = cacheGet("gecko");
|
|
163
|
+
let response;
|
|
164
|
+
if (rate) {
|
|
165
|
+
response = rate;
|
|
166
|
+
} else {
|
|
167
|
+
const httpResponse = await fetch(
|
|
140
168
|
"https://api.coingecko.com/api/v3/simple/price",
|
|
141
169
|
{
|
|
142
170
|
method: "POST",
|
|
143
171
|
body: JSON.stringify({
|
|
144
172
|
params: {
|
|
145
|
-
ids: [
|
|
173
|
+
ids: [curr],
|
|
146
174
|
vs_currencies: "usd"
|
|
147
175
|
}
|
|
148
176
|
})
|
|
149
177
|
}
|
|
150
|
-
)
|
|
151
|
-
|
|
178
|
+
);
|
|
179
|
+
const data = await httpResponse.json();
|
|
180
|
+
cacheSet("gecko", data === void 0 ? undefinedValue : data);
|
|
181
|
+
response = data;
|
|
152
182
|
}
|
|
153
|
-
|
|
183
|
+
const rateValue = +response[Object.keys(response)[0]].usd;
|
|
184
|
+
return 1 / rateValue;
|
|
154
185
|
},
|
|
155
|
-
enabled: !!
|
|
186
|
+
enabled: !!currency
|
|
156
187
|
});
|
|
157
188
|
}
|
|
158
|
-
function
|
|
159
|
-
const { data:
|
|
160
|
-
return
|
|
161
|
-
queryKey: ["ecencу-wallets", "hive-keys",
|
|
189
|
+
function useHiveKeysQuery(username) {
|
|
190
|
+
const { data: seed } = useSeedPhrase(username);
|
|
191
|
+
return useQuery({
|
|
192
|
+
queryKey: ["ecencу-wallets", "hive-keys", username, seed],
|
|
162
193
|
queryFn: async () => {
|
|
163
|
-
if (!
|
|
194
|
+
if (!seed) {
|
|
164
195
|
throw new Error("[Ecency][Wallets] - no seed to create Hive account");
|
|
165
|
-
|
|
196
|
+
}
|
|
197
|
+
const ownerKey = PrivateKey.fromLogin(username, seed, "owner");
|
|
198
|
+
const activeKey = PrivateKey.fromLogin(username, seed, "active");
|
|
199
|
+
const postingKey = PrivateKey.fromLogin(username, seed, "posting");
|
|
200
|
+
const memoKey = PrivateKey.fromLogin(username, seed, "memo");
|
|
166
201
|
return {
|
|
167
|
-
username
|
|
168
|
-
owner:
|
|
169
|
-
active:
|
|
170
|
-
posting:
|
|
171
|
-
memo:
|
|
172
|
-
ownerPubkey:
|
|
173
|
-
activePubkey:
|
|
174
|
-
postingPubkey:
|
|
175
|
-
memoPubkey:
|
|
202
|
+
username,
|
|
203
|
+
owner: ownerKey.toString(),
|
|
204
|
+
active: activeKey.toString(),
|
|
205
|
+
posting: postingKey.toString(),
|
|
206
|
+
memo: memoKey.toString(),
|
|
207
|
+
ownerPubkey: ownerKey.createPublic().toString(),
|
|
208
|
+
activePubkey: activeKey.createPublic().toString(),
|
|
209
|
+
postingPubkey: postingKey.createPublic().toString(),
|
|
210
|
+
memoPubkey: memoKey.createPublic().toString()
|
|
176
211
|
};
|
|
177
212
|
}
|
|
178
213
|
});
|
|
179
214
|
}
|
|
180
|
-
const
|
|
181
|
-
[
|
|
215
|
+
const PATHS = {
|
|
216
|
+
[EcencyWalletCurrency.BTC]: "m/44'/0'/0'/0/0",
|
|
182
217
|
// Bitcoin (BIP44)
|
|
183
|
-
[
|
|
218
|
+
[EcencyWalletCurrency.ETH]: "m/44'/60'/0'/0/0",
|
|
184
219
|
// Ethereum (BIP44)
|
|
185
|
-
[
|
|
220
|
+
[EcencyWalletCurrency.SOL]: "m/44'/501'/0'/0'",
|
|
186
221
|
// Solana (BIP44)
|
|
187
|
-
[
|
|
222
|
+
[EcencyWalletCurrency.TON]: "m/44'/607'/0'",
|
|
188
223
|
// TON (BIP44)
|
|
189
|
-
[
|
|
224
|
+
[EcencyWalletCurrency.TRON]: "m/44'/195'/0'/0/0",
|
|
190
225
|
// Tron (BIP44)
|
|
191
|
-
[
|
|
226
|
+
[EcencyWalletCurrency.APT]: "m/44'/637'/0'/0'/0'",
|
|
192
227
|
// Aptos (BIP44)
|
|
193
|
-
[
|
|
228
|
+
[EcencyWalletCurrency.ATOM]: "m/44'/118'/0'/0/0"
|
|
194
229
|
// Cosmos (BIP44)
|
|
195
230
|
};
|
|
196
|
-
function
|
|
197
|
-
const { data:
|
|
198
|
-
|
|
231
|
+
function useWalletCreate(username, currency) {
|
|
232
|
+
const { data: mnemonic } = useSeedPhrase(username);
|
|
233
|
+
const queryClient = useQueryClient();
|
|
234
|
+
const createWallet = useMutation({
|
|
235
|
+
mutationKey: ["ecency-wallets", "create-wallet", username, currency],
|
|
199
236
|
mutationFn: async () => {
|
|
200
|
-
if (!
|
|
237
|
+
if (!mnemonic) {
|
|
201
238
|
throw new Error("[Ecency][Wallets] - No seed to create a wallet");
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
239
|
+
}
|
|
240
|
+
const wallet = getWallet(currency);
|
|
241
|
+
const privateKey = await (wallet == null ? void 0 : wallet.getDerivedPrivateKey({
|
|
242
|
+
mnemonic,
|
|
243
|
+
hdPath: PATHS[currency]
|
|
205
244
|
}));
|
|
206
|
-
await
|
|
207
|
-
const
|
|
208
|
-
privateKey
|
|
245
|
+
await delay(1e3);
|
|
246
|
+
const address = await (wallet == null ? void 0 : wallet.getNewAddress({
|
|
247
|
+
privateKey
|
|
209
248
|
}));
|
|
210
249
|
return {
|
|
211
|
-
privateKey
|
|
212
|
-
address:
|
|
213
|
-
publicKey:
|
|
214
|
-
username
|
|
215
|
-
currency
|
|
250
|
+
privateKey,
|
|
251
|
+
address: address.address,
|
|
252
|
+
publicKey: address.publicKey,
|
|
253
|
+
username,
|
|
254
|
+
currency
|
|
216
255
|
};
|
|
217
256
|
},
|
|
218
|
-
onSuccess: (
|
|
219
|
-
|
|
220
|
-
["ecency-wallets", "wallets",
|
|
221
|
-
(
|
|
257
|
+
onSuccess: (info) => {
|
|
258
|
+
queryClient.setQueryData(
|
|
259
|
+
["ecency-wallets", "wallets", info.username],
|
|
260
|
+
(data) => new Map(data ? Array.from(data.entries()) : []).set(info.currency, info)
|
|
222
261
|
);
|
|
223
262
|
}
|
|
224
|
-
})
|
|
263
|
+
});
|
|
264
|
+
const importWallet = useCallback(() => {
|
|
225
265
|
}, []);
|
|
226
266
|
return {
|
|
227
|
-
createWallet
|
|
228
|
-
importWallet
|
|
267
|
+
createWallet,
|
|
268
|
+
importWallet
|
|
229
269
|
};
|
|
230
270
|
}
|
|
231
|
-
const
|
|
232
|
-
function
|
|
233
|
-
const { data
|
|
234
|
-
queryKey: ["ecency-wallets", "wallets",
|
|
235
|
-
})
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
271
|
+
const CONFIG = { privateApiHost: "https://ecency.com" };
|
|
272
|
+
function useCreateAccountWithWallets(username) {
|
|
273
|
+
const { data } = useQuery({
|
|
274
|
+
queryKey: ["ecency-wallets", "wallets", username]
|
|
275
|
+
});
|
|
276
|
+
const { data: hiveKeys } = useHiveKeysQuery(username);
|
|
277
|
+
return useMutation({
|
|
278
|
+
mutationKey: ["ecency-wallets", "create-account-with-wallets", username],
|
|
279
|
+
mutationFn: ({ currency, address }) => fetch(CONFIG.privateApiHost + "/private-api/wallets-add", {
|
|
239
280
|
method: "POST",
|
|
240
281
|
headers: {
|
|
241
282
|
"Content-Type": "application/json"
|
|
242
283
|
},
|
|
243
284
|
body: JSON.stringify({
|
|
244
|
-
username
|
|
245
|
-
token:
|
|
246
|
-
address
|
|
285
|
+
username,
|
|
286
|
+
token: currency,
|
|
287
|
+
address,
|
|
247
288
|
meta: {
|
|
248
|
-
ownerPublicKey:
|
|
249
|
-
activePublicKey:
|
|
250
|
-
postingPublicKey:
|
|
251
|
-
memoPublicKey:
|
|
252
|
-
...Array.from((
|
|
253
|
-
(
|
|
254
|
-
...
|
|
255
|
-
[
|
|
289
|
+
ownerPublicKey: hiveKeys == null ? void 0 : hiveKeys.ownerPubkey,
|
|
290
|
+
activePublicKey: hiveKeys == null ? void 0 : hiveKeys.activePubkey,
|
|
291
|
+
postingPublicKey: hiveKeys == null ? void 0 : hiveKeys.postingPubkey,
|
|
292
|
+
memoPublicKey: hiveKeys == null ? void 0 : hiveKeys.memoPubkey,
|
|
293
|
+
...Array.from((data == null ? void 0 : data.entries()) ?? []).reduce(
|
|
294
|
+
(acc, [curr, info]) => ({
|
|
295
|
+
...acc,
|
|
296
|
+
[curr]: info.address
|
|
256
297
|
}),
|
|
257
298
|
{}
|
|
258
299
|
)
|
|
@@ -261,113 +302,98 @@ function M(e) {
|
|
|
261
302
|
})
|
|
262
303
|
});
|
|
263
304
|
}
|
|
264
|
-
const
|
|
305
|
+
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
265
306
|
__proto__: null,
|
|
266
|
-
useCreateAccountWithWallets
|
|
267
|
-
}, Symbol.toStringTag, { value: "Module" }))
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
],
|
|
273
|
-
[
|
|
274
|
-
[
|
|
275
|
-
[
|
|
276
|
-
|
|
277
|
-
[
|
|
278
|
-
[o.ATOM]: ["m/44'/118'/0'/0/0"]
|
|
279
|
-
}, F = {
|
|
280
|
-
[o.BTC]: [
|
|
281
|
-
"",
|
|
282
|
-
// legacy
|
|
283
|
-
"segwit_native",
|
|
284
|
-
"segwite_nested",
|
|
285
|
-
"segwit_taproot"
|
|
286
|
-
],
|
|
287
|
-
[o.ETH]: [void 0],
|
|
288
|
-
[o.SOL]: [void 0],
|
|
289
|
-
[o.TRON]: [void 0],
|
|
290
|
-
[o.APT]: [void 0],
|
|
291
|
-
[o.TON]: [void 0],
|
|
292
|
-
[o.ATOM]: [void 0]
|
|
307
|
+
useCreateAccountWithWallets
|
|
308
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
309
|
+
const HD_PATHS = {
|
|
310
|
+
[EcencyWalletCurrency.BTC]: ["m/84'/0'/0'/0/0"],
|
|
311
|
+
[EcencyWalletCurrency.ETH]: ["m/84'/60'/0'/0/0"],
|
|
312
|
+
// its not working for Trust, Exodus, todo: check others below
|
|
313
|
+
[EcencyWalletCurrency.SOL]: ["m/84'/501'/0'/0/0"],
|
|
314
|
+
[EcencyWalletCurrency.TRON]: ["m/84'/195'/0'/0/0"],
|
|
315
|
+
[EcencyWalletCurrency.APT]: ["m/84'/637'/0'/0/0"],
|
|
316
|
+
[EcencyWalletCurrency.TON]: [],
|
|
317
|
+
// Disabled
|
|
318
|
+
[EcencyWalletCurrency.ATOM]: ["m/84'/118'/0'/0'/0"]
|
|
293
319
|
};
|
|
294
|
-
async function
|
|
295
|
-
for (const
|
|
320
|
+
async function getKeysFromSeed(mnemonic, wallet, currency) {
|
|
321
|
+
for (const hdPath of HD_PATHS[currency] || []) {
|
|
296
322
|
try {
|
|
297
|
-
const
|
|
298
|
-
mnemonic
|
|
299
|
-
hdPath
|
|
323
|
+
const derivedPrivateKey = await wallet.getDerivedPrivateKey({
|
|
324
|
+
mnemonic,
|
|
325
|
+
hdPath
|
|
326
|
+
});
|
|
327
|
+
const derivedPublicKey = await wallet.getNewAddress({
|
|
328
|
+
privateKey: derivedPrivateKey,
|
|
329
|
+
addressType: "segwit_native"
|
|
300
330
|
});
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
addressType: s
|
|
305
|
-
})).address === t)
|
|
306
|
-
return i;
|
|
307
|
-
} catch {
|
|
308
|
-
return;
|
|
331
|
+
return [derivedPrivateKey.toString(), derivedPublicKey.address];
|
|
332
|
+
} catch (error) {
|
|
333
|
+
return [];
|
|
309
334
|
}
|
|
335
|
+
}
|
|
336
|
+
return [];
|
|
310
337
|
}
|
|
311
|
-
function
|
|
312
|
-
const
|
|
313
|
-
return
|
|
314
|
-
mutationKey: ["ecency-wallets", "import-wallet",
|
|
315
|
-
mutationFn: async ({ privateKeyOrSeed
|
|
316
|
-
const
|
|
317
|
-
if (!
|
|
338
|
+
function useImportWallet(username, currency) {
|
|
339
|
+
const queryClient = useQueryClient();
|
|
340
|
+
return useMutation({
|
|
341
|
+
mutationKey: ["ecency-wallets", "import-wallet", username, currency],
|
|
342
|
+
mutationFn: async ({ privateKeyOrSeed }) => {
|
|
343
|
+
const wallet = getWallet(currency);
|
|
344
|
+
if (!wallet) {
|
|
318
345
|
throw new Error("Cannot find token for this currency");
|
|
319
|
-
const s = a.split(" ").length === 12;
|
|
320
|
-
let c = !1, l = a;
|
|
321
|
-
if (s)
|
|
322
|
-
l = await Q(
|
|
323
|
-
a,
|
|
324
|
-
r,
|
|
325
|
-
i,
|
|
326
|
-
t
|
|
327
|
-
), c = !!l;
|
|
328
|
-
else {
|
|
329
|
-
const w = await i.getNewAddress({
|
|
330
|
-
privateKey: a
|
|
331
|
-
}), f = await i.validPrivateKey({
|
|
332
|
-
privateKey: a
|
|
333
|
-
});
|
|
334
|
-
c = w.address === r && f.isValid;
|
|
335
346
|
}
|
|
336
|
-
|
|
347
|
+
const isSeed = privateKeyOrSeed.split(" ").length === 12;
|
|
348
|
+
let address;
|
|
349
|
+
let privateKey = privateKeyOrSeed;
|
|
350
|
+
if (isSeed) {
|
|
351
|
+
[privateKey, address] = await getKeysFromSeed(
|
|
352
|
+
privateKeyOrSeed,
|
|
353
|
+
wallet,
|
|
354
|
+
currency
|
|
355
|
+
);
|
|
356
|
+
} else {
|
|
357
|
+
address = (await wallet.getNewAddress({
|
|
358
|
+
privateKey: privateKeyOrSeed
|
|
359
|
+
})).address;
|
|
360
|
+
}
|
|
361
|
+
if (!address || !privateKeyOrSeed) {
|
|
337
362
|
throw new Error(
|
|
338
363
|
"Private key/seed phrase isn't matching with public key or token"
|
|
339
364
|
);
|
|
365
|
+
}
|
|
340
366
|
return {
|
|
341
|
-
privateKey
|
|
342
|
-
address
|
|
367
|
+
privateKey,
|
|
368
|
+
address,
|
|
343
369
|
publicKey: ""
|
|
344
370
|
};
|
|
345
371
|
},
|
|
346
|
-
onSuccess: ({ privateKey
|
|
347
|
-
|
|
348
|
-
["ecency-wallets", "wallets",
|
|
349
|
-
(
|
|
350
|
-
privateKey
|
|
351
|
-
publicKey
|
|
352
|
-
address
|
|
353
|
-
username
|
|
354
|
-
currency
|
|
355
|
-
custom:
|
|
372
|
+
onSuccess: ({ privateKey, publicKey, address }) => {
|
|
373
|
+
queryClient.setQueryData(
|
|
374
|
+
["ecency-wallets", "wallets", username],
|
|
375
|
+
(data) => new Map(data ? Array.from(data.entries()) : []).set(currency, {
|
|
376
|
+
privateKey,
|
|
377
|
+
publicKey,
|
|
378
|
+
address,
|
|
379
|
+
username,
|
|
380
|
+
currency,
|
|
381
|
+
custom: true
|
|
356
382
|
})
|
|
357
383
|
);
|
|
358
384
|
}
|
|
359
385
|
});
|
|
360
386
|
}
|
|
361
387
|
export {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
388
|
+
EcencyWalletCurrency,
|
|
389
|
+
index as EcencyWalletsPrivateApi,
|
|
390
|
+
delay,
|
|
391
|
+
getWallet,
|
|
392
|
+
mnemonicToSeedBip39,
|
|
393
|
+
useCoinGeckoPriceQuery,
|
|
394
|
+
useGetExternalWalletBalanceQuery,
|
|
395
|
+
useHiveKeysQuery,
|
|
396
|
+
useImportWallet,
|
|
397
|
+
useSeedPhrase,
|
|
398
|
+
useWalletCreate
|
|
373
399
|
};
|