Coder Social home page Coder Social logo

kasarlabs / bitcoin-da Goto Github PK

View Code? Open in Web Editor NEW
37.0 3.0 4.0 333 KB

Bitcoin data-availability adapter written in Rust, allowing Madara to interact with Bitcoin as a data-availability layer. πŸ¦€πŸ§™β€β™‚οΈ

Home Page: https://kasar.io

License: Apache License 2.0

Rust 97.33% Shell 2.67%

bitcoin-da's Introduction

πŸ§™β€β™‚οΈ Bitcoin-da-rs

This crate allows Bitcoin to function as a data availability layer, supporting both write and read operations. It's been developed with Rust sequencers in mind, particularly for integration with Madara. It's modeled after rollkit's bitcoin-da.

⚠️ Disclaimer: The code is currently in its experimental phase and is not recommended for production use.

Prerequisites

Before you can proceed with using or testing this crate, there are a few setup steps you need to follow:

  • Installation: Ensure bitcoind & bitcoin-cli are installed.

  • Setting up a Bitcoin Daemon:

    • Start a bitcoin daemon with:

      bitcoind -conf=path_to/bitcoin.conf
    • If you're setting up for the first time and don't have a wallet yet:

      bitcoin-cli createwallet test
    • Mine some blocks (especially useful for regtest):

      bitcoin-cli -regtest -generate 150

Note: These steps are for both manual and automatic testing. For both cases you will still need to start bitcoind separately. Additionally, automated tests will only handle some bitcoin-cli operations such as loadwallet and setting the test label for the wallet in use.

Building the Library

git clone [email protected]:KasarLabs/bitcoin-da.git
cd da
cargo build

For more detailed instructions on building Rust projects, refer to the cargo documentation.

Example Usage

Here's a simple example to demonstrate how to use the library:

fn test_write() {
        let embedded_data = b"Hello, world!";
        let relayer = Relayer::new(&Config::new(
            "localhost:8332".to_owned(),
            "rpcuser".to_owned(),
            "rpcpass".to_owned(),
        ))
        .unwrap();
        // get network, should be regtest
        let blockchain_info = relayer.client.get_blockchain_info().unwrap();
        let network_name = &blockchain_info.chain;
        let network = Network::from_core_arg(network_name)
            .map_err(|_| BitcoinError::InvalidNetwork)
            .unwrap();
        match relayer.write(&embedded_data) {
            Ok(txid) => {
                println!("Txid: {}", txid);
                println!("Successful write");
            }
            Err(e) => panic!("Write failed with error: {:?}", e),
        }
    }

Automated Testing with the Provided Script

This repo comes with an automation script (run_tests.sh) which simplifies the testing process by handling node operations and test configurations. The script assumes a bitcoin node is already running.

Preparations:

  • Grant the script execute permissions:
    chmod +x run_tests.sh
  1. Setup: Ensure a Bitcoin node is running on the desired network (regtest or signet), and modify the RPC URL in your configuration as necessary. Depending on the test, you might also need to comment/uncomment the required network in the test function.

  2. Command Usage:

./run_tests.sh -l [log level] -b [backtrace] -t [test name] -L --signet|--regtest
  • -l or --log-level: Specify the log level. Valid options are info, debug, or none.
  • -b or --backtrace: Enable (1) or Disable (0) backtrace.
  • -t or --test-name: Specify a test name (optional).
  • -L or --long-tests: Include tests that take a long time to complete in signet due to the time it takes to complete a block. These tests run fast in regtest.
  • --signet: Use the signet network.
  • --regtest: Use the regtest network.

Note: there should be no quotations on any of the argument flags when used.

Examples:

Run all tests with debug logs and backtrace:

./run_tests.sh -l debug -b 1

Run the test_example with info logs:

./run_tests.sh -l info -t test_example

Manual Tests

If you prefer manual testing:

  1. Setup: Ensure a Bitcoin node is running on the desired network (regtest or signet), and modify the RPC URL in your configuration as necessary. Depending on the test, you might also need to comment/uncomment the required network in the test function.

  2. Running Tests:

  • regtest:

    cargo test --features regtest
  • Signet:

    cargo test --features signet
  • With Logs:

    RUST_LOG=debug cargo test
  • Logs + Backtrace:

    RUST_LOG=debug RUST_BACKTRACE=1 cargo test --features regtest
  • Logs + Backtrace + long tests: RUST_LOG=debug RUST_BACKTRACE=1 cargo test --features regtest,long_tests

License

This project is under the Apache 2.0 license. Detailed information can be found in LICENSE.

Contributors ✨

