This repository was archived by the owner on Feb 12, 2024. It is now read-only.
File tree 3 files changed +83
-1
lines changed
3 files changed +83
-1
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,9 @@ const waterfall = require('async/waterfall')
7
7
const Keychain = require ( 'libp2p-keychain' )
8
8
const mergeOptions = require ( 'merge-options' )
9
9
const NoKeychain = require ( './no-keychain' )
10
+ const KeyTransformDatastore = require ( 'datastore-core' ) . KeytransformDatastore
11
+ const keychainTransformer = require ( '../../utils/keychain-encoder' )
12
+
10
13
/*
11
14
* Load stuff from Repo into memory
12
15
*/
@@ -49,7 +52,9 @@ module.exports = function preStart (self) {
49
52
// most likely an init or upgrade has happened
50
53
} else if ( pass ) {
51
54
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)
53
58
self . log ( 'keychain constructed' )
54
59
} else {
55
60
self . _keychain = new NoKeychain ( )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments