diff --git a/typescript/src/lib/bitcoin/script.ts b/typescript/src/lib/bitcoin/script.ts index a66c39305..a514bddbe 100644 --- a/typescript/src/lib/bitcoin/script.ts +++ b/typescript/src/lib/bitcoin/script.ts @@ -1,3 +1,4 @@ +import { Hex } from "../utils" import { payments } from "bitcoinjs-lib" /** @@ -5,9 +6,9 @@ import { payments } from "bitcoinjs-lib" * @param script The script to be checked. * @returns True if the script is P2PKH, false otherwise. */ -function isP2PKHScript(script: Buffer): boolean { +function isP2PKHScript(script: Hex): boolean { try { - payments.p2pkh({ output: script }) + payments.p2pkh({ output: script.toBuffer() }) return true } catch (err) { return false @@ -19,9 +20,9 @@ function isP2PKHScript(script: Buffer): boolean { * @param script The script to be checked. * @returns True if the script is P2WPKH, false otherwise. */ -function isP2WPKHScript(script: Buffer): boolean { +function isP2WPKHScript(script: Hex): boolean { try { - payments.p2wpkh({ output: script }) + payments.p2wpkh({ output: script.toBuffer() }) return true } catch (err) { return false @@ -33,9 +34,9 @@ function isP2WPKHScript(script: Buffer): boolean { * @param script The script to be checked. * @returns True if the script is P2SH, false otherwise. */ -function isP2SHScript(script: Buffer): boolean { +function isP2SHScript(script: Hex): boolean { try { - payments.p2sh({ output: script }) + payments.p2sh({ output: script.toBuffer() }) return true } catch (err) { return false @@ -47,9 +48,9 @@ function isP2SHScript(script: Buffer): boolean { * @param script The script to be checked. * @returns True if the script is P2WSH, false otherwise. */ -function isP2WSHScript(script: Buffer): boolean { +function isP2WSHScript(script: Hex): boolean { try { - payments.p2wsh({ output: script }) + payments.p2wsh({ output: script.toBuffer() }) return true } catch (err) { return false diff --git a/typescript/src/services/deposits/funding.ts b/typescript/src/services/deposits/funding.ts index c520f5e83..a3f9d8f7e 100644 --- a/typescript/src/services/deposits/funding.ts +++ b/typescript/src/services/deposits/funding.ts @@ -92,7 +92,7 @@ export class DepositFunding { // TODO: Add support for other utxo types along with unit tests for the // given type. - if (BitcoinScriptUtils.isP2WPKHScript(previousOutputScript)) { + if (BitcoinScriptUtils.isP2WPKHScript(Hex.from(previousOutputScript))) { psbt.addInput({ hash: utxo.transactionHash.reverse().toBuffer(), index: utxo.outputIndex, diff --git a/typescript/src/services/deposits/refund.ts b/typescript/src/services/deposits/refund.ts index 81d331704..219b1f3ee 100644 --- a/typescript/src/services/deposits/refund.ts +++ b/typescript/src/services/deposits/refund.ts @@ -154,7 +154,7 @@ export class DepositRefund { utxo.outputIndex ] const previousOutputValue = previousOutput.value - const previousOutputScript = previousOutput.script + const previousOutputScript = Hex.from(previousOutput.script) if (BitcoinScriptUtils.isP2SHScript(previousOutputScript)) { // P2SH deposit UTXO diff --git a/typescript/src/services/maintenance/wallet-tx.ts b/typescript/src/services/maintenance/wallet-tx.ts index b0306cdff..4756b8e59 100644 --- a/typescript/src/services/maintenance/wallet-tx.ts +++ b/typescript/src/services/maintenance/wallet-tx.ts @@ -249,7 +249,7 @@ class DepositSweep { utxo.outputIndex ] const previousOutputValue = previousOutput.value - const previousOutputScript = previousOutput.script + const previousOutputScript = Hex.from(previousOutput.script) const deposit = deposits[depositIndex] @@ -309,10 +309,13 @@ class DepositSweep { previousOutput: TxOutput, walletKeyPair: Signer ) { + const previousOutputScript = Hex.from(previousOutput.script) + const previousOutputValue = previousOutput.value + if ( !this.canSpendOutput( Hex.from(walletKeyPair.publicKey), - previousOutput.script + previousOutputScript ) ) { throw new Error("UTXO does not belong to the wallet") @@ -320,11 +323,11 @@ class DepositSweep { const sigHashType = Transaction.SIGHASH_ALL - if (BitcoinScriptUtils.isP2PKHScript(previousOutput.script)) { + if (BitcoinScriptUtils.isP2PKHScript(previousOutputScript)) { // P2PKH const sigHash = transaction.hashForSignature( inputIndex, - previousOutput.script, + previousOutputScript.toBuffer(), sigHashType ) @@ -339,16 +342,17 @@ class DepositSweep { }).input! transaction.ins[inputIndex].script = scriptSig - } else if (BitcoinScriptUtils.isP2WPKHScript(previousOutput.script)) { + } else if (BitcoinScriptUtils.isP2WPKHScript(previousOutputScript)) { // P2WPKH - const publicKeyHash = payments.p2wpkh({ output: previousOutput.script }) - .hash! + const publicKeyHash = payments.p2wpkh({ + output: previousOutputScript.toBuffer(), + }).hash! const p2pkhScript = payments.p2pkh({ hash: publicKeyHash }).output! const sigHash = transaction.hashForWitnessV0( inputIndex, p2pkhScript, - previousOutput.value, + previousOutputValue, sigHashType ) @@ -496,10 +500,14 @@ class DepositSweep { * @returns True if the provided output script matches the P2PKH or P2WPKH * output scripts derived from the given public key. False otherwise. */ - private canSpendOutput(publicKey: Hex, outputScript: Buffer): boolean { + private canSpendOutput(publicKey: Hex, outputScript: Hex): boolean { const pubkeyBuffer = publicKey.toBuffer() - const p2pkhOutput = payments.p2pkh({ pubkey: pubkeyBuffer }).output! - const p2wpkhOutput = payments.p2wpkh({ pubkey: pubkeyBuffer }).output! + const p2pkhOutput = Hex.from( + payments.p2pkh({ pubkey: pubkeyBuffer }).output! + ) + const p2wpkhOutput = Hex.from( + payments.p2wpkh({ pubkey: pubkeyBuffer }).output! + ) return outputScript.equals(p2pkhOutput) || outputScript.equals(p2wpkhOutput) } @@ -661,7 +669,7 @@ class Redemption { const previousOutput = Transaction.fromHex(mainUtxo.transactionHex).outs[ mainUtxo.outputIndex ] - const previousOutputScript = previousOutput.script + const previousOutputScript = Hex.from(previousOutput.script) const previousOutputValue = previousOutput.value if (BitcoinScriptUtils.isP2PKHScript(previousOutputScript)) { @@ -675,7 +683,7 @@ class Redemption { hash: mainUtxo.transactionHash.reverse().toBuffer(), index: mainUtxo.outputIndex, witnessUtxo: { - script: previousOutputScript, + script: previousOutputScript.toBuffer(), value: previousOutputValue, }, }) diff --git a/typescript/test/lib/bitcoin.test.ts b/typescript/test/lib/bitcoin.test.ts index cdb706947..fde3ac882 100644 --- a/typescript/test/lib/bitcoin.test.ts +++ b/typescript/test/lib/bitcoin.test.ts @@ -821,33 +821,27 @@ describe("Bitcoin", () => { const testData = [ { testFunction: isP2PKHScript, - validScript: Buffer.from( - "76a9148db50eb52063ea9d98b3eac91489a90f738986f688ac", - "hex" + validScript: Hex.from( + "76a9148db50eb52063ea9d98b3eac91489a90f738986f688ac" ), name: "P2PKH", }, { testFunction: isP2WPKHScript, - validScript: Buffer.from( - "00148db50eb52063ea9d98b3eac91489a90f738986f6", - "hex" - ), + validScript: Hex.from("00148db50eb52063ea9d98b3eac91489a90f738986f6"), name: "P2WPKH", }, { testFunction: isP2SHScript, - validScript: Buffer.from( - "a914a9a5f97d5d3c4687a52e90718168270005b369c487", - "hex" + validScript: Hex.from( + "a914a9a5f97d5d3c4687a52e90718168270005b369c487" ), name: "P2SH", }, { testFunction: isP2WSHScript, - validScript: Buffer.from( - "0020b1f83e226979dc9fe74e87f6d303dbb08a27a1c7ce91664033f34c7f2d214cd7", - "hex" + validScript: Hex.from( + "0020b1f83e226979dc9fe74e87f6d303dbb08a27a1c7ce91664033f34c7f2d214cd7" ), name: "P2WSH", },