Collaborative work by Kasar and Taproot Wizards πŸ§™β€β™‚οΈ.

Antoine
Antoine

πŸ’»
Antiyro
Antiyro

πŸ’»
Betacod
Betacod

πŸ’»
Sparqet
Sparqet

πŸ’»
Axel Izsak
Axel Izsak

πŸ’»
Zarboq
Zarboq

πŸ’»

bitcoin-da's People

Contributors

0xeniotna avatar antiyro avatar sparqet avatar eytanlvy avatar solarsailorneo avatar zarboq avatar

Stargazers

Frances He avatar  avatar Lasse Herskind avatar  avatar Starkience avatar 22388o⚑️  avatar  avatar Oflow Show avatar Oak avatar tedison avatar  avatar  avatar Francesco 'makevoid' Canessa avatar  avatar Mayur Chougule avatar Marcel Hintermann avatar Algorithm avatar Victor Afanassieff avatar Bakuchi avatar ben2077 avatar  avatar 0xKubitus avatar Dylan LeClair avatar Ming avatar Rajiv Patel-O'Connor avatar Đ avatar StarkNet δΈ­ζ–‡ | China avatar Thomas Butler avatar Stone avatar Chen Kai avatar Javed Khan avatar V.O.T avatar  avatar  avatar  avatar  avatar Axel Izsak avatar

Watchers

 avatar  avatar CipherSync avatar

bitcoin-da's Issues

lib/wire

Find or create Rust equivalents for the following:

  wire.TxIn
  wire.OutPoint
  wire.TxOut
  wire.TxWitness
  wire.NewMsgTx

add/write

The Write function writes data to the blockchain. The conversion must handle data preparation, including appending protocol IDs, creating Taproot addresses, and committing and revealing transactions.

da/clean_repo

There are things that can be removed/need to be changed in bitcoin-da repo:

  • in the config struct there are useless fields
  • relayer::new_relayer should be relayer::new
  • Relayer.client should be pub in Relayer struct

feat/improve_script

There might be an issue with the script used to inscribe data.
I need to investigate a biit more

add/read

The goal of this issue is to recode read rollkit function in Rust. ReadTransaction and Read functions are responsible for reading data from Bitcoin. The logic needs to be translated with attention to how transactions and blocks are structured on Bitcoin depending on Rust availabilities.

add/setup_project

The goal of this issue is to setup the minimal Rust interface to kick-start the project. This means creating an infrastructure similar to rollkit/bitcoin-da, the necessary functions, files, implementations etc

add/read_transaction

The goal of this issue is to recode ReadTransaction rollkit function in Rust. read_transaction and read functions are responsible for reading data from Bitcoin. The logic needs to be translated with attention to how transactions and blocks are structured on Bitcoin depending on Rust availabilities.

add/extract_push_data

The goal of this issue is to recode ExtractPushData function from rollkit/bitcoin-da in Rust. The ExtractPushData function extracts push data from a given script, using a specific template matching logic.

add/reveal_tx

The goal of this issue is to recode revealTx function from rollkit/bitcoin-da in Rust. The reveal_tx function reveals embedded data on the chain as part of the script satisfying the tapscript spend path. This is a complex Bitcoin script logic that will need careful conversion

(still tbd)

Add rollup block height

Right now, we simply include the statediff but the script also expects the block height.
Not high priority but still a TODO.

The blockheight should be appended (in Madara) at the beginning of the data, extracted in bitcoin-da (write or build_script) and added to the script at the appropriate line.

lib/bytes-fmt

Replace bytes and fmt imports. The current implementation relies on the following:

bytes.HasPrefix
fmt.Errorf

These need to be replaced with equivalent Rust functionality.

lib/btcutil

Find or create Rust equivalents for the following:

btcutil.DecodeWIF
btcutil.NewAddressTaproot
btcutil.DecodeAddress
btcutil.NewAmount

add/error_handling

The goal of this issue is to handle errors in Rust format. Go uses explicit error handling, while Rust uses the Result and Option types. You will need to convert error handling accordingly. PLEASE keep it no-std compatible.

lib/btcec-v2

Repleace btece-v2 imports in Rust. The current implementation uses btcec.PublicKey and schnorr.SerializePubKey. Find or create Rust equivalents for these functions.

add/commit_tx

The goal of this issue is to recode commitTx function from rollkit/bitcoin-da in Rust. The commit_tx function commits a transaction to a given Taproot address. This logic must be properly translated into Rust, ensuring that the Bitcoin protocol's requirements are met.

add/create_taproot_address

