UNPKG

@imikailoby/sats

Version:

Tiny non-custodial Bitcoin SDK (TS) — keys, addresses, PSBT, provider chain

42 lines (41 loc) 1.59 kB
import { getJson, postText } from "../utils/http"; import { BroadcastError, ProviderError } from "../core/errors"; /** * Create Esplora-compatible provider bound to baseUrl. * Uses GET/POST helpers with timeouts and maps to domain errors. */ export function esplora(baseUrl, opts = {}) { const timeoutMs = opts.timeoutMs ?? 4000; const J = async (path) => getJson(baseUrl, path, { timeoutMs }); const T = async (path, body) => postText(baseUrl, path, body, { timeoutMs }); return { async getAddressUtxos(addr) { try { const utxos = await J(`/address/${addr}/utxo`); return utxos.map((u) => ({ txid: u.txid, vout: u.vout, value: u.value })); } catch (e) { throw new ProviderError(`esplora getAddressUtxos failed for ${baseUrl}`, e); } }, async getAddressBalance(addr) { try { const a = await J(`/address/${addr}`); const s = a.chain_stats || a.mempool_stats || {}; return { funded: s.funded_txo_sum || 0, spent: s.spent_txo_sum || 0 }; } catch (e) { throw new ProviderError(`esplora getAddressBalance failed for ${baseUrl}`, e); } }, async broadcast(rawHex) { try { const txid = await T(`/tx`, rawHex); return { txid }; } catch (e) { throw new BroadcastError(`esplora broadcast failed for ${baseUrl}`, e); } }, }; }