It is possible to write to the blockchain of bitcoin not only financial transactions, but also practically any data. The data recorded in the Bitcoin blockchain becomes public and it is almost impossible to delete or change it. This feature can be used, for example, to create decentralized sites that are not amenable to any blocking.
Using the blockchaindata-lib library, writing and reading data on the blockchain can be organized in just a few lines of code.
Instruments
The blockchaindata-lib library is written in node.js. Source code with examples is available at
github.com/3s3s/blockchaindata-lib github. To add a library to your project, just run one command in the console
npm install blockchaindata-lib
To work with the blockchain, you need to configure RPC access to the wallet of Bitcoin or a fork compatible with Bitcoin (Litecoin, Dogecoin, etc.). By default, blockchaindata-lib will try to access the RPC server at the following details:
: http://127.0.0.1:18332 : rpc_btc_test : rpc_btc_password_test
Defaults can be changed by library function
updateNetwork(url, user, password)
Thus, in order to work with the library, the first thing you need to do is run a Bitcoin test wallet:
- Download a Bitcoin wallet (or compatible fork.)
- Create ~ / .bitcoin / bitcoin.conf file (or in windows% APPDATA% / Bitcoin / bitcoin.conf)
- In the bitcoin.conf file, write the following settings
testnet=1 server=1 rpcbind=127.0.0.1 rpcallowip=127.0.0.1 rpcuser=rpc_btc_test rpcpassword=rpc_btc_password_test txindex=1
- Save the changes to bitcoin.conf and run bitcoind (or in windows bitcoin-qt.exe)
- Wait for blockchain synchronization
With the above settings, the wallet will launch in the Bitcoin test network.
Data recording
Bitcoin prohibits transactions in which there are no expenses. Therefore, before you write something to the Bitcoin blockchain, you will definitely have to replenish your wallet. Bitcoins for a test network can be obtained for free if you search on Google for something like βbitcoin testnet faucetβ. When the wallet is replenished, you can finally record data.
The library has several functions for recording data:
SaveTextToBlockchain( dataString ) SaveJSONToBlockchain( objectJSON ) SaveFileToBlockchain( data )
In principle, what these functions do is clear from the name. Function Result - Object
{result: <true | false>, message: <string>, txid: <string>}
Consider a usage example:
'use strict'; const blockchaindata = require('blockchaindata-lib') async function test1() { try {
If this code is executed without errors, the transaction hash will be displayed in the console. You can read the data from this hash.
Reading data
In order to read the data recorded earlier in the blockchain, you only need to know the hash of the transaction. The wallet may be empty, that is, if you only want to read the data, then replenishing the wallet is not necessary.
Code example
'use strict'; const blockchaindata = require('blockchaindata-lib'); async function test2() {
If this code works without errors, then the html text of the page that I previously saved on the blockchain will be displayed in the console.
Where to apply?
In the blockchain, for example, you can save a static site. This site will be virtually impossible to delete or somehow block. However, to read such a site, you will need additional software: a new browser or browser extension.
The browser extension is the easiest solution. An example of such an extension can be found here
github.com/3s3s/blockchaindata
You can download and install this extension for Firefox here:
yadi.sk/d/a3xM9BCepP4nBw
After installation, Firefox will intercept and process links to sites in the format
http: // tbtc / <txid>
For example, such a link will work
http: // tbtc / 8af6633160b982a0b0b4d4962ad28e0d5b3dd97e05e27cc2dd64ec0c56820df5
Efficiency
The blockchaindata-lib library compresses data before placing it on the blockchain using the deflate algorithm, therefore this method is especially effective for writing text documents. During testing, I managed to write up to 70 kb of compressed data to the blockchain. Although there are no restrictions on the size of incoming data, errors on large data sets appear due to the internal implementation of bitcoin code.
When writing data to the blockchain, one must also consider the financial component. Reading and writing data to the Bitcoin test network using the blockchaindata-lib library is simple and free. However, if you want to write data to the working network of bitcoin, then you will need to take into account the non-zero price of bitcoin.