@ecency/wallets 1.0.9 → 1.0.10
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 +11 -0
- package/dist/ecency-wallets.es.js +165 -88
- package/package.json +1 -1
|
@@ -52,6 +52,11 @@ declare interface Payload {
|
|
|
52
52
|
address: string;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
declare interface Payload_2 {
|
|
56
|
+
address: string;
|
|
57
|
+
privateKeyOrSeed: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
55
60
|
export declare function useCoinGeckoPriceQuery(currency?: EcencyWalletCurrency): UseQueryResult<number, Error>;
|
|
56
61
|
|
|
57
62
|
declare function useCreateAccountWithWallets(username: string): UseMutationResult<Response, Error, Payload, unknown>;
|
|
@@ -68,6 +73,12 @@ export declare function useGetExternalWalletBalanceQuery(currency: EcencyWalletC
|
|
|
68
73
|
|
|
69
74
|
export declare function useHiveKeysQuery(username: string): UseQueryResult<EcencyHiveKeys, Error>;
|
|
70
75
|
|
|
76
|
+
export declare function useImportWallet(username: string, currency: EcencyWalletCurrency): UseMutationResult< {
|
|
77
|
+
privateKey: string;
|
|
78
|
+
address: string;
|
|
79
|
+
publicKey: string;
|
|
80
|
+
}, Error, Payload_2, unknown>;
|
|
81
|
+
|
|
71
82
|
export declare function useSeedPhrase(): UseQueryResult<string, Error>;
|
|
72
83
|
|
|
73
84
|
export declare function useWalletCreate(username: string, currency: EcencyWalletCurrency): {
|
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { useCallback as
|
|
2
|
-
import { useQuery as
|
|
3
|
-
import { BtcWallet as
|
|
4
|
-
import { EthWallet as
|
|
5
|
-
import { TrxWallet as
|
|
6
|
-
import { TonWallet as
|
|
7
|
-
import { SolWallet as
|
|
8
|
-
import { AtomWallet as
|
|
9
|
-
import { AptosWallet as
|
|
10
|
-
import
|
|
11
|
-
import { LRUCache as
|
|
1
|
+
import { useCallback as v } from "react";
|
|
2
|
+
import { useQuery as u, 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 K } from "@okxweb3/coin-cosmos";
|
|
9
|
+
import { AptosWallet as N } from "@okxweb3/coin-aptos";
|
|
10
|
+
import D, { mnemonicToSeedSync as j } from "bip39";
|
|
11
|
+
import { LRUCache as H } from "lru-cache";
|
|
12
12
|
import { PrivateKey as p } from "@hiveio/dhive";
|
|
13
13
|
var o = /* @__PURE__ */ ((e) => (e.BTC = "btc", e.ETH = "eth", e.APT = "atpos", e.ATOM = "cosmos", e.TON = "ton", e.TRON = "tron", e.SOL = "Solana", e))(o || {});
|
|
14
|
-
function
|
|
14
|
+
function _(e) {
|
|
15
15
|
return new Promise((t) => setTimeout(t, e));
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function b(e) {
|
|
18
18
|
switch (e) {
|
|
19
19
|
case o.BTC:
|
|
20
|
-
return new T();
|
|
21
|
-
case o.ETH:
|
|
22
20
|
return new S();
|
|
23
|
-
case o.
|
|
21
|
+
case o.ETH:
|
|
24
22
|
return new R();
|
|
23
|
+
case o.TRON:
|
|
24
|
+
return new O();
|
|
25
25
|
case o.TON:
|
|
26
|
-
return new
|
|
26
|
+
return new A();
|
|
27
27
|
case o.SOL:
|
|
28
|
-
return new
|
|
28
|
+
return new k();
|
|
29
29
|
case o.ATOM:
|
|
30
|
-
return new
|
|
30
|
+
return new K();
|
|
31
31
|
case o.APT:
|
|
32
|
-
return new
|
|
32
|
+
return new N();
|
|
33
33
|
default:
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
function
|
|
38
|
-
return
|
|
37
|
+
function re(e) {
|
|
38
|
+
return j(e).toString("hex");
|
|
39
39
|
}
|
|
40
|
-
function
|
|
41
|
-
return
|
|
40
|
+
function ie(e, t) {
|
|
41
|
+
return u({
|
|
42
42
|
queryKey: ["ecency-wallets", "external-wallet-balance", e, t],
|
|
43
43
|
queryFn: async () => {
|
|
44
44
|
switch (e) {
|
|
45
45
|
case o.BTC:
|
|
46
|
-
const
|
|
46
|
+
const a = await (await fetch(
|
|
47
47
|
`https://mempool.space/api/address/${t}`
|
|
48
48
|
)).json();
|
|
49
|
-
return (
|
|
49
|
+
return (a.chain_stats.funded_txo_sum - a.chain_stats.spent_txo_sum) / 1e8;
|
|
50
50
|
case o.ETH:
|
|
51
51
|
return +(await (await fetch("https://eth.llamarpc.com", {
|
|
52
52
|
method: "POST",
|
|
@@ -79,12 +79,12 @@ function se(e, t) {
|
|
|
79
79
|
`https://tonapi.io/v1/blockchain/getAccount?account=${t}`
|
|
80
80
|
)).json()).balance / 1e9;
|
|
81
81
|
case o.APT:
|
|
82
|
-
const
|
|
82
|
+
const f = (await (await fetch(
|
|
83
83
|
`https://fullnode.mainnet.aptoslabs.com/v1/accounts/${t}/resources`
|
|
84
84
|
)).json()).find(
|
|
85
|
-
(
|
|
85
|
+
(P) => P.type.includes("coin::CoinStore")
|
|
86
86
|
);
|
|
87
|
-
return
|
|
87
|
+
return f ? parseInt(f.data.coin.value) / 1e8 : 0;
|
|
88
88
|
case o.ATOM:
|
|
89
89
|
return +(await (await fetch(
|
|
90
90
|
`https://rest.cosmos.directory/cosmoshub/auth/accounts/${t}`
|
|
@@ -93,13 +93,13 @@ function se(e, t) {
|
|
|
93
93
|
}
|
|
94
94
|
});
|
|
95
95
|
}
|
|
96
|
-
function
|
|
97
|
-
return
|
|
96
|
+
function g() {
|
|
97
|
+
return u({
|
|
98
98
|
queryKey: ["ecency-wallets", "seed"],
|
|
99
|
-
queryFn: async () =>
|
|
99
|
+
queryFn: async () => D.generateMnemonic(128)
|
|
100
100
|
});
|
|
101
101
|
}
|
|
102
|
-
const
|
|
102
|
+
const C = {
|
|
103
103
|
max: 500,
|
|
104
104
|
// how long to live in ms
|
|
105
105
|
ttl: 1e3 * 60 * 5,
|
|
@@ -107,12 +107,12 @@ const _ = {
|
|
|
107
107
|
allowStale: !1,
|
|
108
108
|
updateAgeOnGet: !1,
|
|
109
109
|
updateAgeOnHas: !1
|
|
110
|
-
},
|
|
111
|
-
const t =
|
|
112
|
-
return t ===
|
|
110
|
+
}, T = new H(C), d = Symbol("undefined"), W = (e, t) => T.set(e, t === void 0 ? d : t), q = (e) => {
|
|
111
|
+
const t = T.get(e);
|
|
112
|
+
return t === d ? void 0 : t;
|
|
113
113
|
};
|
|
114
114
|
function ce(e) {
|
|
115
|
-
return
|
|
115
|
+
return u({
|
|
116
116
|
queryKey: ["ecency-wallets", "coingecko-price", e],
|
|
117
117
|
queryFn: async () => {
|
|
118
118
|
let t = e;
|
|
@@ -132,11 +132,11 @@ function ce(e) {
|
|
|
132
132
|
default:
|
|
133
133
|
t = e;
|
|
134
134
|
}
|
|
135
|
-
let n =
|
|
135
|
+
let n = q("gecko"), a;
|
|
136
136
|
if (n)
|
|
137
|
-
|
|
137
|
+
a = n;
|
|
138
138
|
else {
|
|
139
|
-
const
|
|
139
|
+
const s = await (await fetch(
|
|
140
140
|
"https://api.coingecko.com/api/v3/simple/price",
|
|
141
141
|
{
|
|
142
142
|
method: "POST",
|
|
@@ -148,36 +148,36 @@ function ce(e) {
|
|
|
148
148
|
})
|
|
149
149
|
}
|
|
150
150
|
)).json();
|
|
151
|
-
|
|
151
|
+
W("gecko", s === void 0 ? d : s), a = s;
|
|
152
152
|
}
|
|
153
|
-
return 1 / +
|
|
153
|
+
return 1 / +a[Object.keys(a)[0]].usd;
|
|
154
154
|
},
|
|
155
155
|
enabled: !!e
|
|
156
156
|
});
|
|
157
157
|
}
|
|
158
|
-
function
|
|
159
|
-
const { data: t } =
|
|
160
|
-
return
|
|
158
|
+
function B(e) {
|
|
159
|
+
const { data: t } = g();
|
|
160
|
+
return u({
|
|
161
161
|
queryKey: ["ecencу-wallets", "hive-keys", e, t],
|
|
162
162
|
queryFn: async () => {
|
|
163
163
|
if (!t)
|
|
164
164
|
throw new Error("[Ecency][Wallets] - no seed to create Hive account");
|
|
165
|
-
const n = p.fromLogin(e, t, "owner"),
|
|
165
|
+
const n = p.fromLogin(e, t, "owner"), a = p.fromLogin(e, t, "active"), i = p.fromLogin(e, t, "posting"), r = p.fromLogin(e, t, "memo");
|
|
166
166
|
return {
|
|
167
167
|
username: e,
|
|
168
168
|
owner: n.toString(),
|
|
169
|
-
active:
|
|
170
|
-
posting:
|
|
169
|
+
active: a.toString(),
|
|
170
|
+
posting: i.toString(),
|
|
171
171
|
memo: r.toString(),
|
|
172
172
|
ownerPubkey: n.createPublic().toString(),
|
|
173
|
-
activePubkey:
|
|
174
|
-
postingPubkey:
|
|
173
|
+
activePubkey: a.createPublic().toString(),
|
|
174
|
+
postingPubkey: i.createPublic().toString(),
|
|
175
175
|
memoPubkey: r.createPublic().toString()
|
|
176
176
|
};
|
|
177
177
|
}
|
|
178
178
|
});
|
|
179
179
|
}
|
|
180
|
-
const
|
|
180
|
+
const L = {
|
|
181
181
|
[o.BTC]: "m/44'/0'/0'/0/0",
|
|
182
182
|
// Bitcoin (BIP44)
|
|
183
183
|
[o.ETH]: "m/44'/60'/0'/0/0",
|
|
@@ -193,66 +193,66 @@ const x = {
|
|
|
193
193
|
[o.ATOM]: "m/44'/118'/0'/0/0"
|
|
194
194
|
// Cosmos (BIP44)
|
|
195
195
|
};
|
|
196
|
-
function
|
|
197
|
-
const { data: n } =
|
|
196
|
+
function le(e, t) {
|
|
197
|
+
const { data: n } = g(), a = h(), i = m({
|
|
198
198
|
mutationKey: ["ecency-wallets", "create-wallet", e, t],
|
|
199
199
|
mutationFn: async () => {
|
|
200
200
|
if (!n)
|
|
201
201
|
throw new Error("[Ecency][Wallets] - No seed to create a wallet");
|
|
202
|
-
const
|
|
202
|
+
const s = b(t), c = await (s == null ? void 0 : s.getDerivedPrivateKey({
|
|
203
203
|
mnemonic: n,
|
|
204
|
-
hdPath:
|
|
204
|
+
hdPath: L[t]
|
|
205
205
|
}));
|
|
206
|
-
await
|
|
207
|
-
const
|
|
208
|
-
privateKey:
|
|
206
|
+
await _(1e3);
|
|
207
|
+
const l = await (s == null ? void 0 : s.getNewAddress({
|
|
208
|
+
privateKey: c
|
|
209
209
|
}));
|
|
210
210
|
return {
|
|
211
|
-
privateKey:
|
|
212
|
-
address:
|
|
213
|
-
publicKey:
|
|
211
|
+
privateKey: c,
|
|
212
|
+
address: l.address,
|
|
213
|
+
publicKey: l.publicKey,
|
|
214
214
|
username: e,
|
|
215
215
|
currency: t
|
|
216
216
|
};
|
|
217
217
|
},
|
|
218
|
-
onSuccess: (
|
|
219
|
-
|
|
220
|
-
["ecency-wallets", "wallets",
|
|
221
|
-
(
|
|
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)
|
|
222
222
|
);
|
|
223
223
|
}
|
|
224
|
-
}), r =
|
|
224
|
+
}), r = v(() => {
|
|
225
225
|
}, []);
|
|
226
226
|
return {
|
|
227
|
-
createWallet:
|
|
227
|
+
createWallet: i,
|
|
228
228
|
importWallet: r
|
|
229
229
|
};
|
|
230
230
|
}
|
|
231
|
-
const
|
|
232
|
-
function
|
|
233
|
-
const { data: t } =
|
|
231
|
+
const x = { privateApiHost: "https://ecency.com" };
|
|
232
|
+
function M(e) {
|
|
233
|
+
const { data: t } = u({
|
|
234
234
|
queryKey: ["ecency-wallets", "wallets", e]
|
|
235
|
-
}), { data: n } =
|
|
236
|
-
return
|
|
235
|
+
}), { data: n } = B(e);
|
|
236
|
+
return m({
|
|
237
237
|
mutationKey: ["ecency-wallets", "create-account-with-wallets", e],
|
|
238
|
-
mutationFn: ({ currency:
|
|
238
|
+
mutationFn: ({ currency: a, address: i }) => fetch(x.privateApiHost + "/private-api/wallets-add", {
|
|
239
239
|
method: "POST",
|
|
240
240
|
headers: {
|
|
241
241
|
"Content-Type": "application/json"
|
|
242
242
|
},
|
|
243
243
|
body: JSON.stringify({
|
|
244
244
|
username: e,
|
|
245
|
-
token:
|
|
246
|
-
address:
|
|
245
|
+
token: a,
|
|
246
|
+
address: i,
|
|
247
247
|
meta: {
|
|
248
248
|
ownerPublicKey: n == null ? void 0 : n.ownerPubkey,
|
|
249
249
|
activePublicKey: n == null ? void 0 : n.activePubkey,
|
|
250
250
|
postingPublicKey: n == null ? void 0 : n.postingPubkey,
|
|
251
251
|
memoPublicKey: n == null ? void 0 : n.memoPubkey,
|
|
252
252
|
...Array.from((t == null ? void 0 : t.entries()) ?? []).reduce(
|
|
253
|
-
(r, [
|
|
253
|
+
(r, [s, c]) => ({
|
|
254
254
|
...r,
|
|
255
|
-
[
|
|
255
|
+
[s]: c.address
|
|
256
256
|
}),
|
|
257
257
|
{}
|
|
258
258
|
)
|
|
@@ -261,19 +261,96 @@ function L(e) {
|
|
|
261
261
|
})
|
|
262
262
|
});
|
|
263
263
|
}
|
|
264
|
-
const
|
|
264
|
+
const ue = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
265
265
|
__proto__: null,
|
|
266
|
-
useCreateAccountWithWallets:
|
|
267
|
-
}, Symbol.toStringTag, { value: "Module" }))
|
|
266
|
+
useCreateAccountWithWallets: M
|
|
267
|
+
}, Symbol.toStringTag, { value: "Module" })), E = {
|
|
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
|
+
],
|
|
273
|
+
[o.ETH]: ["m/44'/60'/0'/0/0"],
|
|
274
|
+
[o.SOL]: ["m/44'/501'/0'/0'"],
|
|
275
|
+
[o.TRON]: ["m/44'/195'/0'/0/0"],
|
|
276
|
+
[o.APT]: ["m/44'/637'/0'/0'/0'"],
|
|
277
|
+
[o.TON]: ["m/44'/607'/0'"],
|
|
278
|
+
[o.ATOM]: ["m/44'/118'/0'/0/0"]
|
|
279
|
+
};
|
|
280
|
+
async function F(e, t, n, a) {
|
|
281
|
+
for (const i of E[a] || [])
|
|
282
|
+
try {
|
|
283
|
+
const r = await n.getDerivedPrivateKey({
|
|
284
|
+
mnemonic: e,
|
|
285
|
+
hdPath: i
|
|
286
|
+
});
|
|
287
|
+
return await n.getNewAddress({
|
|
288
|
+
privateKey: r
|
|
289
|
+
}) === t ? r : void 0;
|
|
290
|
+
} catch {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function pe(e, t) {
|
|
295
|
+
const n = h();
|
|
296
|
+
return m({
|
|
297
|
+
mutationKey: ["ecency-wallets", "import-wallet", e, t],
|
|
298
|
+
mutationFn: async ({ privateKeyOrSeed: a, address: i }) => {
|
|
299
|
+
const r = b(t);
|
|
300
|
+
if (!r)
|
|
301
|
+
throw new Error("Cannot find token for this currency");
|
|
302
|
+
const s = a.split(" ").length === 12;
|
|
303
|
+
let c = !1, l = a;
|
|
304
|
+
if (s)
|
|
305
|
+
l = await F(
|
|
306
|
+
a,
|
|
307
|
+
i,
|
|
308
|
+
r,
|
|
309
|
+
t
|
|
310
|
+
), c = !!l;
|
|
311
|
+
else {
|
|
312
|
+
const w = await r.getNewAddress({
|
|
313
|
+
privateKey: a
|
|
314
|
+
}), y = await r.validPrivateKey({
|
|
315
|
+
privateKey: a
|
|
316
|
+
});
|
|
317
|
+
c = w === i && y;
|
|
318
|
+
}
|
|
319
|
+
if (!c)
|
|
320
|
+
throw new Error(
|
|
321
|
+
"Private key/seed phrase isn't matching with public key or token"
|
|
322
|
+
);
|
|
323
|
+
return {
|
|
324
|
+
privateKey: l,
|
|
325
|
+
address: i,
|
|
326
|
+
publicKey: ""
|
|
327
|
+
};
|
|
328
|
+
},
|
|
329
|
+
onSuccess: ({ privateKey: a, publicKey: i, address: r }) => {
|
|
330
|
+
n.setQueryData(
|
|
331
|
+
["ecency-wallets", "wallets", e],
|
|
332
|
+
(s) => new Map(s ? Array.from(s.entries()) : []).set(t, {
|
|
333
|
+
privateKey: a,
|
|
334
|
+
publicKey: i,
|
|
335
|
+
address: r,
|
|
336
|
+
username: e,
|
|
337
|
+
currency: t,
|
|
338
|
+
custom: !0
|
|
339
|
+
})
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
268
344
|
export {
|
|
269
345
|
o as EcencyWalletCurrency,
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
346
|
+
ue as EcencyWalletsPrivateApi,
|
|
347
|
+
_ as delay,
|
|
348
|
+
b as getWallet,
|
|
349
|
+
re as mnemonicToSeedBip39,
|
|
274
350
|
ce as useCoinGeckoPriceQuery,
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
351
|
+
ie as useGetExternalWalletBalanceQuery,
|
|
352
|
+
B as useHiveKeysQuery,
|
|
353
|
+
pe as useImportWallet,
|
|
354
|
+
g as useSeedPhrase,
|
|
355
|
+
le as useWalletCreate
|
|
279
356
|
};
|