Skip to content

Commit

Permalink
Used Hex as argument in isScript functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 13, 2023
1 parent 5faf6b4 commit ba1ca6e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
17 changes: 9 additions & 8 deletions typescript/src/lib/bitcoin/script.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Hex } from "../utils"
import { payments } from "bitcoinjs-lib"

/**
* Checks if the provided script comes from a P2PKH input.
* @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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/services/deposits/funding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/services/deposits/refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 21 additions & 13 deletions typescript/src/services/maintenance/wallet-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -309,22 +309,25 @@ 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")
}

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
)

Expand All @@ -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
)

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -675,7 +683,7 @@ class Redemption {
hash: mainUtxo.transactionHash.reverse().toBuffer(),
index: mainUtxo.outputIndex,
witnessUtxo: {
script: previousOutputScript,
script: previousOutputScript.toBuffer(),
value: previousOutputValue,
},
})
Expand Down
20 changes: 7 additions & 13 deletions typescript/test/lib/bitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down

0 comments on commit ba1ca6e

Please sign in to comment.