Home

Awesome

<div align="center"> <img src="./docs/img/btczee.png" alt="btczee-logo" height="260"/> <h2>Bitcoin protocol implementation in Zig.</h2>

<a href="https://github.com/zig-bitcoin/btczee/actions/workflows/check.yml"><img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/zig-bitcoin/btczee/check.yml?style=for-the-badge" height=30></a> <a href="https://ziglang.org/"> <img alt="Zig" src="https://img.shields.io/badge/zig-%23000000.svg?style=for-the-badge&logo=zig&logoColor=white" height=30></a> <a href="https://bitcoin.org/"> <img alt="Bitcoin" src="https://img.shields.io/badge/Bitcoin-000?style=for-the-badge&logo=bitcoin&logoColor=white" height=30></a> <a href="https://lightning.network/"><img src="https://img.shields.io/badge/Ligthning Network-000.svg?&style=for-the-badge&logo=data:image/svg%2bxml;base64%2CPD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI%2FPg0KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIEZpcmV3b3JrcyAxMCwgRXhwb3J0IFNWRyBFeHRlbnNpb24gYnkgQWFyb24gQmVhbGwgKGh0dHA6Ly9maXJld29ya3MuYWJlYWxsLmNvbSkgLiBWZXJzaW9uOiAwLjYuMSAgLS0%2BDQo8IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPg0KPHN2ZyBpZD0iYml0Y29pbl9saWdodG5pbmdfaWNvbi5mdy1QYWdlJTIwMSIgdmlld0JveD0iMCAwIDI4MCAyODAiIHN0eWxlPSJiYWNrZ3JvdW5kLWNvbG9yOiNmZmZmZmYwMCIgdmVyc2lvbj0iMS4xIg0KCXhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbDpzcGFjZT0icHJlc2VydmUiDQoJeD0iMHB4IiB5PSIwcHgiIHdpZHRoPSIyODBweCIgaGVpZ2h0PSIyODBweCINCj4NCgk8cGF0aCBpZD0iRWxsaXBzZSIgZD0iTSA3IDE0MC41IEMgNyA2Ni43NjkgNjYuNzY5IDcgMTQwLjUgNyBDIDIxNC4yMzEgNyAyNzQgNjYuNzY5IDI3NCAxNDAuNSBDIDI3NCAyMTQuMjMxIDIxNC4yMzEgMjc0IDE0MC41IDI3NCBDIDY2Ljc2OSAyNzQgNyAyMTQuMjMxIDcgMTQwLjUgWiIgZmlsbD0iI2Y3OTMxYSIvPg0KCTxwYXRoIGQ9Ik0gMTYxLjE5NDMgNTEuNSBDIDE1My4yMzQ5IDcyLjE2MDcgMTQ1LjI3NTYgOTQuNDEwNyAxMzUuNzI0NCAxMTYuNjYwNyBDIDEzNS43MjQ0IDExNi42NjA3IDEzNS43MjQ0IDExOS44MzkzIDEzOC45MDgxIDExOS44MzkzIEwgMjA0LjE3NDcgMTE5LjgzOTMgQyAyMDQuMTc0NyAxMTkuODM5MyAyMDQuMTc0NyAxMjEuNDI4NiAyMDUuNzY2NyAxMjMuMDE3OSBMIDExMC4yNTQ1IDIyOS41IEMgMTA4LjY2MjYgMjI3LjkxMDcgMTA4LjY2MjYgMjI2LjMyMTQgMTA4LjY2MjYgMjI0LjczMjEgTCAxNDIuMDkxOSAxNTMuMjE0MyBMIDE0Mi4wOTE5IDE0Ni44NTcxIEwgNzUuMjMzMyAxNDYuODU3MSBMIDc1LjIzMzMgMTQwLjUgTCAxNTYuNDE4NyA1MS41IEwgMTYxLjE5NDMgNTEuNSBaIiBmaWxsPSIjZmZmZmZmIi8%2BDQo8L3N2Zz4%3D" alt="Bitcoin Lightning" height="30"></a>

</div>

About

btczee is a Bitcoin protocol implementation in Zig. It aims to provide a clean and simple implementation of the Bitcoin protocol. The goal is to have a fully functional Bitcoin node that can be used to interact with the Bitcoin network.

Architecture

You can find the architecture of the project and description of components in the docs/architecture.md file.

