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

Commit c236111

Browse files
committed
feat: encoding key's name to base32
Related to #1937
1 parent 50f3667 commit c236111

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/core/components/pre-start.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const mergeOptions = require('merge-options')
88
const NoKeychain = require('./no-keychain')
99
const callbackify = require('callbackify')
1010
const promisify = require('promisify-es6')
11+
const KeyTransformDatastore = require('datastore-core').KeytransformDatastore
12+
const keychainTransformer = require('../../utils/keychain-encoder')
1113

1214
/*
1315
* Load stuff from Repo into memory
@@ -36,7 +38,8 @@ module.exports = function preStart (self) {
3638
// most likely an init or upgrade has happened
3739
} else if (pass) {
3840
const keychainOptions = Object.assign({ passPhrase: pass }, config.Keychain)
39-
self._keychain = new Keychain(self._repo.keys, keychainOptions)
41+
const keychainTransformedDatastore = new KeyTransformDatastore(self._repo.keys, keychainTransformer)
42+
self._keychain = new Keychain(keychainTransformedDatastore, keychainOptions)
4043
self.log('keychain constructed')
4144
} else {
4245
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] = Buffer.from(decodedBaseNamespace).toString() // Replace the baseNamespace with encoded one
44+
45+
return Key.withNamespaces(namespaces)
46+
}
47+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const encoder = require('../../src/utils/keychain-encoder')
5+
const Key = require('interface-datastore').Key
6+
const { expect } = require('interface-ipfs-core/src/utils/mocha')
7+
8+
function test (input, expected, fnc) {
9+
input = new Key(input)
10+
const output = fnc(input)
11+
12+
expect(output.toString()).to.equal(expected)
13+
}
14+
15+
describe('keychain-encode', () => {
16+
it('encode keys', () => {
17+
test('/self', '/key_onswyzq', encoder.convert)
18+
test('bbbba', '/key_mjrgeytb', encoder.convert)
19+
test('/some/path/self', '/some/path/key_onswyzq', encoder.convert)
20+
})
21+
it('decode keys', () => {
22+
test('/key_onswyzq', '/self', encoder.invert)
23+
test('key_mjrgeytb', '/bbbba', encoder.invert)
24+
test('/some/path/key_onswyzq', '/some/path/self', encoder.invert)
25+
})
26+
it('decode expects specific format', () => {
27+
expect(() => { encoder.invert(new Key('/some/path/onswyzq')) }).to.throw('Unknown')
28+
})
29+
})

0 commit comments

Comments
 (0)