feat: Phase 4 — mesh authentication, envelope signature verification, TX validation

- Identity announcements: verify Ed25519 key validity and X25519 consistency
- Envelope signatures: verify Ed25519 signatures on signed messages, drop invalid
- Block header validation: height range, hash length, timestamp sanity checks
- TX relay validation: hex validity, size bounds, version check before broadcast
- Rate limiter struct for per-peer relay operations
- Message sequence number field (seq) added to TypedEnvelope for ordering

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dorian
2026-03-18 00:49:38 +00:00
parent dd8e8e9e4f
commit 5853b6a065
4 changed files with 179 additions and 6 deletions

View File

@@ -279,7 +279,7 @@
> cryptographic proof of identity (digital signatures) so every message is provably from who it claims.
> We also add checks so fake Bitcoin data can't be relayed.
- [ ] **Implement signed identity announcements**: In `core/archipelago/src/mesh/listener.rs`, find the identity advertisement handling (around line 923+). Modify the peer identity broadcast to include an Ed25519 signature:
- [x] **Implement signed identity announcements**: In `core/archipelago/src/mesh/listener.rs`, find the identity advertisement handling (around line 923+). Modify the peer identity broadcast to include an Ed25519 signature:
1. When broadcasting identity (DID + Ed25519 pubkey), sign the announcement with the node's private key:
```rust
// In the identity broadcast function
@@ -299,7 +299,7 @@
4. Update the `TypedEnvelope` struct in `message_types.rs` to include an optional `identity_signature` field if not already present.
Build and test with two mesh-connected nodes if available. If only one node, verify the code compiles and the identity broadcast includes signatures.
- [ ] **Verify envelope signatures on received messages**: In `core/archipelago/src/mesh/listener.rs`, find where incoming `TypedEnvelope` messages are processed. Add signature verification:
- [x] **Verify envelope signatures on received messages**: In `core/archipelago/src/mesh/listener.rs`, find where incoming `TypedEnvelope` messages are processed. Add signature verification:
1. Before processing any message, call `envelope.verify_signature()` (which should already exist in `message_types.rs`).
2. If verification fails, log a warning and drop the message:
```rust
@@ -311,7 +311,7 @@
3. For alert messages specifically, verify the alert is signed by the claimed peer's key before displaying or relaying.
Build and deploy.
- [ ] **Add Bitcoin transaction/block validation before relay**: In `core/archipelago/src/mesh/bitcoin_relay.rs`, find lines 210-232 where block headers and transactions are relayed:
- [x] **Add Bitcoin transaction/block validation before relay**: In `core/archipelago/src/mesh/bitcoin_relay.rs`, find lines 210-232 where block headers and transactions are relayed:
1. For block headers, add basic validation:
```rust
fn validate_block_header(header: &BlockHeader, last_known_height: u32) -> Result<bool> {
@@ -350,14 +350,14 @@
4. Call these validation functions before relaying any data.
Build and deploy.
- [ ] **Add message sequence numbers**: In `core/archipelago/src/mesh/message_types.rs`, add a `sequence: u64` field to `TypedEnvelope`:
- [x] **Add message sequence numbers**: In `core/archipelago/src/mesh/message_types.rs`, add a `sequence: u64` field to `TypedEnvelope`:
1. Add the field to the struct (with `#[serde(default)]` for backwards compatibility with old messages).
2. In the message creation code, increment a per-peer counter for each outgoing message.
3. On receive, track the last seen sequence per peer and log out-of-order messages at `debug!` level.
4. Do NOT reject out-of-order messages (mesh is unreliable), but allow upper layers to reorder if needed.
Build and deploy.
- [ ] **Verify Phase 4 — Mesh authentication active**: Run these checks:
- [x] **Verify Phase 4 — Mesh authentication active**: Run these checks:
1. `grep -rn "verify_signature\|verify_strict" core/archipelago/src/mesh/ --include="*.rs"` — should show verification calls in listener.rs and message_types.rs.
2. `grep -rn "validate_block_header\|validate_raw_transaction" core/archipelago/src/mesh/bitcoin_relay.rs` — validation functions exist.
3. `cargo test --all-features` — all mesh tests pass.