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,17 +11,16 @@ 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
- exports = module . exports = Id
16
+ exports = module . exports = PeerId
17
17
18
18
exports . Buffer = Buffer
19
19
20
- function Id ( id , privKey , pubKey ) {
20
+ function PeerId ( id , privKey , pubKey ) {
21
21
const self = this
22
22
23
- if ( ! ( self instanceof Id ) ) {
23
+ if ( ! ( self instanceof PeerId ) ) {
24
24
throw new Error ( 'Id must be called with new' )
25
25
}
26
26
@@ -37,6 +37,14 @@ function Id (id, privKey, pubKey) {
37
37
}
38
38
}
39
39
40
+ self . toJSON = function ( ) {
41
+ return {
42
+ id : self . id . toString ( 'hex' ) ,
43
+ privKey : self . privKey . toString ( 'hex' ) ,
44
+ pubKey : self . pubKey . toString ( 'hex' )
45
+ }
46
+ }
47
+
40
48
// encode/decode functions
41
49
self . toHexString = function ( ) {
42
50
return self . id . toString ( 'hex' )
@@ -52,23 +60,25 @@ function Id (id, privKey, pubKey) {
52
60
}
53
61
54
62
// unwrap the private key protobuf
55
- function unmarshal ( key ) {
56
- return messages . PrivateKey . decode ( key )
63
+ function keyUnmarshal ( key ) {
64
+ return pbCrypto . PrivateKey . decode ( key )
57
65
}
58
66
59
67
// create a public key protobuf to be base64 string stored in config
60
- function marshal ( data , type ) {
61
- var epb
68
+ function keyMarshal ( data , type ) {
69
+ const RSA = 0
70
+
71
+ let epb
62
72
if ( type === 'Public' ) {
63
- epb = messages . PublicKey . encode ( {
64
- Type : 0 ,
73
+ epb = pbCrypto . PublicKey . encode ( {
74
+ Type : RSA ,
65
75
Data : data
66
76
} )
67
77
}
68
78
69
79
if ( type === 'Private' ) {
70
- epb = messages . PrivateKey . encode ( {
71
- Type : 0 ,
80
+ epb = pbCrypto . PrivateKey . encode ( {
81
+ Type : RSA ,
72
82
Data : data
73
83
} )
74
84
}
@@ -88,10 +98,10 @@ function formatKey (key, type) {
88
98
const nDerBuf = new Buffer ( fDerBuf . getBytes ( ) , 'binary' )
89
99
90
100
// protobuf the new DER bytes to the PublicKey Data: field
91
- const marshalKey = marshal ( nDerBuf , type )
101
+ const marsheledKey = keyMarshal ( nDerBuf , type )
92
102
93
103
// encode the protobuf public key to base64 string
94
- const b64 = marshalKey . toString ( 'base64' )
104
+ const b64 = marsheledKey . toString ( 'base64' )
95
105
return b64
96
106
}
97
107
@@ -120,26 +130,26 @@ exports.create = function (opts) {
120
130
121
131
const mhId = multihashing ( new Buffer ( protoPublic64 , 'base64' ) , 'sha2-256' )
122
132
123
- return new Id ( mhId , bufProtoPriv64 , bufProtoPub64 )
133
+ return new PeerId ( mhId , bufProtoPriv64 , bufProtoPub64 )
124
134
}
125
135
126
136
exports . createFromHexString = function ( str ) {
127
- return new Id ( new Buffer ( str , 'hex' ) )
137
+ return new PeerId ( new Buffer ( str , 'hex' ) )
128
138
}
129
139
130
140
exports . createFromBytes = function ( buf ) {
131
- return new Id ( buf )
141
+ return new PeerId ( buf )
132
142
}
133
143
134
144
exports . createFromB58String = function ( str ) {
135
- return new Id ( new Buffer ( base58 . decode ( str ) ) )
145
+ return new PeerId ( new Buffer ( base58 . decode ( str ) ) )
136
146
}
137
147
138
148
// Public Key input will be a buffer
139
149
exports . createFromPubKey = function ( pubKey ) {
140
150
const buf = new Buffer ( pubKey , 'base64' )
141
151
const mhId = multihashing ( buf , 'sha2-256' )
142
- return new Id ( mhId , null , pubKey )
152
+ return new PeerId ( mhId , null , pubKey )
143
153
}
144
154
145
155
// Private key input will be a string
@@ -148,7 +158,7 @@ exports.createFromPrivKey = function (privKey) {
148
158
const buf = new Buffer ( privKey , 'base64' )
149
159
150
160
// get the private key data from the protobuf
151
- const mpk = unmarshal ( buf )
161
+ const mpk = keyUnmarshal ( buf )
152
162
153
163
// create a forge buffer
154
164
const fbuf = forge . util . createBuffer ( mpk . Data . toString ( 'binary' ) )
@@ -171,5 +181,12 @@ exports.createFromPrivKey = function (privKey) {
171
181
// buffer the public key for consistency before storing
172
182
const bufProtoPub64 = new Buffer ( protoPublic64 , 'base64' )
173
183
const mhId = multihashing ( new Buffer ( protoPublic64 , 'base64' ) , 'sha2-256' )
174
- return new Id ( mhId , privKey , bufProtoPub64 )
184
+ return new PeerId ( mhId , privKey , bufProtoPub64 )
185
+ }
186
+
187
+ exports . createFromJSON = function ( obj ) {
188
+ return new PeerId (
189
+ new Buffer ( obj . id , 'hex' ) ,
190
+ new Buffer ( obj . privKey , 'hex' ) ,
191
+ new Buffer ( obj . pubKey , 'hex' ) )
175
192
}
0 commit comments