Skip to content

Commit

Permalink
feat: add non struct getBitcoinScript
Browse files Browse the repository at this point in the history
  • Loading branch information
reednaa committed Jun 10, 2024
1 parent ea720de commit 9e9d140
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/library/BtcScript.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ library BtcScript {
}
}

/**
* @notice Global helper for encoding Bitcoin scripts
* @param addressType Enum of address type. Used to specify which script is used.
* @param implementationHash P2PKH, address hash or P2SH, script hash.
*/
function getBitcoinScript(AddressType addressType, bytes32 implementationHash) internal pure returns(bytes memory script) {
// Check if segwit
if (addressType == AddressType.P2PKH) return scriptP2PKH(bytes20(implementationHash));
if (addressType == AddressType.P2SH) return scriptP2SH(bytes20(implementationHash));
if (addressType == AddressType.P2WPKH) {
return scriptP2WPKH(bytes20(implementationHash));
}
if (addressType == AddressType.P2SH) {
return scriptP2WSH(implementationHash);
}
if (addressType == AddressType.P2TR) {
return scriptP2TR(implementationHash);
}
}

/// @notice Get the associated script out for a P2PKH address
function scriptP2PKH(bytes20 pHash) internal pure returns(bytes memory) {
// OP_DUB, OP_HASH160, <pubKeyHash 20>, OP_EQUALVERIFY, OP_CHECKSIG
Expand Down

0 comments on commit 9e9d140

Please sign in to comment.