bitcoinda

package module
v0.0.0-...-96e7f14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

bitcoin-da:

This package provides a reader / writer interface to bitcoin.

Example:

// ExampleRelayer_Read tests that reading data from the blockchain works as
// expected.
func ExampleRelayer_Read() {
	// Example usage
	relayer, err := bitcoinda.NewRelayer(bitcoinda.Config{
		Host:         "localhost:18332",
		User:         "rpcuser",
		Pass:         "rpcpass",
		HTTPPostMode: true,
		DisableTLS:   true,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	_, err = relayer.Write([]byte("rollkit-btc: gm"))
	if err != nil {
		fmt.Println(err)
		return
	}
	// TODO: either mock or generate block
	// We're assuming the prev tx was mined at height 146
	height := uint64(146)
	blobs, err := relayer.Read(height)
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, blob := range blobs {
		got, err := hex.DecodeString(fmt.Sprintf("%x", blob))
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println(string(got))
	}
	// Output: rollkit-btc: gm
}

Tests:

Running the tests requires a local regtest node.

bitcoind -chain=regtest -rpcport=18332 -rpcuser=rpcuser -rpcpassword=rpcpass -fallbackfee=0.000001 -txindex=1

bitcoin-cli -regtest -rpcport=18332 -rpcuser=rpcuser -rpcpassword=rpcpass createwallet w1

export COINBASE=$(bitcoin-cli -regtest -rpcport=18332 -rpcuser=rpcuser -rpcpassword=rpcpass getnewaddress)

bitcoin-cli -regtest -rpcport=18332 -rpcuser=rpcuser -rpcpassword=rpcpass generatetoaddress 101 $COINBASE

Idle for a while till coinbase coins mature.

=== RUN   ExampleRelayer_Write
--- PASS: ExampleRelayer_Write (0.13s)
=== RUN   ExampleRelayer_Read
--- PASS: ExampleRelayer_Read (0.10s)
PASS
ok      github.com/rollkit/bitcoin-da   0.375s

Writer:

A commit transaction containing a taproot with one leaf script

OP_FALSE
OP_IF
  "roll" marker
  <embedded data>
OP_ENDIF
<pubkey>
OP_CHECKSIG

is used to create a new bech32m address and is sent an output.

A reveal transaction then posts the embedded data on chain and spends the commit output.

Reader:

The address of the reveal transaction is implicity used as a namespace.

Clients may call listunspent on the reveal transaction address to get a list of transactions and read the embedded data from the first witness input.

Spec:

For more details, read the spec

Documentation

Index

Examples

Constants

View Source
const DefaultMaxBytes = 1048576

Variables

View Source
var PROTOCOL_ID = []byte{0x72, 0x6f, 0x6c, 0x6c}

PROTOCOL_ID allows data identification by looking at the first few bytes

Functions

func ExtractPushData

func ExtractPushData(version uint16, pkScript []byte) ([]byte, error)

Types

type BitcoinDA

type BitcoinDA struct {
	// contains filtered or unexported fields
}

func NewBitcoinDA

func NewBitcoinDA(relayer *Relayer) *BitcoinDA

func (*BitcoinDA) Commit

func (b *BitcoinDA) Commit(ctx context.Context, daBlobs []da.Blob, ns da.Namespace) ([]da.Commitment, error)

Commit creates a Commitment for each given Blob.

func (*BitcoinDA) Get

func (b *BitcoinDA) Get(ctx context.Context, ids []da.ID, ns da.Namespace) ([]da.Blob, error)

Get returns Blob for each given ID, or an error.

func (*BitcoinDA) GetIDs

func (b *BitcoinDA) GetIDs(ctx context.Context, height uint64, ns da.Namespace) ([]da.ID, error)

GetIDs returns IDs of all Blobs located in DA at given height.

func (*BitcoinDA) GetProofs

func (b *BitcoinDA) GetProofs(ctx context.Context, daIDs []da.ID, ns da.Namespace) ([]da.Proof, error)

GetProofs returns the inclusion proofs for the given IDs.

func (*BitcoinDA) MaxBlobSize

func (b *BitcoinDA) MaxBlobSize(ctx context.Context) (uint64, error)

MaxBlobSize returns the max blob size

func (*BitcoinDA) Submit

func (b *BitcoinDA) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64, ns da.Namespace) ([]da.ID, error)

Submit submits the Blobs to Data Availability layer.

func (*BitcoinDA) Validate

func (b *BitcoinDA) Validate(ctx context.Context, ids []da.ID, daProofs []da.Proof, ns da.Namespace) ([]bool, error)

Validate validates Commitments against the corresponding Proofs. This should be possible without retrieving the Blobs.

type Config

type Config struct {
	Host         string
	User         string
	Pass         string
	HTTPPostMode bool
	DisableTLS   bool
}

type Relayer

type Relayer struct {
	// contains filtered or unexported fields
}

Relayer is a bitcoin client wrapper which provides reader and writer methods to write binary blobs to the blockchain.

func NewRelayer

func NewRelayer(config Config) (*Relayer, error)

NewRelayer returns a new relayer. It can error if there's an RPC connection error with the connection config.

func (Relayer) Read

func (r Relayer) Read(height uint64) ([][]byte, error)
Example

ExampleRelayer_Read tests that reading data from the blockchain works as expected.

package main

import (
	"encoding/hex"
	"fmt"

	bitcoinda "github.com/rollkit/bitcoin-da"
)

func main() {
	// Example usage
	relayer, err := bitcoinda.NewRelayer(bitcoinda.Config{
		Host:         "localhost:18332",
		User:         "rpcuser",
		Pass:         "rpcpass",
		HTTPPostMode: true,
		DisableTLS:   true,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	_, err = relayer.Write([]byte("rollkit-btc: gm"))
	if err != nil {
		fmt.Println(err)
		return
	}
	// TODO: either mock or generate block
	// We're assuming the prev tx was mined at height 146
	height := uint64(146)
	blobs, err := relayer.Read(height)
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, blob := range blobs {
		got, err := hex.DecodeString(fmt.Sprintf("%x", blob))
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println(string(got))
	}
}
Output:

rollkit-btc: gm

func (Relayer) ReadTransaction

func (r Relayer) ReadTransaction(hash *chainhash.Hash) ([]byte, error)

func (Relayer) Write

func (r Relayer) Write(data []byte) (*chainhash.Hash, error)
Example

ExampleRelayer_Write tests that writing data to the blockchain works as expected.

package main

import (
	"fmt"

	bitcoinda "github.com/rollkit/bitcoin-da"
)

func main() {
	// Example usage
	relayer, err := bitcoinda.NewRelayer(bitcoinda.Config{
		Host:         "localhost:18332",
		User:         "rpcuser",
		Pass:         "rpcpass",
		HTTPPostMode: true,
		DisableTLS:   true,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Writing...")
	_, err = relayer.Write([]byte("rollkit-btc: gm"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("done")
}
Output:

Writing...
done

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL