Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txscript: add new ScriptTemplate DSL for writing Scripts #2216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Roasbeef
Copy link
Member

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.

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.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 9984164707

Details

  • 0 of 133 (0.0%) changed or added relevant lines in 1 file are covered.
  • 12 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.2%) to 57.06%

Changes Missing Coverage Covered Lines Changed/Added Lines %
txscript/template.go 0 133 0.0%
Files with Coverage Reduction New Missed Lines %
mempool/mempool.go 1 66.67%
btcec/schnorr/musig2/sign.go 2 90.44%
peer/peer.go 9 73.94%
Totals Coverage Status
Change from base Build 9957391466: -0.2%
Covered Lines: 29826
Relevant Lines: 52271

💛 - Coveralls

// processScript converts the template output to actual script bytes. We scan
// each line, then go through each element one by one, deciding to either add a
// normal op code, a push data, or an integer value.
func processScript(script string) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add a unit test?

// looksLikeInt checks if a string looks like an integer.
func looksLikeInt(s string) bool {
// Check if the string starts with an optional sign
if len(s) > 0 && (s[0] == '+' || s[0] == '-') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when will + or - be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants