@cero-base/core 1.18.1 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -6
- package/package.json +9 -9
- package/src/blobs/index.js +2 -2
- package/src/database/changes.js +37 -59
- package/src/database/dispatch.js +31 -45
- package/src/database/index.js +395 -343
- package/src/database/rotation.js +201 -189
- package/src/identity/index.js +3 -3
- package/src/lib/utils.js +50 -18
- package/src/pairing/candidate.js +125 -0
- package/src/pairing/index.js +6 -223
- package/src/pairing/request.js +102 -0
- package/src/storage/index.js +11 -18
- package/types/blobs/codec.d.ts +8 -8
- package/types/blobs/index.d.ts +34 -33
- package/types/blobs/server.d.ts +8 -8
- package/types/database/changes.d.ts +5 -7
- package/types/database/dispatch.d.ts +30 -23
- package/types/database/encryption.d.ts +14 -10
- package/types/database/envelope.d.ts +2 -2
- package/types/database/index.d.ts +173 -181
- package/types/database/rotation.d.ts +53 -30
- package/types/identity/index.d.ts +81 -81
- package/types/index.d.ts +37 -51
- package/types/lib/constants.d.ts +34 -38
- package/types/lib/errors.d.ts +10 -10
- package/types/lib/schema.d.ts +47 -37
- package/types/lib/spec/index.d.ts +29 -55
- package/types/lib/utils.d.ts +27 -17
- package/types/network/discovery.d.ts +7 -7
- package/types/network/index.d.ts +50 -48
- package/types/pairing/candidate.d.ts +37 -0
- package/types/pairing/index.d.ts +87 -139
- package/types/pairing/invite.d.ts +88 -88
- package/types/pairing/request.d.ts +135 -0
- package/types/rpc/client.d.ts +2 -2
- package/types/rpc/index.d.ts +33 -33
- package/types/rpc/peer.d.ts +9 -7
- package/types/rpc/server.d.ts +2 -2
- package/types/storage/index.d.ts +70 -68
- package/src/database/bootstrap.js +0 -133
- package/src/database/hooks.js +0 -112
- package/src/database/membership.js +0 -67
- package/types/database/bootstrap.d.ts +0 -20
- package/types/database/hooks.d.ts +0 -8
- package/types/database/membership.d.ts +0 -7
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import safetyCatch from 'safety-catch'
|
|
2
|
+
import c from 'compact-encoding'
|
|
3
|
+
|
|
4
|
+
import { channelTopic } from '../network/index.js'
|
|
5
|
+
import { CeroError } from '../lib/errors.js'
|
|
6
|
+
import { Response, STATUS_DENIED } from './request.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Internal — the joiner side: a candidate's handshake state machine (this *is* the
|
|
10
|
+
* blind-pairing `addCandidate`). One-shot: created by `Pairing.join()`, settled
|
|
11
|
+
* exactly once via `_done` or `_fail`.
|
|
12
|
+
*
|
|
13
|
+
* @property {any} _candidate blind-pairing addCandidate handle (null until started).
|
|
14
|
+
* @property {ReturnType<typeof setTimeout> | null} _timer Pending timeout, if any.
|
|
15
|
+
* @property {((result: any) => void) | null} _resolve Promise resolver, set in `start`.
|
|
16
|
+
* @property {((err: Error) => void) | null} _reject Promise rejecter, set in `start`.
|
|
17
|
+
* @property {boolean} _settled Whether the handshake has already settled.
|
|
18
|
+
*/
|
|
19
|
+
export class Candidate {
|
|
20
|
+
/**
|
|
21
|
+
* @param {import('./index.js').Pairing} pairing
|
|
22
|
+
* @param {import('./invite.js').Invite} invite
|
|
23
|
+
* @param {Uint8Array | null} userData
|
|
24
|
+
* @param {number} timeout
|
|
25
|
+
*/
|
|
26
|
+
constructor(pairing, invite, userData, timeout) {
|
|
27
|
+
this.pairing = pairing
|
|
28
|
+
this.invite = invite
|
|
29
|
+
this.userData = userData
|
|
30
|
+
this.timeout = timeout
|
|
31
|
+
this._candidate = null
|
|
32
|
+
this._timer = null
|
|
33
|
+
this._resolve = null
|
|
34
|
+
this._reject = null
|
|
35
|
+
this._settled = false
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Kick off the handshake; returns a promise that settles with the host response.
|
|
40
|
+
*
|
|
41
|
+
* @returns {Promise<import('./index.js').JoinResult>}
|
|
42
|
+
*/
|
|
43
|
+
start() {
|
|
44
|
+
this.pairing._candidates.add(this)
|
|
45
|
+
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
this._resolve = resolve
|
|
48
|
+
this._reject = reject
|
|
49
|
+
|
|
50
|
+
if (this.timeout > 0) {
|
|
51
|
+
this._timer = setTimeout(() => this._fail(CeroError.TIMEOUT()), this.timeout)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
this._candidate = this.pairing._blind.addCandidate({
|
|
56
|
+
invite: this.invite.blind,
|
|
57
|
+
userData: this.userData,
|
|
58
|
+
// must hash exactly like the member side — see Pairing._open
|
|
59
|
+
discoveryKey: channelTopic(this.invite.discoveryKey, this.pairing.network.channel),
|
|
60
|
+
onadd: (result) => this._done(result)
|
|
61
|
+
})
|
|
62
|
+
this._candidate.request.on('rejected', (err) => this._fail(fromBlindError(err)))
|
|
63
|
+
this.pairing.network.refreshInjected().catch(this._onerror ?? (() => {}))
|
|
64
|
+
} catch (err) {
|
|
65
|
+
this._fail(err instanceof CeroError ? err : CeroError.NETWORK_ERROR(err.message))
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_done(result) {
|
|
71
|
+
if (this._settled) return
|
|
72
|
+
|
|
73
|
+
let envelope = null
|
|
74
|
+
try {
|
|
75
|
+
if (result?.data) envelope = c.decode(Response, result.data)
|
|
76
|
+
} catch {}
|
|
77
|
+
|
|
78
|
+
if (envelope && envelope.status === STATUS_DENIED) {
|
|
79
|
+
this._fail(CeroError.DENIED(envelope.reason || null))
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!envelope) {
|
|
84
|
+
this._fail(CeroError.NETWORK_ERROR('malformed pairing response'))
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this._settled = true
|
|
89
|
+
this.pairing._candidates.delete(this)
|
|
90
|
+
clearTimeout(this._timer)
|
|
91
|
+
|
|
92
|
+
this._resolve({
|
|
93
|
+
key: envelope.key,
|
|
94
|
+
encryptionKey: envelope.encryptionKey,
|
|
95
|
+
additional: envelope.extra
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
this._candidate.close().catch(safetyCatch)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_fail(err) {
|
|
102
|
+
if (this._settled) return
|
|
103
|
+
this._settled = true
|
|
104
|
+
this.pairing._candidates.delete(this)
|
|
105
|
+
clearTimeout(this._timer)
|
|
106
|
+
if (this._candidate) this._candidate.close().catch(safetyCatch)
|
|
107
|
+
this._reject(err)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function fromBlindError(err) {
|
|
112
|
+
// code-less rejections are protocol failures (undecodable reply, key
|
|
113
|
+
// mismatch, bad signature) — never a host decision, don't report DENIED
|
|
114
|
+
if (!err || !err.code) return CeroError.NETWORK_ERROR(err?.message || 'pairing failed')
|
|
115
|
+
switch (err.code) {
|
|
116
|
+
case 'INVITE_EXPIRED':
|
|
117
|
+
return CeroError.EXPIRED(err.message)
|
|
118
|
+
case 'INVITE_USED':
|
|
119
|
+
return CeroError.DENIED('used', err.message)
|
|
120
|
+
case 'PAIRING_REJECTED':
|
|
121
|
+
return CeroError.DENIED(null, err.message)
|
|
122
|
+
default:
|
|
123
|
+
return CeroError.DENIED(err.message)
|
|
124
|
+
}
|
|
125
|
+
}
|
package/src/pairing/index.js
CHANGED
|
@@ -3,22 +3,14 @@ import ReadyResource from 'ready-resource'
|
|
|
3
3
|
import safetyCatch from 'safety-catch'
|
|
4
4
|
import b4a from 'b4a'
|
|
5
5
|
import c from 'compact-encoding'
|
|
6
|
-
import { discoveryKey
|
|
6
|
+
import { discoveryKey } from 'hypercore-crypto'
|
|
7
7
|
|
|
8
8
|
import { Invite } from './invite.js'
|
|
9
|
+
import { Candidate } from './candidate.js'
|
|
10
|
+
import { Request } from './request.js'
|
|
9
11
|
import { channelTopic } from '../network/index.js'
|
|
10
|
-
import { getEncoding } from '../lib/spec/index.js'
|
|
11
12
|
import { CeroError } from '../lib/errors.js'
|
|
12
13
|
|
|
13
|
-
const STATUS_ACCEPTED = 0
|
|
14
|
-
const STATUS_DENIED = 1
|
|
15
|
-
|
|
16
|
-
// Wire envelope carried in BlindPairing's `additional.data` on the confirm response.
|
|
17
|
-
// We piggyback on the confirm path for *both* success and deny — blind-pairing's
|
|
18
|
-
// native deny() has no payload field, and its discoveryKey check pins the wire
|
|
19
|
-
// `key` to the invite, so the real resource key flows through this envelope.
|
|
20
|
-
const Response = getEncoding('@cero/confirm')
|
|
21
|
-
|
|
22
14
|
/**
|
|
23
15
|
* @typedef {object} PairingOpts
|
|
24
16
|
* @property {import('../network/index.js').Network} network Network to host the BlindPairing member.
|
|
@@ -36,23 +28,10 @@ const Response = getEncoding('@cero/confirm')
|
|
|
36
28
|
* @property {boolean} [reuse] Reusable until expiry/revoke; default `false` (consumed on first settle).
|
|
37
29
|
*
|
|
38
30
|
* @typedef {object} JoinOpts
|
|
39
|
-
* @property {any}
|
|
31
|
+
* @property {any} userData Payload sent with the candidate request. Required: blind-pairing derives the handshake token from it, so a candidate without one is never served.
|
|
40
32
|
* @property {number} [timeout] Deadline for the handshake, in ms. Defaults to 30000.
|
|
41
33
|
*
|
|
42
|
-
* @typedef {object} ConfirmOpts
|
|
43
|
-
* @property {Uint8Array} [key] 32-byte resource key delivered to the joiner; required at runtime.
|
|
44
|
-
* @property {Uint8Array | null} [encryptionKey] Optional 32-byte symmetric key.
|
|
45
|
-
* @property {Uint8Array | null} [additional] Extra opaque bytes piggybacked on the response.
|
|
46
|
-
*
|
|
47
34
|
* @typedef {{ key: Uint8Array, encryptionKey: Uint8Array | null, additional: Uint8Array | null }} JoinResult
|
|
48
|
-
*
|
|
49
|
-
* @typedef {object} RequestOpts
|
|
50
|
-
* @property {Pairing} pairing Owning Pairing instance.
|
|
51
|
-
* @property {any} req blind-pairing candidate request being answered.
|
|
52
|
-
* @property {Invite} invite Invite the candidate paired against.
|
|
53
|
-
* @property {Uint8Array} seed Per-invite seed used to sign the response.
|
|
54
|
-
* @property {any} userData Decoded joiner payload (raw bytes when no encoding).
|
|
55
|
-
* @property {() => void} onsettle Called once when the candidate is confirmed or denied.
|
|
56
35
|
*/
|
|
57
36
|
|
|
58
37
|
/**
|
|
@@ -271,7 +250,7 @@ export class Pairing extends ReadyResource {
|
|
|
271
250
|
* @param {JoinOpts} [opts]
|
|
272
251
|
* @returns {Promise<JoinResult>}
|
|
273
252
|
*/
|
|
274
|
-
async join(inviteStr, { userData
|
|
253
|
+
async join(inviteStr, { userData, timeout = 30000 } = {}) {
|
|
275
254
|
if (this.closing || this.closed) throw CeroError.CLOSED('Pairing')
|
|
276
255
|
if (!this.opened) await this.ready()
|
|
277
256
|
|
|
@@ -366,188 +345,8 @@ export class Pairing extends ReadyResource {
|
|
|
366
345
|
}
|
|
367
346
|
}
|
|
368
347
|
|
|
369
|
-
/**
|
|
370
|
-
* Internal — a pairing request from an incoming candidate, awaiting the host's
|
|
371
|
-
* accept/deny. Built in `_oncandidate` and emitted on the `'candidate'` event so
|
|
372
|
-
* the host can decide via `confirm` / `deny`. (The candidate is the joiner; this
|
|
373
|
-
* is the host's handle to answer it.)
|
|
374
|
-
*
|
|
375
|
-
* @property {boolean} _settled Whether confirm/deny has already run.
|
|
376
|
-
*/
|
|
377
|
-
class Request {
|
|
378
|
-
/** @param {RequestOpts} opts */
|
|
379
|
-
constructor({ pairing, req, invite, seed, userData, onsettle }) {
|
|
380
|
-
this.pairing = pairing
|
|
381
|
-
this._req = req
|
|
382
|
-
this._seed = seed
|
|
383
|
-
this._settled = false
|
|
384
|
-
this._onsettle = onsettle
|
|
385
|
-
|
|
386
|
-
this.invite = invite
|
|
387
|
-
this.userData = userData
|
|
388
|
-
this.publicKey = req.publicKey
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* Accept the candidate and reveal the resource key. Idempotent.
|
|
393
|
-
*
|
|
394
|
-
* @param {ConfirmOpts} opts
|
|
395
|
-
* @returns {Promise<void>}
|
|
396
|
-
*/
|
|
397
|
-
async confirm({ key, encryptionKey = null, additional = null } = {}) {
|
|
398
|
-
if (this._settled) return
|
|
399
|
-
if (!key || key.length !== 32) throw CeroError.INVALID('key must be a 32-byte buffer')
|
|
400
|
-
if (encryptionKey && encryptionKey.length !== 32) {
|
|
401
|
-
throw CeroError.INVALID('encryptionKey must be a 32-byte buffer')
|
|
402
|
-
}
|
|
403
|
-
if (additional && !b4a.isBuffer(additional)) {
|
|
404
|
-
throw CeroError.INVALID('additional must be a buffer')
|
|
405
|
-
}
|
|
406
|
-
this._respond({ status: STATUS_ACCEPTED, reason: '', key, encryptionKey, extra: additional })
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Reject the candidate with an optional reason. Idempotent.
|
|
411
|
-
*
|
|
412
|
-
* @param {string} [reason]
|
|
413
|
-
* @returns {Promise<void>}
|
|
414
|
-
*/
|
|
415
|
-
async deny(reason = '') {
|
|
416
|
-
if (this._settled) return
|
|
417
|
-
this._respond({
|
|
418
|
-
status: STATUS_DENIED,
|
|
419
|
-
reason,
|
|
420
|
-
key: null,
|
|
421
|
-
encryptionKey: null,
|
|
422
|
-
extra: null
|
|
423
|
-
})
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
// sign the envelope with the per-invite seed and answer the candidate
|
|
427
|
-
// (callers guard idempotency before this runs)
|
|
428
|
-
_respond(envelope) {
|
|
429
|
-
this._settled = true
|
|
430
|
-
this._onsettle()
|
|
431
|
-
const data = c.encode(Response, envelope)
|
|
432
|
-
const signature = sign(data, keyPair(this._seed).secretKey)
|
|
433
|
-
this._req.confirm({
|
|
434
|
-
key: this.pairing.topic,
|
|
435
|
-
encryptionKey: undefined,
|
|
436
|
-
additional: { data, signature }
|
|
437
|
-
})
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* Internal — the joiner side: a candidate's handshake state machine (this *is* the
|
|
443
|
-
* blind-pairing `addCandidate`). One-shot: created by `Pairing.join()`, settled
|
|
444
|
-
* exactly once via `_done` or `_fail`.
|
|
445
|
-
*
|
|
446
|
-
* @property {any} _candidate blind-pairing addCandidate handle (null until started).
|
|
447
|
-
* @property {ReturnType<typeof setTimeout> | null} _timer Pending timeout, if any.
|
|
448
|
-
* @property {((result: JoinResult) => void) | null} _resolve Promise resolver, set in `start`.
|
|
449
|
-
* @property {((err: Error) => void) | null} _reject Promise rejecter, set in `start`.
|
|
450
|
-
* @property {boolean} _settled Whether the handshake has already settled.
|
|
451
|
-
*/
|
|
452
|
-
class Candidate {
|
|
453
|
-
/**
|
|
454
|
-
* @param {Pairing} pairing
|
|
455
|
-
* @param {Invite} invite
|
|
456
|
-
* @param {Uint8Array | null} encodedUserData
|
|
457
|
-
* @param {number} timeout
|
|
458
|
-
*/
|
|
459
|
-
constructor(pairing, invite, encodedUserData, timeout) {
|
|
460
|
-
this.pairing = pairing
|
|
461
|
-
this.invite = invite
|
|
462
|
-
this.userData = encodedUserData
|
|
463
|
-
this.timeout = timeout
|
|
464
|
-
this._candidate = null
|
|
465
|
-
this._timer = null
|
|
466
|
-
this._resolve = null
|
|
467
|
-
this._reject = null
|
|
468
|
-
this._settled = false
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Kick off the handshake; returns a promise that settles with the host response.
|
|
473
|
-
*
|
|
474
|
-
* @returns {Promise<JoinResult>}
|
|
475
|
-
*/
|
|
476
|
-
start() {
|
|
477
|
-
this.pairing._candidates.add(this)
|
|
478
|
-
|
|
479
|
-
return new Promise((resolve, reject) => {
|
|
480
|
-
this._resolve = resolve
|
|
481
|
-
this._reject = reject
|
|
482
|
-
|
|
483
|
-
if (this.timeout > 0) {
|
|
484
|
-
this._timer = setTimeout(() => this._fail(CeroError.TIMEOUT()), this.timeout)
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
try {
|
|
488
|
-
this._candidate = this.pairing._blind.addCandidate({
|
|
489
|
-
invite: this.invite.blind,
|
|
490
|
-
userData: this.userData,
|
|
491
|
-
// must hash exactly like the member side — see Pairing._open
|
|
492
|
-
discoveryKey: channelTopic(this.invite.discoveryKey, this.pairing.network.channel),
|
|
493
|
-
onadd: (result) => this._done(result)
|
|
494
|
-
})
|
|
495
|
-
this._candidate.request.on('rejected', (err) => this._fail(fromBlindError(err)))
|
|
496
|
-
this.pairing.network.refreshInjected().catch(this._onerror ?? (() => {}))
|
|
497
|
-
} catch (err) {
|
|
498
|
-
this._fail(err instanceof CeroError ? err : CeroError.NETWORK_ERROR(err.message))
|
|
499
|
-
}
|
|
500
|
-
})
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
_done(result) {
|
|
504
|
-
if (this._settled) return
|
|
505
|
-
|
|
506
|
-
let envelope = null
|
|
507
|
-
const buf = result?.data ?? null
|
|
508
|
-
if (buf) {
|
|
509
|
-
try {
|
|
510
|
-
envelope = c.decode(Response, buf)
|
|
511
|
-
} catch {
|
|
512
|
-
envelope = null
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
if (envelope && envelope.status === STATUS_DENIED) {
|
|
517
|
-
this._fail(CeroError.DENIED(envelope.reason || null))
|
|
518
|
-
return
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
if (!envelope) {
|
|
522
|
-
this._fail(CeroError.NETWORK_ERROR('malformed pairing response'))
|
|
523
|
-
return
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
this._settled = true
|
|
527
|
-
this.pairing._candidates.delete(this)
|
|
528
|
-
clearTimeout(this._timer)
|
|
529
|
-
|
|
530
|
-
this._resolve({
|
|
531
|
-
key: envelope.key,
|
|
532
|
-
encryptionKey: envelope.encryptionKey,
|
|
533
|
-
additional: envelope.extra
|
|
534
|
-
})
|
|
535
|
-
|
|
536
|
-
this._candidate.close().catch(safetyCatch)
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
_fail(err) {
|
|
540
|
-
if (this._settled) return
|
|
541
|
-
this._settled = true
|
|
542
|
-
this.pairing._candidates.delete(this)
|
|
543
|
-
clearTimeout(this._timer)
|
|
544
|
-
if (this._candidate) this._candidate.close().catch(safetyCatch)
|
|
545
|
-
this._reject(err)
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
|
|
549
348
|
function encodeUserData(data, encoding) {
|
|
550
|
-
if (data == null)
|
|
349
|
+
if (data == null) throw CeroError.REQUIRED('userData')
|
|
551
350
|
if (encoding) return c.encode(encoding, data)
|
|
552
351
|
if (b4a.isBuffer(data)) return data
|
|
553
352
|
throw CeroError.INVALID('userData must be a buffer when no joinerEncoding is provided')
|
|
@@ -562,19 +361,3 @@ function decodeUserData(buf, encoding) {
|
|
|
562
361
|
return buf
|
|
563
362
|
}
|
|
564
363
|
}
|
|
565
|
-
|
|
566
|
-
function fromBlindError(err) {
|
|
567
|
-
// code-less rejections are protocol failures (undecodable reply, key
|
|
568
|
-
// mismatch, bad signature) — never a host decision, don't report DENIED
|
|
569
|
-
if (!err || !err.code) return CeroError.NETWORK_ERROR(err?.message || 'pairing failed')
|
|
570
|
-
switch (err.code) {
|
|
571
|
-
case 'INVITE_EXPIRED':
|
|
572
|
-
return CeroError.EXPIRED(err.message)
|
|
573
|
-
case 'INVITE_USED':
|
|
574
|
-
return CeroError.DENIED('used', err.message)
|
|
575
|
-
case 'PAIRING_REJECTED':
|
|
576
|
-
return CeroError.DENIED(null, err.message)
|
|
577
|
-
default:
|
|
578
|
-
return CeroError.DENIED(err.message)
|
|
579
|
-
}
|
|
580
|
-
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import b4a from 'b4a'
|
|
2
|
+
import c from 'compact-encoding'
|
|
3
|
+
import { keyPair, sign } from 'hypercore-crypto'
|
|
4
|
+
|
|
5
|
+
import { getEncoding } from '../lib/spec/index.js'
|
|
6
|
+
import { CeroError } from '../lib/errors.js'
|
|
7
|
+
|
|
8
|
+
export const STATUS_ACCEPTED = 0
|
|
9
|
+
export const STATUS_DENIED = 1
|
|
10
|
+
|
|
11
|
+
// Wire envelope carried in BlindPairing's `additional.data` on the confirm response.
|
|
12
|
+
// We piggyback on the confirm path for *both* success and deny — blind-pairing's
|
|
13
|
+
// native deny() has no payload field, and its discoveryKey check pins the wire
|
|
14
|
+
// `key` to the invite, so the real resource key flows through this envelope.
|
|
15
|
+
export const Response = getEncoding('@cero/confirm')
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {object} ConfirmOpts
|
|
19
|
+
* @property {Uint8Array} [key] 32-byte resource key delivered to the joiner; required at runtime.
|
|
20
|
+
* @property {Uint8Array | null} [encryptionKey] Optional 32-byte symmetric key.
|
|
21
|
+
* @property {Uint8Array | null} [additional] Extra opaque bytes piggybacked on the response.
|
|
22
|
+
*
|
|
23
|
+
* @typedef {object} RequestOpts
|
|
24
|
+
* @property {import('./index.js').Pairing} pairing Owning Pairing instance.
|
|
25
|
+
* @property {any} req blind-pairing candidate request being answered.
|
|
26
|
+
* @property {import('./invite.js').Invite} invite Invite the candidate paired against.
|
|
27
|
+
* @property {Uint8Array} seed Per-invite seed used to sign the response.
|
|
28
|
+
* @property {any} userData Decoded joiner payload (raw bytes when no encoding).
|
|
29
|
+
* @property {() => void} onsettle Called once when the candidate is confirmed or denied.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Internal — a pairing request from an incoming candidate, awaiting the host's
|
|
34
|
+
* accept/deny. Built in `_oncandidate` and emitted on the `'candidate'` event so
|
|
35
|
+
* the host can decide via `confirm` / `deny`. (The candidate is the joiner; this
|
|
36
|
+
* is the host's handle to answer it.)
|
|
37
|
+
*
|
|
38
|
+
* @property {boolean} _settled Whether confirm/deny has already run.
|
|
39
|
+
*/
|
|
40
|
+
export class Request {
|
|
41
|
+
/** @param {RequestOpts} opts */
|
|
42
|
+
constructor(opts) {
|
|
43
|
+
this.pairing = opts.pairing
|
|
44
|
+
this._req = opts.req
|
|
45
|
+
this._seed = opts.seed
|
|
46
|
+
this._settled = false
|
|
47
|
+
this._onsettle = opts.onsettle
|
|
48
|
+
|
|
49
|
+
this.invite = opts.invite
|
|
50
|
+
this.userData = opts.userData
|
|
51
|
+
this.publicKey = opts.req.publicKey
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Accept the candidate and reveal the resource key. Idempotent.
|
|
56
|
+
*
|
|
57
|
+
* @param {ConfirmOpts} opts
|
|
58
|
+
* @returns {Promise<void>}
|
|
59
|
+
*/
|
|
60
|
+
async confirm({ key, encryptionKey = null, additional = null } = {}) {
|
|
61
|
+
if (this._settled) return
|
|
62
|
+
if (!key || key.length !== 32) throw CeroError.INVALID('key must be a 32-byte buffer')
|
|
63
|
+
if (encryptionKey && encryptionKey.length !== 32) {
|
|
64
|
+
throw CeroError.INVALID('encryptionKey must be a 32-byte buffer')
|
|
65
|
+
}
|
|
66
|
+
if (additional && !b4a.isBuffer(additional)) {
|
|
67
|
+
throw CeroError.INVALID('additional must be a buffer')
|
|
68
|
+
}
|
|
69
|
+
this._respond({ status: STATUS_ACCEPTED, reason: '', key, encryptionKey, extra: additional })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Reject the candidate with an optional reason. Idempotent.
|
|
74
|
+
*
|
|
75
|
+
* @param {string} [reason]
|
|
76
|
+
* @returns {Promise<void>}
|
|
77
|
+
*/
|
|
78
|
+
async deny(reason = '') {
|
|
79
|
+
if (this._settled) return
|
|
80
|
+
this._respond({
|
|
81
|
+
status: STATUS_DENIED,
|
|
82
|
+
reason,
|
|
83
|
+
key: null,
|
|
84
|
+
encryptionKey: null,
|
|
85
|
+
extra: null
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// sign the envelope with the per-invite seed and answer the candidate
|
|
90
|
+
// (callers guard idempotency before this runs)
|
|
91
|
+
_respond(envelope) {
|
|
92
|
+
this._settled = true
|
|
93
|
+
this._onsettle()
|
|
94
|
+
const data = c.encode(Response, envelope)
|
|
95
|
+
const signature = sign(data, keyPair(this._seed).secretKey)
|
|
96
|
+
this._req.confirm({
|
|
97
|
+
key: this.pairing.topic,
|
|
98
|
+
encryptionKey: undefined,
|
|
99
|
+
additional: { data, signature }
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
}
|
package/src/storage/index.js
CHANGED
|
@@ -6,19 +6,9 @@ import HyperDB from 'hyperdb'
|
|
|
6
6
|
import ReadyResource from 'ready-resource'
|
|
7
7
|
|
|
8
8
|
import { ROCKS, BEE, SINGLE, COLLECTION, QUERY_RESERVED } from '../lib/constants.js'
|
|
9
|
-
import { genId, subscribe,
|
|
9
|
+
import { genId, subscribe, filter } from '../lib/utils.js'
|
|
10
10
|
import { CeroError } from '../lib/errors.js'
|
|
11
11
|
|
|
12
|
-
// The subset of the query grammar hyperdb itself honors (pushed down);
|
|
13
|
-
// the rest of QUERY_RESERVED is applied in memory here.
|
|
14
|
-
const RANGE_OPS = ['gt', 'gte', 'lt', 'lte', 'reverse', 'limit']
|
|
15
|
-
|
|
16
|
-
const pick = (query, keys) => {
|
|
17
|
-
const out = {}
|
|
18
|
-
if (query) for (const k of keys) if (query[k] !== undefined) out[k] = query[k]
|
|
19
|
-
return out
|
|
20
|
-
}
|
|
21
|
-
|
|
22
12
|
/**
|
|
23
13
|
* @typedef {object} StorageOpts
|
|
24
14
|
* @property {{ database: any, meta?: { ns?: string, refs?: Record<string, { kind?: string }> } }} spec
|
|
@@ -222,13 +212,12 @@ export class Storage extends ReadyResource {
|
|
|
222
212
|
const eq = Object.keys(query || {}).filter((k) => !QUERY_RESERVED.has(k))
|
|
223
213
|
if (eq.length || query?.search) {
|
|
224
214
|
const rows = await this.db.find(col, {}).toArray()
|
|
225
|
-
|
|
226
|
-
if (query.search) matched = matched.filter((r) => searchHit(r, query.search, query.fields))
|
|
215
|
+
const matched = filter(rows, { ...query, limit: undefined })
|
|
227
216
|
const data = query.limit != null ? matched.slice(0, query.limit) : matched
|
|
228
217
|
return { data, total: matched.length, size: data.length }
|
|
229
218
|
}
|
|
230
219
|
|
|
231
|
-
const data = await this.db.find(col,
|
|
220
|
+
const data = await this.db.find(col, range(query)).toArray()
|
|
232
221
|
// a limit caps `data`, so `total` needs a full count; with no query
|
|
233
222
|
// `data` is already everything — skip the second scan.
|
|
234
223
|
const filtered = query && Object.keys(query).length > 0
|
|
@@ -249,10 +238,8 @@ export class Storage extends ReadyResource {
|
|
|
249
238
|
const col = this._col(ref)
|
|
250
239
|
const eq = Object.keys(query || {}).filter((k) => !QUERY_RESERVED.has(k))
|
|
251
240
|
const filtered = eq.length || query?.search
|
|
252
|
-
const rows = await this.db.find(col, filtered ? {} :
|
|
253
|
-
|
|
254
|
-
if (query?.search) matched = matched.filter((r) => searchHit(r, query.search, query.fields))
|
|
255
|
-
return { data: matched.length }
|
|
241
|
+
const rows = await this.db.find(col, filtered ? {} : range(query)).toArray()
|
|
242
|
+
return { data: (filtered ? filter(rows, query) : rows).length }
|
|
256
243
|
}
|
|
257
244
|
|
|
258
245
|
/**
|
|
@@ -308,3 +295,9 @@ export class Storage extends ReadyResource {
|
|
|
308
295
|
await this.db.flush()
|
|
309
296
|
}
|
|
310
297
|
}
|
|
298
|
+
|
|
299
|
+
// hyperdb bounds are records, a bare id must be wrapped
|
|
300
|
+
function range({ gt, gte, lt, lte, reverse, limit } = {}) {
|
|
301
|
+
const id = (v) => (v === undefined ? undefined : { id: v })
|
|
302
|
+
return { gt: id(gt), gte: id(gte), lt: id(lt), lte: id(lte), reverse, limit }
|
|
303
|
+
}
|
package/types/blobs/codec.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
export type RawBlobId = {
|
|
2
|
+
blockOffset: number;
|
|
3
|
+
blockLength: number;
|
|
4
|
+
byteOffset: number;
|
|
5
|
+
byteLength: number;
|
|
6
|
+
};
|
|
1
7
|
/**
|
|
2
8
|
* Encode a coreKey + raw hyperblobs blobId + type into a durable string id.
|
|
3
9
|
*
|
|
@@ -6,21 +12,15 @@
|
|
|
6
12
|
* @param {string} type
|
|
7
13
|
* @returns {string}
|
|
8
14
|
*/
|
|
9
|
-
export function encodeId(coreKey: Uint8Array, blobId: RawBlobId, type: string): string;
|
|
15
|
+
export declare function encodeId(coreKey: Uint8Array, blobId: RawBlobId, type: string): string;
|
|
10
16
|
/**
|
|
11
17
|
* Decode a string id back into its coreKey, raw blobId and type.
|
|
12
18
|
*
|
|
13
19
|
* @param {string} id
|
|
14
20
|
* @returns {{ coreKey: Uint8Array, blobId: RawBlobId, type: string }}
|
|
15
21
|
*/
|
|
16
|
-
export function decodeId(id: string): {
|
|
22
|
+
export declare function decodeId(id: string): {
|
|
17
23
|
coreKey: Uint8Array;
|
|
18
24
|
blobId: RawBlobId;
|
|
19
25
|
type: string;
|
|
20
26
|
};
|
|
21
|
-
export type RawBlobId = {
|
|
22
|
-
blockOffset: number;
|
|
23
|
-
blockLength: number;
|
|
24
|
-
byteOffset: number;
|
|
25
|
-
byteLength: number;
|
|
26
|
-
};
|