Skip to content

Commit

Permalink
feat: add function that gets input for given script, add function exp…
Browse files Browse the repository at this point in the history
…orts (#13)
  • Loading branch information
Polybius93 authored Jul 9, 2024
1 parent ae43ae0 commit ea53ec0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "dlc-btc-lib",
"version": "1.0.16",
"version": "1.0.17",
"description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
20 changes: 20 additions & 0 deletions src/functions/bitcoin/bitcoin-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,26 @@ export function validateScript(script: Uint8Array, outputScript: Uint8Array): bo
);
}

export function getInputIndicesByScript(script: Uint8Array, transaction: Transaction): number[] {
const inputIndices: number[] = [];

createRangeFromLength(transaction.inputsLength).forEach(index => {
const inputScript = transaction.getInput(index).witnessUtxo?.script;

if (!inputScript) {
throw new Error('Could not get Input Script');
}

if (
inputScript.length === script.length &&
inputScript.every((value, index) => value === script[index])
) {
inputIndices.push(index);
}
});
return inputIndices;
}

export function finalizeUserInputs(
transaction: Transaction,
userPayment: P2TROut | P2Ret
Expand Down
9 changes: 8 additions & 1 deletion src/functions/bitcoin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { finalizeUserInputs, getFeeAmount } from '../bitcoin/bitcoin-functions.js';
import {
finalizeUserInputs,
getFeeAmount,
getFeeRecipientAddressFromPublicKey,
getInputIndicesByScript,
} from '../bitcoin/bitcoin-functions.js';
import {
broadcastTransaction,
fetchBitcoinBlockchainBlockHeight,
Expand All @@ -21,4 +26,6 @@ export {
finalizeUserInputs,
getFeeAmount,
getBalance,
getFeeRecipientAddressFromPublicKey,
getInputIndicesByScript,
};

0 comments on commit ea53ec0

Please sign in to comment.