Skip to content

Commit

Permalink
chore: merge main updates, cleanup test constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jul 12, 2024
2 parents 103fa66 + 04bdc9d commit 3f83934
Show file tree
Hide file tree
Showing 13 changed files with 1,475 additions and 375 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
56 changes: 10 additions & 46 deletions src/functions/bitcoin/bitcoin-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 @@ -416,19 +421,6 @@ export function getInputByPaymentTypeArray(
});
}

export function getValueMatchingOutputFromTransaction(
bitcoinTransaction: BitcoinTransaction,
bitcoinValue: number
): BitcoinTransactionVectorOutput {
const valueMatchingTransactionOutput = bitcoinTransaction.vout.find(
output => output.value === bitcoinValue
);
if (!valueMatchingTransactionOutput) {
throw new Error('Could not find Value matching Input in Transaction');
}
return valueMatchingTransactionOutput;
}

export function getScriptMatchingOutputFromTransaction(
bitcoinTransaction: BitcoinTransaction,
script: Uint8Array
Expand All @@ -446,46 +438,18 @@ export function validateScript(script: Uint8Array, outputScript: Uint8Array): bo
}

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

createRangeFromLength(transaction.inputsLength).forEach(index => {
return createRangeFromLength(transaction.inputsLength).flatMap(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 inputScript && compareUint8Arrays(inputScript, script) ? [index] : [];
});
return inputIndices;
}

export function finalizeUserInputs(
transaction: Transaction,
userPayment: P2TROut | P2Ret
): Transaction {
const userPaymentScript = userPayment.script;
export function finalizeUserInputs(transaction: Transaction, userPayment: P2TROut | P2Ret): void {
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 (inputScript && compareUint8Arrays(inputScript, userPayment.script))
transaction.finalizeIdx(index);
}
});

return transaction;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export function isDefined<T>(argument: T | undefined): argument is T {
return !isUndefined(argument);
}

export function compareUint8Arrays(a: Uint8Array, b: Uint8Array): boolean {
return a.length === b.length && a.every((value, index) => value === b[index]);
}

export async function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Expand Down
15 changes: 15 additions & 0 deletions tests/mocks/api.test.constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
export const TEST_REGTEST_BITCOIN_BLOCKCHAIN_API = 'https://devnet.dlc.link/electrs';
export const TEST_TESTNET_BITCOIN_BLOCKCHAIN_API = 'https://testnet.dlc.link/electrs';

export const TEST_BITCOIN_BLOCKCHAIN_FEE_RECOMMENDATION_API =
'https://devnet.dlc.link/electrs/fee-estimates';

export const TEST_REGTEST_ATTESTOR_APIS = [
'https://devnet.dlc.link/attestor-1',
'https://devnet.dlc.link/attestor-2',
'https://devnet.dlc.link/attestor-3',
];

export const TEST_ETHEREUM_NODE_API = 'https://sepolia-rollup.arbitrum.io/rpc';
export const TEST_ETHEREUM_READ_ONLY_NODE_API = 'https://sepolia-rollup.arbitrum.io/rpc';

export const TEST_ETHEREUM_GITHUB_DEPLOYMENT_PLAN_ROOT_URL =
'https://raw.githubusercontent.com/DLC-link/dlc-solidity';
Loading

0 comments on commit 3f83934

Please sign in to comment.