Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,30 @@ Add new entry

Remove an entry.

#### `stream = pass.listWriters(query)`

List all writers, optionally takes a hyperdb query

#### `writer = await pass.getWriter(key)

Get a writer, object similar to the addWriter input.

#### `await pass.removeWriter(writerKey)`

Remove a writer explictly.

#### `await pass.addWriter(writerKey)`
#### `await pass.addWriter(writer)`

Add a writer explictly.
Writer should look like this

```
{
key: otherSidesLocalWriterKey,
name: 'optional name for the writer',
readOnly: false // weather the person can write or only read
}
```

#### `pass.writerKey`

Expand Down Expand Up @@ -127,7 +144,7 @@ Fully close the pass instance.

Suspend the swarm and discovery

#### `await pass.resume`
#### `await pass.resume()`

Resume the swarm is suspended

Expand Down
53 changes: 33 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const { Router, encode, decode } = require('./spec/hyperdispatch')
const BlindPeering = require('blind-peering')
const db = require('./spec/db/index.js')
const enc = require('hypercore-id-encoding')
const c = require('compact-encoding')

const { getEncoding } = require('./spec/schema/index.js')

const PUBLIC_INVITE_METADATA = getEncoding('@autopass/public-invite-metadata')
const INVITEE = getEncoding('@autopass/invitee')

