Updates
This commit is contained in:
@@ -1,28 +1,33 @@
|
||||
# hyper-p2p-blind-pair-handoff
|
||||
|
||||
**Status:** scaffold (`0.0.0-scaffold`) · **Protocol:** `blind-pair-handoff/v1` · **Wave:** 8
|
||||
Production module: Blind pair handoff.
|
||||
|
||||
Blind pairing handoff.
|
||||
**Protocol:** `blind-pair-handoff/v1`
|
||||
|
||||
## Holepunch references (inspiration only)
|
||||
## When to use
|
||||
|
||||
- `blind-pairing`
|
||||
Peer session migration.
|
||||
|
||||
> This module composes on Hyperswarm/Hypercore — it does **not** re-implement upstream packages.
|
||||
## When not to use
|
||||
|
||||
## Composes with
|
||||
Direct connect only.
|
||||
|
||||
- `hyper-p2p-session-bridge`
|
||||
## Quick start
|
||||
|
||||
## Planned API
|
||||
```js
|
||||
const { HyperP2PBlindPairHandoff } = require('hyper-p2p-blind-pair-handoff')
|
||||
const m = new HyperP2PBlindPairHandoff()
|
||||
await m.ready()
|
||||
await m.close()
|
||||
```
|
||||
|
||||
- `constructor(opts)` — topic, optional keyPair
|
||||
- `getStats()` — scaffold counters
|
||||
- `ready()` — no-op until implemented
|
||||
- Domain methods — throw `not implemented: scaffold` until Wave 8+ pass
|
||||
## Docs
|
||||
|
||||
## Layout
|
||||
- [docs/api.md](docs/api.md)
|
||||
- [docs/architecture.md](docs/architecture.md)
|
||||
|
||||
`modules/trust-security/hyper-p2p-blind-pair-handoff/`
|
||||
## Test
|
||||
|
||||
See [`modules/_shared/MODULE_SYSTEM.md`](../../_shared/MODULE_SYSTEM.md).
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
# hyper-p2p-blind-pair-handoff API
|
||||
# API: hyper-p2p-blind-pair-handoff
|
||||
|
||||
**Status:** scaffold · **Protocol:** `blind-pair-handoff/v1`
|
||||
**Protocol:** `blind-pair-handoff/v1` · **Export:** `HyperP2PBlindPairHandoff`
|
||||
|
||||
## Class `HyperP2PBlindPairHandoff`
|
||||
## Methods
|
||||
|
||||
Scaffold stub — methods throw `not implemented: scaffold` until promoted to production tier.
|
||||
### `offerHandoff(...)`
|
||||
|
||||
### `constructor(opts?)`
|
||||
Domain API.
|
||||
|
||||
### `getStats()`
|
||||
### `acceptHandoff(...)`
|
||||
|
||||
Returns `{ created, errors, protocol, tier: 'scaffold' }`.
|
||||
Domain API.
|
||||
|
||||
### `ready()`
|
||||
### `completeHandoff(...)`
|
||||
|
||||
Resolves immediately (no-op).
|
||||
Domain API.
|
||||
|
||||
## Wire (planned)
|
||||
|
||||
| Message | Direction | Notes |
|
||||
|---------|-----------|-------|
|
||||
| TBD | gossip | Defined in implementation pass |
|
||||
### `getStats()` / `ready()` / `close()`
|
||||
|
||||
Lifecycle helpers.
|
||||
|
||||
## P2P
|
||||
|
||||
Joins Hyperswarm when `topic` is set.
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
# hyper-p2p-blind-pair-handoff architecture
|
||||
# Architecture: hyper-p2p-blind-pair-handoff
|
||||
|
||||
**Tier:** scaffold · **Category:** `trust-security`
|
||||
## Wire messages
|
||||
|
||||
## Role
|
||||
| type | fields | direction | behavior |
|
||||
|------|--------|-----------|----------|
|
||||
| `handoff-offer` | sessionId, targetPeer | gossip | Offer |
|
||||
| `handoff-accept` | sessionId | gossip | Accept |
|
||||
| `handoff-complete` | sessionId | gossip | Done |
|
||||
|
||||
Blind pairing handoff.
|
||||
## State
|
||||
|
||||
## Composition
|
||||
|
||||
Uses `../../_shared/p2p-bare.js` for Hyperswarm + Protomux when implemented. Does **not** duplicate Holepunch core storage/transport.
|
||||
|
||||
## Holepunch boundary
|
||||
|
||||
Inspiration: n/a
|
||||
In-memory structures; gossip via `initModuleSwarm` / `gossipSend` when P2P enabled.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PBlindPairHandoff } = require('../index.js')
|
||||
|
||||
async function main () {
|
||||
const m = new HyperP2PBlindPairHandoff()
|
||||
console.log('[scaffold]', m.getStats())
|
||||
console.log(m.getStats())
|
||||
await m.close()
|
||||
}
|
||||
main().catch(console.error)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'blind-pair-handoff/v1'
|
||||
|
||||
@@ -10,50 +10,117 @@ class HyperP2PBlindPairHandoff extends EventEmitter {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this._handoffs = new Map()
|
||||
this._stats = { offered: 0, accepted: 0, completed: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'blind-pair-handoff-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
offerHandoff (sessionId, targetPeer) {
|
||||
assertNonEmpty(sessionId, 'sessionId')
|
||||
assertNonEmpty(targetPeer, 'targetPeer')
|
||||
const handoff = {
|
||||
sessionId,
|
||||
targetPeer,
|
||||
state: 'offered',
|
||||
offeredAt: Date.now(),
|
||||
acceptedAt: null,
|
||||
completedAt: null
|
||||
}
|
||||
this._handoffs.set(sessionId, handoff)
|
||||
this._stats.offered++
|
||||
this._gossip({ type: 'handoff-offer', sessionId, targetPeer, offeredAt: handoff.offeredAt })
|
||||
this.emit('offered', handoff)
|
||||
return handoff
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'blind-pair-handoff-sync', key, value: null })
|
||||
return ok
|
||||
acceptHandoff (sessionId) {
|
||||
assertNonEmpty(sessionId, 'sessionId')
|
||||
const h = this._handoffs.get(sessionId)
|
||||
if (!h) throw new Error(`handoff not found: ${sessionId}`)
|
||||
if (h.state !== 'offered') throw new Error(`handoff not offered: ${sessionId}`)
|
||||
h.state = 'accepted'
|
||||
h.acceptedAt = Date.now()
|
||||
this._stats.accepted++
|
||||
this._gossip({ type: 'handoff-accept', sessionId, acceptedAt: h.acceptedAt })
|
||||
this.emit('accepted', h)
|
||||
return h
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
completeHandoff (sessionId) {
|
||||
assertNonEmpty(sessionId, 'sessionId')
|
||||
const h = this._handoffs.get(sessionId)
|
||||
if (!h) throw new Error(`handoff not found: ${sessionId}`)
|
||||
if (h.state !== 'accepted') throw new Error(`handoff not accepted: ${sessionId}`)
|
||||
h.state = 'completed'
|
||||
h.completedAt = Date.now()
|
||||
this._stats.completed++
|
||||
this._gossip({ type: 'handoff-complete', sessionId, completedAt: h.completedAt })
|
||||
this.emit('completed', h)
|
||||
return h
|
||||
}
|
||||
|
||||
getHandoff (sessionId) {
|
||||
assertNonEmpty(sessionId, 'sessionId')
|
||||
return this._handoffs.get(sessionId) || null
|
||||
}
|
||||
|
||||
_gossip (data) {
|
||||
if (!this._peerMsgs) return
|
||||
gossipSend(this, data)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'blind-pair-handoff-sync') return
|
||||
if (!d || !d.type || !d.sessionId) return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
if (d.type === 'handoff-offer') {
|
||||
if (!this._handoffs.has(d.sessionId)) {
|
||||
this._handoffs.set(d.sessionId, {
|
||||
sessionId: d.sessionId,
|
||||
targetPeer: d.targetPeer,
|
||||
state: 'offered',
|
||||
offeredAt: d.offeredAt || Date.now(),
|
||||
acceptedAt: null,
|
||||
completedAt: null
|
||||
})
|
||||
}
|
||||
}
|
||||
if (d.type === 'handoff-accept') {
|
||||
const h = this._handoffs.get(d.sessionId)
|
||||
if (h && h.state === 'offered') {
|
||||
h.state = 'accepted'
|
||||
h.acceptedAt = d.acceptedAt || Date.now()
|
||||
}
|
||||
}
|
||||
if (d.type === 'handoff-complete') {
|
||||
const h = this._handoffs.get(d.sessionId)
|
||||
if (h && h.state === 'accepted') {
|
||||
h.state = 'completed'
|
||||
h.completedAt = d.completedAt || Date.now()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
getStats () {
|
||||
return { ...this._stats, handoffs: this._handoffs.size, protocol: PROTOCOL }
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (d) => this._onGossip(d)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,33 @@ const { HyperP2PBlindPairHandoff, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PBlindPairHandoff)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'blind-pair-handoff/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('handoff lifecycle', async (t) => {
|
||||
const m = new HyperP2PBlindPairHandoff()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
m.offerHandoff('sess-1', 'peer-b')
|
||||
m.acceptHandoff('sess-1')
|
||||
const done = m.completeHandoff('sess-1')
|
||||
t.is(done.state, 'completed')
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('accept without offer throws', async (t) => {
|
||||
const m = new HyperP2PBlindPairHandoff()
|
||||
try { m.acceptHandoff('missing') } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PBlindPairHandoff()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
try { m.offerHandoff(null, 'p') } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PBlindPairHandoff()
|
||||
t.ok(m.getStats().protocol)
|
||||
m.offerHandoff('s', 'p')
|
||||
t.is(m.getStats().offered, 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,28 +1,34 @@
|
||||
# hyper-p2p-encrypted-topic
|
||||
|
||||
**Status:** scaffold (`0.0.0-scaffold`) · **Protocol:** `encrypted-topic/v1` · **Wave:** 8
|
||||
Production module: Topic encryption demo.
|
||||
|
||||
Encrypted topic wrapper.
|
||||
**Protocol:** `encrypted-topic/v1`
|
||||
|
||||
## Holepunch references (inspiration only)
|
||||
## When to use
|
||||
|
||||
- `blind-encryption-sodium`
|
||||
Topic-scoped payload crypto.
|
||||
|
||||
> This module composes on Hyperswarm/Hypercore — it does **not** re-implement upstream packages.
|
||||
## When not to use
|
||||
|
||||
## Composes with
|
||||
Production AEAD.
|
||||
|
||||
- `hyper-p2p-presence`
|
||||
## Quick start
|
||||
|
||||
## Planned API
|
||||
```js
|
||||
const { HyperP2PEncryptedTopic } = require('hyper-p2p-encrypted-topic')
|
||||
const m = new HyperP2PEncryptedTopic()
|
||||
m.registerTopic('t','hint')
|
||||
await m.ready()
|
||||
await m.close()
|
||||
```
|
||||
|
||||
- `constructor(opts)` — topic, optional keyPair
|
||||
- `getStats()` — scaffold counters
|
||||
- `ready()` — no-op until implemented
|
||||
- Domain methods — throw `not implemented: scaffold` until Wave 8+ pass
|
||||
## Docs
|
||||
|
||||
## Layout
|
||||
- [docs/api.md](docs/api.md)
|
||||
- [docs/architecture.md](docs/architecture.md)
|
||||
|
||||
`modules/trust-security/hyper-p2p-encrypted-topic/`
|
||||
## Test
|
||||
|
||||
See [`modules/_shared/MODULE_SYSTEM.md`](../../_shared/MODULE_SYSTEM.md).
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
# hyper-p2p-encrypted-topic API
|
||||
# API: hyper-p2p-encrypted-topic
|
||||
|
||||
**Status:** scaffold · **Protocol:** `encrypted-topic/v1`
|
||||
**Protocol:** `encrypted-topic/v1` · **Export:** `HyperP2PEncryptedTopic`
|
||||
|
||||
## Class `HyperP2PEncryptedTopic`
|
||||
## Methods
|
||||
|
||||
Scaffold stub — methods throw `not implemented: scaffold` until promoted to production tier.
|
||||
### `registerTopic(...)`
|
||||
|
||||
### `constructor(opts?)`
|
||||
Domain API.
|
||||
|
||||
### `getStats()`
|
||||
### `encryptPayload(...)`
|
||||
|
||||
Returns `{ created, errors, protocol, tier: 'scaffold' }`.
|
||||
Domain API.
|
||||
|
||||
### `ready()`
|
||||
### `decryptPayload(...)`
|
||||
|
||||
Resolves immediately (no-op).
|
||||
Domain API.
|
||||
|
||||
## Wire (planned)
|
||||
|
||||
| Message | Direction | Notes |
|
||||
|---------|-----------|-------|
|
||||
| TBD | gossip | Defined in implementation pass |
|
||||
### `getStats()` / `ready()` / `close()`
|
||||
|
||||
Lifecycle helpers.
|
||||
|
||||
## P2P
|
||||
|
||||
Joins Hyperswarm when `topic` is set.
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
# hyper-p2p-encrypted-topic architecture
|
||||
# Architecture: hyper-p2p-encrypted-topic
|
||||
|
||||
**Tier:** scaffold · **Category:** `trust-security`
|
||||
## Wire messages
|
||||
|
||||
## Role
|
||||
| type | fields | direction | behavior |
|
||||
|------|--------|-----------|----------|
|
||||
| `topic-register` | topicId, keyHint | gossip | Register |
|
||||
|
||||
Encrypted topic wrapper.
|
||||
## State
|
||||
|
||||
## Composition
|
||||
|
||||
Uses `../../_shared/p2p-bare.js` for Hyperswarm + Protomux when implemented. Does **not** duplicate Holepunch core storage/transport.
|
||||
|
||||
## Holepunch boundary
|
||||
|
||||
Inspiration: n/a
|
||||
In-memory structures; gossip via `initModuleSwarm` / `gossipSend` when P2P enabled.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PEncryptedTopic } = require('../index.js')
|
||||
|
||||
async function main () {
|
||||
const m = new HyperP2PEncryptedTopic()
|
||||
console.log('[scaffold]', m.getStats())
|
||||
console.log(m.getStats())
|
||||
await m.close()
|
||||
}
|
||||
main().catch(console.error)
|
||||
|
||||
@@ -1,60 +1,105 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const crypto = require('hypercore-crypto')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'encrypted-topic/v1'
|
||||
|
||||
function deriveKey (hint) {
|
||||
return crypto.hash(b4a.from(String(hint)))
|
||||
}
|
||||
|
||||
function xorCrypt (keyBuf, dataBuf) {
|
||||
const out = b4a.alloc(dataBuf.length)
|
||||
for (let i = 0; i < dataBuf.length; i++) {
|
||||
out[i] = dataBuf[i] ^ keyBuf[i % keyBuf.length]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
class HyperP2PEncryptedTopic extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.keyPair = opts.keyPair || crypto.keyPair()
|
||||
this._topics = new Map()
|
||||
this._stats = { registered: 0, encrypted: 0, decrypted: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'encrypted-topic-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
registerTopic (topicId, keyHint) {
|
||||
assertNonEmpty(topicId, 'topicId')
|
||||
assertNonEmpty(keyHint, 'keyHint')
|
||||
const entry = { topicId, keyHint, key: deriveKey(keyHint), registeredAt: Date.now() }
|
||||
this._topics.set(topicId, entry)
|
||||
this._stats.registered++
|
||||
this._gossip({ type: 'topic-register', topicId, keyHint })
|
||||
this.emit('registered', { topicId, keyHint })
|
||||
return entry
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'encrypted-topic-sync', key, value: null })
|
||||
return ok
|
||||
encryptPayload (topicId, data) {
|
||||
assertNonEmpty(topicId, 'topicId')
|
||||
const entry = this._topics.get(topicId)
|
||||
if (!entry) throw new Error(`topic not registered: ${topicId}`)
|
||||
const plain = b4a.isBuffer(data) ? data : b4a.from(data)
|
||||
const cipher = xorCrypt(entry.key, plain)
|
||||
this._stats.encrypted++
|
||||
return cipher
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
decryptPayload (topicId, buf) {
|
||||
assertNonEmpty(topicId, 'topicId')
|
||||
if (!b4a.isBuffer(buf)) throw new Error('buf must be a buffer')
|
||||
const entry = this._topics.get(topicId)
|
||||
if (!entry) throw new Error(`topic not registered: ${topicId}`)
|
||||
const plain = xorCrypt(entry.key, buf)
|
||||
this._stats.decrypted++
|
||||
return plain
|
||||
}
|
||||
|
||||
_gossip (data) {
|
||||
if (!this._peerMsgs) return
|
||||
gossipSend(this, data)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'encrypted-topic-sync') return
|
||||
if (!d || d.type !== 'topic-register' || !d.topicId) return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
if (!this._topics.has(d.topicId)) {
|
||||
this._topics.set(d.topicId, {
|
||||
topicId: d.topicId,
|
||||
keyHint: d.keyHint,
|
||||
key: deriveKey(d.keyHint),
|
||||
registeredAt: Date.now()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
getStats () {
|
||||
return { ...this._stats, topics: this._topics.size, protocol: PROTOCOL }
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (d) => this._onGossip(d)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { HyperP2PEncryptedTopic, PROTOCOL }
|
||||
module.exports = { HyperP2PEncryptedTopic, PROTOCOL, deriveKey, xorCrypt }
|
||||
|
||||
@@ -1,26 +1,39 @@
|
||||
require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const b4a = require('b4a')
|
||||
const { HyperP2PEncryptedTopic, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PEncryptedTopic)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'encrypted-topic/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('encrypt decrypt roundtrip', async (t) => {
|
||||
const m = new HyperP2PEncryptedTopic()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
m.registerTopic('secret', 'hint-abc')
|
||||
const plain = b4a.from('hello peers')
|
||||
const cipher = m.encryptPayload('secret', plain)
|
||||
const out = m.decryptPayload('secret', cipher)
|
||||
t.alike(out, plain)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('unregistered topic throws', async (t) => {
|
||||
const m = new HyperP2PEncryptedTopic()
|
||||
try { m.encryptPayload('nope', 'x') } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PEncryptedTopic()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
try { m.registerTopic(null, 'h') } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PEncryptedTopic()
|
||||
t.ok(m.getStats().protocol)
|
||||
m.registerTopic('t', 'h')
|
||||
m.encryptPayload('t', 'a')
|
||||
t.is(m.getStats().encrypted, 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,28 +1,34 @@
|
||||
# hyper-p2p-key-rotation
|
||||
|
||||
**Status:** scaffold (`0.0.0-scaffold`) · **Protocol:** `key-rotation/v1` · **Wave:** 8
|
||||
Production module: Key rotation scheduling.
|
||||
|
||||
Key rotation gossip.
|
||||
**Protocol:** `key-rotation/v1`
|
||||
|
||||
## Holepunch references (inspiration only)
|
||||
## When to use
|
||||
|
||||
- `hypercore-crypto`
|
||||
Rotating keys on a timeline.
|
||||
|
||||
> This module composes on Hyperswarm/Hypercore — it does **not** re-implement upstream packages.
|
||||
## When not to use
|
||||
|
||||
## Composes with
|
||||
Static keys only.
|
||||
|
||||
- `hyper-p2p-capabilities`
|
||||
## Quick start
|
||||
|
||||
## Planned API
|
||||
```js
|
||||
const { HyperP2PKeyRotation } = require('hyper-p2p-key-rotation')
|
||||
const m = new HyperP2PKeyRotation()
|
||||
m.scheduleRotation('k', 'mat', Date.now())
|
||||
await m.ready()
|
||||
await m.close()
|
||||
```
|
||||
|
||||
- `constructor(opts)` — topic, optional keyPair
|
||||
- `getStats()` — scaffold counters
|
||||
- `ready()` — no-op until implemented
|
||||
- Domain methods — throw `not implemented: scaffold` until Wave 8+ pass
|
||||
## Docs
|
||||
|
||||
## Layout
|
||||
- [docs/api.md](docs/api.md)
|
||||
- [docs/architecture.md](docs/architecture.md)
|
||||
|
||||
`modules/trust-security/hyper-p2p-key-rotation/`
|
||||
## Test
|
||||
|
||||
See [`modules/_shared/MODULE_SYSTEM.md`](../../_shared/MODULE_SYSTEM.md).
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
# hyper-p2p-key-rotation API
|
||||
# API: hyper-p2p-key-rotation
|
||||
|
||||
**Status:** scaffold · **Protocol:** `key-rotation/v1`
|
||||
**Protocol:** `key-rotation/v1` · **Export:** `HyperP2PKeyRotation`
|
||||
|
||||
## Class `HyperP2PKeyRotation`
|
||||
## Methods
|
||||
|
||||
Scaffold stub — methods throw `not implemented: scaffold` until promoted to production tier.
|
||||
### `scheduleRotation(...)`
|
||||
|
||||
### `constructor(opts?)`
|
||||
Domain API.
|
||||
|
||||
### `getStats()`
|
||||
### `activeKey(...)`
|
||||
|
||||
Returns `{ created, errors, protocol, tier: 'scaffold' }`.
|
||||
Domain API.
|
||||
|
||||
### `ready()`
|
||||
### `pendingRotations(...)`
|
||||
|
||||
Resolves immediately (no-op).
|
||||
Domain API.
|
||||
|
||||
## Wire (planned)
|
||||
|
||||
| Message | Direction | Notes |
|
||||
|---------|-----------|-------|
|
||||
| TBD | gossip | Defined in implementation pass |
|
||||
### `getStats()` / `ready()` / `close()`
|
||||
|
||||
Lifecycle helpers.
|
||||
|
||||
## P2P
|
||||
|
||||
Joins Hyperswarm when `topic` is set.
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
# hyper-p2p-key-rotation architecture
|
||||
# Architecture: hyper-p2p-key-rotation
|
||||
|
||||
**Tier:** scaffold · **Category:** `trust-security`
|
||||
## Wire messages
|
||||
|
||||
## Role
|
||||
| type | fields | direction | behavior |
|
||||
|------|--------|-----------|----------|
|
||||
| `rotation-scheduled` | keyId, newMaterial, activateAt | gossip | Schedule |
|
||||
| `rotation-activated` | keyId, material, activatedAt | gossip | Activate |
|
||||
|
||||
Key rotation gossip.
|
||||
## State
|
||||
|
||||
## Composition
|
||||
|
||||
Uses `../../_shared/p2p-bare.js` for Hyperswarm + Protomux when implemented. Does **not** duplicate Holepunch core storage/transport.
|
||||
|
||||
## Holepunch boundary
|
||||
|
||||
Inspiration: n/a
|
||||
In-memory structures; gossip via `initModuleSwarm` / `gossipSend` when P2P enabled.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PKeyRotation } = require('../index.js')
|
||||
|
||||
async function main () {
|
||||
const m = new HyperP2PKeyRotation()
|
||||
console.log('[scaffold]', m.getStats())
|
||||
console.log(m.getStats())
|
||||
await m.close()
|
||||
}
|
||||
main().catch(console.error)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'key-rotation/v1'
|
||||
|
||||
@@ -10,50 +10,112 @@ class HyperP2PKeyRotation extends EventEmitter {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this._active = new Map()
|
||||
this._pending = new Map()
|
||||
this._stats = { scheduled: 0, activated: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'key-rotation-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
scheduleRotation (keyId, newMaterial, activateAt) {
|
||||
assertNonEmpty(keyId, 'keyId')
|
||||
assertNonEmpty(newMaterial, 'newMaterial')
|
||||
const at = activateAt == null ? Date.now() : activateAt
|
||||
const entry = { keyId, newMaterial, activateAt: at, scheduledAt: Date.now() }
|
||||
const list = this._pending.get(keyId) || []
|
||||
list.push(entry)
|
||||
this._pending.set(keyId, list)
|
||||
this._stats.scheduled++
|
||||
this._maybeActivate(keyId)
|
||||
this._gossip({ type: 'rotation-scheduled', keyId, newMaterial, activateAt: at })
|
||||
this.emit('scheduled', entry)
|
||||
return entry
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'key-rotation-sync', key, value: null })
|
||||
return ok
|
||||
activeKey (keyId) {
|
||||
assertNonEmpty(keyId, 'keyId')
|
||||
this._maybeActivate(keyId)
|
||||
return this._active.get(keyId) || null
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
pendingRotations () {
|
||||
const out = []
|
||||
const now = Date.now()
|
||||
for (const [keyId, list] of this._pending) {
|
||||
for (const e of list) {
|
||||
if (e.activateAt > now) out.push({ ...e })
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'key-rotation-sync') return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
_maybeActivate (keyId) {
|
||||
const list = this._pending.get(keyId) || []
|
||||
const now = Date.now()
|
||||
for (const e of list) {
|
||||
if (e.activateAt <= now) {
|
||||
const active = { keyId, material: e.newMaterial, activatedAt: now }
|
||||
this._active.set(keyId, active)
|
||||
this._stats.activated++
|
||||
this._gossip({ type: 'rotation-activated', keyId, material: e.newMaterial, activatedAt: now })
|
||||
this.emit('activated', active)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
_gossip (data) {
|
||||
if (!this._peerMsgs) return
|
||||
gossipSend(this, data)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || !d.type) return
|
||||
this._stats.gossipIn++
|
||||
if (d.type === 'rotation-scheduled' && d.keyId) {
|
||||
const list = this._pending.get(d.keyId) || []
|
||||
list.push({
|
||||
keyId: d.keyId,
|
||||
newMaterial: d.newMaterial,
|
||||
activateAt: d.activateAt,
|
||||
scheduledAt: Date.now()
|
||||
})
|
||||
this._pending.set(d.keyId, list)
|
||||
this._maybeActivate(d.keyId)
|
||||
}
|
||||
if (d.type === 'rotation-activated' && d.keyId) {
|
||||
this._active.set(d.keyId, {
|
||||
keyId: d.keyId,
|
||||
material: d.material,
|
||||
activatedAt: d.activatedAt || Date.now()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return {
|
||||
...this._stats,
|
||||
activeKeys: this._active.size,
|
||||
pending: this.pendingRotations().length,
|
||||
protocol: PROTOCOL
|
||||
}
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (d) => this._onGossip(d)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,35 @@ const { HyperP2PKeyRotation, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PKeyRotation)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'key-rotation/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('scheduleRotation activates immediately', async (t) => {
|
||||
const m = new HyperP2PKeyRotation()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
m.scheduleRotation('main', 'mat-v2', Date.now())
|
||||
const active = m.activeKey('main')
|
||||
t.is(active.material, 'mat-v2')
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('pendingRotations future only', async (t) => {
|
||||
const m = new HyperP2PKeyRotation()
|
||||
m.scheduleRotation('k', 'future', Date.now() + 60000)
|
||||
t.is(m.pendingRotations().length, 1)
|
||||
t.is(m.activeKey('k'), null)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PKeyRotation()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
try { m.scheduleRotation(null, 'x', 0) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PKeyRotation()
|
||||
t.ok(m.getStats().protocol)
|
||||
m.scheduleRotation('a', 'b', Date.now())
|
||||
t.is(m.getStats().protocol, 'key-rotation/v1')
|
||||
t.is(m.getStats().scheduled, 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,28 +1,33 @@
|
||||
# hyper-p2p-multisig-threshold
|
||||
|
||||
**Status:** scaffold (`0.0.0-scaffold`) · **Protocol:** `multisig-threshold/v1` · **Wave:** 8
|
||||
Production module: Threshold multisig.
|
||||
|
||||
Threshold multisig helper.
|
||||
**Protocol:** `multisig-threshold/v1`
|
||||
|
||||
## Holepunch references (inspiration only)
|
||||
## When to use
|
||||
|
||||
- `hyper-multisig`
|
||||
M-of-N approvals.
|
||||
|
||||
> This module composes on Hyperswarm/Hypercore — it does **not** re-implement upstream packages.
|
||||
## When not to use
|
||||
|
||||
## Composes with
|
||||
Single signer.
|
||||
|
||||
- `hyper-p2p-quorum-pool`
|
||||
## Quick start
|
||||
|
||||
## Planned API
|
||||
```js
|
||||
const { HyperP2PMultisigThreshold } = require('hyper-p2p-multisig-threshold')
|
||||
const m = new HyperP2PMultisigThreshold()
|
||||
await m.ready()
|
||||
await m.close()
|
||||
```
|
||||
|
||||
- `constructor(opts)` — topic, optional keyPair
|
||||
- `getStats()` — scaffold counters
|
||||
- `ready()` — no-op until implemented
|
||||
- Domain methods — throw `not implemented: scaffold` until Wave 8+ pass
|
||||
## Docs
|
||||
|
||||
## Layout
|
||||
- [docs/api.md](docs/api.md)
|
||||
- [docs/architecture.md](docs/architecture.md)
|
||||
|
||||
`modules/trust-security/hyper-p2p-multisig-threshold/`
|
||||
## Test
|
||||
|
||||
See [`modules/_shared/MODULE_SYSTEM.md`](../../_shared/MODULE_SYSTEM.md).
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
# hyper-p2p-multisig-threshold API
|
||||
# API: hyper-p2p-multisig-threshold
|
||||
|
||||
**Status:** scaffold · **Protocol:** `multisig-threshold/v1`
|
||||
**Protocol:** `multisig-threshold/v1` · **Export:** `HyperP2PMultisigThreshold`
|
||||
|
||||
## Class `HyperP2PMultisigThreshold`
|
||||
## Methods
|
||||
|
||||
Scaffold stub — methods throw `not implemented: scaffold` until promoted to production tier.
|
||||
### `createProposal(...)`
|
||||
|
||||
### `constructor(opts?)`
|
||||
Domain API.
|
||||
|
||||
### `getStats()`
|
||||
### `addSignature(...)`
|
||||
|
||||
Returns `{ created, errors, protocol, tier: 'scaffold' }`.
|
||||
Domain API.
|
||||
|
||||
### `ready()`
|
||||
### `isApproved(...)`
|
||||
|
||||
Resolves immediately (no-op).
|
||||
Domain API.
|
||||
|
||||
## Wire (planned)
|
||||
|
||||
| Message | Direction | Notes |
|
||||
|---------|-----------|-------|
|
||||
| TBD | gossip | Defined in implementation pass |
|
||||
### `getStats()` / `ready()` / `close()`
|
||||
|
||||
Lifecycle helpers.
|
||||
|
||||
## P2P
|
||||
|
||||
Joins Hyperswarm when `topic` is set.
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
# hyper-p2p-multisig-threshold architecture
|
||||
# Architecture: hyper-p2p-multisig-threshold
|
||||
|
||||
**Tier:** scaffold · **Category:** `trust-security`
|
||||
## Wire messages
|
||||
|
||||
## Role
|
||||
| type | fields | direction | behavior |
|
||||
|------|--------|-----------|----------|
|
||||
| `proposal-create` | id, signers, threshold | gossip | Create |
|
||||
| `proposal-sig` | id, signer | gossip | Sign |
|
||||
|
||||
Threshold multisig helper.
|
||||
## State
|
||||
|
||||
## Composition
|
||||
|
||||
Uses `../../_shared/p2p-bare.js` for Hyperswarm + Protomux when implemented. Does **not** duplicate Holepunch core storage/transport.
|
||||
|
||||
## Holepunch boundary
|
||||
|
||||
Inspiration: n/a
|
||||
In-memory structures; gossip via `initModuleSwarm` / `gossipSend` when P2P enabled.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PMultisigThreshold } = require('../index.js')
|
||||
|
||||
async function main () {
|
||||
const m = new HyperP2PMultisigThreshold()
|
||||
console.log('[scaffold]', m.getStats())
|
||||
console.log(m.getStats())
|
||||
await m.close()
|
||||
}
|
||||
main().catch(console.error)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'multisig-threshold/v1'
|
||||
|
||||
@@ -10,50 +10,114 @@ class HyperP2PMultisigThreshold extends EventEmitter {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this._proposals = new Map()
|
||||
this._stats = { created: 0, signatures: 0, approved: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'multisig-threshold-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
createProposal (id, signers, threshold) {
|
||||
assertNonEmpty(id, 'id')
|
||||
if (!Array.isArray(signers) || signers.length === 0) {
|
||||
throw new Error('signers must be a non-empty array')
|
||||
}
|
||||
if (threshold < 1 || threshold > signers.length) {
|
||||
throw new Error('threshold must be between 1 and signers.length')
|
||||
}
|
||||
const proposal = {
|
||||
id,
|
||||
signers: [...signers],
|
||||
threshold,
|
||||
signatures: new Set(),
|
||||
createdAt: Date.now(),
|
||||
approved: false
|
||||
}
|
||||
this._proposals.set(id, proposal)
|
||||
this._stats.created++
|
||||
this._gossip({
|
||||
type: 'proposal-create',
|
||||
id,
|
||||
signers: proposal.signers,
|
||||
threshold,
|
||||
createdAt: proposal.createdAt
|
||||
})
|
||||
this.emit('proposal', { id, signers, threshold })
|
||||
return { id, signers: proposal.signers, threshold }
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'multisig-threshold-sync', key, value: null })
|
||||
return ok
|
||||
addSignature (id, signer) {
|
||||
assertNonEmpty(id, 'id')
|
||||
assertNonEmpty(signer, 'signer')
|
||||
const p = this._proposals.get(id)
|
||||
if (!p) throw new Error(`proposal not found: ${id}`)
|
||||
if (!p.signers.includes(signer)) throw new Error(`signer not authorized: ${signer}`)
|
||||
p.signatures.add(signer)
|
||||
this._stats.signatures++
|
||||
const approved = p.signatures.size >= p.threshold
|
||||
if (approved && !p.approved) {
|
||||
p.approved = true
|
||||
this._stats.approved++
|
||||
this.emit('approved', { id })
|
||||
}
|
||||
this._gossip({ type: 'proposal-sig', id, signer })
|
||||
return { id, signer, count: p.signatures.size, approved: p.approved }
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
isApproved (id) {
|
||||
assertNonEmpty(id, 'id')
|
||||
const p = this._proposals.get(id)
|
||||
return !!(p && p.approved)
|
||||
}
|
||||
|
||||
_gossip (data) {
|
||||
if (!this._peerMsgs) return
|
||||
gossipSend(this, data)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'multisig-threshold-sync') return
|
||||
if (!d || !d.type) return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
if (d.type === 'proposal-create' && d.id) {
|
||||
if (!this._proposals.has(d.id)) {
|
||||
this._proposals.set(d.id, {
|
||||
id: d.id,
|
||||
signers: d.signers || [],
|
||||
threshold: d.threshold,
|
||||
signatures: new Set(),
|
||||
createdAt: d.createdAt || Date.now(),
|
||||
approved: false
|
||||
})
|
||||
}
|
||||
}
|
||||
if (d.type === 'proposal-sig' && d.id && d.signer) {
|
||||
const p = this._proposals.get(d.id)
|
||||
if (p && p.signers.includes(d.signer)) {
|
||||
p.signatures.add(d.signer)
|
||||
if (p.signatures.size >= p.threshold) p.approved = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
getStats () {
|
||||
return { ...this._stats, proposals: this._proposals.size, protocol: PROTOCOL }
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (d) => this._onGossip(d)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,35 @@ const { HyperP2PMultisigThreshold, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PMultisigThreshold)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'multisig-threshold/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('isApproved at threshold', async (t) => {
|
||||
const m = new HyperP2PMultisigThreshold()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
m.createProposal('p1', ['a', 'b', 'c'], 2)
|
||||
m.addSignature('p1', 'a')
|
||||
t.not(m.isApproved('p1'))
|
||||
m.addSignature('p1', 'b')
|
||||
t.ok(m.isApproved('p1'))
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('unauthorized signer throws', async (t) => {
|
||||
const m = new HyperP2PMultisigThreshold()
|
||||
m.createProposal('p', ['a'], 1)
|
||||
try { m.addSignature('p', 'z') } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PMultisigThreshold()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
try { m.createProposal('x', [], 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PMultisigThreshold()
|
||||
t.ok(m.getStats().protocol)
|
||||
m.createProposal('q', ['a'], 1)
|
||||
t.is(m.getStats().created, 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
@@ -1,28 +1,34 @@
|
||||
# hyper-p2p-session-rotation
|
||||
|
||||
**Status:** scaffold (`0.0.0-scaffold`) · **Protocol:** `session-rotation/v1` · **Wave:** 8
|
||||
Production module: Session token rotation.
|
||||
|
||||
Session key rotation.
|
||||
**Protocol:** `session-rotation/v1`
|
||||
|
||||
## Holepunch references (inspiration only)
|
||||
## When to use
|
||||
|
||||
- `hyperswarm-secret-stream`
|
||||
Refreshing session tokens.
|
||||
|
||||
> This module composes on Hyperswarm/Hypercore — it does **not** re-implement upstream packages.
|
||||
## When not to use
|
||||
|
||||
## Composes with
|
||||
Immutable sessions.
|
||||
|
||||
- `hyper-p2p-session-bridge`
|
||||
## Quick start
|
||||
|
||||
## Planned API
|
||||
```js
|
||||
const { HyperP2PSessionRotation } = require('hyper-p2p-session-rotation')
|
||||
const m = new HyperP2PSessionRotation()
|
||||
m.rotateSession('s', 'tok')
|
||||
await m.ready()
|
||||
await m.close()
|
||||
```
|
||||
|
||||
- `constructor(opts)` — topic, optional keyPair
|
||||
- `getStats()` — scaffold counters
|
||||
- `ready()` — no-op until implemented
|
||||
- Domain methods — throw `not implemented: scaffold` until Wave 8+ pass
|
||||
## Docs
|
||||
|
||||
## Layout
|
||||
- [docs/api.md](docs/api.md)
|
||||
- [docs/architecture.md](docs/architecture.md)
|
||||
|
||||
`modules/trust-security/hyper-p2p-session-rotation/`
|
||||
## Test
|
||||
|
||||
See [`modules/_shared/MODULE_SYSTEM.md`](../../_shared/MODULE_SYSTEM.md).
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
# hyper-p2p-session-rotation API
|
||||
# API: hyper-p2p-session-rotation
|
||||
|
||||
**Status:** scaffold · **Protocol:** `session-rotation/v1`
|
||||
**Protocol:** `session-rotation/v1` · **Export:** `HyperP2PSessionRotation`
|
||||
|
||||
## Class `HyperP2PSessionRotation`
|
||||
## Methods
|
||||
|
||||
Scaffold stub — methods throw `not implemented: scaffold` until promoted to production tier.
|
||||
### `rotateSession(...)`
|
||||
|
||||
### `constructor(opts?)`
|
||||
Domain API.
|
||||
|
||||
### `getStats()`
|
||||
### `getSession(...)`
|
||||
|
||||
Returns `{ created, errors, protocol, tier: 'scaffold' }`.
|
||||
Domain API.
|
||||
|
||||
### `ready()`
|
||||
### `listSessions(...)`
|
||||
|
||||
Resolves immediately (no-op).
|
||||
Domain API.
|
||||
|
||||
## Wire (planned)
|
||||
|
||||
| Message | Direction | Notes |
|
||||
|---------|-----------|-------|
|
||||
| TBD | gossip | Defined in implementation pass |
|
||||
### `getStats()` / `ready()` / `close()`
|
||||
|
||||
Lifecycle helpers.
|
||||
|
||||
## P2P
|
||||
|
||||
Joins Hyperswarm when `topic` is set.
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
# hyper-p2p-session-rotation architecture
|
||||
# Architecture: hyper-p2p-session-rotation
|
||||
|
||||
**Tier:** scaffold · **Category:** `trust-security`
|
||||
## Wire messages
|
||||
|
||||
## Role
|
||||
| type | fields | direction | behavior |
|
||||
|------|--------|-----------|----------|
|
||||
| `session-rotate` | sessionId, token, version | gossip | Rotate |
|
||||
|
||||
Session key rotation.
|
||||
## State
|
||||
|
||||
## Composition
|
||||
|
||||
Uses `../../_shared/p2p-bare.js` for Hyperswarm + Protomux when implemented. Does **not** duplicate Holepunch core storage/transport.
|
||||
|
||||
## Holepunch boundary
|
||||
|
||||
Inspiration: n/a
|
||||
In-memory structures; gossip via `initModuleSwarm` / `gossipSend` when P2P enabled.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PSessionRotation } = require('../index.js')
|
||||
|
||||
async function main () {
|
||||
const m = new HyperP2PSessionRotation()
|
||||
console.log('[scaffold]', m.getStats())
|
||||
console.log(m.getStats())
|
||||
await m.close()
|
||||
}
|
||||
main().catch(console.error)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'session-rotation/v1'
|
||||
|
||||
@@ -10,50 +10,77 @@ class HyperP2PSessionRotation extends EventEmitter {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this._sessions = new Map()
|
||||
this._stats = { rotations: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'session-rotation-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
rotateSession (sessionId, newToken) {
|
||||
assertNonEmpty(sessionId, 'sessionId')
|
||||
assertNonEmpty(newToken, 'newToken')
|
||||
const prev = this._sessions.get(sessionId)
|
||||
const session = {
|
||||
sessionId,
|
||||
token: newToken,
|
||||
version: (prev ? prev.version : 0) + 1,
|
||||
rotatedAt: Date.now()
|
||||
}
|
||||
this._sessions.set(sessionId, session)
|
||||
this._stats.rotations++
|
||||
this._gossip({ type: 'session-rotate', sessionId, token: newToken, version: session.version, rotatedAt: session.rotatedAt })
|
||||
this.emit('rotated', session)
|
||||
return session
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'session-rotation-sync', key, value: null })
|
||||
return ok
|
||||
getSession (sessionId) {
|
||||
assertNonEmpty(sessionId, 'sessionId')
|
||||
return this._sessions.get(sessionId) || null
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
listSessions () {
|
||||
return [...this._sessions.values()]
|
||||
}
|
||||
|
||||
_gossip (data) {
|
||||
if (!this._peerMsgs) return
|
||||
gossipSend(this, data)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'session-rotation-sync') return
|
||||
if (!d || d.type !== 'session-rotate' || !d.sessionId) return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
const prev = this._sessions.get(d.sessionId)
|
||||
if (!prev || d.version >= prev.version) {
|
||||
this._sessions.set(d.sessionId, {
|
||||
sessionId: d.sessionId,
|
||||
token: d.token,
|
||||
version: d.version,
|
||||
rotatedAt: d.rotatedAt || Date.now()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
getStats () {
|
||||
return { ...this._stats, sessions: this._sessions.size, protocol: PROTOCOL }
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (d) => this._onGossip(d)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,35 @@ const { HyperP2PSessionRotation, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PSessionRotation)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'session-rotation/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('rotateSession bumps version', async (t) => {
|
||||
const m = new HyperP2PSessionRotation()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
m.rotateSession('s1', 'tok-a')
|
||||
const s2 = m.rotateSession('s1', 'tok-b')
|
||||
t.is(s2.version, 2)
|
||||
t.is(m.getSession('s1').token, 'tok-b')
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('listSessions', async (t) => {
|
||||
const m = new HyperP2PSessionRotation()
|
||||
m.rotateSession('a', '1')
|
||||
m.rotateSession('b', '2')
|
||||
t.is(m.listSessions().length, 2)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PSessionRotation()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
try { m.rotateSession('', 'x') } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PSessionRotation()
|
||||
t.ok(m.getStats().protocol)
|
||||
m.rotateSession('x', 'y')
|
||||
t.is(m.getStats().rotations, 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user