diff --git a/package.json b/package.json index cc048c4..b551228 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "dlc-btc-lib", - "version": "1.0.17", + "version": "1.0.18", "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", diff --git a/src/functions/bitcoin/bitcoin-functions.ts b/src/functions/bitcoin/bitcoin-functions.ts index 82ea715..6bce9ec 100644 --- a/src/functions/bitcoin/bitcoin-functions.ts +++ b/src/functions/bitcoin/bitcoin-functions.ts @@ -24,7 +24,12 @@ import { PaymentTypes, UTXO, } from '../../models/bitcoin-models.js'; -import { createRangeFromLength, isDefined, isUndefined } from '../../utilities/index.js'; +import { + compareUint8Arrays, + createRangeFromLength, + isDefined, + isUndefined, +} from '../../utilities/index.js'; const TAPROOT_UNSPENDABLE_KEY_HEX = '0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0'; @@ -428,51 +433,21 @@ export function getValueMatchingOutputFromTransaction( return valueMatchingTransactionOutput; } -export function validateScript(script: Uint8Array, outputScript: Uint8Array): boolean { - return ( - outputScript.length === script.length && - outputScript.every((value, index) => value === script[index]) - ); -} - 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 createRangeFromLength(transaction.inputsLength).flatMap(index => { + return compareUint8Arrays(transaction.getInput(index).witnessUtxo?.script, script) + ? [index] + : []; }); - return inputIndices; } export function finalizeUserInputs( transaction: Transaction, userPayment: P2TROut | P2Ret ): Transaction { - const userPaymentScript = userPayment.script; 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 === userPaymentScript.length && - inputScript.every((value, index) => value === userPaymentScript[index]) - ) { + if (compareUint8Arrays(transaction.getInput(index).witnessUtxo?.script, userPayment.script)) transaction.finalizeIdx(index); - } }); return transaction; diff --git a/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts b/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts index e401a7e..7641404 100644 --- a/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts +++ b/src/proof-of-reserve-handlers/proof-of-reserve-handler.ts @@ -6,7 +6,6 @@ import { deriveUnhardenedPublicKey, getUnspendableKeyCommittedToUUID, getValueMatchingOutputFromTransaction, - validateScript, } from '../functions/bitcoin/bitcoin-functions.js'; import { checkBitcoinTransactionConfirmations, @@ -14,6 +13,7 @@ import { fetchBitcoinTransaction, } from '../functions/bitcoin/bitcoin-request-functions.js'; import { RawVault } from '../models/ethereum-models.js'; +import { compareUint8Arrays } from '../utilities/index.js'; export class ProofOfReserveHandler { private bitcoinBlockchainAPI: string; @@ -65,7 +65,7 @@ export class ProofOfReserveHandler { this.bitcoinNetwork ); - return validateScript( + return compareUint8Arrays( taprootMultisigPayment.script, hex.decode(vaultTransactionOutput.scriptpubkey) ); diff --git a/src/utilities/index.ts b/src/utilities/index.ts index dbb158e..70fee41 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -45,6 +45,12 @@ export function isDefined(argument: T | undefined): argument is T { return !isUndefined(argument); } +export function compareUint8Arrays(a: Uint8Array | undefined, b: Uint8Array | undefined): boolean { + return isDefined(a) && isDefined(b) + ? a.length === b.length && a.every((value, index) => value === b[index]) + : false; +} + export async function delay(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); }