Skip to content

Commit

Permalink
feat: add electrs method to fetch utxos
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Nov 1, 2023
1 parent 15277fc commit 71cef82
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 71cef82

Please sign in to comment.