Ethereum: Redeem script. script hash, witness script and witness program

Ethereum Scripting 101: Understanding Redeem Scripts, Witness Scripts, and Witness Programs

As a user of the Ethereum blockchain, it is essential to understand the underlying scripting language used for transactions, contracts, and other smart contract functionality. In this article, we’ll delve into the world of Ethereum scripts, focusing on two key concepts: redeem scripts, witness scripts, and witness programs.

What are Scripts?

In Bitcoin, scripts are a way to execute complex logic using digital signatures. They’re essentially self-contained blocks of code that can be executed by the network. In Ethereum, scripts are used for a variety of purposes, including:

  • Transaction validation

  • Smart contract execution

  • Payment processing

Redeem Script: A Special Case

A redeem script is a specific type of script in Ethereum that allows you to verify and confirm transactions. It’s called “redeem” because it’s designed to redeem or “unfreeze” funds held in a contract or other smart contract.

A redeem script consists of two main parts:

  • PubKeyHash: This is the public key used for signing the transaction.

  • Script bytes: These are the binary representation of the script, which contains the logic to verify and confirm the transaction.

The syntax of the redeem script is as follows:

OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG ...

Here’s a breakdown of each component:

  • OP_DUP: This is the instruction that duplicates the signature from the previous step.

  • OP_HASH160: This specifies the public key used for signing the transaction.

  • : This is the public key being hashed and verified.

  • OP_EQUALVERIFY: This checks whether the signature on top of the hash matches the expected signature.

  • OP_CHECKSIG: This verifies that the original signature was generated by the contract owner.

Witness Script

A witness script, also known as a “witness program,” is a type of Ethereum script that’s used for validation and confirmation. It’s designed to verify the transactions and contracts on the network without relying on other scripts.

Witness programs are typically implemented using a combination of scripts and a witness program header. The header contains information about the witness program, including:

  • Witness Program ID: This is the unique identifier assigned to the witness program.

  • Script signature: This is the digital signature associated with the witness program.

The syntax for a witness script is as follows:

0x... // Witness Program Header

OP_DUP OP_HASH160 OP_EQUALVERIFY ...

WITNESSE_program_id

Here’s a breakdown of each component:

  • 0x...: This is the hexadecimal representation of the witness program header.

  • OP_DUP: This duplicates the signature from the previous step.

  • OP_HASH160: This specifies the public key used for signing the transaction.

  • : This is the public key being hashed and verified.

  • OP_EQUALVERIFY: This checks whether the signature on top of the hash matches the expected signature.

  • WITNESSE_program_id: This contains information about the witness program, including its ID.

Watch Program Header

A witness program header contains information about the witness program, such as:

  • Program name: The name of the contract or smart contract.

  • Program version: The version number of the contract or smart contract.

  • Gas limits

    : The maximum amount of gas allowed for the contract or smart contract.

The syntax for a witness program header is as follows:

0x... // Witness Program Header

OP_DUP OP_HASH160 OP_EQUALVERIFY ...

PROGNAME PROGVERS[1..n] …

Here’s a breakdown of each component:

  • 0x...: This is the hexadecimal representation of the witness program header.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart