UNPKG

@btc-vision/walletconnect

Version:

The OP_NET Wallet Connect library helps your dApp connect to any compatible wallet.

179 lines (178 loc) 6.35 kB
import { networks } from '@btc-vision/bitcoin'; import { UnisatChainType, UnisatSigner, } from '@btc-vision/transaction'; import { JSONRpcProvider } from 'opnet'; const notInstalledError = 'UNISAT is not installed'; class UnisatWallet { walletBase; accountsChangedHookWrapper; chainChangedHookWrapper; disconnectHookWrapper; _isConnected = false; isInstalled() { if (typeof window === 'undefined') { return false; } this.walletBase = window.unisat; return !!this.walletBase; } isConnected() { return !!this.walletBase && this._isConnected; } async canAutoConnect() { const accounts = (await this.walletBase?.getAccounts()) || []; return accounts.length > 0; } getWalletInstance() { return (this._isConnected && this.walletBase) || null; } async getProvider() { if (!this._isConnected || !this.walletBase) return null; const chain = await this.walletBase.getChain(); switch (chain.enum) { case UnisatChainType.BITCOIN_MAINNET: return new JSONRpcProvider({ url: 'https://mainnet.opnet.org', network: networks.bitcoin, }); case UnisatChainType.OPNET_TESTNET: return new JSONRpcProvider({ url: 'https://testnet.opnet.org', network: networks.opnetTestnet, }); case UnisatChainType.BITCOIN_REGTEST: return new JSONRpcProvider({ url: 'https://regtest.opnet.org', network: networks.regtest, }); default: return null; } } async getSigner() { const signer = new UnisatSigner(); await signer.init(); return signer; } getChainId() { throw new Error('Method not implemented.'); } async connect() { if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } return this.walletBase.requestAccounts().then((accounts) => { this._isConnected = accounts.length > 0; return accounts; }); } async disconnect() { if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } return this._isConnected ? await this.walletBase.disconnect().then(() => { this._isConnected = false; }) : undefined; } getPublicKey() { if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } return this.walletBase.getPublicKey(); } async getNetwork() { if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } const chainInfo = await this.walletBase.getChain(); if (!chainInfo) { throw new Error('Failed to retrieve chain information'); } return chainInfo.enum; } setAccountsChangedHook(fn) { console.log('Setting account changed hook for Unisat'); if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } this.accountsChangedHookWrapper = (accounts) => { console.log('Unisat Account Changed Hook', accounts, accounts.length); if (accounts.length > 0) { fn(accounts); } else { console.log('Unisat Account Changed Hook --> Disconnect', accounts.length, !!this.disconnectHookWrapper); this._isConnected = false; this.disconnectHookWrapper?.(); } }; this.walletBase.on('accountsChanged', this.accountsChangedHookWrapper); } removeAccountsChangedHook() { if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } if (this.accountsChangedHookWrapper) { console.log('Removing account changed hook for Unisat'); this.walletBase.removeListener('accountsChanged', this.accountsChangedHookWrapper); this.accountsChangedHookWrapper = undefined; } } setDisconnectHook(fn) { console.log('Setting disconnect hook for Unisat'); if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } this.disconnectHookWrapper = () => { console.log('Unisat Disconnecting Hook'); fn(); }; this.walletBase.on('disconnect', this.disconnectHookWrapper); } removeDisconnectHook() { if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } if (this.disconnectHookWrapper) { console.log('Removing disconnect hook for Unisat'); this.walletBase.removeListener('disconnect', this.disconnectHookWrapper); this.disconnectHookWrapper = undefined; } } setChainChangedHook(fn) { console.log('Setting chain changed hook for Unisat'); if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } this.chainChangedHookWrapper = (chainInfo) => { console.log('Unisat ChainChanged Hook', chainInfo); fn(chainInfo.enum); }; this.walletBase.on('chainChanged', this.chainChangedHookWrapper); } removeChainChangedHook() { if (!this.isInstalled() || !this.walletBase) { throw new Error(notInstalledError); } if (this.chainChangedHookWrapper) { console.log('Removing chain changed hook for Unisat'); this.walletBase.removeListener('chainChanged', this.chainChangedHookWrapper); this.chainChangedHookWrapper = undefined; } } getMLDSAPublicKey() { return Promise.resolve(null); } getHashedMLDSAKey() { return Promise.resolve(null); } signMLDSAMessage(_message) { return Promise.resolve(null); } verifyMLDSASignature(_message, _signature) { return Promise.resolve(false); } } export default UnisatWallet;