Store data on Bitcoin for 350 sats/KB up to 185 KB by using P2SH-P2WSH witness scripts

Bitcandle

Store data on Bitcoin for 350 sats/KB up to 185 kB by using P2SH-P2WSH witness scripts.

225ed8bc432d37cf434f80717286fd5671f676f12b573294db72a2a8f9b1e7ba

Disclaimer

Storing data on Bitcoin is a controversial subject as it bloats the blockchain with somewhat unnecessary data.
I built this tool to show that there exists an efficient method of storing data: P2SH-P2WSH, lesser known than standard P2PKH / P2PK / OP_RETURN methods.

Cost

Before injecting data we need to create P2SH-P2WSH UTXOs. This can be done in a single transaction, sent by the user, by sending coins to many outputs.
To spend the P2SH-P2WSH UTXOs (injecting data), it costs roughly 0.00000349 BTC per KB. (1 sat/B fee rate).
This is about 84 times cheaper than the P2PKH injection method.

It is obviously free to retrieve data.

Usage

Usage:
  bitcandle [flags]
  bitcandle [command]

Available Commands:
  help        Help about any command
  inject      Inject a file on the Bitcoin network
  retrieve    Retrieve a file on the Bitcoin network

Flags:
  -h, --help   help for bitcandle

Use "bitcandle [command] --help" for more information about a command.

Example

Inject data

$ ./bitcandle inject \
    --fee 1 \
    --file ./image.jpg \
    --network mainnet \
    --change-address bc1q8sl9tnvnuc8z7q80u9wffdf9ugt4arrp6vlamg

✔ Loaded 4556 bytes to inject.
✔ Loaded existing private key.
✔ Connected to electrum server (blockstream.info:110).
ℹ Estimated injection cost: 0.00002176 BTC.
ℹ You must send 0.00002176 BTC to 33z4X8jkMd8WCzhrfgEigzgLyrap1ACWUE.
█████████████████████████████████████████
█████████████████████████████████████████
████ ▄▄▄▄▄ ██▀▄███▀  ▀▄ ▀ ▄ ▀█ ▄▄▄▄▄ ████
████ █   █ █▄▀█▄▀▀▄ ▀█▀▄▀█▀▀██ █   █ ████
████ █▄▄▄█ ██▄▀▀ ▄       █ ▀▀█ █▄▄▄█ ████
████▄▄▄▄▄▄▄█ █▄▀▄█▄▀▄▀ ▀ ▀ █ █▄▄▄▄▄▄▄████
████▄ ▄▀  ▄  ▀█▀ ▀███▄█▀ ▄▀█   ███ ▀▀████
████ ▀ ▄ █▄█ ▀ ▄ ▀ █▄ ▀▀▀ ▄█ █▀▀▀▄█▄ ████
████▄▄▄▀██▄▀ █  ██ ██ ▀▀▀ ██  ▄██▀▀▀ ████
█████▀ ▀▀▄▄▀ ▄▀▀█▄█▀▄▀▀█▀ ▀█▄▄█▀ ▄▄ ▄████
████▄▄ █▄▀▄▀▀█ ▀ ▀███▄█▀▀███▀   ██▀▄ ████
████▄▀▀▄ ▀▄███▀▄ ▀ ▀▄ ███▀█▀▄ ▄▀ ▀█▄▄████
████▄▄▄▀ ▀▄▀█▀█ ██ ██ ▀▀ ▄▀▄ █ ▄▄ ▀▀▀████
████▄▀▀▄ ▄▄▀  █▀█▄█▀▄▀█▀█▀ █▄▀██▄▀█  ████
████▄█▄███▄▄ █▄▀ ▀███▄ ▀  ▀▄ ▄▄▄  █▀ ████
████ ▄▄▄▄▄ █ ██▄ ▀ ▀▄▀▀▀█ ▄▄ █▄█  █ ▄████
████ █   █ █▄   ██ ██  ██ ██ ▄▄▄  █▄▀████
████ █▄▄▄█ █ █▀▀█▄██ ▀▄▀▄▀ ▀▄▄▄▀ ▄█▀▄████
████▄▄▄▄▄▄▄█▄█▄█▄██▄▄▄▄██▄█▄▄▄███▄█▄▄████
█████████████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
✔ Payment received. (1/1)
✔ All payments received.
✔ Data injected.
ℹ TxID: 225ed8bc432d37cf434f80717286fd5671f676f12b573294db72a2a8f9b1e7ba

