0% found this document useful (0 votes)
33 views23 pages

BitCoin Notations

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views23 pages

BitCoin Notations

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Bitcoin Basic Notations

Lecture 4

Dr Sadiqa Arshad
[email protected]
Bitcoin Script

• Bitcoin’s scripting language is simply called Script.


• It is a simple language that is not Turing complete, (lacks several
logical functions, including loops).
• Script is used almost exclusively to lock and unlock bitcoin, not
to build applications or run programs.
• Script’s simplicity also gives Bitcoin security and makes it easier
for developers to avoid losing money while designing wallets or
applications on top of Bitcoin.
• The script of a Bitcoin transaction determines to whom the
bitcoin was sent.
Bitcoin Script

Bitcoin has a few different scripts, with Pay-to-Public-Key-Hash


(P2PKH) being the most popular. P2PKH is a simple script which
pays bitcoin to an address.
Hash Functions

All cryptographic hash functions take an input, called a preimage, and


produce an output, called a hash or digest, of a fixed length. This length
varies based on the precise function used.

• The output of a cryptographic hash function is deterministic.

• A cryptographic hash function is a one-way function.

• The output is random and unpredictable.

Note: A hash is not ‘encryption’ – it cannot be decrypted back to the original text
Secure Hash Algorithm (SHA)

SHA-256
• SHA-256 (secure hash algorithm, FIPS 182-2) is a cryptographic hash
function with digest length of 256 bits.

• It is a keyless hash function; that is, an MDC (Manipulation Detection


Code).

• A message is processed by blocks of 512 = 16 × 32 bits, each block


requiring 64 rounds.

The Bitcoin protocol uses SHA-256 to derive transaction IDs (txids), block hashes,
addresses, and Merkle trees. SHA-256 is applied twice, as in the case of txids.
Bitcoin Address Types

• Bitcoin uses different


address formats to
receive payments.
• Each format has its own
benefits and trade-offs.
Redeem Script

• A RedeemScript is the script used to unlock bitcoin sent to a P2SH or


P2WSH address.

• In a P2SH or P2WSH transaction, bitcoin is locked to the hash of a


RedeemScript, ensuring that only someone who can reproduce the
RedeemScript and add any required signatures can spend the bitcoin.

• Redeem scripts most commonly entail multisig scripts or wrapped


SegWit scripts.
Pay-to-Script-Hash (P2SH)

Pay-to-Script-Hash (P2SH) is a type of ScriptPubKey which allows for the spending of


bitcoin based on the satisfaction of the script whose hash is specified within the
transaction.

A P2SH transaction is a transaction whose inputs were locked using a P2SH


ScriptPubKey.
Pay-to-Script-Hash (P2SH)

In a Pay-to-Script-Hash (P2SH) transaction, the sender, Alice includes a script hash that
refers to the actual spending conditions.
Bob, the recipient, needs to provide both the redeem script and the necessary signatures
when he wants to spend the bitcoins.

Example:
Alice wants to send 1 BTC to Bob in a way that requires:
2 signatures out of 3 possible parties (Bob, Charlie, and Dave) to spend the bitcoin.
This is known as a 2-of-3 multisig condition.
Step1:
Creating the Script:
Alice constructs a redeem script that specifies the spending condition:
• 2 out of 3 signatures are required to spend the bitcoin.
• The public keys of Bob, Charlie, and Dave are included in the script
Pay-to-Script-Hash (P2SH)

OP_2 <Bob's public key> <Charlie's public key> <Dave's public key> OP_3 OP_CHECKMULTISIG

•OP_2 means at least 2 signatures are needed.


•OP_3 means there are 3 public keys.
•OP_CHECKMULTISIG verifies that the signatures match the provided public keys.

Step2:
Hashing the Script:
•Alice hashes this redeem script using a cryptographic hash function like
SHA-256 and RIPEMD-160. The resulting hash is the script hash.
Pay-to-Script-Hash (P2SH)

Constructing the Transaction:


•Alice creates a transaction to send 1 BTC to the hash of the redeem script.

•The output of the transaction specifies the destination as a P2SH address,


which is a hash of the redeem script

Step 4
Broadcasting the Transaction:
•Alice broadcasts the transaction to the Bitcoin network, and Bob now
effectively holds 1 BTC but cannot spend it yet without the redeem script and
the necessary signatures.
Pay-to-Script-Hash (P2SH)