The goal of this issue is to recode createTaprootAddress function from rollkit/bitcoin-da in Rust. The create_taproot_address function creates a Taproot address using specific Bitcoin scripting logic.
(Not explored if you find relevent data concerning TaprootAddress please comment it below)

Interval private key management

Right now, the internal private key is a constant in the code. It is used to derive the taproot address. We need to find a way to keep it safe somewhere.

Should maybe be randomized or added in a .env

add/new_relayer

The goal of this issue is to recode newRelayer function from rollkit/bitcoin-da in Rust. The new_relayer function initializes a connection to the Bitcoin node using specific connection configuration. You will need to find a Rust library or write code that supports Bitcoin's RPC protocol and implement it.

lib/rpcclient

Find or create Rust equivalents for the following:

rpcclient.Client
rpcclient.New
rpcclient.ConnConfig

feat/hardcoded_fees

In the crate, fees to write data in the blockchain are hardcoded. We should find a way to do it in a smoother way.
In crazy market conditions, we could have bad suprises.

test/bitcoin-da

Tests are:
#35 relayer funcs - new/close
#36 write and what is inside create_address, commit, reveal
#37 readand related functions

The goal is to write unit tests for each functions.
Decomposing every tests is easier to debug.

Would be great to have a separated test mod

add/keys_and_serializations

The Go code uses specific methods for handling private/public keys and their serialization. You will need to find equivalent methods in Rust or write custom logic.

Additional infos:

Converting Bitcoin keys and serialization involves handling private and public keys and working with Bitcoin's specific formats like WIF and Taproot. The private keys are decoded into Wallet Import Format, while the public keys utilize the Schnorr signature scheme
Specific libraries are used for these functions, such as those provided by btcsuite, for handling elliptic curve operations, Schnorr signatures, and Bitcoin transaction scripts.

Please comunicate with the linked pull request solver for the btcsuites Rust imports. If you have additional infos coment them below.

lib/txscript

Find or create Rust equivalents for the following:

All listed functions and constants related to txscript.

  txscript.OP_0
  txscript.OP_IF
  txscript.OP_ENDIF
  txscript.OP_CHECKSIG
  txscript.NewBaseTapLeaf
  txscript.AssembleTaprootScriptTree
  txscript.ComputeTaprootOutputKey
  txscript.NewCannedPrevOutputFetcher
  txscript.NewTxSigHashes
  txscript.RawTxInTapscriptSignature
  txscript.SigHashDefault
  txscript.OP_1
  txscript.OP_FALSE

add/librairy_availability

The Rollkit/bitcoin-da implementation relies on specific Bitcoin libraries. You will need to find equivalent Rust libraries or implement certain functionalities manually to abstract any of the imports used from rollkit. Most of them are already integrated into https://github.com/rust-bitcoin/rust-bitcoin. For example:

btcutil.DecodeWIF(bobPrivateKey)

Could be converted into:

PrivateKey::from_wif(bobPrivateKey).unwrap()

From Rust-Bitcoin library

List of imports that needs equivalents:

  • bytes

    • bytes.HasPrefix
  • fmt

    • fmt.Errorf
  • github.com/btcsuite/btcd/btcec/v2

    • btcec.PublicKey
  • github.com/btcsuite/btcd/btcec/v2/schnorr

    • schnorr.SerializePubKey
  • github.com/btcsuite/btcd/btcutil

    • btcutil.DecodeWIF
    • btcutil.NewAddressTaproot
    • btcutil.DecodeAddress
    • btcutil.NewAmount
  • github.com/btcsuite/btcd/chaincfg

    • chaincfg.RegressionNetParams
  • github.com/btcsuite/btcd/chaincfg/chainhash

    • chainhash.Hash
  • github.com/btcsuite/btcd/rpcclient

    • rpcclient.Client
    • rpcclient.New
    • rpcclient.ConnConfig
  • github.com/btcsuite/btcd/txscript

    • txscript.NewScriptBuilder
    • txscript.OP_0
    • txscript.OP_IF
    • txscript.OP_ENDIF
    • txscript.OP_CHECKSIG
    • txscript.NewBaseTapLeaf
    • txscript.AssembleTaprootScriptTree
    • txscript.ComputeTaprootOutputKey
    • txscript.NewCannedPrevOutputFetcher
    • txscript.NewTxSigHashes
    • txscript.RawTxInTapscriptSignature
    • txscript.SigHashDefault
    • txscript.OP_1
    • txscript.OP_FALSE
  • github.com/btcsuite/btcd/wire

    • wire.TxIn
    • wire.OutPoint
    • wire.TxOut
    • wire.TxWitness
    • wire.NewMsgTx

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.