graph TD
    Node[Node] --> Network
    Node --> Mempool
    Node --> Wallet
    Node --> Storage
    Node --> Miner

    Network --> Mempool
    Network --> Storage

    Mempool --> Wallet
    Mempool --> Storage

    Wallet --> Storage

    Miner --> Mempool
    Miner --> Storage

    subgraph "Core Components"
        Node
        Network
        Mempool
        Wallet
        Storage
        Miner
    end

    subgraph "Supporting Components"
        Types
        Primitives
        Config
    end

    Node -.-> Types
    Node -.-> Primitives
    Node -.-> Config

    classDef core fill:#f9f,stroke:#333,stroke-width:2px;
    classDef support fill:#bbf,stroke:#333,stroke-width:1px;
    class Node,Network,Mempool,Wallet,Storage,Miner core;
    class Types,Primitives,Config support;

Run

Usage: btczee [command] [args]

Commands:
  node     <subcommand>
  wallet   <subcommand>

Node

Usage: btczee node <subcommand>

Subcommands:
  help   Display help for node

Example:

zig build run -- node

# OR (after a build)
./zig-out/bin/btczee node

Wallet

Usage: btczee wallet <subcommand>

Subcommands:
  create    Create a new wallet
  load      Load an existing wallet
  help      Display help for wallet

Example:

zig build run -- wallet create

# OR (after a build)
./zig-out/bin/btczee wallet create

Test

zig build test --summary all

Build

zig build -Doptimize=ReleaseFast

Benchmark

zig build bench

Documentation

You can generate the documentation by running the following command:

zig build docs

Roadmap

You can find the roadmap of the project in the docs/roadmap.md file.

License

btczee is licensed under the MIT license. See the LICENSE file for more details.

References

Acknowledgements

Supported Opcodes

47/107 opcodes supported (43.92%).

%%{init: {"pie": {"textPosition": 0.75}, "themeVariables": {"pieOuterStrokeWidth": "5px"}} }%%
pie showData
    title Opcode Implementation Status
    "Implemented" : 47
    "Disabled" : 0
    "Not Implemented" : 60
