Skip to content

Commit

Permalink
chore: processUtxos to return utxo list
Browse files Browse the repository at this point in the history
  • Loading branch information
slavastartsev committed Jan 8, 2025
1 parent 4f6c956 commit b674a02
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
50 changes: 16 additions & 34 deletions sdk/src/wallet/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@ export const processUtxos = async (
cardinalOutputsSet: Set<string>,
esploraClient: EsploraClient,
ordinalsClient: OrdinalsClient
): Promise<boolean[]> => {
): Promise<UTXO[]> => {
const rootUtxoNodes = await createUtxoNodes(utxos, cardinalOutputsSet, ordinalsClient);

await processNodes(rootUtxoNodes, cardinalOutputsSet, esploraClient, ordinalsClient);

return rootUtxoNodes.map(checkUtxoNode);
const allowedList = rootUtxoNodes.map(checkUtxoNode);

return utxos.filter((_, index) => allowedList[index]);
};

type OutputNodeData = Pick<UTXO, 'txid' | 'vout'> & { cardinal: boolean; indexed: boolean };
Expand Down Expand Up @@ -204,13 +206,11 @@ export async function createBitcoinPsbt(

const cardinalOutputsSet = new Set(cardinalOutputs.map((output) => output.outpoint));

const allowedList = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);
const allowedUtxos = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);

// To construct the spending transaction and estimate the fee, we need the transactions for the UTXOs
const possibleInputs: Input[] = [];

await Promise.all(
utxos.map(async (utxo, index) => {
const possibleInputs = await Promise.all(
allowedUtxos.map(async (utxo) => {
const hex = await esploraClient.getTransactionHex(utxo.txid);
const transaction = Transaction.fromRaw(Buffer.from(hex, 'hex'), { allowUnknownOutputs: true });
const input = getInputFromUtxoAndTx(
Expand All @@ -221,11 +221,7 @@ export async function createBitcoinPsbt(
publicKey
);

// to support taproot addresses we want to exclude outputs which contain inscriptions
if (cardinalOutputsSet.has(OutPoint.toString(utxo))) return possibleInputs.push(input);

// allow to spend output if none of `vin` contains ordinals
if (allowedList[index]) return possibleInputs.push(input);
return input;
})
);

Expand Down Expand Up @@ -419,12 +415,10 @@ export async function estimateTxFee(

const cardinalOutputsSet = new Set(cardinalOutputs.map((output) => output.outpoint));

const allowedList = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);
const allowedUtxos = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);

const possibleInputs: Input[] = [];

await Promise.all(
utxos.map(async (utxo, index) => {
const possibleInputs = await Promise.all(
allowedUtxos.map(async (utxo) => {
const hex = await esploraClient.getTransactionHex(utxo.txid);
const transaction = Transaction.fromRaw(Buffer.from(hex, 'hex'), { allowUnknownOutputs: true });
const input = getInputFromUtxoAndTx(
Expand All @@ -435,11 +429,7 @@ export async function estimateTxFee(
publicKey
);

// to support taproot addresses we want to exclude outputs which contain inscriptions
if (cardinalOutputsSet.has(OutPoint.toString(utxo))) return possibleInputs.push(input);

// allow to spend output if none of `vin` contains ordinals
if (allowedList[index]) return possibleInputs.push(input);
return input;
})
);

Expand Down Expand Up @@ -533,20 +523,12 @@ export async function getBalance(address?: string) {

const cardinalOutputsSet = new Set(cardinalOutputs.map((output) => output.outpoint));

const allowedList = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);
const allowedUtxos = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);

const total = utxos.reduce((acc, utxo, index) => {
// there will be a match if output is confirmed and has no ordinals
if (cardinalOutputsSet.has(OutPoint.toString(utxo))) return acc + utxo.value;

// allow to spend output if none of `vin` contains ordinals
if (allowedList[index]) return acc + utxo.value;

return acc;
}, 0);
const total = allowedUtxos.reduce((acc, utxo) => acc + utxo.value, 0);

const confirmed = utxos.reduce((acc, utxo) => {
if (cardinalOutputsSet.has(OutPoint.toString(utxo)) && utxo.confirmed) {
const confirmed = allowedUtxos.reduce((acc, utxo) => {
if (utxo.confirmed) {
return acc + utxo.value;
}

Expand Down
6 changes: 3 additions & 3 deletions sdk/test/utxo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ describe('UTXO Tests', () => {
const balanceData = await getBalance(taprootAddress);

expect(balanceData.total).toEqual(BigInt(total));
expect(balanceData.confirmed).toBeLessThan(BigInt(confirmed));
expect(balanceData.confirmed).toEqual(BigInt(confirmed));
}
);

Expand Down Expand Up @@ -658,8 +658,8 @@ describe('UTXO Tests', () => {
return result;
});

const allowedList = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);
const allowedUtxos = await processUtxos(utxos, cardinalOutputsSet, esploraClient, ordinalsClient);

expect(allowedList).toEqual([true, true, false, false, false]);
expect(allowedUtxos).toEqual([utxos[0], utxos[1]]);
});
});

0 comments on commit b674a02

Please sign in to comment.