Skip to content

Commit

Permalink
Added check for mismatch between utxo and deposit values
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 22, 2023
1 parent acc089c commit 8416229
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions typescript/src/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ export async function assembleDepositSweepTransactionBitcoinJsLib(
)
}
for (const utxo of utxos) {
// TODO: Validate that the utxo's value is the same as the value in deposit
transaction.addInput(
utxo.transactionHash.reverse().toBuffer(),
utxo.outputIndex
Expand Down Expand Up @@ -383,6 +382,7 @@ export async function assembleDepositSweepTransactionBitcoinJsLib(
transaction,
i,
utxoWithDeposit,
previousOutputValue,
keyPair
)
} else if (isP2WSH(previousOutputScript)) {
Expand All @@ -391,6 +391,7 @@ export async function assembleDepositSweepTransactionBitcoinJsLib(
transaction,
i,
utxoWithDeposit,
previousOutputValue,
keyPair
)
} else {
Expand Down Expand Up @@ -545,6 +546,7 @@ async function signMainUtxoInputBitcoinJsLib(
prevOutValue: number,
keyPair: Signer
) {
// TODO: Check that input the belongs to the wallet.
const sigHashType = Transaction.SIGHASH_ALL

if (isP2PKH(prevOutScript)) {
Expand Down Expand Up @@ -603,10 +605,11 @@ async function signP2SHDepositInputBitcoinJsLib(
transaction: Transaction,
inputIndex: number,
deposit: Deposit,
prevOutValue: number,
keyPair: Signer
) {
const { walletPublicKey, depositScript } =
await prepareInputSignDataBitcoinIsLib(deposit, keyPair)
await prepareInputSignDataBitcoinIsLib(deposit, prevOutValue, keyPair)

const sigHashType = Transaction.SIGHASH_ALL

Expand All @@ -631,10 +634,11 @@ async function signP2WSHDepositInputBitcoinJsLib(
transaction: Transaction,
inputIndex: number,
deposit: Deposit,
prevOutValue: number,
keyPair: Signer
) {
const { walletPublicKey, depositScript, previousOutputValue } =
await prepareInputSignDataBitcoinIsLib(deposit, keyPair)
await prepareInputSignDataBitcoinIsLib(deposit, prevOutValue, keyPair)

const sigHashType = Transaction.SIGHASH_ALL

Expand All @@ -657,12 +661,17 @@ async function signP2WSHDepositInputBitcoinJsLib(

async function prepareInputSignDataBitcoinIsLib(
deposit: Deposit,
prevOutValue: number,
ecPair: Signer
): Promise<{
walletPublicKey: string
depositScript: any
previousOutputValue: number
}> {
if (prevOutValue != deposit.amount.toNumber()) {
throw new Error("Mismatch between amount in deposit and deposit tx")
}

const walletPublicKey = ecPair.publicKey.toString("hex")

if (computeHash160(walletPublicKey) != deposit.walletPublicKeyHash) {
Expand Down

0 comments on commit 8416229

Please sign in to comment.