Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "qbittorrent"]
path = qbittorrent
url = https://github.com/robertpelloni/qbittorrent
21 changes: 21 additions & 0 deletions README-monorepo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Megatorrent Monorepo

This repository contains the reference implementation for the Megatorrent protocol, a decentralized, mutable successor to BitTorrent.

## Structure

* `docs/`: Architecture and Protocol Specifications.
* `tracker/`: Node.js implementation of the Tracker and Reference Client (the root of this repo, historically).
* `qbittorrent/`: C++ Client Fork (Submodule).

## Getting Started

### Node.js Tracker & Reference Client
Run the tracker and client tests:
```bash
npm install
npm test
```

### qBittorrent Client
See `qbittorrent/README.md` for C++ build instructions.
49 changes: 49 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

## 6. Obfuscated Storage Protocol (Phase 2)
To achieve plausible deniability and distributed redundancy, data is not stored as "files" but as "Blobs" of high-entropy data.

### Concepts
* **Blob**: A fixed-size (or variable) container stored by a Host. To the Host, it is just random bytes.
* **Chunk**: A segment of a user's file.
* **Encryption**: Each Chunk is encrypted with a *unique* random key (ChaCha20-Poly1305) before being placed into a Blob.
* **Muxing**: A Blob may contain multiple Chunks (potentially from different files) or padding.

### Data Structure: The `FileEntry`
This structure replaces the simple "magnet link" in the Megatorrent Manifest.

```json
{
"name": "episode1.mkv",
"mime": "video/x-matroska",
"size": 104857600,
"chunks": [
{
"blobId": "<SHA256 Hash of the Blob>",
"offset": 0, // Byte offset in the Blob
"length": 1048576, // Length of the encrypted chunk
"key": "<32-byte Hex Key>",
"nonce": "<12-byte Hex Nonce>",
"authTag": "<16-byte Hex Tag>"
},
...
]
}
```

### Process
1. **Ingest**:
* File is split into N chunks.
* For each chunk:
* Generate random Key & Nonce.
* Encrypt Chunk -> EncryptedChunk.
* (Simplification for Ref Impl) EncryptedChunk becomes a "Blob" directly (or is wrapped).
* Blob ID = SHA256(Blob).
* Result: A list of Blobs (to be uploaded) and a `FileEntry` (to be put in the Manifest).

2. **Access**:
* Subscriber receives Manifest.
* Parses `FileEntry`.
* Requests Blob(ID) from the network (simulated via local dir or tracker relay).
* Extracts bytes at `offset` for `length`.
* Decrypts using `key` and `nonce`.
* Reassembles file.
37 changes: 37 additions & 0 deletions lib/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import stringify from 'fast-json-stable-stringify'
import sodium from 'sodium-native'

export function verify (message, signature, publicKey) {
const msgBuffer = Buffer.isBuffer(message) ? message : Buffer.from(message)
return sodium.crypto_sign_verify_detached(signature, msgBuffer, publicKey)
}

