btc-wallet 0.3.32 → 0.3.33

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ interface DialogOptions {
2
+ title: string;
3
+ message: string;
4
+ dangerouslyUseHTML?: boolean;
5
+ closable?: boolean;
6
+ }
1
7
  export declare class Dialog {
2
8
  private static template;
3
9
  private static style;
@@ -6,8 +12,6 @@ export declare class Dialog {
6
12
  title?: string;
7
13
  message: string;
8
14
  }): Promise<boolean>;
9
- static alert(options: {
10
- title?: string;
11
- message: string;
12
- }): Promise<void>;
15
+ static alert(options: DialogOptions): Promise<void>;
13
16
  }
17
+ export {};
@@ -1,13 +1,15 @@
1
1
  export declare function getNonce(url: string, accountId: string): Promise<string>;
2
2
  export declare function getNearNonce(url: string, accountId: string): Promise<string>;
3
3
  export declare function receiveTransaction(url: string, data: any): Promise<any>;
4
- export declare function receiveDepositMsg(url: string, { btcPublicKey, txHash, depositType, postActions, extraMsg, }: {
4
+ interface ReceiveDepositMsgParams {
5
5
  btcPublicKey: string;
6
6
  txHash: string;
7
7
  depositType?: number;
8
8
  postActions?: string;
9
9
  extraMsg?: string;
10
- }): Promise<any>;
10
+ }
11
+ export declare function preReceiveDepositMsg(url: string, { btcPublicKey, depositType, postActions, extraMsg }: Omit<ReceiveDepositMsgParams, 'txHash'>): Promise<any>;
12
+ export declare function receiveDepositMsg(url: string, { btcPublicKey, txHash, depositType, postActions, extraMsg }: ReceiveDepositMsgParams): Promise<any>;
11
13
  export declare function checkBridgeTransactionStatus(url: string, txHash: string): Promise<{
12
14
  Status: number;
13
15
  ToTxHash: string;
@@ -16,3 +18,5 @@ export declare function checkBtcTransactionStatus(url: string, sig: string): Pro
16
18
  Status: number;
17
19
  NearHashList: string[];
18
20
  }>;
