Skip to content

Commit

Permalink
Added functionalities for checking input type
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 18, 2023
1 parent b9f8ce4 commit 85567ff
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sha256 from "bcrypto/lib/sha256-browser.js"
import { BigNumber } from "ethers"
import { Hex } from "./hex"
import { BitcoinNetwork, toBcoinNetwork } from "./bitcoin-network"
import { payments } from "bitcoinjs-lib"

/**
* Represents a transaction hash (or transaction ID) as an un-prefixed hex
Expand Down Expand Up @@ -679,3 +680,59 @@ export function readCompactSizeUint(varLenData: Hex): {
}
}
}

/**
* 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.
*/
export function isP2PKH(script: Buffer): boolean {
try {
payments.p2pkh({ output: script });

Check failure on line 691 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
return true;

Check failure on line 692 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
} catch (err) {
return false;

Check failure on line 694 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
}
}

/**
* Checks if the provided script comes from a P2WPKH input.
* @param script The script to be checked.
* @returns True if the script is P2WPKH, false otherwise.
*/
export function isP2WPKH(script: Buffer): boolean {
try {
payments.p2wpkh({ output: script });

Check failure on line 705 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
return true;

Check failure on line 706 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
} catch (err) {
return false;

Check failure on line 708 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
}
}

/**
* Checks if the provided script comes from a P2SH input.
* @param script The script to be checked.
* @returns True if the script is P2SH, false otherwise.
*/
export function isP2SH(script: Buffer): boolean {
try {
payments.p2sh({ output: script });

Check failure on line 719 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
return true;

Check failure on line 720 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
} catch (err) {
return false;

Check failure on line 722 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
}
}

/**
* Checks if the provided script comes from a P2PKH input.
* @param script The script to be checked.
* @returns True if the script is P2WSH, false otherwise.
*/
export function isP2WSH(script: Buffer): boolean {
try {
payments.p2wsh({ output: script });

Check failure on line 733 in typescript/src/bitcoin.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Delete `;`
return true;
} catch (err) {
return false;
}
}

0 comments on commit 85567ff

Please sign in to comment.