Code Monkey home page Code Monkey logo

rust-bitcoin-script's Introduction

bitcoin-script

Rust crates.io docs.rs

Bitcoin scripts inline in Rust.


Usage

This crate exports a bitcoin_script! macro which can be used to build Bitcoin scripts. The macro returns the Script type from the bitcoin crate.

Example:

#![feature(proc_macro_hygiene)]

use bitcoin_script::bitcoin_script;

let htlc_script = bitcoin_script! {
    OP_IF
        OP_SHA256 <digest> OP_EQUALVERIFY OP_DUP OP_SHA256 <seller_pubkey_hash>
    OP_ELSE
        100 OP_CSV OP_DROP OP_DUP OP_HASH160 <buyer_pubkey_hash>
    OP_ENDIF
    OP_EQUALVERIFY
    OP_CHECKSIG
};

NOTE: As of rustc 1.41, the Rust compiler prevents using procedural macros as expressions. To use this macro you'll need to be on nightly and add #![feature(proc_macro_hygiene)] to the root of your crate. This will be stablized in the near future, the PR can be found here: rust-lang/rust#68717

Syntax

Scripts are based on the standard syntax made up of opcodes, base-10 integers, or hex string literals. Additionally, Rust expressions can be interpolated in order to support dynamically capturing Rust variables or computing values (delimited by <angle brackets>).

Whitespace is ignored - scripts can be formatted in the author's preferred style.

Opcodes

All normal opcodes are available, in the form OP_X.

let script = bitcoin_script!(OP_CHECKSIG OP_VERIFY);

Integer Literals

Positive and negative 64-bit integer literals can be used, and will resolve to their most efficient encoding.

For example:

  • 2 will resolve to OP_PUSHNUM_2 (0x52)
  • 255 will resolve to a length-delimited varint: 0x02ff00 (note the extra zero byte, due to the way Bitcoin scripts use the most-significant bit to represent the sign)`
let script = bitcoin_script!(123 -456 999999);

Hex Literals

Hex strings can be specified, prefixed with 0x.

let script = bitcoin_script!(
    0x0102030405060708090a0b0c0d0e0f OP_HASH160
);

Escape Sequences

Dynamic Rust expressions are supported inside the script, surrounded by angle brackets. In many cases, this will just be a variable identifier, but this can also be a function call or arithmetic.

Rust expressions of the following types are supported:

let bytes = vec![1, 2, 3];

let script = bitcoin_script! {
    <bytes> OP_CHECKSIGVERIFY

    <2016 * 5> OP_CSV
};

rust-bitcoin-script's People

Contributors

mappum avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.