Skip to content

Commit 34261d6

Browse files
committed
translate bitcoin script section
1 parent caf046d commit 34261d6

File tree

9 files changed

+645
-5
lines changed

9 files changed

+645
-5
lines changed
Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,72 @@
11
---
22
sidebar_position: 1
33
---
4-
# Dual Stack model
4+
5+
# Dual Stack Model
6+
7+
Bitcoin script execution involves a stack-based programming language used to verify transactions on the Bitcoin network.
8+
Here is an overview of the Bitcoin script execution stack:
9+
10+
## Script Composition
11+
12+
- **ScriptPubKey**: This is the locking script attached to the output, specifying the conditions that must be met to
13+
spend the output.
14+
- **ScriptSig**: This is the unlocking script included in the input, providing the data required to meet the conditions
15+
in ScriptPubKey.
16+
17+
## Execution Stack
18+
19+
The execution stack is used to process and evaluate Bitcoin scripts. Here’s how it works:
20+
21+
1. **Push Operations**: Push data and commands (opcodes) onto the stack.
22+
- Example: `OP_DUP` duplicates the top item on the stack.
23+
- Example: `OP_HASH160` performs a RIPEMD-160 hash after SHA-256.
24+
25+
2. **Script Execution**:
26+
- ScriptSig and ScriptPubKey are concatenated and executed sequentially.
27+
- First, ScriptSig is executed, pushing its data onto the stack.
28+
- Then, ScriptPubKey is executed, using the data left on the stack by ScriptSig.
29+
30+
3. **Stack Operations**:
31+
- Various operations modify the stack. For example:
32+
- `OP_ADD` pops two items from the stack, adds them, and pushes the result back onto the stack.
33+
- `OP_EQUALVERIFY` checks if the top two items are equal and removes them if they are.
34+
35+
4. **Conditionals and Control Structures**:
36+
- Conditional operations like `OP_IF`, `OP_ELSE`, and `OP_ENDIF` allow for more complex scripts by enabling
37+
conditional execution paths.
38+
39+
5. **Validation**:
40+
- After executing all commands, the stack should be in a specific state for the transaction to be valid.
41+
- Typically, the final stack state should have a single `TRUE` value, indicating successful script execution.
42+
43+
## Example Transaction Execution
44+
45+
1. **ScriptSig**: `<signature> <public key>`
46+
- Pushes the signature and public key onto the stack.
47+
48+
2. **ScriptPubKey**: `OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG`
49+
- `OP_DUP`: Duplicates the public key.
50+
- `OP_HASH160`: Hashes the duplicated public key.
51+
- `<PubKeyHash>`: Pushes the expected public key hash.
52+
- `OP_EQUALVERIFY`: Verifies that the public key hash matches the expected hash.
53+
- `OP_CHECKSIG`: Verifies the signature using the public key.
54+
55+
## Stack Execution Flow
56+
57+
- **Initial Stack (after executing ScriptSig)**:
58+
- Stack: `[signature, public key]`
59+
60+
- **Executing ScriptPubKey**:
61+
- `OP_DUP`: `[signature, public key, public key]`
62+
- `OP_HASH160`: `[signature, public key, public key hash]`
63+
- `<PubKeyHash>`: `[signature, public key, public key hash, expected public key hash]`
64+
- `OP_EQUALVERIFY`: Verifies and removes the public key hash and expected public key hash if they match.
65+
- `OP_CHECKSIG`: Verifies the signature using the public key.
66+
67+
- **Final Stack**:
68+
- If valid: `[TRUE]`
69+
- If invalid: `[FALSE]` or script failure, resulting in the transaction being rejected.
70+
71+
Understanding the Bitcoin script execution stack is crucial for developers working on Bitcoin transactions as it ensures
72+
the correct and secure validation of transaction conditions.

docs/contract/bitcoin-scripts/opcode.md

Lines changed: 143 additions & 1 deletion
Large diffs are not rendered by default.

