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