Skip to content

Commit

Permalink
feat: modify uint8array comparing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jul 10, 2024
1 parent ea53ec0 commit edccfdb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 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.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",
Expand Down
47 changes: 11 additions & 36 deletions src/functions/bitcoin/bitcoin-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/proof-of-reserve-handlers/proof-of-reserve-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
deriveUnhardenedPublicKey,
getUnspendableKeyCommittedToUUID,
getValueMatchingOutputFromTransaction,
validateScript,
} from '../functions/bitcoin/bitcoin-functions.js';
import {
checkBitcoinTransactionConfirmations,
fetchBitcoinBlockchainBlockHeight,
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;
Expand Down Expand Up @@ -65,7 +65,7 @@ export class ProofOfReserveHandler {
this.bitcoinNetwork
);

return validateScript(
return compareUint8Arrays(
taprootMultisigPayment.script,
hex.decode(vaultTransactionOutput.scriptpubkey)
);
Expand Down
6 changes: 6 additions & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export function isDefined<T>(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));
}
Expand Down

0 comments on commit edccfdb

Please sign in to comment.