btc-wallet 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -1853,12 +1853,12 @@ var ConnectProvider = ({
1853
1853
  version: ((_c = options.aaOptions.accountContracts[((_b = Object.keys(options.aaOptions.accountContracts)) == null ? void 0 : _b[0]) || "BTC"]) == null ? void 0 : _c[0].version) || "1.0.0"
1854
1854
  });
1855
1855
  const setAccountContract = (0, import_react10.useCallback)(
1856
- (config) => {
1857
- if (!checkBTCVersion(options.aaOptions.accountContracts, config.name, config.version)) {
1856
+ (config2) => {
1857
+ if (!checkBTCVersion(options.aaOptions.accountContracts, config2.name, config2.version)) {
1858
1858
  throw new Error("Invalid Account Contract");
1859
1859
  }
1860
- localStorage.setItem(SAContractKey, JSON.stringify(config));
1861
- _setAccountContract(config);
1860
+ localStorage.setItem(SAContractKey, JSON.stringify(config2));
1861
+ _setAccountContract(config2);
1862
1862
  },
1863
1863
  [options.aaOptions.accountContracts, _setAccountContract]
1864
1864
  );
@@ -2017,9 +2017,9 @@ var ConnectProvider = ({
2017
2017
  setConnectorId(void 0);
2018
2018
  }, [connector]);
2019
2019
  (0, import_react10.useEffect)(() => {
2020
- const config = JSON.parse(localStorage.getItem(SAContractKey) || "{}");
2021
- if (config.name && config.version && checkBTCVersion(options.aaOptions.accountContracts, config.name, config.version)) {
2022
- _setAccountContract(config);
2020
+ const config2 = JSON.parse(localStorage.getItem(SAContractKey) || "{}");
2021
+ if (config2.name && config2.version && checkBTCVersion(options.aaOptions.accountContracts, config2.name, config2.version)) {
2022
+ _setAccountContract(config2);
2023
2023
  }
2024
2024
  }, [options.aaOptions.accountContracts, _setAccountContract]);
2025
2025
  (0, import_react10.useEffect)(() => {
@@ -2478,10 +2478,19 @@ function removeWalletButton() {
2478
2478
  }
2479
2479
 
2480
2480
  // src/utils/setupBTCWallet.ts
2481
- var { signedDelegate, transfer, functionCall } = import_transactions.actionCreators;
2482
- var base_url = "https://api.dev.satoshibridge.top/v1";
2483
- var token = "nbtc1-nsp.testnet";
2484
- var contractId = "dev1-nsp.testnet";
2481
+ var { transfer, functionCall } = import_transactions.actionCreators;
2482
+ var config = {
2483
+ testnet: {
2484
+ base_url: "https://api.testnet.satoshibridge.top/v1",
2485
+ token: "nbtc2-nsp.testnet",
2486
+ contractId: "dev2-nsp.testnet"
2487
+ },
2488
+ mainnet: {
2489
+ base_url: "https://api.mainnet.satoshibridge.top/v1",
2490
+ token: "",
2491
+ contractId: ""
2492
+ }
2493
+ };
2485
2494
  var state = {
2486
2495
  saveAccount(account) {
2487
2496
  window.localStorage.setItem("btc-wallet-account", account);
@@ -2548,14 +2557,9 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2548
2557
  const accountId = state.getAccount();
2549
2558
  initWalletButton(options.network.networkId, accountId, wallet);
2550
2559
  context.on("updatePublicKey", (btcPublicKey) => __async(void 0, null, function* () {
2551
- const { nearTempAddress, nearTempPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
2552
- removeWalletButton();
2560
+ const { nearTempAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
2553
2561
  setTimeout(() => {
2554
- initWalletButton(
2555
- options.network.networkId,
2556
- nearTempAddress,
2557
- wallet
2558
- );
2562
+ initWalletButton(options.network.networkId, nearTempAddress, wallet);
2559
2563
  }, 1e3);
2560
2564
  emitter.emit("accountsChanged", {
2561
2565
  accounts: [
@@ -2571,20 +2575,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2571
2575
  });
2572
2576
  }));
2573
2577
  if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
2574
- btcContext.autoConnect();
2578
+ yield btcContext.autoConnect();
2575
2579
  }
2576
2580
  clearInterval(inter);
2577
2581
  }
2578
2582
  }), 500);
2579
2583
  }
2580
2584
  function viewMethod(_02) {
2581
- return __async(this, arguments, function* ({
2582
- method,
2583
- args = {}
2584
- }) {
2585
+ return __async(this, arguments, function* ({ method, args = {} }) {
2585
2586
  const res = yield provider.query({
2586
2587
  request_type: "call_function",
2587
- account_id: contractId,
2588
+ account_id: config[options.network.networkId].contractId,
2588
2589
  method_name: method,
2589
2590
  args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
2590
2591
  finality: "optimistic"
@@ -2612,7 +2613,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2612
2613
  });
2613
2614
  }
2614
2615
  function signIn(_02) {
2615
- return __async(this, arguments, function* ({ contractId: contractId2, methodNames }) {
2616
+ return __async(this, arguments, function* ({ contractId, methodNames }) {
2616
2617
  const accountId = state.getAccount();
2617
2618
  const publicKey = state.getPublicKey();
2618
2619
  const btcContext = window.btcContext;
@@ -2686,11 +2687,11 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2686
2687
  nonce: BigInt(rawAccessKey.nonce || 0)
2687
2688
  });
2688
2689
  const publicKeyFormat = import_key_pair.PublicKey.from(publicKey);
2689
- const nearNonceApi = yield getNearNonceFromApi(accountId);
2690
+ const nearNonceApi = yield getNearNonceFromApi(options.network.networkId, accountId);
2690
2691
  const newTransactions = params.transactions.map((transaction, index) => {
2691
2692
  let nearNonceNumber = accessKey.nonce + BigInt(1);
2692
2693
  if (nearNonceApi) {
2693
- nearNonceNumber = Number(nearNonceApi.result_data) > nearNonceNumber ? BigInt(Number(nearNonceApi.result_data)) : nearNonceNumber;
2694
+ nearNonceNumber = BigInt(nearNonceApi.result_data) > nearNonceNumber ? BigInt(nearNonceApi.result_data) : nearNonceNumber;
2694
2695
  }
2695
2696
  const newActions = transaction.actions.map((action) => {
2696
2697
  switch (action.type) {
@@ -2714,8 +2715,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2714
2715
  (0, import_utils5.baseDecode)(header.hash)
2715
2716
  );
2716
2717
  const txBytes = (0, import_transaction.encodeTransaction)(_transaction);
2717
- const txHex = Array.from(txBytes, (byte) => ("0" + (byte & 255).toString(16)).slice(-2)).join("");
2718
- ;
2718
+ const txHex = Array.from(
2719
+ txBytes,
2720
+ (byte) => ("0" + (byte & 255).toString(16)).slice(-2)
2721
+ ).join("");
2719
2722
  const hash = import_bs58.default.encode(new Uint8Array(import_js_sha256.sha256.array(txBytes)));
2720
2723
  return { txBytes, txHex, hash };
2721
2724
  });
@@ -2723,29 +2726,26 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2723
2726
  method: "get_account",
2724
2727
  args: { account_id: accountId }
2725
2728
  });
2726
- const nonceApi = yield getNonceFromApi(accountId);
2727
- const nonce = (nonceApi == null ? void 0 : nonceApi.result_data) ? Number(nonceApi == null ? void 0 : nonceApi.result_data) : accountInfo.nonce;
2729
+ const nonceApi = yield getNonceFromApi(options.network.networkId, accountId);
2730
+ const nonce = Number(nonceApi == null ? void 0 : nonceApi.result_data) > Number(accountInfo.nonce) ? String(nonceApi == null ? void 0 : nonceApi.result_data) : String(accountInfo.nonce);
2728
2731
  const intention = {
2729
2732
  chain_id: "397",
2730
2733
  csna: accountId,
2731
2734
  near_transactions: newTransactions.map((t) => t.txHex),
2732
- gas_token: token,
2735
+ gas_token: config[options.network.networkId].token,
2733
2736
  gas_limit: "3000",
2734
- nonce: Number(nonce).toString()
2737
+ nonce
2735
2738
  };
2736
2739
  const strIntention = JSON.stringify(intention);
2737
2740
  const signature = yield btcContext.signMessage(strIntention);
2738
- const result = yield uploadBTCTx({
2741
+ const result = yield uploadBTCTx(options.network.networkId, {
2739
2742
  sig: signature,
2740
2743
  btcPubKey: state.getBtcPublicKey(),
2741
2744
  data: toHex(strIntention)
2742
2745
  });
2743
2746
  if (result.result_code === 0) {
2744
2747
  const hash = newTransactions.map((t) => t.hash);
2745
- const result2 = yield pollTransactionStatuses(
2746
- options.network.networkId,
2747
- hash
2748
- );
2748
+ const result2 = yield pollTransactionStatuses(options.network.networkId, hash);
2749
2749
  return result2;
2750
2750
  } else {
2751
2751
  return null;
@@ -2754,24 +2754,24 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2754
2754
  }
2755
2755
  return wallet;
2756
2756
  });
2757
- function getNonceFromApi(accountId) {
2758
- return fetch(`${base_url}/nonce?csna=${accountId}`, {
2757
+ function getNonceFromApi(network, accountId) {
2758
+ return fetch(`${config[network].base_url}/nonce?csna=${accountId}`, {
2759
2759
  method: "GET",
2760
2760
  headers: {
2761
2761
  "Content-Type": "application/json"
2762
2762
  }
2763
2763
  }).then((res) => res.json());
2764
2764
  }
2765
- function getNearNonceFromApi(accountId) {
2766
- return fetch(`${base_url}/nonceNear?csna=${accountId}`, {
2765
+ function getNearNonceFromApi(network, accountId) {
2766
+ return fetch(`${config[network].base_url}/nonceNear?csna=${accountId}`, {
2767
2767
  method: "GET",
2768
2768
  headers: {
2769
2769
  "Content-Type": "application/json"
2770
2770
  }
2771
2771
  }).then((res) => res.json());
2772
2772
  }
2773
- function uploadBTCTx(data) {
2774
- return fetch(`${base_url}/receiveTransaction`, {
2773
+ function uploadBTCTx(network, data) {
2774
+ return fetch(`${config[network].base_url}/receiveTransaction`, {
2775
2775
  method: "POST",
2776
2776
  headers: {
2777
2777
  "Content-Type": "application/json"
@@ -2812,13 +2812,13 @@ function toHex(originalString) {
2812
2812
  }
2813
2813
  function initWalletButton(network, accountId, wallet) {
2814
2814
  const checkAndSetupWalletButton = () => {
2815
- if (accountId && window.btcContext.account) {
2815
+ if (accountId) {
2816
2816
  setupWalletButton(network, wallet, window.btcContext);
2817
- } else {
2818
- removeWalletButton();
2819
- setTimeout(() => {
2817
+ !window.btcContext.account && setTimeout(() => {
2820
2818
  checkAndSetupWalletButton();
2821
2819
  }, 5e3);
2820
+ } else {
2821
+ removeWalletButton();
2822
2822
  }
2823
2823
  };
2824
2824
  checkAndSetupWalletButton();
@@ -2871,7 +2871,7 @@ function pollTransactionStatuses(network, hashes) {
2871
2871
 
2872
2872
  // src/index.ts
2873
2873
  var getVersion = () => {
2874
- return "0.0.6";
2874
+ return "0.0.8";
2875
2875
  };
2876
2876
  if (typeof window !== "undefined") {
2877
2877
  window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();