Skip to content

Commit

Permalink
fix(sdk): types
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao committed Oct 14, 2024
1 parent b13d56d commit ae6cc7f
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions sdk/src/wallet/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Transaction, Script, selectUTXO, TEST_NETWORK, NETWORK, p2wpkh, p2sh }
import { hex, base64 } from '@scure/base';
import { AddressType, getAddressInfo, Network } from 'bitcoin-address-validation';
import { EsploraClient, UTXO } from '../esplora';
import { SelectionStrategy } from '@scure/btc-signer/lib/utxo';

export type BitcoinNetworkName = Exclude<Network, 'regtest'>;

Expand Down Expand Up @@ -49,7 +50,7 @@ export async function createBitcoinPsbt(
publicKey?: string,
opReturnData?: string,
confirmationTarget: number = 3,
utxoSelectionStrategy: string = 'default'
utxoSelectionStrategy: SelectionStrategy = 'default'
): Promise<string> {
const addressInfo = getAddressInfo(fromAddress);

Expand Down Expand Up @@ -82,7 +83,13 @@ export async function createBitcoinPsbt(
confirmedUtxos.map(async (utxo) => {
const hex = await esploraClient.getTransactionHex(utxo.txid);
const transaction = Transaction.fromRaw(Buffer.from(hex, 'hex'), { allowUnknownOutputs: true });
const input = getInputFromUtxoAndTx(addressInfo.network, utxo, transaction, addressInfo.type, publicKey);
const input = getInputFromUtxoAndTx(
addressInfo.network as BitcoinNetworkName,
utxo,
transaction,
addressInfo.type,
publicKey
);
possibleInputs.push(input);
})
);
Expand Down Expand Up @@ -182,7 +189,6 @@ export function getInputFromUtxoAndTx(
return input;
}


/**
* Estimate the tx inclusion fee for a given address or public key with an optional OP_RETURN output.
*
Expand All @@ -192,8 +198,8 @@ export function getInputFromUtxoAndTx(
* @param opReturnData Optional OP_RETURN data to include in an output.
* @param confirmationTarget The number of blocks to include this tx (for fee estimation).
* @param utxoSelectionStrategy The strategy to use for selecting UTXOs. See https://github.com/paulmillr/scure-btc-signer/tree/main#utxo-selection for options.
* @returns {Promise<number>} The fee amount for estiamted transaction inclusion in satoshis.
*
* @returns {Promise<bigint>} The fee amount for estiamted transaction inclusion in satoshis.
*
* @dev Wtih no amount set, we estimate the fee for all UTXOs by trying to spend all inputs using strategy 'all'. If an amount is set, we use the 'default
* strategy to select the UTXOs.
*/
Expand All @@ -203,8 +209,8 @@ export async function estimateTxFee(
publicKey?: string,
opReturnData?: string,
confirmationTarget: number = 3,
utxoSelectionStrategy: string = 'default'
): Promise<number> {
utxoSelectionStrategy: SelectionStrategy = 'default'
): Promise<bigint> {
const addressInfo = getAddressInfo(fromAddress);

if (addressInfo.network === 'regtest') {
Expand Down Expand Up @@ -240,7 +246,13 @@ export async function estimateTxFee(
confirmedUtxos.map(async (utxo) => {
const hex = await esploraClient.getTransactionHex(utxo.txid);
const transaction = Transaction.fromRaw(Buffer.from(hex, 'hex'), { allowUnknownOutputs: true });
const input = getInputFromUtxoAndTx(addressInfo.network, utxo, transaction, addressInfo.type, publicKey);
const input = getInputFromUtxoAndTx(
addressInfo.network as BitcoinNetworkName,
utxo,
transaction,
addressInfo.type,
publicKey
);
possibleInputs.push(input);
})
);
Expand All @@ -249,7 +261,7 @@ export async function estimateTxFee(
const targetOutputs: Output[] = [
{
address: toAddress,
amount: BigInt(amount? amount : 0),
amount: BigInt(amount ? amount : 0),
},
];

Expand Down Expand Up @@ -291,12 +303,12 @@ export async function estimateTxFee(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
dust: BigInt(546) as any, // TODO: update scure-btc-signer
});

// Add the target outputs after the fact
if (amount === undefined) {
transaction.tx.addOutput(targetOutputs[0]);
transaction.tx.addOutput(targetOutputs[1]);
}

return transaction.fee;
}
}

0 comments on commit ae6cc7f

Please sign in to comment.