btc-wallet 0.0.6 → 0.0.8
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/index.js +49 -49
- package/dist/index.js.map +3 -3
- package/dist/utils/initWalletButton.d.ts +1 -1
- package/dist/utils/setupBTCWallet.d.ts +2 -2
- package/esm/index.js +51 -55
- package/esm/index.js.map +3 -3
- package/package.json +1 -1
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Wallet } from '@near-wallet-selector/core';
|
1
|
+
import type { Wallet } from '@near-wallet-selector/core';
|
2
2
|
export declare function setupWalletButton(network: string, wallet: Wallet, originalWallet: {
|
3
3
|
account: string | null;
|
4
4
|
getPublicKey: () => Promise<string>;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import type { InjectedWallet, WalletModuleFactory } from
|
2
|
-
import type { useBtcWalletSelector } from
|
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
|
-
(
|
1816
|
-
if (!checkBTCVersion(options.aaOptions.accountContracts,
|
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(
|
1820
|
-
_setAccountContract(
|
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
|
1980
|
-
if (
|
1981
|
-
_setAccountContract(
|
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 {
|
2445
|
-
var
|
2446
|
-
|
2447
|
-
|
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);
|
@@ -2511,14 +2516,9 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2511
2516
|
const accountId = state.getAccount();
|
2512
2517
|
initWalletButton(options.network.networkId, accountId, wallet);
|
2513
2518
|
context.on("updatePublicKey", (btcPublicKey) => __async(void 0, null, function* () {
|
2514
|
-
const { nearTempAddress
|
2515
|
-
removeWalletButton();
|
2519
|
+
const { nearTempAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
|
2516
2520
|
setTimeout(() => {
|
2517
|
-
initWalletButton(
|
2518
|
-
options.network.networkId,
|
2519
|
-
nearTempAddress,
|
2520
|
-
wallet
|
2521
|
-
);
|
2521
|
+
initWalletButton(options.network.networkId, nearTempAddress, wallet);
|
2522
2522
|
}, 1e3);
|
2523
2523
|
emitter.emit("accountsChanged", {
|
2524
2524
|
accounts: [
|
@@ -2534,20 +2534,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2534
2534
|
});
|
2535
2535
|
}));
|
2536
2536
|
if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
|
2537
|
-
btcContext.autoConnect();
|
2537
|
+
yield btcContext.autoConnect();
|
2538
2538
|
}
|
2539
2539
|
clearInterval(inter);
|
2540
2540
|
}
|
2541
2541
|
}), 500);
|
2542
2542
|
}
|
2543
2543
|
function viewMethod(_02) {
|
2544
|
-
return __async(this, arguments, function* ({
|
2545
|
-
method,
|
2546
|
-
args = {}
|
2547
|
-
}) {
|
2544
|
+
return __async(this, arguments, function* ({ method, args = {} }) {
|
2548
2545
|
const res = yield provider.query({
|
2549
2546
|
request_type: "call_function",
|
2550
|
-
account_id: contractId,
|
2547
|
+
account_id: config[options.network.networkId].contractId,
|
2551
2548
|
method_name: method,
|
2552
2549
|
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2553
2550
|
finality: "optimistic"
|
@@ -2575,7 +2572,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2575
2572
|
});
|
2576
2573
|
}
|
2577
2574
|
function signIn(_02) {
|
2578
|
-
return __async(this, arguments, function* ({ contractId
|
2575
|
+
return __async(this, arguments, function* ({ contractId, methodNames }) {
|
2579
2576
|
const accountId = state.getAccount();
|
2580
2577
|
const publicKey = state.getPublicKey();
|
2581
2578
|
const btcContext = window.btcContext;
|
@@ -2649,11 +2646,11 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2649
2646
|
nonce: BigInt(rawAccessKey.nonce || 0)
|
2650
2647
|
});
|
2651
2648
|
const publicKeyFormat = PublicKey.from(publicKey);
|
2652
|
-
const nearNonceApi = yield getNearNonceFromApi(accountId);
|
2649
|
+
const nearNonceApi = yield getNearNonceFromApi(options.network.networkId, accountId);
|
2653
2650
|
const newTransactions = params.transactions.map((transaction, index) => {
|
2654
2651
|
let nearNonceNumber = accessKey.nonce + BigInt(1);
|
2655
2652
|
if (nearNonceApi) {
|
2656
|
-
nearNonceNumber =
|
2653
|
+
nearNonceNumber = BigInt(nearNonceApi.result_data) > nearNonceNumber ? BigInt(nearNonceApi.result_data) : nearNonceNumber;
|
2657
2654
|
}
|
2658
2655
|
const newActions = transaction.actions.map((action) => {
|
2659
2656
|
switch (action.type) {
|
@@ -2677,8 +2674,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2677
2674
|
baseDecode(header.hash)
|
2678
2675
|
);
|
2679
2676
|
const txBytes = encodeTransaction(_transaction);
|
2680
|
-
const txHex = Array.from(
|
2681
|
-
|
2677
|
+
const txHex = Array.from(
|
2678
|
+
txBytes,
|
2679
|
+
(byte) => ("0" + (byte & 255).toString(16)).slice(-2)
|
2680
|
+
).join("");
|
2682
2681
|
const hash = bs58.encode(new Uint8Array(sha256.array(txBytes)));
|
2683
2682
|
return { txBytes, txHex, hash };
|
2684
2683
|
});
|
@@ -2686,29 +2685,26 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2686
2685
|
method: "get_account",
|
2687
2686
|
args: { account_id: accountId }
|
2688
2687
|
});
|
2689
|
-
const nonceApi = yield getNonceFromApi(accountId);
|
2690
|
-
const nonce = (nonceApi == null ? void 0 : nonceApi.result_data)
|
2688
|
+
const nonceApi = yield getNonceFromApi(options.network.networkId, accountId);
|
2689
|
+
const nonce = Number(nonceApi == null ? void 0 : nonceApi.result_data) > Number(accountInfo.nonce) ? String(nonceApi == null ? void 0 : nonceApi.result_data) : String(accountInfo.nonce);
|
2691
2690
|
const intention = {
|
2692
2691
|
chain_id: "397",
|
2693
2692
|
csna: accountId,
|
2694
2693
|
near_transactions: newTransactions.map((t) => t.txHex),
|
2695
|
-
gas_token: token,
|
2694
|
+
gas_token: config[options.network.networkId].token,
|
2696
2695
|
gas_limit: "3000",
|
2697
|
-
nonce
|
2696
|
+
nonce
|
2698
2697
|
};
|
2699
2698
|
const strIntention = JSON.stringify(intention);
|
2700
2699
|
const signature = yield btcContext.signMessage(strIntention);
|
2701
|
-
const result = yield uploadBTCTx({
|
2700
|
+
const result = yield uploadBTCTx(options.network.networkId, {
|
2702
2701
|
sig: signature,
|
2703
2702
|
btcPubKey: state.getBtcPublicKey(),
|
2704
2703
|
data: toHex(strIntention)
|
2705
2704
|
});
|
2706
2705
|
if (result.result_code === 0) {
|
2707
2706
|
const hash = newTransactions.map((t) => t.hash);
|
2708
|
-
const result2 = yield pollTransactionStatuses(
|
2709
|
-
options.network.networkId,
|
2710
|
-
hash
|
2711
|
-
);
|
2707
|
+
const result2 = yield pollTransactionStatuses(options.network.networkId, hash);
|
2712
2708
|
return result2;
|
2713
2709
|
} else {
|
2714
2710
|
return null;
|
@@ -2717,24 +2713,24 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2717
2713
|
}
|
2718
2714
|
return wallet;
|
2719
2715
|
});
|
2720
|
-
function getNonceFromApi(accountId) {
|
2721
|
-
return fetch(`${base_url}/nonce?csna=${accountId}`, {
|
2716
|
+
function getNonceFromApi(network, accountId) {
|
2717
|
+
return fetch(`${config[network].base_url}/nonce?csna=${accountId}`, {
|
2722
2718
|
method: "GET",
|
2723
2719
|
headers: {
|
2724
2720
|
"Content-Type": "application/json"
|
2725
2721
|
}
|
2726
2722
|
}).then((res) => res.json());
|
2727
2723
|
}
|
2728
|
-
function getNearNonceFromApi(accountId) {
|
2729
|
-
return fetch(`${base_url}/nonceNear?csna=${accountId}`, {
|
2724
|
+
function getNearNonceFromApi(network, accountId) {
|
2725
|
+
return fetch(`${config[network].base_url}/nonceNear?csna=${accountId}`, {
|
2730
2726
|
method: "GET",
|
2731
2727
|
headers: {
|
2732
2728
|
"Content-Type": "application/json"
|
2733
2729
|
}
|
2734
2730
|
}).then((res) => res.json());
|
2735
2731
|
}
|
2736
|
-
function uploadBTCTx(data) {
|
2737
|
-
return fetch(`${base_url}/receiveTransaction`, {
|
2732
|
+
function uploadBTCTx(network, data) {
|
2733
|
+
return fetch(`${config[network].base_url}/receiveTransaction`, {
|
2738
2734
|
method: "POST",
|
2739
2735
|
headers: {
|
2740
2736
|
"Content-Type": "application/json"
|
@@ -2775,13 +2771,13 @@ function toHex(originalString) {
|
|
2775
2771
|
}
|
2776
2772
|
function initWalletButton(network, accountId, wallet) {
|
2777
2773
|
const checkAndSetupWalletButton = () => {
|
2778
|
-
if (accountId
|
2774
|
+
if (accountId) {
|
2779
2775
|
setupWalletButton(network, wallet, window.btcContext);
|
2780
|
-
|
2781
|
-
removeWalletButton();
|
2782
|
-
setTimeout(() => {
|
2776
|
+
!window.btcContext.account && setTimeout(() => {
|
2783
2777
|
checkAndSetupWalletButton();
|
2784
2778
|
}, 5e3);
|
2779
|
+
} else {
|
2780
|
+
removeWalletButton();
|
2785
2781
|
}
|
2786
2782
|
};
|
2787
2783
|
checkAndSetupWalletButton();
|
@@ -2834,7 +2830,7 @@ function pollTransactionStatuses(network, hashes) {
|
|
2834
2830
|
|
2835
2831
|
// src/index.ts
|
2836
2832
|
var getVersion = () => {
|
2837
|
-
return "0.0.
|
2833
|
+
return "0.0.8";
|
2838
2834
|
};
|
2839
2835
|
if (typeof window !== "undefined") {
|
2840
2836
|
window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();
|