btc-wallet 0.4.6-beta → 0.4.7-beta

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/esm/index.js CHANGED
@@ -2627,35 +2627,107 @@ function createFloatingButtonWithIframe({
2627
2627
  }) {
2628
2628
  const button = document.createElement("img");
2629
2629
  button.id = "satoshi-wallet-button";
2630
- button.src = openImageUrl;
2630
+ const isIframeVisible = localStorage.getItem("btc-wallet-iframe-visible") === "true";
2631
+ button.src = isIframeVisible ? closeImageUrl : openImageUrl;
2632
+ iframe.style.display = isIframeVisible ? "block" : "none";
2633
+ const windowWidth = window.innerWidth;
2634
+ const windowHeight = window.innerHeight;
2635
+ const savedPosition = JSON.parse(
2636
+ localStorage.getItem("btc-wallet-button-position") || '{"right": "20px", "bottom": "20px"}'
2637
+ );
2638
+ const right = Math.min(Math.max(20, parseInt(savedPosition.right)), windowWidth - 80);
2639
+ const bottom = Math.min(Math.max(20, parseInt(savedPosition.bottom)), windowHeight - 80);
2631
2640
  Object.assign(button.style, {
2632
2641
  position: "fixed",
2633
- bottom: "20px",
2634
- right: "20px",
2642
+ bottom: `${bottom}px`,
2643
+ right: `${right}px`,
2635
2644
  zIndex: "100000",
2636
2645
  width: "60px",
2637
2646
  height: "60px",
2638
2647
  borderRadius: "50%",
2639
- cursor: "pointer",
2640
- transition: "transform 0.15s ease"
2648
+ cursor: "grab",
2649
+ transition: "transform 0.15s ease",
2650
+ userSelect: "none"
2641
2651
  });
2642
2652
  document.body.appendChild(button);
2643
- const iframeVisible = localStorage.getItem("iframeVisible") === "true" || localStorage.getItem("iframeVisible") === null;
2644
- button.src = iframeVisible ? closeImageUrl : openImageUrl;
2645
- iframe.style.display = iframeVisible ? "block" : "none";
2646
- button.onclick = function() {
2653
+ let isDragging = false;
2654
+ let startX = 0;
2655
+ let startY = 0;
2656
+ let initialRight = 0;
2657
+ let initialBottom = 0;
2658
+ let dragStartTime = 0;
2659
+ button.addEventListener("mousedown", (e) => {
2660
+ isDragging = true;
2661
+ startX = e.clientX;
2662
+ startY = e.clientY;
2663
+ initialRight = parseInt(button.style.right);
2664
+ initialBottom = parseInt(button.style.bottom);
2665
+ dragStartTime = Date.now();
2666
+ button.style.cursor = "grabbing";
2667
+ button.style.transition = "none";
2668
+ e.preventDefault();
2669
+ });
2670
+ document.addEventListener("mousemove", (e) => {
2671
+ if (!isDragging)
2672
+ return;
2673
+ const deltaX = startX - e.clientX;
2674
+ const deltaY = startY - e.clientY;
2675
+ let newRight = initialRight + deltaX;
2676
+ let newBottom = initialBottom + deltaY;
2677
+ newRight = Math.min(Math.max(20, newRight), windowWidth - 80);
2678
+ newBottom = Math.min(Math.max(20, newBottom), windowHeight - 80);
2679
+ const snapThreshold = 20;
2680
+ const buttonLeft = windowWidth - newRight - 60;
2681
+ if (buttonLeft < snapThreshold) {
2682
+ newRight = windowWidth - 80;
2683
+ } else if (buttonLeft > windowWidth - snapThreshold - 60) {
2684
+ newRight = 20;
2685
+ }
2686
+ if (newBottom < snapThreshold) {
2687
+ newBottom = 20;
2688
+ } else if (newBottom > windowHeight - snapThreshold - 60) {
2689
+ newBottom = windowHeight - 80;
2690
+ }
2691
+ button.style.right = `${newRight}px`;
2692
+ button.style.bottom = `${newBottom}px`;
2693
+ updateIframePosition(iframe, newRight, newBottom, windowWidth, windowHeight);
2694
+ });
2695
+ document.addEventListener("mouseup", () => {
2696
+ if (!isDragging)
2697
+ return;
2698
+ const dragEndTime = Date.now();
2699
+ const isDragEvent = dragEndTime - dragStartTime > 200;
2700
+ isDragging = false;
2701
+ button.style.cursor = "grab";
2702
+ button.style.transition = "transform 0.15s ease";
2703
+ localStorage.setItem(
2704
+ "btc-wallet-button-position",
2705
+ JSON.stringify({
2706
+ right: button.style.right,
2707
+ bottom: button.style.bottom
2708
+ })
2709
+ );
2710
+ if (!isDragEvent) {
2711
+ handleButtonClick();
2712
+ }
2713
+ });
2714
+ const handleButtonClick = () => {
2647
2715
  const isCurrentlyVisible = iframe.style.display === "block";
2648
2716
  button.style.transform = "scale(0.8)";
2649
2717
  setTimeout(() => {
2650
2718
  button.style.transform = "scale(1)";
2651
2719
  }, 150);
2652
- iframe.style.display = isCurrentlyVisible ? "none" : "block";
2653
- button.src = isCurrentlyVisible ? openImageUrl : closeImageUrl;
2654
- localStorage.setItem("iframeVisible", String(!isCurrentlyVisible));
2720
+ const newVisibleState = !isCurrentlyVisible;
2721
+ iframe.style.display = newVisibleState ? "block" : "none";
2722
+ button.src = newVisibleState ? closeImageUrl : openImageUrl;
2723
+ localStorage.setItem("btc-wallet-iframe-visible", String(newVisibleState));
2655
2724
  setTimeout(() => {
2656
- iframe.focus();
2725
+ if (newVisibleState) {
2726
+ iframe.focus();
2727
+ }
2657
2728
  }, 0);
2658
2729
  };
2730
+ button.onclick = null;
2659
2731
  return button;
2660
2732
  }
2661
2733
  function createIframe({
@@ -2666,6 +2738,7 @@ function createIframe({
2666
2738
  iframe.id = "satoshi-wallet-iframe";
2667
2739
  iframe.allow = "clipboard-read; clipboard-write";
2668
2740
  iframe.src = iframeUrl;
2741
+ const isVisible = localStorage.getItem("btc-wallet-iframe-visible") === "true";
2669
2742
  Object.assign(iframe.style, __spreadValues({
2670
2743
  position: "fixed",
2671
2744
  bottom: "90px",
@@ -2673,7 +2746,7 @@ function createIframe({
2673
2746
  zIndex: "100000",
2674
2747
  boxShadow: "0 0 10px rgba(0, 0, 0, 0.1)",
2675
2748
  borderRadius: "10px",
2676
- display: "block",
2749
+ display: isVisible ? "block" : "none",
2677
2750
  border: "none"
2678
2751
  }, iframeStyle));
2679
2752
  document.body.appendChild(iframe);
@@ -2731,6 +2804,20 @@ function removeWalletButton() {
2731
2804
  const iframe = document.getElementById("satoshi-wallet-iframe");
2732
2805
  iframe == null ? void 0 : iframe.remove();
2733
2806
  }
2807
+ function updateIframePosition(iframe, buttonRight, buttonBottom, windowWidth, windowHeight) {
2808
+ const iframeWidth = parseInt(iframe.style.width);
2809
+ const iframeHeight = parseInt(iframe.style.height);
2810
+ let iframeRight = buttonRight;
2811
+ let iframeBottom = buttonBottom + 70;
2812
+ if (iframeRight + iframeWidth > windowWidth - 20) {
2813
+ iframeRight = Math.max(20, windowWidth - iframeWidth - 20);
2814
+ }
2815
+ if (iframeBottom + iframeHeight > windowHeight - 20) {
2816
+ iframeBottom = Math.max(20, buttonBottom - iframeHeight - 10);
2817
+ }
2818
+ iframe.style.right = `${iframeRight}px`;
2819
+ iframe.style.bottom = `${iframeBottom}px`;
2820
+ }
2734
2821
 
2735
2822
  // src/utils/nearUtils.ts
2736
2823
  import { providers } from "near-api-js";
@@ -3199,6 +3286,7 @@ import coinselect from "coinselect";
3199
3286
  var NEAR_STORAGE_DEPOSIT_AMOUNT = "1250000000000000000000";
3200
3287
  var NBTC_STORAGE_DEPOSIT_AMOUNT = "3000";
3201
3288
  var GAS_LIMIT = "50000000000000";
3289
+ var NEW_ACCOUNT_MIN_DEPOSIT_AMOUNT = "1000";
3202
3290
  function getBtcProvider() {
3203
3291
  if (typeof window === "undefined" || !window.btcContext) {
3204
3292
  throw new Error("BTC Provider is not initialized.");
@@ -3368,12 +3456,14 @@ function getDepositAmount(amount, option) {
3368
3456
  } = yield nearCall(config.bridgeContractId, "get_config", {});
3369
3457
  const depositAmount = Math.max(Number(min_deposit_amount), Number(amount));
3370
3458
  const protocolFee = Math.max(Number(fee_min), Number(depositAmount) * fee_rate);
3371
- const totalDepositAmount = new Big(depositAmount).plus(protocolFee).plus(repayAmount).round(0, Big.roundDown).toNumber();
3459
+ const newAccountMinDepositAmount = !(accountInfo == null ? void 0 : accountInfo.nonce) ? NEW_ACCOUNT_MIN_DEPOSIT_AMOUNT : 0;
3460
+ const totalDepositAmount = new Big(depositAmount).plus(protocolFee).plus(repayAmount).plus(newAccountMinDepositAmount).round(0, Big.roundDown).toNumber();
3372
3461
  return {
3373
3462
  depositAmount,
3374
3463
  totalDepositAmount,
3375
3464
  protocolFee,
3376
- repayAmount
3465
+ repayAmount,
3466
+ newAccountMinDepositAmount
3377
3467
  };
3378
3468
  });
3379
3469
  }
@@ -3500,6 +3590,14 @@ function checkSatoshiWhitelist(btcAccountId, env = "mainnet") {
3500
3590
  return __async(this, null, function* () {
3501
3591
  if (env !== "private_mainnet")
3502
3592
  return;
3593
+ const hasShownNotice = localStorage.getItem("btc-wallet-private-mainnet-notice");
3594
+ if (!hasShownNotice) {
3595
+ Dialog.alert({
3596
+ title: "Notice",
3597
+ message: "You are currently using Satoshi Private Mainnet. This is a private version for testing. Please try a small amount of assets in Ramp"
3598
+ });
3599
+ localStorage.setItem("btc-wallet-private-mainnet-notice", "true");
3600
+ }
3503
3601
  if (!btcAccountId)
3504
3602
  return;
3505
3603
  const config = yield getConfig(env);
@@ -3664,24 +3762,41 @@ function uint8ArrayToHex(uint8Array) {
3664
3762
 
3665
3763
  // src/core/setupBTCWallet.ts
3666
3764
  var { transfer, functionCall } = actionCreators;
3765
+ var STORAGE_KEYS = {
3766
+ ACCOUNT: "btc-wallet-account",
3767
+ PUBLIC_KEY: "btc-wallet-publickey",
3768
+ BTC_PUBLIC_KEY: "btc-wallet-btc-publickey"
3769
+ };
3667
3770
  var state = {
3668
3771
  saveAccount(account) {
3669
- window.localStorage.setItem("btc-wallet-account", account);
3772
+ if (!account) {
3773
+ this.removeAccount();
3774
+ return;
3775
+ }
3776
+ window.localStorage.setItem(STORAGE_KEYS.ACCOUNT, account);
3670
3777
  },
3671
3778
  removeAccount() {
3672
- window.localStorage.removeItem("btc-wallet-account");
3779
+ window.localStorage.removeItem(STORAGE_KEYS.ACCOUNT);
3673
3780
  },
3674
3781
  savePublicKey(publicKey) {
3675
- window.localStorage.setItem("btc-wallet-publickey", publicKey);
3782
+ if (!publicKey) {
3783
+ this.removePublicKey();
3784
+ return;
3785
+ }
3786
+ window.localStorage.setItem(STORAGE_KEYS.PUBLIC_KEY, publicKey);
3676
3787
  },
3677
3788
  removePublicKey() {
3678
- window.localStorage.removeItem("btc-wallet-publickey");
3789
+ window.localStorage.removeItem(STORAGE_KEYS.PUBLIC_KEY);
3679
3790
  },
3680
3791
  saveBtcPublicKey(publicKey) {
3681
- window.localStorage.setItem("btc-wallet-btc-publickey", publicKey);
3792
+ if (!publicKey) {
3793
+ this.removeBtcPublicKey();
3794
+ return;
3795
+ }
3796
+ window.localStorage.setItem(STORAGE_KEYS.BTC_PUBLIC_KEY, publicKey);
3682
3797
  },
3683
3798
  removeBtcPublicKey() {
3684
- window.localStorage.removeItem("btc-wallet-btc-publickey");
3799
+ window.localStorage.removeItem(STORAGE_KEYS.BTC_PUBLIC_KEY);
3685
3800
  },
3686
3801
  clear() {
3687
3802
  this.removeAccount();
@@ -3689,17 +3804,39 @@ var state = {
3689
3804
  this.removeBtcPublicKey();
3690
3805
  },
3691
3806
  save(account, publicKey) {
3807
+ if (!account || !publicKey) {
3808
+ this.clear();
3809
+ return;
3810
+ }
3692
3811
  this.saveAccount(account);
3693
3812
  this.savePublicKey(publicKey);
3694
3813
  },
3695
3814
  getAccount() {
3696
- return window.localStorage.getItem("btc-wallet-account");
3815
+ return window.localStorage.getItem(STORAGE_KEYS.ACCOUNT);
3697
3816
  },
3698
3817
  getPublicKey() {
3699
- return window.localStorage.getItem("btc-wallet-publickey");
3818
+ return window.localStorage.getItem(STORAGE_KEYS.PUBLIC_KEY);
3700
3819
  },
3701
3820
  getBtcPublicKey() {
3702
- return window.localStorage.getItem("btc-wallet-btc-publickey");
3821
+ return window.localStorage.getItem(STORAGE_KEYS.BTC_PUBLIC_KEY);
3822
+ },
3823
+ isValid() {
3824
+ const account = this.getAccount();
3825
+ const publicKey = this.getPublicKey();
3826
+ const btcPublicKey = this.getBtcPublicKey();
3827
+ const allEmpty = !account && !publicKey && !btcPublicKey;
3828
+ const allExist = account && publicKey && btcPublicKey;
3829
+ return allEmpty || allExist;
3830
+ },
3831
+ syncSave(account, publicKey, btcPublicKey) {
3832
+ if (!account || !publicKey || !btcPublicKey) {
3833
+ this.clear();
3834
+ return;
3835
+ }
3836
+ this.clear();
3837
+ this.savePublicKey(publicKey);
3838
+ this.saveBtcPublicKey(btcPublicKey);
3839
+ this.saveAccount(account);
3703
3840
  }
3704
3841
  };
3705
3842
  var BTCWallet = (_0) => __async(void 0, [_0], function* ({
@@ -3726,16 +3863,33 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3726
3863
  const currentConfig = walletConfig[env];
3727
3864
  const walletNetwork = ["mainnet", "private_mainnet"].includes(env) ? "mainnet" : "testnet";
3728
3865
  yield initBtcContext();
3866
+ function validateWalletState() {
3867
+ const accountId = state.getAccount();
3868
+ const publicKey = state.getPublicKey();
3869
+ const btcPublicKey = state.getBtcPublicKey();
3870
+ if (!accountId && publicKey || accountId && !publicKey || !publicKey && btcPublicKey) {
3871
+ state.clear();
3872
+ return false;
3873
+ }
3874
+ return true;
3875
+ }
3729
3876
  function setupBtcContextListeners() {
3730
3877
  return __async(this, null, function* () {
3731
3878
  const handleConnectionUpdate = () => __async(this, null, function* () {
3732
3879
  yield checkBtcNetwork(walletNetwork);
3733
- const accountId = state.getAccount();
3880
+ if (!state.isValid()) {
3881
+ state.clear();
3882
+ }
3883
+ validateWalletState();
3734
3884
  const btcContext = window.btcContext;
3735
- if (accountId && btcContext.account) {
3736
- yield checkSatoshiWhitelist(btcContext.account, env);
3737
- removeWalletButton();
3738
- setupWalletButton(env, wallet, btcContext);
3885
+ if (btcContext.account) {
3886
+ const btcPublicKey = yield btcContext.getPublicKey();
3887
+ if (btcPublicKey) {
3888
+ const { nearAddress, nearPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3889
+ yield checkSatoshiWhitelist(btcContext.account, env);
3890
+ removeWalletButton();
3891
+ setupWalletButton(env, wallet, btcContext);
3892
+ }
3739
3893
  } else {
3740
3894
  removeWalletButton();
3741
3895
  setTimeout(() => {
@@ -3746,19 +3900,31 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3746
3900
  const context = window.btcContext.getContext();
3747
3901
  context.on("updatePublicKey", (btcPublicKey) => __async(this, null, function* () {
3748
3902
  console.log("updatePublicKey");
3749
- const { nearAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3750
- emitter.emit("accountsChanged", {
3751
- accounts: [{ accountId: nearAddress }]
3752
- });
3753
- yield handleConnectionUpdate();
3903
+ state.clear();
3904
+ try {
3905
+ const { nearAddress, nearPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3906
+ if (!nearAddress || !nearPublicKey) {
3907
+ throw new Error("Failed to get near account info");
3908
+ }
3909
+ emitter.emit("accountsChanged", {
3910
+ accounts: [{ accountId: nearAddress }]
3911
+ });
3912
+ yield handleConnectionUpdate();
3913
+ } catch (error) {
3914
+ console.error("Error updating public key:", error);
3915
+ state.clear();
3916
+ emitter.emit("accountsChanged", { accounts: [] });
3917
+ }
3754
3918
  }));
3755
3919
  context.on("btcLoginError", () => __async(this, null, function* () {
3756
3920
  console.log("btcLoginError");
3921
+ state.clear();
3757
3922
  emitter.emit("accountsChanged", { accounts: [] });
3758
3923
  yield handleConnectionUpdate();
3759
3924
  }));
3760
3925
  context.on("btcLogOut", () => __async(this, null, function* () {
3761
3926
  console.log("btcLogOut");
3927
+ state.clear();
3762
3928
  emitter.emit("accountsChanged", { accounts: [] });
3763
3929
  yield handleConnectionUpdate();
3764
3930
  }));
@@ -3802,9 +3968,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3802
3968
  "get_chain_signature_near_account_public_key",
3803
3969
  { btc_public_key: btcPublicKey }
3804
3970
  );
3805
- state.saveAccount(csna);
3806
- state.savePublicKey(nearPublicKey);
3807
- state.saveBtcPublicKey(btcPublicKey);
3971
+ state.syncSave(csna, nearPublicKey, btcPublicKey);
3808
3972
  return {
3809
3973
  nearAddress: csna,
3810
3974
  nearPublicKey
@@ -3814,10 +3978,8 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3814
3978
  function signIn(_02) {
3815
3979
  return __async(this, arguments, function* ({ contractId, methodNames }) {
3816
3980
  const btcContext = window.btcContext;
3817
- const accountId = state.getAccount();
3818
- const publicKey = state.getPublicKey();
3819
- console.log("isLogin:", accountId && publicKey);
3820
- if (!accountId || !publicKey) {
3981
+ state.clear();
3982
+ if (!state.getAccount() || !state.getPublicKey()) {
3821
3983
  yield btcContext.login();
3822
3984
  }
3823
3985
  const btcPublicKey = yield btcContext.getPublicKey();
@@ -3883,6 +4045,9 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3883
4045
  }
3884
4046
  function signAndSendTransactions(params) {
3885
4047
  return __async(this, null, function* () {
4048
+ if (!validateWalletState()) {
4049
+ throw new Error("Wallet state is invalid, please reconnect your wallet.");
4050
+ }
3886
4051
  const btcContext = window.btcContext;
3887
4052
  const accountId = state.getAccount();
3888
4053
  const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
@@ -4089,18 +4254,6 @@ function setupBTCWallet({
4089
4254
  env = "mainnet"
4090
4255
  } = {}) {
4091
4256
  console.log("\u26A1\uFE0F BTC Wallet Version:", getVersion(), "env:", env);
4092
- if (env === "private_mainnet" && typeof window !== "undefined") {
4093
- setTimeout(() => {
4094
- const hasShownNotice = localStorage.getItem("satoshi_private_mainnet_notice");
4095
- if (!hasShownNotice) {
4096
- Dialog.alert({
4097
- title: "Notice",
4098
- message: "You are currently using Satoshi Private Mainnet. This is a private version for testing. Please try a small amount of assets in Ramp"
4099
- });
4100
- localStorage.setItem("satoshi_private_mainnet_notice", "true");
4101
- }
4102
- }, 1e3);
4103
- }
4104
4257
  const btcWallet = () => __async(this, null, function* () {
4105
4258
  return {
4106
4259
  id: "btc-wallet",
@@ -4124,7 +4277,7 @@ function setupBTCWallet({
4124
4277
 
4125
4278
  // src/index.ts
4126
4279
  var getVersion = () => {
4127
- return "0.4.6-beta";
4280
+ return "0.4.7-beta";
4128
4281
  };
4129
4282
  if (typeof window !== "undefined") {
4130
4283
  window.__BTC_WALLET_VERSION = getVersion();