Retrieve data

$ ./bitcandle retrieve \
    --tx 225ed8bc432d37cf434f80717286fd5671f676f12b573294db72a2a8f9b1e7ba \
    --network mainnet \
    -o /tmp/image.jpg

✔ Connected to electrum server (blockstream.info:110).
✔ Retrieved file.
✔ Saved file to "/tmp/image.jpg".

Docker

$ mkdir data
$ cp ~/Downloads/image.jpg ./data/
$ docker run -it --rm -v $PWD/data:/data aureleoules/bitcandle inject -f ./image.jpg [args]
...
$ docker run -it --rm -v $PWD/data:/data aureleoules/bitcandle retrieve [args]
...

Working example

$ docker run -it --rm -v $PWD/data:/data aureleoules/bitcandle \
    retrieve \
    --tx 225ed8bc432d37cf434f80717286fd5671f676f12b573294db72a2a8f9b1e7ba \
    -o /data/image.jpg

How it works

This tool uses witness scripts in P2SH-P2WSH inputs to store data.
Instead of storing data in the transaction outputs like standard approaches such as P2PKH, P2PK or OP_RETURN, this tool stores data in the transaction inputs (witness).

Witness script

We need to create a new P2SH-P2WSH UTXO. To do that we need to create a witness script.
The script needs to look like this:

  • OP_HASH160
  • OP_PUSHDATA [CHUNK 3 HASH]
  • OP_EQUALVERIFY
  • OP_HASH160
  • OP_PUSHDATA [CHUNK 2 HASH]
  • OP_EQUALVERIFY
  • OP_HASH160
  • OP_PUSHDATA [CHUNK 1 HASH]
  • OP_EQUALVERIFY
  • OP_PUSHDATA [PUBKEY]
  • OP_CHECKSIG

This witness script is hashed and wrapped in a P2SH-P2WSH output script to create a P2SH-P2WSH address such as: 3N9fEcf9yUSspvUc78cQQVJDQi5NkgrHtLQ.

The user must send enough funds to this address so that this UTXO can be spent.

Hashes of chunks are pushed on the stack in order to ensure data integrity.
Once we spend this UTXO, at attacker could scramble chunks of data and the transaction would this be valid if these op codes were not added.

We must also add the PUBKEY and the CHECKSIG op code so that transactions outputs are signed. This prevents attackers from redirecting the output change to another change address. This may not be necessary for small change amounts (minimum on mainnet is 546 sats) but it is recommended as it makes sure the transaction id does not change while the transaction is in the mempool.

This witness script ensures data integrity and prevents output sniping.

Witness data

In order to spend the UTXO and essentially store the file, we must include witness data that unlocks the witness script built previously.
It looks something like this:

  • [SIG]
  • [CHUNK 1]
  • [CHUNK 2]
  • [CHUNK 3]
  • [witness script hex]

This is not a script, this is simply a stack of data. It can only fit 99 chunks of 80 bytes of data.
For larger files, multiple UTXOs must be created using different witness scripts.

Notes

Witness scripts are built deterministically such that for the same file and same public key, the P2SH-P2WSH addresses will remain the same. This may help easily retrieving any stuck funds if needed.
Not a single satoshi is burned in the data injection process. This is a clear advantage compared to other known injection methods like P2PKH. All the fees go back to miners and the change is sent to the address specified.

Use cases

This software can be useful to

  • store censorship resistant documents
  • store pictures of loved ones we wish to remember forever
  • store documents publicly and permanently accessible

License

MIT - Aurèle Oulès

Owner
Aurèle Oulès
i build things on the internet
Aurèle Oulès
Similar Resources

Bitcoin Core integration/staging tree

