This repository was archived by the owner on Feb 12, 2024. It is now read-only.
File tree 3 files changed +80
-1
lines changed
3 files changed +80
-1
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ const mergeOptions = require('merge-options')
8
8
const NoKeychain = require ( './no-keychain' )
9
9
const callbackify = require ( 'callbackify' )
10
10
const promisify = require ( 'promisify-es6' )
11
+ const KeyTransformDatastore = require ( 'datastore-core' ) . KeytransformDatastore
12
+ const keychainTransformer = require ( '../../utils/keychain-encoder' )
11
13
12
14
/*
13
15
* Load stuff from Repo into memory
@@ -36,7 +38,8 @@ module.exports = function preStart (self) {
36
38
// most likely an init or upgrade has happened
37
39
} else if ( pass ) {
38
40
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 )
40
43
self . log ( 'keychain constructed' )
41
44
} else {
42
45
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 ] = Buffer . from ( decodedBaseNamespace ) . toString ( ) // 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
+ 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
+ } )
You can’t perform that action at this time.
0 commit comments