btc-wallet 0.3.31 → 0.3.33

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,6 @@ export interface AccountInfo {
10
10
  }
11
11
  export interface DebtInfo {
12
12
  gas_token_id: string;
13
- transfer_amount: string;
14
13
  near_gas_debt_amount: string;
15
14
  protocol_fee_debt_amount: string;
16
15
  }
@@ -67,4 +66,5 @@ interface ExecuteBTCDepositAndActionParams<T extends boolean = true> {
67
66
  */
68
67
  type ExecuteBTCDepositAndActionReturn<T extends boolean> = T extends true ? FinalExecutionOutcome[] : string;
69
68
  export declare function executeBTCDepositAndAction<T extends boolean = true>({ action, amount, feeRate, fixedAmount, pollResult, registerDeposit, env, }: ExecuteBTCDepositAndActionParams<T>): Promise<ExecuteBTCDepositAndActionReturn<T>>;
69
+ export declare function checkSatoshiWhitelist(btcAccountId: string, env?: ENV): Promise<void>;
70
70
  export {};
package/dist/index.js CHANGED
@@ -92,6 +92,7 @@ __export(src_exports, {
92
92
  XverseConnector: () => XverseConnector,
93
93
  checkGasTokenArrears: () => checkGasTokenArrears,
94
94
  checkGasTokenBalance: () => checkGasTokenBalance,
95
+ checkSatoshiWhitelist: () => checkSatoshiWhitelist,
95
96
  estimateDepositAmount: () => estimateDepositAmount,
96
97
  executeBTCDepositAndAction: () => executeBTCDepositAndAction,
97
98
  getAccountInfo: () => getAccountInfo,
@@ -1392,6 +1393,14 @@ function retryOperation(_0, _1) {
1392
1393
  throw new Error("Unexpected execution path");
1393
1394
  });
1394
1395
  }
1396
+ function toHex(originalString) {
1397
+ const charArray = originalString.split("");
1398
+ const asciiArray = charArray.map((char) => char.charCodeAt(0));
1399
+ const hexArray = asciiArray.map((code) => code.toString(16));
1400
+ let hexString = hexArray.join("");
1401
+ hexString = hexString.replace(/(^0+)/g, "");
1402
+ return hexString;
1403
+ }
1395
1404
 
1396
1405
  // src/utils/ethereumUtils.ts
1397
1406
  var import_util2 = require("@ethereumjs/util");
@@ -2420,10 +2429,8 @@ function BtcWalletSelectorContextProvider({
2420
2429
  const [isProcessing, setIsProcessing] = (0, import_react11.useState)(false);
2421
2430
  const connectors = [
2422
2431
  new UnisatConnector(),
2423
- new XverseConnector(),
2424
2432
  new OKXConnector(),
2425
- new BitgetConnector(),
2426
- new MagicEdenConnector()
2433
+ new BitgetConnector()
2427
2434
  ];
2428
2435
  const walletSelectorContextValue = (0, import_react11.useMemo)(() => {
2429
2436
  const simpleFn = {};
@@ -2636,7 +2643,7 @@ var walletConfig = {
2636
2643
  accountContractId: "acc.toalice.near",
2637
2644
  bridgeContractId: "brg.toalice.near",
2638
2645
  walletUrl: "https://wallet-stg.satoshibridge.top",
2639
- bridgeUrl: "https://stg.satoshibridge.top/"
2646
+ bridgeUrl: "https://ramp.satos.network/"
2640
2647
  },
2641
2648
  mainnet: {
2642
2649
  base_url: "https://api.mainnet.satoshibridge.top",
@@ -2983,14 +2990,24 @@ function receiveTransaction(url, data) {
2983
2990
  return result_data;
2984
2991
  });
2985
2992
  }
2993
+ function preReceiveDepositMsg(_0, _1) {
2994
+ return __async(this, arguments, function* (url, { btcPublicKey, depositType = 1, postActions, extraMsg }) {
2995
+ const { result_code, result_message, result_data } = yield request(
2996
+ `${url}/v1/preReceiveDepositMsg`,
2997
+ {
2998
+ method: "POST",
2999
+ body: { btcPublicKey, depositType, postActions, extraMsg }
3000
+ }
3001
+ );
3002
+ console.log("preReceiveDepositMsg resp:", { result_code, result_message, result_data });
3003
+ if (result_code !== 0) {
3004
+ throw new Error(result_message);
3005
+ }
3006
+ return result_data;
3007
+ });
3008
+ }
2986
3009
  function receiveDepositMsg(_0, _1) {
2987
- return __async(this, arguments, function* (url, {
2988
- btcPublicKey,
2989
- txHash,
2990
- depositType = 1,
2991
- postActions,
2992
- extraMsg
2993
- }) {
3010
+ return __async(this, arguments, function* (url, { btcPublicKey, txHash, depositType = 1, postActions, extraMsg }) {
2994
3011
  const { result_code, result_message, result_data } = yield request(
2995
3012
  `${url}/v1/receiveDepositMsg`,
2996
3013
  {
@@ -3025,7 +3042,7 @@ function checkBridgeTransactionStatus(url, txHash) {
3025
3042
  }
3026
3043
  function checkBtcTransactionStatus(url, sig) {
3027
3044
  return __async(this, null, function* () {
3028
- const { result_code, result_message, result_data } = yield request(`${url}/v1/btcTx?sig=${sig}`, {
3045
+ const { result_code, result_message, result_data } = yield request(`${url}/v1/btcTx?sig=${toHex(sig)}`, {
3029
3046
  timeout: 3e5,
3030
3047
  pollingInterval: 5e3,
3031
3048
  maxPollingAttempts: 60,
@@ -3041,6 +3058,15 @@ function checkBtcTransactionStatus(url, sig) {
3041
3058
  return result_data;
3042
3059
  });
3043
3060
  }
3061
+ function getWhitelist(url) {
3062
+ return __async(this, null, function* () {
3063
+ const data = yield request(`${url}/v1/whitelist/users`).catch((error) => {
3064
+ console.error("getWhitelist error:", error);
3065
+ return [];
3066
+ });
3067
+ return data;
3068
+ });
3069
+ }
3044
3070
 
3045
3071
  // src/utils/Dialog.ts
3046
3072
  var Dialog = class {
@@ -3082,13 +3108,22 @@ var Dialog = class {
3082
3108
  });
3083
3109
  }
3084
3110
  static alert(options) {
3111
+ const messageEl = options.dangerouslyUseHTML ? { dangerouslySetInnerHTML: { __html: options.message } } : { children: options.message };
3085
3112
  return new Promise((resolve) => {
3113
+ var _a;
3086
3114
  this.injectStyles();
3087
3115
  const container = document.createElement("div");
3088
3116
  container.innerHTML = this.template;
3117
+ (_a = container.querySelector(".dialog-overlay")) == null ? void 0 : _a.classList.add("dialog-alert");
3118
+ if (options.closable === false) {
3119
+ const overlay = container.querySelector(".dialog-overlay");
3120
+ overlay.style.pointerEvents = "none";
3121
+ const dialogContainer = container.querySelector(".dialog-container");
3122
+ dialogContainer.style.pointerEvents = "auto";
3123
+ }
3089
3124
  document.body.appendChild(container);
3090
3125
  const titleEl = container.querySelector(".dialog-title");
3091
- const messageEl = container.querySelector(".dialog-message");
3126
+ const messageEl2 = container.querySelector(".dialog-message");
3092
3127
  const confirmBtn = container.querySelector(".dialog-confirm-btn");
3093
3128
  const cancelBtn = container.querySelector(".dialog-cancel-btn");
3094
3129
  if (options.title) {
@@ -3096,9 +3131,15 @@ var Dialog = class {
3096
3131
  } else {
3097
3132
  titleEl.style.display = "none";
3098
3133
  }
3099
- messageEl.textContent = options.message;
3134
+ messageEl2.innerHTML = options.message;
3100
3135
  cancelBtn.style.display = "none";
3136
+ if (options.closable === false) {
3137
+ confirmBtn.style.display = "none";
3138
+ }
3101
3139
  const cleanup = () => {
3140
+ if (options.closable === false) {
3141
+ return;
3142
+ }
3102
3143
  document.body.removeChild(container);
3103
3144
  };
3104
3145
  confirmBtn.addEventListener("click", () => {
@@ -3116,7 +3157,7 @@ Dialog.template = `
3116
3157
  <div class="dialog-message"></div>
3117
3158
  <div class="dialog-buttons">
3118
3159
  <button class="dialog-cancel-btn">Cancel</button>
3119
- <button class="dialog-confirm-btn">Confirm</button>
3160
+ <button class="dialog-confirm-btn">OK</button>
3120
3161
  </div>
3121
3162
  </div>
3122
3163
  </div>
@@ -3160,6 +3201,9 @@ Dialog.style = `
3160
3201
  justify-content: flex-end;
3161
3202
  gap: 12px;
3162
3203
  }
3204
+ .dialog-alert .dialog-buttons {
3205
+ justify-content: center;
3206
+ }
3163
3207
  .dialog-confirm-btn {
3164
3208
  padding: 8px 24px;
3165
3209
  background-color: #ff7a00;
@@ -3284,23 +3328,26 @@ function checkGasTokenBalance(csna, gasToken, minAmount, env) {
3284
3328
  }
3285
3329
  function checkGasTokenArrears(accountInfo, env, autoDeposit) {
3286
3330
  return __async(this, null, function* () {
3287
- var _a, _b, _c, _d;
3288
- 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))
3331
+ var _a, _b, _c;
3332
+ const debtAmount = new import_big.default(((_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();
3333
+ const relayerFeeAmount = ((_c = accountInfo == null ? void 0 : accountInfo.relayer_fee) == null ? void 0 : _c.amount) || "0";
3334
+ const hasDebtArrears = new import_big.default(debtAmount).gt(0);
3335
+ const hasRelayerFeeArrears = new import_big.default(relayerFeeAmount).gt(0);
3336
+ if (!hasDebtArrears && !hasRelayerFeeArrears)
3289
3337
  return;
3290
3338
  const config = yield getConfig(env);
3291
- const arrearsType = accountInfo.debt_info.transfer_amount ? "Deposit" : ((_c = accountInfo.relayer_fee) == null ? void 0 : _c.amount) ? "RelayerFee" : void 0;
3292
- const transferAmount = arrearsType === "Deposit" ? accountInfo.debt_info.transfer_amount : (_d = accountInfo.relayer_fee) == null ? void 0 : _d.amount;
3339
+ const transferAmount = hasDebtArrears ? debtAmount : relayerFeeAmount;
3293
3340
  console.log("get_account:", accountInfo);
3294
3341
  const action = {
3295
3342
  receiver_id: config.accountContractId,
3296
3343
  amount: transferAmount,
3297
- msg: JSON.stringify(arrearsType)
3344
+ msg: JSON.stringify(hasDebtArrears ? "Repay" : "RelayerFee")
3298
3345
  };
3299
3346
  if (!autoDeposit)
3300
3347
  return action;
3301
3348
  const confirmed = yield Dialog.confirm({
3302
- title: arrearsType === "Deposit" ? "Has gas token arrears" : "Has relayer fee arrears",
3303
- 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."
3349
+ title: hasDebtArrears ? "Has gas token arrears" : "Has relayer fee arrears",
3350
+ message: hasDebtArrears ? "You have gas token arrears, please deposit gas token to continue." : "You have relayer fee arrears, please deposit relayer fee to continue."
3304
3351
  });
3305
3352
  if (confirmed) {
3306
3353
  yield executeBTCDepositAndAction({ action, env });
@@ -3494,8 +3541,14 @@ function executeBTCDepositAndAction(_0) {
3494
3541
  console.log("user deposit address:", userDepositAddress);
3495
3542
  console.log("send amount:", sendAmount);
3496
3543
  console.log("fee rate:", _feeRate);
3497
- const txHash = yield sendBitcoin(userDepositAddress, Number(sendAmount), _feeRate);
3498
3544
  const postActionsStr = newActions.length > 0 ? JSON.stringify(newActions) : void 0;
3545
+ yield preReceiveDepositMsg(config.base_url, {
3546
+ btcPublicKey,
3547
+ depositType: postActionsStr || depositMsg.extra_msg ? 1 : 0,
3548
+ postActions: postActionsStr,
3549
+ extraMsg: depositMsg.extra_msg
3550
+ });
3551
+ const txHash = yield sendBitcoin(userDepositAddress, Number(sendAmount), _feeRate);
3499
3552
  yield receiveDepositMsg(config.base_url, {
3500
3553
  btcPublicKey,
3501
3554
  txHash,
@@ -3517,6 +3570,28 @@ function executeBTCDepositAndAction(_0) {
3517
3570
  }
3518
3571
  });
3519
3572
  }
3573
+ function checkSatoshiWhitelist(btcAccountId, env = "mainnet") {
3574
+ return __async(this, null, function* () {
3575
+ if (env !== "private_mainnet")
3576
+ return;
3577
+ if (!btcAccountId)
3578
+ return;
3579
+ const config = yield getConfig(env);
3580
+ const whitelist = yield getWhitelist(config.base_url);
3581
+ if (!(whitelist == null ? void 0 : whitelist.length))
3582
+ return;
3583
+ const isWhitelisted = whitelist.includes(btcAccountId);
3584
+ if (!isWhitelisted) {
3585
+ Dialog.alert({
3586
+ title: "Account is not whitelisted",
3587
+ 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>`,
3588
+ dangerouslyUseHTML: true,
3589
+ closable: false
3590
+ });
3591
+ throw new Error("Account is not whitelisted");
3592
+ }
3593
+ });
3594
+ }
3520
3595
 
3521
3596
  // src/core/setupBTCWallet.ts
3522
3597
  var { transfer, functionCall } = import_transactions.actionCreators;
@@ -3575,7 +3650,8 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3575
3650
  signMessage,
3576
3651
  isSignedIn,
3577
3652
  signAndSendTransaction,
3578
- signAndSendTransactions
3653
+ signAndSendTransactions,
3654
+ calculateGasLimit
3579
3655
  };
3580
3656
  const env = metadata.env || options.network.networkId || "mainnet";
3581
3657
  const currentConfig = walletConfig[env];
@@ -3588,6 +3664,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3588
3664
  const accountId = state.getAccount();
3589
3665
  const btcContext = window.btcContext;
3590
3666
  if (accountId && btcContext.account) {
3667
+ yield checkSatoshiWhitelist(btcContext.account, env);
3591
3668
  removeWalletButton();
3592
3669
  setupWalletButton(env, wallet, btcContext);
3593
3670
  } else {
@@ -3784,6 +3861,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3784
3861
  return result;
3785
3862
  });
3786
3863
  }
3864
+ function calculateGasLimit(params) {
3865
+ return __async(this, null, function* () {
3866
+ const accountId = state.getAccount();
3867
+ const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3868
+ const trans = [...params.transactions];
3869
+ console.log("raw trans:", trans);
3870
+ const gasTokenBalance = (accountInfo == null ? void 0 : accountInfo.gas_token[currentConfig.token]) || "0";
3871
+ const { gasLimit } = yield calculateGasStrategy(gasTokenBalance, trans);
3872
+ return gasLimit;
3873
+ });
3874
+ }
3787
3875
  function createGasTokenTransfer(accountId, amount) {
3788
3876
  return __async(this, null, function* () {
3789
3877
  return {
@@ -3837,58 +3925,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3837
3925
  }
3838
3926
  function calculateGasStrategy(gasTokenBalance, transactions2) {
3839
3927
  return __async(this, null, function* () {
3840
- var _a;
3841
3928
  const accountId = state.getAccount();
3842
- const nearAccount = yield provider.query({
3843
- request_type: "view_account",
3844
- account_id: accountId,
3845
- finality: "final"
3846
- });
3847
- const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
3848
- console.log("available near balance:", availableBalance);
3849
- console.log("available gas token balance:", gasTokenBalance);
3850
3929
  const convertTx = yield Promise.all(
3851
3930
  transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
3852
3931
  );
3853
- if (availableBalance > 0.2) {
3854
- console.log("near balance is enough, get the protocol fee of each transaction");
3855
- const gasTokens = yield nearCall2(
3856
- currentConfig.accountContractId,
3857
- "list_gas_token",
3858
- { token_ids: [currentConfig.token] }
3859
- );
3860
- console.log("list_gas_token gas tokens:", gasTokens);
3861
- const perTxFee = Math.max(
3862
- Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
3863
- 100
3864
- );
3865
- console.log("perTxFee:", perTxFee);
3866
- const protocolFee = new import_big2.default(perTxFee || "0").mul(convertTx.length).toFixed(0);
3867
- console.log("protocolFee:", protocolFee);
3868
- if (new import_big2.default(gasTokenBalance).gte(protocolFee)) {
3869
- console.log("use near pay gas and enough gas token balance");
3870
- return { useNearPayGas: true, gasLimit: protocolFee };
3871
- } else {
3872
- console.log("use near pay gas and not enough gas token balance");
3873
- const transferTx = yield createGasTokenTransfer(accountId, protocolFee);
3874
- return recalculateGasWithTransfer(transferTx, convertTx, true, perTxFee.toString());
3875
- }
3876
- } else {
3877
- console.log("near balance is not enough, predict the gas token amount required");
3878
- const adjustedGas = yield getPredictedGasAmount(
3879
- currentConfig.accountContractId,
3880
- currentConfig.token,
3881
- convertTx.map((t) => t.txHex)
3882
- );
3883
- if (new import_big2.default(gasTokenBalance).gte(adjustedGas)) {
3884
- console.log("use gas token and gas token balance is enough");
3885
- return { useNearPayGas: false, gasLimit: adjustedGas };
3886
- } else {
3887
- console.log("use gas token and gas token balance is not enough, need to transfer");
3888
- const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
3889
- return recalculateGasWithTransfer(transferTx, convertTx, false);
3890
- }
3891
- }
3932
+ const adjustedGas = yield getPredictedGasAmount(
3933
+ currentConfig.accountContractId,
3934
+ currentConfig.token,
3935
+ convertTx.map((t) => t.txHex)
3936
+ );
3937
+ const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
3938
+ return recalculateGasWithTransfer(transferTx, convertTx, false);
3892
3939
  });
3893
3940
  }
3894
3941
  function convertTransactionToTxHex(transaction, index = 0) {
@@ -3959,14 +4006,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3959
4006
  }
3960
4007
  return wallet;
3961
4008
  });
3962
- function toHex(originalString) {
3963
- const charArray = originalString.split("");
3964
- const asciiArray = charArray.map((char) => char.charCodeAt(0));
3965
- const hexArray = asciiArray.map((code) => code.toString(16));
3966
- let hexString = hexArray.join("");
3967
- hexString = hexString.replace(/(^0+)/g, "");
3968
- return hexString;
3969
- }
3970
4009
  function setupBTCWallet({
3971
4010
  iconUrl = "https://assets.deltatrade.ai/assets/chain/btc.svg",
3972
4011
  deprecated = false,
@@ -3975,6 +4014,18 @@ function setupBTCWallet({
3975
4014
  env = "mainnet"
3976
4015
  } = {}) {
3977
4016
  console.log("\u26A1\uFE0F BTC Wallet Version:", getVersion(), "env:", env);
4017
+ if (env === "private_mainnet" && typeof window !== "undefined") {
4018
+ setTimeout(() => {
4019
+ const hasShownNotice = localStorage.getItem("satoshi_private_mainnet_notice");
4020
+ if (!hasShownNotice) {
4021
+ Dialog.alert({
4022
+ title: "Notice",
4023
+ message: "You are currently using Satoshi Private Mainnet. This is a private version for testing. Please try a small amount of assets in Bridge"
4024
+ });
4025
+ localStorage.setItem("satoshi_private_mainnet_notice", "true");
4026
+ }
4027
+ }, 1e3);
4028
+ }
3978
4029
  const btcWallet = () => __async(this, null, function* () {
3979
4030
  return {
3980
4031
  id: "btc-wallet",
@@ -3998,7 +4049,7 @@ function setupBTCWallet({
3998
4049
 
3999
4050
  // src/index.ts
4000
4051
  var getVersion = () => {
4001
- return "0.3.31";
4052
+ return "0.3.33";
4002
4053
  };
4003
4054
  if (typeof window !== "undefined") {
4004
4055
  window.__BTC_WALLET_VERSION = getVersion();