hypercore 10.0.0-alpha.30 → 10.0.0-alpha.33
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/index.js +13 -10
- package/lib/core.js +25 -14
- package/lib/replicator.js +4 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -65,7 +65,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
65
65
|
this.opened = false
|
|
66
66
|
this.closed = false
|
|
67
67
|
this.sessions = opts._sessions || [this]
|
|
68
|
-
this.
|
|
68
|
+
this.auth = opts.auth || null
|
|
69
69
|
this.autoClose = !!opts.autoClose
|
|
70
70
|
|
|
71
71
|
this.closing = null
|
|
@@ -168,13 +168,13 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
_passCapabilities (o) {
|
|
171
|
-
if (!this.
|
|
171
|
+
if (!this.auth) this.auth = o.auth
|
|
172
172
|
this.crypto = o.crypto
|
|
173
173
|
this.key = o.key
|
|
174
174
|
this.core = o.core
|
|
175
175
|
this.replicator = o.replicator
|
|
176
176
|
this.encryption = o.encryption
|
|
177
|
-
this.writable = !!this.sign
|
|
177
|
+
this.writable = !!(this.auth && this.auth.sign)
|
|
178
178
|
this.autoClose = o.autoClose
|
|
179
179
|
}
|
|
180
180
|
|
|
@@ -204,10 +204,12 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
204
204
|
// but we only do this to validate the keypair to help catch bugs so yolo
|
|
205
205
|
if (this.key && keyPair) keyPair.publicKey = this.key
|
|
206
206
|
|
|
207
|
-
if (opts.
|
|
208
|
-
this.
|
|
207
|
+
if (opts.auth) {
|
|
208
|
+
this.auth = opts.auth
|
|
209
|
+
} else if (opts.sign) {
|
|
210
|
+
this.auth = Core.createAuth(this.crypto, keyPair, opts)
|
|
209
211
|
} else if (keyPair && keyPair.secretKey) {
|
|
210
|
-
this.
|
|
212
|
+
this.auth = Core.createAuth(this.crypto, keyPair)
|
|
211
213
|
}
|
|
212
214
|
|
|
213
215
|
if (isFirst) {
|
|
@@ -219,8 +221,8 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
219
221
|
}
|
|
220
222
|
}
|
|
221
223
|
|
|
222
|
-
if (!this.
|
|
223
|
-
this.writable = !!this.sign
|
|
224
|
+
if (!this.auth) this.auth = this.core.defaultAuth
|
|
225
|
+
this.writable = !!this.auth.sign
|
|
224
226
|
|
|
225
227
|
if (opts.valueEncoding) {
|
|
226
228
|
this.valueEncoding = c.from(codecs(opts.valueEncoding))
|
|
@@ -249,6 +251,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
249
251
|
keyPair,
|
|
250
252
|
crypto: this.crypto,
|
|
251
253
|
legacy: opts.legacy,
|
|
254
|
+
auth: opts.auth,
|
|
252
255
|
onupdate: this._oncoreupdate.bind(this)
|
|
253
256
|
})
|
|
254
257
|
|
|
@@ -543,7 +546,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
543
546
|
if (this.writable === false) throw new Error('Core is not writable')
|
|
544
547
|
|
|
545
548
|
if (fork === -1) fork = this.core.tree.fork + 1
|
|
546
|
-
await this.core.truncate(newLength, fork, this.
|
|
549
|
+
await this.core.truncate(newLength, fork, this.auth)
|
|
547
550
|
|
|
548
551
|
// TODO: Should propagate from an event triggered by the oplog
|
|
549
552
|
this.replicator.updateAll()
|
|
@@ -565,7 +568,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
565
568
|
}
|
|
566
569
|
}
|
|
567
570
|
|
|
568
|
-
return await this.core.append(buffers, this.
|
|
571
|
+
return await this.core.append(buffers, this.auth, { preappend })
|
|
569
572
|
}
|
|
570
573
|
|
|
571
574
|
async treeHash (length) {
|
package/lib/core.js
CHANGED
|
@@ -8,7 +8,7 @@ const Bitfield = require('./bitfield')
|
|
|
8
8
|
const m = require('./messages')
|
|
9
9
|
|
|
10
10
|
module.exports = class Core {
|
|
11
|
-
constructor (header, crypto, oplog, tree, blocks, bitfield,
|
|
11
|
+
constructor (header, crypto, oplog, tree, blocks, bitfield, auth, legacy, onupdate) {
|
|
12
12
|
this.onupdate = onupdate
|
|
13
13
|
this.header = header
|
|
14
14
|
this.crypto = crypto
|
|
@@ -16,7 +16,7 @@ module.exports = class Core {
|
|
|
16
16
|
this.tree = tree
|
|
17
17
|
this.blocks = blocks
|
|
18
18
|
this.bitfield = bitfield
|
|
19
|
-
this.
|
|
19
|
+
this.defaultAuth = auth
|
|
20
20
|
this.truncating = 0
|
|
21
21
|
|
|
22
22
|
this._maxOplogSize = 65536
|
|
@@ -51,10 +51,21 @@ module.exports = class Core {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
static createAuth (crypto, { publicKey, secretKey }, opts = {}) {
|
|
55
|
+
if (secretKey && !crypto.validateKeyPair({ publicKey, secretKey })) throw new Error('Invalid key pair')
|
|
56
|
+
|
|
57
|
+
const sign = opts.sign
|
|
58
|
+
? opts.sign
|
|
59
|
+
: secretKey
|
|
60
|
+
? (signable) => crypto.sign(signable, secretKey)
|
|
61
|
+
: undefined
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
sign,
|
|
65
|
+
verify (signable, signature) {
|
|
66
|
+
return crypto.verify(signable, signature, publicKey)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
static async resume (oplogFile, treeFile, bitfieldFile, dataFile, opts) {
|
|
@@ -116,7 +127,7 @@ module.exports = class Core {
|
|
|
116
127
|
await bitfield.clear()
|
|
117
128
|
}
|
|
118
129
|
|
|
119
|
-
const
|
|
130
|
+
const auth = opts.auth || this.createAuth(crypto, header.signer)
|
|
120
131
|
|
|
121
132
|
for (const e of entries) {
|
|
122
133
|
if (e.userData) {
|
|
@@ -147,7 +158,7 @@ module.exports = class Core {
|
|
|
147
158
|
}
|
|
148
159
|
}
|
|
149
160
|
|
|
150
|
-
return new this(header, crypto, oplog, tree, blocks, bitfield,
|
|
161
|
+
return new this(header, crypto, oplog, tree, blocks, bitfield, auth, !!opts.legacy, opts.onupdate || noop)
|
|
151
162
|
}
|
|
152
163
|
|
|
153
164
|
_shouldFlush () {
|
|
@@ -212,13 +223,13 @@ module.exports = class Core {
|
|
|
212
223
|
}
|
|
213
224
|
}
|
|
214
225
|
|
|
215
|
-
async truncate (length, fork,
|
|
226
|
+
async truncate (length, fork, auth = this.defaultAuth) {
|
|
216
227
|
this.truncating++
|
|
217
228
|
await this._mutex.lock()
|
|
218
229
|
|
|
219
230
|
try {
|
|
220
231
|
const batch = await this.tree.truncate(length, fork)
|
|
221
|
-
batch.signature = await sign(batch.signable())
|
|
232
|
+
batch.signature = await auth.sign(batch.signable())
|
|
222
233
|
await this._truncate(batch, null)
|
|
223
234
|
} finally {
|
|
224
235
|
this.truncating--
|
|
@@ -226,7 +237,7 @@ module.exports = class Core {
|
|
|
226
237
|
}
|
|
227
238
|
}
|
|
228
239
|
|
|
229
|
-
async append (values,
|
|
240
|
+
async append (values, auth = this.defaultAuth, hooks = {}) {
|
|
230
241
|
await this._mutex.lock()
|
|
231
242
|
|
|
232
243
|
try {
|
|
@@ -238,7 +249,7 @@ module.exports = class Core {
|
|
|
238
249
|
for (const val of values) batch.append(val)
|
|
239
250
|
|
|
240
251
|
const hash = batch.hash()
|
|
241
|
-
batch.signature = await sign(this._legacy ? batch.signableLegacy(hash) : batch.signable(hash))
|
|
252
|
+
batch.signature = await auth.sign(this._legacy ? batch.signableLegacy(hash) : batch.signable(hash))
|
|
242
253
|
|
|
243
254
|
const entry = {
|
|
244
255
|
userData: null,
|
|
@@ -270,9 +281,9 @@ module.exports = class Core {
|
|
|
270
281
|
}
|
|
271
282
|
}
|
|
272
283
|
|
|
273
|
-
_signed (batch, hash) {
|
|
284
|
+
_signed (batch, hash, auth = this.defaultAuth) {
|
|
274
285
|
const signable = this._legacy ? batch.signableLegacy(hash) : batch.signable(hash)
|
|
275
|
-
return
|
|
286
|
+
return auth.verify(signable, batch.signature)
|
|
276
287
|
}
|
|
277
288
|
|
|
278
289
|
async _verifyExclusive ({ batch, bitfield, value, from }) {
|
package/lib/replicator.js
CHANGED
|
@@ -1382,6 +1382,10 @@ module.exports = class Replicator {
|
|
|
1382
1382
|
}
|
|
1383
1383
|
|
|
1384
1384
|
_updatePeerNonPrimary (peer) {
|
|
1385
|
+
if (peer.inflight >= peer.maxInflight) {
|
|
1386
|
+
return false
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1385
1389
|
const ranges = new RandomIterator(this._ranges)
|
|
1386
1390
|
|
|
1387
1391
|
for (const r of ranges) {
|