1
1
import * as bitcoin from 'bitcoinjs-lib' ;
2
2
import { Transaction , Script , selectUTXO , TEST_NETWORK , NETWORK , p2wpkh , p2sh , p2tr } from '@scure/btc-signer' ;
3
3
import { 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' ;
5
5
import { EsploraClient , UTXO } from '../esplora' ;
6
6
import { OrdinalsClient , OutPoint , OutputJson } from '../ordinal-api' ;
7
7
import { parseInscriptions } from '../inscription' ;
@@ -19,6 +19,10 @@ export const getBtcNetwork = (name: BitcoinNetworkName) => {
19
19
return bitcoinNetworks [ name ] ;
20
20
} ;
21
21
22
+ export const getAddressInfo = ( address : string , isSignet : boolean ) : AddressInfo => {
23
+ return getAddressInfoRaw ( address , isSignet ? { castTestnetTo : Network . signet } : undefined ) ;
24
+ } ;
25
+
22
26
type Output = { address : string ; amount : bigint } | { script : Uint8Array ; amount : bigint } ;
23
27
24
28
const isCardinalOutput = ( output : OutputJson ) =>
@@ -101,8 +105,8 @@ const getSafeUtxos = async (
101
105
return findSafeUtxos ( utxos , cardinalOutputsSet , esploraClient , ordinalsClient ) ;
102
106
} ;
103
107
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 ) ;
106
110
107
111
const esploraClient = new EsploraClient ( addressInfo . network ) ;
108
112
const ordinalsClient = new OrdinalsClient ( addressInfo . network ) ;
@@ -155,6 +159,7 @@ export interface Input {
155
159
* @param opReturnData Optional OP_RETURN data to include in an output.
156
160
* @param feeRate Optional fee rate in satoshis per byte.
157
161
* @param confirmationTarget The number of blocks to include this tx (for fee estimation).
162
+ * @param isSignet True if using Bitcoin Signet.
158
163
* @returns {Promise<string> } The Base64 encoded PSBT.
159
164
*
160
165
* @example
@@ -179,9 +184,10 @@ export async function createBitcoinPsbt(
179
184
publicKey ?: string ,
180
185
opReturnData ?: string ,
181
186
feeRate ?: number ,
182
- confirmationTarget : number = 3
187
+ confirmationTarget : number = 3 ,
188
+ isSignet : boolean = false
183
189
) : Promise < string > {
184
- const addressInfo = getAddressInfo ( fromAddress ) ;
190
+ const addressInfo = getAddressInfo ( fromAddress , isSignet ) ;
185
191
186
192
// TODO: possibly, allow other strategies to be passed to this function
187
193
const utxoSelectionStrategy : 'all' | 'default' = 'default' ;
@@ -206,7 +212,7 @@ export async function createBitcoinPsbt(
206
212
let possibleInputs : Input [ ] = [ ] ;
207
213
const esploraClient = new EsploraClient ( addressInfo . network ) ;
208
214
[ possibleInputs , feeRate ] = await Promise . all ( [
209
- collectPossibleInputs ( fromAddress , publicKey ) ,
215
+ collectPossibleInputs ( fromAddress , publicKey , isSignet ) ,
210
216
feeRate === undefined ? esploraClient . getFeeEstimate ( confirmationTarget ) : feeRate ,
211
217
] ) ;
212
218
@@ -321,6 +327,7 @@ export function getInputFromUtxoAndTx(
321
327
* @param opReturnData Optional OP_RETURN data to include in an output.
322
328
* @param feeRate Optional fee rate in satoshis per byte.
323
329
* @param confirmationTarget The number of blocks to include this tx (for fee estimation).
330
+ * @param isSignet True if using Bitcoin Signet
324
331
* @returns {Promise<bigint> } The fee amount for estimated transaction inclusion in satoshis.
325
332
*
326
333
* @example
@@ -355,9 +362,10 @@ export async function estimateTxFee(
355
362
publicKey ?: string ,
356
363
opReturnData ?: string ,
357
364
feeRate ?: number ,
358
- confirmationTarget : number = 3
365
+ confirmationTarget : number = 3 ,
366
+ isSignet : boolean = false
359
367
) : Promise < bigint > {
360
- const addressInfo = getAddressInfo ( fromAddress ) ;
368
+ const addressInfo = getAddressInfo ( fromAddress , isSignet ) ;
361
369
362
370
if ( addressInfo . network === 'regtest' ) {
363
371
throw new Error ( 'Bitcoin regtest not supported' ) ;
@@ -382,7 +390,7 @@ export async function estimateTxFee(
382
390
let possibleInputs : Input [ ] = [ ] ;
383
391
const esploraClient = new EsploraClient ( addressInfo . network ) ;
384
392
[ possibleInputs , feeRate ] = await Promise . all ( [
385
- collectPossibleInputs ( fromAddress , publicKey ) ,
393
+ collectPossibleInputs ( fromAddress , publicKey , isSignet ) ,
386
394
feeRate === undefined ? esploraClient . getFeeEstimate ( confirmationTarget ) : feeRate ,
387
395
] ) ;
388
396
@@ -445,6 +453,7 @@ export async function estimateTxFee(
445
453
* @typedef { {confirmed: BigInt, unconfirmed: BigInt, total: bigint} } Balance
446
454
*
447
455
* @param {string } [address] The Bitcoin address. If no address specified returning object will contain zeros.
456
+ * @param isSignet True if using Bitcoin Signet.
448
457
* @returns {Promise<Balance> } The balance object of provided address in satoshis.
449
458
*
450
459
* @example
@@ -457,12 +466,12 @@ export async function estimateTxFee(
457
466
*
458
467
* @dev UTXOs that contain inscriptions or runes will not be used to calculate balance.
459
468
*/
460
- export async function getBalance ( address ?: string ) {
469
+ export async function getBalance ( address ?: string , isSignet : boolean = false ) {
461
470
if ( ! address ) {
462
471
return { confirmed : BigInt ( 0 ) , unconfirmed : BigInt ( 0 ) , total : BigInt ( 0 ) } ;
463
472
}
464
473
465
- const addressInfo = getAddressInfo ( address ) ;
474
+ const addressInfo = getAddressInfo ( address , isSignet ) ;
466
475
467
476
const esploraClient = new EsploraClient ( addressInfo . network ) ;
468
477
const ordinalsClient = new OrdinalsClient ( addressInfo . network ) ;
0 commit comments