@btc-vision/walletconnect
Version:
The OP_NET Wallet Connect library helps your dApp connect to any compatible wallet.
153 lines (152 loc) • 6.33 kB
JavaScript
import { networks } from '@btc-vision/bitcoin';
import { Address, UnisatChainType, UnisatSigner } from '@btc-vision/transaction';
import { JSONRpcProvider } from 'opnet';
export var SupportedWallets;
(function (SupportedWallets) {
SupportedWallets["OP_WALLET"] = "op_wallet";
SupportedWallets["UNISAT"] = "unisat";
})(SupportedWallets || (SupportedWallets = {}));
export class WalletConnection {
constructor() {
this.signer = null;
this.walletType = null;
this.walletWindowInstance = null;
}
async connect(walletType) {
if (this.walletType === walletType)
return;
if (this.walletType)
this.disconnect();
switch (walletType) {
case SupportedWallets.OP_WALLET: {
if (window.opnet) {
try {
await window.opnet.requestAccounts();
this.walletType = walletType;
this.walletWindowInstance = window.opnet;
return;
}
catch (error) {
if (!error)
return;
if (error instanceof Error)
throw new Error(error.message);
if (typeof error === 'object' &&
'message' in error &&
typeof error.message === 'string') {
throw new Error(error.message);
}
console.warn(error);
throw new Error(`Unknown error, check console`);
}
}
else {
throw new Error('OP_WALLET not found');
}
}
case SupportedWallets.UNISAT: {
if (window.unisat) {
try {
await window.unisat.requestAccounts();
const signer = new UnisatSigner();
await signer.init();
this.signer = signer;
this.walletType = walletType;
this.walletWindowInstance = window.unisat;
return;
}
catch (error) {
if (!error)
return;
if (error instanceof Error)
throw new Error(error.message);
if (typeof error === 'object' &&
'message' in error &&
typeof error.message === 'string')
throw new Error(error.message);
console.error(error);
throw new Error(`Unknown error, check console`);
}
}
else {
throw new Error('Unisat not found');
}
}
default:
throw new Error('Unsupported wallet');
}
}
disconnect() {
if (!this.walletWindowInstance)
return;
if (this.walletType === SupportedWallets.OP_WALLET ||
this.walletType === SupportedWallets.UNISAT)
this.walletWindowInstance.disconnect();
this.signer = null;
this.walletType = null;
this.walletWindowInstance = null;
}
async getAddress() {
if (!this.walletWindowInstance)
throw new Error('Wallet not connected');
if (this.walletType === SupportedWallets.OP_WALLET ||
this.walletType === SupportedWallets.UNISAT) {
const publicKey = await this.walletWindowInstance.getPublicKey();
return Address.fromString(publicKey);
}
throw new Error('Unsupported wallet');
}
async getAddressTyped() {
if (!this.walletWindowInstance)
throw new Error('Wallet not connected');
if (this.walletType === SupportedWallets.OP_WALLET ||
this.walletType === SupportedWallets.UNISAT) {
const accounts = await this.walletWindowInstance.getAccounts();
return accounts[0];
}
throw new Error('Unsupported wallet');
}
async getNetwork() {
if (!this.walletWindowInstance)
throw new Error('Wallet not connected');
if (this.walletType === SupportedWallets.OP_WALLET ||
this.walletType === SupportedWallets.UNISAT) {
const chain = await this.walletWindowInstance.getChain();
switch (chain.enum) {
case UnisatChainType.BITCOIN_MAINNET:
return networks.bitcoin;
case UnisatChainType.BITCOIN_TESTNET:
return networks.testnet;
case UnisatChainType.BITCOIN_REGTEST:
return networks.regtest;
case UnisatChainType.FRACTAL_BITCOIN_MAINNET:
return networks.bitcoin;
case UnisatChainType.FRACTAL_BITCOIN_TESTNET:
return networks.testnet;
default:
throw new Error('Unsupported network');
}
}
throw new Error('Unsupported wallet');
}
async getProvider() {
if (!this.walletWindowInstance)
throw new Error('Wallet not connected');
if (this.walletType === SupportedWallets.OP_WALLET ||
this.walletType === SupportedWallets.UNISAT) {
const chain = await this.walletWindowInstance.getChain();
switch (chain.enum) {
case UnisatChainType.BITCOIN_MAINNET:
return new JSONRpcProvider('https://api.opnet.org', networks.bitcoin);
case UnisatChainType.BITCOIN_TESTNET:
return new JSONRpcProvider('https://testnet.opnet.org', networks.testnet);
case UnisatChainType.BITCOIN_REGTEST:
return new JSONRpcProvider('https://regtest.opnet.org', networks.regtest);
default:
throw new Error('Unsupported network');
}
}
throw new Error('Unsupported wallet');
}
}
export default WalletConnection;