This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
const base32 = require('base32.js') | ||
const Key = require('interface-datastore').Key | ||
const KEY_PREFIX = 'key_' | ||
|
||
module.exports = { | ||
/** | ||
* Encode baseNamespace of a Key to base32 | ||
* | ||
* @param {Key} key | ||
* @returns {Key} | ||
* | ||
* @example convert(new Key('/info/self.data')) | ||
* // => Key('/info/key_onswyzq.data') | ||
*/ | ||
convert (key) { | ||
const encoder = new base32.Encoder({ type: 'rfc4648' }) | ||
const baseNameBuff = Buffer.from(key.baseNamespace()) | ||
const encodedBaseNamespace = KEY_PREFIX + encoder.finalize(baseNameBuff).toLowerCase() | ||
const namespaces = key.namespaces() | ||
namespaces[namespaces.length - 1] = encodedBaseNamespace // Replace the baseNamespace with encoded one | ||
return Key.withNamespaces(namespaces) | ||
}, | ||
|
||
/** | ||
* Decode baseNamespace of a Key from base32 | ||
* | ||
* @param {Key} key | ||
* @returns {Key} | ||
* | ||
* @example invert(new Key('/info/key_onswyzq.data')) | ||
* // => Key('/info/self.data') | ||
*/ | ||
invert (key) { | ||
const baseNamespace = key.baseNamespace() | ||
if (!baseNamespace.startsWith(KEY_PREFIX)) { | ||
throw Error('Unknown format of key\'s name!') | ||
} | ||
|
||
const decoder = new base32.Decoder({ type: 'rfc4648' }) | ||
const decodedBaseNamespace = decoder.finalize(baseNamespace.replace(KEY_PREFIX, '').toUpperCase()) | ||
const namespaces = key.namespaces() | ||
namespaces[namespaces.length - 1] = Buffer.from(decodedBaseNamespace).toString() // Replace the baseNamespace with encoded one | ||
|
||
return Key.withNamespaces(namespaces) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const encoder = require('../../src/utils/keychain-encoder') | ||
const Key = require('interface-datastore').Key | ||
|
||
const expect = require('chai').expect | ||
|
||
function test (input, expected, fnc) { | ||
input = new Key(input) | ||
const output = fnc(input) | ||
|
||
expect(output.toString()).to.equal(expected) | ||
} | ||
|
||
describe('keychain-encode', () => { | ||
it('encode keys', () => { | ||
test('/self', '/key_onswyzq', encoder.convert) | ||
test('bbbba', '/key_mjrgeytb', encoder.convert) | ||
test('/some/path/self', '/some/path/key_onswyzq', encoder.convert) | ||
}) | ||
it('decode keys', () => { | ||
test('/key_onswyzq', '/self', encoder.invert) | ||
test('key_mjrgeytb', '/bbbba', encoder.invert) | ||
test('/some/path/key_onswyzq', '/some/path/self', encoder.invert) | ||
}) | ||
it('decode expects specific format', () => { | ||
expect(() => { encoder.invert(new Key('/some/path/onswyzq')) }).to.throw('Unknown') | ||
}) | ||
}) |