|
1 | | -# SPV Contracts |
| 1 | +# SPV Contracts |
| 2 | + |
| 3 | +Smart contract for verifying Bitcoin block headers on EVM-compatible chains using the **Simple Payment Verification (SPV)** method. |
| 4 | + |
| 5 | +This contract behaves like an SPV node: it builds a valid chain of Bitcoin block headers, verifies them according to Bitcoin consensus rules, and enables Merkle Proof-based verification of Bitcoin transactions. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Stores and verifies Bitcoin block headers |
| 10 | +- Validates headers using: |
| 11 | + - Proof of Work (`bits` → `target`) |
| 12 | + - Median time rule |
| 13 | + - Chain continuity |
| 14 | +- Handles difficulty adjustment every 2016 blocks |
| 15 | +- Supports pending difficulty epochs before finalization |
| 16 | +- Stores historical targets and supports reorg handling |
| 17 | + |
| 18 | +## Contract: `SPVContract.sol` |
| 19 | + |
| 20 | +### Key Functions |
| 21 | + |
| 22 | +#### `addBlockHeader(bytes calldata blockHeaderRaw)` |
| 23 | +Adds and validates a new block header, updates internal state, and emits an event. |
| 24 | + |
| 25 | +### Validation Rules |
| 26 | +- `prevBlockHash` must point to a known block |
| 27 | +- New `blockHash` must not exist |
| 28 | +- Header `bits` must match the expected network target |
| 29 | +- Header `time` must be > median of last 11 blocks |
| 30 | +- `blockHash` must be less than or equal to the target (valid PoW) |
| 31 | + |
| 32 | +## Storage Structure |
| 33 | + |
| 34 | +- `BlocksData` – stores block headers, timestamps, and chain height |
| 35 | +- `TargetsData` – handles target values and difficulty epochs |
| 36 | +- `pendingTargetHeightCount` – controls target finalization after N blocks |
| 37 | + |
| 38 | +## Dev Info |
| 39 | +### Compilation |
| 40 | + |
| 41 | +To compile the contracts, use the next script: |
| 42 | + |
| 43 | +```bash |
| 44 | +npm run compile |
| 45 | +``` |
| 46 | + |
| 47 | +### Test |
| 48 | + |
| 49 | +To run the tests, execute the following command: |
| 50 | + |
| 51 | +```bash |
| 52 | +npm run test |
| 53 | +``` |
| 54 | + |
| 55 | +Or to see the coverage, run: |
| 56 | + |
| 57 | +```bash |
| 58 | +npm run coverage |
| 59 | +``` |
| 60 | + |
| 61 | +### Local deployment |
| 62 | + |
| 63 | +To deploy the contracts locally, run the following commands (in the different terminals): |
| 64 | + |
| 65 | +```bash |
| 66 | +npm run private-network |
| 67 | +npm run deploy-localhost |
| 68 | +``` |
| 69 | + |
| 70 | +### Bindings |
| 71 | + |
| 72 | +The command to generate the bindings is as follows: |
| 73 | + |
| 74 | +```bash |
| 75 | +npm run generate-types |
| 76 | +``` |
0 commit comments