class AutopassPairer extends ReadyResource {
constructor(store, invite, opts = {}) {
Expand All @@ -29,6 +35,7 @@ class AutopassPairer extends ReadyResource {
this.pass = null
this.relayThrough = opts.relayThrough || null
this.blindEncryption = opts.blindEncryption || null
this.name = opts.name || null

this.ready().catch(noop)
}
Expand All @@ -54,9 +61,10 @@ class AutopassPairer extends ReadyResource {
await core.ready()
const key = core.key
await core.close()

this.candidate = this.pairing.addCandidate({
invite: z32.decode(this.invite),
userData: key,
userData: c.encode(INVITEE, { key, name: this.name }),
onadd: async (result) => {
if (this.pass === null) {
this.pass = new Autopass(this.store, {
Expand All @@ -70,10 +78,12 @@ class AutopassPairer extends ReadyResource {

await this.pass.deleteInvite()
}
const readOnly = JSON.parse(result.data.toString()).readOnly

const metadata = result.data ? c.decode(PUBLIC_INVITE_METADATA, result.data) : null
Comment thread
mafintosh marked this conversation as resolved.

this.swarm = null
this.store = null
if (this.onresolve && readOnly) {
if (this.onresolve && metadata && metadata.readOnly) {
this._whenReadable()
} else if (this.onresolve) {
this._whenWritable()
Expand All @@ -84,11 +94,7 @@ class AutopassPairer extends ReadyResource {
}

_whenReadable() {
const check = () => {
this.pass.base.off('update', check)
this.onresolve(this.pass)
}
this.pass.base.on('update', check)
this.onresolve(this.pass)
}

_whenWritable() {
Expand Down Expand Up @@ -149,11 +155,13 @@ class Autopass extends ReadyResource {
this.debug = !!opts.key
// Register handlers for commands
this.router.add('@autopass/remove-writer', async (data, context) => {
await context.view.delete('@autopass/writer', data)
await context.base.removeWriter(data.key)
})

this.router.add('@autopass/add-writer', async (data, context) => {
await context.base.addWriter(data.key)
await context.view.insert('@autopass/writer', data)
if (!data.readOnly) await context.base.addWriter(data.key)
})

this.router.add('@autopass/put', async (data, context) => {
Expand Down Expand Up @@ -263,7 +271,7 @@ class Autopass extends ReadyResource {
const { id, invite, publicKey, expires, additional } = BlindPairing.createInvite(
this.base.key,
{
data: Buffer.from(JSON.stringify({ readOnly }))
data: c.encode(PUBLIC_INVITE_METADATA, { readOnly })
}
)

Expand Down Expand Up @@ -293,19 +301,25 @@ class Autopass extends ReadyResource {
return { value: data.value, file: data.file }
}

async addWriter(key) {
await this.base.append(
encode('@autopass/add-writer', {
key: b4a.isBuffer(key) ? key : b4a.from(key)
})
)
listWriters(query) {
return this.base.view.find('@autopass/writer', query)
}

async getWriter(key) {
return this.base.view.get('@autopass/writer', { key })
}

async addWriter(data) {
if (typeof data === 'string') data = b4a.from(data, 'hex')
if (b4a.isBuffer(data)) data = { key: data, name: null, readOnly: false }
await this.base.append(encode('@autopass/add-writer', data))
return true
}
Comment thread
mafintosh marked this conversation as resolved.

async removeWriter(key) {
await this.base.append(
encode('@autopass/remove-writer', {
key: b4a.isBuffer(key) ? key : b4a.from(key)
key: b4a.isBuffer(key) ? key : b4a.from(key, 'hex')
})
)
}
Expand Down Expand Up @@ -337,9 +351,8 @@ class Autopass extends ReadyResource {
}
const readOnly = inv.readOnly
candidate.open(inv.publicKey)
if (!readOnly) {
await this.addWriter(candidate.userData)
}
const { key, name } = c.decode(INVITEE, candidate.userData)
await this.addWriter({ key, name, readOnly })
candidate.confirm({
key: this.base.key,
encryptionKey: this.base.encryptionKey,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "3.3.0",
"main": "index.js",
"scripts": {
"brittle": "npx brittle test.js",
"lint": "npx prettier . --check",
"brittle": "brittle test.js",
"lint": "prettier . --check",
"format": "prettier . --write",
"test": "npm run lint && npm run brittle"
},
"author": "Holepunch Inc",
Expand Down
39 changes: 30 additions & 9 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const Hyperdispatch = require('hyperdispatch')

// SCHEMA CREATION START //
const autopass = Hyperschema.from('./spec/schema')
const template = autopass.namespace('autopass')
const pass = autopass.namespace('autopass')
// You can find a list of supported data types here: https://github.com/holepunchto/compact-encoding
template.register({
pass.register({
name: 'records',
compact: false,
fields: [
Expand All @@ -27,7 +27,7 @@ template.register({
}
]
})
template.register({
pass.register({
name: 'mirrors',
compact: false,
fields: [
Expand All @@ -38,19 +38,27 @@ template.register({
}
]
})
template.register({
pass.register({
name: 'writer',
compact: false,
fields: [
{
name: 'key',
type: 'buffer',
required: true
},
{
name: 'name',
type: 'string'
},
{
name: 'readOnly',
type: 'bool'
}
]
})

template.register({
pass.register({
name: 'delete',
compact: false,
fields: [
Expand All @@ -62,15 +70,28 @@ template.register({
]
})

template.register({
pass.register({
name: 'invitee',
fields: [
{ name: 'key', type: 'fixed32', required: true },
{ name: 'name', type: 'string' }
]
})

pass.register({
name: 'public-invite-metadata',
fields: [{ name: 'readOnly', type: 'bool' }]
})

pass.register({
name: 'additional-invite-data',
fields: [
{ name: 'data', type: 'buffer', required: true },
{ name: 'signature', type: 'buffer', required: true }
]
})

template.register({
pass.register({
name: 'invite',
compact: false,
fields: [
Expand Down Expand Up @@ -107,7 +128,7 @@ template.register({
]
})

template.register({
pass.register({
name: 'del-invite',
compact: false,
fields: [
Expand All @@ -118,7 +139,7 @@ template.register({
}
]
})
template.register({
pass.register({
name: 'del-mirror',
compact: false,
fields: [
Expand Down
16 changes: 8 additions & 8 deletions spec/db/db.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": 0,
"version": 1,
"offset": 0,
"schema": [
{
"name": "records",
"namespace": "autopass",
"id": 0,
"type": 1,
"version": 0,
"version": 1,
"versionField": null,
"indexes": [],
"schema": "@autopass/records",
Expand All @@ -20,7 +20,7 @@
"namespace": "autopass",
"id": 1,
"type": 1,
"version": 0,
"version": 1,
"versionField": null,
"indexes": [],
"schema": "@autopass/invite",
Expand All @@ -33,7 +33,7 @@
"namespace": "autopass",
"id": 2,
"type": 1,
"version": 0,
"version": 1,
"versionField": null,
"indexes": [],
"schema": "@autopass/mirrors",
Expand All @@ -46,7 +46,7 @@
"namespace": "autopass",
"id": 3,
"type": 1,
"version": 0,
"version": 1,
"versionField": null,
"indexes": [],
"schema": "@autopass/writer",
Expand All @@ -59,7 +59,7 @@
"namespace": "autopass",
"id": 4,
"type": 1,
"version": 0,
"version": 1,
"versionField": null,
"indexes": [],
"schema": "@autopass/delete",
Expand All @@ -72,7 +72,7 @@
"namespace": "autopass",
"id": 5,
"type": 1,
"version": 0,
"version": 1,
"versionField": null,
"indexes": [],
"schema": "@autopass/del-invite",
Expand All @@ -85,7 +85,7 @@
"namespace": "autopass",
"id": 6,
"type": 1,
"version": 0,
"version": 1,
"versionField": null,
"indexes": [],
"schema": "@autopass/del-mirror",
Expand Down
Loading
Loading