-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.js
60 lines (54 loc) · 1.64 KB
/
keys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: LGPL-3.0-only
const crypto = require('crypto')
const ssbKeys = require('ssb-keys')
const hkdf = require('futoin-hkdf')
/**
* Operations related to keys
*/
const keys = {
/**
* Generate a seed value that can be used to derive feeds.
*/
generateSeed() {
return crypto.randomBytes(32)
},
/**
* Derive the root meta feed key from a seed.
*
* ```js
* const seed = sbot.metafeeds.keys.generateSeed()
* const mfKey = sbot.metafeeds.keys.deriveRootMetaFeedKeyFromSeed(seed)
* ```
* @param {Buffer} seed
*/
deriveRootMetaFeedKeyFromSeed(seed) {
return keys.deriveFeedKeyFromSeed(seed, 'metafeed', 'bendybutt-v1')
},
/**
* Derive a new feed key from a seed. Label must be either `metafeed` for the
* top level meta feed or a base64 encoded nonce. Feedformat can be
* `bendybutt-v1` for a meta feed or `classic` or `indexed-v1`.
*
* ```js
* const seed = sbot.metafeeds.keys.generateSeed()
* const mfKey = sbot.metafeeds.keys.deriveFeedKeyFromSeed(seed, 'metafeed')
* ```
* @param {Buffer} seed
* @param {string} label
* @param {'bendybutt-v1' | 'indexed-v1' | 'gabbygrove-v1' | 'classic'} format
* default is 'classic'
*/
deriveFeedKeyFromSeed(seed, label, format = 'classic') {
if (!label) throw new Error('label was not supplied')
const ED25519_LENGTH = 32
const derived_seed = hkdf(seed, ED25519_LENGTH, {
salt: 'ssb',
info: 'ssb-meta-feed-seed-v1:' + label,
hash: 'SHA-256',
})
return ssbKeys.generate('ed25519', derived_seed, format)
},
}
module.exports = keys