UNPKG

@bigmi/client

Version:

Reactive primitives for Bitcoin apps.

120 lines (119 loc) 4.22 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_errors_connectors = require("../errors/connectors.js"); const require_factories_createConnector = require("../factories/createConnector.js"); let _bigmi_core = require("@bigmi/core"); //#region src/connectors/reown.ts function reown(parameters) { const { chainId, shimDisconnect = true, connector, address, walletInfo } = parameters; const id = walletInfo?.name?.toLowerCase().replace(/\s+/g, "-") || "reown"; const name = walletInfo?.name || "Reown Bitcoin Wallet"; const imageUrl = walletInfo?.icon; return require_factories_createConnector.createConnector((config) => ({ id, name, type: reown.type, icon: imageUrl, async setup() {}, async getInternalProvider() { return { id, name, imageUrl, connector, address }; }, async getProvider() { const provider = await this.getInternalProvider(); if (!provider) return; return { request: this.request.bind(provider) }; }, async request({ method, params }) { switch (method) { case "signPsbt": { const { psbt, ...options } = params; const signInputs = options.inputsToSign.flatMap(({ address, signingIndexes, sigHash }) => signingIndexes.map((index) => ({ address, index, sighashTypes: sigHash !== void 0 ? [sigHash] : [] }))); const psbtBase64 = (0, _bigmi_core.hexToBase64)(psbt); return (0, _bigmi_core.base64ToHex)((await this.connector.signPSBT({ psbt: psbtBase64, signInputs, broadcast: false })).psbt); } default: throw new _bigmi_core.MethodNotSupportedRpcError(method); } }, async connect() { const provider = await this.getInternalProvider(); if (!provider) throw new _bigmi_core.ProviderNotFoundError(); try { await provider.connector.connect(); const accounts = await this.getAccounts(); const detectedChainId = (0, _bigmi_core.getAddressChainId)(accounts[0].address); if (shimDisconnect) await Promise.all([config.storage?.setItem(`${this.id}.connected`, true), config.storage?.removeItem(`${this.id}.disconnected`)]); return { accounts, chainId: chainId ?? detectedChainId }; } catch (error) { throw new _bigmi_core.UserRejectedRequestError(error.message); } }, async disconnect() { if (shimDisconnect) await Promise.all([config.storage?.setItem(`${this.id}.disconnected`, true), config.storage?.removeItem(`${this.id}.connected`)]); }, async getAccounts() { const provider = await this.getInternalProvider(); if (!provider) throw new _bigmi_core.ProviderNotFoundError(); const createAccount = (addr, publicKey = "") => { const { type, purpose } = (0, _bigmi_core.getAddressInfo)(addr); return { address: addr, addressType: type, publicKey, purpose }; }; try { const paymentAccount = (await (0, _bigmi_core.withTimeout)(() => provider.connector.getAccountAddresses(), { timeout: 1e3 })).find((acc) => acc.purpose === "payment"); if (paymentAccount) return [createAccount(paymentAccount.address, paymentAccount.publicKey ?? "")]; } catch {} if (provider.address) return [createAccount(provider.address)]; throw new _bigmi_core.ProviderNotFoundError(); }, async getChainId() { if (chainId) return chainId; const accounts = await this.getAccounts(); if (accounts.length === 0) throw new require_errors_connectors.ConnectorChainIdDetectionError({ connector: this.name }); return (0, _bigmi_core.getAddressChainId)(accounts[0].address); }, async isAuthorized() { try { return shimDisconnect && Boolean(await config.storage?.getItem(`${this.id}.connected`)); } catch { return false; } }, async onAccountsChanged(accounts) { if (accounts.length === 0) this.onDisconnect(); else { const newAccounts = await this.getAccounts(); config.emitter.emit("change", { accounts: newAccounts }); } }, onChainChanged(chainId) { config.emitter.emit("change", { chainId }); }, async onDisconnect(_error) { config.emitter.emit("disconnect"); } })); } reown.type = "UTXO"; //#endregion exports.reown = reown; //# sourceMappingURL=reown.js.map