@ecency/sdk 1.0.2 → 1.0.5

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.
@@ -62,6 +62,8 @@ declare interface Payload_3 {
62
62
  privateKeyOrSeed: string;
63
63
  }
64
64
 
65
+ export declare function useAccountUpdate(username: string): UseMutationResult<any, Error, any, unknown>;
66
+
65
67
  declare function useCheckWalletExistence(): UseMutationResult<boolean, Error, Payload_2, unknown>;
66
68
 
67
69
  export declare function useCoinGeckoPriceQuery(currency?: EcencyWalletCurrency): UseQueryResult<number, Error>;
@@ -86,6 +88,8 @@ address: any;
86
88
  publicKey: string;
87
89
  }, Error, Payload_3, unknown>;
88
90
 
91
+ export declare function useSaveWalletInformationToMetadata(username: string): UseMutationResult<any, Error, void, unknown>;
92
+
89
93
  export declare function useSeedPhrase(username: string): UseQueryResult<string, Error>;
90
94
 
91
95
  export declare function useWalletCreate(username: string, currency: EcencyWalletCurrency): {
@@ -1,62 +1,57 @@
1
- import { useCallback as O } from "react";
2
- import { useQuery as p, useQueryClient as y, useMutation as m } from "@tanstack/react-query";
3
- import { BtcWallet as R } 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";
1
+ import { useCallback as k } from "react";
2
+ import { useQuery as p, useQueryClient as f, useMutation as u } from "@tanstack/react-query";
3
+ import { BtcWallet as O } from "@okxweb3/coin-bitcoin";
4
+ import { EthWallet as A } from "@okxweb3/coin-ethereum";
5
+ import { TrxWallet as R } from "@okxweb3/coin-tron";
6
+ import { TonWallet as K } from "@okxweb3/coin-ton";
7
7
  import { SolWallet as N } from "@okxweb3/coin-solana";
8
- import { AtomWallet as K } from "@okxweb3/coin-cosmos";
9
- import { AptosWallet as C } from "@okxweb3/coin-aptos";
10
- import D, { mnemonicToSeedSync as H } from "bip39";
8
+ import { AtomWallet as C } from "@okxweb3/coin-cosmos";
9
+ import { AptosWallet as x } from "@okxweb3/coin-aptos";
10
+ import D, { mnemonicToSeedSync as _ } from "bip39";
11
11
  import { LRUCache as j } from "lru-cache";
12
- import { PrivateKey as u } from "@hiveio/dhive";
12
+ import { PrivateKey as m, Client as H } from "@hiveio/dhive";
13
+ import E from "hivesigner";
13
14
  var o = /* @__PURE__ */ ((e) => (e.BTC = "BTC", e.ETH = "ETH", e.APT = "APT", e.ATOM = "ATOM", e.TON = "TON", e.TRON = "TRX", e.SOL = "SOL", e))(o || {});
14
- function x(e) {
15
+ function W(e) {
15
16
  return new Promise((t) => setTimeout(t, e));
16
17
  }
17
- function h(e) {
18
+ function g(e) {
18
19
  switch (e) {
19
20
  case o.BTC:
20
- return new R();
21
+ return new O();
21
22
  case o.ETH:
22
- return new v();
23
+ return new A();
23
24
  case o.TRON:
24
- return new k();
25
+ return new R();
25
26
  case o.TON:
26
- return new A();
27
+ return new K();
27
28
  case o.SOL:
28
29
  return new N();
29
30
  case o.ATOM:
30
- return new K();
31
- case o.APT:
32
31
  return new C();
32
+ case o.APT:
33
+ return new x();
33
34
  default:
34
35
  return;
35
36
  }
36
37
  }
37
- function ce(e) {
38
- return H(e).toString("hex");
38
+ function ye(e) {
39
+ return _(e).toString("hex");
39
40
  }
40
- function ie(e, t) {
41
+ function fe(e, t) {
41
42
  return p({
42
43
  queryKey: ["ecency-wallets", "external-wallet-balance", e, t],
43
44
  queryFn: async () => {
44
45
  switch (e) {
45
46
  case o.BTC:
46
- const r = await (await fetch(
47
+ const s = await (await fetch(
47
48
  `https://mempool.space/api/address/${t}`
48
49
  )).json();
49
- return (r.chain_stats.funded_txo_sum - r.chain_stats.spent_txo_sum) / 1e8;
50
+ return (s.chain_stats.funded_txo_sum - s.chain_stats.spent_txo_sum) / 1e8;
50
51
  case o.ETH:
51
- return +(await (await fetch("https://eth.llamarpc.com", {
52
- method: "POST",
53
- body: JSON.stringify({
54
- jsonrpc: "2.0",
55
- id: "1",
56
- method: "eth_getBalance",
57
- params: [t, "latest"]
58
- })
59
- })).json()).result / 1e18;
52
+ return +(await (await fetch(
53
+ `https://api.ethplorer.io/getAddressInfo/${t}?apiKey=freekey`
54
+ )).json()).ETH.balance;
60
55
  case o.SOL:
61
56
  return (await (await fetch(
62
57
  "https://api.mainnet-beta.solana.com",
@@ -79,12 +74,12 @@ function ie(e, t) {
79
74
  `https://tonapi.io/v1/blockchain/getAccount?account=${t}`
80
75
  )).json()).balance / 1e9;
81
76
  case o.APT:
82
- const d = (await (await fetch(
77
+ const y = (await (await fetch(
83
78
  `https://fullnode.mainnet.aptoslabs.com/v1/accounts/${t}/resources`
84
79
  )).json()).find(
85
80
  (S) => S.type.includes("coin::CoinStore")
86
81
  );
87
- return d ? parseInt(d.data.coin.value) / 1e8 : 0;
82
+ return y ? parseInt(y.data.coin.value) / 1e8 : 0;
88
83
  case o.ATOM:
89
84
  return +(await (await fetch(
90
85
  `https://rest.cosmos.directory/cosmoshub/auth/accounts/${t}`
@@ -93,13 +88,13 @@ function ie(e, t) {
93
88
  }
94
89
  });
95
90
  }
96
- function f(e) {
91
+ function T(e) {
97
92
  return p({
98
93
  queryKey: ["ecency-wallets", "seed", e],
99
94
  queryFn: async () => D.generateMnemonic(128)
100
95
  });
101
96
  }
102
- const W = {
97
+ const B = {
103
98
  max: 500,
104
99
  // how long to live in ms
105
100
  ttl: 1e3 * 60 * 5,
@@ -107,11 +102,11 @@ const W = {
107
102
  allowStale: !1,
108
103
  updateAgeOnGet: !1,
109
104
  updateAgeOnHas: !1
110
- }, T = new j(W), w = Symbol("undefined"), _ = (e, t) => T.set(e, t === void 0 ? w : t), B = (e) => {
111
- const t = T.get(e);
105
+ }, b = new j(B), w = Symbol("undefined"), F = (e, t) => b.set(e, t === void 0 ? w : t), M = (e) => {
106
+ const t = b.get(e);
112
107
  return t === w ? void 0 : t;
113
108
  };
114
- function le(e) {
109
+ function ge(e) {
115
110
  return p({
116
111
  queryKey: ["ecency-wallets", "coingecko-price", e],
117
112
  queryFn: async () => {
@@ -132,11 +127,11 @@ function le(e) {
132
127
  default:
133
128
  t = e;
134
129
  }
135
- let n = B("gecko"), r;
130
+ let n = M("gecko"), s;
136
131
  if (n)
137
- r = n;
132
+ s = n;
138
133
  else {
139
- const a = await (await fetch(
134
+ const r = await (await fetch(
140
135
  "https://api.coingecko.com/api/v3/simple/price",
141
136
  {
142
137
  method: "POST",
@@ -148,30 +143,30 @@ function le(e) {
148
143
  })
149
144
  }
150
145
  )).json();
151
- _("gecko", a === void 0 ? w : a), r = a;
146
+ F("gecko", r === void 0 ? w : r), s = r;
152
147
  }
153
- return 1 / +r[Object.keys(r)[0]].usd;
148
+ return 1 / +s[Object.keys(s)[0]].usd;
154
149
  },
155
150
  enabled: !!e
156
151
  });
157
152
  }
158
- function E(e) {
159
- const { data: t } = f(e);
153
+ function q(e) {
154
+ const { data: t } = T(e);
160
155
  return p({
161
156
  queryKey: ["ecencу-wallets", "hive-keys", e, t],
162
157
  queryFn: async () => {
163
158
  if (!t)
164
159
  throw new Error("[Ecency][Wallets] - no seed to create Hive account");
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");
160
+ const n = m.fromLogin(e, t, "owner"), s = m.fromLogin(e, t, "active"), a = m.fromLogin(e, t, "posting"), i = m.fromLogin(e, t, "memo");
166
161
  return {
167
162
  username: e,
168
163
  owner: n.toString(),
169
- active: r.toString(),
170
- posting: s.toString(),
164
+ active: s.toString(),
165
+ posting: a.toString(),
171
166
  memo: i.toString(),
172
167
  ownerPubkey: n.createPublic().toString(),
173
- activePubkey: r.createPublic().toString(),
174
- postingPubkey: s.createPublic().toString(),
168
+ activePubkey: s.createPublic().toString(),
169
+ postingPubkey: a.createPublic().toString(),
175
170
  memoPubkey: i.createPublic().toString()
176
171
  };
177
172
  }
@@ -193,18 +188,18 @@ const L = {
193
188
  [o.ATOM]: "m/44'/118'/0'/0/0"
194
189
  // Cosmos (BIP44)
195
190
  };
196
- function pe(e, t) {
197
- const { data: n } = f(e), r = y(), s = m({
191
+ function Te(e, t) {
192
+ const { data: n } = T(e), s = f(), a = u({
198
193
  mutationKey: ["ecency-wallets", "create-wallet", e, t],
199
194
  mutationFn: async () => {
200
195
  if (!n)
201
196
  throw new Error("[Ecency][Wallets] - No seed to create a wallet");
202
- const a = h(t), c = await (a == null ? void 0 : a.getDerivedPrivateKey({
197
+ const r = g(t), c = await (r == null ? void 0 : r.getDerivedPrivateKey({
203
198
  mnemonic: n,
204
199
  hdPath: L[t]
205
200
  }));
206
- await x(1e3);
207
- const l = await (a == null ? void 0 : a.getNewAddress({
201
+ await W(1e3);
202
+ const l = await (r == null ? void 0 : r.getNewAddress({
208
203
  privateKey: c
209
204
  }));
210
205
  return {
@@ -215,44 +210,68 @@ function pe(e, t) {
215
210
  currency: t
216
211
  };
217
212
  },
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)
213
+ onSuccess: (r) => {
214
+ s.setQueryData(
215
+ ["ecency-wallets", "wallets", r.username],
216
+ (c) => new Map(c ? Array.from(c.entries()) : []).set(r.currency, r)
222
217
  );
223
218
  }
224
- }), i = O(() => {
219
+ }), i = k(() => {
225
220
  }, []);
226
221
  return {
227
- createWallet: s,
222
+ createWallet: a,
228
223
  importWallet: i
229
224
  };
230
225
  }
231
- const b = { privateApiHost: "https://ecency.com" };
232
- function q(e) {
226
+ const d = {
227
+ privateApiHost: "https://ecency.com",
228
+ storage: localStorage,
229
+ storagePrefix: "ecency",
230
+ hiveClient: new H(
231
+ [
232
+ "https://api.hive.blog",
233
+ "https://api.deathwing.me",
234
+ "https://rpc.mahdiyari.info",
235
+ "https://api.openhive.network",
236
+ "https://techcoderx.com",
237
+ "https://hive-api.arcange.eu",
238
+ "https://api.syncad.com",
239
+ "https://anyx.io",
240
+ "https://api.c0ff33a.uk",
241
+ "https://hiveapi.actifit.io",
242
+ "https://hive-api.3speak.tv"
243
+ ],
244
+ {
245
+ timeout: 2e3,
246
+ failoverThreshold: 2,
247
+ consoleOnFailover: !0
248
+ }
249
+ )
250
+ };
251
+ function J(e) {
233
252
  const { data: t } = p({
234
253
  queryKey: ["ecency-wallets", "wallets", e]
235
- }), { data: n } = E(e);
236
- return m({
254
+ }), { data: n } = q(e);
255
+ return u({
237
256
  mutationKey: ["ecency-wallets", "create-account-with-wallets", e],
238
- mutationFn: ({ currency: r, address: s }) => fetch(b.privateApiHost + "/private-api/wallets-add", {
257
+ mutationFn: ({ currency: s, address: a }) => fetch(d.privateApiHost + "/private-api/wallets-add", {
239
258
  method: "POST",
240
259
  headers: {
241
260
  "Content-Type": "application/json"
242
261
  },
243
262
  body: JSON.stringify({
244
263
  username: e,
245
- token: r,
246
- address: s,
264
+ token: s,
265
+ address: a,
247
266
  meta: {
248
267
  ownerPublicKey: n == null ? void 0 : n.ownerPubkey,
249
268
  activePublicKey: n == null ? void 0 : n.activePubkey,
250
269
  postingPublicKey: n == null ? void 0 : n.postingPubkey,
251
270
  memoPublicKey: n == null ? void 0 : n.memoPubkey,
252
271
  ...Array.from((t == null ? void 0 : t.entries()) ?? []).reduce(
253
- (i, [a, c]) => ({
272
+ (i, [r, c]) => ({
254
273
  ...i,
255
- [a]: c.address
274
+ [r]: c.address
256
275
  }),
257
276
  {}
258
277
  )
@@ -261,11 +280,11 @@ function q(e) {
261
280
  })
262
281
  });
263
282
  }
264
- function g() {
265
- return m({
283
+ function v() {
284
+ return u({
266
285
  mutationKey: ["ecency-wallets", "check-wallet-existence"],
267
286
  mutationFn: async ({ address: e, currency: t }) => (await (await fetch(
268
- b.privateApiHost + "/private-api/wallets-exist",
287
+ d.privateApiHost + "/private-api/wallets-exist",
269
288
  {
270
289
  method: "POST",
271
290
  headers: {
@@ -279,11 +298,11 @@ function g() {
279
298
  )).json()).length === 0
280
299
  });
281
300
  }
282
- const ue = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
301
+ const be = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
283
302
  __proto__: null,
284
- useCheckWalletExistence: g,
285
- useCreateAccountWithWallets: q
286
- }, Symbol.toStringTag, { value: "Module" })), M = {
303
+ useCheckWalletExistence: v,
304
+ useCreateAccountWithWallets: J
305
+ }, Symbol.toStringTag, { value: "Module" })), Q = {
287
306
  [o.BTC]: ["m/84'/0'/0'/0/0"],
288
307
  [o.ETH]: ["m/84'/60'/0'/0/0"],
289
308
  // its not working for Trust, Exodus, todo: check others below
@@ -294,43 +313,43 @@ const ue = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
294
313
  // Disabled
295
314
  [o.ATOM]: ["m/84'/118'/0'/0'/0"]
296
315
  };
297
- async function F(e, t, n) {
298
- for (const r of M[n] || [])
316
+ async function I(e, t, n) {
317
+ for (const s of Q[n] || [])
299
318
  try {
300
- const s = await t.getDerivedPrivateKey({
319
+ const a = await t.getDerivedPrivateKey({
301
320
  mnemonic: e,
302
- hdPath: r
321
+ hdPath: s
303
322
  }), i = await t.getNewAddress({
304
- privateKey: s,
323
+ privateKey: a,
305
324
  addressType: n === o.BTC ? "segwit_native" : void 0
306
325
  });
307
- return [s.toString(), i.address];
326
+ return [a.toString(), i.address];
308
327
  } catch {
309
328
  return [];
310
329
  }
311
330
  return [];
312
331
  }
313
- function me(e, t) {
314
- const n = y(), { mutateAsync: r } = g();
315
- return m({
332
+ function ve(e, t) {
333
+ const n = f(), { mutateAsync: s } = v();
334
+ return u({
316
335
  mutationKey: ["ecency-wallets", "import-wallet", e, t],
317
- mutationFn: async ({ privateKeyOrSeed: s }) => {
318
- const i = h(t);
336
+ mutationFn: async ({ privateKeyOrSeed: a }) => {
337
+ const i = g(t);
319
338
  if (!i)
320
339
  throw new Error("Cannot find token for this currency");
321
- const a = s.split(" ").length === 12;
322
- let c, l = s;
323
- if (a ? [l, c] = await F(
324
- s,
340
+ const r = a.split(" ").length === 12;
341
+ let c, l = a;
342
+ if (r ? [l, c] = await I(
343
+ a,
325
344
  i,
326
345
  t
327
346
  ) : c = (await i.getNewAddress({
328
- privateKey: s
329
- })).address, !c || !s)
347
+ privateKey: a
348
+ })).address, !c || !a)
330
349
  throw new Error(
331
350
  "Private key/seed phrase isn't matching with public key or token"
332
351
  );
333
- if (!await r({
352
+ if (!await s({
334
353
  address: c,
335
354
  currency: t
336
355
  }))
@@ -343,13 +362,13 @@ function me(e, t) {
343
362
  publicKey: ""
344
363
  };
345
364
  },
346
- onSuccess: ({ privateKey: s, publicKey: i, address: a }) => {
365
+ onSuccess: ({ privateKey: a, publicKey: i, address: r }) => {
347
366
  n.setQueryData(
348
367
  ["ecency-wallets", "wallets", e],
349
368
  (c) => new Map(c ? Array.from(c.entries()) : []).set(t, {
350
- privateKey: s,
369
+ privateKey: a,
351
370
  publicKey: i,
352
- address: a,
371
+ address: r,
353
372
  username: e,
354
373
  currency: t,
355
374
  custom: !0
@@ -358,16 +377,93 @@ function me(e, t) {
358
377
  }
359
378
  });
360
379
  }
380
+ function $(e) {
381
+ let t = atob(e);
382
+ if (t[0] === "{")
383
+ return JSON.parse(t);
384
+ }
385
+ const h = (e) => {
386
+ try {
387
+ const t = d.storage.getItem(
388
+ d.storagePrefix + "_user_" + e
389
+ );
390
+ return $(JSON.parse(t));
391
+ } catch (t) {
392
+ console.error(t);
393
+ return;
394
+ }
395
+ }, G = (e) => h(e) && h(e).accessToken, U = (e) => h(e) && h(e).postingKey;
396
+ function V(e = [], t, n) {
397
+ return u({
398
+ mutationKey: [...e, t],
399
+ mutationFn: async (s) => {
400
+ const a = U(t);
401
+ if (a) {
402
+ const r = m.fromString(a);
403
+ return d.hiveClient.broadcast.sendOperations(
404
+ n(s),
405
+ r
406
+ );
407
+ }
408
+ let i = G(t);
409
+ if (i)
410
+ return (await new E.Client({
411
+ accessToken: i
412
+ }).broadcast(n(s))).result;
413
+ throw new Error(
414
+ "[SDK][Broadcast] – cannot broadcast w/o posting key or token"
415
+ );
416
+ }
417
+ });
418
+ }
419
+ function z(e) {
420
+ return V(
421
+ ["accounts", "update"],
422
+ e,
423
+ ({ newProfile: t }) => [
424
+ [
425
+ "account_update2",
426
+ {
427
+ account: e,
428
+ json_metadata: "",
429
+ posting_json_metadata: JSON.stringify({
430
+ profile: { ...t, version: 2 }
431
+ }),
432
+ extensions: []
433
+ }
434
+ ]
435
+ ]
436
+ );
437
+ }
438
+ function Pe(e) {
439
+ const { data: t } = p({
440
+ queryKey: ["ecency-wallets", "wallets", e]
441
+ }), { mutateAsync: n } = z(e);
442
+ return u({
443
+ mutationKey: ["ecency-wallets", "save-wallet-to-metadata", e],
444
+ mutationFn: () => n({
445
+ ...Array.from((t == null ? void 0 : t.entries()) ?? []).reduce(
446
+ (s, [a, i]) => ({
447
+ ...s,
448
+ [a]: i.address
449
+ }),
450
+ {}
451
+ )
452
+ })
453
+ });
454
+ }
361
455
  export {
362
456
  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
- E as useHiveKeysQuery,
370
- me as useImportWallet,
371
- f as useSeedPhrase,
372
- pe as useWalletCreate
457
+ be as EcencyWalletsPrivateApi,
458
+ W as delay,
459
+ g as getWallet,
460
+ ye as mnemonicToSeedBip39,
461
+ z as useAccountUpdate,
462
+ ge as useCoinGeckoPriceQuery,
463
+ fe as useGetExternalWalletBalanceQuery,
464
+ q as useHiveKeysQuery,
465
+ ve as useImportWallet,
466
+ Pe as useSaveWalletInformationToMetadata,
467
+ T as useSeedPhrase,
468
+ Te as useWalletCreate
373
469
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ecency/sdk",
3
3
  "private": false,
4
- "version": "1.0.2",
4
+ "version": "1.0.5",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "main": "./dist/ecency-sdk.umd.js",
@@ -41,6 +41,7 @@
41
41
  "@okxweb3/coin-ton": "^1.1.1-beta.1",
42
42
  "@okxweb3/coin-tron": "^1.1.0",
43
43
  "@okxweb3/crypto-lib": "^1.0.10",
44
- "bip39": "^3.1.0"
44
+ "bip39": "^3.1.0",
45
+ "hivesigner": "3.3.4"
45
46
  }
46
47
  }