-
Notifications
You must be signed in to change notification settings - Fork 0
/
key-path.js
51 lines (44 loc) · 1.3 KB
/
key-path.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
const { toHexString } = require('ara-util/transform')
const { blake2b } = require('ara-crypto')
const { resolve } = require('path')
const toilet = require('toiletdb')
const mkdirp = require('mkdirp')
const pify = require('pify')
const rc = require('./rc')()
const fs = require('fs')
function createAFSKeyPath(did) {
const { root } = rc.network.afs.archive
if (null == did || 'string' !== typeof did) {
throw new TypeError('Expecting non-empty string for id.')
}
try {
fs.accessSync(root)
} catch (err) {
fs.mkdirSync(root)
}
const hash = blake2b(Buffer.from(did, 'hex'))
return resolve(root, toHexString(hash))
}
function createIdentityKeyPathFromPublicKey(publicKey) {
const { root } = rc.network.identity
if (Array.isArray(publicKey) && 0 < publicKey.length) {
const { publicKeyHex } = publicKey[0]
publicKey = Buffer.from(publicKeyHex, 'hex')
}
if ('string' === typeof publicKey) {
publicKey = Buffer.from(publicKey, 'hex')
}
const hash = toHexString(blake2b(publicKey))
return resolve(root, hash)
}
async function getCache() {
await pify(mkdirp)(rc.network.afs.archive.root)
const store = toilet(rc.network.afs.archive.store)
await pify(store.open)()
return store
}
module.exports = {
getCache,
createAFSKeyPath,
createIdentityKeyPathFromPublicKey
}