@cero-base/core 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/core",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -152,26 +152,26 @@
152
152
  },
153
153
  "dependencies": {
154
154
  "@hyperswarm/secret-stream": "^6.9.2",
155
- "autobee": "2.12.1",
155
+ "autobee": "2.13.0",
156
156
  "b4a": "^1.9.0",
157
157
  "bare-crypto": "^1.15.4",
158
158
  "bare-fs": "^4.8.4",
159
159
  "bare-path": "^3.1.2",
160
160
  "bip39-mnemonic": "^2.5.0",
161
- "blind-peering": "^2.12.0",
161
+ "blind-peering": "^2.12.2",
162
162
  "blind-push": "^1.0.2",
163
163
  "compact-encoding": "^3.5.2",
164
164
  "corestore": "^7.16.1",
165
165
  "framed-stream": "^1.0.1",
166
166
  "hrpc": "^4.3.1",
167
167
  "hyperblobs": "^2.13.0",
168
- "hypercore": "^11.39.3",
168
+ "hypercore": "^11.39.6",
169
169
  "hypercore-blob-server": "^1.15.1",
170
170
  "hypercore-crypto": "^3.7.0",
171
171
  "hypercore-id-encoding": "^1.3.0",
172
- "hypercore-storage": "^3.3.1",
173
- "hyperdb": "^6.11.0",
174
- "hyperdht": "^6.34.1",
172
+ "hypercore-storage": "^3.4.0",
173
+ "hyperdb": "^6.11.1",
174
+ "hyperdht": "^6.35.0",
175
175
  "hyperdispatch": "^1.6.0",
176
176
  "hyperschema": "^1.26.2",
177
177
  "hyperswarm": "^4.17.2",
@@ -36,6 +36,10 @@ import { makeChanges } from './changes.js'
36
36
  import { makeDispatcher, BESPOKE } from './dispatch.js'
37
37
  import { keyPair } from '../mailbox/inbox.js'
38
38
 
39
+ // autobee remaps an older store once a recovery peer, a mirror, serves it; unset, it closes the store
40
+ const recovery = []
41
+ Hypercore.setRecoveryPeers(recovery)
42
+
39
43
  /**
40
44
  * @typedef {object} DatabaseOpts
41
45
  * @property {import('corestore')} store Corestore (or compatible) used to materialize the autobee.
@@ -99,6 +103,9 @@ export class Database extends ReadyResource {
99
103
  this.store = opts.store
100
104
  this.identity = opts.identity
101
105
  this.network = opts.network || null
106
+ for (const key of this.network?.mirrors || []) {
107
+ if (!recovery.some((k) => b4a.equals(k, key))) recovery.push(key)
108
+ }
102
109
  this.spec = opts.spec
103
110
  this.meta = opts.spec.meta || { ns: NAMESPACE, refs: {} }
104
111
  this.ns = this.meta.ns || NAMESPACE
@@ -3,6 +3,8 @@ import safetyCatch from 'safety-catch'
3
3
  import crypto from 'hypercore-crypto'
4
4
  import b4a from 'b4a'
5
5
 
6
+ import { CeroError } from '../lib/errors.js'
7
+
6
8
  const NS = b4a.from('cero/mailbox')
7
9
  const READ_TIMEOUT = 30000
8
10
 
@@ -16,17 +18,32 @@ export function keyPair(secret) {
16
18
  return crypto.encryptionKeyPair(crypto.hash([NS, secret]))
17
19
  }
18
20
 
21
+ /**
22
+ * Erase the core a message travelled in. Corestore leaves a core that still has a session open,
23
+ * and that is an error here, not silence.
24
+ *
25
+ * @param {import('corestore')} store
26
+ * @param {Uint8Array} key
27
+ * @returns {Promise<void>}
28
+ */
29
+ export async function erase(store, key) {
30
+ const [gone] = await store.purge(key)
31
+ if (!gone) throw CeroError.CONFLICT(`mailbox: ${b4a.toHex(key)} is still open, not erased`)
32
+ }
33
+
19
34
  /**
20
35
  * Receives at the address a secret owns, direct from the sender or from a mirror. A message
21
36
  * that does not open is dropped; one that opens is kept in `box` until `onmessage` resolves.
37
+ * A core the inbox fetched is erased once dropped, or once handled with `keep` false; one this
38
+ * device held already is left alone.
22
39
  */
23
40
  export class Inbox extends ReadyResource {
24
41
  /**
25
42
  * @param {import('../network/index.js').Network} network
26
43
  * @param {Uint8Array} secret
27
- * @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void }} opts
44
+ * @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void, keep?: boolean }} opts
28
45
  */
29
- constructor(network, secret, { box, onmessage, onerror }) {
46
+ constructor(network, secret, { box, onmessage, onerror, keep = true }) {
30
47
  super()
31
48
  this.network = network
32
49
  this.keyPair = keyPair(secret)
@@ -34,6 +51,7 @@ export class Inbox extends ReadyResource {
34
51
  this.box = box
35
52
  this.onmessage = onmessage
36
53
  this.onerror = onerror
54
+ this.keep = keep
37
55
  /** @private */
38
56
  this._seen = new Set()
39
57
  /** @private */
@@ -77,25 +95,32 @@ export class Inbox extends ReadyResource {
77
95
 
78
96
  /** @private */
79
97
  async _read(id, key) {
80
- const core = this.network.store.get({ key })
98
+ const { store } = this.network
99
+ // a core this device already holds is not the inbox's to erase, whatever is announced here
100
+ const fetched = !(await store.storage.hasCore(crypto.discoveryKey(key)))
101
+ const core = store.get({ key })
81
102
  let message
82
103
  try {
83
104
  message = crypto.decrypt(await core.get(0, { timeout: READ_TIMEOUT }), this.keyPair)
84
105
  } finally {
85
106
  await core.close()
86
107
  }
87
- if (!message) return
88
- const mail = { id, address: this.address, message }
108
+ if (!message) {
109
+ if (fetched) await erase(store, key)
110
+ return
111
+ }
112
+ const mail = { id, address: this.address, message, fetched }
89
113
  await this.box.put(mail)
90
114
  this._handle(mail)
91
115
  }
92
116
 
93
117
  // dropped once handled; one that throws stays for the next time this address is received
94
118
  /** @private */
95
- async _handle({ id, message }) {
119
+ async _handle({ id, message, fetched }) {
96
120
  try {
97
121
  await this.onmessage(message)
98
122
  await this.box.del(id)
123
+ if (!this.keep && fetched) await erase(this.network.store, b4a.from(id, 'hex'))
99
124
  } catch (err) {
100
125
  this.onerror(err)
101
126
  }
@@ -3,16 +3,19 @@ import safetyCatch from 'safety-catch'
3
3
  import crypto from 'hypercore-crypto'
4
4
  import b4a from 'b4a'
5
5
 
6
- import { Inbox, keyPair } from './inbox.js'
6
+ import { Inbox, keyPair, erase } from './inbox.js'
7
7
  import { Post } from './post.js'
8
8
  import { CeroError } from '../lib/errors.js'
9
9
 
10
10
  /**
11
11
  * @typedef {object} Mail
12
12
  * @property {string} id Unique per message, hex.
13
+ * @property {string} [name] The core it travels in, so a restart sends it on the same core.
13
14
  * @property {Uint8Array} address Where it goes, or where it arrived.
14
15
  * @property {Uint8Array} message
15
16
  * @property {Uint8Array[]} [mirrors] Where a sent message waits for an owner who is offline.
17
+ * @property {boolean} [keep] False erases the sender's copy once someone read it.
18
+ * @property {boolean} [fetched] The inbox fetched the core it arrived in, so it may erase it.
16
19
  *
17
20
  * @typedef {object} Box Where mail is kept; memory unless given a persistent one.
18
21
  * @property {() => Promise<Mail[]>} list
@@ -81,13 +84,15 @@ export class Mailbox extends ReadyResource {
81
84
  *
82
85
  * @param {Uint8Array} secret
83
86
  * @param {(message: Uint8Array) => unknown} onmessage
87
+ * @param {{ keep?: boolean }} [opts] False erases each message from this device once handled.
84
88
  * @returns {{ close: () => Promise<void> }}
85
89
  */
86
- receive(secret, onmessage) {
90
+ receive(secret, onmessage, { keep = true } = {}) {
87
91
  const inbox = new Inbox(this.network, secret, {
88
92
  box: this.inbox,
89
93
  onmessage,
90
- onerror: this.onerror
94
+ onerror: this.onerror,
95
+ keep
91
96
  })
92
97
  this._track(inbox)
93
98
  inbox.ready().catch(this.onerror)
@@ -100,21 +105,24 @@ export class Mailbox extends ReadyResource {
100
105
  *
101
106
  * @param {Uint8Array} address
102
107
  * @param {Uint8Array} message
103
- * @param {{ mirrors?: Uint8Array[] }} [opts] Where it waits for an owner who is offline; the network's by default.
108
+ * @param {{ mirrors?: Uint8Array[], keep?: boolean }} [opts] `mirrors`: where it waits for an owner who is offline, the network's by default. `keep`: false erases it from this device once someone read it.
104
109
  * @returns {Promise<void>}
105
110
  */
106
- async send(address, message, { mirrors = this.network.mirrors } = {}) {
111
+ async send(address, message, { mirrors = this.network.mirrors, keep = true } = {}) {
107
112
  if (!this.opened) await this.ready()
108
- const mail = { id: b4a.toHex(crypto.randomBytes(16)), address, message, mirrors }
113
+ const name = b4a.toHex(crypto.randomBytes(32))
114
+ const mail = { id: b4a.toHex(crypto.randomBytes(16)), name, address, message, mirrors, keep }
109
115
  await this.outbox.put(mail)
110
116
  this._deliver(mail)
111
117
  }
112
118
 
113
119
  /** @private */
114
- _deliver({ id, address, message, mirrors }) {
115
- const post = new Post(this.network, address, message, { mirrors })
120
+ _deliver({ id, name, address, message, mirrors, keep = true }) {
121
+ const post = new Post(this.network, address, message, { mirrors, name })
116
122
  this._track(post)
117
- deliver(post, () => this.outbox.del(id)).catch(safetyCatch)
123
+ deliver(post, () => this.outbox.del(id))
124
+ .then(() => keep || erase(this.network.store, post.key).catch(this.onerror))
125
+ .catch(safetyCatch)
118
126
  }
119
127
 
120
128
  /** @private */
@@ -7,21 +7,23 @@ import b4a from 'b4a'
7
7
  * One sealed message on its way to an address: written to a fresh core, announced to peers
8
8
  * receiving at the address, and deposited on `mirrors` for an owner who is offline.
9
9
  * `delivered` resolves on the first read, by the owner or by a mirror holding it for them.
10
- * Given a `core` instead, it announces that core as it is.
10
+ * Given a `core` instead, it announces that core as it is; given a `name`, the core is the
11
+ * store's core of that name, and a message it already holds is not written twice.
11
12
  */
12
13
  export class Post extends ReadyResource {
13
14
  /**
14
15
  * @param {import('../network/index.js').Network} network
15
16
  * @param {Uint8Array} address
16
17
  * @param {Uint8Array | null} message
17
- * @param {{ mirrors?: Uint8Array[], core?: import('hypercore') }} [opts]
18
+ * @param {{ mirrors?: Uint8Array[], core?: import('hypercore'), name?: string }} [opts]
18
19
  */
19
- constructor(network, address, message, { mirrors = [], core = null } = {}) {
20
+ constructor(network, address, message, { mirrors = [], core = null, name } = {}) {
20
21
  super()
21
22
  this.network = network
22
23
  this.address = address
23
24
  this.message = message
24
25
  this.mirrors = mirrors
26
+ this.name = name || b4a.toHex(crypto.randomBytes(32))
25
27
  /** @type {Promise<void>} */
26
28
  this.delivered = new Promise((resolve) => {
27
29
  /** @private */
@@ -38,11 +40,11 @@ export class Post extends ReadyResource {
38
40
  /** @private */
39
41
  async _open() {
40
42
  const { network, address } = this
41
- const core = this._core || network.store.get({ name: b4a.toHex(crypto.randomBytes(32)) })
43
+ const core = this._core || network.store.get({ name: this.name })
42
44
  this._core = core
43
45
  await core.ready()
44
46
  core.once('upload', () => this._ondelivered())
45
- if (this.message) await core.append(crypto.encrypt(this.message, address))
47
+ if (this.message && core.length === 0) await core.append(crypto.encrypt(this.message, address))
46
48
 
47
49
  const wakeup = [{ key: core.key, length: core.length }]
48
50
  const session = network.wakeup.session(address, {
@@ -59,6 +61,11 @@ export class Post extends ReadyResource {
59
61
  }
60
62
  }
61
63
 
64
+ /** The key of the core it travels in, once opened. */
65
+ get key() {
66
+ return this._core?.key ?? null
67
+ }
68
+
62
69
  /** @private */
63
70
  async _close() {
64
71
  this._session?.destroy()
@@ -9,9 +9,20 @@ export declare function keyPair(secret: Uint8Array): {
9
9
  publicKey: Uint8Array;
10
10
  secretKey: Uint8Array;
11
11
  };
12
+ /**
13
+ * Erase the core a message travelled in. Corestore leaves a core that still has a session open,
14
+ * and that is an error here, not silence.
15
+ *
16
+ * @param {import('corestore')} store
17
+ * @param {Uint8Array} key
18
+ * @returns {Promise<void>}
19
+ */
20
+ export declare function erase(store: import('corestore'), key: Uint8Array): Promise<void>;
12
21
  /**
13
22
  * Receives at the address a secret owns, direct from the sender or from a mirror. A message
14
23
  * that does not open is dropped; one that opens is kept in `box` until `onmessage` resolves.
24
+ * A core the inbox fetched is erased once dropped, or once handled with `keep` false; one this
25
+ * device held already is left alone.
15
26
  */
16
27
  export declare class Inbox extends ReadyResource {
17
28
  network: import("../index.js").Network;
@@ -23,6 +34,7 @@ export declare class Inbox extends ReadyResource {
23
34
  box: import("./index.js").Box;
24
35
  onmessage: (message: Uint8Array) => unknown;
25
36
  onerror: (err: Error) => void;
37
+ keep: boolean;
26
38
  /** @private */
27
39
  _seen;
28
40
  /** @private */
@@ -32,12 +44,13 @@ export declare class Inbox extends ReadyResource {
32
44
  /**
33
45
  * @param {import('../network/index.js').Network} network
34
46
  * @param {Uint8Array} secret
35
- * @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void }} opts
47
+ * @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void, keep?: boolean }} opts
36
48
  */
37
- constructor(network: import('../network/index.js').Network, secret: Uint8Array, { box, onmessage, onerror }: {
49
+ constructor(network: import('../network/index.js').Network, secret: Uint8Array, { box, onmessage, onerror, keep }: {
38
50
  box: import('./index.js').Box;
39
51
  onmessage: (message: Uint8Array) => unknown;
40
52
  onerror: (err: Error) => void;
53
+ keep?: boolean;
41
54
  });
42
55
  /** @private */
43
56
  private _open;
@@ -4,6 +4,10 @@ export type Mail = {
4
4
  * Unique per message, hex.
5
5
  */
6
6
  id: string;
7
+ /**
8
+ * The core it travels in, so a restart sends it on the same core.
9
+ */
10
+ name?: string;
7
11
  /**
8
12
  * Where it goes, or where it arrived.
9
13
  */
@@ -13,6 +17,14 @@ export type Mail = {
13
17
  * Where a sent message waits for an owner who is offline.
14
18
  */
15
19
  mirrors?: Uint8Array[];
20
+ /**
21
+ * False erases the sender's copy once someone read it.
22
+ */
23
+ keep?: boolean;
24
+ /**
25
+ * The inbox fetched the core it arrived in, so it may erase it.
26
+ */
27
+ fetched?: boolean;
16
28
  };
17
29
  export type Box = {
18
30
  list: () => Promise<Mail[]>;
@@ -36,9 +48,12 @@ export type MailboxOpts = {
36
48
  /**
37
49
  * @typedef {object} Mail
38
50
  * @property {string} id Unique per message, hex.
51
+ * @property {string} [name] The core it travels in, so a restart sends it on the same core.
39
52
  * @property {Uint8Array} address Where it goes, or where it arrived.
40
53
  * @property {Uint8Array} message
41
54
  * @property {Uint8Array[]} [mirrors] Where a sent message waits for an owner who is offline.
55
+ * @property {boolean} [keep] False erases the sender's copy once someone read it.
56
+ * @property {boolean} [fetched] The inbox fetched the core it arrived in, so it may erase it.
42
57
  *
43
58
  * @typedef {object} Box Where mail is kept; memory unless given a persistent one.
44
59
  * @property {() => Promise<Mail[]>} list
@@ -87,9 +102,12 @@ export declare class Mailbox extends ReadyResource {
87
102
  *
88
103
  * @param {Uint8Array} secret
89
104
  * @param {(message: Uint8Array) => unknown} onmessage
105
+ * @param {{ keep?: boolean }} [opts] False erases each message from this device once handled.
90
106
  * @returns {{ close: () => Promise<void> }}
91
107
  */
92
- receive(secret: Uint8Array, onmessage: (message: Uint8Array) => unknown): {
108
+ receive(secret: Uint8Array, onmessage: (message: Uint8Array) => unknown, { keep }?: {
109
+ keep?: boolean;
110
+ }): {
93
111
  close: () => Promise<void>;
94
112
  };
95
113
  /**
@@ -98,11 +116,12 @@ export declare class Mailbox extends ReadyResource {
98
116
  *
99
117
  * @param {Uint8Array} address
100
118
  * @param {Uint8Array} message
101
- * @param {{ mirrors?: Uint8Array[] }} [opts] Where it waits for an owner who is offline; the network's by default.
119
+ * @param {{ mirrors?: Uint8Array[], keep?: boolean }} [opts] `mirrors`: where it waits for an owner who is offline, the network's by default. `keep`: false erases it from this device once someone read it.
102
120
  * @returns {Promise<void>}
103
121
  */
104
- send(address: Uint8Array, message: Uint8Array, { mirrors }?: {
122
+ send(address: Uint8Array, message: Uint8Array, { mirrors, keep }?: {
105
123
  mirrors?: Uint8Array[];
124
+ keep?: boolean;
106
125
  }): Promise<void>;
107
126
  /** @private */
108
127
  private _deliver;
@@ -3,13 +3,15 @@ import ReadyResource from 'ready-resource';
3
3
  * One sealed message on its way to an address: written to a fresh core, announced to peers
4
4
  * receiving at the address, and deposited on `mirrors` for an owner who is offline.
5
5
  * `delivered` resolves on the first read, by the owner or by a mirror holding it for them.
6
- * Given a `core` instead, it announces that core as it is.
6
+ * Given a `core` instead, it announces that core as it is; given a `name`, the core is the
7
+ * store's core of that name, and a message it already holds is not written twice.
7
8
  */
8
9
  export declare class Post extends ReadyResource {
9
10
  network: import("../index.js").Network;
10
11
  address: Uint8Array<ArrayBufferLike>;
11
12
  message: Uint8Array<ArrayBufferLike>;
12
13
  mirrors: Uint8Array<ArrayBufferLike>[];
14
+ name: string;
13
15
  /** @type {Promise<void>} */
14
16
  delivered: Promise<void>;
15
17
  /** @private */
@@ -24,14 +26,17 @@ export declare class Post extends ReadyResource {
24
26
  * @param {import('../network/index.js').Network} network
25
27
  * @param {Uint8Array} address
26
28
  * @param {Uint8Array | null} message
27
- * @param {{ mirrors?: Uint8Array[], core?: import('hypercore') }} [opts]
29
+ * @param {{ mirrors?: Uint8Array[], core?: import('hypercore'), name?: string }} [opts]
28
30
  */
29
- constructor(network: import('../network/index.js').Network, address: Uint8Array, message: Uint8Array | null, { mirrors, core }?: {
31
+ constructor(network: import('../network/index.js').Network, address: Uint8Array, message: Uint8Array | null, { mirrors, core, name }?: {
30
32
  mirrors?: Uint8Array[];
31
33
  core?: import('hypercore');
34
+ name?: string;
32
35
  });
33
36
  /** @private */
34
37
  private _open;
38
+ /** The key of the core it travels in, once opened. */
39
+ get key(): any;
35
40
  /** @private */
36
41
  private _close;
37
42
  }