@cero-base/core 3.0.0 → 3.1.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/package.json +1 -1
- package/src/database/dispatch.js +33 -1
- package/src/lib/constants.js +4 -0
- package/src/lib/spec/index.js +45 -2
- package/src/lib/spec/schema.json +40 -1
- package/src/pairing/index.js +87 -20
- package/src/pairing/pass.js +103 -0
- package/types/lib/constants.d.ts +2 -0
- package/types/pairing/index.d.ts +21 -0
- package/types/pairing/pass.d.ts +74 -0
package/package.json
CHANGED
package/src/database/dispatch.js
CHANGED
|
@@ -17,6 +17,8 @@ import {
|
|
|
17
17
|
REFUSALS,
|
|
18
18
|
SPENT,
|
|
19
19
|
ASKS,
|
|
20
|
+
PASSES,
|
|
21
|
+
MAX_PASS,
|
|
20
22
|
OWNER,
|
|
21
23
|
INVITE,
|
|
22
24
|
REMOVE,
|
|
@@ -75,7 +77,8 @@ const REQUESTS = {
|
|
|
75
77
|
deny: 'deny',
|
|
76
78
|
'rotate-key': 'epoch',
|
|
77
79
|
revoke: 'revoke',
|
|
78
|
-
'ask-key': 'owed'
|
|
80
|
+
'ask-key': 'owed',
|
|
81
|
+
'add-pass': 'pass'
|
|
79
82
|
}
|
|
80
83
|
|
|
81
84
|
// the device keys a request stands on: its own, and those of the chain its join proved or a link
|
|
@@ -186,6 +189,7 @@ export function makeDispatcher({
|
|
|
186
189
|
const refusals = `@${ns}/${REFUSALS}`
|
|
187
190
|
const spent = `@${ns}/${SPENT}`
|
|
188
191
|
const asks = `@${ns}/${ASKS}`
|
|
192
|
+
const passes = `@${ns}/${PASSES}`
|
|
189
193
|
const getMember = (view, id) => view.get(`@${ns}/members`, { id })
|
|
190
194
|
const getDevice = (view, id) => view.get(`@${ns}/devices`, { id })
|
|
191
195
|
|
|
@@ -989,6 +993,27 @@ export function makeDispatcher({
|
|
|
989
993
|
await ctx.view.delete(asks, op)
|
|
990
994
|
})
|
|
991
995
|
|
|
996
|
+
// a pass waits in a room both are in for the member it is sealed to: left by a member who
|
|
997
|
+
// writes, under her name, never over another, and taken out by either of them or by one who may
|
|
998
|
+
// remove
|
|
999
|
+
add('add-pass', async (op, ctx) => {
|
|
1000
|
+
await requireWrite(ctx)
|
|
1001
|
+
if (!(op.pass.byteLength <= MAX_PASS)) throw CeroError.REFUSED('pass')
|
|
1002
|
+
if (await ctx.view.get(passes, { id: op.id })) throw CeroError.REFUSED('pass')
|
|
1003
|
+
ctx.touched?.push(PASSES)
|
|
1004
|
+
await ctx.view.insert(passes, { ...op, by: await getSignerMember(ctx.view, ctx.key) })
|
|
1005
|
+
})
|
|
1006
|
+
add('del-pass', async (op, ctx) => {
|
|
1007
|
+
const row = await ctx.view.get(passes, { id: op.id })
|
|
1008
|
+
if (!row) return
|
|
1009
|
+
const { memberId, role } = await author(ctx)
|
|
1010
|
+
if (!memberId || (memberId !== row.to && memberId !== row.by && !can(role, REMOVE))) {
|
|
1011
|
+
throw CeroError.REFUSED('pass')
|
|
1012
|
+
}
|
|
1013
|
+
ctx.touched?.push(PASSES)
|
|
1014
|
+
await ctx.view.delete(passes, { id: op.id })
|
|
1015
|
+
})
|
|
1016
|
+
|
|
992
1017
|
// any WRITE member mints an invite, its rank capped at admission; an existing row is never
|
|
993
1018
|
// overwritten, and altering or revoking one needs REMOVE
|
|
994
1019
|
const invites = `@${ns}/invites`
|
|
@@ -1029,6 +1054,8 @@ export function makeDispatcher({
|
|
|
1029
1054
|
await drop(ctx, 'invites', invite.id)
|
|
1030
1055
|
const { secret, confirm } = invite
|
|
1031
1056
|
await ctx.view.insert(spent, { id: invite.id, reason, expires, secret, confirm })
|
|
1057
|
+
// one made to a member went to her with the keys: unused, she holds them and no seat
|
|
1058
|
+
if (invite.to && reason !== 'spent') await owe(ctx.view)
|
|
1032
1059
|
const kept = async (request) => {
|
|
1033
1060
|
if (!request.admitted) return false
|
|
1034
1061
|
if (!request.promised || reason === 'spent') return true
|
|
@@ -1201,6 +1228,11 @@ export function makeDispatcher({
|
|
|
1201
1228
|
const last = await recorded(ctx.view)
|
|
1202
1229
|
return refuse(ctx, { ...turned, identity: last ? join.identity : null }, 'removed', last)
|
|
1203
1230
|
}
|
|
1231
|
+
// one made to a member lets in her devices alone
|
|
1232
|
+
if (invite.to && invite.to !== memberId) {
|
|
1233
|
+
const turned = { id, reply: join.reply, expires: invite.expires || 0, invite: invite.id }
|
|
1234
|
+
return refuse(ctx, { ...turned, identity: join.identity }, 'denied', true)
|
|
1235
|
+
}
|
|
1204
1236
|
await ctx.view.delete(refusals, { id })
|
|
1205
1237
|
const gone = known ? null : await ctx.view.get(removals, { id: leftId(memberId) })
|
|
1206
1238
|
const older = gone && (invite.index ?? 0) <= gone.index
|
package/src/lib/constants.js
CHANGED
|
@@ -63,3 +63,7 @@ export const REFUSALS = 'refusals'
|
|
|
63
63
|
export const SPENT = 'spent'
|
|
64
64
|
// devices that asked for the keys they lack, until each closes its own
|
|
65
65
|
export const ASKS = 'owed'
|
|
66
|
+
// invites into other rooms, sealed to a member's devices, until one of them takes its own
|
|
67
|
+
export const PASSES = 'seats'
|
|
68
|
+
// what a room keeps of one
|
|
69
|
+
export const MAX_PASS = 64 * 1024
|
package/src/lib/spec/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// This file is autogenerated by the hyperschema compiler
|
|
2
|
-
// Schema Version:
|
|
2
|
+
// Schema Version: 4
|
|
3
3
|
/* eslint-disable camelcase */
|
|
4
4
|
/* eslint-disable quotes */
|
|
5
5
|
/* eslint-disable space-before-function-paren */
|
|
6
6
|
|
|
7
7
|
import { c } from 'hyperschema/runtime'
|
|
8
8
|
|
|
9
|
-
const VERSION =
|
|
9
|
+
const VERSION = 4
|
|
10
10
|
|
|
11
11
|
// eslint-disable-next-line no-unused-vars
|
|
12
12
|
let version = VERSION
|
|
@@ -315,6 +315,47 @@ const encoding8 = {
|
|
|
315
315
|
}
|
|
316
316
|
}
|
|
317
317
|
|
|
318
|
+
// @cero/pass.epochs
|
|
319
|
+
const encoding9_3 = encoding4_4
|
|
320
|
+
|
|
321
|
+
// @cero/pass
|
|
322
|
+
const encoding9 = {
|
|
323
|
+
preencode(state, m) {
|
|
324
|
+
c.string.preencode(state, m.invite)
|
|
325
|
+
c.fixed32.preencode(state, m.key)
|
|
326
|
+
c.fixed32.preencode(state, m.encryptionKey)
|
|
327
|
+
state.end++ // max flag is 2 so always one byte
|
|
328
|
+
|
|
329
|
+
if (version >= 4 && m.epochs) encoding9_3.preencode(state, m.epochs)
|
|
330
|
+
if (version >= 4 && m.data) c.buffer.preencode(state, m.data)
|
|
331
|
+
},
|
|
332
|
+
encode(state, m) {
|
|
333
|
+
const flags = ((version >= 4 && m.epochs) ? 1 : 0) | ((version >= 4 && m.data) ? 2 : 0)
|
|
334
|
+
|
|
335
|
+
c.string.encode(state, m.invite)
|
|
336
|
+
c.fixed32.encode(state, m.key)
|
|
337
|
+
c.fixed32.encode(state, m.encryptionKey)
|
|
338
|
+
c.uint.encode(state, flags)
|
|
339
|
+
|
|
340
|
+
if (version >= 4 && m.epochs) encoding9_3.encode(state, m.epochs)
|
|
341
|
+
if (version >= 4 && m.data) c.buffer.encode(state, m.data)
|
|
342
|
+
},
|
|
343
|
+
decode(state) {
|
|
344
|
+
const r0 = c.string.decode(state)
|
|
345
|
+
const r1 = c.fixed32.decode(state)
|
|
346
|
+
const r2 = c.fixed32.decode(state)
|
|
347
|
+
const flags = c.uint.decode(state)
|
|
348
|
+
|
|
349
|
+
return {
|
|
350
|
+
invite: r0,
|
|
351
|
+
key: r1,
|
|
352
|
+
encryptionKey: r2,
|
|
353
|
+
epochs: (version >= 4 && (flags & 1) !== 0) ? encoding9_3.decode(state) : null,
|
|
354
|
+
data: (version >= 4 && (flags & 2) !== 0) ? c.buffer.decode(state) : null
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
318
359
|
function setVersion(v) {
|
|
319
360
|
version = v
|
|
320
361
|
}
|
|
@@ -356,6 +397,8 @@ function getEncoding(name) {
|
|
|
356
397
|
return encoding7
|
|
357
398
|
case '@cero/blob-id':
|
|
358
399
|
return encoding8
|
|
400
|
+
case '@cero/pass':
|
|
401
|
+
return encoding9
|
|
359
402
|
default:
|
|
360
403
|
throw new Error('Encoder not found ' + name)
|
|
361
404
|
}
|
package/src/lib/spec/schema.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version":
|
|
2
|
+
"version": 4,
|
|
3
3
|
"schema": [
|
|
4
4
|
{
|
|
5
5
|
"name": "link",
|
|
@@ -292,6 +292,45 @@
|
|
|
292
292
|
"version": 1
|
|
293
293
|
}
|
|
294
294
|
]
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
"name": "pass",
|
|
298
|
+
"namespace": "cero",
|
|
299
|
+
"compact": false,
|
|
300
|
+
"flagsPosition": 3,
|
|
301
|
+
"fields": [
|
|
302
|
+
{
|
|
303
|
+
"name": "invite",
|
|
304
|
+
"required": true,
|
|
305
|
+
"type": "string",
|
|
306
|
+
"version": 4
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
"name": "key",
|
|
310
|
+
"required": true,
|
|
311
|
+
"type": "fixed32",
|
|
312
|
+
"version": 4
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
"name": "encryptionKey",
|
|
316
|
+
"required": true,
|
|
317
|
+
"type": "fixed32",
|
|
318
|
+
"version": 4
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
"name": "epochs",
|
|
322
|
+
"required": false,
|
|
323
|
+
"array": true,
|
|
324
|
+
"type": "@cero/epoch",
|
|
325
|
+
"version": 4
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
"name": "data",
|
|
329
|
+
"required": false,
|
|
330
|
+
"type": "buffer",
|
|
331
|
+
"version": 4
|
|
332
|
+
}
|
|
333
|
+
]
|
|
295
334
|
}
|
|
296
335
|
]
|
|
297
336
|
}
|
package/src/pairing/index.js
CHANGED
|
@@ -25,6 +25,8 @@ import { CeroError } from '../lib/errors.js'
|
|
|
25
25
|
import { can, grants, isRank, isOptions, joining, INVITE, REMOVE } from '../lib/utils.js'
|
|
26
26
|
import { NAMESPACE, MEMBER, REFUSALS, SPENT, ASKS } from '../lib/constants.js'
|
|
27
27
|
|
|
28
|
+
export { Pass } from './pass.js'
|
|
29
|
+
|
|
28
30
|
const Join = getEncoding('@cero/join')
|
|
29
31
|
// what a sync follows in the log
|
|
30
32
|
const WATCHED = ['*', 'invites', 'requests', 'members', 'devices', ASKS]
|
|
@@ -43,6 +45,7 @@ const MAX_DELAY = 2 ** 31 - 1
|
|
|
43
45
|
* @property {boolean} [reuse] Admit more than one joiner. Otherwise spent by the first.
|
|
44
46
|
* @property {boolean} [confirm] Its joins wait for a member to accept them, as requests.
|
|
45
47
|
* @property {Uint8Array | null} [data] The app's payload in the invite, readable before joining: `Invite.parse(invite).data`.
|
|
48
|
+
* @property {string | null} [to] The id of the one member it is made for: nobody else joins with it. Single-use, never `confirm`.
|
|
46
49
|
*
|
|
47
50
|
* @typedef {object} AcceptOpts
|
|
48
51
|
* @property {string} [role] Role granted: the invite's by default, at most the invite's.
|
|
@@ -144,8 +147,16 @@ export class Pairing extends ReadyResource {
|
|
|
144
147
|
async invite(opts = {}) {
|
|
145
148
|
if (!isOptions(opts)) throw CeroError.INVALID('invite options are an object')
|
|
146
149
|
const { role = MEMBER, ttl = 0, reuse = false, confirm = false, data = null } = opts
|
|
150
|
+
const to = opts.to == null ? null : memberId(opts.to)
|
|
151
|
+
if (to && (reuse || confirm)) {
|
|
152
|
+
throw CeroError.INVALID("an invite made 'to' a member is single-use and waits for no accept")
|
|
153
|
+
}
|
|
147
154
|
if (this.closing || this.closed) throw CeroError.CLOSED('Pairing')
|
|
148
155
|
if (!this.opened) await this.ready()
|
|
156
|
+
// the encoder drops a field the built spec lacks: the invite would let anyone in
|
|
157
|
+
if (to && !('to' in (this.db.meta.since?.types[`@${this.db.ns}/invite`] ?? {}))) {
|
|
158
|
+
throw CeroError.UNSUPPORTED("'to' in 'invites'")
|
|
159
|
+
}
|
|
149
160
|
// an invite is a capability: capped at our own rank, or apply would drop the mismatch silently
|
|
150
161
|
await this._checkGrant(role)
|
|
151
162
|
// a rank above member is handed out once
|
|
@@ -163,7 +174,8 @@ export class Pairing extends ReadyResource {
|
|
|
163
174
|
confirm,
|
|
164
175
|
expires: invite.expires,
|
|
165
176
|
createdAt: Date.now(),
|
|
166
|
-
secret
|
|
177
|
+
secret,
|
|
178
|
+
to
|
|
167
179
|
}
|
|
168
180
|
invite.stamp = await this.db.write([['add-invite', row]])
|
|
169
181
|
if (secret || invite.stamp > this.db.start) invite.version = STAMPED
|
|
@@ -285,7 +297,8 @@ export class Pairing extends ReadyResource {
|
|
|
285
297
|
const secrets = new Map([...invites, ...spent].map((row) => [row.id, row.secret]))
|
|
286
298
|
await this._answer(owed, refused, role, secrets)
|
|
287
299
|
await this._deliver(await this._list(ASKS))
|
|
288
|
-
|
|
300
|
+
// one made for a member owes no answer: her device joins with the keys it was handed
|
|
301
|
+
const live = invites.some((row) => !row.to && !expired(row))
|
|
289
302
|
this.serving = inviter && (live || owed.length > 0 || refused.length > 0)
|
|
290
303
|
this.emit('serving', this.serving)
|
|
291
304
|
}
|
|
@@ -530,20 +543,9 @@ export class Pairing extends ReadyResource {
|
|
|
530
543
|
keep = async () => {}
|
|
531
544
|
} = {}
|
|
532
545
|
) {
|
|
533
|
-
|
|
534
|
-
if (typeof identity?.sign !== 'function') throw CeroError.REQUIRED('identity')
|
|
535
|
-
if (!spec?.dispatch) throw CeroError.REQUIRED('spec')
|
|
536
|
-
if (writer?.publicKey?.byteLength !== 32 || writer.secretKey?.byteLength !== 64) {
|
|
537
|
-
throw CeroError.INVALID('writer must be a keypair')
|
|
538
|
-
}
|
|
539
|
-
const parsed = Invite.parse(invite)
|
|
540
|
-
if (parsed.expired) throw CeroError.EXPIRED()
|
|
546
|
+
const { parsed, stamp } = joinable(mailbox, invite, { identity, spec, writer })
|
|
541
547
|
// an invite with its own address proves its answers: one without a valid tag is nobody's
|
|
542
548
|
const mine = parsed.own ? Mailbox.keyPair(writer.secretKey) : null
|
|
543
|
-
// an invite with no stamp was minted before invites carried one: at the start
|
|
544
|
-
const { version = 1, start = 1 } = spec.meta || {}
|
|
545
|
-
const stamp = parsed.stamp || start
|
|
546
|
-
if (stamp > version) throw CeroError.BEHIND(stamp)
|
|
547
549
|
if (mailbox.closing || mailbox.closed) throw CeroError.CLOSED('join')
|
|
548
550
|
if (!mailbox.opened) await mailbox.ready()
|
|
549
551
|
|
|
@@ -587,11 +589,7 @@ export class Pairing extends ReadyResource {
|
|
|
587
589
|
|
|
588
590
|
// in the network's store, which every connection replicates: open the database from it
|
|
589
591
|
const { network } = mailbox
|
|
590
|
-
const
|
|
591
|
-
const core = store.get({
|
|
592
|
-
keyPair: writer,
|
|
593
|
-
manifest: { version: store.manifestVersion, signers: [{ publicKey: writer.publicKey }] }
|
|
594
|
-
})
|
|
592
|
+
const core = coreOf(network.store, writer)
|
|
595
593
|
// a reply read no longer than the join waits for it
|
|
596
594
|
const reads = timeout > 0 ? { timeout: Math.min(timeout, 30000) } : {}
|
|
597
595
|
const inbox = mailbox.receive(writer.secretKey, onreply, { keep: false, ...reads })
|
|
@@ -617,7 +615,7 @@ export class Pairing extends ReadyResource {
|
|
|
617
615
|
const left = parsed.expires - Date.now()
|
|
618
616
|
// Node's setTimeout overflows past ~24.8 days: a longer ttl expires at the next resume
|
|
619
617
|
const expiry = parsed.expires && left < MAX_DELAY ? setTimeout(expire, left) : null
|
|
620
|
-
|
|
618
|
+
written(core, spec, stamp, parsed, identity, writer)
|
|
621
619
|
.then(() => post.ready())
|
|
622
620
|
.catch((err) => fail(CeroError.NETWORK_ERROR(err.message)))
|
|
623
621
|
try {
|
|
@@ -631,6 +629,28 @@ export class Pairing extends ReadyResource {
|
|
|
631
629
|
}
|
|
632
630
|
}
|
|
633
631
|
|
|
632
|
+
/**
|
|
633
|
+
* Write the join into the writer's own core and stop there: nothing is announced and no reply
|
|
634
|
+
* is waited for. A device that holds the database's keys opens it with this writer and applies
|
|
635
|
+
* its own join; `Pairing.join` with the same writer goes on from it and writes nothing more.
|
|
636
|
+
*
|
|
637
|
+
* @param {Mailbox} mailbox
|
|
638
|
+
* @param {string} invite
|
|
639
|
+
* @param {{ identity: JoinOpts['identity'], spec: JoinOpts['spec'], writer: KeyPair }} opts
|
|
640
|
+
* @returns {Promise<void>}
|
|
641
|
+
*/
|
|
642
|
+
static async write(mailbox, invite, { identity, spec, writer } = {}) {
|
|
643
|
+
const { parsed, stamp } = joinable(mailbox, invite, { identity, spec, writer })
|
|
644
|
+
if (mailbox.closing || mailbox.closed) throw CeroError.CLOSED('join')
|
|
645
|
+
if (!mailbox.opened) await mailbox.ready()
|
|
646
|
+
const core = coreOf(mailbox.network.store, writer)
|
|
647
|
+
try {
|
|
648
|
+
await written(core, spec, stamp, parsed, identity, writer)
|
|
649
|
+
} finally {
|
|
650
|
+
await core.close()
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
634
654
|
/**
|
|
635
655
|
* Take back a join that waits for a member: the writer's own core says so after its join, and
|
|
636
656
|
* the database drops the request unless a member let it in first. Announced as the join was.
|
|
@@ -713,6 +733,44 @@ export function sealJoin(invite, identity, writer, reply) {
|
|
|
713
733
|
// the cancel being appended, by writer: two at once on one writer append it once
|
|
714
734
|
const cancels = new Map()
|
|
715
735
|
|
|
736
|
+
// the join being written, by writer: two at once on one writer write it once
|
|
737
|
+
const writing = new Map()
|
|
738
|
+
|
|
739
|
+
// what a join is made with, checked before anything is written: its invite, and the stamp it takes
|
|
740
|
+
function joinable(mailbox, invite, { identity, spec, writer }) {
|
|
741
|
+
if (!mailbox) throw CeroError.REQUIRED('mailbox')
|
|
742
|
+
if (typeof identity?.sign !== 'function') throw CeroError.REQUIRED('identity')
|
|
743
|
+
if (!spec?.dispatch) throw CeroError.REQUIRED('spec')
|
|
744
|
+
if (writer?.publicKey?.byteLength !== 32 || writer.secretKey?.byteLength !== 64) {
|
|
745
|
+
throw CeroError.INVALID('writer must be a keypair')
|
|
746
|
+
}
|
|
747
|
+
const parsed = Invite.parse(invite)
|
|
748
|
+
if (parsed.expired) throw CeroError.EXPIRED()
|
|
749
|
+
// an invite with no stamp was minted before invites carried one: at the start
|
|
750
|
+
const { version = 1, start = 1 } = spec.meta || {}
|
|
751
|
+
const stamp = parsed.stamp || start
|
|
752
|
+
if (stamp > version) throw CeroError.BEHIND(stamp)
|
|
753
|
+
return { parsed, stamp }
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// a writer's core in the network's store, which every connection replicates
|
|
757
|
+
function coreOf(store, writer) {
|
|
758
|
+
return store.get({
|
|
759
|
+
keyPair: writer,
|
|
760
|
+
manifest: { version: store.manifestVersion, signers: [{ publicKey: writer.publicKey }] }
|
|
761
|
+
})
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
function written(core, spec, stamp, invite, identity, writer) {
|
|
765
|
+
const id = b4a.toHex(writer.publicKey)
|
|
766
|
+
const run = () => write(core, spec, stamp, invite, identity, writer)
|
|
767
|
+
const last = (writing.get(id) ?? Promise.resolve()).then(run, run)
|
|
768
|
+
writing.set(id, last)
|
|
769
|
+
const done = () => writing.get(id) === last && writing.delete(id)
|
|
770
|
+
last.then(done, done)
|
|
771
|
+
return last
|
|
772
|
+
}
|
|
773
|
+
|
|
716
774
|
async function retract(core, op) {
|
|
717
775
|
const at = await core.getUserData('cero/cancel')
|
|
718
776
|
if (at && c.decode(c.uint, at) === core.length) return
|
|
@@ -768,3 +826,12 @@ function getInviteId(invite) {
|
|
|
768
826
|
return null
|
|
769
827
|
}
|
|
770
828
|
}
|
|
829
|
+
|
|
830
|
+
// a member's id as its row has it, from any form of the identity's key
|
|
831
|
+
function memberId(to) {
|
|
832
|
+
try {
|
|
833
|
+
return hid.normalize(to)
|
|
834
|
+
} catch {
|
|
835
|
+
throw CeroError.INVALID("'to' is a member's id")
|
|
836
|
+
}
|
|
837
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import b4a from 'b4a'
|
|
2
|
+
import c from 'compact-encoding'
|
|
3
|
+
import hid from 'hypercore-id-encoding'
|
|
4
|
+
|
|
5
|
+
import { Invite, STAMPED } from './invite.js'
|
|
6
|
+
import { Identity } from '../identity/index.js'
|
|
7
|
+
import { wraps, opened } from '../database/encryption.js'
|
|
8
|
+
import { getEncoding } from '../lib/spec/index.js'
|
|
9
|
+
import { CeroError } from '../lib/errors.js'
|
|
10
|
+
import { MAX_PASS } from '../lib/constants.js'
|
|
11
|
+
|
|
12
|
+
const Encoding = getEncoding('@cero/pass')
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {object} PassFields
|
|
16
|
+
* @property {string} invite An invite into the database.
|
|
17
|
+
* @property {Uint8Array} key The database's key, the one its invite opens.
|
|
18
|
+
* @property {Uint8Array} encryptionKey The database's encryption key.
|
|
19
|
+
* @property {Array<{ epoch: number, stamp: number, entropy: Uint8Array }>} [epochs] Its rotation epochs.
|
|
20
|
+
* @property {Uint8Array | null} [data] The sealer's own bytes, read only by a device that opens the pass.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* An invite into a database with the keys that read it, sealed to devices: one that opens its box
|
|
25
|
+
* joins with the keys in hand and waits for no answer. Unsigned, so whoever opens one holds what
|
|
26
|
+
* it carries to the database's own log.
|
|
27
|
+
*/
|
|
28
|
+
export const Pass = { seal, open, fits }
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* One box per device, under its device key. A key nothing can be sealed to gets none.
|
|
32
|
+
*
|
|
33
|
+
* @param {Array<{ key: Uint8Array }>} devices
|
|
34
|
+
* @param {PassFields} fields
|
|
35
|
+
* @returns {Uint8Array}
|
|
36
|
+
*/
|
|
37
|
+
function seal(devices, fields) {
|
|
38
|
+
if (!Array.isArray(devices) || devices.some((d) => d?.key?.byteLength !== 32)) {
|
|
39
|
+
throw CeroError.INVALID('devices are rows with a 32-byte key')
|
|
40
|
+
}
|
|
41
|
+
const message = wire(() => c.encode(Encoding, checked(fields)))
|
|
42
|
+
const boxes = []
|
|
43
|
+
for (const { key } of devices) {
|
|
44
|
+
try {
|
|
45
|
+
boxes.push({ id: hid.encode(key), box: Identity.seal(key, message) })
|
|
46
|
+
} catch {
|
|
47
|
+
// not a point on the curve: no device holds its secret
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return c.encode(wraps, boxes)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What a pass carries, read from this device's own box.
|
|
55
|
+
*
|
|
56
|
+
* @param {import('../identity/index.js').Identity} identity
|
|
57
|
+
* @param {Uint8Array} pass
|
|
58
|
+
* @returns {PassFields | null} Null when no box is this device's.
|
|
59
|
+
*/
|
|
60
|
+
function open(identity, pass) {
|
|
61
|
+
return wire(() => {
|
|
62
|
+
for (const message of opened(identity, identity.device.id, pass)) {
|
|
63
|
+
const fields = checked(c.decode(Encoding, message))
|
|
64
|
+
return { ...fields, epochs: fields.epochs || [] }
|
|
65
|
+
}
|
|
66
|
+
return null
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether a pass to `devices` with these keys stays within what a room keeps of one, whatever
|
|
72
|
+
* invite with no data it carries: known before the invite is minted.
|
|
73
|
+
*
|
|
74
|
+
* @param {Array<{ key: Uint8Array }>} devices
|
|
75
|
+
* @param {Omit<PassFields, 'invite'>} fields
|
|
76
|
+
* @returns {boolean}
|
|
77
|
+
*/
|
|
78
|
+
function fits(devices, fields) {
|
|
79
|
+
const { key } = fields
|
|
80
|
+
const widest = Number.MAX_SAFE_INTEGER
|
|
81
|
+
const address = Identity.randomKeyPair().publicKey
|
|
82
|
+
const link = { key, length: widest }
|
|
83
|
+
const at = { version: STAMPED, expires: widest, stamp: widest, own: true }
|
|
84
|
+
const invite = new Invite({ ...at, key, address, seed: key, link }).toString()
|
|
85
|
+
return seal(devices, { ...fields, invite }).byteLength <= MAX_PASS
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// the invite opens the database the keys are for
|
|
89
|
+
function checked(fields) {
|
|
90
|
+
if (!b4a.equals(Invite.parse(fields?.invite).key, fields.key)) {
|
|
91
|
+
throw new Error('its invite is for another database')
|
|
92
|
+
}
|
|
93
|
+
return fields
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// what does not encode, or decode, is no pass
|
|
97
|
+
function wire(fn) {
|
|
98
|
+
try {
|
|
99
|
+
return fn()
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw CeroError.INVALID(`not a pass: ${err.message}`)
|
|
102
|
+
}
|
|
103
|
+
}
|
package/types/lib/constants.d.ts
CHANGED
package/types/pairing/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import ReadyResource from 'ready-resource';
|
|
2
2
|
import { Invite } from './invite.js';
|
|
3
3
|
import { Mailbox } from '../mailbox/index.js';
|
|
4
|
+
export { Pass } from './pass.js';
|
|
4
5
|
export type KeyPair = {
|
|
5
6
|
publicKey: Uint8Array;
|
|
6
7
|
secretKey: Uint8Array;
|
|
@@ -36,6 +37,10 @@ export type InviteOpts = {
|
|
|
36
37
|
* The app's payload in the invite, readable before joining: `Invite.parse(invite).data`.
|
|
37
38
|
*/
|
|
38
39
|
data?: Uint8Array | null;
|
|
40
|
+
/**
|
|
41
|
+
* The id of the one member it is made for: nobody else joins with it. Single-use, never `confirm`.
|
|
42
|
+
*/
|
|
43
|
+
to?: string | null;
|
|
39
44
|
};
|
|
40
45
|
export type AcceptOpts = {
|
|
41
46
|
/**
|
|
@@ -113,6 +118,7 @@ export type JoinResult = {
|
|
|
113
118
|
* @property {boolean} [reuse] Admit more than one joiner. Otherwise spent by the first.
|
|
114
119
|
* @property {boolean} [confirm] Its joins wait for a member to accept them, as requests.
|
|
115
120
|
* @property {Uint8Array | null} [data] The app's payload in the invite, readable before joining: `Invite.parse(invite).data`.
|
|
121
|
+
* @property {string | null} [to] The id of the one member it is made for: nobody else joins with it. Single-use, never `confirm`.
|
|
116
122
|
*
|
|
117
123
|
* @typedef {object} AcceptOpts
|
|
118
124
|
* @property {string} [role] Role granted: the invite's by default, at most the invite's.
|
|
@@ -248,6 +254,21 @@ export declare class Pairing extends ReadyResource {
|
|
|
248
254
|
* @returns {Promise<JoinResult>}
|
|
249
255
|
*/
|
|
250
256
|
static join(mailbox: Mailbox, invite: string, { identity, spec, writer, timeout, signal, keep }?: JoinOpts): Promise<JoinResult>;
|
|
257
|
+
/**
|
|
258
|
+
* Write the join into the writer's own core and stop there: nothing is announced and no reply
|
|
259
|
+
* is waited for. A device that holds the database's keys opens it with this writer and applies
|
|
260
|
+
* its own join; `Pairing.join` with the same writer goes on from it and writes nothing more.
|
|
261
|
+
*
|
|
262
|
+
* @param {Mailbox} mailbox
|
|
263
|
+
* @param {string} invite
|
|
264
|
+
* @param {{ identity: JoinOpts['identity'], spec: JoinOpts['spec'], writer: KeyPair }} opts
|
|
265
|
+
* @returns {Promise<void>}
|
|
266
|
+
*/
|
|
267
|
+
static write(mailbox: Mailbox, invite: string, { identity, spec, writer }?: {
|
|
268
|
+
identity: JoinOpts['identity'];
|
|
269
|
+
spec: JoinOpts['spec'];
|
|
270
|
+
writer: KeyPair;
|
|
271
|
+
}): Promise<void>;
|
|
251
272
|
/**
|
|
252
273
|
* Take back a join that waits for a member: the writer's own core says so after its join, and
|
|
253
274
|
* the database drops the request unless a member let it in first. Announced as the join was.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export type PassFields = {
|
|
2
|
+
/**
|
|
3
|
+
* An invite into the database.
|
|
4
|
+
*/
|
|
5
|
+
invite: string;
|
|
6
|
+
/**
|
|
7
|
+
* The database's key, the one its invite opens.
|
|
8
|
+
*/
|
|
9
|
+
key: Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* The database's encryption key.
|
|
12
|
+
*/
|
|
13
|
+
encryptionKey: Uint8Array;
|
|
14
|
+
/**
|
|
15
|
+
* Its rotation epochs.
|
|
16
|
+
*/
|
|
17
|
+
epochs?: Array<{
|
|
18
|
+
epoch: number;
|
|
19
|
+
stamp: number;
|
|
20
|
+
entropy: Uint8Array;
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* The sealer's own bytes, read only by a device that opens the pass.
|
|
24
|
+
*/
|
|
25
|
+
data?: Uint8Array | null;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {object} PassFields
|
|
29
|
+
* @property {string} invite An invite into the database.
|
|
30
|
+
* @property {Uint8Array} key The database's key, the one its invite opens.
|
|
31
|
+
* @property {Uint8Array} encryptionKey The database's encryption key.
|
|
32
|
+
* @property {Array<{ epoch: number, stamp: number, entropy: Uint8Array }>} [epochs] Its rotation epochs.
|
|
33
|
+
* @property {Uint8Array | null} [data] The sealer's own bytes, read only by a device that opens the pass.
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* An invite into a database with the keys that read it, sealed to devices: one that opens its box
|
|
37
|
+
* joins with the keys in hand and waits for no answer. Unsigned, so whoever opens one holds what
|
|
38
|
+
* it carries to the database's own log.
|
|
39
|
+
*/
|
|
40
|
+
export declare const Pass: {
|
|
41
|
+
seal: typeof seal;
|
|
42
|
+
open: typeof open;
|
|
43
|
+
fits: typeof fits;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* One box per device, under its device key. A key nothing can be sealed to gets none.
|
|
47
|
+
*
|
|
48
|
+
* @param {Array<{ key: Uint8Array }>} devices
|
|
49
|
+
* @param {PassFields} fields
|
|
50
|
+
* @returns {Uint8Array}
|
|
51
|
+
*/
|
|
52
|
+
declare function seal(devices: Array<{
|
|
53
|
+
key: Uint8Array;
|
|
54
|
+
}>, fields: PassFields): Uint8Array;
|
|
55
|
+
/**
|
|
56
|
+
* What a pass carries, read from this device's own box.
|
|
57
|
+
*
|
|
58
|
+
* @param {import('../identity/index.js').Identity} identity
|
|
59
|
+
* @param {Uint8Array} pass
|
|
60
|
+
* @returns {PassFields | null} Null when no box is this device's.
|
|
61
|
+
*/
|
|
62
|
+
declare function open(identity: import('../identity/index.js').Identity, pass: Uint8Array): PassFields | null;
|
|
63
|
+
/**
|
|
64
|
+
* Whether a pass to `devices` with these keys stays within what a room keeps of one, whatever
|
|
65
|
+
* invite with no data it carries: known before the invite is minted.
|
|
66
|
+
*
|
|
67
|
+
* @param {Array<{ key: Uint8Array }>} devices
|
|
68
|
+
* @param {Omit<PassFields, 'invite'>} fields
|
|
69
|
+
* @returns {boolean}
|
|
70
|
+
*/
|
|
71
|
+
declare function fits(devices: Array<{
|
|
72
|
+
key: Uint8Array;
|
|
73
|
+
}>, fields: Omit<PassFields, 'invite'>): boolean;
|
|
74
|
+
export {};
|