OpcodeHexSupportedDescription
op0 / opFalse0x00An empty array of bytes is pushed onto the stack.
opData1-opData750x01-0x4bThe next opcode bytes is data to be pushed onto the stack.
opPushData10x4cThe next byte contains the number of bytes to be pushed onto the stack.
opPushData20x4dThe next two bytes contain the number of bytes to be pushed onto the stack in little endian order.
opPushData40x4eThe next four bytes contain the number of bytes to be pushed onto the stack in little endian order.
op1Negate0x4fThe number -1 is pushed onto the stack.
opReserved0x50Transaction is invalid unless occurring in an unexecuted opIF branch
op1 / opTrue0x51The number 1 is pushed onto the stack.
op20x52The number 2 is pushed onto the stack.
op30x53The number 3 is pushed onto the stack.
op40x54The number 4 is pushed onto the stack.
op50x55The number 5 is pushed onto the stack.
op60x56The number 6 is pushed onto the stack.
op70x57The number 7 is pushed onto the stack.
op80x58The number 8 is pushed onto the stack.
op90x59The number 9 is pushed onto the stack.
op100x5aThe number 10 is pushed onto the stack.
op110x5bThe number 11 is pushed onto the stack.
op120x5cThe number 12 is pushed onto the stack.
op130x5dThe number 13 is pushed onto the stack.
op140x5eThe number 14 is pushed onto the stack.
op150x5fThe number 15 is pushed onto the stack.
op160x60The number 16 is pushed onto the stack.
opNop0x61Does nothing.
opVer0x62Transaction is invalid unless occurring in an unexecuted opIF branch
opIf0x63If the top stack value is not False, the statements are executed. The top stack value is removed.
opNotIf0x64If the top stack value is False, the statements are executed. The top stack value is removed.
opVerIf0x65Transaction is invalid even when occurring in an unexecuted opIF branch
opVerNotIf0x66Transaction is invalid even when occurring in an unexecuted opIF branch
opElse0x67If the preceding opIF or opNOTIF or opELSE was not executed then these statements are and if the preceding opIF or opNOTIF or opELSE was executed then these statements are not.
opEndIf0x68Ends an if/else block.
opVerify0x69Marks transaction as invalid if top stack value is not true.
opReturn0x6aMarks transaction as invalid.
opToAltStack0x6bPuts the input onto the top of the alt stack. Removes it from the main stack.
opFromAltStack0x6cPuts the input onto the top of the main stack. Removes it from the alt stack.
op2Drop0x6dRemoves the top two stack items.
op2Dup0x6eDuplicates the top two stack items.
op3Dup0x6fDuplicates the top three stack items.
op2Over0x70Copies the pair of items two spaces back in the stack to the front.
op2Rot0x71The fifth and sixth items back are moved to the top of the stack.
op2Swap0x72Swaps the top two pairs of items.
opIfDup0x73If the top stack value is not 0, duplicate it.
opDepth0x74Puts the number of stack items onto the stack.
opDrop0x75Removes the top stack item.
opDup0x76Duplicates the top stack item.
opNip0x77Removes the second-to-top stack item.
opOver0x78Copies the second-to-top stack item to the top.
opPick0x79The item n back in the stack is copied to the top.
opRoll0x7aThe item n back in the stack is moved to the top.
opRot0x7bThe top three items on the stack are rotated to the left.
opSwap0x7cThe top two items on the stack are swapped.
opTuck0x7dThe item at the top of the stack is copied and inserted before the second-to-top item.
opCat0x7eConcatenates two strings. Disabled.
opSubStr0x7fReturns a section of a string. Disabled.
opLeft0x80Keeps only characters left of the specified point in a string. Disabled.
opRight0x81Keeps only characters right of the specified point in a string. Disabled.
opSize0x82Pushes the string length of the top element of the stack (without popping it).
opInvert0x83Flips all of the bits in the input. Disabled.
opAnd0x84Boolean and between each bit in the inputs. Disabled.
opOr0x85Boolean or between each bit in the inputs. Disabled.
opXor0x86Boolean exclusive or between each bit in the inputs. Disabled.
opEqual0x87Returns 1 if the inputs are exactly equal, 0 otherwise.
opEqualVerify0x88Same as opEQUAL, but runs opVERIFY afterward.
opReserved10x89Transaction is invalid unless occurring in an unexecuted opIF branch
opReserved20x8aTransaction is invalid unless occurring in an unexecuted opIF branch
op1Add0x8b1 is added to the input.
op1Sub0x8c1 is subtracted from the input.
op2Mul0x8dThe input is multiplied by 2. Disabled.
op2Div0x8eThe input is divided by 2. Disabled.
opNegate0x8fThe sign of the input is flipped.
opAbs0x90The input is made positive.
opNot0x91If the input is 0 or 1, it is flipped. Otherwise the output will be 0.
op0NotEqual0x92Returns 0 if the input is 0. 1 otherwise.
opAdd0x93a is added to b.
opSub0x94b is subtracted from a.
opMul0x95a is multiplied by b. Disabled.
opDiv0x96a is divided by b. Disabled.
opMod0x97Returns the remainder after dividing a by b. Disabled.
opLShift0x98Shifts a left b bits, preserving sign. Disabled.
opRShift0x99Shifts a right b bits, preserving sign. Disabled.
opBoolAnd0x9aIf both a and b are not 0, the output is 1. Otherwise 0.
opBoolOr0x9bIf a or b is not 0, the output is 1. Otherwise 0.
opNumEqual0x9cReturns 1 if the numbers are equal, 0 otherwise.
opNumEqualVerify0x9dSame as opNUMEQUAL, but runs opVERIFY afterward.
opNumNorEqual0x9eReturns 1 if the numbers are not equal, 0 otherwise.
opLessThan0x9fReturns 1 if a is less than b, 0 otherwise.
opGreaterThan0xa0Returns 1 if a is greater than b, 0 otherwise.
opLessThanOrEqual0xa1Returns 1 if a is less than or equal to b, 0 otherwise.
opGreaterThanOrEqual0xa2Returns 1 if a is greater than or equal to b, 0 otherwise.
opMin0xa3Returns the smaller of a and b.
opMax0xa4Returns the larger of a and b.
opWithin0xa5Returns 1 if x is within the specified range (left-inclusive), 0 otherwise.
opRipeMd1600xa6The input is hashed using RIPEMD-160.
opSha10xa7The input is hashed using SHA-1.
opSha2560xa8The input is hashed using SHA-256.
opHash1600xa9The input is hashed twice: first with SHA-256 and then with RIPEMD-160.
opHash2560xaaThe input is hashed two times with SHA-256.
opCodeSeparator0xabAll of the signature checking words will only match signatures to the data after the most recently-executed opCODESEPARATOR.
opCheckSig0xacThe entire transaction's outputs, inputs, and script are hashed. The signature used by opCHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise.
opCheckSigVerify0xadSame as opCHECKSIG, but opVERIFY is executed afterward.
opCheckMultiSig0xaeCompares the first signature against each public key until it finds an ECDSA match. Starting with the subsequent public key, it compares the second signature against each remaining public key until it finds an ECDSA match. The process is repeated until all signatures have been checked or not enough public keys remain to produce a successful result. All signatures need to match a public key. If all signatures are valid, 1 is returned, 0 otherwise. Due to a bug, one extra unused value is removed from the stack.
opCheckMultiSigVerify0xafSame as opCHECKMULTISIG, but opVERIFY is executed afterward.
opNop10xb0The word is ignored. Does not mark transaction as invalid.
opCheckLockTimeVerify0xb1Marks transaction as invalid if the top stack item is greater than the transaction's nLockTime field, otherwise script evaluation continues as though an opNOP was executed.
opCheckSequenceVerify0xb2Marks transaction as invalid if the relative lock time of the input is not equal to or longer than the value of the top stack item.
opNop4-opNop100xb3-0xb9The word is ignored. Does not mark transaction as invalid.
opCheckSigAdd0xbaIncrements n by one and returns to the stack if the signature is valid for the public key and transaction. Only available in tapscript.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> <!-- prettier-ignore-start --> <!-- markdownlint-disable --> <table> <tbody> <tr> <td align="center" valign="top" width="14.28%"><a href="https://github.com/AbdelStark"><img src="https://avatars.githubusercontent.com/u/45264458?v=4?s=100" width="100px;" alt="A₿del ∞/21M 🐺 - 🐱"/><br /><sub><b>A₿del ∞/21M 🐺 - 🐱</b></sub></a><br /><a href="https://github.com/zig-bitcoin/btczee/commits?author=AbdelStark" title="Code">💻</a> <a href="#ideas-AbdelStark" title="Ideas, Planning, & Feedback">🤔</a> <a href="#mentoring-AbdelStark" title="Mentoring">🧑‍🏫</a> <a href="#projectManagement-AbdelStark" title="Project Management">📆</a> <a href="#research-AbdelStark" title="Research">🔬</a> <a href="https://github.com/zig-bitcoin/btczee/pulls?q=is%3Apr+reviewed-by%3AAbdelStark" title="Reviewed Pull Requests">👀</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/lana-shanghai"><img src="https://avatars.githubusercontent.com/u/31368580?v=4?s=100" width="100px;" alt="lanaivina"/><br /><sub><b>lanaivina</b></sub></a><br /><a href="https://github.com/zig-bitcoin/btczee/commits?author=lana-shanghai" title="Code">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/tdelabro"><img src="https://avatars.githubusercontent.com/u/34384633?v=4?s=100" width="100px;" alt="Timothée Delabrouille"/><br /><sub><b>Timothée Delabrouille</b></sub></a><br /><a href="https://github.com/zig-bitcoin/btczee/commits?author=tdelabro" title="Code">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://okhaimie.com/"><img src="https://avatars.githubusercontent.com/u/57156589?v=4?s=100" width="100px;" alt="okhai"/><br /><sub><b>okhai</b></sub></a><br /><a href="https://github.com/zig-bitcoin/btczee/commits?author=okhaimie-dev" title="Code">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/supreme2580"><img src="https://avatars.githubusercontent.com/u/100731397?v=4?s=100" width="100px;" alt="Supreme Labs"/><br /><sub><b>Supreme Labs</b></sub></a><br /><a href="https://github.com/zig-bitcoin/btczee/commits?author=supreme2580" title="Code">💻</a></td> <td align="center" valign="top" width="14.28%"><a href="https://varun-doshi.vercel.app/"><img src="https://avatars.githubusercontent.com/u/61531351?v=4?s=100" width="100px;" alt="Varun Doshi"/><br /><sub><b>Varun Doshi</b></sub></a><br /><a href="https://github.com/zig-bitcoin/btczee/commits?author=varun-doshi" title="Code">💻</a></td> </tr> </tbody> <tfoot> <tr> <td align="center" size="13px" colspan="7"> <img src="https://raw.githubusercontent.com/all-contributors/all-contributors-cli/1b8533af435da9854653492b1327a23a4dbd0a10/assets/logo-small.svg"> <a href="https://all-contributors.js.org/docs/en/bot/usage">Add your contributions</a> </img> </td> </tr> </tfoot> </table> <!-- markdownlint-restore --> <!-- prettier-ignore-end --> <!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the all-contributors specification. Contributions of any kind welcome!