btc-wallet 0.0.5 → 0.0.7

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.
@@ -1,5 +1,5 @@
1
- import type { InjectedWallet, WalletModuleFactory } from "@near-wallet-selector/core";
2
- import type { useBtcWalletSelector } from "./../components/btcWalletSelectorContext";
1
+ import type { InjectedWallet, WalletModuleFactory } from '@near-wallet-selector/core';
2
+ import type { useBtcWalletSelector } from './../components/btcWalletSelectorContext';
3
3
  declare global {
4
4
  interface Window {
5
5
  btcContext: ReturnType<typeof useBtcWalletSelector>;
package/esm/index.js CHANGED
@@ -1812,12 +1812,12 @@ var ConnectProvider = ({
1812
1812
  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"
1813
1813
  });
1814
1814
  const setAccountContract = useCallback7(
1815
- (config) => {
1816
- if (!checkBTCVersion(options.aaOptions.accountContracts, config.name, config.version)) {
1815
+ (config2) => {
1816
+ if (!checkBTCVersion(options.aaOptions.accountContracts, config2.name, config2.version)) {
1817
1817
  throw new Error("Invalid Account Contract");
1818
1818
  }
1819
- localStorage.setItem(SAContractKey, JSON.stringify(config));
1820
- _setAccountContract(config);
1819
+ localStorage.setItem(SAContractKey, JSON.stringify(config2));
1820
+ _setAccountContract(config2);
1821
1821
  },
1822
1822
  [options.aaOptions.accountContracts, _setAccountContract]
1823
1823
  );
@@ -1976,9 +1976,9 @@ var ConnectProvider = ({
1976
1976
  setConnectorId(void 0);
1977
1977
  }, [connector]);
1978
1978
  useEffect5(() => {
1979
- const config = JSON.parse(localStorage.getItem(SAContractKey) || "{}");
1980
- if (config.name && config.version && checkBTCVersion(options.aaOptions.accountContracts, config.name, config.version)) {
1981
- _setAccountContract(config);
1979
+ const config2 = JSON.parse(localStorage.getItem(SAContractKey) || "{}");
1980
+ if (config2.name && config2.version && checkBTCVersion(options.aaOptions.accountContracts, config2.name, config2.version)) {
1981
+ _setAccountContract(config2);
1982
1982
  }
1983
1983
  }, [options.aaOptions.accountContracts, _setAccountContract]);
1984
1984
  useEffect5(() => {
@@ -2307,13 +2307,9 @@ function sleep(time) {
2307
2307
 
2308
2308
  // src/utils/setupBTCWallet.ts
2309
2309
  import { providers, transactions } from "near-api-js";
2310
- import {
2311
- actionCreators
2312
- } from "@near-js/transactions";
2310
+ import { actionCreators } from "@near-js/transactions";
2313
2311
  import { PublicKey } from "near-api-js/lib/utils/key_pair";
2314
- import {
2315
- encodeTransaction
2316
- } from "near-api-js/lib/transaction";
2312
+ import { encodeTransaction } from "near-api-js/lib/transaction";
2317
2313
  import { baseDecode } from "@near-js/utils";
2318
2314
  import bs58 from "bs58";
2319
2315
  import { sha256 } from "js-sha256";
@@ -2441,10 +2437,19 @@ function removeWalletButton() {
2441
2437
  }
2442
2438
 
2443
2439
  // src/utils/setupBTCWallet.ts
2444
- var { signedDelegate, transfer, functionCall } = actionCreators;
2445
- var base_url = "https://api.dev.satoshibridge.top/v1";
2446
- var token = "nbtc1-nsp.testnet";
2447
- var contractId = "dev1-nsp.testnet";
2440
+ var { transfer, functionCall } = actionCreators;
2441
+ var config = {
2442
+ testnet: {
2443
+ base_url: "https://api.testnet.satoshibridge.top/v1",
2444
+ token: "nbtc2-nsp.testnet",
2445
+ contractId: "dev2-nsp.testnet"
2446
+ },
2447
+ mainnet: {
2448
+ base_url: "https://api.mainnet.satoshibridge.top/v1",
2449
+ token: "",
2450
+ contractId: ""
2451
+ }
2452
+ };
2448
2453
  var state = {
2449
2454
  saveAccount(account) {
2450
2455
  window.localStorage.setItem("btc-wallet-account", account);
@@ -2514,11 +2519,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2514
2519
  const { nearTempAddress, nearTempPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
2515
2520
  removeWalletButton();
2516
2521
  setTimeout(() => {
2517
- initWalletButton(
2518
- options.network.networkId,
2519
- nearTempAddress,
2520
- wallet
2521
- );
2522
+ initWalletButton(options.network.networkId, nearTempAddress, wallet);
2522
2523
  }, 1e3);
2523
2524
  emitter.emit("accountsChanged", {
2524
2525
  accounts: [
@@ -2541,13 +2542,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2541
2542
  }), 500);
2542
2543
  }
2543
2544
  function viewMethod(_02) {
2544
- return __async(this, arguments, function* ({
2545
- method,
2546
- args = {}
2547
- }) {
2545
+ return __async(this, arguments, function* ({ method, args = {} }) {
2548
2546
  const res = yield provider.query({
2549
2547
  request_type: "call_function",
2550
- account_id: contractId,
2548
+ account_id: config[options.network.networkId].contractId,
2551
2549
  method_name: method,
2552
2550
  args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
2553
2551
  finality: "optimistic"
@@ -2575,7 +2573,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2575
2573
  });
2576
2574
  }
2577
2575
  function signIn(_02) {
2578
- return __async(this, arguments, function* ({ contractId: contractId2, methodNames }) {
2576
+ return __async(this, arguments, function* ({ contractId, methodNames }) {
2579
2577
  const accountId = state.getAccount();
2580
2578
  const publicKey = state.getPublicKey();
2581
2579
  const btcContext = window.btcContext;
@@ -2649,11 +2647,11 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2649
2647
  nonce: BigInt(rawAccessKey.nonce || 0)
2650
2648
  });
2651
2649
  const publicKeyFormat = PublicKey.from(publicKey);
2652
- const nearNonceApi = yield getNearNonceFromApi(accountId);
2650
+ const nearNonceApi = yield getNearNonceFromApi(options.network.networkId, accountId);
2653
2651
  const newTransactions = params.transactions.map((transaction, index) => {
2654
2652
  let nearNonceNumber = accessKey.nonce + BigInt(1);
2655
2653
  if (nearNonceApi) {
2656
- nearNonceNumber = Number(nearNonceApi.result_data) > nearNonceNumber ? BigInt(Number(nearNonceApi.result_data)) : nearNonceNumber;
2654
+ nearNonceNumber = BigInt(nearNonceApi.result_data) > nearNonceNumber ? BigInt(nearNonceApi.result_data) : nearNonceNumber;
2657
2655
  }
2658
2656
  const newActions = transaction.actions.map((action) => {
2659
2657
  switch (action.type) {
@@ -2677,7 +2675,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2677
2675
  baseDecode(header.hash)
2678
2676
  );
2679
2677
  const txBytes = encodeTransaction(_transaction);
2680
- const txHex = toHex(txBytes.toString());
2678
+ const txHex = Array.from(
2679
+ txBytes,
2680
+ (byte) => ("0" + (byte & 255).toString(16)).slice(-2)
2681
+ ).join("");
2681
2682
  const hash = bs58.encode(new Uint8Array(sha256.array(txBytes)));
2682
2683
  return { txBytes, txHex, hash };
2683
2684
  });
@@ -2685,29 +2686,26 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2685
2686
  method: "get_account",
2686
2687
  args: { account_id: accountId }
2687
2688
  });
2688
- const nonceApi = yield getNonceFromApi(accountId);
2689
- const nonce = (nonceApi == null ? void 0 : nonceApi.result_data) ? Number(nonceApi == null ? void 0 : nonceApi.result_data) : accountInfo.nonce;
2689
+ const nonceApi = yield getNonceFromApi(options.network.networkId, accountId);
2690
+ const nonce = Number(nonceApi == null ? void 0 : nonceApi.result_data) > Number(accountInfo.nonce) ? String(nonceApi == null ? void 0 : nonceApi.result_data) : String(accountInfo.nonce);
2690
2691
  const intention = {
2691
2692
  chain_id: "397",
2692
2693
  csna: accountId,
2693
- near_transactions: newTransactions.map((t) => Array.from(t.txHex)),
2694
- gas_token: token,
2694
+ near_transactions: newTransactions.map((t) => t.txHex),
2695
+ gas_token: config[options.network.networkId].token,
2695
2696
  gas_limit: "3000",
2696
- nonce: Number(nonce).toString()
2697
+ nonce
2697
2698
  };
2698
2699
  const strIntention = JSON.stringify(intention);
2699
2700
  const signature = yield btcContext.signMessage(strIntention);
2700
- const result = yield uploadBTCTx({
2701
+ const result = yield uploadBTCTx(options.network.networkId, {
2701
2702
  sig: signature,
2702
2703
  btcPubKey: state.getBtcPublicKey(),
2703
2704
  data: toHex(strIntention)
2704
2705
  });
2705
2706
  if (result.result_code === 0) {
2706
2707
  const hash = newTransactions.map((t) => t.hash);
2707
- const result2 = yield pollTransactionStatuses(
2708
- options.network.networkId,
2709
- hash
2710
- );
2708
+ const result2 = yield pollTransactionStatuses(options.network.networkId, hash);
2711
2709
  return result2;
2712
2710
  } else {
2713
2711
  return null;
@@ -2716,24 +2714,24 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2716
2714
  }
2717
2715
  return wallet;
2718
2716
  });
2719
- function getNonceFromApi(accountId) {
2720
- return fetch(`${base_url}/nonce?csna=${accountId}`, {
2717
+ function getNonceFromApi(network, accountId) {
2718
+ return fetch(`${config[network].base_url}/nonce?csna=${accountId}`, {
2721
2719
  method: "GET",
2722
2720
  headers: {
2723
2721
  "Content-Type": "application/json"
2724
2722
  }
2725
2723
  }).then((res) => res.json());
2726
2724
  }
2727
- function getNearNonceFromApi(accountId) {
2728
- return fetch(`${base_url}/nonceNear?csna=${accountId}`, {
2725
+ function getNearNonceFromApi(network, accountId) {
2726
+ return fetch(`${config[network].base_url}/nonceNear?csna=${accountId}`, {
2729
2727
  method: "GET",
2730
2728
  headers: {
2731
2729
  "Content-Type": "application/json"
2732
2730
  }
2733
2731
  }).then((res) => res.json());
2734
2732
  }
2735
- function uploadBTCTx(data) {
2736
- return fetch(`${base_url}/receiveTransaction`, {
2733
+ function uploadBTCTx(network, data) {
2734
+ return fetch(`${config[network].base_url}/receiveTransaction`, {
2737
2735
  method: "POST",
2738
2736
  headers: {
2739
2737
  "Content-Type": "application/json"
@@ -2833,7 +2831,7 @@ function pollTransactionStatuses(network, hashes) {
2833
2831
 
2834
2832
  // src/index.ts
2835
2833
  var getVersion = () => {
2836
- return "0.0.5";
2834
+ return "0.0.7";
2837
2835
  };
2838
2836
  if (typeof window !== "undefined") {
2839
2837
  window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();