11import * as bitcoin from 'bitcoinjs-lib' ;
22import { Transaction , Script , selectUTXO , TEST_NETWORK , NETWORK , p2wpkh , p2sh , p2tr } from '@scure/btc-signer' ;
33import { hex , base64 } from '@scure/base' ;
4- import { AddressType , getAddressInfo , Network } from 'bitcoin-address-validation' ;
4+ import { AddressType , getAddressInfo as getAddressInfoRaw , Network , AddressInfo } from 'bitcoin-address-validation' ;
55import { EsploraClient , UTXO } from '../esplora' ;
66import { OrdinalsClient , OutPoint , OutputJson } from '../ordinal-api' ;
77import { parseInscriptions } from '../inscription' ;
@@ -19,6 +19,10 @@ export const getBtcNetwork = (name: BitcoinNetworkName) => {
1919 return bitcoinNetworks [ name ] ;
2020} ;
2121
22+ export const getAddressInfo = ( address : string , isSignet : boolean ) : AddressInfo => {
23+ return getAddressInfoRaw ( address , isSignet ? { castTestnetTo : Network . signet } : undefined ) ;
24+ } ;
25+
2226type Output = { address : string ; amount : bigint } | { script : Uint8Array ; amount : bigint } ;
2327
2428const isCardinalOutput = ( output : OutputJson ) =>
@@ -101,8 +105,8 @@ const getSafeUtxos = async (
101105 return findSafeUtxos ( utxos , cardinalOutputsSet , esploraClient , ordinalsClient ) ;
102106} ;
103107
104- const collectPossibleInputs = async ( fromAddress : string , publicKey : string ) => {
105- const addressInfo = getAddressInfo ( fromAddress ) ;
108+ const collectPossibleInputs = async ( fromAddress : string , publicKey : string , isSignet : boolean ) => {
109+ const addressInfo = getAddressInfo ( fromAddress , isSignet ) ;
106110
107111 const esploraClient = new EsploraClient ( addressInfo . network ) ;
108112 const ordinalsClient = new OrdinalsClient ( addressInfo . network ) ;
@@ -155,6 +159,7 @@ export interface Input {
155159 * @param opReturnData Optional OP_RETURN data to include in an output.
156160 * @param feeRate Optional fee rate in satoshis per byte.
157161 * @param confirmationTarget The number of blocks to include this tx (for fee estimation).
162+ * @param isSignet True if using Bitcoin Signet.
158163 * @returns {Promise<string> } The Base64 encoded PSBT.
159164 *
160165 * @example
@@ -179,9 +184,10 @@ export async function createBitcoinPsbt(
179184 publicKey ?: string ,
180185 opReturnData ?: string ,
181186 feeRate ?: number ,
182- confirmationTarget : number = 3
187+ confirmationTarget : number = 3 ,
188+ isSignet : boolean = false
183189) : Promise < string > {
184- const addressInfo = getAddressInfo ( fromAddress ) ;
190+ const addressInfo = getAddressInfo ( fromAddress , isSignet ) ;
185191
186192 // TODO: possibly, allow other strategies to be passed to this function
187193 const utxoSelectionStrategy : 'all' | 'default' = 'default' ;
@@ -206,7 +212,7 @@ export async function createBitcoinPsbt(
206212 let possibleInputs : Input [ ] = [ ] ;
207213 const esploraClient = new EsploraClient ( addressInfo . network ) ;
208214 [ possibleInputs , feeRate ] = await Promise . all ( [
209- collectPossibleInputs ( fromAddress , publicKey ) ,
215+ collectPossibleInputs ( fromAddress , publicKey , isSignet ) ,
210216 feeRate === undefined ? esploraClient . getFeeEstimate ( confirmationTarget ) : feeRate ,
211217 ] ) ;
212218
@@ -321,6 +327,7 @@ export function getInputFromUtxoAndTx(
321327 * @param opReturnData Optional OP_RETURN data to include in an output.
322328 * @param feeRate Optional fee rate in satoshis per byte.
323329 * @param confirmationTarget The number of blocks to include this tx (for fee estimation).
330+ * @param isSignet True if using Bitcoin Signet
324331 * @returns {Promise<bigint> } The fee amount for estimated transaction inclusion in satoshis.
325332 *
326333 * @example
@@ -355,9 +362,10 @@ export async function estimateTxFee(
355362 publicKey ?: string ,
356363 opReturnData ?: string ,
357364 feeRate ?: number ,
358- confirmationTarget : number = 3
365+ confirmationTarget : number = 3 ,
366+ isSignet : boolean = false
359367) : Promise < bigint > {
360- const addressInfo = getAddressInfo ( fromAddress ) ;
368+ const addressInfo = getAddressInfo ( fromAddress , isSignet ) ;
361369
362370 if ( addressInfo . network === 'regtest' ) {
363371 throw new Error ( 'Bitcoin regtest not supported' ) ;
@@ -382,7 +390,7 @@ export async function estimateTxFee(
382390 let possibleInputs : Input [ ] = [ ] ;
383391 const esploraClient = new EsploraClient ( addressInfo . network ) ;
384392 [ possibleInputs , feeRate ] = await Promise . all ( [
385- collectPossibleInputs ( fromAddress , publicKey ) ,
393+ collectPossibleInputs ( fromAddress , publicKey , isSignet ) ,
386394 feeRate === undefined ? esploraClient . getFeeEstimate ( confirmationTarget ) : feeRate ,
387395 ] ) ;
388396
@@ -445,6 +453,7 @@ export async function estimateTxFee(
445453 * @typedef { {confirmed: BigInt, unconfirmed: BigInt, total: bigint} } Balance
446454 *
447455 * @param {string } [address] The Bitcoin address. If no address specified returning object will contain zeros.
456+ * @param isSignet True if using Bitcoin Signet.
448457 * @returns {Promise<Balance> } The balance object of provided address in satoshis.
449458 *
450459 * @example
@@ -457,12 +466,12 @@ export async function estimateTxFee(
457466 *
458467 * @dev UTXOs that contain inscriptions or runes will not be used to calculate balance.
459468 */
460- export async function getBalance ( address ?: string ) {
469+ export async function getBalance ( address ?: string , isSignet : boolean = false ) {
461470 if ( ! address ) {
462471 return { confirmed : BigInt ( 0 ) , unconfirmed : BigInt ( 0 ) , total : BigInt ( 0 ) } ;
463472 }
464473
465- const addressInfo = getAddressInfo ( address ) ;
474+ const addressInfo = getAddressInfo ( address , isSignet ) ;
466475
467476 const esploraClient = new EsploraClient ( addressInfo . network ) ;
468477 const ordinalsClient = new OrdinalsClient ( addressInfo . network ) ;
0 commit comments