@@ -13,14 +13,14 @@ const path = require('path')
13
13
14
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' )
@@ -122,26 +130,26 @@ exports.create = function (opts) {
122
130
123
131
const mhId = multihashing ( new Buffer ( protoPublic64 , 'base64' ) , 'sha2-256' )
124
132
125
- return new Id ( mhId , bufProtoPriv64 , bufProtoPub64 )
133
+ return new PeerId ( mhId , bufProtoPriv64 , bufProtoPub64 )
126
134
}
127
135
128
136
exports . createFromHexString = function ( str ) {
129
- return new Id ( new Buffer ( str , 'hex' ) )
137
+ return new PeerId ( new Buffer ( str , 'hex' ) )
130
138
}
131
139
132
140
exports . createFromBytes = function ( buf ) {
133
- return new Id ( buf )
141
+ return new PeerId ( buf )
134
142
}
135
143
136
144
exports . createFromB58String = function ( str ) {
137
- return new Id ( new Buffer ( base58 . decode ( str ) ) )
145
+ return new PeerId ( new Buffer ( base58 . decode ( str ) ) )
138
146
}
139
147
140
148
// Public Key input will be a buffer
141
149
exports . createFromPubKey = function ( pubKey ) {
142
150
const buf = new Buffer ( pubKey , 'base64' )
143
151
const mhId = multihashing ( buf , 'sha2-256' )
144
- return new Id ( mhId , null , pubKey )
152
+ return new PeerId ( mhId , null , pubKey )
145
153
}
146
154
147
155
// Private key input will be a string
@@ -173,5 +181,12 @@ exports.createFromPrivKey = function (privKey) {
173
181
// buffer the public key for consistency before storing
174
182
const bufProtoPub64 = new Buffer ( protoPublic64 , 'base64' )
175
183
const mhId = multihashing ( new Buffer ( protoPublic64 , 'base64' ) , 'sha2-256' )
176
- 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' ) )
177
192
}
0 commit comments