@otoplo/wallet-common 0.2.0 → 0.2.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.
package/dist/index.js CHANGED
@@ -1,12 +1,12 @@
1
- import { AddressType as O, Networks as E, Address as I, CommonUtils as $, BufferUtils as m, HDPrivateKey as J, TransactionBuilder as j, Transaction as x, UnitUtils as z, Script as b, Opcode as A, Hash as _, ScriptOpcode as L, BNExtended as F, Output as we, TxSigner as q, SighashType as X, ScriptFactory as Ae, OutputSighashType as Y, BufferWriter as ve } from "libnexa-ts";
1
+ import { AddressType as O, Networks as E, Address as I, CommonUtils as W, BufferUtils as m, HDPrivateKey as j, TransactionBuilder as z, Transaction as x, UnitUtils as q, Script as b, Opcode as A, Hash as _, ScriptOpcode as L, BNExtended as F, Output as we, TxSigner as X, SighashType as Y, ScriptFactory as Ae, OutputSighashType as Z, BufferWriter as ve } from "libnexa-ts";
2
2
  import B from "js-big-decimal";
3
3
  import { ElectrumClient as be, TransportScheme as H, ConnectionStatus as Se } from "@otoplo/electrum-client";
4
4
  import { mnemonicToSeedSync as ke, generateMnemonic as Te, validateMnemonic as Ie } from "@scure/bip39";
5
5
  import { DAppProvider as xe } from "wallet-comms-sdk";
6
6
  import { wordlist as se } from "@scure/bip39/wordlists/english.js";
7
- import { createSlice as S, createAsyncThunk as Ne, createSelector as W } from "@reduxjs/toolkit";
7
+ import { createSlice as S, createAsyncThunk as Ne, createSelector as G } from "@reduxjs/toolkit";
8
8
  import { useSelector as Me } from "react-redux";
9
- const w = -1, Z = 9223372036854775807n, Ee = 274710, yt = "0014461ad25081cb0119d034385ff154c8d3ad6bdd76";
9
+ const w = -1, Q = 9223372036854775807n, Ee = 274710, yt = "0014461ad25081cb0119d034385ff154c8d3ad6bdd76";
10
10
  function C() {
11
11
  return E.defaultNetwork == E.testnet;
12
12
  }
