Lab02
Lab02
1 Objective
The objective of this lab assignment is to gain a deeper understanding of Bitcoin Script and how
it operates within the Bitcoin network.
2 Prerequisites
2.1 Introduction to Bitcoin and Bitcoin Script
Bitcoin is a decentralized digital currency that allows peer-to-peer transactions without the need
for intermediaries like banks. It operates on a distributed ledger called the blockchain, which
records all transactions in a secure and transparent manner. One of the key features of Bitcoin is
its scripting language, known as Bitcoin Script, which plays a crucial role in defining the rules for
spending bitcoins.
Bitcoin Addresses: Bitcoin addresses are used to receive and send bitcoins. They are derived from
public keys, which are in turn derived from private keys. A typical Bitcoin address is a long string
of alphanumeric characters, and it serves as a destination for receiving funds.
Bitcoin Transactions:Bitcoin transactions are the fundamental building blocks of the Bitcoin net-
work. Each transaction contains inputs and outputs. Inputs reference unspent transaction outputs
(UTXOs) from previous transactions and provide the necessary funds for the new transaction.
Outputs specify the destinations and amounts of the transferred funds.
Bitcoin Script Opcodes Bitcoin Script uses various opcodes to perform operations on the data
stack. Here are some essential Bitcoin Script opcodes:
1/6
Blockchain course | 2024 Knowledge Engineering Department
Creating Bitcoin Script Bitcoin Script is typically used in two main types of transactions: Pay-to-
Public-Key-Hash (P2PKH) and Pay-to-Script-Hash (P2SH). P2PKH scripts lock funds to a single
public key, while P2SH scripts allow more complex spending conditions using a script hash.
In this tutorial, we’ll focus on creating a simple P2PKH script to lock and unlock funds.
P2PKH Script Example Below is an example of a P2PKH script in Python, this script generates
a random private key, derives the public key, and calculates the corresponding Bitcoin address:
# P2PKH Script
from bitcoin import *
from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress
2/6
Blockchain course | 2024 Knowledge Engineering Department
4.2 Task
Your first task is to create a simple Bitcoin script that locks funds to a specific address using the
Pay-to-Public-Key-Hash (P2PKH) script. You will be provided with a testnet address.
4.3 Requirements
• Create a P2PKH Script: In Python, you can create a P2PKH script as follows:
# P2PKH Script
from bitcoin import *
from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress
• Lock Funds: Use a Bitcoin testnet faucet to send testnet BTC to the generated Bitcoin
address.
• Spend Locked Funds: Write a Python script to spend the locked funds:
3/6
Blockchain course | 2024 Knowledge Engineering Department
4.4 Evaluation
Your code will be evaluated to check if it successfully locks and unlocks funds. We will discuss the
results and any issues you encountered.
5.2 Task
Your second task is to create a 2-of-2 multisig script using provided public keys.
5.3 Requirements
• Create a 2-of-2 Multisig Script: Write scripts to create a 2-of-2 multisig address, lock
funds to it, and then spend those funds.
4/6
Blockchain course | 2024 Knowledge Engineering Department
• Lock Funds: Use a Bitcoin testnet faucet to send testnet BTC to the generated multisig
address.
• Spend Locked Funds: Write a Python script to spend the locked funds from the multisig
address:
5.4 Evaluation
Your ability to create and spend funds from a multisig address will be evaluated. We will also
discuss the security benefits of multisig addresses.
5/6
Blockchain course | 2024 Knowledge Engineering Department
6.2 Task
Write a report summarizing your experiences and insights gained from this lab assignment. Discuss
the advantages and limitations of Bitcoin Script.
6.3 Evaluation
Your reflections and understanding of the practical applications of Bitcoin Script will be evaluated.
7 Submission regulation
• Students create a folder <Group’s ID> containing the contents following:
– <Code> folder: contains the whole project.
– Executable file (optinal).
– <Report.pdf> file(at most 15 pages, single page, 12pt font) that includes:
∗ Information about team members.
∗ Project structure.
∗ Any other remarks about your design or implementation.
∗ All links and books related to your submission must be mentioned.
∗ DO NOT insert your source code in the report.
∗ Description of how your system operates: Provide a detailed explanation of the func-
tioning and flow of your system, highlighting key components and interactions.
∗ Challenges encountered during implementation: Discuss any technical, design-related,
or logistical hurdles you faced and how you overcame them.
∗ Lessons learned: Share insights and knowledge gained throughout the project and
explain how it contributed to your personal growth.
∗ The report can be written in Vietnamese or English.
• Compress the above folder into Group’s ID.zip for submission.
• Submission with wrong regulation will result in a "0" (zero).
• Plagiarism and Cheating will result in a "0" (zero) for the entire course and will be subject
to appropriate referral to the Management Board for further action.
6/6