1
1
/*
2
2
* Id is an object representation of a peer Id. a peer Id is a multihash
3
3
*/
4
+
4
5
'use strict'
5
6
6
7
const fs = require ( 'fs' )
@@ -10,8 +11,7 @@ const forge = require('node-forge')
10
11
const protobuf = require ( 'protocol-buffers' )
11
12
const path = require ( 'path' )
12
13
13
- // protobuf read from file
14
- const messages = protobuf ( fs . readFileSync ( path . resolve ( __dirname , '../protos/crypto.proto' ) ) )
14
+ const pbCrypto = protobuf ( fs . readFileSync ( path . resolve ( __dirname , '../protos/crypto.proto' ) ) )
15
15
16
16
exports = module . exports = Id
17
17
@@ -52,23 +52,25 @@ function Id (id, privKey, pubKey) {
52
52
}
53
53
54
54
// unwrap the private key protobuf
55
- function unmarshal ( key ) {
56
- return messages . PrivateKey . decode ( key )
55
+ function keyUnmarshal ( key ) {
56
+ return pbCrypto . PrivateKey . decode ( key )
57
57
}
58
58
59
59
// create a public key protobuf to be base64 string stored in config
60
- function marshal ( data , type ) {
61
- var epb
60
+ function keyMarshal ( data , type ) {
61
+ const RSA = 0
62
+
63
+ let epb
62
64
if ( type === 'Public' ) {
63
- epb = messages . PublicKey . encode ( {
64
- Type : 0 ,
65
+ epb = pbCrypto . PublicKey . encode ( {
66
+ Type : RSA ,
65
67
Data : data
66
68
} )
67
69
}
68
70
69
71
if ( type === 'Private' ) {
70
- epb = messages . PrivateKey . encode ( {
71
- Type : 0 ,
72
+ epb = pbCrypto . PrivateKey . encode ( {
73
+ Type : RSA ,
72
74
Data : data
73
75
} )
74
76
}
@@ -88,10 +90,10 @@ function formatKey (key, type) {
88
90
const nDerBuf = new Buffer ( fDerBuf . getBytes ( ) , 'binary' )
89
91
90
92
// protobuf the new DER bytes to the PublicKey Data: field
91
- const marshalKey = marshal ( nDerBuf , type )
93
+ const marsheledKey = keyMarshal ( nDerBuf , type )
92
94
93
95
// encode the protobuf public key to base64 string
94
- const b64 = marshalKey . toString ( 'base64' )
96
+ const b64 = marsheledKey . toString ( 'base64' )
95
97
return b64
96
98
}
97
99
@@ -148,7 +150,7 @@ exports.createFromPrivKey = function (privKey) {
148
150
const buf = new Buffer ( privKey , 'base64' )
149
151
150
152
// get the private key data from the protobuf
151
- const mpk = unmarshal ( buf )
153
+ const mpk = keyUnmarshal ( buf )
152
154
153
155
// create a forge buffer
154
156
const fbuf = forge . util . createBuffer ( mpk . Data . toString ( 'binary' ) )
0 commit comments