21
+ export declare function getWhitelist(url: string): Promise<string[]>;
22
+ export {};
package/esm/index.js CHANGED
@@ -2382,10 +2382,8 @@ function BtcWalletSelectorContextProvider({
2382
2382
  const [isProcessing, setIsProcessing] = useState8(false);
2383
2383
  const connectors = [
2384
2384
  new UnisatConnector(),
2385
- new XverseConnector(),
2386
2385
  new OKXConnector(),
2387
- new BitgetConnector(),
2388
- new MagicEdenConnector()
2386
+ new BitgetConnector()
2389
2387
  ];
2390
2388
  const walletSelectorContextValue = useMemo6(() => {
2391
2389
  const simpleFn = {};
@@ -2598,7 +2596,7 @@ var walletConfig = {
2598
2596
  accountContractId: "acc.toalice.near",
2599
2597
  bridgeContractId: "brg.toalice.near",
2600
2598
  walletUrl: "https://wallet-stg.satoshibridge.top",
2601
- bridgeUrl: "https://stg.satoshibridge.top/"
2599
+ bridgeUrl: "https://ramp.satos.network/"
2602
2600
  },
2603
2601
  mainnet: {
2604
2602
  base_url: "https://api.mainnet.satoshibridge.top",
@@ -2945,14 +2943,24 @@ function receiveTransaction(url, data) {
2945
2943
  return result_data;
2946
2944
  });
2947
2945
  }
2946
+ function preReceiveDepositMsg(_0, _1) {
2947
+ return __async(this, arguments, function* (url, { btcPublicKey, depositType = 1, postActions, extraMsg }) {
2948
+ const { result_code, result_message, result_data } = yield request(
2949
+ `${url}/v1/preReceiveDepositMsg`,
2950
+ {
2951
+ method: "POST",
2952
+ body: { btcPublicKey, depositType, postActions, extraMsg }
2953
+ }
2954
+ );
2955
+ console.log("preReceiveDepositMsg resp:", { result_code, result_message, result_data });
2956
+ if (result_code !== 0) {
2957
+ throw new Error(result_message);
2958
+ }
2959
+ return result_data;
2960
+ });
2961
+ }
2948
2962
  function receiveDepositMsg(_0, _1) {
2949
- return __async(this, arguments, function* (url, {
2950
- btcPublicKey,
2951
- txHash,
2952
- depositType = 1,
2953
- postActions,
2954
- extraMsg
2955
- }) {
2963
+ return __async(this, arguments, function* (url, { btcPublicKey, txHash, depositType = 1, postActions, extraMsg }) {
2956
2964
  const { result_code, result_message, result_data } = yield request(
2957
2965
  `${url}/v1/receiveDepositMsg`,
2958
2966
  {
@@ -3003,6 +3011,15 @@ function checkBtcTransactionStatus(url, sig) {
3003
3011
  return result_data;
3004
3012
  });
3005
3013
  }
3014
+ function getWhitelist(url) {
3015
+ return __async(this, null, function* () {
3016
+ const data = yield request(`${url}/v1/whitelist/users`).catch((error) => {
3017
+ console.error("getWhitelist error:", error);
3018
+ return [];
3019
+ });
3020
+ return data;
3021
+ });
3022
+ }
3006
3023
 
3007
3024
  // src/utils/Dialog.ts
3008
3025
  var Dialog = class {
@@ -3044,13 +3061,22 @@ var Dialog = class {
3044
3061
  });
3045
3062
  }
3046
3063
  static alert(options) {
3064
+ const messageEl = options.dangerouslyUseHTML ? { dangerouslySetInnerHTML: { __html: options.message } } : { children: options.message };
3047
3065
  return new Promise((resolve) => {
3066
+ var _a;
3048
3067
  this.injectStyles();
3049
3068
  const container = document.createElement("div");
3050
3069
  container.innerHTML = this.template;
3070
+ (_a = container.querySelector(".dialog-overlay")) == null ? void 0 : _a.classList.add("dialog-alert");
3071
+ if (options.closable === false) {
3072
+ const overlay = container.querySelector(".dialog-overlay");
3073
+ overlay.style.pointerEvents = "none";
3074
+ const dialogContainer = container.querySelector(".dialog-container");
3075
+ dialogContainer.style.pointerEvents = "auto";
3076
+ }
3051
3077
  document.body.appendChild(container);
3052
3078
  const titleEl = container.querySelector(".dialog-title");
3053
- const messageEl = container.querySelector(".dialog-message");
3079
+ const messageEl2 = container.querySelector(".dialog-message");
3054
3080
  const confirmBtn = container.querySelector(".dialog-confirm-btn");
3055
3081
  const cancelBtn = container.querySelector(".dialog-cancel-btn");
3056
3082
  if (options.title) {
@@ -3058,9 +3084,15 @@ var Dialog = class {
3058
3084
  } else {
3059
3085
  titleEl.style.display = "none";
3060
3086
  }
3061
- messageEl.textContent = options.message;
3087
+ messageEl2.innerHTML = options.message;
3062
3088
  cancelBtn.style.display = "none";
3089
+ if (options.closable === false) {
3090
+ confirmBtn.style.display = "none";
3091
+ }
3063
3092
  const cleanup = () => {
3093
+ if (options.closable === false) {
3094
+ return;
3095
+ }
3064
3096
  document.body.removeChild(container);
3065
3097
  };
3066
3098
  confirmBtn.addEventListener("click", () => {
@@ -3078,7 +3110,7 @@ Dialog.template = `
3078
3110
  <div class="dialog-message"></div>
3079
3111
  <div class="dialog-buttons">
3080
3112
  <button class="dialog-cancel-btn">Cancel</button>
3081
- <button class="dialog-confirm-btn">Confirm</button>
3113
+ <button class="dialog-confirm-btn">OK</button>
3082
3114
  </div>
3083
3115
  </div>
3084
3116
  </div>
@@ -3122,6 +3154,9 @@ Dialog.style = `
3122
3154
  justify-content: flex-end;
3123
3155
  gap: 12px;
3124
3156
  }
3157
+ .dialog-alert .dialog-buttons {
3158
+ justify-content: center;
3159
+ }
3125
3160
  .dialog-confirm-btn {
3126
3161
  padding: 8px 24px;
3127
3162
  background-color: #ff7a00;
@@ -3246,23 +3281,26 @@ function checkGasTokenBalance(csna, gasToken, minAmount, env) {
3246
3281
  }
3247
3282
  function checkGasTokenArrears(accountInfo, env, autoDeposit) {
3248
3283
  return __async(this, null, function* () {
3249
- var _a, _b, _c, _d;
3250
- if (!((_a = accountInfo == null ? void 0 : accountInfo.debt_info) == null ? void 0 : _a.transfer_amount) || !((_b = accountInfo == null ? void 0 : accountInfo.relayer_fee) == null ? void 0 : _b.amount))
3284
+ var _a, _b, _c;
3285
+ const debtAmount = new Big(((_a = accountInfo == null ? void 0 : accountInfo.debt_info) == null ? void 0 : _a.near_gas_debt_amount) || 0).plus(((_b = accountInfo == null ? void 0 : accountInfo.debt_info) == null ? void 0 : _b.protocol_fee_debt_amount) || 0).toString();
3286
+ const relayerFeeAmount = ((_c = accountInfo == null ? void 0 : accountInfo.relayer_fee) == null ? void 0 : _c.amount) || "0";
3287
+ const hasDebtArrears = new Big(debtAmount).gt(0);
3288
+ const hasRelayerFeeArrears = new Big(relayerFeeAmount).gt(0);
3289
+ if (!hasDebtArrears && !hasRelayerFeeArrears)
3251
3290
  return;
3252
3291
  const config = yield getConfig(env);
3253
- const arrearsType = accountInfo.debt_info.transfer_amount ? "Deposit" : ((_c = accountInfo.relayer_fee) == null ? void 0 : _c.amount) ? "RelayerFee" : void 0;
3254
- const transferAmount = arrearsType === "Deposit" ? accountInfo.debt_info.transfer_amount : (_d = accountInfo.relayer_fee) == null ? void 0 : _d.amount;
3292
+ const transferAmount = hasDebtArrears ? debtAmount : relayerFeeAmount;
3255
3293
  console.log("get_account:", accountInfo);
3256
3294
  const action = {
3257
3295
  receiver_id: config.accountContractId,
3258
3296
  amount: transferAmount,
3259
- msg: JSON.stringify(arrearsType)
3297
+ msg: JSON.stringify(hasDebtArrears ? "Repay" : "RelayerFee")
3260
3298
  };
3261
3299
  if (!autoDeposit)
3262
3300
  return action;
3263
3301
  const confirmed = yield Dialog.confirm({
3264
- title: arrearsType === "Deposit" ? "Has gas token arrears" : "Has relayer fee arrears",
3265
- message: arrearsType === "Deposit" ? "You have gas token arrears, please deposit gas token to continue." : "You have relayer fee arrears, please deposit relayer fee to continue."
3302
+ title: hasDebtArrears ? "Has gas token arrears" : "Has relayer fee arrears",
3303
+ message: hasDebtArrears ? "You have gas token arrears, please deposit gas token to continue." : "You have relayer fee arrears, please deposit relayer fee to continue."
3266
3304
  });
3267
3305
  if (confirmed) {
3268
3306
  yield executeBTCDepositAndAction({ action, env });
@@ -3456,8 +3494,14 @@ function executeBTCDepositAndAction(_0) {
3456
3494
  console.log("user deposit address:", userDepositAddress);
3457
3495
  console.log("send amount:", sendAmount);
3458
3496
  console.log("fee rate:", _feeRate);
3459
- const txHash = yield sendBitcoin(userDepositAddress, Number(sendAmount), _feeRate);
3460
3497
  const postActionsStr = newActions.length > 0 ? JSON.stringify(newActions) : void 0;
3498
+ yield preReceiveDepositMsg(config.base_url, {
3499
+ btcPublicKey,
3500
+ depositType: postActionsStr || depositMsg.extra_msg ? 1 : 0,
3501
+ postActions: postActionsStr,
3502
+ extraMsg: depositMsg.extra_msg
3503
+ });
3504
+ const txHash = yield sendBitcoin(userDepositAddress, Number(sendAmount), _feeRate);
3461
3505
  yield receiveDepositMsg(config.base_url, {
3462
3506
  btcPublicKey,
3463
3507
  txHash,
@@ -3479,6 +3523,28 @@ function executeBTCDepositAndAction(_0) {
3479
3523
  }
3480
3524
  });
3481
3525
  }
3526
+ function checkSatoshiWhitelist(btcAccountId, env = "mainnet") {
3527
+ return __async(this, null, function* () {
3528
+ if (env !== "private_mainnet")
3529
+ return;
3530
+ if (!btcAccountId)
3531
+ return;
3532
+ const config = yield getConfig(env);
3533
+ const whitelist = yield getWhitelist(config.base_url);
3534
+ if (!(whitelist == null ? void 0 : whitelist.length))
3535
+ return;
3536
+ const isWhitelisted = whitelist.includes(btcAccountId);
3537
+ if (!isWhitelisted) {
3538
+ Dialog.alert({
3539
+ title: "Account is not whitelisted",
3540
+ message: `Sorry, you are not whitelisted. Please fill out the form to get whitelisted. <a style="color: #ff7a00; text-decoration: underline;" href="https://forms.gle/rrTP1ZbGU5mRZpHdA" target="_blank">https://forms.gle/rrTP1ZbGU5mRZpHdA</a>`,
3541
+ dangerouslyUseHTML: true,
3542
+ closable: false
3543
+ });
3544
+ throw new Error("Account is not whitelisted");
3545
+ }
3546
+ });
3547
+ }
3482
3548
 
3483
3549
  // src/core/setupBTCWallet.ts
3484
3550
  var { transfer, functionCall } = actionCreators;
@@ -3537,7 +3603,8 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3537
3603
  signMessage,
3538
3604
  isSignedIn,
3539
3605
  signAndSendTransaction,
3540
- signAndSendTransactions
3606
+ signAndSendTransactions,
3607
+ calculateGasLimit
3541
3608
  };
3542
3609
  const env = metadata.env || options.network.networkId || "mainnet";
3543
3610
  const currentConfig = walletConfig[env];
@@ -3550,6 +3617,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3550
3617
  const accountId = state.getAccount();
3551
3618
  const btcContext = window.btcContext;
3552
3619
  if (accountId && btcContext.account) {
3620
+ yield checkSatoshiWhitelist(btcContext.account, env);
3553
3621
  removeWalletButton();
3554
3622
  setupWalletButton(env, wallet, btcContext);
3555
3623
  } else {
@@ -3746,6 +3814,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3746
3814
  return result;
3747
3815
  });
3748
3816
  }
3817
+ function calculateGasLimit(params) {
3818
+ return __async(this, null, function* () {
3819
+ const accountId = state.getAccount();
3820
+ const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3821
+ const trans = [...params.transactions];
3822
+ console.log("raw trans:", trans);
3823
+ const gasTokenBalance = (accountInfo == null ? void 0 : accountInfo.gas_token[currentConfig.token]) || "0";
3824
+ const { gasLimit } = yield calculateGasStrategy(gasTokenBalance, trans);
3825
+ return gasLimit;
3826
+ });
3827
+ }
3749
3828
  function createGasTokenTransfer(accountId, amount) {
3750
3829
  return __async(this, null, function* () {
3751
3830
  return {
@@ -3799,58 +3878,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3799
3878
  }
3800
3879
  function calculateGasStrategy(gasTokenBalance, transactions2) {
3801
3880
  return __async(this, null, function* () {
3802
- var _a;
3803
3881
  const accountId = state.getAccount();
3804
- const nearAccount = yield provider.query({
3805
- request_type: "view_account",
3806
- account_id: accountId,
3807
- finality: "final"
3808
- });
3809
- const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
3810
- console.log("available near balance:", availableBalance);
3811
- console.log("available gas token balance:", gasTokenBalance);
3812
3882
  const convertTx = yield Promise.all(
3813
3883
  transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
3814
3884
  );
3815
- if (availableBalance > 0.2) {
3816
- console.log("near balance is enough, get the protocol fee of each transaction");
3817
- const gasTokens = yield nearCall2(
3818
- currentConfig.accountContractId,
3819
- "list_gas_token",
3820
- { token_ids: [currentConfig.token] }
3821
- );
3822
- console.log("list_gas_token gas tokens:", gasTokens);
3823
- const perTxFee = Math.max(
3824
- Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
3825
- 100
3826
- );
3827
- console.log("perTxFee:", perTxFee);
3828
- const protocolFee = new Big2(perTxFee || "0").mul(convertTx.length).toFixed(0);
3829
- console.log("protocolFee:", protocolFee);
3830
- if (new Big2(gasTokenBalance).gte(protocolFee)) {
3831
- console.log("use near pay gas and enough gas token balance");
3832
- return { useNearPayGas: true, gasLimit: protocolFee };
3833
- } else {
3834
- console.log("use near pay gas and not enough gas token balance");
3835
- const transferTx = yield createGasTokenTransfer(accountId, protocolFee);
3836
- return recalculateGasWithTransfer(transferTx, convertTx, true, perTxFee.toString());
3837
- }
3838
- } else {
3839
- console.log("near balance is not enough, predict the gas token amount required");
3840
- const adjustedGas = yield getPredictedGasAmount(
3841
- currentConfig.accountContractId,
3842
- currentConfig.token,
3843
- convertTx.map((t) => t.txHex)
3844
- );
3845
- if (new Big2(gasTokenBalance).gte(adjustedGas)) {
3846
- console.log("use gas token and gas token balance is enough");
3847
- return { useNearPayGas: false, gasLimit: adjustedGas };
3848
- } else {
3849
- console.log("use gas token and gas token balance is not enough, need to transfer");
3850
- const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
3851
- return recalculateGasWithTransfer(transferTx, convertTx, false);
3852
- }
3853
- }
3885
+ const adjustedGas = yield getPredictedGasAmount(
3886
+ currentConfig.accountContractId,
3887
+ currentConfig.token,
3888
+ convertTx.map((t) => t.txHex)
3889
+ );
3890
+ const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
3891
+ return recalculateGasWithTransfer(transferTx, convertTx, false);
3854
3892
  });
3855
3893
  }
3856
3894
  function convertTransactionToTxHex(transaction, index = 0) {
@@ -3929,6 +3967,18 @@ function setupBTCWallet({
3929
3967
  env = "mainnet"
3930
3968
  } = {}) {
3931
3969
  console.log("\u26A1\uFE0F BTC Wallet Version:", getVersion(), "env:", env);
3970
+ if (env === "private_mainnet" && typeof window !== "undefined") {
3971
+ setTimeout(() => {
3972
+ const hasShownNotice = localStorage.getItem("satoshi_private_mainnet_notice");
3973
+ if (!hasShownNotice) {
3974
+ Dialog.alert({
3975
+ title: "Notice",
3976
+ message: "You are currently using Satoshi Private Mainnet. This is a private version for testing. Please try a small amount of assets in Bridge"
3977
+ });
3978
+ localStorage.setItem("satoshi_private_mainnet_notice", "true");
3979
+ }
3980
+ }, 1e3);
3981
+ }
3932
3982
  const btcWallet = () => __async(this, null, function* () {
3933
3983
  return {
3934
3984
  id: "btc-wallet",
@@ -3952,7 +4002,7 @@ function setupBTCWallet({
3952
4002
 
3953
4003
  // src/index.ts
3954
4004
  var getVersion = () => {
3955
- return "0.3.32";
4005
+ return "0.3.33";
3956
4006
  };
3957
4007
  if (typeof window !== "undefined") {
3958
4008
  window.__BTC_WALLET_VERSION = getVersion();
@@ -3972,6 +4022,7 @@ export {
3972
4022
  XverseConnector,
3973
4023
  checkGasTokenArrears,
3974
4024
  checkGasTokenBalance,
4025
+ checkSatoshiWhitelist,
3975
4026
  estimateDepositAmount,
3976
4027
  executeBTCDepositAndAction,
3977
4028
  getAccountInfo,