Scripts and transaction validation

Introduction

  basics

The lock and unlock scripts for pay-to-pubkey are the simplest form of assigning satoshis to a public key.

Lock script

  pupKey

The lock script has two components:

⟨pubKey⟩ OP_CHECKSIG

These components are based on the following data structures:

VarInt : Length of public key
[Byte] : Serialized public key
OpCode : OP_CHECKSIG

Unlock script

     sig

The unlock script has one component:

⟨signature⟩

This component is based on the following data structure:

VarInt : Length of signature
[Byte] : Serialized signature

Combined script

 execute

The combined script has three components:

⟨signature⟩ ⟨pubKey⟩ OP_CHECKSIG

The script is parsed and executed from left to right. Every data object is pushed on top of the stack implicitly. Validating the script the following steps are executed:

  1. The signature is pushed on the stack
⟨signature⟩ ⟨pubKey⟩ OP_CHECKSIG ⟨signature⟩
  1. The public key is pushed on the stack
⟨pubKey⟩ OP_CHECKSIG ⟨pubKey⟩⟨signature⟩

  1. The OpCode OP_CHECKSIG pops the first two items off the stack, checks the signature and pushes ⟨1⟩ on the stack if the signature is valid or stops the execution with an error otherwise
OP_CHECKSIG ⟨1⟩

  1. The script evaluates as valid if the item on top of the stack is ⟨1⟩ after the execution has finished