You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71-74Lines changed: 71 additions & 74 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
# 🛡️ SPV Contract: Bitcoin Light Client on EVM
2
-
Welcome to the **SPV Contract**, a robust and efficient Solidity implementation for verifying Bitcoin block headers directly on an EVM-compatible blockchain. This contract empowers dApps to act as a **Simplified Payment Verification (SPV)** client, allowing them to validate the existence and inclusion of Bitcoin transactions without needing to run a full Bitcoin node.
1
+
# 🛡️ SPV Gateway: Bitcoin Light Client on EVM
2
+
Welcome to the **SPV Gateway**, a robust and efficient Solidity implementation for verifying Bitcoin block headers directly on an EVM-compatible blockchain. This contract empowers dApps to act as a **Simplified Payment Verification (SPV)** client, allowing them to validate the existence and inclusion of Bitcoin transactions without needing to run a full Bitcoin node.
3
3
4
-
# ✨ Why this SPV Contract?
5
-
In the decentralized world, connecting different blockchain ecosystems securely is paramount. This SPV contract provides a trust-minimized bridge, enabling smart contracts on EVM chains to cryptographically verify the state of the Bitcoin blockchain. This opens doors for exciting use cases like:
4
+
# ✨ Why this SPV Gateway?
5
+
In the decentralized world, connecting different blockchain ecosystems securely is paramount. This SPV Gateway provides a trust-minimized bridge, enabling smart contracts on EVM chains to cryptographically verify the state of the Bitcoin blockchain. This opens doors for exciting use cases like:
6
6
-**Cross-chain bridges** for Bitcoin-backed assets
7
7
-**Light clients** for dApps that need to confirm Bitcoin transaction finality
8
8
-**Decentralized custodianship** solutions
@@ -26,98 +26,98 @@ In the decentralized world, connecting different blockchain ecosystems securely
26
26
# ⚙️ How it Works (Under the Hood)
27
27
The contract operates by receiving raw Bitcoin block headers, which are then parsed and validated against Bitcoin's strict consensus rules.
28
28
29
-
1.**Header Parsing:** Raw 80-byte Bitcoin block headers are parsed into a structured *BlockHeaderData* format. This involves handling Bitcoin's unique little-endian byte ordering.
29
+
1.**Header Parsing:** Raw 80-byte Bitcoin block headers are parsed into a structured *BlockHeader.HeaderData* format. This involves handling Bitcoin's unique little-endian byte ordering.
30
30
2.**Double SHA256 Hashing:** Each block header is double SHA256 hashed to derive its unique block hash, which is then byte-reversed for standard representation.
31
31
3.**Proof-of-Work Verification:** The calculated block hash is checked against the current network difficulty target (derived from the *bits* field in the header).
32
32
4.**Chain Extension & Reorganization:** New blocks are added to a data structure that allows for tracking multiple chains. When a new block extends a chain with higher cumulative work, the *mainchainHead* is updated, reflecting potential chain reorganizations.
33
33
5.**Difficulty Adjustment:** Every 2016 blocks, the contract calculates a new difficulty target based on the time taken to mine the preceding epoch. This ensures the 10-minute average block time is maintained.
34
34
35
35
# 📊 Flow Diagrams
36
-
These diagrams outline the step-by-step process for adding block headers to the SPV Contract.
36
+
These diagrams outline the step-by-step process for adding block headers to the SPV Gateway.
SPVContract->>SPVContract: 7. Add Block To Chain (_addBlock)
182
-
SPVContract->>SPVContract: Emit BlockHeaderAdded
181
+
SPVGateway->>SPVGateway: 7. Add Block To Chain (_addBlock)
182
+
SPVGateway->>SPVGateway: Emit BlockHeaderAdded
183
183
end
184
184
185
-
SPVContract-->>Caller: Transaction Complete
186
-
deactivate SPVContract
185
+
SPVGateway-->>Caller: Transaction Complete
186
+
deactivate SPVGateway
187
187
```
188
188
189
189
190
190
# 📦 Contract Components
191
-
The solution is primarily composed of the main SPV contract and two essential helper libraries that manage the intricacies of Bitcoin's block structure and difficulty adjustments.
191
+
The solution primarily consists of the main SPV Gateway contract and the TargetsHelper library, which manages difficulty adjustments.
192
192
193
-
## SPVContract
193
+
## SPVGateway
194
194
This is the central contract that users will interact with. It serves as the primary interface for managing Bitcoin block headers on the EVM. It handles the core logic for adding and validating blocks, tracking the main Bitcoin chain, and providing querying functionalities. All custom errors and events related to the SPV operations are defined here, ensuring clear feedback and transparency during contract execution.
195
195
196
-
## BlockHeader Library
197
-
This is a pure utility library specifically designed to handle the low-level details of Bitcoin block headers. It's responsible for the precise parsing of raw 80-byte Bitcoin block header data into a structured format that Solidity can easily work with. Crucially, it manages the byte order conversions, translating Bitcoin's little-endian format to Solidity's big-endian, and vice-versa. It also provides the essential function for calculating the double SHA256 hash of a block header, which is fundamental for verifying Proof-of-Work.
198
-
199
196
## TargetsHelper Library
200
197
This library encapsulates all the complex mathematical and logical operations related to Bitcoin's difficulty targets. It provides functions to accurately calculate new difficulty targets based on elapsed time between blocks, ensuring the contract adheres to Bitcoin's dynamic difficulty adjustment rules. Additionally, it offers utilities for converting between the compact "bits" format (as found in Bitcoin block headers) and the full 256-bit target value, and it calculates the cumulative work associated with a given block or epoch, which is vital for determining the most valid chain.
0 commit comments