@cero-base/cero 2.8.0 → 2.9.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/cero",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "The ideal p2p API — everything is a handle, handles contain refs, refs contain rows.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -113,21 +113,21 @@
113
113
  "test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
114
114
  },
115
115
  "dependencies": {
116
- "@cero-base/core": "^2.8.0",
116
+ "@cero-base/core": "^2.9.0",
117
117
  "b4a": "^1.9.0",
118
118
  "bare-abort-controller": "^1.1.2",
119
119
  "bare-crypto": "^1.15.4",
120
120
  "bare-fs": "^4.8.4",
121
121
  "bare-path": "^3.1.2",
122
- "ble-swarm": "^2.3.4",
122
+ "ble-swarm": "^2.4.0",
123
123
  "compact-encoding": "^3.5.2",
124
124
  "corestore": "^7.16.1",
125
125
  "hrpc": "^4.3.1",
126
- "hypercore": "^11.39.3",
126
+ "hypercore": "^11.39.6",
127
127
  "hypercore-crypto": "^3.7.0",
128
128
  "hypercore-id-encoding": "^1.3.0",
129
- "hypercore-storage": "^3.3.1",
130
- "hyperdb": "^6.11.0",
129
+ "hypercore-storage": "^3.4.0",
130
+ "hyperdb": "^6.11.1",
131
131
  "hyperdispatch": "^1.6.0",
132
132
  "hyperschema": "^1.26.2",
133
133
  "protomux": "^3.13.0",
@@ -217,7 +217,12 @@ export const local = {
217
217
  id: required(string),
218
218
  address: required(bytes),
219
219
  message: required(bytes),
220
- mirrors: bytes
220
+ mirrors: bytes,
221
+ // sent: the core it travels in, and whether it goes once someone read it
222
+ name: string,
223
+ erase: bool,
224
+ // received: the inbox fetched the core, so it may erase it once handled
225
+ fetched: bool
221
226
  },
222
227
  environment: {
223
228
  channel: required(string)
@@ -14,6 +14,7 @@ import { Pairing } from '@cero-base/core/pairing'
14
14
  import { read } from '../push/index.js'
15
15
  import { Invite } from '@cero-base/core/invite'
16
16
  import { Mailbox } from '@cero-base/core/mailbox'
17
+ import { seal, unseal, decodeAddress } from '../lib/mail.js'
17
18
  import hid from 'hypercore-id-encoding'
18
19
  import { admission, onAbort, filter } from '@cero-base/core/utils'
19
20
  import { CeroError } from '@cero-base/core/errors'
@@ -599,6 +600,23 @@ export class Handle extends ReadyResource {
599
600
  return this.identity.toPhrase()
600
601
  }
601
602
 
603
+ /** @private */
604
+ async _send(address, value, { keep = true } = {}) {
605
+ const to = decodeAddress(address)
606
+ await this.root.mailbox.send(to, seal(this.identity, to, value), { keep })
607
+ }
608
+
609
+ /** @private */
610
+ _inbox(secret, fn, { keep = true } = {}) {
611
+ const to = Mailbox.getAddress(secret)
612
+ const onmessage = async (bytes) => {
613
+ const mail = unseal(to, bytes)
614
+ if (mail) await fn(mail.value, { id: mail.id, from: mail.from, device: mail.device })
615
+ }
616
+ const inbox = this.root.mailbox.receive(secret, onmessage, { keep })
617
+ return () => inbox.close()
618
+ }
619
+
602
620
  /**
603
621
  * @param {Uint8Array} coreKey
604
622
  * @param {object} info
package/src/index.js CHANGED
@@ -20,12 +20,14 @@ import { Local, box } from './local/index.js'
20
20
  import { Bluetooth } from './lib/bluetooth.js'
21
21
  import * as verbs from './lib/operators.js'
22
22
  import { peek } from './lib/peek.js'
23
+ import { address } from './lib/mail.js'
23
24
  import { t, schema } from './lib/spec.js'
24
25
  import { FLUSH, TIMEOUT } from './lib/constants.js'
25
26
 
26
27
  export { Handle, Ref, Local }
27
28
  export * from './lib/operators.js'
28
29
  export { peek } from './lib/peek.js'
30
+ export { address } from './lib/mail.js'
29
31
  export { t, schema } from './lib/spec.js'
30
32
 
31
33
  /**
@@ -272,7 +274,7 @@ export async function link(me, invite, { onrequest } = {}) {
272
274
 
273
275
  // the facade: cero.put and import { put } are the same function
274
276
  /** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore, link: typeof link }} */
275
- export const cero = Object.assign(start, verbs, { t, schema, peek, restore, link })
277
+ export const cero = Object.assign(start, verbs, { t, schema, peek, restore, link, address })
276
278
 
277
279
  function pointerManifest(store, identity) {
278
280
  return { version: store.manifestVersion, signers: [{ publicKey: identity.publicKey }] }
@@ -0,0 +1,76 @@
1
+ import c from 'compact-encoding'
2
+ import b4a from 'b4a'
3
+ import hid from 'hypercore-id-encoding'
4
+ import { Identity } from '@cero-base/core/identity'
5
+ import { Mailbox } from '@cero-base/core/mailbox'
6
+ import { CeroError } from '@cero-base/core/errors'
7
+ import crypto from 'hypercore-crypto'
8
+
9
+ const NS = b4a.from('cero/mail')
10
+
11
+ /**
12
+ * The address a secret owns: share it, anyone can send to it, only the secret opens.
13
+ *
14
+ * @param {Uint8Array} secret
15
+ * @returns {string}
16
+ */
17
+ export function address(secret) {
18
+ return hid.encode(Mailbox.getAddress(secret))
19
+ }
20
+
21
+ /**
22
+ * @param {string} address
23
+ * @returns {Uint8Array}
24
+ */
25
+ export function decodeAddress(address) {
26
+ try {
27
+ return hid.decode(address)
28
+ } catch {
29
+ throw CeroError.INVALID(`'${address}' is not an address`)
30
+ }
31
+ }
32
+
33
+ /**
34
+ * A value signed by `identity` for the address `to`, so it cannot be redirected elsewhere, with
35
+ * an id of its own under the signature, so a message that arrives twice is known as one.
36
+ *
37
+ * @param {import('@cero-base/core/identity').Identity} identity
38
+ * @param {Uint8Array} to
39
+ * @param {unknown} value
40
+ * @returns {Uint8Array}
41
+ */
42
+ export function seal(identity, to, value) {
43
+ const id = crypto.randomBytes(16)
44
+ const data = c.encode(c.any, value)
45
+ return c.encode(c.any, { id, data, proof: identity.sign(signed(to, id, data)) })
46
+ }
47
+
48
+ /**
49
+ * The value, its id and who sent it, or null for anything unsigned, tampered or signed for
50
+ * another address.
51
+ *
52
+ * @param {Uint8Array} to
53
+ * @param {Uint8Array} bytes
54
+ * @returns {{ value: unknown, id: string, from: string, device: string } | null}
55
+ */
56
+ export function unseal(to, bytes) {
57
+ try {
58
+ const { id, data, proof } = c.decode(c.any, bytes)
59
+ // fixed sizes ahead of the data, so the signed bytes split one way only
60
+ if (id?.byteLength !== 16) return null
61
+ const who = Identity.verify(proof, signed(to, id, data))
62
+ if (!who) return null
63
+ return {
64
+ value: c.decode(c.any, data),
65
+ id: b4a.toHex(id),
66
+ from: hid.encode(who.identity),
67
+ device: hid.encode(who.device)
68
+ }
69
+ } catch {
70
+ return null
71
+ }
72
+ }
73
+
74
+ function signed(to, id, data) {
75
+ return b4a.concat([NS, to, id, data])
76
+ }
@@ -503,6 +503,37 @@ export async function receive(me, payload) {
503
503
  return me._receive(payload)
504
504
  }
505
505
 
506
+ /**
507
+ * Send a value to an address, signed by this identity. Resolves once the outbox keeps it; it is
508
+ * delivered in the background, from a mirror too when the owner is offline. `keep: false` erases
509
+ * this device's copy once someone read it.
510
+ *
511
+ * @param {Context} me
512
+ * @param {string} address As `cero.address(secret)` gives it.
513
+ * @param {unknown} data
514
+ * @param {{ keep?: boolean }} [opts]
515
+ * @returns {Promise<void>}
516
+ */
517
+ export function send(me, address, data, opts) {
518
+ return me._send(address, data, opts)
519
+ }
520
+
521
+ /**
522
+ * Receive what is sent to the address `secret` owns, with its id and who sent it. A message stays
523
+ * until `fn` resolves, and one it did not finish comes again on the next start, under the same id;
524
+ * anything unsigned or signed for another address is dropped. `keep: false` erases each message
525
+ * from this device once handled. Where cero runs only, not over RPC.
526
+ *
527
+ * @param {Context} me
528
+ * @param {Uint8Array} secret
529
+ * @param {(data: unknown, mail: { id: string, from: string, device: string }) => unknown} fn
530
+ * @param {{ keep?: boolean }} [opts]
531
+ * @returns {() => Promise<void>} Stops receiving.
532
+ */
533
+ export function inbox(me, secret, fn, opts) {
534
+ return me._inbox(secret, fn, opts)
535
+ }
536
+
506
537
  /**
507
538
  * Reveal the recovery phrase.
508
539
  *
@@ -79,13 +79,18 @@ export class Local extends ReadyResource {
79
79
  export function box(store, name) {
80
80
  return {
81
81
  list: async () => (await store.get(name)).data.map(unpack),
82
- put: (mail) => store.put(name, { ...mail, mirrors: b4a.concat(mail.mirrors || []) }),
82
+ put: (mail) => store.put(name, pack(mail)),
83
83
  del: (id) => store.del(name, id)
84
84
  }
85
85
  }
86
86
 
87
- function unpack({ id, address, message, mirrors }) {
87
+ // the row says erase, so one without it, or from before it, is kept
88
+ function pack({ mirrors = [], keep = true, ...mail }) {
89
+ return { ...mail, mirrors: b4a.concat(mirrors), erase: !keep }
90
+ }
91
+
92
+ function unpack({ id, address, message, mirrors, name, erase, fetched }) {
88
93
  const keys = []
89
94
  for (let i = 0; i < (mirrors?.byteLength || 0); i += 32) keys.push(mirrors.subarray(i, i + 32))
90
- return { id, address, message, mirrors: keys }
95
+ return { id, address, message, mirrors: keys, name, keep: !erase, fetched }
91
96
  }
package/src/rpc/client.js CHANGED
@@ -400,6 +400,16 @@ const operators = {
400
400
  return this._verb('nearby', on, opts)
401
401
  },
402
402
 
403
+ /** @private */
404
+ _send(address, data, opts) {
405
+ return this._verb('send', address, data, opts)
406
+ },
407
+
408
+ /** @private */
409
+ _inbox() {
410
+ throw CeroError.INVALID('inbox runs where cero runs, not over RPC')
411
+ },
412
+
403
413
  /** @private */
404
414
  async _verb(verb, ...args) {
405
415
  const { data } = await this.rpc.verb({
package/src/rpc/server.js CHANGED
@@ -25,7 +25,8 @@ import {
25
25
  deactivate,
26
26
  phrase,
27
27
  nearby,
28
- receive
28
+ receive,
29
+ send
29
30
  } from '../lib/operators.js'
30
31
 
31
32
  // what a UI reaches by name: these operators, never close or a handle's privates
@@ -43,7 +44,8 @@ const VERBS = new Map(
43
44
  deactivate,
44
45
  phrase,
45
46
  nearby,
46
- receive
47
+ receive,
48
+ send
47
49
  })
48
50
  )
49
51
 
@@ -180,6 +180,9 @@ export declare const local: {
180
180
  address: import("@cero-base/core").Prim;
181
181
  message: import("@cero-base/core").Prim;
182
182
  mirrors: import("@cero-base/core").Prim;
183
+ name: import("@cero-base/core").Prim;
184
+ erase: import("@cero-base/core").Prim;
185
+ fetched: import("@cero-base/core").Prim;
183
186
  };
184
187
  environment: {
185
188
  channel: import("@cero-base/core").Prim;
@@ -57,6 +57,16 @@ export declare const cero: {
57
57
  ref: string | null;
58
58
  row: object | null;
59
59
  } | null>;
60
+ send(me: operators.Context, address: string, data: unknown, opts?: {
61
+ keep?: boolean;
62
+ }): Promise<void>;
63
+ inbox(me: operators.Context, secret: Uint8Array, fn: (data: unknown, mail: {
64
+ id: string;
65
+ from: string;
66
+ device: string;
67
+ }) => unknown, opts?: {
68
+ keep?: boolean;
69
+ }): () => Promise<void>;
60
70
  phrase(me: operators.Context): Promise<string>;
61
71
  nearby(ctx: operators.Context, on: boolean, opts?: {
62
72
  topic?: string;
@@ -317,6 +317,10 @@ export declare class Handle extends ReadyResource {
317
317
  private _receive;
318
318
  /** @private */
319
319
  private _phrase;
320
+ /** @private */
321
+ private _send;
322
+ /** @private */
323
+ private _inbox;
320
324
  /**
321
325
  * @param {Uint8Array} coreKey
322
326
  * @param {object} info
package/types/index.d.ts CHANGED
@@ -6,6 +6,7 @@ import { t, schema } from './lib/spec.js';
6
6
  export { Handle, Ref, Local };
7
7
  export * from './lib/operators.js';
8
8
  export { peek } from './lib/peek.js';
9
+ export { address } from './lib/mail.js';
9
10
  export { t, schema } from './lib/spec.js';
10
11
  export type Context = import('./handle/index.js').Context;
11
12
  export type CeroOpts = {
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The address a secret owns: share it, anyone can send to it, only the secret opens.
3
+ *
4
+ * @param {Uint8Array} secret
5
+ * @returns {string}
6
+ */
7
+ export declare function address(secret: Uint8Array): string;
8
+ /**
9
+ * @param {string} address
10
+ * @returns {Uint8Array}
11
+ */
12
+ export declare function decodeAddress(address: string): Uint8Array;
13
+ /**
14
+ * A value signed by `identity` for the address `to`, so it cannot be redirected elsewhere, with
15
+ * an id of its own under the signature, so a message that arrives twice is known as one.
16
+ *
17
+ * @param {import('@cero-base/core/identity').Identity} identity
18
+ * @param {Uint8Array} to
19
+ * @param {unknown} value
20
+ * @returns {Uint8Array}
21
+ */
22
+ export declare function seal(identity: import('@cero-base/core/identity').Identity, to: Uint8Array, value: unknown): Uint8Array;
23
+ /**
24
+ * The value, its id and who sent it, or null for anything unsigned, tampered or signed for
25
+ * another address.
26
+ *
27
+ * @param {Uint8Array} to
28
+ * @param {Uint8Array} bytes
29
+ * @returns {{ value: unknown, id: string, from: string, device: string } | null}
30
+ */
31
+ export declare function unseal(to: Uint8Array, bytes: Uint8Array): {
32
+ value: unknown;
33
+ id: string;
34
+ from: string;
35
+ device: string;
36
+ } | null;
@@ -245,6 +245,39 @@ export declare function receive(me: Context, payload: Uint8Array): Promise<{
245
245
  ref: string | null;
246
246
  row: object | null;
247
247
  } | null>;
248
+ /**
249
+ * Send a value to an address, signed by this identity. Resolves once the outbox keeps it; it is
250
+ * delivered in the background, from a mirror too when the owner is offline. `keep: false` erases
251
+ * this device's copy once someone read it.
252
+ *
253
+ * @param {Context} me
254
+ * @param {string} address As `cero.address(secret)` gives it.
255
+ * @param {unknown} data
256
+ * @param {{ keep?: boolean }} [opts]
257
+ * @returns {Promise<void>}
258
+ */
259
+ export declare function send(me: Context, address: string, data: unknown, opts?: {
260
+ keep?: boolean;
261
+ }): Promise<void>;
262
+ /**
263
+ * Receive what is sent to the address `secret` owns, with its id and who sent it. A message stays
264
+ * until `fn` resolves, and one it did not finish comes again on the next start, under the same id;
265
+ * anything unsigned or signed for another address is dropped. `keep: false` erases each message
266
+ * from this device once handled. Where cero runs only, not over RPC.
267
+ *
268
+ * @param {Context} me
269
+ * @param {Uint8Array} secret
270
+ * @param {(data: unknown, mail: { id: string, from: string, device: string }) => unknown} fn
271
+ * @param {{ keep?: boolean }} [opts]
272
+ * @returns {() => Promise<void>} Stops receiving.
273
+ */
274
+ export declare function inbox(me: Context, secret: Uint8Array, fn: (data: unknown, mail: {
275
+ id: string;
276
+ from: string;
277
+ device: string;
278
+ }) => unknown, opts?: {
279
+ keep?: boolean;
280
+ }): () => Promise<void>;
248
281
  /**
249
282
  * Reveal the recovery phrase.
250
283
  *
@@ -49,6 +49,16 @@ declare const remote: {
49
49
  ref: string | null;
50
50
  row: object | null;
51
51
  } | null>;
52
+ send(me: verbs.Context, address: string, data: unknown, opts?: {
53
+ keep?: boolean;
54
+ }): Promise<void>;
55
+ inbox(me: verbs.Context, secret: Uint8Array, fn: (data: unknown, mail: {
56
+ id: string;
57
+ from: string;
58
+ device: string;
59
+ }) => unknown, opts?: {
60
+ keep?: boolean;
61
+ }): () => Promise<void>;
52
62
  phrase(me: verbs.Context): Promise<string>;
53
63
  nearby(ctx: verbs.Context, on: boolean, opts?: {
54
64
  topic?: string;