Bob Wants to Spend the Bitcoin

Reconstruct the Redeem Script:

•Bob provides the original redeem script that specifies the 2-of-3 multisig
condition. This script needs to match the script hash in Alice's original
transaction.
Provide Signatures:

•Bob must collect at least 2 valid signatures from the participants listed in the
redeem script (e.g., himself and Charlie).
Pay-to-Script-Hash (P2SH)
Construct the Spending Transaction:

Bob creates a new transaction to send the 1 BTC to someone else.


•In the input of this transaction, Bob includes:
• The redeem script.
• The required signatures (Bob’s and Charlie’s in this case).

Broadcast the Transaction:


•Bob broadcasts the transaction to the network. The Bitcoin nodes will check:
• That the redeem script matches the script hash from Alice’s original
transaction.
• That the signatures are valid according to the public keys in the redeem script.
Once validated, the transaction is confirmed, and the 1 BTC is transferred
according to Bob’s spending conditions.
Pay-to-Script-Hash (P2SH)

P2SH Address:
Holds the hash of the redeem script.

Redeem Script:
Specifies the actual conditions for spending, such as multisignature.

Signatures:
Needed to satisfy the conditions in the redeem script when spending the
bitcoins.
Pay-to-Public-Key (P2PK)

• Early Bitcoin address format.


• Funds are locked directly to a recipient's public key.
• To spend, the recipient provides a digital signature from
their private key.

• Limitations:
• Lacks privacy (exposes public keys).
• Results in larger transaction sizes.

• Status: Deprecated in favor of P2PKH


Pay-to-Public-Key-Hash(P2PKH)

Historically, the most common address type.


Sends funds to a hashed version of the recipient’s public key, improving privacy.

The recipient must provide:


• Public key that hashes to the address.
• Digital signature from their private key.

Limitations:
• Lacks the efficiency and security of newer formats (e.g., P2WPKH, P2TR).
Bitcoin Transactions
Bitcoin Change Transactions

•Inputs: These are the previous unspent transaction outputs (UTXOs) the sender is
using to fund the current transaction.
•Outputs: These include the recipient's address (the main output) and the change
address (if necessary).

NOTE: Bitcoin transactions must balance: the total value of the inputs must equal
the total value of the outputs plus the transaction fee.
Bitcoin Change Transactions

Why Change Outputs are Needed:


•In most cases, the inputs that a sender uses to fund a transaction are not exactly
equal to the amount the sender wants to transfer. For example:
• Alice wants to send 0.7 BTC to Bob.
• Alice has a UTXO (from a previous transaction) worth 1 BTC.
• If Alice sends the full 1 BTC to Bob, she would overpay. Instead, Alice
creates a transaction where:
• Bob receives 0.7 BTC (this is the main output).
• Alice gets the 0.3 BTC back in a change output (minus any transaction
fees).
Bitcoin Change Transactions

How Change Output Works:


• The transaction will have multiple outputs: one to the recipient (Bob)
and one to the sender's change address (Alice).

• The change output functions like an automatic refund mechanism


that returns the leftover Bitcoin from a transaction to the sender.

Change Addresses:
• Change addresses are new Bitcoin addresses that receive the change.
Modern wallets generate a new change address for every transaction
for privacy reasons.
• If Alice is using a Bitcoin wallet, it will usually handle the creation of
the change output and the change address automatically.
Bitcoin Change Transactions

Transaction Fees and Change:


Bitcoin transactions require a fee to incentivize miners to include them in
blocks. When calculating the change output, the wallet deducts the
transaction fee from the total inputs.

Example:
Alice's input: 1 BTC
Amount to Bob: 0.7 BTC
Fee: 0.0001 BTC (let’s say)
Change returned to Alice: 1 BTC - 0.7 BTC - 0.0001 BTC = 0.2999 BTC
Bitcoin Change Transactions

Privacy and Security Considerations:


• Change addresses are often different from the original sending
address to enhance privacy.
• This prevents others from easily tracking how much Bitcoin is left
with the sender.
• Wallets automatically generate change addresses and handle the
change outputs without the user needing to intervene manually.
Summary

• A transaction change output ensures that the sender receives any


leftover funds when the total input exceeds the amount being sent.
• It is a critical component of Bitcoin transactions that keeps funds
balanced, covering both the payment to the recipient and the
transaction fees.
• Bitcoin wallets manage change outputs automatically, making the
process seamless for users.

You might also like