Mastering Bitcoin SV smart contract. Part I

Mikael Lazarev
4 min readAug 23, 2020

--

As a blockchain developer & co-founder of Saint-Peteresburgr Blockhain Developer Coummunity, I’m instersting in all blockchain technologies. In this article you could find information how to write smart contract for Bitcoin SV.

What is Bitcoin SV?

Bitcoin SV (Satoshi view) is a fork of orginial Bitcoin, which has unlimited storage for any transaction and cheap transactions.

How smart contracts works on Bitcoin SV

When we talk about smartcontracts the first thing is come to mind is Ethereum. However, Ethereum wasn't the first blockchain which offer smartcontracts, Bitcoin also has possibility to write smartcontracts using Script language.

Let’s start from basics. For understading how contract works, we have to understand a topic called UTXO — the core concept of storing data in Bitcoin:

What is UTXO?

There are two different data models: Ethereum uses address based transaction model and Bitcoin used UTXO. In address model (used in Ethereum) blockchain stores data as banks accounts. (pairs of address-values)

For UTXO (Unspent transactios outout) Each bitcoin transaction begins with coins used to balance the ledger. UTXOs are processed continuously and are responsible for beginning and ending each transaction. Confirmation of transaction results in the removal of spent coins from the UTXO database. But a record of the spent coins still exists on the ledger.

It means, that each bitcoin transactions have several inputs and several outputs.

How UTXO works. Image from https://medium.com/codechain/codechains-utxo-model-fc967901578d

To evaluate the amount of address, you have to compute all inputs of transaction.

Each bitcoin input & output has special code (written in Script language, more info: https://en.bitcoin.it/wiki/Script) which is executed by miners to proceed transactions. It’s a significant part, even if Alice sends money to Bobs, she adds a special script called Pay to Public Key Hash, and transfering money is a result of invoking this script.

The script returns a boolean value which proves that transaction is valid. If script returns false, it means that transaction is invalid and miner does not include it into block.

The most popular code is Pay to Public Key Hash. Lets consider it in details to understand the basic of Script (Here you could see Scrypt — high-level smart contract language for Bitcoin SV):

1 contract P2PKH {
2 Ripemd160 pubKeyHash;

3 public function unlock(Sig sig, PubKey pubKey) {
4 require(hash160(pubKey) == this.pubKeyHash);
5 require(checkSig(sig, pubKey));
6 }
7 }

The P2PKH script pattern contains a hashed public key (pubKeyHash — line 2). To solve this script, the owner of the hashed public key above needs to provide the original public key(line 4), along with a valid signature for it (line 5).

If any require returns false, the miner consider this transaction is invalid and doesn't add it to block.

Standard and Non Standard Scripts

In last many years the usage of script instructions was quite limited due to an idea or a restriction imposed or agreed upon for the acceptance of scripts by miners. This was done for many reasons which are beyond the scope of this book but that limited the possible use of most of the script functionality. This idea was called the standard and non-standard scripts and at some points only accepted scripts by miners was decided to be standard scripts. This does not mean that you cannot write non-standard scripts in your transaction but those transactions would be rejected by most of the miners making the usage invalid. So what are these standards? The code level details of this is present in functions IsStandard() and IsStandardTx() under policy.cpp

https://github.com/bitcoin-sv/bitcoin-sv/blob/master/src/policy/policy.cpp

Storing data

Bitcoin has an option to store data. After OP_RETURN (special opCode which tells virtual machine that the code is finished) you could store any data which miners would ingnored but add to block.

[ TX: ScriprtCode | Data1] => [ TX: ScriptCode | Data 2] => ….

So, each time script code could update our data based on some parameters andd save to the chain! Its amazing.

How to implement business logic

To make a real business application, we would create a contract which saves all data to itself. Smartcontract could also read, proceed this data and store it as result of transaction. From one’s perspective, smartcontract on Bitcoin SV works like sending money to itself with different script (it changes cause data changes) and could be considered as state machine.

[ Address 1: State 1] => [Address 1: State 2] =>… => [ Address 1: State N]

In other word, each transaction is send money to initial address, however, the state is changing (or not changing :) each time it’s invoked.

Gathering data

To get current state of data, we could find the last transaction, get the script and extract data from it. Of course, we could use Scrypt data primitives, but it has not arrays for example, and usually we need dynamic data size.

As you know, reading blockchain is very fast, and it’s why Bitcoin SV could become an interesting player for immutable data storage market.

How to implement this I’ll show you in next article.

Stay tuned!

--

--