Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 4026069

Browse files
committed
feat: encoding key's name to base32
1 parent 79a7fe8 commit 4026069

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/core/components/pre-start.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const waterfall = require('async/waterfall')
77
const Keychain = require('libp2p-keychain')
88
const mergeOptions = require('merge-options')
99
const NoKeychain = require('./no-keychain')
10+
const KeyTransformDatastore = require('datastore-core').KeytransformDatastore
11+
const keychainTransformer = require('../../utils/keychain-encoder')
12+
1013
/*
1114
* Load stuff from Repo into memory
1215
*/
@@ -49,7 +52,9 @@ module.exports = function preStart (self) {
4952
// most likely an init or upgrade has happened
5053
} else if (pass) {
5154
const keychainOptions = Object.assign({ passPhrase: pass }, config.Keychain)
52-
self._keychain = new Keychain(self._repo.keys, keychainOptions)
55+
const keychainTransformedDatastore = new KeyTransformDatastore(self._repo.keys, keychainTransformer)
56+
self._keychain = new Keychain(keychainTransformedDatastore, keychainOptions)
57+
// self._keychain = new Keychain(self._repo.keys, keychainOptions)
5358
self.log('keychain constructed')
5459
} else {
5560
self._keychain = new NoKeychain()

src/utils/keychain-encoder.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
const base32 = require('base32.js')
3+
const Key = require('interface-datastore').Key
4+
const KEY_PREFIX = 'key_'
5+
6+
module.exports = {
7+
/**
8+
* Encode baseNamespace of a Key to base32
9+
*
10+
* @param {Key} key
11+
* @returns {Key}
12+
*
13+
* @example convert(new Key('/info/self.data'))
14+
* // => Key('/info/key_onswyzq.data')
15+
*/
16+
convert (key) {
17+
const encoder = new base32.Encoder({ type: 'rfc4648' })
18+
const baseNameBuff = Buffer.from(key.baseNamespace())
19+
const encodedBaseNamespace = KEY_PREFIX + encoder.finalize(baseNameBuff).toLowerCase()
20+
const namespaces = key.namespaces()
21+
namespaces[namespaces.length - 1] = encodedBaseNamespace // Replace the baseNamespace with encoded one
22+
return Key.withNamespaces(namespaces)
23+
},
24+
25+
/**
26+
* Decode baseNamespace of a Key from base32
27+
*
28+
* @param {Key} key
29+
* @returns {Key}
30+
*
31+
* @example invert(new Key('/info/key_onswyzq.data'))
32+
* // => Key('/info/self.data')
33+
*/
34+
invert (key) {
35+
const baseNamespace = key.baseNamespace()
36+
if (!baseNamespace.startsWith(KEY_PREFIX)) {
37+
throw Error('Unknown format of key\'s name!')
38+
}
39+
40+
const decoder = new base32.Decoder({ type: 'rfc4648' })
41+
const decodedBaseNamespace = decoder.finalize(baseNamespace.replace(KEY_PREFIX, '').toUpperCase())
42+
const namespaces = key.namespaces()
43+
namespaces[namespaces.length - 1] = decodedBaseNamespace // Replace the baseNamespace with encoded one
44+
45+
return Key.withNamespaces(namespaces)
46+
}
47+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const encoder = require('../../src/utils/keychain-encoder')
5+
const Key = require('interface-datastore').Key
6+
7+
const expect = require('chai').expect
8+
9+
function test (input, expected, fnc) {
10+
input = new Key(input)
11+
const output = fnc(input)
12+
13+
expect(output.toString()).to.equal(expected)
14+
}
15+
16+
describe('keychain-encode', () => {
17+
it('encode keys', () => {
18+
test('/self', '/key_onswyzq', encoder.convert)
19+
test('bbbba', '/key_mjrgeytb', encoder.convert)
20+
test('/some/path/self', '/some/path/key_onswyzq', encoder.convert)
21+
})
22+
it('decode keys', () => {
23+
test('/key_onswyzq', '/self', encoder.invert)
24+
test('key_mjrgeytb', '/bbbba', encoder.invert)
25+
test('/some/path/key_onswyzq', '/some/path/self', encoder.invert)
26+
})
27+
it('decode expects specific format', () => {
28+
expect(() => { encoder.invert(new Key('/some/path/onswyzq')) }).to.throw('Unknown')
29+
})
30+
})

0 commit comments

Comments
 (0)