docs/contract/bitcoin-scripts/p2ms.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,90 @@
11
---
22
sidebar_position: 5
33
---
4+
45
# P2MS
6+
7+
Introduction to P2MS output.
8+
9+
## What is P2MS Output?
10+
11+
P2MS (Pay-to-MultiSig, or "Pay to Multiple Signatures") is a type of transaction output in the Bitcoin network that
12+
allows multiple signers to jointly manage a fund. This mechanism is often used to enhance security and fault tolerance,
13+
such as in corporate accounts managed by multiple parties. A P2MS output can specify how many signers' signatures are
14+
required to spend the funds, providing a flexible multi-signature solution.
15+
16+
## Structure of P2MS Output
17+
18+
The script for a P2MS output is called the locking script (scriptPubKey), and its typical structure is as follows:
19+
20+
```
21+
m <public key 1> <public key 2> ... <public key n> n OP_CHECKMULTISIG
22+
```
23+
24+
In this script:
25+
26+
- `m`: Indicates the minimum number of signatures required to spend the funds.
27+
- `<public key 1> <public key 2> ... <public key n>`: These are the public keys of the participants in the
28+
multi-signature scheme.
29+
- `n`: Indicates the total number of public keys involved.
30+
- `OP_CHECKMULTISIG`: This opcode verifies if the provided signatures meet the required minimum number of signatures.
31+
32+
To better understand the P2MS output, let's break down its script:
33+
34+
1. `m`: Specifies the required minimum number of signatures.
35+
2. `<public key 1> <public key 2> ... <public key n>`: Provides multiple public keys.
36+
3. `n`: Specifies the total number of public keys.
37+
4. `OP_CHECKMULTISIG`: Verifies if the signatures meet the `m` valid signatures requirement.
38+
39+
## Characteristics of P2MS Output
40+
41+
1. **Increased Security**: P2MS output allows multiple signers to manage a fund together, reducing the risk of a single
42+
point of failure. If one signer's private key is compromised, the attacker still needs the private keys of the other
43+
signers to spend the funds.
44+
2. **Flexibility**: P2MS output can set the signature threshold (`m`) and the total number of signers (`n`) as needed.
45+
For example, it can be set to 3-of-5 signatures, meaning any 3 out of 5 public keys are needed to spend the funds.
46+
3. **Complexity**: Compared to P2PKH output, the script for P2MS output is more complex, requiring more computation and
47+
verification steps. While it increases security, it also adds complexity and transaction size in bytes.
48+
49+
## Use Cases for P2MS Output
50+
51+
P2MS output is very useful in scenarios that require joint management or enhanced security. Here are some typical use
52+
cases:
53+
54+
1. **Corporate Fund Management**: Companies can use P2MS output to set up multi-signature accounts, ensuring that
55+
multiple authorized signers are required to spend company funds.
56+
2. **Joint Investments**: Multiple investors can use P2MS output to jointly manage an investment fund, only being able
57+
to spend the funds with consensus.
58+
3. **Family Financial Security**: Family members can set up multi-signature accounts to ensure the secure management of
59+
family funds.
60+
61+
## Example of a P2MS Transaction
62+
63+
Here is a simplified example of a P2MS transaction:
64+
65+
- Locking Script (scriptPubKey):
66+
```
67+
2 <public key 1> <public key 2> <public key 3> 3 OP_CHECKMULTISIG
68+
```
69+
70+
- Unlocking Script (scriptSig):
71+
```
72+
OP_0 <signature 1> <signature 2>
73+
```
74+
75+
When spending this Bitcoin, the unlocking script must be provided, containing the signatures that meet the minimum
76+
number required. In this example, at least two signatures are needed to spend the funds. The verification process is as
77+
follows:
78+
79+
1. `OP_0`: Due to a small bug in the Bitcoin script, an invalid opcode (usually `OP_0`) needs to be placed on top of the
80+
stack.
81+
2. `<signature 1> <signature 2>`: Provide two valid signatures.
82+
3. `OP_CHECKMULTISIG`: Takes signatures and public keys from the stack, verifying if at least two signatures are valid.
83+
84+
## Conclusion
85+
86+
P2MS output, as a transaction type that allows multi-signature verification, provides enhanced security and flexibility
87+
in the Bitcoin network. It is suitable for scenarios requiring joint management, such as corporate fund management,
88+
joint investments, and family financial security. Although its script structure is relatively complex, the use of P2MS
89+
output can significantly increase the security of fund management. Understanding P2MS output is important for mastering
90+
advanced uses of Bitcoin and multi-signature mechanisms.

