Skip to content

Commit

Permalink
Merge pull request #77 from bob-collective/feat/electrs-utxo
Browse files Browse the repository at this point in the history
feat: add electrs method to fetch utxos
  • Loading branch information
gregdhill authored Nov 1, 2023
2 parents eea6eec + 71cef82 commit b0379a3
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions sdk/src/electrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export interface MerkleProof {
pos: number,
}

/**
* @ignore
*/
export interface UTXO {
txid: string
vout: number,
value: number,
}

export interface ElectrsClient {
/**
* Get the block hash of the Bitcoin block at a specific height.
Expand Down Expand Up @@ -122,6 +131,14 @@ export interface ElectrsClient {
*/
getFeeEstimate(confirmationTarget: number): Promise<number>;

/**
* Get the Unspent Transaction Outputs (UTXOs) for an address.
*
* @param {string} address - The Bitcoin address to checl.
* @returns {Promise<Array<UTXO>>} A promise that resolves to an array of UTXOs.
*/
getAddressUtxos(address: string): Promise<Array<UTXO>>;

/**
* Broadcast a raw transaction to the network.
*
Expand Down Expand Up @@ -231,6 +248,32 @@ export class DefaultElectrsClient implements ElectrsClient {
return response[confirmationTarget];
}

/**
* @ignore
*/
async getAddressUtxos(address: string, confirmed?: boolean): Promise<Array<UTXO>> {
const response = await this.getJson<Array<{
txid: string,
vout: number,
status: {
confirmed: boolean,
block_height: number,
block_hash: string,
block_time: number
},
value: number,
}>>(`${this.basePath}/address/${address}/utxo`);
return response
.filter(utxo => (typeof confirmed !== "undefined") ? confirmed === utxo.status.confirmed : true)
.map<UTXO>(utxo => {
return {
txid: utxo.txid,
vout: utxo.vout,
value: utxo.value
}
});
}

/**
* @ignore
*/
Expand Down

0 comments on commit b0379a3

Please sign in to comment.