Bitcoin – Script

Bitcoin has a nice concept called Bitcoin script. Before understanding it, let us answer the following questions when Alice (A) wants to transfer some bitcoins to Bob (B).

  • How will Bob claim that transaction?
  • How will Bob know that transaction is intendant for him and no one else other than Alice actually has initiated that transaction?
Address generation mechanism
Address generation mechanism

A and B are bitcoin addresses that are generated from the respective public keys of Alice and Bob. To transfer money, Alice needs to have Bob’s bitcoin address. Further, Alice signs the transaction using her private key and broadcasts the tuple that contains transaction information, public key, and signature of Alice in the bitcoin network.

Transfer of credential - bitcoin
Transfer of transaction record from Alice to Bob via the bitcoin network

Bob receives the tuple from the bitcoin network as it was intended for him. He verifies the transaction by validating the signature, which was generated by Alice using her private key. After verifying the signature using the public key of Alice. Bob also verifies Alice’s bitcoin address and the public key of Alice. After the entire sanity checking, Bob will accept the transaction.

Terminologies used in Bitcoin for Transactions

Every transaction is characterized by two parameters such as output and input. The output tells about sending of bitcoin from A to B, and the input tells about receiving of bitcoin from B to A.  Example:

  • Alice sends some bitcoins: The output (out) of the transaction.
  • Bob receives some bitcoins: The input (in) of the transaction.
Bitcoin output and input transaction parameters

It can be represented in the form of series of inputs and outputs. Alice has received 100 (bitcoin) BTC in the above diagram, and later, she transfers 40 BTC to Bob and 60 BTC to Charley, and so on. So this way, the generated bitcoins in the network move through multiple parties with different transactions. Every transaction is characterized by one output and one input and can be validated to have corresponding output and input pair. 

Bitcoin Script

Bitcoin indeed transfers scripts instead of the signature and the public key. It uses two scripts. 

  • Scriptsig: It contains the public key and signature of the sender.
  • ScriptPubKey: It contains operations code, sender bitcoin address, and other data.
Bitcoin script
Bitcoin scrip format

Bitcoin script follows a Forth-like programming language to validate Bitcoin transactions. A list of instructions is recorded with each transaction and describes how the next person can access the bitcoins if they want to spend.

How Forth works – It is a stacked-based computer programming language originally designed by Charles Moore. A procedural programming language without type checking. It uses a stack for recursive subroutine execution and uses reverse polish notation (RPN) or postfix notation. It does not support looping, which may to beneficial to avoid many kinds of attacks.

Example code in forth as compare to C code

Example code in forth as compare to C code

Summary

We have seen what a bitcoin script is, how to generate public identity for bitcoin users, how bitcoin scripts work, and the internals of bitcoin script.

References:

  • NPTEL lecture series on Blockchains Architecture, Design and Use Cases by Prof. Sandip Chakraborty, IIT Kharagpur.

 273 total views,  1 views today

Scroll to Top
Scroll to Top