Browse Source

allow keypair to be passed to writer method

Kieran Gibb 2 years ago
parent
commit
8631f3a240
No known key found for this signature in database
3 changed files with 42 additions and 3 deletions
  1. 4
    1
      README.md
  2. 12
    2
      index.js
  3. 26
    0
      test/basic.js

+ 4
- 1
README.md View File

@@ -67,7 +67,7 @@ and are the same as
67 67
 Valid `opts` include:
68 68
 - `opts.key` (string): optional encryption key to use during replication.
69 69
 
70
-### multi.writer([name, ]cb)
70
+### multi.writer([name], [options], cb)
71 71
 
72 72
 If no `name` is given, a new local writeable feed is created and returned via
73 73
 `cb`.
@@ -83,6 +83,9 @@ var content = multi.writer('content')  // created if doesn't exist
83 83
 main === multi.writer('main')          // => true
84 84
 ```
85 85
 
86
+`options` is an optional object which may contain:
87
+- `options.keypair` - an object with a custom keypair for the new writer.  This should have properties `keypair.publicKey` and `keypair.secretKey`, both of which should be buffers.
88
+
86 89
 ### var feeds = multi.feeds()
87 90
 
88 91
 An array of all hypercores in the multifeed. Check a feed's `key` to

+ 12
- 2
index.js View File

@@ -173,12 +173,20 @@ Multifeed.prototype._loadFeeds = function (cb) {
173 173
   next(0)
174 174
 }
175 175
 
176
-Multifeed.prototype.writer = function (name, cb) {
176
+Multifeed.prototype.writer = function (name, opts, cb) {
177 177
   if (typeof name === 'function' && !cb) {
178 178
     cb = name
179 179
     name = undefined
180
+    opts = {}
180 181
   }
182
+
183
+  if (typeof opts === 'function' && !cb) {
184
+    cb = opts
185
+    opts = {}
186
+  }
187
+
181 188
   var self = this
189
+  var keypair = opts.keypair
182 190
 
183 191
   this.ready(function () {
184 192
     // Short-circuit if already loaded
@@ -204,7 +212,9 @@ Multifeed.prototype.writer = function (name, cb) {
204 212
           return
205 213
         }
206 214
 
207
-        var feed = self._hypercore(storage, self._opts)
215
+        var feed = keypair
216
+          ? self._hypercore(storage, keypair.publicKey, Object.assign(self._opts, { secretKey: keypair.secretKey }))
217
+          : self._hypercore(storage, self._opts)
208 218
 
209 219
         feed.ready(function () {
210 220
           self._addFeed(feed, String(idx))

+ 26
- 0
test/basic.js View File

@@ -310,3 +310,29 @@ test('replicate slow-to-open multifeeds', function (t) {
310 310
     t.equals(feedEvents2, 2)
311 311
   }
312 312
 })
313
+
314
+test('can create writer with custom keypair', function (t) {
315
+  t.plan(7)
316
+
317
+  const keypair = {
318
+    publicKey: Buffer.from('ce1f0639f6559736d5c98f9df9af111ff20f0980674297e4eb40cc8f00f1157e', 'hex'),
319
+    secretKey: Buffer.from('559f807745b2dd136ec96ebdffa81f0631bfc4bc6ee4bc86f5666b24db91665ace1f0639f6559736d5c98f9df9af111ff20f0980674297e4eb40cc8f00f1157e', 'hex')
320
+  }
321
+
322
+  var multi = multifeed(hypercore, ram, { valueEncoding: 'json' })
323
+  multi.ready(function () {
324
+    multi.writer('moose', { keypair }, function (err, w) {
325
+      t.error(err, 'valid writer created')
326
+      t.same(w.key.toString('hex'), keypair.publicKey.toString('hex'), 'public keys match')
327
+      t.same(w.secretKey.toString('hex'), keypair.secretKey.toString('hex'), 'secret keys match')
328
+      w.append('foo', function (err) {
329
+        t.error(err, 'no error when appending to feed')
330
+        w.get(0, function (err, data) {
331
+          t.error(err)
332
+          t.equals(data.toString(), 'foo')
333
+          t.deepEquals(multi.feeds(), [w])
334
+        })
335
+      })
336
+    })
337
+  })
338
+})