Skip to content

Commit

Permalink
minimize btc interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gbarkhatov committed Dec 15, 2024
1 parent f354953 commit dea30bf
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 325 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-birds-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@babylonlabs-io/bbn-wallet-connect": patch
---

Minimize BTC interface
58 changes: 18 additions & 40 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,19 @@ export interface IBTCProvider extends IProvider {
getNetwork(): Promise<Network>;

/**
* Signs a message using BIP-322 simple.
* Signs a message using the specified signing method.
* @param message - The message to sign.
* @param type - The signing method to use.
* @returns A promise that resolves to the signed message.
*/
signMessageBIP322(message: string): Promise<string>;

signMessage(message: string, type: "ecdsa" | "bip322-simple"): Promise<string>;

/**
* Retrieves the inscriptions for the connected wallet.
* @returns A promise that resolves to an array of inscriptions.
*/
getInscriptions(): Promise<InscriptionIdentifier[]>;

/**
* Registers an event listener for the specified event.
* At the moment, only the "accountChanged" event is supported.
Expand All @@ -190,51 +195,24 @@ export interface IBTCProvider extends IProvider {
*/
on(eventName: string, callBack: () => void): void;

off(eventName: string, callBack: () => void): void;

/**
* Gets the balance for the connected wallet address.
* By default, this method will return the mempool balance if not implemented by the child class.
* @returns A promise that resolves to the balance of the wallet.
*/
getBalance(): Promise<number>;

/**
* Retrieves the network fees.
* @returns A promise that resolves to the network fees.
*/
getNetworkFees(): Promise<Fees>;

/**
* Pushes a transaction to the network.
* @param txHex - The hexadecimal representation of the transaction.
* @returns A promise that resolves to a string representing the transaction ID.
*/
pushTx(txHex: string): Promise<string>;

/**
* Retrieves the unspent transaction outputs (UTXOs) for a given address and amount.
*
* If the amount is provided, it will return UTXOs that cover the specified amount.
* If the amount is not provided, it will return all available UTXOs for the address.
*
* @param address - The address to retrieve UTXOs for.
* @param amount - Optional amount of funds required.
* @returns A promise that resolves to an array of UTXOs.
* Unregisters an event listener for the specified event.
* @param eventName - The name of the event to listen for.
* @param callBack - The callback function to be executed when the event occurs.
*/
getUtxos(address: string, amount?: number): Promise<UTXO[]>;
off(eventName: string, callBack: () => void): void;

/**
* Retrieves the tip height of the BTC chain.
* @returns A promise that resolves to the block height.
* Gets the name of the wallet provider.
* @returns A promise that resolves to the name of the wallet provider.
*/
getBTCTipHeight(): Promise<number>;
getWalletProviderName(): Promise<string>;

/**
* Retrieves the inscriptions for the connected wallet.
* @returns A promise that resolves to an array of inscriptions.
* Gets the icon URL of the wallet provider.
* @returns A promise that resolves to the icon URL of the wallet provider.
*/
getInscriptions(): Promise<InscriptionIdentifier[]>;
getWalletProviderIcon(): Promise<string>;
}