Bitcoin Core integration/staging tree https://bitcoincore.org For an immediately usable, binary version of the Bitcoin Core software, see https://bitc

Dec 30, 2022

Mastering Bitcoin 2nd Edition - Programming the Open Blockchain

Code Examples: Mastering Bitcoin Mastering Bitcoin is a book for developers, although the first two chapters cover bitcoin at a level that is also app

Jan 1, 2023

Bitcoin Improvement Proposals

People wishing to submit BIPs, first should propose their idea or document to the [email protected] mailing list (do not assign a

Jan 2, 2023

Full bitcoin solution written in Go (golang)

Full bitcoin solution written in Go (golang)

About Gocoin Gocoin is a full Bitcoin solution written in Go language (golang). The software architecture is focused on maximum performance of the nod

Dec 20, 2022

A simple, concurrent bitcoin miner framework implemented in Go.

Bitcoin Miner A simple, concurrent bitcoin miner framework implemented in Go. Disclaimer: this is not a product intended to be used for real mining, s

Dec 29, 2022

Bitcoin address balance checker on steroids.

BTCSteroids Bitcoin address balance checker on steroids. Table of contents Quick start What's included Use Cases Thanks Copyright and license Quick st

Dec 12, 2022

Bitcoin UTXO & xPub Management Suite

Bitcoin UTXO & xPub Management Suite

BUX Bitcoin UTXO & xPub Management Suite Table of Contents About Installation Documentation Examples & Tests Benchmarks Code Standards Usage Contribut

Dec 19, 2022

Cross commerce challenge - Cross Commerce Store Challenge With Golang

Cross commerce challenge - Cross Commerce Store Challenge With Golang

Cross Commerce Store Desafio Simples Aplicação ETL Todo o código fonte está cont

Feb 13, 2022

Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

Jan 9, 2023
Btc-globe - Visualize Bitcoin node locations using golang
Btc-globe - Visualize Bitcoin node locations using golang

btc-globe Visualize Bitcoin nodes by location using Golang

Jan 19, 2022
A full node Bitcoin (BSV) implementation written in Go

bsvd bsvd is a full node Bitcoin (BSV) implementation written in Go (golang). This project is a port of the bchd codebase to Bitcoin (BSV). It provide

Dec 25, 2022
A db for bitcoin-sv & BTC

Welcome to go-svdb Project =========== Boquan Team The Boquan is a team dedicated to promoting and developing true bitcoin. The team has successfully

Sep 3, 2021
Moeing chain is an EVM&Web3 compatible sidechain for Bitcoin Cash

Full node client of smartBCH This repository contains the code of the full node client of smartBCH, an EVM&Web3 compatible sidechain for Bitcoin Cash.

Nov 29, 2022
A curated Golang toolkit for creating Bitcoin SV powered apps
A curated Golang toolkit for creating Bitcoin SV powered apps

bsv A curated Golang toolkit for creating Bitcoin SV powered apps Table of Contents Installation Maintainers License Installation bsv requires a suppo

May 10, 2022
A work-in-progress Bitcoin wallet based on Output Descriptors

go-wallet A work-in-progress Bitcoin wallet Descriptors go-wallet is designed around Bitcoin Descriptors. It implements a Output Script Descriptors la

May 4, 2022
The go-to Bitcoin Node (BN) Go library.

go-bitcoin Go wrapper for bitcoin RPC RPC services Start by creating a connection to a bitcoin node b, err := New("rcp host", rpc port, "rpc usernam

Feb 13, 2022
Bitcoin CPU miner written in Go.

CPU Miner Bitcoin CPU miner written in Go. Introduction This is a CPU miner written in Go. It is a proof of concept and is not intended for production

Dec 29, 2022
A fully validating Bitcoin node with Utreexo support

btcd btcd is an alternative full node bitcoin implementation written in Go (golang). This project is currently under active development and is in a Be

Dec 21, 2022
Bitcoin futures curve from Deribit as a JSON webservice

Curve Bitcoin futures curve from Deribit as a JSON webservice Building go build . Running ./curve Expiration date and annualised yield of each contr

Dec 13, 2021