@@ -19,7 +19,7 @@ function R(a, e = O.PayToScriptTemplate) {
19
19
  function At() {
20
20
  return `https://${C() ? "testnet-" : ""}explorer.nexa.org`;
21
21
  }
22
- function G() {
22
+ function J() {
23
23
  return Math.floor(Date.now() / 1e3);
24
24
  }
25
25
  function vt(a, e) {
@@ -39,18 +39,18 @@ function St(a) {
39
39
  return a.length === 0 ? "" : a.charAt(0).toUpperCase() + a.substring(1);
40
40
  }
41
41
  function ae(a) {
42
- return $.isHexa(a) ? m.hexToBuffer(a) : I.fromString(a).data;
42
+ return W.isHexa(a) ? m.hexToBuffer(a) : I.fromString(a).data;
43
43
  }
44
44
  function v(a) {
45
- return $.isHexa(a) ? a : m.bufferToHex(ae(a));
45
+ return W.isHexa(a) ? a : m.bufferToHex(ae(a));
46
46
  }
47
47
  function kt(a) {
48
- return $.isHexa(a) ? new I(ae(a), E.defaultNetwork, O.GroupIdAddress).toString() : a;
48
+ return W.isHexa(a) ? new I(ae(a), E.defaultNetwork, O.GroupIdAddress).toString() : a;
49
49
  }
50
50
  function U(a) {
51
51
  return a == null || a === "" ? "" : BigInt(a) <= 0n ? "0" : a.toString();
52
52
  }
53
- function Q(a) {
53
+ function ee(a) {
54
54
  let e = BigInt(0), t = BigInt(0);
55
55
  return a.forEach((s) => {
56
56
  e += BigInt(s.confirmed), t += BigInt(s.unconfirmed);
@@ -236,7 +236,7 @@ class Et {
236
236
  accountId: e,
237
237
  tokenIdHex: t.tokenIdHex,
238
238
  type: "token",
239
- addedTime: G()
239
+ addedTime: J()
240
240
  };
241
241
  await this.store.upsertToken(t), await this.saveAsset(s), this.notify({ type: "token_refresh" });
242
242
  }
@@ -270,9 +270,6 @@ class Et {
270
270
  async countLocalNfts(e) {
271
271
  return this.store.countNfts(e);
272
272
  }
273
- async isNftExist(e) {
274
- return this.store.isNftExist(e);
275
- }
276
273
  async getAssets(e, t) {
277
274
  return this.store.getAssets(e, t);
278
275
  }
@@ -559,12 +556,12 @@ class Pt {
559
556
  accountKeys = /* @__PURE__ */ new Map();
560
557
  walletKeys = /* @__PURE__ */ new Map();
561
558
  init(e) {
562
- this.seed = typeof e == "string" ? ke(e) : e, this.masterKey = J.fromSeed(this.seed).deriveChild(44, !0).deriveChild(29223, !0);
559
+ this.seed = typeof e == "string" ? ke(e) : e, this.masterKey = j.fromSeed(this.seed).deriveChild(44, !0).deriveChild(29223, !0);
563
560
  }
564
561
  reset() {
565
562
  if (!this.seed)
566
563
  throw new Error("KeysManager not initialized");
567
- this.masterKey = J.fromSeed(this.seed).deriveChild(44, !0).deriveChild(29223, !0), this.accountKeys.clear(), this.walletKeys.clear();
564
+ this.masterKey = j.fromSeed(this.seed).deriveChild(44, !0).deriveChild(29223, !0), this.accountKeys.clear(), this.walletKeys.clear();
568
565
  }
569
566
  getAccountKey(e) {
570
567
  let t = this.accountKeys.get(e);
@@ -690,7 +687,7 @@ class Ut {
690
687
  await this.deleteNft(e, o.tokenIdHex);
691
688
  for (const [o, r] of Object.entries(t)) {
692
689
  const c = v(o), d = r && BigInt(r.confirmed) + BigInt(r.unconfirmed) > 0n, u = n?.some((l) => l.tokenIdHex == c);
693
- !d && !u || d && u || (!d && u ? await this.deleteNft(e, c) : await this.handleNftReceive(e, c, G()));
690
+ !d && !u || d && u || (!d && u ? await this.deleteNft(e, c) : await this.handleNftReceive(e, c, J()));
694
691
  }
695
692
  }
696
693
  async deleteToken(e, t) {
@@ -774,7 +771,7 @@ class Dt {
774
771
  // Will be set in classifyAndSaveTransaction
775
772
  txId: s.txid,
776
773
  txIdem: s.txidem,
777
- time: d ? s.time : G(),
774
+ time: d ? s.time : J(),
778
775
  height: d ? s.height : 0,
779
776
  type: c,
780
777
  fee: s.fee_satoshi.toString(),
@@ -797,7 +794,7 @@ class Dt {
797
794
  const n = {
798
795
  isConsolidate: !0,
799
796
  templateData: s
800
- }, i = new j(), o = await this.populateNexaInputsAndChange(i, e, t, n);
797
+ }, i = new z(), o = await this.populateNexaInputsAndChange(i, e, t, n);
801
798
  return this.finalizeTransaction(i, Array.from(o.values()));
802
799
  }
803
800
  prepareTransaction(e, t, s, n) {
@@ -805,9 +802,9 @@ class Dt {
805
802
  throw new Error("Invalid Address.");
806
803
  if (s && BigInt(t) < 1n || !s && parseInt(t) < x.DUST_AMOUNT)
807
804
  throw new Error("The amount is too low.");
808
- if (s && BigInt(t) > Z || !s && parseInt(t) > x.MAX_MONEY)
805
+ if (s && BigInt(t) > Q || !s && parseInt(t) > x.MAX_MONEY)
809
806
  throw new Error("The amount is too high.");
810
- const i = new j();
807
+ const i = new z();
811
808
  if (n && i.addData(n), s) {
812
809
  if (!R(s, O.GroupIdAddress))
813
810
  throw new Error("Invalid Token ID");
@@ -868,8 +865,8 @@ class Dt {
868
865
  }
869
866
  const c = {
870
867
  errorMsg: "Not enough Nexa balance.",
871
- amount: z.formatNEXA(e.transaction.outputs[0].value),
872
- fee: z.formatNEXA(e.transaction.estimateRequiredFee())
868
+ amount: q.formatNEXA(e.transaction.outputs[0].value),
869
+ fee: q.formatNEXA(e.transaction.estimateRequiredFee())
873
870
  };
874
871
  throw new Error(JSON.stringify(c));
875
872
  }
@@ -893,7 +890,7 @@ class Dt {
893
890
  const f = this.keyManager.getKey(u.keyPath);
894
891
  c.set(u.address, f.privateKey);
895
892
  }
896
- if (d > Z)
893
+ if (d > Q)
897
894
  throw new Error("Token inputs exceeded max amount. Consider sending in small chunks");
898
895
  if (e.transaction.inputs.length > this.MAX_INPUTS_OUTPUTS)
899
896
  throw new Error("Too many inputs. Consider consolidating transactions or reduce the send amount.");
@@ -995,16 +992,16 @@ const ce = [
995
992
  { code: "jpy", symbol: "¥", name: "Japanese Yen" }
996
993
  ], De = Object.fromEntries(
997
994
  ce.map((a) => [a.code, a.symbol])
998
- ), Kt = ce.map((a) => a.code), ee = {
995
+ ), Kt = ce.map((a) => a.code), te = {
999
996
  nexacoin: "nexa"
1000
997
  };
1001
998
  async function Ve(a) {
1002
- const e = Object.keys(ee).join(","), t = await fetch(`https://api.coingecko.com/api/v3/coins/markets?ids=${e}&vs_currency=${a}`);
999
+ const e = Object.keys(te).join(","), t = await fetch(`https://api.coingecko.com/api/v3/coins/markets?ids=${e}&vs_currency=${a}`);
1003
1000
  if (!t.ok)
1004
1001
  throw new Error("Failed to fetch market data");
1005
1002
  const s = await t.json(), n = {};
1006
1003
  return s.forEach((i) => {
1007
- n[ee[i.id]] = {
1004
+ n[te[i.id]] = {
1008
1005
  id: i.id,
1009
1006
  name: i.name ?? "",
1010
1007
  symbol: i.symbol ?? "",
@@ -1260,12 +1257,12 @@ class Wt {
1260
1257
  const s = e.txDetails.tx.inputs[t], n = this.validateAndGetSubscript(s);
1261
1258
  if (s.scriptSig.isEmpty()) {
1262
1259
  e.allMyOutputsCovered = !0;
1263
- const i = q.sign(e.txDetails.tx, t, X.ALL, n, e.privateKey).toTxFormat(), o = b.empty().add(e.privateKey.publicKey.toBuffer());
1260
+ const i = X.sign(e.txDetails.tx, t, Y.ALL, n, e.privateKey).toTxFormat(), o = b.empty().add(e.privateKey.publicKey.toBuffer());
1264
1261
  s.scriptSig = Ae.buildScriptTemplateIn(void 0, o, b.empty().add(i));
1265
1262
  } else {
1266
- const i = s.scriptSig.findPlaceholder(), r = s.scriptSig.chunks[i].buf.subarray(64), c = X.fromBuffer(r);
1263
+ const i = s.scriptSig.findPlaceholder(), r = s.scriptSig.chunks[i].buf.subarray(64), c = Y.fromBuffer(r);
1267
1264
  this.processSignatureCoverage(e, c);
1268
- const d = q.sign(e.txDetails.tx, t, c, n, e.privateKey).toTxFormat(r);
1265
+ const d = X.sign(e.txDetails.tx, t, c, n, e.privateKey).toTxFormat(r);
1269
1266
  s.scriptSig.replaceChunk(i, b.empty().add(d));
1270
1267
  }
1271
1268
  }
@@ -1290,9 +1287,9 @@ class Wt {
1290
1287
  }
1291
1288
  processSignatureCoverage(e, t) {
1292
1289
  if (!e.allMyOutputsCovered)
1293
- if (t.outType == Y.TWO)
1290
+ if (t.outType == Z.TWO)
1294
1291
  e.coveredOutputs.add(t.outData[0]), e.coveredOutputs.add(t.outData[1]), this.checkSignatureCoverage(e);
1295
- else if (t.outType == Y.FIRSTN) {
1292
+ else if (t.outType == Z.FIRSTN) {
1296
1293
  const s = t.outData[0];
1297
1294
  for (let n = 0; n < s; n++)
1298
1295
  e.coveredOutputs.add(n);
@@ -1474,7 +1471,7 @@ class T {
1474
1471
  id: w,
1475
1472
  name: "Main Wallet",
1476
1473
  address: this.getReceiveAddress(),
1477
- balance: Q(e.map((t) => t.balance)),
1474
+ balance: ee(e.map((t) => t.balance)),
1478
1475
  tokensBalance: K(e.map((t) => t.tokensBalance)),
1479
1476
  sessions: {}
1480
1477
  }
@@ -1615,7 +1612,7 @@ class T {
1615
1612
  for (const r of o)
1616
1613
  t.set(r.tx_hash, r);
1617
1614
  await this.checkGapLimit(y.RECEIVE), await this.checkGapLimit(y.CHANGE);
1618
- const s = this.getMainAddresses(), n = Q(s.map((o) => o.balance)), i = K(s.map((o) => o.tokensBalance));
1615
+ const s = this.getMainAddresses(), n = ee(s.map((o) => o.balance)), i = K(s.map((o) => o.tokensBalance));
1619
1616
  this.notify({ type: "account_balance_updated", accountId: w, balance: n, tokensBalance: i }), this.notify({ type: "main_address_updated", address: this.getReceiveAddress() }), await this.postProcessActivity(w, Array.from(t.values()), s.map((o) => o.address), i);
1620
1617
  }
1621
1618
  async postProcessAccountsUpdate(e) {
@@ -1789,7 +1786,7 @@ const ze = {
1789
1786
  }), jt = le.actions, Qe = le.reducer, et = {
1790
1787
  currency: "usd",
1791
1788
  data: {}
1792
- }, te = Ne("market/updateMarketData", async (a, e) => {
1789
+ }, $ = Ne("market/updateMarketData", async (a, e) => {
1793
1790
  const t = e.getState();
1794
1791
  return await Ve(t.market.currency);
1795
1792
  }), he = S({
@@ -1804,15 +1801,13 @@ const ze = {
1804
1801
  }
1805
1802
  },
1806
1803
  extraReducers: (a) => {
1807
- a.addCase(te.fulfilled, (e, t) => {
1804
+ a.addCase($.fulfilled, (e, t) => {
1808
1805
  e.data = t.payload;
1809
- }).addCase(te.rejected, (e, t) => {
1806
+ }).addCase($.rejected, (e, t) => {
1810
1807
  console.error(t.error.message);
1811
1808
  });
1812
1809
  }
1813
- });
1814
- he.actions;
1815
- const tt = he.reducer, st = {
1810
+ }), zt = { updateMarketData: $ }, qt = he.actions, tt = he.reducer, st = {
1816
1811
  wallet: {},
1817
1812
  web3: {}
1818
1813
  }, pe = S({
@@ -1829,7 +1824,7 @@ const tt = he.reducer, st = {
1829
1824
  e.payload === "wallet" ? a.wallet = {} : e.payload === "web3" ? a.web3 = {} : e.payload === "all" && (a.wallet = {}, a.web3 = {});
1830
1825
  }
1831
1826
  }
1832
- }), zt = pe.actions, at = pe.reducer, nt = {
1827
+ }), Xt = pe.actions, at = pe.reducer, nt = {
1833
1828
  status: "Offline",
1834
1829
  height: 0,
1835
1830
  hasNetwork: !0,
@@ -1851,7 +1846,7 @@ const tt = he.reducer, st = {
1851
1846
  a.isSuspended = e.payload;
1852
1847
  }
1853
1848
  }
1854
- }), qt = fe.actions, it = fe.reducer, rt = {
1849
+ }), Yt = fe.actions, it = fe.reducer, rt = {
1855
1850
  isAuthorized: !1,
1856
1851
  selectedAccount: w,
1857
1852
  accounts: {
@@ -1922,7 +1917,7 @@ const tt = he.reducer, st = {
1922
1917
  a.accounts[e.payload.accountId].sessions = {};
1923
1918
  }
1924
1919
  }
1925
- }), Xt = me.actions, ot = me.reducer, Yt = {
1920
+ }), Zt = me.actions, ot = me.reducer, Qt = {
1926
1921
  loader: Qe,
1927
1922
  dapp: Ye,
1928
1923
  status: it,
@@ -1930,16 +1925,16 @@ const tt = he.reducer, st = {
1930
1925
  auth: qe,
1931
1926
  notifications: at,
1932
1927
  market: tt
1933
- }, g = Me.withTypes(), Zt = () => g((a) => a.auth), Qt = (a) => g((e) => e.market.data[a]), es = () => g((a) => a.status.height), ts = (a) => g((e) => e.wallet.accounts[a]), ss = () => g((a) => a.wallet.selectedAccount), as = () => g((a) => Math.max(...Object.keys(a.wallet.accounts).map(Number))), ns = (a) => g((e) => e.wallet.accounts[a].address), is = (a) => g((e) => e.wallet.accounts[a].balance), rs = (a, e) => g((t) => t.wallet.accounts[a].tokensBalance[e]), os = (a, e) => g((t) => t.wallet.accounts[a].sessions[e]), cs = () => g((a) => a.wallet.vaults), ge = W(
1928
+ }, g = Me.withTypes(), es = () => g((a) => a.auth), ts = (a) => g((e) => e.market.data[a]), ss = () => g((a) => a.status.height), as = (a) => g((e) => e.wallet.accounts[a]), ns = () => g((a) => a.wallet.selectedAccount), is = () => g((a) => Math.max(...Object.keys(a.wallet.accounts).map(Number))), rs = (a) => g((e) => e.wallet.accounts[a].address), os = (a) => g((e) => e.wallet.accounts[a].balance), cs = (a, e) => g((t) => t.wallet.accounts[a].tokensBalance[e]), ds = (a, e) => g((t) => t.wallet.accounts[a].sessions[e]), us = () => g((a) => a.wallet.vaults), ge = G(
1934
1929
  [(a) => a.notifications.wallet],
1935
1930
  (a) => Object.values(a).sort((e, t) => t.createdAt - e.createdAt)
1936
- ), ye = W(
1931
+ ), ye = G(
1937
1932
  [(a) => a.notifications.web3],
1938
1933
  (a) => Object.values(a).sort((e, t) => t.createdAt - e.createdAt)
1939
- ), ct = W(
1934
+ ), ct = G(
1940
1935
  [ge, ye],
1941
1936
  (a, e) => [...a, ...e].sort((t, s) => s.createdAt - t.createdAt)
1942
- ), ds = () => g(ge), us = () => g(ye), ls = () => g(ct);
1937
+ ), ls = () => g(ge), hs = () => g(ye), ps = () => g(ct);
1943
1938
  export {
1944
1939
  h as AccountType,
1945
1940
  Ut as AssetService,
@@ -1948,9 +1943,9 @@ export {
1948
1943
  Pt as KeyManager,
1949
1944
  y as KeySpace,
1950
1945
  w as MAIN_WALLET_ID,
1951
- Z as MAX_INT64,
1946
+ Q as MAX_INT64,
1952
1947
  ie as RostrumService,
1953
- ee as SUPPORTED_TOKENS,
1948
+ te as SUPPORTED_TOKENS,
1954
1949
  $t as SessionManager,
1955
1950
  N as SessionRequestType,
1956
1951
  Dt as TransactionService,
@@ -1966,7 +1961,7 @@ export {
1966
1961
  St as capitalizeFirstLetter,
1967
1962
  Kt as currencyCodes,
1968
1963
  De as currencySymbols,
1969
- G as currentTimestamp,
1964
+ J as currentTimestamp,
1970
1965
  Jt as dappModalActions,
1971
1966
  Ye as dappModalReducer,
1972
1967
  Ht as encryptMnemonic,
@@ -1995,38 +1990,41 @@ export {
1995
1990
  k as keyPathToString,
1996
1991
  jt as loaderActions,
1997
1992
  Qe as loaderReducer,
1998
- zt as notificationsActions,
1993
+ qt as marketActions,
1994
+ tt as marketReducer,
1995
+ zt as marketThunks,
1996
+ Xt as notificationsActions,
1999
1997
  at as notificationsReducer,
2000
1998
  Ot as parseTokenDataUrl,
2001
1999
  Nt as prettifyAmount,
2002
- Yt as sharedReducers,
2000
+ Qt as sharedReducers,
2003
2001
  xt as sleep,
2004
- qt as statusActions,
2002
+ Yt as statusActions,
2005
2003
  it as statusReducer,
2006
2004
  Ce as stringToKeyPath,
2007
- Q as sumBalance,
2005
+ ee as sumBalance,
2008
2006
  K as sumTokensBalance,
2009
2007
  U as tokenAmountToAssetAmount,
2010
2008
  kt as tokenHexToAddr,
2011
2009
  v as tokenIdToHex,
2012
2010
  Bt as transformTokenIconUrl,
2013
2011
  bt as truncateStringMiddle,
2014
- ts as useAccount,
2015
- ls as useAllNotifications,
2016
- Zt as useAuth,
2017
- es as useBlockHeight,
2018
- os as useDAppSession,
2019
- as as useLastAccountId,
2020
- ns as useMainReceiveAddress,
2021
- Qt as useMarketData,
2022
- ss as useSelectedAccount,
2023
- rs as useTokenBalance,
2024
- cs as useVaults,
2025
- is as useWalletBalance,
2026
- ds as useWalletNotifications,
2027
- us as useWeb3Notifications,
2012
+ as as useAccount,
2013
+ ps as useAllNotifications,
2014
+ es as useAuth,
2015
+ ss as useBlockHeight,
2016
+ ds as useDAppSession,
2017
+ is as useLastAccountId,
2018
+ rs as useMainReceiveAddress,
2019
+ ts as useMarketData,
2020
+ ns as useSelectedAccount,
2021
+ cs as useTokenBalance,
2022
+ us as useVaults,
2023
+ os as useWalletBalance,
2024
+ ls as useWalletNotifications,
2025
+ hs as useWeb3Notifications,
2028
2026
  Rt as validateAndDecryptMnemonic,
2029
- Xt as walletActions,
2027
+ Zt as walletActions,
2030
2028
  ot as walletReducer
2031
2029
  };
2032
2030
  //# sourceMappingURL=index.js.map