Skip to content

Commit 1de9afa

Browse files
add read only invites
1 parent 59638a6 commit 1de9afa

9 files changed

Lines changed: 345 additions & 89 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Add a writer explictly.
8383

8484
Get the local writer key.
8585

86-
#### `inv = await pass.createInvite()`
86+
#### `inv = await pass.createInvite({ readOnly })`
8787

8888
Get invite to add a writer.
8989

index.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,27 @@ class AutopassPairer extends ReadyResource {
7070

7171
await this.pass.deleteInvite()
7272
}
73+
const readOnly = JSON.parse(result.data.toString()).readOnly
7374
this.swarm = null
7475
this.store = null
75-
if (this.onresolve) this._whenWritable()
76+
if (this.onresolve && readOnly) {
77+
this._whenReadable()
78+
} else if (this.onresolve) {
79+
this._whenWritable()
80+
}
7681
this.candidate.close().catch(noop)
7782
}
7883
})
7984
}
8085

86+
_whenReadable() {
87+
const check = () => {
88+
this.pass.base.off('update', check)
89+
this.onresolve(this.pass)
90+
}
91+
this.pass.base.on('update', check)
92+
}
93+
8194
_whenWritable() {
8295
if (this.pass.base.writable) return
8396
const check = () => {
@@ -238,14 +251,20 @@ class Autopass extends ReadyResource {
238251

239252
async createInvite(opts) {
240253
if (this.opened === false) await this.ready()
254+
const readOnly = opts?.readOnly ? true : false
241255
const existing = await this.base.view.findOne('@autopass/invite', {})
242256
if (existing) {
243257
if (this.member) await this.member.flushed()
244258
return z32.encode(existing.invite)
245259
}
246-
const { id, invite, publicKey, expires } = BlindPairing.createInvite(this.base.key)
260+
const { id, invite, publicKey, expires, additional } = BlindPairing.createInvite(
261+
this.base.key,
262+
{
263+
data: Buffer.from(JSON.stringify({ readOnly }))
264+
}
265+
)
247266

248-
const record = { id, invite, publicKey, expires }
267+
const record = { id, invite, publicKey, expires, readOnly, additional }
249268
await this.base.append(encode('@autopass/add-invite', record))
250269
if (this.member) await this.member.flushed()
251270
return z32.encode(record.invite)
@@ -313,11 +332,15 @@ class Autopass extends ReadyResource {
313332
if (inv === null || !b4a.equals(inv.id, id)) {
314333
return
315334
}
335+
const readOnly = inv.readOnly
316336
candidate.open(inv.publicKey)
317-
await this.addWriter(candidate.userData)
337+
if (!readOnly) {
338+
await this.addWriter(candidate.userData)
339+
}
318340
candidate.confirm({
319341
key: this.base.key,
320-
encryptionKey: this.base.encryptionKey
342+
encryptionKey: this.base.encryptionKey,
343+
additional: inv.additional
321344
})
322345
await this.deleteInvite()
323346
}

schema.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ template.register({
6262
]
6363
})
6464

65+
template.register({
66+
name: 'additional-invite-data',
67+
fields: [
68+
{ name: 'data', type: 'buffer', required: true },
69+
{ name: 'signature', type: 'buffer', required: true }
70+
]
71+
})
72+
6573
template.register({
6674
name: 'invite',
6775
compact: false,
@@ -85,6 +93,16 @@ template.register({
8593
name: 'expires',
8694
type: 'int',
8795
required: true
96+
},
97+
{
98+
name: 'readOnly',
99+
type: 'bool',
100+
required: true
101+
},
102+
{
103+
name: 'additional',
104+
type: '@autopass/additional-invite-data',
105+
required: false
88106
}
89107
]
90108
})

spec/db/db.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"namespace": "autopass",
88
"id": 0,
99
"type": 1,
10+
"version": 0,
11+
"versionField": null,
1012
"indexes": [],
1113
"schema": "@autopass/records",
1214
"derived": false,
@@ -18,6 +20,8 @@
1820
"namespace": "autopass",
1921
"id": 1,
2022
"type": 1,
23+
"version": 0,
24+
"versionField": null,
2125
"indexes": [],
2226
"schema": "@autopass/invite",
2327
"derived": false,
@@ -29,6 +33,8 @@
2933
"namespace": "autopass",
3034
"id": 2,
3135
"type": 1,
36+
"version": 0,
37+
"versionField": null,
3238
"indexes": [],
3339
"schema": "@autopass/mirrors",
3440
"derived": false,
@@ -40,6 +46,8 @@
4046
"namespace": "autopass",
4147
"id": 3,
4248
"type": 1,
49+
"version": 0,
50+
"versionField": null,
4351
"indexes": [],
4452
"schema": "@autopass/writer",
4553
"derived": false,
@@ -51,6 +59,8 @@
5159
"namespace": "autopass",
5260
"id": 4,
5361
"type": 1,
62+
"version": 0,
63+
"versionField": null,
5464
"indexes": [],
5565
"schema": "@autopass/delete",
5666
"derived": false,
@@ -62,6 +72,8 @@
6272
"namespace": "autopass",
6373
"id": 5,
6474
"type": 1,
75+
"version": 0,
76+
"versionField": null,
6577
"indexes": [],
6678
"schema": "@autopass/del-invite",
6779
"derived": false,
@@ -73,6 +85,8 @@
7385
"namespace": "autopass",
7486
"id": 6,
7587
"type": 1,
88+
"version": 0,
89+
"versionField": null,
7690
"indexes": [],
7791
"schema": "@autopass/del-mirror",
7892
"derived": false,

0 commit comments

Comments
 (0)