blind-peer 0.0.1

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.
@@ -0,0 +1,23 @@
1
+ name: Build Status
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ pull_request:
7
+ branches:
8
+ - main
9
+ jobs:
10
+ build:
11
+ strategy:
12
+ matrix:
13
+ node-version: [lts/*]
14
+ os: [ubuntu-latest, macos-latest, windows-latest]
15
+ runs-on: ${{ matrix.os }}
16
+ steps:
17
+ - uses: actions/checkout@v3
18
+ - name: Use Node.js ${{ matrix.node-version }}
19
+ uses: actions/setup-node@v3
20
+ with:
21
+ node-version: ${{ matrix.node-version }}
22
+ - run: npm install
23
+ - run: npm test
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # blind-peer
2
+
3
+ Its a blind peer
4
+
5
+ ```
6
+ npm install blind-peer
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ``` js
12
+ const BlindPeer = require('blind-peer')
13
+ ```
14
+
15
+ ## License
16
+
17
+ Apache-2.0
package/build.js ADDED
@@ -0,0 +1,102 @@
1
+ const HyperDB = require('hyperdb/builder')
2
+ const Hyperschema = require('hyperschema')
3
+
4
+ const SCHEMA_DIR = './spec/hyperschema'
5
+ const DB_DIR = './spec/hyperdb'
6
+
7
+ const schema = Hyperschema.from(SCHEMA_DIR)
8
+ const blind = schema.namespace('blind-peer')
9
+
10
+ blind.register({
11
+ name: 'request-mailbox',
12
+ fields: [
13
+ {
14
+ name: 'autobase',
15
+ type: 'fixed32',
16
+ required: true
17
+ },
18
+ {
19
+ name: 'blockEncryptionKey',
20
+ type: 'fixed32'
21
+ }
22
+ ]
23
+ })
24
+
25
+ blind.register({
26
+ name: 'response-mailbox',
27
+ fields: [
28
+ {
29
+ name: 'autobase',
30
+ type: 'fixed32',
31
+ required: true
32
+ },
33
+ {
34
+ name: 'writer',
35
+ type: 'fixed32',
36
+ required: true
37
+ },
38
+ {
39
+ name: 'open',
40
+ type: 'bool'
41
+ }
42
+ ]
43
+ })
44
+
45
+ blind.register({
46
+ name: 'request-post',
47
+ fields: [
48
+ {
49
+ name: 'autobase',
50
+ type: 'fixed32',
51
+ required: true
52
+ },
53
+ {
54
+ name: 'message',
55
+ type: 'string'
56
+ }
57
+ ]
58
+ })
59
+
60
+ blind.register({
61
+ name: 'response-post',
62
+ fields: [
63
+ {
64
+ name: 'length',
65
+ type: 'uint',
66
+ required: true
67
+ }
68
+ ]
69
+ })
70
+
71
+ blind.register({
72
+ name: 'mailbox',
73
+ fields: [
74
+ {
75
+ name: 'autobase',
76
+ type: 'fixed32',
77
+ required: true
78
+ },
79
+ {
80
+ name: 'writer',
81
+ type: 'fixed32',
82
+ required: true
83
+ },
84
+ {
85
+ name: 'blockEncryptionKey',
86
+ type: 'fixed32'
87
+ }
88
+ ]
89
+ })
90
+
91
+ Hyperschema.toDisk(schema)
92
+
93
+ const db = HyperDB.from(SCHEMA_DIR, DB_DIR)
94
+ const blindDB = db.namespace('blind-peer')
95
+
96
+ blindDB.collections.register({
97
+ name: 'mailbox',
98
+ schema: '@blind-peer/mailbox',
99
+ key: ['autobase']
100
+ })
101
+
102
+ HyperDB.toDisk(db)
package/client.js ADDED
@@ -0,0 +1,31 @@
1
+ const schema = require('./spec/hyperschema')
2
+ const c = require('compact-encoding')
3
+ const ProtomuxRPC = require('protomux-rpc')
4
+
5
+ const addMailboxEncoding = {
6
+ requestEncoding: schema.resolveStruct('@blind-peer/request-mailbox'),
7
+ responseEncoding: schema.resolveStruct('@blind-peer/response-mailbox')
8
+ }
9
+
10
+ const postEncoding = {
11
+ requestEncoding: schema.resolveStruct('@blind-peer/request-post'),
12
+ responseEncoding: schema.resolveStruct('@blind-peer/response-post')
13
+ }
14
+
15
+ module.exports = class BlindPeerClient {
16
+ constructor (stream) {
17
+ this.stream = stream
18
+ this.rpc = new ProtomuxRPC(stream, {
19
+ id: stream.remotePublicKey,
20
+ valueEncoding: c.none
21
+ })
22
+ }
23
+
24
+ addMailbox (data) {
25
+ return this.rpc.request('add-mailbox', data, addMailboxEncoding)
26
+ }
27
+
28
+ post ({ autobase, message }) {
29
+ return this.rpc.request('post', { autobase, message }, postEncoding)
30
+ }
31
+ }
@@ -0,0 +1,62 @@
1
+ import BlindPeerClient from '../client.js'
2
+ import Autobase from 'autobase'
3
+ import c from 'compact-encoding'
4
+ import Corestore from 'corestore'
5
+ import Hyperswarm from 'hyperswarm'
6
+ import debounce from 'debounceify'
7
+
8
+ const base = new Autobase(new Corestore('/tmp/my-corestore'), {
9
+ encryptionKey: Buffer.alloc(30).fill('secret'),
10
+ valueEncoding: c.json,
11
+ open (store) {
12
+ return store.get('view', { valueEncoding: c.json })
13
+ },
14
+ async apply (nodes, view, base) {
15
+ for (const node of nodes) {
16
+ if (node.value.add) await base.addWriter(Buffer.from(node.value.key, 'hex'), { indexer: false })
17
+ view.append(node.value)
18
+ }
19
+ }
20
+ })
21
+
22
+ await base.ready()
23
+ console.log('Autobase:', base.key.toString('hex'))
24
+
25
+ base.view.on('append', debounce(async function () {
26
+ base.ack() // ack for good messure
27
+ console.log('someone appended to the autobase!')
28
+ for (let i = 0; i < base.view.length; i++) {
29
+ console.log(i, await base.view.get(i))
30
+ }
31
+ }))
32
+
33
+ // TODO: record in autobase
34
+ const publicKey = Buffer.from(process.argv[2], 'hex')
35
+
36
+ const s = new Hyperswarm({ keyPair: await base.store.createKeyPair('tmp') })
37
+
38
+ s.on('connection', async c => {
39
+ base.store.replicate(c)
40
+
41
+ if (!c.remotePublicKey.equals(publicKey)) return
42
+
43
+ const peer = new BlindPeerClient(c)
44
+
45
+ const info = await peer.addMailbox({ autobase: base.key })
46
+
47
+ if (info.open === false) {
48
+ await base.append({ add: true, key: info.writer.toString('hex') })
49
+ await base.update()
50
+
51
+ const core = base.store.get({ key: info.writer, active: false })
52
+ await core.setEncryptionKey(base.encryptionKey)
53
+ const req = { autobase: base.key, blockEncryptionKey: core.encryption.blockKey }
54
+ await core.close()
55
+
56
+ await peer.addMailbox(req)
57
+
58
+ console.log('opened mailbox...')
59
+ }
60
+ })
61
+
62
+ s.joinPeer(publicKey)
@@ -0,0 +1,6 @@
1
+ import BlindPeer from '../index.js'
2
+
3
+ const m = new BlindPeer('/tmp/blind-peer')
4
+ await m.listen()
5
+
6
+ console.log(m.publicKey.toString('hex'))
@@ -0,0 +1,21 @@
1
+ import BlindPeerClient from '../client.js'
2
+ import Hyperswarm from 'hyperswarm'
3
+
4
+ const publicKey = Buffer.from(process.argv[2], 'hex')
5
+ const autobase = Buffer.from(process.argv[3], 'hex')
6
+ const message = process.argv[4]
7
+
8
+ const s = new Hyperswarm()
9
+
10
+ s.on('connection', async c => {
11
+ if (!c.remotePublicKey.equals(publicKey)) return
12
+
13
+ const peer = new BlindPeerClient(c)
14
+ const reply = await peer.post({ autobase, message })
15
+
16
+ console.log(reply)
17
+
18
+ s.destroy()
19
+ })
20
+
21
+ s.joinPeer(publicKey)
package/index.js ADDED
@@ -0,0 +1,139 @@
1
+ const AutobaseLightWriter = require('autobase-light-writer')
2
+ const HyperDB = require('hyperdb')
3
+ const Corestore = require('corestore')
4
+ const definition = require('./spec/hyperdb')
5
+ const schema = require('./spec/hyperschema')
6
+ const path = require('path')
7
+ const Hyperswarm = require('hyperswarm')
8
+ const ProtomuxRPC = require('protomux-rpc')
9
+ const c = require('compact-encoding')
10
+
11
+ module.exports = class BlindPeer {
12
+ constructor (storage) {
13
+ this.db = HyperDB.rocks(path.join(storage, 'hyperdb'), definition)
14
+ this.store = new Corestore(path.join(storage, 'corestore'))
15
+ this.store.on('core-open', this._oncoreopen.bind(this))
16
+ this.swarm = null
17
+ }
18
+
19
+ _onconnection (connection) {
20
+ this.store.replicate(connection)
21
+
22
+ const rpc = new ProtomuxRPC(connection, {
23
+ id: this.swarm.keyPair.publicKey,
24
+ valueEncoding: c.none
25
+ })
26
+
27
+ rpc.respond('add-mailbox', {
28
+ requestEncoding: schema.resolveStruct('@blind-peer/request-mailbox'),
29
+ responseEncoding: schema.resolveStruct('@blind-peer/response-mailbox')
30
+ }, this._onrpcadd.bind(this))
31
+
32
+ rpc.respond('post', {
33
+ requestEncoding: schema.resolveStruct('@blind-peer/request-post'),
34
+ responseEncoding: schema.resolveStruct('@blind-peer/response-post')
35
+ }, this._onrpcpost.bind(this))
36
+ }
37
+
38
+ async _onrpcadd (req) {
39
+ const res = await this.add(req)
40
+
41
+ return {
42
+ autobase: res.autobase,
43
+ writer: res.writer,
44
+ open: !!res.blockEncryptionKey // TODO: get rid of the encryption for these guys with a manifest upgrade, then no attacks cause self-described
45
+ }
46
+ }
47
+
48
+ async _onrpcpost (req) {
49
+ return await this.post(req)
50
+ }
51
+
52
+ async _oncoreopen (core) {
53
+ try {
54
+ const entry = await this.db.get('@blind-peer/mailbox', { autobase: core.key })
55
+ if (!entry || !entry.blockEncryptionKey) return
56
+
57
+ const w = new AutobaseLightWriter(this.store.namespace(entry.autobase), entry.autobase, {
58
+ active: false,
59
+ blockEncryptionKey: entry.blockEncryptionKey,
60
+ valueEncoding: c.json
61
+ })
62
+
63
+ for (const peer of core.peers) {
64
+ w.local.replicate(peer.stream)
65
+ }
66
+
67
+ core.on('peer-add', (peer) => {
68
+ w.local.replicate(peer.stream)
69
+ })
70
+
71
+ core.on('close', () => {
72
+ w.close().catch(noop)
73
+ })
74
+ } catch (err) {
75
+ console.log(err)
76
+ }
77
+ }
78
+
79
+ get publicKey () {
80
+ return this.swarm.server.publicKey
81
+ }
82
+
83
+ async listen ({ bootstrap } = {}) {
84
+ this.swarm = new Hyperswarm({
85
+ keyPair: await this.store.createKeyPair('blind-mailbox'),
86
+ bootstrap
87
+ })
88
+ this.swarm.on('connection', this._onconnection.bind(this))
89
+ return this.swarm.listen()
90
+ }
91
+
92
+ async get ({ autobase }) {
93
+ return await this.db.get('@blind-peer/mailbox', { autobase })
94
+ }
95
+
96
+ async add ({ autobase, blockEncryptionKey = null }) {
97
+ const prev = await this.db.get('@blind-peer/mailbox', { autobase })
98
+
99
+ if (prev) {
100
+ if (prev.blockEncryptionKey) return prev
101
+ prev.blockEncryptionKey = blockEncryptionKey
102
+ await this.db.insert('@blind-peer/mailbox', prev)
103
+ await this.db.flush()
104
+ return prev
105
+ }
106
+
107
+ const w = new AutobaseLightWriter(this.store.namespace(autobase), autobase, { active: false })
108
+ await w.ready()
109
+ const entry = { autobase, writer: w.local.key, blockEncryptionKey }
110
+ await this.db.insert('@blind-peer/mailbox', entry)
111
+ await this.db.flush()
112
+ await w.close()
113
+
114
+ return entry
115
+ }
116
+
117
+ async post ({ autobase, message }) {
118
+ const entry = await this.db.get('@blind-peer/mailbox', { autobase })
119
+ if (!entry || !entry.blockEncryptionKey) return false
120
+
121
+ const w = new AutobaseLightWriter(this.store.namespace(autobase), autobase, {
122
+ active: false,
123
+ blockEncryptionKey: entry.blockEncryptionKey,
124
+ valueEncoding: c.json
125
+ })
126
+ await w.append({ mailbox: true, text: message })
127
+ const length = w.local.length
128
+ await w.close()
129
+
130
+ return { length }
131
+ }
132
+
133
+ async close () {
134
+ if (this.swarm !== null) await this.swarm.destroy()
135
+ await this.store.close()
136
+ }
137
+ }
138
+
139
+ function noop () {}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "blind-peer",
3
+ "version": "0.0.1",
4
+ "description": "WIP - nothing to see here",
5
+ "main": "index.js",
6
+ "dependencies": {
7
+ "autobase-light-writer": "^1.1.0",
8
+ "corestore": "^6.18.4",
9
+ "hyperdb": "^4.1.2",
10
+ "hyperschema": "^1.0.3",
11
+ "hyperswarm": "^4.8.4",
12
+ "protomux-rpc": "^1.6.0"
13
+ },
14
+ "devDependencies": {
15
+ "debounceify": "^1.1.0",
16
+ "standard": "^17.1.2"
17
+ },
18
+ "scripts": {
19
+ "test": "standard"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/mafintosh/blind-peer.git"
24
+ },
25
+ "author": "Holepunch Inc.",
26
+ "license": "Apache-2.0",
27
+ "bugs": {
28
+ "url": "https://github.com/mafintosh/blind-peer/issues"
29
+ },
30
+ "homepage": "https://github.com/mafintosh/blind-peer"
31
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "version": 0,
3
+ "offset": 0,
4
+ "schema": [
5
+ {
6
+ "name": "mailbox",
7
+ "namespace": "blind-peer",
8
+ "id": 0,
9
+ "type": 1,
10
+ "indexes": [],
11
+ "schema": "@blind-peer/mailbox",
12
+ "derived": false,
13
+ "key": [
14
+ "autobase"
15
+ ],
16
+ "trigger": null
17
+ }
18
+ ]
19
+ }
@@ -0,0 +1,83 @@
1
+ // This file is autogenerated by the hyperdb compiler
2
+ /* eslint-disable camelcase */
3
+
4
+ const { IndexEncoder, c } = require('hyperdb/runtime')
5
+
6
+ const { version, resolveStruct } = require('./messages.js')
7
+
8
+ // '@blind-peer/mailbox' collection key
9
+ const collection0_key = new IndexEncoder([
10
+ IndexEncoder.BUFFER
11
+ ], { prefix: 0 })
12
+
13
+ function collection0_indexify (record) {
14
+ const a = record.autobase
15
+ return a === undefined ? [] : [a]
16
+ }
17
+
18
+ // '@blind-peer/mailbox' reconstruction function
19
+ function collection0_reconstruct (version, keyBuf, valueBuf) {
20
+ const key = collection0_key.decode(keyBuf)
21
+ const value = c.decode(resolveStruct('@blind-peer/mailbox/value', version), valueBuf)
22
+ // TODO: This should be fully code generated
23
+ return {
24
+ autobase: key[0],
25
+ ...value
26
+ }
27
+ }
28
+ // '@blind-peer/mailbox' key reconstruction function
29
+ function collection0_reconstruct_key (keyBuf) {
30
+ const key = collection0_key.decode(keyBuf)
31
+ return {
32
+ autobase: key[0]
33
+ }
34
+ }
35
+
36
+ // '@blind-peer/mailbox'
37
+ const collection0 = {
38
+ name: '@blind-peer/mailbox',
39
+ id: 0,
40
+ encodeKey (record) {
41
+ const key = [record.autobase]
42
+ return collection0_key.encode(key)
43
+ },
44
+ encodeKeyRange ({ gt, lt, gte, lte } = {}) {
45
+ return collection0_key.encodeRange({
46
+ gt: gt ? collection0_indexify(gt) : null,
47
+ lt: lt ? collection0_indexify(lt) : null,
48
+ gte: gte ? collection0_indexify(gte) : null,
49
+ lte: lte ? collection0_indexify(lte) : null
50
+ })
51
+ },
52
+ encodeValue (version, record) {
53
+ return c.encode(resolveStruct('@blind-peer/mailbox/value', version), record)
54
+ },
55
+ trigger: null,
56
+ reconstruct: collection0_reconstruct,
57
+ reconstructKey: collection0_reconstruct_key,
58
+ indexes: []
59
+ }
60
+
61
+ module.exports = {
62
+ version,
63
+ collections: [
64
+ collection0
65
+ ],
66
+ indexes: [
67
+ ],
68
+ resolveCollection,
69
+ resolveIndex
70
+ }
71
+
72
+ function resolveCollection (name) {
73
+ switch (name) {
74
+ case '@blind-peer/mailbox': return collection0
75
+ default: return null
76
+ }
77
+ }
78
+
79
+ function resolveIndex (name) {
80
+ switch (name) {
81
+ default: return null
82
+ }
83
+ }
@@ -0,0 +1,234 @@
1
+ // This file is autogenerated by the hyperschema compiler
2
+ // Schema Version: 1
3
+ /* eslint-disable camelcase */
4
+ /* eslint-disable quotes */
5
+
6
+ const VERSION = 1
7
+ const { c } = require('hyperschema/runtime')
8
+
9
+ // eslint-disable-next-line no-unused-vars
10
+ let version = VERSION
11
+
12
+ // @blind-peer/request-mailbox
13
+ const encoding0 = {
14
+ preencode (state, m) {
15
+ let flags = 0
16
+ if (m.blockEncryptionKey) flags |= 1
17
+
18
+ c.fixed32.preencode(state, m.autobase)
19
+ c.uint.preencode(state, flags)
20
+
21
+ if (m.blockEncryptionKey) c.fixed32.preencode(state, m.blockEncryptionKey)
22
+ },
23
+ encode (state, m) {
24
+ let flags = 0
25
+ if (m.blockEncryptionKey) flags |= 1
26
+
27
+ c.fixed32.encode(state, m.autobase)
28
+ c.uint.encode(state, flags)
29
+
30
+ if (m.blockEncryptionKey) c.fixed32.encode(state, m.blockEncryptionKey)
31
+ },
32
+ decode (state) {
33
+ const res = {}
34
+ res.autobase = null
35
+ res.blockEncryptionKey = null
36
+
37
+ res.autobase = c.fixed32.decode(state)
38
+
39
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
40
+ if ((flags & 1) !== 0) res.blockEncryptionKey = c.fixed32.decode(state)
41
+
42
+ return res
43
+ }
44
+ }
45
+
46
+ // @blind-peer/response-mailbox
47
+ const encoding1 = {
48
+ preencode (state, m) {
49
+ let flags = 0
50
+ if (m.open) flags |= 1
51
+
52
+ c.fixed32.preencode(state, m.autobase)
53
+ c.fixed32.preencode(state, m.writer)
54
+ c.uint.preencode(state, flags)
55
+ },
56
+ encode (state, m) {
57
+ let flags = 0
58
+ if (m.open) flags |= 1
59
+
60
+ c.fixed32.encode(state, m.autobase)
61
+ c.fixed32.encode(state, m.writer)
62
+ c.uint.encode(state, flags)
63
+ },
64
+ decode (state) {
65
+ const res = {}
66
+ res.autobase = null
67
+ res.writer = null
68
+ res.open = false
69
+
70
+ res.autobase = c.fixed32.decode(state)
71
+ res.writer = c.fixed32.decode(state)
72
+
73
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
74
+ if ((flags & 1) !== 0) res.open = true
75
+
76
+ return res
77
+ }
78
+ }
79
+
80
+ // @blind-peer/request-post
81
+ const encoding2 = {
82
+ preencode (state, m) {
83
+ let flags = 0
84
+ if (m.message) flags |= 1
85
+
86
+ c.fixed32.preencode(state, m.autobase)
87
+ c.uint.preencode(state, flags)
88
+
89
+ if (m.message) c.string.preencode(state, m.message)
90
+ },
91
+ encode (state, m) {
92
+ let flags = 0
93
+ if (m.message) flags |= 1
94
+
95
+ c.fixed32.encode(state, m.autobase)
96
+ c.uint.encode(state, flags)
97
+
98
+ if (m.message) c.string.encode(state, m.message)
99
+ },
100
+ decode (state) {
101
+ const res = {}
102
+ res.autobase = null
103
+ res.message = null
104
+
105
+ res.autobase = c.fixed32.decode(state)
106
+
107
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
108
+ if ((flags & 1) !== 0) res.message = c.string.decode(state)
109
+
110
+ return res
111
+ }
112
+ }
113
+
114
+ // @blind-peer/response-post
115
+ const encoding3 = {
116
+ preencode (state, m) {
117
+ c.uint.preencode(state, m.length)
118
+ },
119
+ encode (state, m) {
120
+ c.uint.encode(state, m.length)
121
+ },
122
+ decode (state) {
123
+ const res = {}
124
+ res.length = 0
125
+
126
+ res.length = c.uint.decode(state)
127
+
128
+ return res
129
+ }
130
+ }
131
+
132
+ // @blind-peer/mailbox
133
+ const encoding4 = {
134
+ preencode (state, m) {
135
+ let flags = 0
136
+ if (m.blockEncryptionKey) flags |= 1
137
+
138
+ c.fixed32.preencode(state, m.autobase)
139
+ c.fixed32.preencode(state, m.writer)
140
+ c.uint.preencode(state, flags)
141
+
142
+ if (m.blockEncryptionKey) c.fixed32.preencode(state, m.blockEncryptionKey)
143
+ },
144
+ encode (state, m) {
145
+ let flags = 0
146
+ if (m.blockEncryptionKey) flags |= 1
147
+
148
+ c.fixed32.encode(state, m.autobase)
149
+ c.fixed32.encode(state, m.writer)
150
+ c.uint.encode(state, flags)
151
+
152
+ if (m.blockEncryptionKey) c.fixed32.encode(state, m.blockEncryptionKey)
153
+ },
154
+ decode (state) {
155
+ const res = {}
156
+ res.autobase = null
157
+ res.writer = null
158
+ res.blockEncryptionKey = null
159
+
160
+ res.autobase = c.fixed32.decode(state)
161
+ res.writer = c.fixed32.decode(state)
162
+
163
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
164
+ if ((flags & 1) !== 0) res.blockEncryptionKey = c.fixed32.decode(state)
165
+
166
+ return res
167
+ }
168
+ }
169
+
170
+ // @blind-peer/mailbox/value
171
+ const encoding5 = {
172
+ preencode (state, m) {
173
+ let flags = 0
174
+ if (m.blockEncryptionKey) flags |= 1
175
+
176
+ c.fixed32.preencode(state, m.writer)
177
+ c.uint.preencode(state, flags)
178
+
179
+ if (m.blockEncryptionKey) c.fixed32.preencode(state, m.blockEncryptionKey)
180
+ },
181
+ encode (state, m) {
182
+ let flags = 0
183
+ if (m.blockEncryptionKey) flags |= 1
184
+
185
+ c.fixed32.encode(state, m.writer)
186
+ c.uint.encode(state, flags)
187
+
188
+ if (m.blockEncryptionKey) c.fixed32.encode(state, m.blockEncryptionKey)
189
+ },
190
+ decode (state) {
191
+ const res = {}
192
+ res.writer = null
193
+ res.blockEncryptionKey = null
194
+
195
+ res.writer = c.fixed32.decode(state)
196
+
197
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
198
+ if ((flags & 1) !== 0) res.blockEncryptionKey = c.fixed32.decode(state)
199
+
200
+ return res
201
+ }
202
+ }
203
+
204
+ function getStructByName (name) {
205
+ switch (name) {
206
+ case '@blind-peer/request-mailbox': return encoding0
207
+ case '@blind-peer/response-mailbox': return encoding1
208
+ case '@blind-peer/request-post': return encoding2
209
+ case '@blind-peer/response-post': return encoding3
210
+ case '@blind-peer/mailbox': return encoding4
211
+ case '@blind-peer/mailbox/value': return encoding5
212
+ default: throw new Error('Encoder not found ' + name)
213
+ }
214
+ }
215
+
216
+ function resolveStruct (name, v = VERSION) {
217
+ const enc = getStructByName(name)
218
+ return {
219
+ preencode (state, m) {
220
+ version = v
221
+ enc.preencode(state, m)
222
+ },
223
+ encode (state, m) {
224
+ version = v
225
+ enc.encode(state, m)
226
+ },
227
+ decode (state) {
228
+ version = v
229
+ return enc.decode(state)
230
+ }
231
+ }
232
+ }
233
+
234
+ module.exports = { resolveStruct, version }
@@ -0,0 +1,199 @@
1
+ // This file is autogenerated by the hyperschema compiler
2
+ // Schema Version: 1
3
+ /* eslint-disable camelcase */
4
+ /* eslint-disable quotes */
5
+
6
+ const VERSION = 1
7
+ const { c } = require('hyperschema/runtime')
8
+
9
+ // eslint-disable-next-line no-unused-vars
10
+ let version = VERSION
11
+
12
+ // @blind-peer/request-mailbox
13
+ const encoding0 = {
14
+ preencode (state, m) {
15
+ let flags = 0
16
+ if (m.blockEncryptionKey) flags |= 1
17
+
18
+ c.fixed32.preencode(state, m.autobase)
19
+ c.uint.preencode(state, flags)
20
+
21
+ if (m.blockEncryptionKey) c.fixed32.preencode(state, m.blockEncryptionKey)
22
+ },
23
+ encode (state, m) {
24
+ let flags = 0
25
+ if (m.blockEncryptionKey) flags |= 1
26
+
27
+ c.fixed32.encode(state, m.autobase)
28
+ c.uint.encode(state, flags)
29
+
30
+ if (m.blockEncryptionKey) c.fixed32.encode(state, m.blockEncryptionKey)
31
+ },
32
+ decode (state) {
33
+ const res = {}
34
+ res.autobase = null
35
+ res.blockEncryptionKey = null
36
+
37
+ res.autobase = c.fixed32.decode(state)
38
+
39
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
40
+ if ((flags & 1) !== 0) res.blockEncryptionKey = c.fixed32.decode(state)
41
+
42
+ return res
43
+ }
44
+ }
45
+
46
+ // @blind-peer/response-mailbox
47
+ const encoding1 = {
48
+ preencode (state, m) {
49
+ let flags = 0
50
+ if (m.open) flags |= 1
51
+
52
+ c.fixed32.preencode(state, m.autobase)
53
+ c.fixed32.preencode(state, m.writer)
54
+ c.uint.preencode(state, flags)
55
+ },
56
+ encode (state, m) {
57
+ let flags = 0
58
+ if (m.open) flags |= 1
59
+
60
+ c.fixed32.encode(state, m.autobase)
61
+ c.fixed32.encode(state, m.writer)
62
+ c.uint.encode(state, flags)
63
+ },
64
+ decode (state) {
65
+ const res = {}
66
+ res.autobase = null
67
+ res.writer = null
68
+ res.open = false
69
+
70
+ res.autobase = c.fixed32.decode(state)
71
+ res.writer = c.fixed32.decode(state)
72
+
73
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
74
+ if ((flags & 1) !== 0) res.open = true
75
+
76
+ return res
77
+ }
78
+ }
79
+
80
+ // @blind-peer/request-post
81
+ const encoding2 = {
82
+ preencode (state, m) {
83
+ let flags = 0
84
+ if (m.message) flags |= 1
85
+
86
+ c.fixed32.preencode(state, m.autobase)
87
+ c.uint.preencode(state, flags)
88
+
89
+ if (m.message) c.string.preencode(state, m.message)
90
+ },
91
+ encode (state, m) {
92
+ let flags = 0
93
+ if (m.message) flags |= 1
94
+
95
+ c.fixed32.encode(state, m.autobase)
96
+ c.uint.encode(state, flags)
97
+
98
+ if (m.message) c.string.encode(state, m.message)
99
+ },
100
+ decode (state) {
101
+ const res = {}
102
+ res.autobase = null
103
+ res.message = null
104
+
105
+ res.autobase = c.fixed32.decode(state)
106
+
107
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
108
+ if ((flags & 1) !== 0) res.message = c.string.decode(state)
109
+
110
+ return res
111
+ }
112
+ }
113
+
114
+ // @blind-peer/response-post
115
+ const encoding3 = {
116
+ preencode (state, m) {
117
+ c.uint.preencode(state, m.length)
118
+ },
119
+ encode (state, m) {
120
+ c.uint.encode(state, m.length)
121
+ },
122
+ decode (state) {
123
+ const res = {}
124
+ res.length = 0
125
+
126
+ res.length = c.uint.decode(state)
127
+
128
+ return res
129
+ }
130
+ }
131
+
132
+ // @blind-peer/mailbox
133
+ const encoding4 = {
134
+ preencode (state, m) {
135
+ let flags = 0
136
+ if (m.blockEncryptionKey) flags |= 1
137
+
138
+ c.fixed32.preencode(state, m.autobase)
139
+ c.fixed32.preencode(state, m.writer)
140
+ c.uint.preencode(state, flags)
141
+
142
+ if (m.blockEncryptionKey) c.fixed32.preencode(state, m.blockEncryptionKey)
143
+ },
144
+ encode (state, m) {
145
+ let flags = 0
146
+ if (m.blockEncryptionKey) flags |= 1
147
+
148
+ c.fixed32.encode(state, m.autobase)
149
+ c.fixed32.encode(state, m.writer)
150
+ c.uint.encode(state, flags)
151
+
152
+ if (m.blockEncryptionKey) c.fixed32.encode(state, m.blockEncryptionKey)
153
+ },
154
+ decode (state) {
155
+ const res = {}
156
+ res.autobase = null
157
+ res.writer = null
158
+ res.blockEncryptionKey = null
159
+
160
+ res.autobase = c.fixed32.decode(state)
161
+ res.writer = c.fixed32.decode(state)
162
+
163
+ const flags = state.start < state.end ? c.uint.decode(state) : 0
164
+ if ((flags & 1) !== 0) res.blockEncryptionKey = c.fixed32.decode(state)
165
+
166
+ return res
167
+ }
168
+ }
169
+
170
+ function getStructByName (name) {
171
+ switch (name) {
172
+ case '@blind-peer/request-mailbox': return encoding0
173
+ case '@blind-peer/response-mailbox': return encoding1
174
+ case '@blind-peer/request-post': return encoding2
175
+ case '@blind-peer/response-post': return encoding3
176
+ case '@blind-peer/mailbox': return encoding4
177
+ default: throw new Error('Encoder not found ' + name)
178
+ }
179
+ }
180
+
181
+ function resolveStruct (name, v = VERSION) {
182
+ const enc = getStructByName(name)
183
+ return {
184
+ preencode (state, m) {
185
+ version = v
186
+ enc.preencode(state, m)
187
+ },
188
+ encode (state, m) {
189
+ version = v
190
+ enc.encode(state, m)
191
+ },
192
+ decode (state) {
193
+ version = v
194
+ return enc.decode(state)
195
+ }
196
+ }
197
+ }
198
+
199
+ module.exports = { resolveStruct, version }
@@ -0,0 +1,107 @@
1
+ {
2
+ "version": 1,
3
+ "schema": [
4
+ {
5
+ "name": "request-mailbox",
6
+ "namespace": "blind-peer",
7
+ "compact": false,
8
+ "flagsPosition": 1,
9
+ "fields": [
10
+ {
11
+ "name": "autobase",
12
+ "required": true,
13
+ "type": "fixed32",
14
+ "version": 1
15
+ },
16
+ {
17
+ "name": "blockEncryptionKey",
18
+ "type": "fixed32",
19
+ "version": 1
20
+ }
21
+ ]
22
+ },
23
+ {
24
+ "name": "response-mailbox",
25
+ "namespace": "blind-peer",
26
+ "compact": false,
27
+ "flagsPosition": 2,
28
+ "fields": [
29
+ {
30
+ "name": "autobase",
31
+ "required": true,
32
+ "type": "fixed32",
33
+ "version": 1
34
+ },
35
+ {
36
+ "name": "writer",
37
+ "required": true,
38
+ "type": "fixed32",
39
+ "version": 1
40
+ },
41
+ {
42
+ "name": "open",
43
+ "type": "bool",
44
+ "version": 1
45
+ }
46
+ ]
47
+ },
48
+ {
49
+ "name": "request-post",
50
+ "namespace": "blind-peer",
51
+ "compact": false,
52
+ "flagsPosition": 1,
53
+ "fields": [
54
+ {
55
+ "name": "autobase",
56
+ "required": true,
57
+ "type": "fixed32",
58
+ "version": 1
59
+ },
60
+ {
61
+ "name": "message",
62
+ "type": "string",
63
+ "version": 1
64
+ }
65
+ ]
66
+ },
67
+ {
68
+ "name": "response-post",
69
+ "namespace": "blind-peer",
70
+ "compact": false,
71
+ "flagsPosition": -1,
72
+ "fields": [
73
+ {
74
+ "name": "length",
75
+ "required": true,
76
+ "type": "uint",
77
+ "version": 1
78
+ }
79
+ ]
80
+ },
81
+ {
82
+ "name": "mailbox",
83
+ "namespace": "blind-peer",
84
+ "compact": false,
85
+ "flagsPosition": 2,
86
+ "fields": [
87
+ {
88
+ "name": "autobase",
89
+ "required": true,
90
+ "type": "fixed32",
91
+ "version": 1
92
+ },
93
+ {
94
+ "name": "writer",
95
+ "required": true,
96
+ "type": "fixed32",
97
+ "version": 1
98
+ },
99
+ {
100
+ "name": "blockEncryptionKey",
101
+ "type": "fixed32",
102
+ "version": 1
103
+ }
104
+ ]
105
+ }
106
+ ]
107
+ }