@cero-base/cero 2.7.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 +7 -7
- package/src/build/index.js +3 -3
- package/src/build/internal.js +2 -0
- package/src/build/schemas.js +15 -3
- package/src/handle/index.js +48 -9
- package/src/index.js +3 -1
- package/src/lib/mail.js +76 -0
- package/src/lib/operators.js +32 -1
- package/src/local/index.js +8 -3
- package/src/push/index.js +43 -9
- package/src/rpc/client.js +10 -0
- package/src/rpc/server.js +4 -2
- package/types/build/internal.d.ts +3 -0
- package/types/build/schemas.d.ts +8 -0
- package/types/extensions/index.d.ts +11 -0
- package/types/handle/index.d.ts +4 -0
- package/types/index.d.ts +1 -0
- package/types/lib/mail.d.ts +36 -0
- package/types/lib/operators.d.ts +35 -1
- package/types/push/index.d.ts +32 -4
- package/types/rpc/client.d.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/cero",
|
|
3
|
-
"version": "2.
|
|
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,
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
}
|
|
104
104
|
},
|
|
105
105
|
"scripts": {
|
|
106
|
-
"build:test": "rm -rf test/fixtures/spec && node test/fixtures/build.js",
|
|
106
|
+
"build:test": "rm -rf test/fixtures/spec && node test/fixtures/build.js && node test/fixtures/compat.js",
|
|
107
107
|
"build:types": "rm -rf types && tsc -p .",
|
|
108
108
|
"pretest": "npm run build:test",
|
|
109
109
|
"prepublishOnly": "npm run build:types",
|
|
@@ -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.
|
|
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.
|
|
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.
|
|
126
|
+
"hypercore": "^11.39.6",
|
|
127
127
|
"hypercore-crypto": "^3.7.0",
|
|
128
128
|
"hypercore-id-encoding": "^1.3.0",
|
|
129
|
-
"hypercore-storage": "^3.
|
|
130
|
-
"hyperdb": "^6.11.
|
|
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",
|
package/src/build/index.js
CHANGED
|
@@ -239,12 +239,12 @@ function register(name, node, ctx) {
|
|
|
239
239
|
}
|
|
240
240
|
|
|
241
241
|
// one contract for the app, kept in contract.json: it rises once per build that changes what is
|
|
242
|
-
// stored, at the root or in any handle type,
|
|
243
|
-
// its builders' versions, so a shipped app's number never goes down
|
|
242
|
+
// stored, at the root or in any handle type, or what a write means to Cero, never for RPC alone.
|
|
243
|
+
// A spec without one starts from its builders' versions, so a shipped app's number never goes down
|
|
244
244
|
async function contract(specDir, dirs) {
|
|
245
245
|
const file = join(specDir, 'contract.json')
|
|
246
246
|
const shapes = await Promise.all(dirs.map((dir) => shapeOf(join(specDir, dir))))
|
|
247
|
-
const json = JSON.stringify(shapes.map((s) => s.stored))
|
|
247
|
+
const json = JSON.stringify([internal.contract.version, ...shapes.map((s) => s.stored)])
|
|
248
248
|
const shape = b4a.toString(crypto.hash(b4a.from(json)), 'hex')
|
|
249
249
|
const kept = await fs.readFile(file, 'utf-8').then(JSON.parse, () => null)
|
|
250
250
|
const version = kept
|
package/src/build/internal.js
CHANGED
|
@@ -3,6 +3,8 @@ import { COUNTERS, EPOCHS, REMOVALS, REFUSALS, SPENT } from '../lib/constants.js
|
|
|
3
3
|
import * as schemas from './schemas.js'
|
|
4
4
|
|
|
5
5
|
// internal refs by scope; kind defaults to collection
|
|
6
|
+
export const { contract } = schemas
|
|
7
|
+
|
|
6
8
|
export const defs = {
|
|
7
9
|
main: {
|
|
8
10
|
members: { type: 'member' },
|
package/src/build/schemas.js
CHANGED
|
@@ -217,21 +217,33 @@ 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)
|
|
224
229
|
},
|
|
225
|
-
// the handles rows a cold push process can read without the root: topic → handle
|
|
230
|
+
// the handles rows a cold push process can read without the root: topic → handle, its keys and
|
|
231
|
+
// the epochs it learned, so a push's block opens there too
|
|
226
232
|
subscription: {
|
|
227
233
|
id: required(string),
|
|
228
234
|
handle: required(string),
|
|
229
235
|
type: required(string),
|
|
230
236
|
key: required(bytes),
|
|
231
|
-
name: string
|
|
237
|
+
name: string,
|
|
238
|
+
encryptionKey: bytes,
|
|
239
|
+
epochs: bytes
|
|
232
240
|
}
|
|
233
241
|
}
|
|
234
242
|
|
|
243
|
+
// what a write means to apply. A release that changes it without changing what is stored bumps
|
|
244
|
+
// this, so an app's next build raises its contract and devices on the old release skip the new writes
|
|
245
|
+
export const contract = { version: 1 }
|
|
246
|
+
|
|
235
247
|
export const rpc = {
|
|
236
248
|
'req-empty': {
|
|
237
249
|
ok: bool
|
package/src/handle/index.js
CHANGED
|
@@ -11,9 +11,10 @@ import { Identity } from '@cero-base/core/identity'
|
|
|
11
11
|
import { Database } from '@cero-base/core/database'
|
|
12
12
|
import { blobEpochKey, epochEntries } from '@cero-base/core/database/encryption'
|
|
13
13
|
import { Pairing } from '@cero-base/core/pairing'
|
|
14
|
-
import {
|
|
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'
|
|
@@ -366,7 +367,12 @@ export class Handle extends ReadyResource {
|
|
|
366
367
|
// device checks its chain against
|
|
367
368
|
/** @private */
|
|
368
369
|
async _invite(opts = {}) {
|
|
369
|
-
if (this.parent)
|
|
370
|
+
if (this.parent) {
|
|
371
|
+
const invite = await this._pair.invite(opts)
|
|
372
|
+
// a close right after the invite must not drop the row that reopens this handle at boot
|
|
373
|
+
await this._saved
|
|
374
|
+
return invite
|
|
375
|
+
}
|
|
370
376
|
const { ttl = '2h' } = opts
|
|
371
377
|
return this._pair.invite({ role: 'owner', confirm: true, ttl, data: this.identity.publicKey })
|
|
372
378
|
}
|
|
@@ -566,7 +572,15 @@ export class Handle extends ReadyResource {
|
|
|
566
572
|
if (!wanted.has(row.id)) await this.local.store.del('topics', row.id)
|
|
567
573
|
}
|
|
568
574
|
for (const [id, h] of wanted) {
|
|
569
|
-
const row = {
|
|
575
|
+
const row = {
|
|
576
|
+
id,
|
|
577
|
+
handle: h.id,
|
|
578
|
+
type: h.type,
|
|
579
|
+
key: h.key,
|
|
580
|
+
name: h.name ?? null,
|
|
581
|
+
encryptionKey: h.encryptionKey ?? null,
|
|
582
|
+
epochs: h.epochs && c.decode(epochEntries, h.epochs).length ? h.epochs : null
|
|
583
|
+
}
|
|
570
584
|
if (same(have.get(id), row)) continue
|
|
571
585
|
await this.local.store.put('topics', row)
|
|
572
586
|
}
|
|
@@ -578,9 +592,7 @@ export class Handle extends ReadyResource {
|
|
|
578
592
|
// a suspended storage answers no read: the cold entry serves a push while the app sleeps
|
|
579
593
|
if (this._sus.suspended) throw CeroError.INVALID('receive: the app is suspended')
|
|
580
594
|
const { data: rows } = await this.local.store.get('topics')
|
|
581
|
-
|
|
582
|
-
const hit = await verify(this.store.store.storage, payload, handles)
|
|
583
|
-
return hit && { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer, ref: hit.ref }
|
|
595
|
+
return read(this.store.store.storage, this.spec, payload, rows)
|
|
584
596
|
}
|
|
585
597
|
|
|
586
598
|
/** @private */
|
|
@@ -588,6 +600,23 @@ export class Handle extends ReadyResource {
|
|
|
588
600
|
return this.identity.toPhrase()
|
|
589
601
|
}
|
|
590
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
|
+
|
|
591
620
|
/**
|
|
592
621
|
* @param {Uint8Array} coreKey
|
|
593
622
|
* @param {object} info
|
|
@@ -1104,8 +1133,12 @@ export class Handle extends ReadyResource {
|
|
|
1104
1133
|
if (!serving) return this.local.store.del('serving', child.id)
|
|
1105
1134
|
await this.local.store.put('serving', { id: child.id, type: child.type })
|
|
1106
1135
|
}
|
|
1107
|
-
|
|
1108
|
-
|
|
1136
|
+
// one at a time, so an invite can wait for its row
|
|
1137
|
+
const queue = () => {
|
|
1138
|
+
child._saved = (child._saved || Promise.resolve()).then(save).catch(this._onerror)
|
|
1139
|
+
}
|
|
1140
|
+
child._pair.on('serving', queue)
|
|
1141
|
+
queue()
|
|
1109
1142
|
}
|
|
1110
1143
|
|
|
1111
1144
|
/** @private */
|
|
@@ -1190,10 +1223,16 @@ function same(row, next) {
|
|
|
1190
1223
|
row.handle === next.handle &&
|
|
1191
1224
|
row.type === next.type &&
|
|
1192
1225
|
(row.name ?? null) === next.name &&
|
|
1193
|
-
b4a.equals(row.key, next.key)
|
|
1226
|
+
b4a.equals(row.key, next.key) &&
|
|
1227
|
+
sameBytes(row.encryptionKey, next.encryptionKey) &&
|
|
1228
|
+
sameBytes(row.epochs, next.epochs)
|
|
1194
1229
|
)
|
|
1195
1230
|
}
|
|
1196
1231
|
|
|
1232
|
+
function sameBytes(a, b) {
|
|
1233
|
+
return a && b ? b4a.equals(a, b) : (a ?? null) === (b ?? null)
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1197
1236
|
// run every teardown step, then surface the first failure: a bug must not leak the storage lock
|
|
1198
1237
|
async function settle(steps) {
|
|
1199
1238
|
let failed = null
|
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 }] }
|
package/src/lib/mail.js
ADDED
|
@@ -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
|
+
}
|
package/src/lib/operators.js
CHANGED
|
@@ -496,13 +496,44 @@ export async function deactivate(ctx) {
|
|
|
496
496
|
*
|
|
497
497
|
* @param {Context} me
|
|
498
498
|
* @param {Uint8Array} payload
|
|
499
|
-
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
|
|
499
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
500
500
|
*/
|
|
501
501
|
export async function receive(me, payload) {
|
|
502
502
|
if (me.parent) throw CeroError.INVALID("receive is the app's, on me")
|
|
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
|
*
|
package/src/local/index.js
CHANGED
|
@@ -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,
|
|
82
|
+
put: (mail) => store.put(name, pack(mail)),
|
|
83
83
|
del: (id) => store.del(name, id)
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
|
|
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/push/index.js
CHANGED
|
@@ -3,11 +3,47 @@ import Corestore from 'corestore'
|
|
|
3
3
|
import b4a from 'b4a'
|
|
4
4
|
import safetyCatch from 'safety-catch'
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import c from 'compact-encoding'
|
|
7
|
+
import { verify, decrypt } from '@cero-base/core/push'
|
|
7
8
|
import { CeroError } from '@cero-base/core/errors'
|
|
9
|
+
import { epochEntries } from '@cero-base/core/database/encryption'
|
|
8
10
|
|
|
9
11
|
import { Local } from '../local/index.js'
|
|
10
12
|
|
|
13
|
+
/**
|
|
14
|
+
* A push verified against the handles the topics rows name, with the row it wrote when the block
|
|
15
|
+
* rode along and this device holds the handle's keys.
|
|
16
|
+
*
|
|
17
|
+
* @param {import('hypercore-storage')} storage
|
|
18
|
+
* @param {import('../lib/spec.js').Spec} spec
|
|
19
|
+
* @param {Uint8Array} payload
|
|
20
|
+
* @param {Array<{ id: string, handle: string, type: string, key: Uint8Array, name: string | null, encryptionKey?: Uint8Array | null, epochs?: Uint8Array | null }>} rows The root's `topics` rows.
|
|
21
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
22
|
+
*/
|
|
23
|
+
export async function read(storage, spec, payload, rows) {
|
|
24
|
+
const handles = rows.map(({ handle, key, type, name }) => ({ id: handle, key, type, name }))
|
|
25
|
+
const hit = await verify(storage, payload, handles)
|
|
26
|
+
if (!hit) return null
|
|
27
|
+
const keys = rows.find((r) => r.handle === hit.id)
|
|
28
|
+
const row = await rowOf(storage, spec, hit, keys)
|
|
29
|
+
return { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer, ref: hit.ref, row }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// the row a push's block holds: null when none rode along, or it does not decode with what we hold
|
|
33
|
+
async function rowOf(storage, spec, hit, { encryptionKey, epochs }) {
|
|
34
|
+
if (!hit.block || !encryptionKey) return null
|
|
35
|
+
try {
|
|
36
|
+
const body = await decrypt(storage, hit, {
|
|
37
|
+
encryptionKey,
|
|
38
|
+
epochs: epochs ? c.decode(epochEntries, epochs) : []
|
|
39
|
+
})
|
|
40
|
+
return body ? (spec.handles[hit.type].dispatch.decode(body).value ?? null) : null
|
|
41
|
+
} catch (err) {
|
|
42
|
+
safetyCatch(err)
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
11
47
|
/**
|
|
12
48
|
* A push the platform delivered while the app is dead, verified from its storage opened
|
|
13
49
|
* read-only. `null` while the live app holds the storage, which shows the activity itself, and
|
|
@@ -17,7 +53,7 @@ import { Local } from '../local/index.js'
|
|
|
17
53
|
* @param {import('../lib/spec.js').Spec} spec
|
|
18
54
|
* @param {Uint8Array} payload
|
|
19
55
|
* @param {{ storageKey?: Uint8Array }} [opts] The key `cero()` encrypts the local store with, if any.
|
|
20
|
-
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
|
|
56
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
21
57
|
*/
|
|
22
58
|
export async function receive(dir, spec, payload, { storageKey } = {}) {
|
|
23
59
|
const storage = new HypercoreStorage(`${dir}/main`, { readOnly: true })
|
|
@@ -29,9 +65,7 @@ export async function receive(dir, spec, payload, { storageKey } = {}) {
|
|
|
29
65
|
local = new Local(null, spec, { store, storageKey })
|
|
30
66
|
await local.ready()
|
|
31
67
|
const { data: rows } = await local.store.get('topics')
|
|
32
|
-
|
|
33
|
-
const hit = await verify(storage, payload, handles)
|
|
34
|
-
return hit && { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer, ref: hit.ref }
|
|
68
|
+
return await read(storage, spec, payload, rows)
|
|
35
69
|
} catch (err) {
|
|
36
70
|
safetyCatch(err)
|
|
37
71
|
return null
|
|
@@ -44,16 +78,16 @@ export async function receive(dir, spec, payload, { storageKey } = {}) {
|
|
|
44
78
|
|
|
45
79
|
/**
|
|
46
80
|
* 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
|
|
48
|
-
* `{ title, body }`, or nothing. The request carries `storage`, `payload` (base64) and, when the
|
|
81
|
+
* new data on a handle this device holds, answer with what the show handler returns: the
|
|
82
|
+
* notification, `{ title, body }`, or nothing. The request carries `storage`, `payload` (base64) and, when the
|
|
49
83
|
* app encrypts its local store, `storageKey` (base64).
|
|
50
84
|
*
|
|
51
85
|
* @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
|
|
86
|
+
* @param {(push: { id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null }) => { title: string, body: string } | null | undefined | Promise<{ title: string, body: string } | null | undefined>} show The show handler: what to show for a verified push, as `cero.receive` would give it to the app.
|
|
53
87
|
* @returns {void}
|
|
54
88
|
*/
|
|
55
89
|
export function push(spec, show) {
|
|
56
|
-
if (typeof show !== 'function') throw CeroError.INVALID('push: show
|
|
90
|
+
if (typeof show !== 'function') throw CeroError.INVALID('push: a show handler says what to show')
|
|
57
91
|
globalThis.BareKit.on('push', async (json, reply) => {
|
|
58
92
|
let shown = null
|
|
59
93
|
try {
|
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
|
|
package/types/build/schemas.d.ts
CHANGED
|
@@ -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;
|
|
@@ -190,8 +193,13 @@ export declare const local: {
|
|
|
190
193
|
type: import("@cero-base/core").Prim;
|
|
191
194
|
key: import("@cero-base/core").Prim;
|
|
192
195
|
name: import("@cero-base/core").Prim;
|
|
196
|
+
encryptionKey: import("@cero-base/core").Prim;
|
|
197
|
+
epochs: import("@cero-base/core").Prim;
|
|
193
198
|
};
|
|
194
199
|
};
|
|
200
|
+
export declare const contract: {
|
|
201
|
+
version: number;
|
|
202
|
+
};
|
|
195
203
|
export declare const rpc: {
|
|
196
204
|
'req-empty': {
|
|
197
205
|
ok: import("@cero-base/core").Prim;
|
|
@@ -55,7 +55,18 @@ export declare const cero: {
|
|
|
55
55
|
name: string | null;
|
|
56
56
|
newer: boolean;
|
|
57
57
|
ref: string | null;
|
|
58
|
+
row: object | null;
|
|
58
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>;
|
|
59
70
|
phrase(me: operators.Context): Promise<string>;
|
|
60
71
|
nearby(ctx: operators.Context, on: boolean, opts?: {
|
|
61
72
|
topic?: string;
|
package/types/handle/index.d.ts
CHANGED
|
@@ -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;
|
package/types/lib/operators.d.ts
CHANGED
|
@@ -235,7 +235,7 @@ export declare function deactivate(ctx: Context): Promise<void>;
|
|
|
235
235
|
*
|
|
236
236
|
* @param {Context} me
|
|
237
237
|
* @param {Uint8Array} payload
|
|
238
|
-
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
|
|
238
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
239
239
|
*/
|
|
240
240
|
export declare function receive(me: Context, payload: Uint8Array): Promise<{
|
|
241
241
|
id: string;
|
|
@@ -243,7 +243,41 @@ export declare function receive(me: Context, payload: Uint8Array): Promise<{
|
|
|
243
243
|
name: string | null;
|
|
244
244
|
newer: boolean;
|
|
245
245
|
ref: string | null;
|
|
246
|
+
row: object | null;
|
|
246
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>;
|
|
247
281
|
/**
|
|
248
282
|
* Reveal the recovery phrase.
|
|
249
283
|
*
|
package/types/push/index.d.ts
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A push verified against the handles the topics rows name, with the row it wrote when the block
|
|
3
|
+
* rode along and this device holds the handle's keys.
|
|
4
|
+
*
|
|
5
|
+
* @param {import('hypercore-storage')} storage
|
|
6
|
+
* @param {import('../lib/spec.js').Spec} spec
|
|
7
|
+
* @param {Uint8Array} payload
|
|
8
|
+
* @param {Array<{ id: string, handle: string, type: string, key: Uint8Array, name: string | null, encryptionKey?: Uint8Array | null, epochs?: Uint8Array | null }>} rows The root's `topics` rows.
|
|
9
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
10
|
+
*/
|
|
11
|
+
export declare function read(storage: import('hypercore-storage'), spec: import('../lib/spec.js').Spec, payload: Uint8Array, rows: Array<{
|
|
12
|
+
id: string;
|
|
13
|
+
handle: string;
|
|
14
|
+
type: string;
|
|
15
|
+
key: Uint8Array;
|
|
16
|
+
name: string | null;
|
|
17
|
+
encryptionKey?: Uint8Array | null;
|
|
18
|
+
epochs?: Uint8Array | null;
|
|
19
|
+
}>): Promise<{
|
|
20
|
+
id: string;
|
|
21
|
+
type: string;
|
|
22
|
+
name: string | null;
|
|
23
|
+
newer: boolean;
|
|
24
|
+
ref: string | null;
|
|
25
|
+
row: object | null;
|
|
26
|
+
} | null>;
|
|
1
27
|
/**
|
|
2
28
|
* A push the platform delivered while the app is dead, verified from its storage opened
|
|
3
29
|
* read-only. `null` while the live app holds the storage, which shows the activity itself, and
|
|
@@ -7,7 +33,7 @@
|
|
|
7
33
|
* @param {import('../lib/spec.js').Spec} spec
|
|
8
34
|
* @param {Uint8Array} payload
|
|
9
35
|
* @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, ref: string | null } | null>}
|
|
36
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
11
37
|
*/
|
|
12
38
|
export declare function receive(dir: string, spec: import('../lib/spec.js').Spec, payload: Uint8Array, { storageKey }?: {
|
|
13
39
|
storageKey?: Uint8Array;
|
|
@@ -17,15 +43,16 @@ export declare function receive(dir: string, spec: import('../lib/spec.js').Spec
|
|
|
17
43
|
name: string | null;
|
|
18
44
|
newer: boolean;
|
|
19
45
|
ref: string | null;
|
|
46
|
+
row: object | null;
|
|
20
47
|
} | null>;
|
|
21
48
|
/**
|
|
22
49
|
* 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
|
|
24
|
-
* `{ title, body }`, or nothing. The request carries `storage`, `payload` (base64) and, when the
|
|
50
|
+
* new data on a handle this device holds, answer with what the show handler returns: the
|
|
51
|
+
* notification, `{ title, body }`, or nothing. The request carries `storage`, `payload` (base64) and, when the
|
|
25
52
|
* app encrypts its local store, `storageKey` (base64).
|
|
26
53
|
*
|
|
27
54
|
* @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
|
|
55
|
+
* @param {(push: { id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null }) => { title: string, body: string } | null | undefined | Promise<{ title: string, body: string } | null | undefined>} show The show handler: what to show for a verified push, as `cero.receive` would give it to the app.
|
|
29
56
|
* @returns {void}
|
|
30
57
|
*/
|
|
31
58
|
export declare function push(spec: import('../lib/spec.js').Spec, show: (push: {
|
|
@@ -34,6 +61,7 @@ export declare function push(spec: import('../lib/spec.js').Spec, show: (push: {
|
|
|
34
61
|
name: string | null;
|
|
35
62
|
newer: boolean;
|
|
36
63
|
ref: string | null;
|
|
64
|
+
row: object | null;
|
|
37
65
|
}) => {
|
|
38
66
|
title: string;
|
|
39
67
|
body: string;
|
package/types/rpc/client.d.ts
CHANGED
|
@@ -47,7 +47,18 @@ declare const remote: {
|
|
|
47
47
|
name: string | null;
|
|
48
48
|
newer: boolean;
|
|
49
49
|
ref: string | null;
|
|
50
|
+
row: object | null;
|
|
50
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>;
|
|
51
62
|
phrase(me: verbs.Context): Promise<string>;
|
|
52
63
|
nearby(ctx: verbs.Context, on: boolean, opts?: {
|
|
53
64
|
topic?: string;
|