export interface IBBNProvider extends IProvider {
Expand Down
66 changes: 20 additions & 46 deletions src/core/wallets/btc/BTCProvider.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import type { BTCConfig, Fees, IBTCProvider, InscriptionIdentifier, Network, UTXO } from "../../types";
import { createMempoolAPI, MempoolApi } from "../../utils/mempool";
import type { BTCConfig, IBTCProvider, InscriptionIdentifier, Network } from "../../types";

/**
* Abstract class representing a wallet provider.
* Provides methods for connecting to a wallet, retrieving wallet information, signing transactions, and more.
*/
export abstract class BTCProvider implements IBTCProvider {
protected mempool: MempoolApi;
constructor(protected config: BTCConfig) {}

constructor(protected config: BTCConfig) {
this.mempool = createMempoolAPI(this.config.mempoolApiUrl);
}
/**
* Connects to the wallet and returns the instance of the wallet provider.
* Currently only supports "native segwit" and "taproot" address types.
Expand Down Expand Up @@ -52,14 +48,19 @@ export abstract class BTCProvider implements IBTCProvider {
abstract getNetwork(): Promise<Network>;

/**
* Signs a message using BIP-322 simple.
* Signs a message using the specified signing method.
* @param message - The message to sign.
* @param type - The signing method to use.
* @returns A promise that resolves to the signed message.
*/
abstract signMessageBIP322(message: string): Promise<string>;

abstract signMessage(message: string, type: "ecdsa" | "bip322-simple"): Promise<string>;

/**
* Retrieves the inscriptions for the connected wallet.
* @returns A promise that resolves to an array of inscriptions.
*/
abstract getInscriptions(): Promise<InscriptionIdentifier[]>;

/**
* Registers an event listener for the specified event.
* At the moment, only the "accountChanged" event is supported.
Expand All @@ -68,49 +69,22 @@ export abstract class BTCProvider implements IBTCProvider {
*/
abstract on(eventName: string, callBack: () => void): void;

abstract off(eventName: string, callBack: () => void): void;

/**
* Gets the balance for the connected wallet address.
* By default, this method will return the mempool balance if not implemented by the child class.
* @returns A promise that resolves to the balance of the wallet.
*/
abstract getBalance(): Promise<number>;

/**
* Retrieves the network fees.
* @returns A promise that resolves to the network fees.
*/
abstract getNetworkFees(): Promise<Fees>;

/**
* Pushes a transaction to the network.
* @param txHex - The hexadecimal representation of the transaction.
* @returns A promise that resolves to a string representing the transaction ID.
*/
abstract pushTx(txHex: string): Promise<string>;

/**
* Retrieves the unspent transaction outputs (UTXOs) for a given address and amount.
*
* If the amount is provided, it will return UTXOs that cover the specified amount.
* If the amount is not provided, it will return all available UTXOs for the address.
*
* @param address - The address to retrieve UTXOs for.
* @param amount - Optional amount of funds required.
* @returns A promise that resolves to an array of UTXOs.
* Unregisters an event listener for the specified event.
* @param eventName - The name of the event to listen for.
* @param callBack - The callback function to be executed when the event occurs.
*/
abstract getUtxos(address: string, amount?: number): Promise<UTXO[]>;
abstract off(eventName: string, callBack: () => void): void;

/**
* Retrieves the tip height of the BTC chain.
* @returns A promise that resolves to the block height.
* Gets the name of the wallet provider.
* @returns A promise that resolves to the name of the wallet provider.
*/
abstract getBTCTipHeight(): Promise<number>;
abstract getWalletProviderName(): Promise<string>;

/**
* Retrieves the inscriptions for the connected wallet.
* @returns A promise that resolves to an array of inscriptions.
* Gets the icon URL of the wallet provider.
* @returns A promise that resolves to the icon URL of the wallet provider.
*/
abstract getInscriptions(): Promise<InscriptionIdentifier[]>;
abstract getWalletProviderIcon(): Promise<string>;
}
53 changes: 17 additions & 36 deletions src/core/wallets/btc/bitget/provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Psbt } from "bitcoinjs-lib";

import type { BTCConfig, Fees, InscriptionIdentifier, UTXO, WalletInfo } from "@/core/types";
import type { BTCConfig, InscriptionIdentifier, WalletInfo } from "@/core/types";
import { Network } from "@/core/types";
import { validateAddress } from "@/core/utils/wallet";
import { BTCProvider } from "@/core/wallets/btc/BTCProvider";

import logo from "./logo.svg";

const INTERNAL_NETWORK_NAMES = {
[Network.MAINNET]: "livenet",
[Network.TESTNET]: "testnet",
Expand Down Expand Up @@ -55,10 +57,6 @@ export class BitgetProvider extends BTCProvider {
}
};

getWalletProviderName = async (): Promise<string> => {
return "Bitget";
};

