btcd/txscript
Olaoluwa Osuntokun c9bd7b4c0f
txscript: add new ScriptTemplate DSL for writing Scripts
In this commit, we add a new function, `ScriptTemplate` to make the
process of making custom Bitcoin scripts a bit less verbose.

ScriptTemplate processes a script template with parameters and returns the
corresponding script bytes. This functions allows Bitcoin scripts to be
created using a DSL-like syntax, based on Go's templating system.

An example of a simple p2pkh template would be:

   `OP_DUP OP_HASH160 0x14e8948c7afa71b6e6fad621256474b5959e0305 OP_EQUALVERIFY OP_CHECKSIG`

Strings that have the `0x` prefix are assumed to byte strings to be pushed
ontop of the stack. Integers can be passed as normal. If a value can't be
parsed as an integer, then it's assume that it's a byte slice without the 0x
prefix.

Normal go template operations can be used as well. The params argument
houses paramters to pass into the script, for example a local variable
storing a computed public key.
2025-07-11 12:20:05 -07:00
..
data Merge pull request #2178 from ProofOfKeags/standardness-cleanup 2024-05-21 15:44:24 -07:00
bench_test.go multi: fix ioutil deprecated function 2024-03-08 17:41:41 -08:00
consensus.go txscript: Implement CheckSequenceVerify (BIP0112) 2016-10-19 12:06:44 -04:00
doc.go fix some typos and make OP_DATA_20 explicit in comment (#2080) 2024-04-10 10:08:30 -04:00
engine.go Merge pull request #2178 from ProofOfKeags/standardness-cleanup 2024-05-21 15:44:24 -07:00
engine_debug_test.go fix typos (#2100) 2024-03-25 09:44:25 -04:00
engine_test.go fix typos (#2100) 2024-03-25 09:44:25 -04:00
error.go Merge pull request #2178 from ProofOfKeags/standardness-cleanup 2024-05-21 15:44:24 -07:00
error_test.go txscript: make OP_CODESEPARATOR non-standard in non-segwit scripts 2024-05-21 12:30:16 -07:00
example_test.go txscript: implement BIP 341+342 segwit v1 taproot+tapscript 2022-03-15 18:22:43 -07:00
hashcache.go refactor: use maps.Copy for cleaner map handling 2025-03-25 01:53:15 +08:00
hashcache_test.go txscript: implement BIP 341+342 segwit v1 taproot+tapscript 2022-03-15 18:22:43 -07:00
log.go all: Remove seelog logger. 2017-06-19 16:46:50 -04:00
opcode.go multi: fix typos 2024-10-08 14:47:41 +02:00
opcode_test.go txscript: implement OP_CHECKSIGADD 2022-03-15 18:23:26 -07:00
pkscript.go txscript: add taproot script type 2022-03-15 18:22:25 -07:00
pkscript_test.go txscript: add taproot script type 2022-03-15 18:22:25 -07:00
README.md txscript: Introduce zero-alloc script tokenizer. 2021-11-16 18:45:22 -08:00
reference_test.go txscript: make OP_CODESEPARATOR non-standard in non-segwit scripts 2024-05-21 12:30:16 -07:00
script.go Merge pull request #2178 from ProofOfKeags/standardness-cleanup 2024-05-21 15:44:24 -07:00
script_test.go txscript: make OP_CODESEPARATOR non-standard in non-segwit scripts 2024-05-21 12:30:16 -07:00
scriptbuilder.go txscript: allow script builder capacity to be specified 2023-02-27 12:38:13 +01:00
scriptbuilder_test.go txscript: allow script builder capacity to be specified 2023-02-27 12:38:13 +01:00
scriptnum.go multi: remove repetitive the 2023-06-26 15:40:51 +08:00
scriptnum_test.go Export MakeScritpNum, AsSmallInt, and IsSmallInt 2023-03-03 15:05:14 -05:00
sigcache.go txscript: update SigCache to cache both ECDSA and Schnorr signatures 2022-03-15 18:22:45 -07:00
sigcache_test.go txscript: implement BIP 341+342 segwit v1 taproot+tapscript 2022-03-15 18:22:43 -07:00
sighash.go txscript: use a single shared scratch buffer in segwit sighash calc 2023-12-19 15:02:05 -08:00
sign.go txscript: fix sighash bug in RawTxInTaprootSignature 2023-01-24 18:43:45 -08:00
sign_test.go txscript: add tests for RawTxInTaprootSignature and RawTxInTapscriptSignature 2023-01-24 18:43:55 -08:00
sigvalidate.go Merge pull request #2178 from ProofOfKeags/standardness-cleanup 2024-05-21 15:44:24 -07:00
stack.go Export MakeScritpNum, AsSmallInt, and IsSmallInt 2023-03-03 15:05:14 -05:00
stack_test.go txscript: Significantly improve errors. 2017-01-12 13:12:39 -06:00
standard.go Merge pull request #2178 from ProofOfKeags/standardness-cleanup 2024-05-21 15:44:24 -07:00
standard_test.go fix typos (#2100) 2024-03-25 09:44:25 -04:00
taproot.go txscript: add more detail to invalid tapscript merkle proof error 2025-06-18 18:11:24 -07:00
taproot_test.go txscript: add more detail to invalid tapscript merkle proof error 2025-06-18 18:11:24 -07:00
template.go txscript: add new ScriptTemplate DSL for writing Scripts 2025-07-11 12:20:05 -07:00
template_test.go txscript: add new ScriptTemplate DSL for writing Scripts 2025-07-11 12:20:05 -07:00
tokenizer.go txscript: add new OpcodePosition method to tokenizer to save code sep position 2022-03-15 18:23:21 -07:00
tokenizer_test.go txscript: Introduce zero-alloc script tokenizer. 2021-11-16 18:45:22 -08:00

txscript

Build Status ISC License GoDoc

Package txscript implements the bitcoin transaction script language. There is a comprehensive test suite.

This package has intentionally been designed so it can be used as a standalone package for any projects needing to use or validate bitcoin transaction scripts.

Bitcoin Scripts

Bitcoin provides a stack-based, FORTH-like language for the scripts in the bitcoin transactions. This language is not turing complete although it is still fairly powerful. A description of the language can be found at https://en.bitcoin.it/wiki/Script

Installation and Updating

$ go get -u github.com/btcsuite/btcd/txscript

Examples

GPG Verification Key

All official release tags are signed by Conformal so users can ensure the code has not been tampered with and is coming from the btcsuite developers. To verify the signature perform the following:

  • Download the public key from the Conformal website at https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt

  • Import the public key into your GPG keyring:

    gpg --import GIT-GPG-KEY-conformal.txt
    
  • Verify the release tag with the following command where TAG_NAME is a placeholder for the specific tag:

    git tag -v TAG_NAME
    

License

Package txscript is licensed under the copyfree ISC License.