Giter Club home page Giter Club logo

btcs's Introduction

btcs

Build Status

bitcoin script parser/evaluator/compiler/decompiler

WARNING: This is not a consensus-critical implementation of bitcoin script. Make sure to actually test your script on testnet or with something like btcdeb.

Motivation

I wanted a better way to hack on Bitcoin scripts

Installing

deps: python3, bison, flex, a C compiler

$ make install

with nix:

$ nix-env -if .

TODOs

  • Tokenizer
  • Evaluator (partially complete)
  • Compiler
  • Decompiler
  • Docs

Usage

Work in progress. Some OPs are working:

$ btcs 1 2 16 3dup 2swap

script      1 2 16 OP_3DUP OP_2SWAP
script_hex  5152606f72
stack       1 2 2 16 16 1
stack_hex   010202101001
results     success

btcs's People

Contributors

jb55 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

btcs's Issues

Decoder

It would be nice to have a mode that decoded hex-encoded script, like bitcoin-cli decodescript but much faster. Then we could do something like:

./bitcoin-iterate -q --output=%os --start 0 --end 80000 | btcs --decode

Inline hex

We could use the same syntax that bitcoin-tx uses:

btcs <<<'0x6a03 0x010203'

Optimizations brainstorming

  • swap drop -> nip
  • dup if dup endif -> ifdup
  • swap over -> tuck
  • dup dup -> 2dup
  • dup dup dup -> 3dup
  • drop drop -> 2drop
  • rot rot rot ->
  • swap 1 pick -> tuck
  • 0 pick -> dup
  • 1 pick 1 pick -> 2dup
  • 2 pick 2 pick 2 pick -> 3dup
  • x add x sub ->
  • 0 roll ->

Numbers

Where x and y are scriptnums

  • x y add -> x + y
  • x y sub -> x - y

Binary literals

Inspired from the mailing list:

   0 pkb6 pkb5 pkb4 pkb3 pkb2 pkb1 pka3 pka2 pka1 9 CHECK_AGGSIG_VERIFY
     (declare pubkeys)
   0b111 CHECK_AGG_SIGNERS VERIFY
     (one of pka{1,2,3} must sign)
   0b001 CHECK_AGG_SIGNERS
   0b010 CHECK_AGG_SIGNERS ADD
   0b100 CHECK_AGG_SIGNERS ADD
   DUP ADD
     (pka{1,2,3} count double)
   0b000001000 CHECK_AGG_SIGNERS ADD
   0b000010000 CHECK_AGG_SIGNERS ADD
   0b000100000 CHECK_AGG_SIGNERS ADD
   0b001000000 CHECK_AGG_SIGNERS ADD
   0b010000000 CHECK_AGG_SIGNERS ADD
   0b100000000 CHECK_AGG_SIGNERS ADD
     (pkb{1..6} count single)
   6 EQUAL

Implement Literals

Perhaps 0xabcdef0123456789 for pushing data. Arbitrary hex strings without the prefix should be possible as well. What about decimal integers/ScriptNums? Strings?

Full support for many types of literals would be nice.

  • #13 Number literals
  • #11 Data literals
  • #18 String literals

segwit stuff

  • Only compressed public keys are accepted in P2WPKH and P2WSH (See BIP143)
  • The argument of OP_IF/NOTIF in P2WSH must be minimal[4]
  • Signature(s) must be null vector(s) if an OP_CHECKSIG or OP_CHECKMULTISIG is failed (for both pre-segregated witness script and P2WSH. See BIP146)

Implement disabled opcodes

because why not. perhaps an option to continue executing and report an invalid script error in the result.

Assert failure in simple data script

$ btcs <<< '@aa dup equal not'                                                                                                                  ~/src/c/btcs
script      0xaa OP_DUP OP_EQUAL OP_NOT

btcs: script_num.c:126: sn_from_val: Assertion `(*sn)->ind == val.ind' failed

stack_hex not displaying correctly when stack contains the number 0

examples:

script      0 OP_NOT
script_hex  0091
stack       1
stack_hex   01
results     success

script      1 OP_NOT
script_hex  5191
stack       0
stack_hex                                {missing 00}
results     success

script      0 0 OP_SUB
script_hex  000094
stack       0
stack_hex                                {missing 00}
results     success

script      1 0 OP_SUB
script_hex  510094
stack       1
stack_hex   01
results     success

script      127 0 127 1
script_hex  017f00017f51
stack       0x7f 0x 0x7f 1                {0x?}
stack_hex   7f7f01                        {missing 00}
results     success

REPL

Would be nice when ran without input

JSON output mode

A lot of bitcoin cli tools use JSON for output. Perhaps there could be a mode that supports this for interop with jq, bitcoin-cli, etc

Input prompt

Implement script_handle_input, which prompts on free variables.

For example, with a script such as:

OP_IF
    [HASHOP] <digest> OP_EQUALVERIFY OP_DUP OP_HASH160 <seller pubkey hash>            
OP_ELSE
    <num> [TIMEOUTOP] OP_DROP OP_DUP OP_HASH160 <buyer pubkey hash>
OP_ENDIF
OP_EQUALVERIFY
OP_CHECKSIG

it should prompt for input on <digest>, <seller pubkey hash> etc. otherwise, provide input via cli arguments. Maybe eventually we will have variables...

4 byte negative numbers not working

Testing if a number is within a range, returns 1 (i.e. 'true'):

script      1 -100 100 OP_WITHIN
script_hex  5101e40164a5
stack       1

script      1 -8388607 8388607 OP_WITHIN
script_hex  5103ffffff03ffff7fa5
stack       1

Increment the range (numbers now 4 bytes), returns 0 which is incorrect it should be 1.

script      1 -8388608 8388608 OP_WITHIN
script_hex  5104000080800400008000a5
stack       0

This test is from bitcoin core, expected return value is 1.

script      0 -2147483647 2147483647 OP_WITHIN
script_hex  0004ffffffff04ffffff7fa5
stack       0

String literals

Sugar for data literals

Would be handy for OP_RETURN stuff maybe?

Script context

Parameters for script context would be nice. I'm currently missing:

BaseSignatureChecker

Used in CLTV, CSV, CHECKSIGVERIFY. This is just an interface to the TransactionSignatureChecker which requires extra context, such as txos. How to represent this state for testing scripts?

  • CSV: checker.CheckSequence
  • CLTV: checker.CheckLockTime
  • CHECKSIG: checker.CheckSig

Other stuff

  • SigVersion
  • flags:
SCRIPT_VERIFY_NONE                                  = 0,
SCRIPT_VERIFY_P2SH                                  = (1U << 0),
SCRIPT_VERIFY_STRICTENC                             = (1U << 1),
SCRIPT_VERIFY_DERSIG                                = (1U << 2),
SCRIPT_VERIFY_LOW_S                                 = (1U << 3),
SCRIPT_VERIFY_NULLDUMMY                             = (1U << 4),
SCRIPT_VERIFY_SIGPUSHONLY                           = (1U << 5),
SCRIPT_VERIFY_MINIMALDATA                           = (1U << 6),
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS            = (1U << 7),
SCRIPT_VERIFY_CLEANSTACK                            = (1U << 8),
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY                   = (1U << 9),
SCRIPT_VERIFY_CHECKSEQUENCEVERIFY                   = (1U << 10),
SCRIPT_VERIFY_WITNESS                               = (1U << 11),
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM = (1U << 12),
SCRIPT_VERIFY_MINIMALIF                             = (1U << 13),
SCRIPT_VERIFY_NULLFAIL                              = (1U << 14),
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE                    = (1U << 15),

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.