getAddress = async (): Promise<string> => {
const accounts = (await this.provider.getAccounts()) || [];
if (!accounts?.[0]) {
Expand Down Expand Up @@ -140,16 +138,6 @@ export class BitgetProvider extends BTCProvider {
}
};

signMessageBIP322 = async (message: string): Promise<string> => {
if (!this.walletInfo) throw new Error("Bitget Wallet not connected");
return await this.provider.signMessage(message, "bip322-simple");
};

signMessage = async (message: string, type: "ecdsa" | "bip322-simple" = "ecdsa"): Promise<string> => {
if (!this.walletInfo) throw new Error("Bitget Wallet not connected");
return await this.provider.signMessage(message, type);
};

getNetwork = async (): Promise<Network> => {
const internalNetwork = await this.provider.getNetwork();

Expand All @@ -162,6 +150,16 @@ export class BitgetProvider extends BTCProvider {
throw new Error("Unsupported network");
};

signMessage = async (message: string, type: "ecdsa" | "bip322-simple" = "ecdsa"): Promise<string> => {
if (!this.walletInfo) throw new Error("Bitget Wallet not connected");

return await this.provider.signMessage(message, type);
};

getInscriptions = async (): Promise<InscriptionIdentifier[]> => {
throw new Error("Method not implemented.");
};

on = (eventName: string, callBack: () => void) => {
if (!this.walletInfo) throw new Error("Bitget Wallet not connected");

Expand All @@ -182,28 +180,11 @@ export class BitgetProvider extends BTCProvider {
return this.provider.off(eventName, callBack);
};

// Mempool calls
getBalance = async (): Promise<number> => {
return await this.mempool.getAddressBalance(await this.getAddress());
};

getNetworkFees = async (): Promise<Fees> => {
return await this.mempool.getNetworkFees();
};

pushTx = async (txHex: string): Promise<string> => {
return await this.mempool.pushTx(txHex);
};

getUtxos = async (address: string, amount: number): Promise<UTXO[]> => {
return await this.mempool.getFundingUTXOs(address, amount);
};

getBTCTipHeight = async (): Promise<number> => {
return await this.mempool.getTipHeight();
getWalletProviderName = async (): Promise<string> => {
return "Bitget";
};

getInscriptions = async (): Promise<InscriptionIdentifier[]> => {
throw new Error("Method not implemented.");
getWalletProviderIcon = async (): Promise<string> => {
return logo;
};
}
59 changes: 20 additions & 39 deletions src/core/wallets/btc/cactus/provider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { BTCConfig, Fees, InscriptionIdentifier, UTXO, WalletInfo } from "@/core/types";
import type { BTCConfig, InscriptionIdentifier, WalletInfo } from "@/core/types";
import { Network } from "@/core/types";
import { validateAddress } from "@/core/utils/wallet";
import { BTCProvider } from "@/core/wallets/btc/BTCProvider";

import logo from "./logo.svg";

const INTERNAL_NETWORK_NAMES = {
[Network.MAINNET]: "mainnet",
[Network.TESTNET]: "testnet",
Expand Down Expand Up @@ -55,17 +57,13 @@ export class CactusLinkProvider extends BTCProvider {
}
};

getWalletProviderName = async (): Promise<string> => {
return "Cactus Link";
};

async getAddress(): Promise<string> {
getAddress = async (): Promise<string> => {
const accounts = (await this.provider.getAccounts()) || [];
if (!accounts?.[0]) {
throw new Error("Cactus Link Wallet not connected");
}
return accounts[0];
}
};

getPublicKeyHex = async (): Promise<string> => {
const publicKey = await this.provider.getPublicKey();
Expand Down Expand Up @@ -97,16 +95,6 @@ export class CactusLinkProvider extends BTCProvider {
return await this.provider.signPsbts(psbtsHexes, options);
};

signMessageBIP322 = async (message: string): Promise<string> => {
if (!this.walletInfo) throw new Error("Cactus Link Wallet not connected");
return await this.provider.signMessage(message, "bip322-simple");
};

signMessage = async (message: string, type: "ecdsa" | "bip322-simple" = "ecdsa"): Promise<string> => {
if (!this.walletInfo) throw new Error("Cactus Link Wallet not connected");
return await this.provider.signMessage(message, type);
};

getNetwork = async (): Promise<Network> => {
const internalNetwork = await this.provider.getNetwork();

Expand All @@ -119,6 +107,17 @@ export class CactusLinkProvider extends BTCProvider {
throw new Error("Unsupported network");
};

signMessage = async (message: string, type: "ecdsa" | "bip322-simple" = "ecdsa"): Promise<string> => {
if (!this.walletInfo) throw new Error("Cactus Link Wallet not connected");

return await this.provider.signMessage(message, type);
};

getInscriptions = async (): Promise<InscriptionIdentifier[]> => {
// Temporary solution to ignore inscriptions filtering for Cactus Link Wallet
return Promise.resolve([]);
};

on = (eventName: string, callBack: () => void) => {
if (!this.walletInfo) throw new Error("Cactus Link Wallet not connected");

Expand All @@ -139,29 +138,11 @@ export class CactusLinkProvider extends BTCProvider {
return this.provider.off(eventName, callBack);
};

// Mempool calls
getBalance = async (): Promise<number> => {
return await this.mempool.getAddressBalance(await this.getAddress());
};

getNetworkFees = async (): Promise<Fees> => {
return await this.mempool.getNetworkFees();
};

pushTx = async (txHex: string): Promise<string> => {
return await this.mempool.pushTx(txHex);
};

getUtxos = async (address: string, amount: number): Promise<UTXO[]> => {
return await this.mempool.getFundingUTXOs(address, amount);
};

getBTCTipHeight = async (): Promise<number> => {
return await this.mempool.getTipHeight();
getWalletProviderName = async (): Promise<string> => {
return "Cactus Link";
};

getInscriptions = async (): Promise<InscriptionIdentifier[]> => {
// Temporary solution to ignore inscriptions filtering for Cactus Link Wallet
return Promise.resolve([]);
getWalletProviderIcon = async (): Promise<string> => {
return logo;
};
}
Loading

0 comments on commit dea30bf

Please sign in to comment.