btc-wallet 0.3.17 → 0.3.19
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/dist/connector/base.d.ts +2 -2
- package/dist/core/btcUtils.d.ts +1 -1
- package/dist/core/btcWalletSelectorContext.d.ts +2 -1
- package/dist/index.js +136 -84
- package/dist/index.js.map +3 -3
- package/esm/index.js +134 -82
- package/esm/index.js.map +3 -3
- package/package.json +1 -1
package/esm/index.js
CHANGED
@@ -85,6 +85,7 @@ var InjectedConnector = class extends BaseConnector {
|
|
85
85
|
requestAccounts() {
|
86
86
|
return __async(this, null, function* () {
|
87
87
|
const accounts = yield this.getProviderOrThrow().requestAccounts();
|
88
|
+
console.log("network:", yield this.getNetwork());
|
88
89
|
console.log("\u{1F680} ~ InjectedConnector ~ requestAccounts ~ accounts:", accounts);
|
89
90
|
return accounts;
|
90
91
|
});
|
@@ -270,7 +271,7 @@ var _network, _event;
|
|
270
271
|
var XverseConnector = class extends BaseConnector {
|
271
272
|
constructor() {
|
272
273
|
super();
|
273
|
-
__privateAdd(this, _network, "
|
274
|
+
__privateAdd(this, _network, "Mainnet");
|
274
275
|
__privateAdd(this, _event, new EventEmitter());
|
275
276
|
this.metadata = {
|
276
277
|
id: "xverse",
|
@@ -446,6 +447,7 @@ var XverseConnector = class extends BaseConnector {
|
|
446
447
|
});
|
447
448
|
}
|
448
449
|
};
|
450
|
+
console.log("\u{1F680} ~ XverseConnector ~ sendBitcoin ~ sendBtcOptions:", sendBtcOptions);
|
449
451
|
sendBtcTransaction(sendBtcOptions).catch((e) => reject(e));
|
450
452
|
});
|
451
453
|
return result;
|
@@ -2446,12 +2448,22 @@ function InitBtcWalletSelectorContext() {
|
|
2446
2448
|
return null;
|
2447
2449
|
}
|
2448
2450
|
function useBtcWalletSelector() {
|
2449
|
-
const { openConnectModal,
|
2450
|
-
const {
|
2451
|
+
const { openConnectModal, disconnect, requestDirectAccount } = useConnectModal();
|
2452
|
+
const {
|
2453
|
+
accounts,
|
2454
|
+
sendBitcoin: sendBitcoin2,
|
2455
|
+
getPublicKey,
|
2456
|
+
provider,
|
2457
|
+
signMessage,
|
2458
|
+
connector,
|
2459
|
+
getNetwork: getNetwork2,
|
2460
|
+
switchNetwork
|
2461
|
+
} = useBTCProvider();
|
2451
2462
|
const publicKey = useRef(null);
|
2452
2463
|
const signMessageFn = useRef(null);
|
2453
2464
|
const connectorRef = useRef(null);
|
2454
2465
|
const context = useContext2(WalletSelectorContext);
|
2466
|
+
const isLoggingIn = useRef(false);
|
2455
2467
|
useEffect6(() => {
|
2456
2468
|
if (provider) {
|
2457
2469
|
getPublicKey().then((res) => {
|
@@ -2486,7 +2498,27 @@ function useBtcWalletSelector() {
|
|
2486
2498
|
login: () => __async(this, null, function* () {
|
2487
2499
|
const account = accounts == null ? void 0 : accounts[0];
|
2488
2500
|
if (!account) {
|
2489
|
-
|
2501
|
+
if (isLoggingIn.current) {
|
2502
|
+
return null;
|
2503
|
+
}
|
2504
|
+
try {
|
2505
|
+
isLoggingIn.current = true;
|
2506
|
+
openConnectModal == null ? void 0 : openConnectModal();
|
2507
|
+
const account1 = yield retryOperation(
|
2508
|
+
() => window.btcContext.account,
|
2509
|
+
(res) => !!res,
|
2510
|
+
{
|
2511
|
+
maxRetries: 100,
|
2512
|
+
delayMs: 1e3
|
2513
|
+
}
|
2514
|
+
);
|
2515
|
+
if (!account1) {
|
2516
|
+
throw new Error("Failed to get account");
|
2517
|
+
}
|
2518
|
+
return account1;
|
2519
|
+
} finally {
|
2520
|
+
isLoggingIn.current = false;
|
2521
|
+
}
|
2490
2522
|
}
|
2491
2523
|
return account;
|
2492
2524
|
}),
|
@@ -2513,16 +2545,18 @@ function useBtcWalletSelector() {
|
|
2513
2545
|
return context;
|
2514
2546
|
},
|
2515
2547
|
getNetwork: getNetwork2,
|
2548
|
+
switchNetwork,
|
2516
2549
|
sendBitcoin: sendBitcoin2
|
2517
2550
|
};
|
2518
2551
|
}, [
|
2519
2552
|
accounts,
|
2520
|
-
context,
|
2521
|
-
disconnect,
|
2522
2553
|
getNetwork2,
|
2554
|
+
switchNetwork,
|
2555
|
+
sendBitcoin2,
|
2523
2556
|
openConnectModal,
|
2524
2557
|
requestDirectAccount,
|
2525
|
-
|
2558
|
+
context,
|
2559
|
+
disconnect
|
2526
2560
|
]);
|
2527
2561
|
return hook;
|
2528
2562
|
}
|
@@ -3184,11 +3218,11 @@ function getAccountInfo(csna, accountContractId) {
|
|
3184
3218
|
return accountInfo;
|
3185
3219
|
});
|
3186
3220
|
}
|
3187
|
-
function checkGasTokenBalance(csna, gasToken, isDev) {
|
3221
|
+
function checkGasTokenBalance(csna, gasToken, minAmount, isDev) {
|
3188
3222
|
return __async(this, null, function* () {
|
3189
3223
|
const amount = yield nearCall(gasToken, "ft_balance_of", { account_id: csna });
|
3190
3224
|
console.log("gas token balance:", amount);
|
3191
|
-
if (new Big(amount).
|
3225
|
+
if (new Big(amount).lt(minAmount)) {
|
3192
3226
|
yield Dialog.confirm({
|
3193
3227
|
title: "Gas token balance is insufficient",
|
3194
3228
|
message: "Please deposit gas token to continue, will open bridge website."
|
@@ -3444,7 +3478,6 @@ var state = {
|
|
3444
3478
|
return window.localStorage.getItem("btc-wallet-btc-publickey");
|
3445
3479
|
}
|
3446
3480
|
};
|
3447
|
-
var inter = null;
|
3448
3481
|
var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
3449
3482
|
metadata,
|
3450
3483
|
options,
|
@@ -3461,47 +3494,74 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3461
3494
|
getAccounts,
|
3462
3495
|
verifyOwner,
|
3463
3496
|
signMessage,
|
3497
|
+
isSignedIn,
|
3464
3498
|
signAndSendTransaction,
|
3465
3499
|
signAndSendTransactions
|
3466
3500
|
};
|
3467
3501
|
const isDev = (_a = "isDev" in metadata && metadata.isDev) != null ? _a : false;
|
3468
3502
|
const currentConfig = isDev ? walletConfig.dev : walletConfig[options.network.networkId];
|
3469
3503
|
const walletNetwork = isDev ? "dev" : options.network.networkId;
|
3470
|
-
|
3471
|
-
|
3472
|
-
|
3473
|
-
const
|
3474
|
-
|
3475
|
-
|
3476
|
-
const
|
3477
|
-
|
3478
|
-
|
3504
|
+
yield initBtcContext();
|
3505
|
+
function setupBtcContextListeners() {
|
3506
|
+
return __async(this, null, function* () {
|
3507
|
+
const handleConnectionUpdate = () => __async(this, null, function* () {
|
3508
|
+
yield checkBtcNetwork(walletNetwork);
|
3509
|
+
const accountId = state.getAccount();
|
3510
|
+
const btcContext = window.btcContext;
|
3511
|
+
if (accountId && btcContext.account) {
|
3512
|
+
setupWalletButton(walletNetwork, wallet, btcContext);
|
3513
|
+
} else {
|
3479
3514
|
removeWalletButton();
|
3480
|
-
|
3481
|
-
|
3482
|
-
|
3483
|
-
{
|
3484
|
-
accountId: nearTempAddress
|
3485
|
-
}
|
3486
|
-
]
|
3487
|
-
});
|
3488
|
-
}));
|
3489
|
-
context.on("btcLoginError", (e) => __async(void 0, null, function* () {
|
3490
|
-
emitter.emit("accountsChanged", {
|
3491
|
-
accounts: []
|
3492
|
-
});
|
3493
|
-
}));
|
3494
|
-
context.on("btcLogOut", (e) => __async(void 0, null, function* () {
|
3495
|
-
emitter.emit("accountsChanged", {
|
3496
|
-
accounts: []
|
3497
|
-
});
|
3498
|
-
}));
|
3499
|
-
if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
|
3500
|
-
yield btcContext.autoConnect();
|
3515
|
+
setTimeout(() => {
|
3516
|
+
handleConnectionUpdate();
|
3517
|
+
}, 5e3);
|
3501
3518
|
}
|
3502
|
-
|
3519
|
+
});
|
3520
|
+
const context = window.btcContext.getContext();
|
3521
|
+
context.on("updatePublicKey", (btcPublicKey) => __async(this, null, function* () {
|
3522
|
+
console.log("updatePublicKey");
|
3523
|
+
const { nearAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
|
3524
|
+
emitter.emit("accountsChanged", {
|
3525
|
+
accounts: [{ accountId: nearAddress }]
|
3526
|
+
});
|
3527
|
+
yield handleConnectionUpdate();
|
3528
|
+
}));
|
3529
|
+
context.on("btcLoginError", () => __async(this, null, function* () {
|
3530
|
+
console.log("btcLoginError");
|
3531
|
+
emitter.emit("accountsChanged", { accounts: [] });
|
3532
|
+
yield handleConnectionUpdate();
|
3533
|
+
}));
|
3534
|
+
context.on("btcLogOut", () => __async(this, null, function* () {
|
3535
|
+
console.log("btcLogOut");
|
3536
|
+
emitter.emit("accountsChanged", { accounts: [] });
|
3537
|
+
yield handleConnectionUpdate();
|
3538
|
+
}));
|
3539
|
+
yield handleConnectionUpdate();
|
3540
|
+
if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
|
3541
|
+
yield window.btcContext.autoConnect();
|
3503
3542
|
}
|
3504
|
-
})
|
3543
|
+
});
|
3544
|
+
}
|
3545
|
+
function initBtcContext() {
|
3546
|
+
return __async(this, null, function* () {
|
3547
|
+
console.log("initBtcContext");
|
3548
|
+
const btcContext = yield retryOperation(
|
3549
|
+
() => __async(this, null, function* () {
|
3550
|
+
const ctx = window.btcContext;
|
3551
|
+
if (!ctx) {
|
3552
|
+
throw new Error("btcContext not found");
|
3553
|
+
}
|
3554
|
+
return ctx;
|
3555
|
+
}),
|
3556
|
+
(res) => !!res,
|
3557
|
+
{
|
3558
|
+
maxRetries: 10,
|
3559
|
+
delayMs: 500
|
3560
|
+
}
|
3561
|
+
);
|
3562
|
+
yield setupBtcContextListeners();
|
3563
|
+
return btcContext;
|
3564
|
+
});
|
3505
3565
|
}
|
3506
3566
|
function nearCall2(contractId, methodName, args) {
|
3507
3567
|
return __async(this, null, function* () {
|
@@ -3510,53 +3570,44 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3510
3570
|
}
|
3511
3571
|
function getNearAccountByBtcPublicKey(btcPublicKey) {
|
3512
3572
|
return __async(this, null, function* () {
|
3513
|
-
const
|
3573
|
+
const nearAddress = yield nearCall2(
|
3514
3574
|
currentConfig.accountContractId,
|
3515
3575
|
"get_chain_signature_near_account_id",
|
3516
3576
|
{ btc_public_key: btcPublicKey }
|
3517
3577
|
);
|
3518
|
-
const
|
3578
|
+
const nearPublicKey = yield nearCall2(
|
3519
3579
|
currentConfig.accountContractId,
|
3520
3580
|
"get_chain_signature_near_account_public_key",
|
3521
3581
|
{ btc_public_key: btcPublicKey }
|
3522
3582
|
);
|
3523
|
-
state.saveAccount(
|
3524
|
-
state.savePublicKey(
|
3583
|
+
state.saveAccount(nearAddress);
|
3584
|
+
state.savePublicKey(nearPublicKey);
|
3525
3585
|
state.saveBtcPublicKey(btcPublicKey);
|
3526
3586
|
return {
|
3527
|
-
|
3528
|
-
|
3587
|
+
nearAddress,
|
3588
|
+
nearPublicKey
|
3529
3589
|
};
|
3530
3590
|
});
|
3531
3591
|
}
|
3532
3592
|
function signIn(_02) {
|
3533
3593
|
return __async(this, arguments, function* ({ contractId, methodNames }) {
|
3594
|
+
const btcContext = window.btcContext;
|
3534
3595
|
const accountId = state.getAccount();
|
3535
3596
|
const publicKey = state.getPublicKey();
|
3536
|
-
|
3537
|
-
|
3538
|
-
|
3539
|
-
return [
|
3540
|
-
{
|
3541
|
-
accountId,
|
3542
|
-
publicKey
|
3543
|
-
}
|
3544
|
-
];
|
3597
|
+
console.log("isLogin:", accountId && publicKey);
|
3598
|
+
if (!accountId || !publicKey) {
|
3599
|
+
yield btcContext.login();
|
3545
3600
|
}
|
3546
|
-
yield btcContext.
|
3547
|
-
const btcPublicKey = yield retryOperation(btcContext.getPublicKey, (res) => !!res, {
|
3548
|
-
maxRetries: 40,
|
3549
|
-
delayMs: 3e3
|
3550
|
-
});
|
3601
|
+
const btcPublicKey = yield btcContext.getPublicKey();
|
3551
3602
|
console.log("btcPublicKey:", btcPublicKey);
|
3552
3603
|
if (!btcPublicKey) {
|
3553
3604
|
throw new Error("No connected BTC wallet, please connect your BTC wallet first.");
|
3554
3605
|
}
|
3555
|
-
const {
|
3606
|
+
const { nearAddress, nearPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
|
3556
3607
|
return [
|
3557
3608
|
{
|
3558
|
-
accountId:
|
3559
|
-
publicKey:
|
3609
|
+
accountId: nearAddress,
|
3610
|
+
publicKey: nearPublicKey
|
3560
3611
|
}
|
3561
3612
|
];
|
3562
3613
|
});
|
@@ -3577,6 +3628,11 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3577
3628
|
removeWalletButton();
|
3578
3629
|
});
|
3579
3630
|
}
|
3631
|
+
function isSignedIn() {
|
3632
|
+
const accountId = state.getAccount();
|
3633
|
+
const publicKey = state.getPublicKey();
|
3634
|
+
return accountId && publicKey;
|
3635
|
+
}
|
3580
3636
|
function getAccounts() {
|
3581
3637
|
return __async(this, null, function* () {
|
3582
3638
|
return [{ accountId: state.getAccount() }];
|
@@ -3608,7 +3664,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3608
3664
|
const btcContext = window.btcContext;
|
3609
3665
|
const accountId = state.getAccount();
|
3610
3666
|
const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
|
3611
|
-
yield checkGasTokenBalance(accountId, currentConfig.token, isDev);
|
3612
3667
|
yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, true);
|
3613
3668
|
const trans = [...params.transactions];
|
3614
3669
|
console.log("raw trans:", trans);
|
@@ -3620,6 +3675,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3620
3675
|
console.log("transferGasTransaction:", transferGasTransaction);
|
3621
3676
|
console.log("useNearPayGas:", useNearPayGas);
|
3622
3677
|
console.log("gasLimit:", gasLimit);
|
3678
|
+
yield checkGasTokenBalance(accountId, currentConfig.token, gasLimit, isDev);
|
3623
3679
|
if (transferGasTransaction) {
|
3624
3680
|
trans.unshift(transferGasTransaction);
|
3625
3681
|
}
|
@@ -3810,22 +3866,18 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3810
3866
|
return { txBytes, txHex, hash };
|
3811
3867
|
});
|
3812
3868
|
}
|
3813
|
-
function
|
3869
|
+
function checkBtcNetwork(network) {
|
3814
3870
|
return __async(this, null, function* () {
|
3815
|
-
const
|
3816
|
-
|
3817
|
-
|
3818
|
-
|
3819
|
-
|
3820
|
-
|
3821
|
-
|
3822
|
-
|
3823
|
-
|
3824
|
-
|
3825
|
-
}
|
3826
|
-
};
|
3827
|
-
yield delay(1e3);
|
3828
|
-
checkAndSetupWalletButton();
|
3871
|
+
const btcContext = window.btcContext;
|
3872
|
+
if (!btcContext.account)
|
3873
|
+
return;
|
3874
|
+
const btcNetwork = yield btcContext.getNetwork();
|
3875
|
+
console.log("btcNetwork:", btcNetwork, network);
|
3876
|
+
if (network === "mainnet" && btcNetwork !== "livenet") {
|
3877
|
+
yield btcContext.switchNetwork("livenet");
|
3878
|
+
} else if (network === "testnet" && btcNetwork !== "testnet") {
|
3879
|
+
yield btcContext.switchNetwork("testnet");
|
3880
|
+
}
|
3829
3881
|
});
|
3830
3882
|
}
|
3831
3883
|
return wallet;
|
@@ -3869,7 +3921,7 @@ function setupBTCWallet({
|
|
3869
3921
|
|
3870
3922
|
// src/index.ts
|
3871
3923
|
var getVersion = () => {
|
3872
|
-
return "0.3.
|
3924
|
+
return "0.3.19";
|
3873
3925
|
};
|
3874
3926
|
if (typeof window !== "undefined") {
|
3875
3927
|
window.__BTC_WALLET_VERSION = getVersion();
|