btc-wallet 0.2.5 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +60 -6
- package/dist/components/signModal/index.d.ts +1 -1
- package/dist/config.d.ts +14 -0
- package/dist/context/index.d.ts +0 -2
- package/dist/core/bridgeSupplyUtils.d.ts +10 -0
- package/dist/{components → core}/btcWalletSelectorContext.d.ts +4 -1
- package/dist/{utils → core}/setupBTCWallet.d.ts +1 -1
- package/dist/evmSigner/index.d.ts +2737 -817
- package/dist/index.d.ts +3 -3
- package/dist/index.js +395 -228
- package/dist/index.js.map +4 -4
- package/dist/utils/ethereumUtils.d.ts +1 -4
- package/dist/utils/index.d.ts +0 -5
- package/dist/utils/request.d.ts +11 -0
- package/esm/index.js +391 -218
- package/esm/index.js.map +4 -4
- package/package.json +9 -7
- package/dist/components/hook.d.ts +0 -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("\u{1F680} ~ InjectedConnector ~ requestAccounts ~ accounts:", accounts);
|
88
89
|
return accounts;
|
89
90
|
});
|
90
91
|
}
|
@@ -485,7 +486,7 @@ var useAccounts = () => {
|
|
485
486
|
// src/hooks/useBTCProvider.ts
|
486
487
|
import { useCallback } from "react";
|
487
488
|
var useBTCProvider = () => {
|
488
|
-
const { connector, provider, accounts, getPublicKey, signMessage, getNetwork, switchNetwork, sendBitcoin } = useConnectProvider();
|
489
|
+
const { connector, provider, accounts, getPublicKey, signMessage, getNetwork, switchNetwork, sendBitcoin: sendBitcoin2 } = useConnectProvider();
|
489
490
|
const sendInscription = useCallback(
|
490
491
|
(address, inscriptionId, options) => __async(void 0, null, function* () {
|
491
492
|
if (!connector) {
|
@@ -496,7 +497,7 @@ var useBTCProvider = () => {
|
|
496
497
|
}),
|
497
498
|
[connector]
|
498
499
|
);
|
499
|
-
return { provider, accounts, getPublicKey, signMessage, getNetwork, switchNetwork, sendBitcoin, sendInscription, connector };
|
500
|
+
return { provider, accounts, getPublicKey, signMessage, getNetwork, switchNetwork, sendBitcoin: sendBitcoin2, sendInscription, connector };
|
500
501
|
};
|
501
502
|
|
502
503
|
// src/hooks/useConnectModal.ts
|
@@ -650,6 +651,7 @@ var WalletClientProvider = class {
|
|
650
651
|
}
|
651
652
|
}
|
652
653
|
if (method === "personal_sign" /* personalSign */) {
|
654
|
+
console.log("personal_sign ---- ", arg);
|
653
655
|
return new Promise((resolve, reject) => {
|
654
656
|
eventUtils_default.emit("personalSign" /* personalSign */, arg);
|
655
657
|
eventUtils_default.once("personalSignResult" /* personalSignResult */, ({ result, error }) => {
|
@@ -679,7 +681,7 @@ var WalletClientProvider = class {
|
|
679
681
|
|
680
682
|
// src/hooks/useETHProvider.ts
|
681
683
|
var useETHProvider = () => {
|
682
|
-
const { evmAccount, smartAccount
|
684
|
+
const { evmAccount, smartAccount } = useConnectProvider();
|
683
685
|
const [chainId, setChainId] = useState();
|
684
686
|
useEffect(() => {
|
685
687
|
if (smartAccount) {
|
@@ -773,7 +775,11 @@ var useETHProvider = () => {
|
|
773
775
|
return (smartAccount == null ? void 0 : smartAccount.provider).publicClient;
|
774
776
|
}, [smartAccount, chainId]);
|
775
777
|
const provider = useMemo(() => {
|
776
|
-
const ethereumProvider = new EthereumProvider(
|
778
|
+
const ethereumProvider = new EthereumProvider(
|
779
|
+
sendUserOp,
|
780
|
+
smartAccount == null ? void 0 : smartAccount.provider,
|
781
|
+
evmAccount
|
782
|
+
);
|
777
783
|
return ethereumProvider;
|
778
784
|
}, [evmAccount, sendUserOp, smartAccount == null ? void 0 : smartAccount.provider]);
|
779
785
|
const walletClient = useMemo(() => {
|
@@ -785,7 +791,6 @@ var useETHProvider = () => {
|
|
785
791
|
provider,
|
786
792
|
evmAccount,
|
787
793
|
account: evmAccount,
|
788
|
-
getSmartAccountInfo,
|
789
794
|
switchChain,
|
790
795
|
chainId,
|
791
796
|
getFeeQuotes,
|
@@ -955,6 +960,7 @@ var ConnectModal = ({ open, onClose }) => {
|
|
955
960
|
yield connect(connector.metadata.id);
|
956
961
|
closeConnectModal();
|
957
962
|
} catch (error) {
|
963
|
+
console.error("onConnect error", error);
|
958
964
|
if (error.code === 4001) {
|
959
965
|
setRetryVisible(true);
|
960
966
|
}
|
@@ -1082,7 +1088,13 @@ var checkBTCVersion = (accountContracts, accountContractKey, version) => {
|
|
1082
1088
|
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
1083
1089
|
|
1084
1090
|
// src/utils/ethereumUtils.ts
|
1085
|
-
import {
|
1091
|
+
import {
|
1092
|
+
bytesToHex,
|
1093
|
+
publicToAddress,
|
1094
|
+
toBytes,
|
1095
|
+
toChecksumAddress,
|
1096
|
+
toRpcSig
|
1097
|
+
} from "@ethereumjs/util";
|
1086
1098
|
import bitcore from "bitcore-lib";
|
1087
1099
|
var pubKeyToEVMAddress = (pubKey) => {
|
1088
1100
|
const address = toChecksumAddress(bytesToHex(publicToAddress(toBytes(`0x${pubKey}`), true)));
|
@@ -1097,57 +1109,6 @@ var convertSignature = (signature) => {
|
|
1097
1109
|
function caculateNativeFee(userOp) {
|
1098
1110
|
return (BigInt(userOp.callGasLimit) + BigInt(userOp.verificationGasLimit) + BigInt(userOp.preVerificationGas)) * BigInt(userOp.maxFeePerGas);
|
1099
1111
|
}
|
1100
|
-
var getBTCAAAddress = (smartAccount, btcAddress, name, version) => __async(void 0, null, function* () {
|
1101
|
-
var _a;
|
1102
|
-
const addresses = yield smartAccount.provider.request({ method: "eth_accounts" });
|
1103
|
-
const owner = addresses[0];
|
1104
|
-
const localKey = `particle_${name}_${version}_${owner}`;
|
1105
|
-
if (typeof window !== "undefined" && localStorage) {
|
1106
|
-
const localAA = localStorage.getItem(localKey);
|
1107
|
-
if (localAA) {
|
1108
|
-
return localAA;
|
1109
|
-
}
|
1110
|
-
}
|
1111
|
-
const btcPublicKey = yield smartAccount.provider.getPublicKey();
|
1112
|
-
const accountInfo = yield smartAccount.sendRpc({
|
1113
|
-
method: "particle_aa_getBTCAccount",
|
1114
|
-
params: [
|
1115
|
-
{
|
1116
|
-
name,
|
1117
|
-
version,
|
1118
|
-
btcPublicKey,
|
1119
|
-
btcAddress
|
1120
|
-
}
|
1121
|
-
]
|
1122
|
-
});
|
1123
|
-
const address = (_a = accountInfo == null ? void 0 : accountInfo[0]) == null ? void 0 : _a.smartAccountAddress;
|
1124
|
-
if (typeof window !== "undefined" && localStorage && address) {
|
1125
|
-
localStorage.setItem(localKey, address);
|
1126
|
-
}
|
1127
|
-
return address;
|
1128
|
-
});
|
1129
|
-
var getBTCAccountInfo = (smartAccount, btcAddress, name, version) => __async(void 0, null, function* () {
|
1130
|
-
const btcPublicKey = yield smartAccount.provider.getPublicKey();
|
1131
|
-
const [accountInfo] = yield smartAccount.sendRpc({
|
1132
|
-
method: "particle_aa_getBTCAccount",
|
1133
|
-
params: [
|
1134
|
-
{
|
1135
|
-
name,
|
1136
|
-
version,
|
1137
|
-
btcPublicKey,
|
1138
|
-
btcAddress
|
1139
|
-
}
|
1140
|
-
]
|
1141
|
-
});
|
1142
|
-
const address = accountInfo.smartAccountAddress;
|
1143
|
-
const addresses = yield smartAccount.provider.request({ method: "eth_accounts" });
|
1144
|
-
const owner = addresses[0];
|
1145
|
-
const localKey = `particle_${name}_${version}_${owner}`;
|
1146
|
-
if (typeof window !== "undefined" && localStorage && address) {
|
1147
|
-
localStorage.setItem(localKey, address);
|
1148
|
-
}
|
1149
|
-
return accountInfo;
|
1150
|
-
});
|
1151
1112
|
|
1152
1113
|
// src/components/copyText/index.tsx
|
1153
1114
|
import { useState as useState4 } from "react";
|
@@ -1417,7 +1378,11 @@ var sign_module_default = classes8;
|
|
1417
1378
|
|
1418
1379
|
// src/components/signModal/index.tsx
|
1419
1380
|
import { Fragment as Fragment3, jsx as jsx8, jsxs as jsxs5 } from "react/jsx-runtime";
|
1420
|
-
var SignModal = ({
|
1381
|
+
var SignModal = ({
|
1382
|
+
open,
|
1383
|
+
onClose,
|
1384
|
+
onOpen
|
1385
|
+
}) => {
|
1421
1386
|
const [userOpBundle, setUserOpBundle] = useState5();
|
1422
1387
|
const [notRemindChecked, setNotRemindChecked] = useState5(false);
|
1423
1388
|
const [loading, setLoading] = useState5(false);
|
@@ -1497,10 +1462,13 @@ var SignModal = ({ open, onClose, onOpen }) => {
|
|
1497
1462
|
}), [userOpBundle, smartAccount, accountContract]);
|
1498
1463
|
useEffect4(() => {
|
1499
1464
|
if (userOpBundle && open) {
|
1465
|
+
console.log("deserializeUserOp start");
|
1500
1466
|
setDeserializeLoading(true);
|
1501
1467
|
deserializeUserOp().then((result) => {
|
1468
|
+
console.log("\u{1F680} ~ deserializeUserOp ~ result:", result);
|
1502
1469
|
setDeserializeResult(result);
|
1503
1470
|
}).catch((error) => {
|
1471
|
+
console.log("\u{1F680} ~ deserializeUserOp ~ error:", error);
|
1504
1472
|
eventUtils_default.emit("sendUserOpResult" /* sendUserOpResult */, {
|
1505
1473
|
error
|
1506
1474
|
});
|
@@ -1511,6 +1479,7 @@ var SignModal = ({ open, onClose, onOpen }) => {
|
|
1511
1479
|
useEffect4(() => {
|
1512
1480
|
if (open && publicClient && evmAccount && userOpBundle) {
|
1513
1481
|
publicClient.getBalance({ address: evmAccount }).then((result) => setNativeBalance(result)).catch((error) => {
|
1482
|
+
console.log("\u{1F680} ~ getBalance ~ error:", error);
|
1514
1483
|
eventUtils_default.emit("sendUserOpResult" /* sendUserOpResult */, {
|
1515
1484
|
error
|
1516
1485
|
});
|
@@ -1595,10 +1564,12 @@ var SignModal = ({ open, onClose, onOpen }) => {
|
|
1595
1564
|
var _a, _b;
|
1596
1565
|
return ((_b = (_a = item.estimatedChanges.natives) == null ? void 0 : _a[0]) == null ? void 0 : _b.nativeChange) && item.estimatedChanges.natives[0].nativeChange.startsWith("-");
|
1597
1566
|
}
|
1598
|
-
).map(
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1567
|
+
).map(
|
1568
|
+
(item) => {
|
1569
|
+
var _a, _b, _c, _d;
|
1570
|
+
return BigInt(((_d = (_c = (_b = (_a = item.estimatedChanges) == null ? void 0 : _a.natives) == null ? void 0 : _b[0]) == null ? void 0 : _c.nativeChange) == null ? void 0 : _d.replace("-", "")) || 0);
|
1571
|
+
}
|
1572
|
+
).reduce((accumulator, currentValue) => accumulator + currentValue, BigInt(0));
|
1602
1573
|
if (userOpBundle.userOp.paymasterAndData.length > 2) {
|
1603
1574
|
setDisabled(nativeBalance < nativeChange);
|
1604
1575
|
} else {
|
@@ -1620,37 +1591,52 @@ var SignModal = ({ open, onClose, onOpen }) => {
|
|
1620
1591
|
return JSON.stringify(obj, null, 2);
|
1621
1592
|
}
|
1622
1593
|
}, [requestArguments]);
|
1623
|
-
return /* @__PURE__ */ jsx8(
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1594
|
+
return /* @__PURE__ */ jsx8(
|
1595
|
+
modal_default,
|
1596
|
+
{
|
1597
|
+
open,
|
1598
|
+
onClose,
|
1599
|
+
isDismissable: false,
|
1600
|
+
contentClassName: sign_module_default.modalContent,
|
1601
|
+
children: /* @__PURE__ */ jsxs5(Fragment3, { children: [
|
1602
|
+
/* @__PURE__ */ jsx8("img", { className: sign_module_default.closeBtn, src: close_default, onClick: closeModal }),
|
1603
|
+
requestArguments && /* @__PURE__ */ jsx8("div", { className: sign_module_default.signTitle, children: requestArguments.method == "personal_sign" /* personalSign */ ? "Sign Message" : "Sign Typed Data" }),
|
1604
|
+
/* @__PURE__ */ jsxs5("div", { className: sign_module_default.chainInfo, children: [
|
1605
|
+
/* @__PURE__ */ jsx8("img", { src: chainInfo == null ? void 0 : chainInfo.icon }),
|
1606
|
+
chainInfo == null ? void 0 : chainInfo.fullname.replace("Mainnet", "")
|
1607
|
+
] }),
|
1608
|
+
/* @__PURE__ */ jsx8("div", { className: sign_module_default.addressContainer, children: /* @__PURE__ */ jsx8(CopyText, { value: evmAccount, style: { textDecorationLine: "none" }, children: /* @__PURE__ */ jsxs5("div", { className: sign_module_default.addressInfo, children: [
|
1609
|
+
shortString(evmAccount),
|
1610
|
+
/* @__PURE__ */ jsx8("img", { src: copy_default })
|
1611
|
+
] }) }) }),
|
1612
|
+
/* @__PURE__ */ jsxs5(
|
1613
|
+
"div",
|
1614
|
+
{
|
1615
|
+
className: sign_module_default.detailsContent + (deserializeResult || requestArguments ? ` ${sign_module_default.fill}` : ""),
|
1616
|
+
children: [
|
1617
|
+
deserializeResult && deserializeResult.map((details, index) => /* @__PURE__ */ jsx8(transactionDetails_default, { details }, `${details.type}-${index}`)),
|
1618
|
+
unsignedMessage && /* @__PURE__ */ jsx8("div", { className: sign_module_default.unsignedMessage, children: unsignedMessage })
|
1619
|
+
]
|
1620
|
+
}
|
1621
|
+
),
|
1622
|
+
gasFee && /* @__PURE__ */ jsx8("div", { className: sign_module_default.estimatedGas, children: `Estimated gas fee: ${formatEther(gasFee)} ${chainInfo == null ? void 0 : chainInfo.nativeCurrency.symbol}` }),
|
1623
|
+
/* @__PURE__ */ jsx8(
|
1624
|
+
button_default,
|
1625
|
+
{
|
1626
|
+
onClick: confirmTx,
|
1627
|
+
className: sign_module_default.signBtn,
|
1628
|
+
isLoading: loading || deserializeLoading,
|
1629
|
+
isDisabled: disabled,
|
1630
|
+
children: deserializeLoading ? "LOADING" : disabled ? "INSUFFICIENT FEE" : "CONFIRM"
|
1631
|
+
}
|
1632
|
+
),
|
1633
|
+
showNotRemind && /* @__PURE__ */ jsxs5("div", { className: sign_module_default.notRemind, onClick: toggleNotRemind, children: [
|
1634
|
+
/* @__PURE__ */ jsx8("img", { src: notRemindChecked ? check_box_default : check_box_blank_default }),
|
1635
|
+
"Do not remind me again"
|
1636
|
+
] })
|
1637
|
+
] })
|
1638
|
+
}
|
1639
|
+
);
|
1654
1640
|
};
|
1655
1641
|
var signModal_default = SignModal;
|
1656
1642
|
|
@@ -1683,6 +1669,7 @@ var AASignerProvider = class {
|
|
1683
1669
|
this.getPublicClient = () => {
|
1684
1670
|
var _a, _b;
|
1685
1671
|
const rpcUrl = ((_a = this == null ? void 0 : this.rpcUrls) == null ? void 0 : _a[this.chainId]) || ((_b = chains3.getEVMChainInfoById(this.chainId || 1)) == null ? void 0 : _b.rpcUrl);
|
1672
|
+
console.log("rpcUrl", rpcUrl);
|
1686
1673
|
return createPublicClient({
|
1687
1674
|
transport: http(rpcUrl)
|
1688
1675
|
});
|
@@ -1717,8 +1704,10 @@ var AASignerProvider = class {
|
|
1717
1704
|
return `0x${this.chainId.toString(16)}`;
|
1718
1705
|
} else if (arg.method === "personal_sign" /* personalSign */) {
|
1719
1706
|
let message = (_a = arg.params) == null ? void 0 : _a[0];
|
1707
|
+
console.log("personal_sign message:", message);
|
1720
1708
|
if (message.length !== 66) {
|
1721
1709
|
const hash = hashMessage({ raw: message });
|
1710
|
+
console.log("personal_sign hash:", hash);
|
1722
1711
|
message = hash;
|
1723
1712
|
}
|
1724
1713
|
const result = yield this.personalSign(message || "");
|
@@ -1726,15 +1715,19 @@ var AASignerProvider = class {
|
|
1726
1715
|
if (!convertResult) {
|
1727
1716
|
throw new Error("sign error");
|
1728
1717
|
}
|
1718
|
+
console.log(`personal_sign result(${convertResult.length}): `, convertResult);
|
1729
1719
|
return convertResult;
|
1730
1720
|
} else if (arg.method === "eth_signTypedData" || arg.method === "eth_signTypedData_v4") {
|
1731
1721
|
const typedData = (_b = arg.params) == null ? void 0 : _b[1];
|
1722
|
+
console.log("signTypedData typedData", typedData);
|
1732
1723
|
const hash = hashTypedData(typeof typedData === "string" ? JSON.parse(typedData) : typedData);
|
1724
|
+
console.log("signTypedData hash", hash);
|
1733
1725
|
const result = yield this.personalSign(hash || "");
|
1734
1726
|
const convertResult = convertSignature(result);
|
1735
1727
|
if (!convertResult) {
|
1736
1728
|
throw new Error("sign error");
|
1737
1729
|
}
|
1730
|
+
console.log(`eth_signTypedData result(${convertResult.length}): `, convertResult);
|
1738
1731
|
return convertResult;
|
1739
1732
|
} else if (arg.method === "wallet_switchEthereumChain") {
|
1740
1733
|
if (arg.params && arg.params instanceof Array && arg.params[0] && arg.params[0].chainId) {
|
@@ -1805,7 +1798,11 @@ var ConnectProvider = ({
|
|
1805
1798
|
isModalOpen: connectModalOpen,
|
1806
1799
|
openModal: openConnectModal
|
1807
1800
|
} = useModalStateValue_default();
|
1808
|
-
const {
|
1801
|
+
const {
|
1802
|
+
closeModal: closeSignModal,
|
1803
|
+
isModalOpen: signModalOpen,
|
1804
|
+
openModal: openSignModal
|
1805
|
+
} = useModalStateValue_default();
|
1809
1806
|
const [connectorId, setConnectorId] = useState7();
|
1810
1807
|
const [accounts, setAccounts] = useState7([]);
|
1811
1808
|
const [evmAccount, setEVMAccount] = useState7();
|
@@ -1814,12 +1811,12 @@ var ConnectProvider = ({
|
|
1814
1811
|
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"
|
1815
1812
|
});
|
1816
1813
|
const setAccountContract = useCallback7(
|
1817
|
-
(
|
1818
|
-
if (!checkBTCVersion(options.aaOptions.accountContracts,
|
1814
|
+
(config) => {
|
1815
|
+
if (!checkBTCVersion(options.aaOptions.accountContracts, config.name, config.version)) {
|
1819
1816
|
throw new Error("Invalid Account Contract");
|
1820
1817
|
}
|
1821
|
-
localStorage.setItem(SAContractKey, JSON.stringify(
|
1822
|
-
_setAccountContract(
|
1818
|
+
localStorage.setItem(SAContractKey, JSON.stringify(config));
|
1819
|
+
_setAccountContract(config);
|
1823
1820
|
},
|
1824
1821
|
[options.aaOptions.accountContracts, _setAccountContract]
|
1825
1822
|
);
|
@@ -1858,7 +1855,7 @@ var ConnectProvider = ({
|
|
1858
1855
|
}),
|
1859
1856
|
[connector]
|
1860
1857
|
);
|
1861
|
-
const
|
1858
|
+
const sendBitcoin2 = useCallback7(
|
1862
1859
|
(toAddress, satoshis, options2) => __async(void 0, null, function* () {
|
1863
1860
|
if (!connector) {
|
1864
1861
|
throw new Error("Wallet not connected!");
|
@@ -1890,7 +1887,12 @@ var ConnectProvider = ({
|
|
1890
1887
|
}
|
1891
1888
|
if (!window.__bitcoinSmartAccount || (window == null ? void 0 : window.__bitcoinSmartAccount) && ((window == null ? void 0 : window.__bitcoinSmartAccount.smartAccountContract.version) !== accountContract.version || (window == null ? void 0 : window.__bitcoinSmartAccount.smartAccountContract.name) !== accountContract.name)) {
|
1892
1889
|
const smartAccount2 = new SmartAccount(
|
1893
|
-
new AASignerProvider(
|
1890
|
+
new AASignerProvider(
|
1891
|
+
evmSupportChainIds,
|
1892
|
+
options.projectId,
|
1893
|
+
options.clientKey,
|
1894
|
+
options.rpcUrls
|
1895
|
+
),
|
1894
1896
|
options
|
1895
1897
|
);
|
1896
1898
|
smartAccount2.setSmartAccountContract(accountContract);
|
@@ -1900,30 +1902,6 @@ var ConnectProvider = ({
|
|
1900
1902
|
window.__bitcoinSmartAccount.provider.personalSign = signMessage;
|
1901
1903
|
return window.__bitcoinSmartAccount;
|
1902
1904
|
}, [options, evmSupportChainIds, getPublicKey, signMessage, accountContract]);
|
1903
|
-
useEffect5(() => {
|
1904
|
-
if (accounts.length > 0 && smartAccount) {
|
1905
|
-
getBTCAAAddress(smartAccount, accounts[0], accountContract.name, accountContract.version).then((res) => {
|
1906
|
-
setEVMAccount(res);
|
1907
|
-
}).catch((e) => {
|
1908
|
-
setEVMAccount(void 0);
|
1909
|
-
});
|
1910
|
-
} else {
|
1911
|
-
setEVMAccount(void 0);
|
1912
|
-
}
|
1913
|
-
}, [accountContract, accounts, smartAccount]);
|
1914
|
-
const getSmartAccountInfo = useCallback7(() => __async(void 0, null, function* () {
|
1915
|
-
if (accounts.length > 0 && smartAccount) {
|
1916
|
-
const accountInfo = yield getBTCAccountInfo(
|
1917
|
-
smartAccount,
|
1918
|
-
accounts[0],
|
1919
|
-
accountContract.name,
|
1920
|
-
accountContract.version
|
1921
|
-
);
|
1922
|
-
setEVMAccount(accountInfo.smartAccountAddress);
|
1923
|
-
return accountInfo;
|
1924
|
-
}
|
1925
|
-
return void 0;
|
1926
|
-
}), [accounts, smartAccount, accountContract, setEVMAccount]);
|
1927
1905
|
const requestAccount = useCallback7(
|
1928
1906
|
(connector2) => __async(void 0, null, function* () {
|
1929
1907
|
let accounts2 = yield connector2.getAccounts();
|
@@ -1934,20 +1912,18 @@ var ConnectProvider = ({
|
|
1934
1912
|
}),
|
1935
1913
|
[autoConnect]
|
1936
1914
|
);
|
1937
|
-
const requestDirectAccount = useCallback7(
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
|
1944
|
-
|
1945
|
-
}),
|
1946
|
-
[]
|
1947
|
-
);
|
1915
|
+
const requestDirectAccount = useCallback7((connector2) => __async(void 0, null, function* () {
|
1916
|
+
let accounts2 = yield connector2.getAccounts();
|
1917
|
+
if (accounts2.length === 0) {
|
1918
|
+
accounts2 = yield connector2.requestAccounts();
|
1919
|
+
}
|
1920
|
+
setAccounts(accounts2);
|
1921
|
+
return accounts2;
|
1922
|
+
}), []);
|
1948
1923
|
useEffect5(() => {
|
1949
1924
|
if (connector) {
|
1950
1925
|
requestAccount(connector).catch((e) => {
|
1926
|
+
console.error("get account error", e);
|
1951
1927
|
setAccounts([]);
|
1952
1928
|
});
|
1953
1929
|
} else {
|
@@ -1978,9 +1954,9 @@ var ConnectProvider = ({
|
|
1978
1954
|
setConnectorId(void 0);
|
1979
1955
|
}, [connector]);
|
1980
1956
|
useEffect5(() => {
|
1981
|
-
const
|
1982
|
-
if (
|
1983
|
-
_setAccountContract(
|
1957
|
+
const config = JSON.parse(localStorage.getItem(SAContractKey) || "{}");
|
1958
|
+
if (config.name && config.version && checkBTCVersion(options.aaOptions.accountContracts, config.name, config.version)) {
|
1959
|
+
_setAccountContract(config);
|
1984
1960
|
}
|
1985
1961
|
}, [options.aaOptions.accountContracts, _setAccountContract]);
|
1986
1962
|
useEffect5(() => {
|
@@ -2003,6 +1979,7 @@ var ConnectProvider = ({
|
|
2003
1979
|
})
|
2004
1980
|
})
|
2005
1981
|
);
|
1982
|
+
console.log("walletEntryPlugin init");
|
2006
1983
|
}
|
2007
1984
|
}, [options, evmSupportChainIds, accountContract]);
|
2008
1985
|
useEffect5(() => {
|
@@ -2011,6 +1988,7 @@ var ConnectProvider = ({
|
|
2011
1988
|
walletEntryPlugin.setWalletCore({
|
2012
1989
|
ethereum: smartAccount.provider
|
2013
1990
|
});
|
1991
|
+
console.log("walletEntryPlugin setWalletCore");
|
2014
1992
|
}
|
2015
1993
|
}, [smartAccount, options]);
|
2016
1994
|
useEffect5(() => {
|
@@ -2018,8 +1996,10 @@ var ConnectProvider = ({
|
|
2018
1996
|
if (((_a2 = options.walletOptions) == null ? void 0 : _a2.visible) !== false) {
|
2019
1997
|
if (evmAccount) {
|
2020
1998
|
walletEntryPlugin.walletEntryCreate();
|
1999
|
+
console.log("walletEntryPlugin walletEntryCreate");
|
2021
2000
|
} else {
|
2022
2001
|
walletEntryPlugin.walletEntryDestroy();
|
2002
|
+
console.log("walletEntryPlugin walletEntryDestroy");
|
2023
2003
|
}
|
2024
2004
|
}
|
2025
2005
|
}, [evmAccount, smartAccount, options]);
|
@@ -2071,10 +2051,9 @@ var ConnectProvider = ({
|
|
2071
2051
|
smartAccount,
|
2072
2052
|
getNetwork,
|
2073
2053
|
switchNetwork,
|
2074
|
-
sendBitcoin,
|
2054
|
+
sendBitcoin: sendBitcoin2,
|
2075
2055
|
accountContract,
|
2076
|
-
setAccountContract
|
2077
|
-
getSmartAccountInfo
|
2056
|
+
setAccountContract
|
2078
2057
|
},
|
2079
2058
|
children: [
|
2080
2059
|
children,
|
@@ -2089,8 +2068,8 @@ var useConnectProvider = () => {
|
|
2089
2068
|
return context;
|
2090
2069
|
};
|
2091
2070
|
|
2092
|
-
// src/
|
2093
|
-
import React, { useContext as useContext2, useEffect as
|
2071
|
+
// src/core/btcWalletSelectorContext.tsx
|
2072
|
+
import React, { useContext as useContext2, useEffect as useEffect6, useMemo as useMemo6, useRef, useState as useState8 } from "react";
|
2094
2073
|
|
2095
2074
|
// src/components/confirmBox/Modal.tsx
|
2096
2075
|
import { jsx as jsx10, jsxs as jsxs7 } from "react/jsx-runtime";
|
@@ -2144,20 +2123,13 @@ function ComfirmBox({ onClose, status = 1, fromChain = {
|
|
2144
2123
|
] });
|
2145
2124
|
}
|
2146
2125
|
|
2147
|
-
// src/
|
2148
|
-
import { useEffect as useEffect6 } from "react";
|
2149
|
-
function InitContextHook() {
|
2150
|
-
const btcContext = useBtcWalletSelector();
|
2151
|
-
useEffect6(() => {
|
2152
|
-
window.btcContext = btcContext;
|
2153
|
-
}, [btcContext]);
|
2154
|
-
return null;
|
2155
|
-
}
|
2156
|
-
|
2157
|
-
// src/components/btcWalletSelectorContext.tsx
|
2126
|
+
// src/core/btcWalletSelectorContext.tsx
|
2158
2127
|
import { jsx as jsx12, jsxs as jsxs9 } from "react/jsx-runtime";
|
2159
2128
|
var WalletSelectorContext = React.createContext(null);
|
2160
|
-
function BtcWalletSelectorContextProvider({
|
2129
|
+
function BtcWalletSelectorContextProvider({
|
2130
|
+
children,
|
2131
|
+
autoConnect = false
|
2132
|
+
}) {
|
2161
2133
|
const [isProcessing, setIsProcessing] = useState8(false);
|
2162
2134
|
const walletSelectorContextValue = useMemo6(() => {
|
2163
2135
|
const simpleFn = {};
|
@@ -2201,24 +2173,38 @@ function BtcWalletSelectorContextProvider({ children, autoConnect = false }) {
|
|
2201
2173
|
connectors: [new UnisatConnector()],
|
2202
2174
|
children: [
|
2203
2175
|
children,
|
2204
|
-
isProcessing && /* @__PURE__ */ jsx12(
|
2205
|
-
|
2206
|
-
|
2207
|
-
|
2176
|
+
isProcessing && /* @__PURE__ */ jsx12(
|
2177
|
+
ComfirmBox,
|
2178
|
+
{
|
2179
|
+
hash: "",
|
2180
|
+
status: 0,
|
2181
|
+
onClose: () => {
|
2182
|
+
setIsProcessing(false);
|
2183
|
+
}
|
2184
|
+
}
|
2185
|
+
),
|
2186
|
+
/* @__PURE__ */ jsx12(InitBtcWalletSelectorContext, {})
|
2208
2187
|
]
|
2209
2188
|
}
|
2210
2189
|
) });
|
2211
2190
|
}
|
2191
|
+
function InitBtcWalletSelectorContext() {
|
2192
|
+
const context = useBtcWalletSelector();
|
2193
|
+
useEffect6(() => {
|
2194
|
+
window.btcContext = context;
|
2195
|
+
}, [context]);
|
2196
|
+
return null;
|
2197
|
+
}
|
2212
2198
|
function useBtcWalletSelector() {
|
2213
2199
|
const { openConnectModal, openConnectModalAsync, disconnect, requestDirectAccount } = useConnectModal();
|
2214
|
-
const { accounts, sendBitcoin, getPublicKey, provider, signMessage, connector } = useBTCProvider();
|
2200
|
+
const { accounts, sendBitcoin: sendBitcoin2, getPublicKey, provider, signMessage, connector } = useBTCProvider();
|
2215
2201
|
const publicKey = useRef(null);
|
2216
2202
|
const signMessageFn = useRef(null);
|
2217
2203
|
const connectorRef = useRef(null);
|
2218
2204
|
const providerRef = useRef(null);
|
2219
2205
|
const [updater, setUpdater] = useState8(1);
|
2220
2206
|
const context = useContext2(WalletSelectorContext);
|
2221
|
-
|
2207
|
+
useEffect6(() => {
|
2222
2208
|
if (provider) {
|
2223
2209
|
getPublicKey().then((res) => {
|
2224
2210
|
publicKey.current = res;
|
@@ -2226,10 +2212,10 @@ function useBtcWalletSelector() {
|
|
2226
2212
|
providerRef.current = provider;
|
2227
2213
|
}
|
2228
2214
|
}, [provider, updater]);
|
2229
|
-
|
2215
|
+
useEffect6(() => {
|
2230
2216
|
signMessageFn.current = signMessage;
|
2231
2217
|
}, [signMessage]);
|
2232
|
-
|
2218
|
+
useEffect6(() => {
|
2233
2219
|
const fn = (account) => {
|
2234
2220
|
if (account) {
|
2235
2221
|
getPublicKey().then((res) => {
|
@@ -2263,7 +2249,7 @@ function useBtcWalletSelector() {
|
|
2263
2249
|
autoConnect: () => __async(this, null, function* () {
|
2264
2250
|
let times = 0;
|
2265
2251
|
while (!connectorRef.current) {
|
2266
|
-
yield
|
2252
|
+
yield delay(500);
|
2267
2253
|
if (times++ > 10) {
|
2268
2254
|
return null;
|
2269
2255
|
}
|
@@ -2280,7 +2266,7 @@ function useBtcWalletSelector() {
|
|
2280
2266
|
getPublicKey: () => __async(this, null, function* () {
|
2281
2267
|
let times = 0;
|
2282
2268
|
while (!publicKey.current) {
|
2283
|
-
yield
|
2269
|
+
yield delay(1e3);
|
2284
2270
|
if (times++ > 10) {
|
2285
2271
|
return null;
|
2286
2272
|
}
|
@@ -2296,23 +2282,19 @@ function useBtcWalletSelector() {
|
|
2296
2282
|
getBalance: () => __async(this, null, function* () {
|
2297
2283
|
let times = 0;
|
2298
2284
|
while (!providerRef.current) {
|
2299
|
-
yield
|
2285
|
+
yield delay(500);
|
2300
2286
|
if (times++ > 10) {
|
2301
2287
|
return null;
|
2302
2288
|
}
|
2303
2289
|
}
|
2304
2290
|
const { total } = yield providerRef.current.getBalance();
|
2305
2291
|
return total;
|
2306
|
-
})
|
2292
|
+
}),
|
2293
|
+
sendBitcoin: sendBitcoin2
|
2307
2294
|
};
|
2308
2295
|
}
|
2309
|
-
function sleep(time) {
|
2310
|
-
return new Promise(function(resolve) {
|
2311
|
-
setTimeout(resolve, time);
|
2312
|
-
});
|
2313
|
-
}
|
2314
2296
|
|
2315
|
-
// src/
|
2297
|
+
// src/core/setupBTCWallet.ts
|
2316
2298
|
import { providers, transactions } from "near-api-js";
|
2317
2299
|
import { actionCreators } from "@near-js/transactions";
|
2318
2300
|
import { PublicKey } from "near-api-js/lib/utils/key_pair";
|
@@ -2328,6 +2310,7 @@ var walletUrlConfig = {
|
|
2328
2310
|
mainnet: "https://wallet.satoshibridge.top"
|
2329
2311
|
};
|
2330
2312
|
function setupWalletButton(network, wallet, originalWallet) {
|
2313
|
+
console.log("setupWalletButton");
|
2331
2314
|
if (document.getElementById("satoshi-wallet-button")) {
|
2332
2315
|
return;
|
2333
2316
|
}
|
@@ -2404,7 +2387,8 @@ function createIframe({
|
|
2404
2387
|
zIndex: "100000",
|
2405
2388
|
boxShadow: "0 0 10px rgba(0, 0, 0, 0.1)",
|
2406
2389
|
borderRadius: "10px",
|
2407
|
-
display: "block"
|
2390
|
+
display: "block",
|
2391
|
+
border: "none"
|
2408
2392
|
}, iframeStyle));
|
2409
2393
|
document.body.appendChild(iframe);
|
2410
2394
|
return iframe;
|
@@ -2415,6 +2399,7 @@ function setupButtonClickHandler(button, iframe, wallet, originalWallet) {
|
|
2415
2399
|
const accountId = (_a = yield wallet == null ? void 0 : wallet.getAccounts()) == null ? void 0 : _a[0].accountId;
|
2416
2400
|
const originalAccountId = originalWallet.account;
|
2417
2401
|
const originalPublicKey = yield originalWallet.getPublicKey();
|
2402
|
+
console.log({ accountId, originalAccountId, originalPublicKey });
|
2418
2403
|
const iframeSrc = new URL(iframe.src);
|
2419
2404
|
iframeSrc.searchParams.set("origin", window.location.origin);
|
2420
2405
|
accountId && iframeSrc.searchParams.set("accountId", accountId);
|
@@ -2427,8 +2412,10 @@ function setupButtonClickHandler(button, iframe, wallet, originalWallet) {
|
|
2427
2412
|
return;
|
2428
2413
|
const { action, requestId, data } = event.data;
|
2429
2414
|
if (action === "signAndSendTransaction") {
|
2415
|
+
console.log("signAndSendTransaction message", event.data);
|
2430
2416
|
try {
|
2431
2417
|
const result = yield wallet.signAndSendTransaction(data);
|
2418
|
+
console.log("signAndSendTransaction result", result);
|
2432
2419
|
(_a2 = event.source) == null ? void 0 : _a2.postMessage(
|
2433
2420
|
{
|
2434
2421
|
requestId,
|
@@ -2438,6 +2425,7 @@ function setupButtonClickHandler(button, iframe, wallet, originalWallet) {
|
|
2438
2425
|
{ targetOrigin: event.origin }
|
2439
2426
|
);
|
2440
2427
|
} catch (error) {
|
2428
|
+
console.error("signAndSendTransaction error", error);
|
2441
2429
|
(_b = event.source) == null ? void 0 : _b.postMessage(
|
2442
2430
|
{
|
2443
2431
|
requestId,
|
@@ -2458,25 +2446,117 @@ function removeWalletButton() {
|
|
2458
2446
|
iframe == null ? void 0 : iframe.remove();
|
2459
2447
|
}
|
2460
2448
|
|
2461
|
-
// src/
|
2462
|
-
var
|
2463
|
-
var config = {
|
2449
|
+
// src/config.ts
|
2450
|
+
var walletConfig = {
|
2464
2451
|
dev: {
|
2465
|
-
base_url: "https://api.dev.satoshibridge.top
|
2452
|
+
base_url: "https://api.dev.satoshibridge.top",
|
2466
2453
|
token: "nbtc1-nsp.testnet",
|
2467
|
-
contractId: "dev1-nsp.testnet"
|
2454
|
+
contractId: "dev1-nsp.testnet",
|
2455
|
+
walletUrl: "https://wallet-dev.satoshibridge.top"
|
2468
2456
|
},
|
2469
2457
|
testnet: {
|
2470
|
-
base_url: "https://api.testnet.satoshibridge.top
|
2458
|
+
base_url: "https://api.testnet.satoshibridge.top",
|
2471
2459
|
token: "nbtc2-nsp.testnet",
|
2472
|
-
contractId: "dev2-nsp.testnet"
|
2460
|
+
contractId: "dev2-nsp.testnet",
|
2461
|
+
walletUrl: "https://wallet-test.satoshibridge.top"
|
2473
2462
|
},
|
2474
2463
|
mainnet: {
|
2475
|
-
base_url: "https://api.mainnet.satoshibridge.top
|
2464
|
+
base_url: "https://api.mainnet.satoshibridge.top",
|
2476
2465
|
token: "",
|
2477
|
-
contractId: ""
|
2466
|
+
contractId: "",
|
2467
|
+
walletUrl: "https://wallet.satoshibridge.top"
|
2478
2468
|
}
|
2479
2469
|
};
|
2470
|
+
var nearRpcUrls = {
|
2471
|
+
mainnet: [
|
2472
|
+
"https://near.lava.build",
|
2473
|
+
"https://rpc.mainnet.near.org",
|
2474
|
+
"https://free.rpc.fastnear.com",
|
2475
|
+
"https://near.drpc.org"
|
2476
|
+
],
|
2477
|
+
testnet: [
|
2478
|
+
"https://near-testnet.lava.build",
|
2479
|
+
"https://rpc.testnet.near.org",
|
2480
|
+
"https://near-testnet.drpc.org"
|
2481
|
+
]
|
2482
|
+
};
|
2483
|
+
var btcRpcUrls = {
|
2484
|
+
mainnet: "https://mempool.space/api",
|
2485
|
+
testnet: "https://mempool.space/testnet/api"
|
2486
|
+
};
|
2487
|
+
|
2488
|
+
// src/utils/request.ts
|
2489
|
+
var cache = /* @__PURE__ */ new Map();
|
2490
|
+
var defaultCacheTimeout = 3e3;
|
2491
|
+
function request(url, options) {
|
2492
|
+
return __async(this, null, function* () {
|
2493
|
+
var _a;
|
2494
|
+
const defaultHeaders = {
|
2495
|
+
"Content-Type": "application/json"
|
2496
|
+
};
|
2497
|
+
const cacheTimeout = (options == null ? void 0 : options.cacheTimeout) || defaultCacheTimeout;
|
2498
|
+
const headers = __spreadValues(__spreadValues({}, defaultHeaders), options == null ? void 0 : options.headers);
|
2499
|
+
let body = options == null ? void 0 : options.body;
|
2500
|
+
if (headers["Content-Type"] === "application/json" && body && typeof body !== "string") {
|
2501
|
+
body = JSON.stringify(body);
|
2502
|
+
}
|
2503
|
+
const method = (options == null ? void 0 : options.method) || "GET";
|
2504
|
+
const cacheKey = method.toUpperCase() === "GET" ? url : null;
|
2505
|
+
if (cacheKey) {
|
2506
|
+
const cached = cache.get(cacheKey);
|
2507
|
+
const isCacheValid = cached && Date.now() - cached.timestamp < cacheTimeout;
|
2508
|
+
if (isCacheValid) {
|
2509
|
+
return Promise.resolve(cached.data);
|
2510
|
+
}
|
2511
|
+
}
|
2512
|
+
const newOptions = __spreadProps(__spreadValues({}, options), {
|
2513
|
+
headers,
|
2514
|
+
body,
|
2515
|
+
method
|
2516
|
+
});
|
2517
|
+
const retryCount = (_a = options == null ? void 0 : options.retryCount) != null ? _a : 1;
|
2518
|
+
const controller = new AbortController();
|
2519
|
+
const timeout = (options == null ? void 0 : options.timeout) || 2e4;
|
2520
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
2521
|
+
try {
|
2522
|
+
const res = yield fetch(url, __spreadProps(__spreadValues({}, newOptions), { signal: controller.signal })).finally(
|
2523
|
+
() => clearTimeout(timeoutId)
|
2524
|
+
);
|
2525
|
+
if (!res.ok)
|
2526
|
+
throw new Error(res.statusText);
|
2527
|
+
const data = yield res.json();
|
2528
|
+
if (cacheKey) {
|
2529
|
+
cache.set(cacheKey, { timestamp: Date.now(), data });
|
2530
|
+
setTimeout(() => {
|
2531
|
+
cache.delete(cacheKey);
|
2532
|
+
}, cacheTimeout);
|
2533
|
+
}
|
2534
|
+
if ((options == null ? void 0 : options.shouldStopPolling) && options.shouldStopPolling(data)) {
|
2535
|
+
return data;
|
2536
|
+
}
|
2537
|
+
return data;
|
2538
|
+
} catch (err) {
|
2539
|
+
console.error(err);
|
2540
|
+
if (retryCount > 0) {
|
2541
|
+
console.log(`Retrying... attempts left: ${retryCount}`);
|
2542
|
+
return request(url, __spreadProps(__spreadValues({}, options), { retryCount: retryCount - 1 }));
|
2543
|
+
} else if ((options == null ? void 0 : options.pollingInterval) && (options == null ? void 0 : options.maxPollingAttempts)) {
|
2544
|
+
if (options.maxPollingAttempts > 0) {
|
2545
|
+
console.log(`Polling... attempts left: ${options.maxPollingAttempts}`);
|
2546
|
+
yield new Promise((resolve) => setTimeout(resolve, options.pollingInterval));
|
2547
|
+
return request(url, __spreadProps(__spreadValues({}, options), {
|
2548
|
+
maxPollingAttempts: options.maxPollingAttempts - 1,
|
2549
|
+
retryCount
|
2550
|
+
}));
|
2551
|
+
}
|
2552
|
+
}
|
2553
|
+
return Promise.reject(err);
|
2554
|
+
}
|
2555
|
+
});
|
2556
|
+
}
|
2557
|
+
|
2558
|
+
// src/core/setupBTCWallet.ts
|
2559
|
+
var { transfer, functionCall } = actionCreators;
|
2480
2560
|
var state = {
|
2481
2561
|
saveAccount(account) {
|
2482
2562
|
window.localStorage.setItem("btc-wallet-account", account);
|
@@ -2534,7 +2614,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2534
2614
|
signAndSendTransaction,
|
2535
2615
|
signAndSendTransactions
|
2536
2616
|
};
|
2537
|
-
const currentConfig = "isDev" in metadata && metadata.isDev ?
|
2617
|
+
const currentConfig = "isDev" in metadata && metadata.isDev ? walletConfig.dev : walletConfig[options.network.networkId];
|
2538
2618
|
const walletNetwork = "isDev" in metadata && metadata.isDev ? "dev" : options.network.networkId;
|
2539
2619
|
initWalletButton(walletNetwork, wallet);
|
2540
2620
|
if (!inter) {
|
@@ -2713,6 +2793,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2713
2793
|
txBytes,
|
2714
2794
|
(byte) => ("0" + (byte & 255).toString(16)).slice(-2)
|
2715
2795
|
).join("");
|
2796
|
+
console.log("txHex:", txHex);
|
2716
2797
|
const hash = bs58.encode(new Uint8Array(sha256.array(txBytes)));
|
2717
2798
|
return { txBytes, txHex, hash };
|
2718
2799
|
});
|
@@ -2749,6 +2830,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2749
2830
|
});
|
2750
2831
|
if (result.result_code === 0) {
|
2751
2832
|
const hash = newTransactions.map((t) => t.hash);
|
2833
|
+
console.log("txHash:", hash);
|
2752
2834
|
const result2 = yield pollTransactionStatuses(options.network.networkId, hash);
|
2753
2835
|
return result2;
|
2754
2836
|
} else {
|
@@ -2761,6 +2843,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2761
2843
|
const checkAndSetupWalletButton = () => {
|
2762
2844
|
const accountId = state.getAccount();
|
2763
2845
|
const btcContext = window.btcContext;
|
2846
|
+
console.log("checkAndSetupWalletButton:", {
|
2847
|
+
nearAccountId: accountId,
|
2848
|
+
btcAccountId: btcContext.account
|
2849
|
+
});
|
2764
2850
|
if (accountId && btcContext.account) {
|
2765
2851
|
setupWalletButton(network, wallet2, btcContext);
|
2766
2852
|
} else {
|
@@ -2777,29 +2863,16 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2777
2863
|
return wallet;
|
2778
2864
|
});
|
2779
2865
|
function getNonceFromApi(url, accountId) {
|
2780
|
-
return
|
2781
|
-
method: "GET",
|
2782
|
-
headers: {
|
2783
|
-
"Content-Type": "application/json"
|
2784
|
-
}
|
2785
|
-
}).then((res) => res.json());
|
2866
|
+
return request(`${url}/v1/nonce?csna=${accountId}`);
|
2786
2867
|
}
|
2787
2868
|
function getNearNonceFromApi(url, accountId) {
|
2788
|
-
return
|
2789
|
-
method: "GET",
|
2790
|
-
headers: {
|
2791
|
-
"Content-Type": "application/json"
|
2792
|
-
}
|
2793
|
-
}).then((res) => res.json());
|
2869
|
+
return request(`${url}/v1/nonceNear?csna=${accountId}`);
|
2794
2870
|
}
|
2795
2871
|
function uploadBTCTx(url, data) {
|
2796
|
-
return
|
2872
|
+
return request(`${url}/v1/receiveTransaction`, {
|
2797
2873
|
method: "POST",
|
2798
|
-
|
2799
|
-
|
2800
|
-
},
|
2801
|
-
body: JSON.stringify(data)
|
2802
|
-
}).then((res) => res.json());
|
2874
|
+
body: data
|
2875
|
+
});
|
2803
2876
|
}
|
2804
2877
|
function setupBTCWallet({
|
2805
2878
|
iconUrl = "https://assets.deltatrade.ai/assets/chain/btc.svg",
|
@@ -2836,23 +2909,10 @@ function toHex(originalString) {
|
|
2836
2909
|
hexString = hexString.replace(/(^0+)/g, "");
|
2837
2910
|
return hexString;
|
2838
2911
|
}
|
2839
|
-
var rcpUrls = {
|
2840
|
-
mainnet: [
|
2841
|
-
"https://near.lava.build",
|
2842
|
-
"https://rpc.mainnet.near.org",
|
2843
|
-
"https://free.rpc.fastnear.com",
|
2844
|
-
"https://near.drpc.org"
|
2845
|
-
],
|
2846
|
-
testnet: [
|
2847
|
-
"https://near-testnet.lava.build",
|
2848
|
-
"https://rpc.testnet.near.org",
|
2849
|
-
"https://near-testnet.drpc.org"
|
2850
|
-
]
|
2851
|
-
};
|
2852
2912
|
function pollTransactionStatuses(network, hashes) {
|
2853
2913
|
return __async(this, null, function* () {
|
2854
2914
|
const provider = new providers.FailoverRpcProvider(
|
2855
|
-
Object.values(
|
2915
|
+
Object.values(nearRpcUrls[network]).map(
|
2856
2916
|
(url) => new providers.JsonRpcProvider({ url })
|
2857
2917
|
)
|
2858
2918
|
);
|
@@ -2864,14 +2924,17 @@ function pollTransactionStatuses(network, hashes) {
|
|
2864
2924
|
try {
|
2865
2925
|
const result = yield provider.txStatus(hash, "unused", "FINAL");
|
2866
2926
|
if (result && result.status) {
|
2927
|
+
console.log(`Transaction ${hash} result:`, result);
|
2867
2928
|
return result;
|
2868
2929
|
}
|
2869
2930
|
} catch (error) {
|
2931
|
+
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
2870
2932
|
}
|
2871
2933
|
if (attempt === maxAttempts) {
|
2872
2934
|
throw new Error(`Transaction not found after max attempts: ${hash}`);
|
2873
2935
|
}
|
2874
2936
|
yield delay(1e4);
|
2937
|
+
console.log(`RPC request failed for ${hash}, retrying ${maxAttempts - attempt} more times`);
|
2875
2938
|
}
|
2876
2939
|
});
|
2877
2940
|
const results = yield Promise.all(hashes.map((hash) => pollStatus(hash)));
|
@@ -2879,9 +2942,119 @@ function pollTransactionStatuses(network, hashes) {
|
|
2879
2942
|
});
|
2880
2943
|
}
|
2881
2944
|
|
2945
|
+
// src/core/bridgeSupplyUtils.ts
|
2946
|
+
import { providers as providers2 } from "near-api-js";
|
2947
|
+
import Big from "big.js";
|
2948
|
+
function nearViewMethod(contractId, methodName, args, network) {
|
2949
|
+
return __async(this, null, function* () {
|
2950
|
+
const nearProvider = new providers2.FailoverRpcProvider(
|
2951
|
+
nearRpcUrls[network].map(
|
2952
|
+
(url) => new providers2.JsonRpcProvider({ url })
|
2953
|
+
)
|
2954
|
+
);
|
2955
|
+
const res = yield nearProvider.query({
|
2956
|
+
request_type: "call_function",
|
2957
|
+
account_id: contractId,
|
2958
|
+
method_name: methodName,
|
2959
|
+
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2960
|
+
finality: "optimistic"
|
2961
|
+
});
|
2962
|
+
return JSON.parse(Buffer.from(res.result).toString());
|
2963
|
+
});
|
2964
|
+
}
|
2965
|
+
function getDepositAddress(btcPublicKey, contractId, network) {
|
2966
|
+
return __async(this, null, function* () {
|
2967
|
+
const res = yield nearViewMethod(
|
2968
|
+
contractId,
|
2969
|
+
"get_user_dapp_deposit_address",
|
2970
|
+
{
|
2971
|
+
deposit_type: {
|
2972
|
+
BtcPublicKey: { btc_public_key: btcPublicKey, dapp_operation: "Burrowland->Supply" }
|
2973
|
+
}
|
2974
|
+
},
|
2975
|
+
network
|
2976
|
+
);
|
2977
|
+
return res;
|
2978
|
+
});
|
2979
|
+
}
|
2980
|
+
function getGasPrice(network) {
|
2981
|
+
return __async(this, null, function* () {
|
2982
|
+
const defaultFeeRate = 100;
|
2983
|
+
try {
|
2984
|
+
const btcRpcUrl = btcRpcUrls[network];
|
2985
|
+
const res = yield fetch(`${btcRpcUrl}/v1/fees/recommended`).then((res2) => res2.json());
|
2986
|
+
const feeRate = res.fastestFee;
|
2987
|
+
return feeRate || defaultFeeRate;
|
2988
|
+
} catch (error) {
|
2989
|
+
return defaultFeeRate;
|
2990
|
+
}
|
2991
|
+
});
|
2992
|
+
}
|
2993
|
+
function sendBitcoin(btcProvider, address, amount, feeRate) {
|
2994
|
+
return __async(this, null, function* () {
|
2995
|
+
const satoshis = new Big(amount).mul(__pow(10, 8)).toNumber();
|
2996
|
+
const txHash = yield btcProvider.sendBitcoin(address, satoshis, { feeRate });
|
2997
|
+
return txHash;
|
2998
|
+
});
|
2999
|
+
}
|
3000
|
+
function receiveDepositMsg(_0, _1) {
|
3001
|
+
return __async(this, arguments, function* (baseUrl, {
|
3002
|
+
btcPublicKey,
|
3003
|
+
txHash,
|
3004
|
+
depositType = 1
|
3005
|
+
}) {
|
3006
|
+
const res = yield request(`${baseUrl}/v1/receiveDepositMsg`, {
|
3007
|
+
method: "POST",
|
3008
|
+
body: { btcPublicKey, txHash, depositType }
|
3009
|
+
});
|
3010
|
+
return res;
|
3011
|
+
});
|
3012
|
+
}
|
3013
|
+
function checkTransactionStatus(baseUrl, txHash) {
|
3014
|
+
return __async(this, null, function* () {
|
3015
|
+
const res = yield request(`${baseUrl}/v1/bridgeFromTx?fromTxHash=${txHash}`, {
|
3016
|
+
timeout: 6e4,
|
3017
|
+
pollingInterval: 5e3,
|
3018
|
+
maxPollingAttempts: 10,
|
3019
|
+
shouldStopPolling: (res2) => res2.result_code === 0
|
3020
|
+
});
|
3021
|
+
return res;
|
3022
|
+
});
|
3023
|
+
}
|
3024
|
+
function executeBurrowSupply(_0) {
|
3025
|
+
return __async(this, arguments, function* ({
|
3026
|
+
amount,
|
3027
|
+
feeRate,
|
3028
|
+
environment = "mainnet"
|
3029
|
+
}) {
|
3030
|
+
try {
|
3031
|
+
if (typeof window === "undefined" || !window.btcContext) {
|
3032
|
+
throw new Error("BTC Provider is not initialized.");
|
3033
|
+
}
|
3034
|
+
const btcProvider = window.btcContext;
|
3035
|
+
const network = environment === "dev" ? "testnet" : environment;
|
3036
|
+
const config = walletConfig[environment];
|
3037
|
+
const btcPublicKey = yield btcProvider.getPublicKey();
|
3038
|
+
if (!btcPublicKey) {
|
3039
|
+
throw new Error("BTC Public Key is not available.");
|
3040
|
+
}
|
3041
|
+
const address = yield getDepositAddress(btcPublicKey, config.contractId, network);
|
3042
|
+
const _feeRate = feeRate || (yield getGasPrice(network));
|
3043
|
+
console.log("feeRate", _feeRate);
|
3044
|
+
const txHash = yield sendBitcoin(btcProvider, address, amount, _feeRate);
|
3045
|
+
const receiveDepositMsgRes = yield receiveDepositMsg(config.base_url, { btcPublicKey, txHash });
|
3046
|
+
console.log("receiveDepositMsg resp:", receiveDepositMsgRes);
|
3047
|
+
const checkTransactionStatusRes = yield checkTransactionStatus(config.base_url, txHash);
|
3048
|
+
console.log("checkTransactionStatus resp:", checkTransactionStatusRes);
|
3049
|
+
} catch (error) {
|
3050
|
+
console.error("Error executing Bridge+BurrowSupply:", error);
|
3051
|
+
}
|
3052
|
+
});
|
3053
|
+
}
|
3054
|
+
|
2882
3055
|
// src/index.ts
|
2883
3056
|
var getVersion = () => {
|
2884
|
-
return "0.2.
|
3057
|
+
return "0.2.7";
|
2885
3058
|
};
|
2886
3059
|
if (typeof window !== "undefined") {
|
2887
3060
|
window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();
|
@@ -2892,13 +3065,13 @@ export {
|
|
2892
3065
|
BtcWalletSelectorContextProvider,
|
2893
3066
|
BybitConnector,
|
2894
3067
|
ConnectProvider,
|
2895
|
-
InitContextHook,
|
2896
3068
|
InjectedConnector,
|
2897
3069
|
OKXConnector,
|
2898
3070
|
TokenPocketConnector,
|
2899
3071
|
UnisatConnector,
|
2900
3072
|
WizzConnector,
|
2901
3073
|
XverseConnector,
|
3074
|
+
executeBurrowSupply,
|
2902
3075
|
getVersion,
|
2903
3076
|
setupBTCWallet,
|
2904
3077
|
useAccountContract,
|