@ecency/wallets 1.0.19 → 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.
@@ -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: string;
78
+ address: any;
79
79
  publicKey: string;
80
80
  }, Error, Payload_2, unknown>;
81
81
 
@@ -1,64 +1,76 @@
1
- import { useCallback as P } from "react";
2
- import { useQuery as p, useQueryClient as h, useMutation as m } from "@tanstack/react-query";
3
- import { BtcWallet as S } from "@okxweb3/coin-bitcoin";
4
- import { EthWallet as R } from "@okxweb3/coin-ethereum";
5
- import { TrxWallet as O } from "@okxweb3/coin-tron";
6
- import { TonWallet as A } from "@okxweb3/coin-ton";
7
- import { SolWallet as k } from "@okxweb3/coin-solana";
8
- import { AtomWallet as N } from "@okxweb3/coin-cosmos";
9
- import { AptosWallet as D } from "@okxweb3/coin-aptos";
10
- import K, { mnemonicToSeedSync as _ } from "bip39";
11
- import { LRUCache as H } from "lru-cache";
12
- import { PrivateKey as u } from "@hiveio/dhive";
13
- var o = /* @__PURE__ */ ((e) => (e.BTC = "btc", e.ETH = "eth", e.APT = "aptos", e.ATOM = "cosmos", e.TON = "ton", e.TRON = "tron", e.SOL = "solana", e))(o || {});
14
- function j(e) {
15
- return new Promise((t) => setTimeout(t, e));
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 T(e) {
18
- switch (e) {
19
- case o.BTC:
20
- return new S();
21
- case o.ETH:
22
- return new R();
23
- case o.TRON:
24
- return new O();
25
- case o.TON:
26
- return new A();
27
- case o.SOL:
28
- return new k();
29
- case o.ATOM:
30
- return new N();
31
- case o.APT:
32
- return new D();
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 re(e) {
38
- return _(e).toString("hex");
46
+ function mnemonicToSeedBip39(value) {
47
+ return mnemonicToSeedSync(value).toString("hex");
39
48
  }
40
- function ce(e, t) {
41
- return p({
42
- queryKey: ["ecency-wallets", "external-wallet-balance", e, t],
49
+ function useGetExternalWalletBalanceQuery(currency, address) {
50
+ return useQuery({
51
+ queryKey: ["ecency-wallets", "external-wallet-balance", currency, address],
43
52
  queryFn: async () => {
44
- switch (e) {
45
- case o.BTC:
46
- const a = await (await fetch(
47
- `https://mempool.space/api/address/${t}`
48
- )).json();
49
- return (a.chain_stats.funded_txo_sum - a.chain_stats.spent_txo_sum) / 1e8;
50
- case o.ETH:
51
- return +(await (await fetch("https://eth.llamarpc.com", {
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: [t, "latest"]
67
+ params: [address, "latest"]
58
68
  })
59
- })).json()).result / 1e18;
60
- case o.SOL:
61
- return (await (await fetch(
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: [t]
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
- return y ? parseInt(y.data.coin.value) / 1e8 : 0;
88
- case o.ATOM:
89
- return +(await (await fetch(
90
- `https://rest.cosmos.directory/cosmoshub/auth/accounts/${t}`
91
- )).json()).result.value.coins[0].amount / 1e6;
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 b(e) {
97
- return p({
98
- queryKey: ["ecency-wallets", "seed", e],
99
- queryFn: async () => K.generateMnemonic(128)
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 C = {
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: !1,
108
- updateAgeOnGet: !1,
109
- updateAgeOnHas: !1
110
- }, g = new H(C), d = Symbol("undefined"), B = (e, t) => g.set(e, t === void 0 ? d : t), E = (e) => {
111
- const t = g.get(e);
112
- return t === d ? void 0 : t;
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 le(e) {
115
- return p({
116
- queryKey: ["ecency-wallets", "coingecko-price", e],
141
+ function useCoinGeckoPriceQuery(currency) {
142
+ return useQuery({
143
+ queryKey: ["ecency-wallets", "coingecko-price", currency],
117
144
  queryFn: async () => {
118
- let t = e;
119
- switch (e) {
120
- case o.BTC:
121
- t = "binance-wrapped-btc";
145
+ let curr = currency;
146
+ switch (currency) {
147
+ case EcencyWalletCurrency.BTC:
148
+ curr = "binance-wrapped-btc";
122
149
  break;
123
- case o.ETH:
124
- t = "ethereum";
150
+ case EcencyWalletCurrency.ETH:
151
+ curr = "ethereum";
125
152
  break;
126
- case o.SOL:
127
- t = "solana";
153
+ case EcencyWalletCurrency.SOL:
154
+ curr = "solana";
128
155
  break;
129
- case o.TON:
130
- t = "trx";
156
+ case EcencyWalletCurrency.TON:
157
+ curr = "trx";
131
158
  break;
132
159
  default:
133
- t = e;
160
+ curr = currency;
134
161
  }
135
- let n = E("gecko"), a;
136
- if (n)
137
- a = n;
138
- else {
139
- const s = await (await fetch(
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: [t],
173
+ ids: [curr],
146
174
  vs_currencies: "usd"
147
175
  }
148
176
  })
149
177
  }
150
- )).json();
151
- B("gecko", s === void 0 ? d : s), a = s;
178
+ );
179
+ const data = await httpResponse.json();
180
+ cacheSet("gecko", data === void 0 ? undefinedValue : data);
181
+ response = data;
152
182
  }
153
- return 1 / +a[Object.keys(a)[0]].usd;
183
+ const rateValue = +response[Object.keys(response)[0]].usd;
184
+ return 1 / rateValue;
154
185
  },
155
- enabled: !!e
186
+ enabled: !!currency
156
187
  });
157
188
  }
158
- function L(e) {
159
- const { data: t } = b(e);
160
- return p({
161
- queryKey: ["ecencу-wallets", "hive-keys", e, t],
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 (!t)
194
+ if (!seed) {
164
195
  throw new Error("[Ecency][Wallets] - no seed to create Hive account");
165
- const n = u.fromLogin(e, t, "owner"), a = u.fromLogin(e, t, "active"), r = u.fromLogin(e, t, "posting"), i = u.fromLogin(e, t, "memo");
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: e,
168
- owner: n.toString(),
169
- active: a.toString(),
170
- posting: r.toString(),
171
- memo: i.toString(),
172
- ownerPubkey: n.createPublic().toString(),
173
- activePubkey: a.createPublic().toString(),
174
- postingPubkey: r.createPublic().toString(),
175
- memoPubkey: i.createPublic().toString()
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 W = {
181
- [o.BTC]: "m/44'/0'/0'/0/0",
215
+ const PATHS = {
216
+ [EcencyWalletCurrency.BTC]: "m/44'/0'/0'/0/0",
182
217
  // Bitcoin (BIP44)
183
- [o.ETH]: "m/44'/60'/0'/0/0",
218
+ [EcencyWalletCurrency.ETH]: "m/44'/60'/0'/0/0",
184
219
  // Ethereum (BIP44)
185
- [o.SOL]: "m/44'/501'/0'/0'",
220
+ [EcencyWalletCurrency.SOL]: "m/44'/501'/0'/0'",
186
221
  // Solana (BIP44)
187
- [o.TON]: "m/44'/607'/0'",
222
+ [EcencyWalletCurrency.TON]: "m/44'/607'/0'",
188
223
  // TON (BIP44)
189
- [o.TRON]: "m/44'/195'/0'/0/0",
224
+ [EcencyWalletCurrency.TRON]: "m/44'/195'/0'/0/0",
190
225
  // Tron (BIP44)
191
- [o.APT]: "m/44'/637'/0'/0'/0'",
226
+ [EcencyWalletCurrency.APT]: "m/44'/637'/0'/0'/0'",
192
227
  // Aptos (BIP44)
193
- [o.ATOM]: "m/44'/118'/0'/0/0"
228
+ [EcencyWalletCurrency.ATOM]: "m/44'/118'/0'/0/0"
194
229
  // Cosmos (BIP44)
195
230
  };
196
- function pe(e, t) {
197
- const { data: n } = b(e), a = h(), r = m({
198
- mutationKey: ["ecency-wallets", "create-wallet", e, t],
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 (!n)
237
+ if (!mnemonic) {
201
238
  throw new Error("[Ecency][Wallets] - No seed to create a wallet");
202
- const s = T(t), c = await (s == null ? void 0 : s.getDerivedPrivateKey({
203
- mnemonic: n,
204
- hdPath: W[t]
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 j(1e3);
207
- const l = await (s == null ? void 0 : s.getNewAddress({
208
- privateKey: c
245
+ await delay(1e3);
246
+ const address = await (wallet == null ? void 0 : wallet.getNewAddress({
247
+ privateKey
209
248
  }));
210
249
  return {
211
- privateKey: c,
212
- address: l.address,
213
- publicKey: l.publicKey,
214
- username: e,
215
- currency: t
250
+ privateKey,
251
+ address: address.address,
252
+ publicKey: address.publicKey,
253
+ username,
254
+ currency
216
255
  };
217
256
  },
218
- onSuccess: (s) => {
219
- a.setQueryData(
220
- ["ecency-wallets", "wallets", s.username],
221
- (c) => new Map(c ? Array.from(c.entries()) : []).set(s.currency, s)
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
- }), i = P(() => {
263
+ });
264
+ const importWallet = useCallback(() => {
225
265
  }, []);
226
266
  return {
227
- createWallet: r,
228
- importWallet: i
267
+ createWallet,
268
+ importWallet
229
269
  };
230
270
  }
231
- const q = { privateApiHost: "https://ecency.com" };
232
- function M(e) {
233
- const { data: t } = p({
234
- queryKey: ["ecency-wallets", "wallets", e]
235
- }), { data: n } = L(e);
236
- return m({
237
- mutationKey: ["ecency-wallets", "create-account-with-wallets", e],
238
- mutationFn: ({ currency: a, address: r }) => fetch(q.privateApiHost + "/private-api/wallets-add", {
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: e,
245
- token: a,
246
- address: r,
285
+ username,
286
+ token: currency,
287
+ address,
247
288
  meta: {
248
- ownerPublicKey: n == null ? void 0 : n.ownerPubkey,
249
- activePublicKey: n == null ? void 0 : n.activePubkey,
250
- postingPublicKey: n == null ? void 0 : n.postingPubkey,
251
- memoPublicKey: n == null ? void 0 : n.memoPubkey,
252
- ...Array.from((t == null ? void 0 : t.entries()) ?? []).reduce(
253
- (i, [s, c]) => ({
254
- ...i,
255
- [s]: c.address
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,121 +302,98 @@ function M(e) {
261
302
  })
262
303
  });
263
304
  }
264
- const ue = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
305
+ const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
265
306
  __proto__: null,
266
- useCreateAccountWithWallets: M
267
- }, Symbol.toStringTag, { value: "Module" })), x = {
268
- [o.BTC]: [
269
- "m/84'/0'/0'/0/0",
270
- "m/44'/0'/0'/0/0",
271
- "m/49'/0'/0'/0/0",
272
- "m/84'/0'/0'/0",
273
- // XVerse wallet – Legacy
274
- "m/44'/0'/0'/0",
275
- // XVerse wallet – Segwit
276
- "m/49'/0'/0'/0",
277
- // XVerse wallet – Native segwit
278
- "m/86'/0'/0'/0"
279
- // XVerse wallet – Taproot
280
- ],
281
- [o.ETH]: ["m/44'/60'/0'/0/0"],
282
- [o.SOL]: ["m/44'/501'/0'/0'"],
283
- [o.TRON]: ["m/44'/195'/0'/0/0"],
284
- [o.APT]: ["m/44'/637'/0'/0'/0'"],
285
- [o.TON]: ["m/44'/607'/0'"],
286
- [o.ATOM]: ["m/44'/118'/0'/0/0"]
287
- }, F = {
288
- [o.BTC]: [
289
- "",
290
- // legacy
291
- "segwit_native",
292
- "segwite_nested",
293
- "segwit_taproot"
294
- ],
295
- [o.ETH]: [void 0],
296
- [o.SOL]: [void 0],
297
- [o.TRON]: [void 0],
298
- [o.APT]: [void 0],
299
- [o.TON]: [void 0],
300
- [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"]
301
319
  };
302
- async function Q(e, t, n, a) {
303
- for (const r of x[a] || [])
320
+ async function getKeysFromSeed(mnemonic, wallet, currency) {
321
+ for (const hdPath of HD_PATHS[currency] || []) {
304
322
  try {
305
- const i = await n.getDerivedPrivateKey({
306
- mnemonic: e,
307
- hdPath: r
323
+ const derivedPrivateKey = await wallet.getDerivedPrivateKey({
324
+ mnemonic,
325
+ hdPath
326
+ });
327
+ const derivedPublicKey = await wallet.getNewAddress({
328
+ privateKey: derivedPrivateKey,
329
+ addressType: "segwit_native"
308
330
  });
309
- for (const s of F[a])
310
- if ((await n.getNewAddress({
311
- privateKey: i,
312
- addressType: s
313
- })).address === t)
314
- return i;
315
- } catch {
316
- return;
331
+ return [derivedPrivateKey.toString(), derivedPublicKey.address];
332
+ } catch (error) {
333
+ return [];
317
334
  }
335
+ }
336
+ return [];
318
337
  }
319
- function me(e, t) {
320
- const n = h();
321
- return m({
322
- mutationKey: ["ecency-wallets", "import-wallet", e, t],
323
- mutationFn: async ({ privateKeyOrSeed: a, address: r }) => {
324
- const i = T(t);
325
- if (!i)
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) {
326
345
  throw new Error("Cannot find token for this currency");
327
- const s = a.split(" ").length === 12;
328
- let c = !1, l = a;
329
- if (s)
330
- l = await Q(
331
- a,
332
- r,
333
- i,
334
- t
335
- ), c = !!l;
336
- else {
337
- const w = await i.getNewAddress({
338
- privateKey: a
339
- }), f = await i.validPrivateKey({
340
- privateKey: a
341
- });
342
- c = w.address === r && f.isValid;
343
346
  }
344
- if (!c)
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) {
345
362
  throw new Error(
346
363
  "Private key/seed phrase isn't matching with public key or token"
347
364
  );
365
+ }
348
366
  return {
349
- privateKey: l,
350
- address: r,
367
+ privateKey,
368
+ address,
351
369
  publicKey: ""
352
370
  };
353
371
  },
354
- onSuccess: ({ privateKey: a, publicKey: r, address: i }) => {
355
- n.setQueryData(
356
- ["ecency-wallets", "wallets", e],
357
- (s) => new Map(s ? Array.from(s.entries()) : []).set(t, {
358
- privateKey: a,
359
- publicKey: r,
360
- address: i,
361
- username: e,
362
- currency: t,
363
- custom: !0
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
364
382
  })
365
383
  );
366
384
  }
367
385
  });
368
386
  }
369
387
  export {
370
- o as EcencyWalletCurrency,
371
- ue as EcencyWalletsPrivateApi,
372
- j as delay,
373
- T as getWallet,
374
- re as mnemonicToSeedBip39,
375
- le as useCoinGeckoPriceQuery,
376
- ce as useGetExternalWalletBalanceQuery,
377
- L as useHiveKeysQuery,
378
- me as useImportWallet,
379
- b as useSeedPhrase,
380
- pe as useWalletCreate
388
+ EcencyWalletCurrency,
389
+ index as EcencyWalletsPrivateApi,
390
+ delay,
391
+ getWallet,
392
+ mnemonicToSeedBip39,
393
+ useCoinGeckoPriceQuery,
394
+ useGetExternalWalletBalanceQuery,
395
+ useHiveKeysQuery,
396
+ useImportWallet,
397
+ useSeedPhrase,
398
+ useWalletCreate
381
399
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ecency/wallets",
3
3
  "private": false,
4
- "version": "1.0.19",
4
+ "version": "1.1.0",
5
5
  "type": "module",
6
6
  "main": "./dist/ecency-wallets.umd.js",
7
7
  "module": "./dist/ecency-wallets.es.js",