export function validateManifest (manifest) {
if (!manifest || typeof manifest !== 'object') throw new Error('Invalid manifest')
if (!manifest.publicKey || !manifest.signature) throw new Error('Missing keys')

// Validation
if (typeof manifest.publicKey !== 'string' || !/^[0-9a-fA-F]{64}$/.test(manifest.publicKey)) {
throw new Error('Invalid public key format')
}
if (typeof manifest.signature !== 'string' || !/^[0-9a-fA-F]{128}$/.test(manifest.signature)) {
throw new Error('Invalid signature format')
}
if (typeof manifest.sequence !== 'number') throw new Error('Invalid sequence')
if (typeof manifest.timestamp !== 'number') throw new Error('Invalid timestamp')
if (!Array.isArray(manifest.collections)) throw new Error('Invalid collections')

// Reconstruct the payload to verify (exclude signature)
const payload = {
publicKey: manifest.publicKey,
sequence: manifest.sequence,
timestamp: manifest.timestamp,
collections: manifest.collections
}

const jsonString = stringify(payload)
const publicKey = Buffer.from(manifest.publicKey, 'hex')
const signature = Buffer.from(manifest.signature, 'hex')
Comment on lines +14 to +34
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern allows both uppercase and lowercase hex characters, but the error handling doesn't normalize case before validation. While this is technically correct, consider documenting this behavior or normalizing to lowercase consistently throughout the codebase for consistency with how keys are generated and stored elsewhere.

Suggested change
if (typeof manifest.publicKey !== 'string' || !/^[0-9a-fA-F]{64}$/.test(manifest.publicKey)) {
throw new Error('Invalid public key format')
}
if (typeof manifest.signature !== 'string' || !/^[0-9a-fA-F]{128}$/.test(manifest.signature)) {
throw new Error('Invalid signature format')
}
if (typeof manifest.sequence !== 'number') throw new Error('Invalid sequence')
if (typeof manifest.timestamp !== 'number') throw new Error('Invalid timestamp')
if (!Array.isArray(manifest.collections)) throw new Error('Invalid collections')
// Reconstruct the payload to verify (exclude signature)
const payload = {
publicKey: manifest.publicKey,
sequence: manifest.sequence,
timestamp: manifest.timestamp,
collections: manifest.collections
}
const jsonString = stringify(payload)
const publicKey = Buffer.from(manifest.publicKey, 'hex')
const signature = Buffer.from(manifest.signature, 'hex')
if (typeof manifest.publicKey !== 'string') {
throw new Error('Invalid public key format')
}
if (typeof manifest.signature !== 'string') {
throw new Error('Invalid signature format')
}
// Normalize hex strings to lowercase for consistent handling
const publicKeyHex = manifest.publicKey.toLowerCase()
const signatureHex = manifest.signature.toLowerCase()
if (!/^[0-9a-f]{64}$/.test(publicKeyHex)) {
throw new Error('Invalid public key format')
}
if (!/^[0-9a-f]{128}$/.test(signatureHex)) {
throw new Error('Invalid signature format')
}
if (typeof manifest.sequence !== 'number') throw new Error('Invalid sequence')
if (typeof manifest.timestamp !== 'number') throw new Error('Invalid timestamp')
if (!Array.isArray(manifest.collections)) throw new Error('Invalid collections')
// Reconstruct the payload to verify (exclude signature)
const payload = {
publicKey: publicKeyHex,
sequence: manifest.sequence,
timestamp: manifest.timestamp,
collections: manifest.collections
}
const jsonString = stringify(payload)
const publicKey = Buffer.from(publicKeyHex, 'hex')
const signature = Buffer.from(signatureHex, 'hex')

Copilot uses AI. Check for mistakes.

return verify(jsonString, signature, publicKey)
}
6 changes: 6 additions & 0 deletions lib/server/parse-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export default function (socket, opts, params) {
return bin2hex(binaryInfoHash)
})
}
} else if (params.action === 'publish') {
// Custom Action: Publish Manifest
if (!params.manifest) throw new Error('missing manifest')
} else if (params.action === 'subscribe') {
// Custom Action: Subscribe to Key
if (!params.key) throw new Error('missing key')
} else {
throw new Error(`invalid action in WS request: ${params.action}`)
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"compact2string": "^1.4.1",
"cross-fetch-ponyfill": "^1.0.3",
"debug": "^4.3.4",
"fast-json-stable-stringify": "^2.1.0",
"ip": "^2.0.1",
"lru": "^3.1.0",
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codebase uses 'lru-cache' in server.js (line 18) but package.json declares 'lru' as a dependency. These are different packages. The 'lru-cache' package should be added to dependencies to match the import statement.

Suggested change
"lru": "^3.1.0",
"lru": "^3.1.0",
"lru-cache": "^7.18.3",

Copilot uses AI. Check for mistakes.
"minimist": "^1.2.8",
Expand All @@ -44,6 +45,7 @@
"run-parallel": "^1.2.0",
"run-series": "^1.1.9",
"socks": "^2.8.3",
"sodium-native": "^5.0.10",
"string2compact": "^2.0.1",
"uint8-util": "^2.2.5",
"unordered-array-remove": "^1.0.2",
Expand All @@ -52,7 +54,7 @@
"devDependencies": {
"@mapbox/node-pre-gyp": "1.0.11",
"@webtorrent/semantic-release-config": "1.0.10",
"magnet-uri": "7.0.7",
"magnet-uri": "^7.0.7",
"semantic-release": "21.1.2",
"standard": "*",
"tape": "5.9.0",
Expand Down
1 change: 1 addition & 0 deletions qbittorrent
Submodule qbittorrent added at 5abf45
Loading
Loading