docs/contract/bitcoin-scripts/p2pk.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,80 @@
11
---
22
sidebar_position: 3
33
---
4+
45
# P2PK
6+
7+
Introduction to P2PK Output.
8+
9+
## What is P2PK Output?
10+
11+
P2PK (Pay-to-PubKey) is an earlier type of output in the Bitcoin network used to pay directly to a specific public key.
12+
Although it is relatively rare in modern Bitcoin transactions, understanding P2PK is important for grasping Bitcoin
13+
transactions and script systems.
14+
15+
### Structure of P2PK Output
16+
17+
The structure of a P2PK output is relatively simple, directly containing the recipient's public key. A typical P2PK
18+
script is as follows:
19+
20+
```
21+
<public key> OP_CHECKSIG
22+
```
23+
24+
In this script:
25+
26+
- `<public key>`: This is the recipient's public key.
27+
- `OP_CHECKSIG`: This is an opcode used to verify the signature.
28+
29+
To better understand the P2PK output, let's break down its script:
30+
31+
1. `<public key>`: This is a compressed or uncompressed Bitcoin public key.
32+
2. `OP_CHECKSIG`: This is an opcode that takes a signature and a public key from the stack and verifies if the signature
33+
is valid using the public key.
34+
35+
### Characteristics of P2PK Output
36+
37+
1. **Simplicity**: The P2PK output structure is simple, containing only a public key and an opcode. This simplicity made
38+
it widely used in early versions of Bitcoin.
39+
2. **Direct Payment to Public Key**: Unlike P2PKH (Pay-to-PubKeyHash) outputs, which use a public key hash to find the
40+
public key, P2PK outputs use the public key directly. This simplifies the verification process but introduces some
41+
privacy and security issues.
42+
3. **Privacy Issues**: Since P2PK outputs directly contain the public key, anyone can see the recipient's public key.
43+
This could lead to privacy issues as the public key can be linked to the user's other transactions or identity
44+
through certain techniques.
45+
4. **Security Issues**: Assuming the threat of quantum computers to public key cryptography, directly exposing the
46+
public key might reduce security since quantum computers could potentially derive the private key from the public
47+
key.
48+
49+
### Use Cases for P2PK Output
50+
51+
Although P2PK outputs are uncommon in modern Bitcoin transactions, they played a significant role in Bitcoin's early
52+
development. Here are some possible use cases:
53+
54+
1. **Miner Rewards**: The Bitcoin genesis block and some early blocks used P2PK outputs for miner rewards.
55+
2. **Simple Peer-to-Peer Payments**: In Bitcoin's early days, users might have used P2PK outputs for simple peer-to-peer
56+
payments.
57+
58+
### Modern Alternatives
59+
60+
Due to the privacy and security issues of P2PK outputs, modern Bitcoin transactions more commonly use P2PKH (
61+
Pay-to-PubKeyHash) outputs and P2SH (Pay-to-ScriptHash) outputs.
62+
63+
- **P2PKH**: P2PKH outputs use public key hashes instead of direct public keys, providing some privacy and security
64+
protection. A typical P2PKH script is as follows:
65+
```
66+
OP_DUP OP_HASH160 <public key hash> OP_EQUALVERIFY OP_CHECKSIG
67+
```
68+
69+
- **P2SH**: P2SH outputs allow more complex scripts, supporting multi-signature and other advanced functions. A typical
70+
P2SH script is as follows:
71+
```
72+
OP_HASH160 <script hash> OP_EQUAL
73+
```
74+
75+
### Conclusion
76+
77+
As an early type of transaction output in the Bitcoin network, P2PK outputs, although less common today, provide an
78+
important foundation for understanding Bitcoin transactions and script systems. As the Bitcoin network has evolved, more
79+
complex and secure output types like P2PKH and P2SH have gradually replaced P2PK outputs, but understanding P2PK outputs
80+
is still essential for a comprehensive grasp of Bitcoin technology.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,87 @@
11
---
22
sidebar_position: 4
33
---
4+
45
# P2PKH
6+
7+
Introduction to P2PKH Output.
8+
9+
## What is P2PKH Output?
10+
11+
P2PKH (Pay-to-PubKeyHash) is the most common type of transaction output in the Bitcoin network. It provides better
12+
privacy and security compared to the earlier P2PK (Pay-to-PubKey) output. P2PKH allows Bitcoin to be paid to a public
13+
key hash rather than directly to a public key, thereby protecting the recipient's privacy to some extent.
14+
15+
## Structure of P2PKH Output
16+
17+
The script for a P2PKH output is called the locking script (scriptPubKey), and its typical structure is as follows:
18+
19+
```
20+
OP_DUP OP_HASH160 <public key hash> OP_EQUALVERIFY OP_CHECKSIG
21+
```
22+
23+
To understand this script, let's break down its components:
24+
25+
1. **OP_DUP**: Duplicates the top stack item.
26+
2. **OP_HASH160**: Performs RIPEMD-160 hashing on the top stack item (after SHA-256 hashing).
27+
3. **\<public key hash>**: The recipient's public key hash.
28+
4. **OP_EQUALVERIFY**: Verifies that the two top stack items are equal; if not, the script fails.
29+
5. **OP_CHECKSIG**: Checks if the signature is valid.
30+
31+
In a P2PKH transaction, the sender's unlocking script (scriptSig) needs to provide:
32+
33+
1. Signature: Proves the sender's authorization of the transaction.
34+
2. Public Key: Matches the public key hash in the locking script.
35+
36+
## Characteristics of P2PKH Output
37+
38+
1. **Enhanced Privacy**: P2PKH outputs include only the hash of the public key, not the public key itself. This makes
39+
tracking the recipient's public key more difficult, thus enhancing privacy.
40+
2. **Improved Security**: Transactions that directly include public keys are more susceptible to potential quantum
41+
computing attacks. P2PKH outputs provide an additional layer of security by using the public key hash. Cracking a
42+
hash is harder than cracking a public key for a quantum computer.
43+
3. **Widespread Use**: P2PKH outputs are the most common type of output in Bitcoin transactions, widely used in various
44+
transaction scenarios.
45+
4. **Support for Lightweight Clients**: P2PKH outputs enable lightweight clients (such as SPV wallets) to verify
46+
transactions more efficiently, as these clients only need to check the public key hash instead of processing the full
47+
public key.
48+
49+
## Use Cases for P2PKH Output
50+
51+
P2PKH outputs are widely used in the Bitcoin network, covering almost all everyday transactions for regular users. Here
52+
are some typical use cases:
53+
54+
1. **Personal Wallet Transactions**: Most Bitcoin wallets default to using P2PKH outputs to receive Bitcoin.
55+
2. **Merchant Payments**: Many online merchants and service providers use P2PKH addresses to receive payments from
56+
customers.
57+
3. **Exchange Transfers**: When users deposit and withdraw Bitcoin on exchanges, they typically use P2PKH outputs.
58+
59+
## Example of a P2PKH Transaction
60+
61+
Here is a simplified example of a P2PKH transaction:
62+
63+
- Locking Script (scriptPubKey):
64+
```
65+
OP_DUP OP_HASH160 <public key hash> OP_EQUALVERIFY OP_CHECKSIG
66+
```
67+
68+
- Unlocking Script (scriptSig):
69+
```
70+
<signature> <public key>
71+
```
72+
73+
When the recipient wants to spend this Bitcoin, they need to provide the unlocking script, which includes a valid
74+
signature and the public key that matches the public key hash. The verification process is as follows:
75+
76+
1. `OP_DUP`: Duplicates the public key in the unlocking script.
77+
2. `OP_HASH160`: Hashes the public key to generate the public key hash.
78+
3. `OP_EQUALVERIFY`: Verifies that the calculated public key hash matches the one in the locking script.
79+
4. `OP_CHECKSIG`: Uses the provided public key to verify the signature's validity.
80+
81+
## Conclusion
82+
83+
As the most common type of transaction output in the Bitcoin network, P2PKH outputs provide enhanced privacy and
84+
security. Their structure makes Bitcoin payments more secure and private, suitable for various transaction scenarios.
85+
From personal wallets to merchant payments, P2PKH outputs play a crucial role in the Bitcoin ecosystem, driving the
86+
widespread adoption and usage of Bitcoin. Understanding P2PKH outputs is essential for comprehending Bitcoin transaction
87+
mechanisms and improving Bitcoin usage security.

0 commit comments

Comments
 (0)