@cero-base/cero 2.5.0 → 2.7.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.
@@ -234,11 +234,8 @@ function topicOf(base, topic) {
234
234
  function signed(info, key) {
235
235
  if (typeof info?.id !== 'string' || typeof info.proof !== 'string') return false
236
236
  try {
237
- return Identity.verify(
238
- hid.decode(info.id),
239
- b4a.concat([DEVICE, key]),
240
- b4a.from(info.proof, 'hex')
241
- )
237
+ const proof = b4a.from(info.proof, 'hex')
238
+ return Identity.verify(proof, b4a.concat([DEVICE, key]), hid.decode(info.id)) !== null
242
239
  } catch {
243
240
  return false
244
241
  }
@@ -106,6 +106,12 @@ const WRITES = { single: ['set', 'del'], collection: ['put', 'set', 'del'] }
106
106
  * opens; register in an extension's `setup` to see every op the root applies too. The imported
107
107
  * operators throw inside a hook; use the ones on `ctx`. Not available over RPC.
108
108
  *
109
+ * On `members`, removing someone or changing her rank is the rank's call by default: the writer
110
+ * outranks whom she acts on, and `ctx.ranked` says whether she does. A rule there may return
111
+ * `'grant'` to allow what rank alone refuses (a bare `true` grants nothing), or `false` to refuse
112
+ * what it allows, and only a writer's grant counts. It is decided before anything moves. The
113
+ * last owner is never removed nor demoted.
114
+ *
109
115
  * @param {Ref} ref
110
116
  * @param {(ctx: HookContext) => unknown} fn
111
117
  * @param {{ signal?: AbortSignal }} [opts]
@@ -485,12 +491,12 @@ export async function deactivate(ctx) {
485
491
  }
486
492
 
487
493
  /**
488
- * A push the platform delivered, as bytes: the handle it is about, verified against this device's
489
- * own data, or `null` for a forged one or one about no handle here.
494
+ * A push the platform delivered, as bytes: the handle it is about and the ref the write was on,
495
+ * verified against this device's own data, or `null` for a forged one or one about no handle here.
490
496
  *
491
497
  * @param {Context} me
492
498
  * @param {Uint8Array} payload
493
- * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
499
+ * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
494
500
  */
495
501
  export async function receive(me, payload) {
496
502
  if (me.parent) throw CeroError.INVALID("receive is the app's, on me")
package/src/lib/peek.js CHANGED
@@ -10,11 +10,11 @@ import { Local } from '../local/index.js'
10
10
 
11
11
  /**
12
12
  * Quickly check whether the on-disk directory at `dir` already holds an initialised cero
13
- * identity (i.e. a stored master seed).
13
+ * identity (i.e. a stored device key with its proof).
14
14
  *
15
15
  * @param {string} dir Cero data directory.
16
16
  * @param {import('./spec.js').Spec} spec Built spec, the value passed to `cero(dir, spec)`.
17
- * @returns {Promise<boolean>} `true` if a master seed exists on disk.
17
+ * @returns {Promise<boolean>} `true` if a device of an identity exists on disk.
18
18
  */
19
19
  export async function peek(dir, spec) {
20
20
  if (typeof dir !== 'string' || !dir) throw CeroError.INVALID('dir must be a non-empty string')
@@ -28,8 +28,8 @@ export async function peek(dir, spec) {
28
28
  await root.ready()
29
29
  await store.ready()
30
30
  await local.ready()
31
- const { data } = await local.store.get('master')
32
- return !!data?.seed
31
+ const { data } = await local.store.get('device')
32
+ return !!data
33
33
  } finally {
34
34
  await local.close().catch(safetyCatch)
35
35
  await store.close().catch(safetyCatch)
package/src/lib/spec.js CHANGED
@@ -15,6 +15,7 @@ export { t, schema } from '@cero-base/core/schema'
15
15
  * @property {Record<string, string>} [required] The required fields and their types.
16
16
  * @property {string[]} [files] The fields holding file ids.
17
17
  * @property {string} [verb]
18
+ * @property {boolean} [push] Whether a write to this ref pushes a notification.
18
19
  *
19
20
  * @typedef {object} SpecMeta
20
21
  * @property {string} [ns]
@@ -1,11 +1,11 @@
1
1
  import b4a from 'b4a'
2
2
 
3
3
  /**
4
- * `@react-native-firebase/messaging` as a push backend: pass `messaging()`. Pushes to the app in
4
+ * `@react-native-firebase/messaging` as the messaging service `connect` takes for push: pass `messaging()`. Pushes to the app in
5
5
  * front; the push worklet answers the ones the OS delivers while it is not.
6
6
  *
7
7
  * @param {{ subscribeToTopic: (topic: string) => Promise<void>, unsubscribeFromTopic: (topic: string) => Promise<void>, onMessage: (fn: (message: object) => void) => () => void }} messaging
8
- * @returns {import('../rpc/push.js').Backend}
8
+ * @returns {import('../rpc/push.js').Messaging}
9
9
  */
10
10
  export function firebase(messaging) {
11
11
  const payload = (message) => message?.data?.payload && b4a.from(message.data.payload, 'base64')
package/src/push/index.js CHANGED
@@ -3,7 +3,8 @@ import Corestore from 'corestore'
3
3
  import b4a from 'b4a'
4
4
  import safetyCatch from 'safety-catch'
5
5
 
6
- import { receive as verify } from '@cero-base/core/push'
6
+ import { verify } from '@cero-base/core/push'
7
+ import { CeroError } from '@cero-base/core/errors'
7
8
 
8
9
  import { Local } from '../local/index.js'
9
10
 
@@ -16,7 +17,7 @@ import { Local } from '../local/index.js'
16
17
  * @param {import('../lib/spec.js').Spec} spec
17
18
  * @param {Uint8Array} payload
18
19
  * @param {{ storageKey?: Uint8Array }} [opts] The key `cero()` encrypts the local store with, if any.
19
- * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
20
+ * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
20
21
  */
21
22
  export async function receive(dir, spec, payload, { storageKey } = {}) {
22
23
  const storage = new HypercoreStorage(`${dir}/main`, { readOnly: true })
@@ -30,7 +31,7 @@ export async function receive(dir, spec, payload, { storageKey } = {}) {
30
31
  const { data: rows } = await local.store.get('topics')
31
32
  const handles = rows.map(({ handle, key, type, name }) => ({ id: handle, key, type, name }))
32
33
  const hit = await verify(storage, payload, handles)
33
- return hit && { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer }
34
+ return hit && { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer, ref: hit.ref }
34
35
  } catch (err) {
35
36
  safetyCatch(err)
36
37
  return null
@@ -42,26 +43,29 @@ export async function receive(dir, spec, payload, { storageKey } = {}) {
42
43
  }
43
44
 
44
45
  /**
45
- * The bare-kit push entry: on each push the OS hands this worklet, verify it and answer with the
46
- * notification to show, or nothing. The request carries `storage`, `payload` (base64) and, when
47
- * the app encrypts its local store, `storageKey` (base64).
46
+ * The bare-kit push entry: on each push the OS hands this worklet, verify it and, for one about
47
+ * new data on a handle this device holds, answer with what `show` returns: the notification,
48
+ * `{ title, body }`, or nothing. The request carries `storage`, `payload` (base64) and, when the
49
+ * app encrypts its local store, `storageKey` (base64).
48
50
  *
49
51
  * @param {import('../lib/spec.js').Spec} spec
52
+ * @param {(push: { id: string, type: string, name: string | null, newer: boolean, ref: string | null }) => { title: string, body: string } | null | undefined | Promise<{ title: string, body: string } | null | undefined>} show What to show for a verified push, as `cero.receive` would give it to the app.
50
53
  * @returns {void}
51
54
  */
52
- export function push(spec) {
55
+ export function push(spec, show) {
56
+ if (typeof show !== 'function') throw CeroError.INVALID('push: show(push) says what to show')
53
57
  globalThis.BareKit.on('push', async (json, reply) => {
54
- let hit = null
58
+ let shown = null
55
59
  try {
56
60
  const { storage, storageKey, payload } = JSON.parse(json)
57
- hit = await receive(storage, spec, b4a.from(payload, 'base64'), {
61
+ const hit = await receive(storage, spec, b4a.from(payload, 'base64'), {
58
62
  storageKey: storageKey ? b4a.from(storageKey, 'base64') : undefined
59
63
  })
64
+ if (hit?.newer) shown = await show(hit)
60
65
  } catch {
61
66
  // the OS waits for an answer: whatever went wrong, no notification
62
- hit = null
67
+ shown = null
63
68
  }
64
- const shown = hit?.newer ? { title: hit.name || hit.type, body: 'New activity' } : null
65
- reply(null, shown && JSON.stringify(shown))
69
+ reply(null, shown ? JSON.stringify(shown) : null)
66
70
  })
67
71
  }
package/src/rpc/client.js CHANGED
@@ -424,6 +424,28 @@ export async function restore(me, phrase) {
424
424
  return me
425
425
  }
426
426
 
427
+ /**
428
+ * Link the app to the identity behind a device invite, as `cero.link` does in the worker: the
429
+ * client is then that identity. `onrequest` gets the request's id as it is sent.
430
+ *
431
+ * @param {Client} me
432
+ * @param {string} invite
433
+ * @param {{ onrequest?: (id: string) => void }} [opts]
434
+ * @returns {Promise<Client>}
435
+ */
436
+ export function link(me, invite, { onrequest } = {}) {
437
+ return new Promise((resolve, reject) => {
438
+ const wire = me.rpc.link({ invite })
439
+ wire.on('data', (frame) => {
440
+ if (frame.request) onrequest?.(frame.request)
441
+ else me._identify(frame)
442
+ })
443
+ wire.on('error', reject)
444
+ wire.on('end', () => resolve(me))
445
+ wire.on('close', () => reject(CeroError.INVALID('link: the worker closed the stream')))
446
+ })
447
+ }
448
+
427
449
  /**
428
450
  * Per-device `local`-namespace surface on a Client.
429
451
  */
@@ -465,12 +487,12 @@ export class Client extends RPCClient {
465
487
  /**
466
488
  * @param {import('streamx').Duplex} ipc Framed IPC stream (must be writable).
467
489
  * @param {Spec} spec Compiled cero spec (schema + rpc + handles).
468
- * @param {{ onerror?: (err: Error) => void }} [opts] Where the worker's background errors go; the console without one.
490
+ * @param {{ onerror?: (err: Error) => void, push?: import('./push.js').Messaging }} [opts] Where the worker's background errors go, the console without one; `push`: the platform's messaging service, to follow the handles' push topics.
469
491
  */
470
492
  constructor(ipc, spec, { onerror, push } = {}) {
471
493
  super(ipc, spec)
472
494
  /** @private */
473
- this._push = push?.backend || null
495
+ this._push = push || null
474
496
  if (spec.local?.schema && !spec.local.codec) bindCodec(spec.local)
475
497
  Object.assign(this, operators)
476
498
  /** @private */
@@ -614,7 +636,7 @@ class Handle {
614
636
  *
615
637
  * @param {import('streamx').Duplex} ipc
616
638
  * @param {Spec} spec
617
- * @param {{ onerror?: (err: Error) => void }} [opts]
639
+ * @param {{ onerror?: (err: Error) => void, push?: import('./push.js').Messaging }} [opts]
618
640
  * @returns {Promise<Client>}
619
641
  */
620
642
  export async function connect(ipc, spec, opts) {
@@ -629,15 +651,15 @@ export async function connect(ipc, spec, opts) {
629
651
  *
630
652
  * @param {import('streamx').Duplex} ipc Framed IPC duplex stream.
631
653
  * @param {Spec} spec Built cero spec.
632
- * @param {{ onerror?: (err: Error) => void }} [opts]
654
+ * @param {{ onerror?: (err: Error) => void, push?: import('./push.js').Messaging }} [opts]
633
655
  * @returns {Promise<Context>}
634
656
  */
635
657
  function start(ipc, spec, opts) {
636
658
  return /** @type {Promise<Context>} */ (connect(ipc, spec, opts))
637
659
  }
638
660
 
639
- /** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, t: typeof t, schema: typeof schema }} */
640
- export const cero = Object.assign(start, remote, { connect, restore, t, schema })
661
+ /** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, link: typeof link, t: typeof t, schema: typeof schema }} */
662
+ export const cero = Object.assign(start, remote, { connect, restore, link, t, schema })
641
663
 
642
664
  // the same shape a local root has
643
665
  function device({ deviceId, deviceName }) {
package/src/rpc/push.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { watch, get, open, activate, receive } from '../lib/operators.js'
2
2
 
3
3
  /**
4
- * @typedef {object} Backend The platform's push service, as `connect` takes it.
4
+ * @typedef {object} Messaging The platform's messaging service, as `connect` takes it for push.
5
5
  * @property {(topic: string) => Promise<void>} subscribe
6
6
  * @property {(topic: string) => Promise<void>} unsubscribe
7
7
  * @property {(event: 'push', fn: (payload: Uint8Array) => void) => (() => void) | void} on
@@ -12,10 +12,10 @@ import { watch, get, open, activate, receive } from '../lib/operators.js'
12
12
  * leaves; and act on a push: the handle it is about ranks first on the swarm.
13
13
  *
14
14
  * @param {import('./client.js').Client} me
15
- * @param {Backend} backend
15
+ * @param {Messaging} messaging
16
16
  * @returns {() => void}
17
17
  */
18
- export function follow(me, backend) {
18
+ export function follow(me, messaging) {
19
19
  const topics = new Set()
20
20
  const stream = watch(me.topics)
21
21
  let closed = false
@@ -26,7 +26,7 @@ export function follow(me, backend) {
26
26
  for (const topic of wanted) {
27
27
  if (topics.has(topic)) continue
28
28
  try {
29
- await backend.subscribe(topic)
29
+ await messaging.subscribe(topic)
30
30
  topics.add(topic)
31
31
  } catch (err) {
32
32
  me._onerror(err)
@@ -35,12 +35,12 @@ export function follow(me, backend) {
35
35
  for (const topic of topics) {
36
36
  if (wanted.has(topic)) continue
37
37
  topics.delete(topic)
38
- await backend.unsubscribe(topic).catch(me._onerror)
38
+ await messaging.unsubscribe(topic).catch(me._onerror)
39
39
  }
40
40
  }
41
41
  }
42
42
  sync().catch((err) => closed || me._onerror(err))
43
- const off = backend.on('push', (payload) => wake(me, payload).catch(me._onerror))
43
+ const off = messaging.on('push', (payload) => wake(me, payload).catch(me._onerror))
44
44
  return () => {
45
45
  closed = true
46
46
  stream.destroy()
package/src/rpc/server.js CHANGED
@@ -4,7 +4,7 @@ import safetyCatch from 'safety-catch'
4
4
  import { RPCServer, bindCodec } from '@cero-base/core/rpc'
5
5
  import { CeroError } from '@cero-base/core/errors'
6
6
 
7
- import { cero, restore } from '../index.js'
7
+ import { cero, restore, link } from '../index.js'
8
8
  import {
9
9
  put,
10
10
  set,
@@ -139,6 +139,7 @@ export class Server extends RPCServer {
139
139
  this._wireHandles()
140
140
  this._wireVerbs()
141
141
  this._wireRestore()
142
+ this._wireLink()
142
143
  }
143
144
 
144
145
  /** @private */
@@ -206,6 +207,30 @@ export class Server extends RPCServer {
206
207
  })
207
208
  }
208
209
 
210
+ /**
211
+ * Wire the `link` handler: a stream, the request's id as it is sent, the identity once linked.
212
+ * @private
213
+ */
214
+ _wireLink() {
215
+ this.rpc.onLink((stream) => {
216
+ stream.on('error', safetyCatch)
217
+ const run = async () => {
218
+ if (!this.me) throw CeroError.NOT_READY('Server', 'server')
219
+ this.me = await link(this.me, stream.data.invite, {
220
+ onrequest: (id) => stream.write({ request: id })
221
+ })
222
+ this.handles = new Map([[this.me.id, this.me]])
223
+ stream.write({ request: '', ...this._identity() })
224
+ stream.end()
225
+ }
226
+ run().catch((err) => {
227
+ stream.once('error', () => {})
228
+ stream.writeStream.destroy(err)
229
+ stream.destroy()
230
+ })
231
+ })
232
+ }
233
+
209
234
  /**
210
235
  * Register the row-level RPC handlers (put/set/get/del/watch/call).
211
236
  * @private
@@ -24,6 +24,10 @@ export declare const defs: {
24
24
  type: string;
25
25
  kind: string;
26
26
  };
27
+ device: {
28
+ type: string;
29
+ kind: string;
30
+ };
27
31
  keypair: {
28
32
  type: string;
29
33
  kind: string;
@@ -5,7 +5,7 @@ export declare const main: {
5
5
  writer: {
6
6
  master: import("@cero-base/core").Prim;
7
7
  writer: import("@cero-base/core").Prim;
8
- sig: import("@cero-base/core").Prim;
8
+ proof: import("@cero-base/core").Prim;
9
9
  isIndexer: import("@cero-base/core").Prim;
10
10
  ts: import("@cero-base/core").Prim;
11
11
  memberId: import("@cero-base/core").Prim;
@@ -32,6 +32,8 @@ export declare const main: {
32
32
  createdAt: import("@cero-base/core").Prim;
33
33
  updatedAt: import("@cero-base/core").Prim;
34
34
  index: import("@cero-base/core").Prim;
35
+ key: import("@cero-base/core").Prim;
36
+ proof: import("@cero-base/core").Prim;
35
37
  };
36
38
  invite: {
37
39
  id: import("@cero-base/core").Prim;
@@ -51,6 +53,7 @@ export declare const main: {
51
53
  createdAt: import("@cero-base/core").Prim;
52
54
  updatedAt: import("@cero-base/core").Prim;
53
55
  index: import("@cero-base/core").Prim;
56
+ epochs: import("@cero-base/core").Prim;
54
57
  };
55
58
  file: {
56
59
  id: import("@cero-base/core").Prim;
@@ -75,6 +78,7 @@ export declare const main: {
75
78
  accept: {
76
79
  id: import("@cero-base/core").Prim;
77
80
  role: import("@cero-base/core").Prim;
81
+ proof: import("@cero-base/core").Prim;
78
82
  };
79
83
  request: {
80
84
  id: import("@cero-base/core").Prim;
@@ -86,6 +90,7 @@ export declare const main: {
86
90
  admitted: import("@cero-base/core").Prim;
87
91
  expires: import("@cero-base/core").Prim;
88
92
  role: import("@cero-base/core").Prim;
93
+ key: import("@cero-base/core").Prim;
89
94
  };
90
95
  removal: {
91
96
  id: import("@cero-base/core").Prim;
@@ -136,6 +141,13 @@ export declare const local: {
136
141
  master: {
137
142
  seed: import("@cero-base/core").Prim;
138
143
  };
144
+ device: {
145
+ publicKey: import("@cero-base/core").Prim;
146
+ secretKey: import("@cero-base/core").Prim;
147
+ proof: import("@cero-base/core").Prim;
148
+ identity: import("@cero-base/core").Prim;
149
+ encryptionKey: import("@cero-base/core").Prim;
150
+ };
139
151
  keypair: {
140
152
  publicKey: import("@cero-base/core").Prim;
141
153
  secretKey: import("@cero-base/core").Prim;
@@ -160,6 +172,8 @@ export declare const local: {
160
172
  key: import("@cero-base/core").Prim;
161
173
  encryptionKey: import("@cero-base/core").Prim;
162
174
  epochs: import("@cero-base/core").Prim;
175
+ device: import("@cero-base/core").Prim;
176
+ proof: import("@cero-base/core").Prim;
163
177
  };
164
178
  mail: {
165
179
  id: import("@cero-base/core").Prim;
@@ -185,6 +199,9 @@ export declare const rpc: {
185
199
  'req-restore': {
186
200
  phrase: import("@cero-base/core").Prim;
187
201
  };
202
+ 'req-link': {
203
+ invite: import("@cero-base/core").Prim;
204
+ };
188
205
  'req-row': {
189
206
  handle: import("@cero-base/core").Prim;
190
207
  ref: import("@cero-base/core").Prim;
@@ -260,6 +277,14 @@ export declare const rpc: {
260
277
  fileToken: import("@cero-base/core").Prim;
261
278
  deviceName: import("@cero-base/core").Prim;
262
279
  };
280
+ 'res-link': {
281
+ request: import("@cero-base/core").Prim;
282
+ id: import("@cero-base/core").Prim;
283
+ deviceId: import("@cero-base/core").Prim;
284
+ deviceName: import("@cero-base/core").Prim;
285
+ fileBase: import("@cero-base/core").Prim;
286
+ fileToken: import("@cero-base/core").Prim;
287
+ };
263
288
  'res-ok': {
264
289
  ok: import("@cero-base/core").Prim;
265
290
  };
@@ -54,6 +54,7 @@ export declare const cero: {
54
54
  type: string;
55
55
  name: string | null;
56
56
  newer: boolean;
57
+ ref: string | null;
57
58
  } | null>;
58
59
  phrase(me: operators.Context): Promise<string>;
59
60
  nearby(ctx: operators.Context, on: boolean, opts?: {
@@ -70,12 +71,17 @@ export declare const cero: {
70
71
  fixed64: import("@cero-base/core").Prim;
71
72
  file: import("@cero-base/core").Prim;
72
73
  required: (marker: import("@cero-base/core").Prim) => import("@cero-base/core").Prim;
73
- single(fields: Record<string, import("@cero-base/core").Prim>): import("@cero-base/core").TypeDef;
74
+ single(fields: Record<string, import("@cero-base/core").Prim>, opts?: {
75
+ push?: boolean;
76
+ }): import("@cero-base/core").TypeDef;
74
77
  collection(fields: Record<string, import("@cero-base/core").Prim>, opts?: {
75
78
  indexes?: Record<string, string[]>;
76
79
  own?: boolean;
80
+ push?: boolean;
81
+ }): import("@cero-base/core").TypeDef;
82
+ action(fields: Record<string, import("@cero-base/core").Prim>, opts?: {
83
+ push?: boolean;
77
84
  }): import("@cero-base/core").TypeDef;
78
- action(fields: Record<string, import("@cero-base/core").Prim>): import("@cero-base/core").TypeDef;
79
85
  extend(fields: Record<string, import("@cero-base/core").Prim>): {
80
86
  kind: 'extend';
81
87
  fields: Record<string, import("@cero-base/core").Prim>;
@@ -280,8 +280,6 @@ export declare class Handle extends ReadyResource {
280
280
  /** @private */
281
281
  private _tx;
282
282
  /** @private */
283
- private _pairing;
284
- /** @private */
285
283
  private _leave;
286
284
  /** @private */
287
285
  private _suspend;
@@ -416,6 +414,12 @@ export declare class Handle extends ReadyResource {
416
414
  /** @private */
417
415
  private _adopt;
418
416
  /** @private */
417
+ private _prune;
418
+ /** @private */
419
+ private _pruneAll;
420
+ /** @private */
421
+ private _carry;
422
+ /** @private */
419
423
  private _hookType;
420
424
  /** @private */
421
425
  private _serve;
package/types/index.d.ts CHANGED
@@ -13,6 +13,14 @@ export type CeroOpts = {
13
13
  * The identity's BIP-39 phrase, as `cero.phrase(me)` gives it: on a new `dir` it recovers that identity.
14
14
  */
15
15
  phrase?: string;
16
+ /**
17
+ * A device invite, as `cero.invite(me)` gives it on another device of yours: on a new `dir` this device joins that identity once a device of it accepts, with no phrase.
18
+ */
19
+ invite?: string;
20
+ /**
21
+ * With `invite`: called with the request's id as it is sent, the same id `me.requests` shows on the accepting device.
22
+ */
23
+ onrequest?: (id: string) => void;
16
24
  /**
17
25
  * Mnemonic length when generating a fresh identity.
18
26
  */
@@ -86,6 +94,8 @@ export type CeroOpts = {
86
94
  /**
87
95
  * @typedef {object} CeroOpts
88
96
  * @property {string} [phrase] The identity's BIP-39 phrase, as `cero.phrase(me)` gives it: on a new `dir` it recovers that identity.
97
+ * @property {string} [invite] A device invite, as `cero.invite(me)` gives it on another device of yours: on a new `dir` this device joins that identity once a device of it accepts, with no phrase.
98
+ * @property {(id: string) => void} [onrequest] With `invite`: called with the request's id as it is sent, the same id `me.requests` shows on the accepting device.
89
99
  * @property {12 | 24} [words] Mnemonic length when generating a fresh identity.
90
100
  * @property {string | null} [name] Friendly device name persisted on the identity claim.
91
101
  * @property {boolean} [isMobile] Marks this device as mobile.
@@ -117,10 +127,23 @@ declare function start(dir: string, spec: import('./lib/spec.js').Spec, opts?: C
117
127
  * @returns {Promise<Handle>} Freshly restored root handle.
118
128
  */
119
129
  export declare function restore(me: Context, phrase: string): Promise<Handle>;
120
- /** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore }} */
130
+ /**
131
+ * Link this device to the identity behind a device invite: closes `me`, deletes `dir/main` and
132
+ * opens the directory with `invite`, as a new device of that identity, with no phrase on it.
133
+ *
134
+ * @param {Context} me The root to replace.
135
+ * @param {string} invite A device invite, as `cero.invite(me)` gives it on a device of that identity.
136
+ * @param {{ onrequest?: (id: string) => void }} [opts] `onrequest`: the request's id as it is sent, the one the accepting device sees in `me.requests`.
137
+ * @returns {Promise<Handle>} The linked root handle.
138
+ */
139
+ export declare function link(me: Context, invite: string, { onrequest }?: {
140
+ onrequest?: (id: string) => void;
141
+ }): Promise<Handle>;
142
+ /** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore, link: typeof link }} */
121
143
  export declare const cero: typeof start & typeof verbs & {
122
144
  t: typeof t;
123
145
  schema: typeof schema;
124
146
  peek: typeof peek;
125
147
  restore: typeof restore;
148
+ link: typeof link;
126
149
  };
@@ -50,6 +50,12 @@ export declare function call(ref: Ref, d?: Row): Promise<void>;
50
50
  * opens; register in an extension's `setup` to see every op the root applies too. The imported
51
51
  * operators throw inside a hook; use the ones on `ctx`. Not available over RPC.
52
52
  *
53
+ * On `members`, removing someone or changing her rank is the rank's call by default: the writer
54
+ * outranks whom she acts on, and `ctx.ranked` says whether she does. A rule there may return
55
+ * `'grant'` to allow what rank alone refuses (a bare `true` grants nothing), or `false` to refuse
56
+ * what it allows, and only a writer's grant counts. It is decided before anything moves. The
57
+ * last owner is never removed nor demoted.
58
+ *
53
59
  * @param {Ref} ref
54
60
  * @param {(ctx: HookContext) => unknown} fn
55
61
  * @param {{ signal?: AbortSignal }} [opts]
@@ -224,18 +230,19 @@ export declare function activate(ctx: Context): Promise<void>;
224
230
  */
225
231
  export declare function deactivate(ctx: Context): Promise<void>;
226
232
  /**
227
- * A push the platform delivered, as bytes: the handle it is about, verified against this device's
228
- * own data, or `null` for a forged one or one about no handle here.
233
+ * A push the platform delivered, as bytes: the handle it is about and the ref the write was on,
234
+ * verified against this device's own data, or `null` for a forged one or one about no handle here.
229
235
  *
230
236
  * @param {Context} me
231
237
  * @param {Uint8Array} payload
232
- * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
238
+ * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
233
239
  */
234
240
  export declare function receive(me: Context, payload: Uint8Array): Promise<{
235
241
  id: string;
236
242
  type: string;
237
243
  name: string | null;
238
244
  newer: boolean;
245
+ ref: string | null;
239
246
  } | null>;
240
247
  /**
241
248
  * Reveal the recovery phrase.
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Quickly check whether the on-disk directory at `dir` already holds an initialised cero
3
- * identity (i.e. a stored master seed).
3
+ * identity (i.e. a stored device key with its proof).
4
4
  *
5
5
  * @param {string} dir Cero data directory.
6
6
  * @param {import('./spec.js').Spec} spec Built spec, the value passed to `cero(dir, spec)`.
7
- * @returns {Promise<boolean>} `true` if a master seed exists on disk.
7
+ * @returns {Promise<boolean>} `true` if a device of an identity exists on disk.
8
8
  */
9
9
  export declare function peek(dir: string, spec: import('./spec.js').Spec): Promise<boolean>;
@@ -31,6 +31,10 @@ export type RefInfo = {
31
31
  */
32
32
  files?: string[];
33
33
  verb?: string;
34
+ /**
35
+ * Whether a write to this ref pushes a notification.
36
+ */
37
+ push?: boolean;
34
38
  };
35
39
  export type SpecMeta = {
36
40
  ns?: string;
@@ -70,6 +74,7 @@ export type Row = import('@cero-base/core/database').Row;
70
74
  * @property {Record<string, string>} [required] The required fields and their types.
71
75
  * @property {string[]} [files] The fields holding file ids.
72
76
  * @property {string} [verb]
77
+ * @property {boolean} [push] Whether a write to this ref pushes a notification.
73
78
  *
74
79
  * @typedef {object} SpecMeta
75
80
  * @property {string} [ns]
@@ -1,12 +1,12 @@
1
1
  /**
2
- * `@react-native-firebase/messaging` as a push backend: pass `messaging()`. Pushes to the app in
2
+ * `@react-native-firebase/messaging` as the messaging service `connect` takes for push: pass `messaging()`. Pushes to the app in
3
3
  * front; the push worklet answers the ones the OS delivers while it is not.
4
4
  *
5
5
  * @param {{ subscribeToTopic: (topic: string) => Promise<void>, unsubscribeFromTopic: (topic: string) => Promise<void>, onMessage: (fn: (message: object) => void) => () => void }} messaging
6
- * @returns {import('../rpc/push.js').Backend}
6
+ * @returns {import('../rpc/push.js').Messaging}
7
7
  */
8
8
  export declare function firebase(messaging: {
9
9
  subscribeToTopic: (topic: string) => Promise<void>;
10
10
  unsubscribeFromTopic: (topic: string) => Promise<void>;
11
11
  onMessage: (fn: (message: object) => void) => () => void;
12
- }): import('../rpc/push.js').Backend;
12
+ }): import('../rpc/push.js').Messaging;
@@ -7,7 +7,7 @@
7
7
  * @param {import('../lib/spec.js').Spec} spec
8
8
  * @param {Uint8Array} payload
9
9
  * @param {{ storageKey?: Uint8Array }} [opts] The key `cero()` encrypts the local store with, if any.
10
- * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
10
+ * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
11
11
  */
12
12
  export declare function receive(dir: string, spec: import('../lib/spec.js').Spec, payload: Uint8Array, { storageKey }?: {
13
13
  storageKey?: Uint8Array;
@@ -16,13 +16,28 @@ export declare function receive(dir: string, spec: import('../lib/spec.js').Spec
16
16
  type: string;
17
17
  name: string | null;
18
18
  newer: boolean;
19
+ ref: string | null;
19
20
  } | null>;
20
21
  /**
21
- * The bare-kit push entry: on each push the OS hands this worklet, verify it and answer with the
22
- * notification to show, or nothing. The request carries `storage`, `payload` (base64) and, when
23
- * the app encrypts its local store, `storageKey` (base64).
22
+ * The bare-kit push entry: on each push the OS hands this worklet, verify it and, for one about
23
+ * new data on a handle this device holds, answer with what `show` returns: the notification,
24
+ * `{ title, body }`, or nothing. The request carries `storage`, `payload` (base64) and, when the
25
+ * app encrypts its local store, `storageKey` (base64).
24
26
  *
25
27
  * @param {import('../lib/spec.js').Spec} spec
28
+ * @param {(push: { id: string, type: string, name: string | null, newer: boolean, ref: string | null }) => { title: string, body: string } | null | undefined | Promise<{ title: string, body: string } | null | undefined>} show What to show for a verified push, as `cero.receive` would give it to the app.
26
29
  * @returns {void}
27
30
  */
28
- export declare function push(spec: import('../lib/spec.js').Spec): void;
31
+ export declare function push(spec: import('../lib/spec.js').Spec, show: (push: {
32
+ id: string;
33
+ type: string;
34
+ name: string | null;
35
+ newer: boolean;
36
+ ref: string | null;
37
+ }) => {
38
+ title: string;
39
+ body: string;
40
+ } | null | undefined | Promise<{
41
+ title: string;
42
+ body: string;
43
+ } | null | undefined>): void;