An introduction to Bitcoin Script

Bitcoin created a lot of changes in the world of economics. Money that is not fully traceable and takes a lot of possibilities to your customer, is one of the characteristics of this currency and other cryptocurrencies. If Blockchain is a bank fund, consider a digital wallet and Coin as a check, depositing Coin inside the wallet is the same as opening your bank fund account and see its inventory. The only one who has its own key can see the amount of inventory.

Also, the Bitcoin Wallet is, for example, you open your bank fund, the recipient receives the amount of money and puts your key inside the new fund that only you know where the box is located. This process is always repeats in the Blockchain system.

The entire receipt, storage, and other bitcoin processes depend on Script or its programming language, which triggered the bitcoin network nodes. In this article, we intend to explain the Bitcoin Script process and examine the nature of Bitcoin.

How Bitcoin works

In general, the bitcoin system can be introduced with two key entities and locks. You use your key to open the box and put the cheques, and then get a new box with the new key to put the next cheque. Also, the types of locks in this system can also be different. For example, you can use combined keys or secret keys.

The key is Scriptsig and the lock that we place on any fund is ScriptPubkey. All of these are programmed codes that used to make a cryptocurrency. When you do a transaction, each node is detected individually and checks the program to see this transaction is correct.

The Coins that you have in the wallet are known as UNSPENT TRANSACTION OUTPUTS or UTXO. Anyone who has a key and a lock can spend their Coins on transactions.

If you have Utxo’s digital wallet, you can pick them up with the Public key. By providing scripting, which includes a digital signature and the Privilege key in ScriptPubKey, you can confirm your ownership of Coins.

Bitcoin stack

Generally, the script runs on the stack. In this way, if we have different functions and put them in a stack to run the script, you must run from the stack of these functions. In the bitcoin script, commands and symbols are placed separately and each of the stacks is left and executed. For example, consider the following code:

<xyz> <md5 hasher> <d16fb36f0911f878998C136191af705e> <check if equal>

Any part that represents with <> isolated indicates an executable command or data. First, the early XYZ field is received, then hash with the MD5 Hasher function and then the next string comes out of the script line. Then check the check function of the Equal, whether the last two strings come out of the stack equal to either. This will run a code in the script.

Pay-to-PubKey (P2PK)

Pay-to-PubKey is a structure used to send and receive bitcoin. However, then this method was changed to Hash Pay-To-PubKey. The routine of this script is in the form of <public key> op_checksig so that each public key is issued or rejected with the Op_Checksig command. In general, Pay-to-PubKey, a signature is first added to the stack and then the Top of the stack. The OP_CHECKSIG function is executed and measures the accuracy of the signature. Finally, the result is in the form of <1> in order to confirm and <0> in order to be incorrect.

PAY-TO-PUBKEY-HASH

Currently, Pay-to-PubKey-Hash is the most frequent type of transaction in Bitcoin script. This process automatically performs the digital wallet and its function is as follows:

Op_dup op_hash160 <public key hash> op_equalverify op_checksig

First, Op_DUP brought out the first data stack and duplicate it. This is in order to store primary data after applying changes on its duplicate. Op_equalverify combines two operands that are op_equal and op_verify. In this case, the first data function runs out of the stack and checks whether data is equal or not, then the second function checks the returned response from the first function and if it was correct, returns the value of 1.the script is:

<Signature> <Public Key>

There is a problem in this type of script, and it is the secret of the Public key until the sender itself wants to determine the amount of bitcoin inventory. In this script, the entire Public key is not available and its hash enters functions. The benefits of this mode can be simply sent and more secure against quantum Computations. Using mathematics, two levels of security run on the Public key that makes decoding it very hard.

PAY-TO-SRRIPT-HASH

In this script, a new initiative is created and it has to hash the entire script string. even the submission process is also encoded and no one except the key holder can figure out what the hash is submitted. Note the following example:

E145FE9ED5C23AA71FDB443DE00C7D9B4A69F8A27A2E4FBB1FE1D0DBFB6583F1

Hash script created is a code string that does not specify what process on what the key is. But the main function has been as follows:

<Multiply by 2>> <4> <check elf Equal>

The main output of the Pay-to-Script-Hash process script is as:

Op_hash160 <redeemscript hash> op_equal

In this section, we have the Redeemscript Hash piece that means the hash script needs to be written for currency. Redeemscript is generally as:

<Signature> <Public Key> <redeemscript>

This structure in Blockchain nodes is known as P2SH so Scriptsig elements are waiting in another stack. Each RedeemScript can be a code string that will be used in functions. Although the use of interesting P2SH scripts has very high security, it is not commonly used due to its high displacement in any block and increase the cost of the block.

In general, if you used MultiSignature or Segwit scripts and need to reduce the code in the block, you can have hashed the code string by P2SH and reduce its size to a large extent.

Segwit Transactions (P2WPKH and P2WSH)

With Segwit’s launch, no need to use Scriptsig and ScriptPubKey. The only new element added is Witness used in these types of scripts. The data they previously put inside Scriptsig will enter Witness in this way.

PAY-TO-WITNESS-PUBKEY-HASH (P2WPKH)

PAY-TO-WITNESS-PUBKEY-HASH (P2WPKH) is the Segwit type of P2PKH that the WITNESS data sent as follows:

<Signature> <Public Key>

As we checked before, in the P2PKH script, there was a scripting option that is removed in Segwit mode. So script pubkey comes in the figure below:

<Op_0> <Public Key Hash>

Additional items have been removed from this string. But how can nodes check this script? Based on the size of the public key, each node can understand whether this script is a P2PKH or changed to P2WPKH.

PAY-TO-WITNESS-SCRIPT-HASH (P2WSH)

There is a new type of Pay-To-Witness script that is known as the whole string and is known as P2WSH. For example, if we have the code below: <signature 1> <public key>

Scriptpubkey is also made in the figure below:

<Op_0> <script hash>

The rules are the same as above. The nodes read the size of the string and realize whether this type of script is from Witness or a P2SH.

Conclusion.

In this article, we tried to check the work of Bitcoin and its scripts to send and receive Coins. Each type of script has its own performance, but more scripts are more likely to go. The following table shows the overall scripts feature:

Script typeDescription
Pay-to-Pubkey (P2PK)Locks funds to a particular public key
Pay-to-Pubkey-Hash (P2PKH)Locks funds to a particular public key hash (i.e., an address)
Pay-to-Script-Hash (P2SH)Locks funds to the hash of a script that the recipient can provide
Pay-to-Witness-Pubkey-Hash (P2WPKH)The SegWit version of P2PK
Pay-to-Witness-Script-Hash (P2WSH)The SegWit version of P2SH

Leave a Reply

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Get last Update & news faster on Nominex    OK No thanks