UNPKG

@bigmi/client

Version:

Reactive primitives for Bitcoin apps.

108 lines (107 loc) 4.05 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/dynamic.ts function dynamic(parameters) { const { chainId, shimDisconnect = true, wallet } = parameters; let accountChanged; return require_factories_createConnector.createConnector((config) => ({ id: wallet.connector.providerId, name: wallet.connector.name, type: dynamic.type, icon: wallet.connector._metadata?.icon, emitter: config.emitter, async isAuthorized() { return wallet.isAuthenticated; }, async request({ method, params }) { switch (method) { case "signPsbt": try { const { psbt, ...options } = params; const allowedSighash = options.inputsToSign.map((input) => Number(input.sigHash)); const psbtBase64 = (0, _bigmi_core.hexToBase64)(psbt); const response = await wallet.signPsbt({ allowedSighash, unsignedPsbtBase64: psbtBase64, signature: options.inputsToSign }); if (!response) throw new Error("Error signing the transaction"); const { signedPsbt } = response; return (0, _bigmi_core.base64ToHex)(signedPsbt); } catch (error) { throw new _bigmi_core.UserRejectedRequestError(error.message); } default: throw new _bigmi_core.MethodNotSupportedRpcError(); } }, async setup() {}, async getProvider() { const internalProvider = await this.getInternalProvider(); if (!internalProvider) throw new require_errors_connectors.ProviderNotFoundError(); return { request: this.request.bind(internalProvider) }; }, async connect() { if (!wallet.connector) throw new Error("DynamicWalletConnector not defined"); try { const accounts = await this.getAccounts(); const chainId = (0, _bigmi_core.getAddressChainId)(accounts[0].address); if (!accountChanged) { accountChanged = this.onAccountsChanged.bind(this); wallet.connector.addListener("accountChange", ({ accounts }) => accountChanged?.(accounts)); } if (shimDisconnect) await Promise.all([config.storage?.setItem(`${this.id}.connected`, true), config.storage?.removeItem(`${this.id}.disconnected`)]); return { accounts, chainId }; } catch (error) { throw new _bigmi_core.UserRejectedRequestError(error.message); } }, async disconnect() { const provider = wallet.connector; if (accountChanged) { provider.removeListener("accountChange", ({ accounts }) => accountChanged?.(accounts)); accountChanged = void 0; } if (shimDisconnect) await Promise.all([config.storage?.setItem(`${this.id}.disconnected`, true), config.storage?.removeItem(`${this.id}.connected`)]); }, async getAccounts() { const account = wallet.additionalAddresses.find((wallet) => wallet.type === "payment"); if (!account) throw new _bigmi_core.BaseError("Please connect a wallet with a segwit address"); return [{ address: account.address, publicKey: account.publicKey, addressType: _bigmi_core.AddressType.p2pkh, purpose: account.type }]; }, 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 getInternalProvider() { return wallet.connector; }, 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"); } })); } dynamic.type = "UTXO"; //#endregion exports.dynamic = dynamic; //# sourceMappingURL=dynamic.js.map