4
4
5
5
'use strict'
6
6
7
- const mh = require ( 'multihashes' )
8
- const CID = require ( 'cids' )
7
+ const { CID } = require ( 'multiformats/cid' )
8
+ const { base58btc } = require ( 'multiformats/bases/base58' )
9
+ const { base16 } = require ( 'multiformats/bases/base16' )
10
+ const Digest = require ( 'multiformats/hashes/digest' )
9
11
const cryptoKeys = require ( 'libp2p-crypto/src/keys' )
10
12
const withIs = require ( 'class-is' )
11
13
const { PeerIdProto } = require ( './proto' )
12
14
const uint8ArrayEquals = require ( 'uint8arrays/equals' )
13
15
const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
14
16
const uint8ArrayToString = require ( 'uint8arrays/to-string' )
17
+ const { identity } = require ( 'multiformats/hashes/identity' )
18
+
19
+ // these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
20
+ const DAG_PB_CODE = 0x70
21
+ const LIBP2P_KEY_CODE = 0x72
15
22
16
23
class PeerId {
17
24
constructor ( id , privKey , pubKey ) {
@@ -24,7 +31,7 @@ class PeerId {
24
31
}
25
32
26
33
this . _id = id
27
- this . _idB58String = mh . toB58String ( this . id )
34
+ this . _idB58String = base58btc . encode ( this . id ) . substring ( 1 )
28
35
this . _privKey = privKey
29
36
this . _pubKey = pubKey
30
37
}
@@ -55,9 +62,9 @@ class PeerId {
55
62
}
56
63
57
64
try {
58
- const decoded = mh . decode ( this . id )
65
+ const decoded = Digest . decode ( this . id )
59
66
60
- if ( decoded . name === ' identity' ) {
67
+ if ( decoded . code === identity . code ) {
61
68
this . _pubKey = cryptoKeys . unmarshalPublicKey ( decoded . digest )
62
69
}
63
70
} catch ( _ ) {
@@ -121,7 +128,7 @@ class PeerId {
121
128
122
129
// encode/decode functions
123
130
toHexString ( ) {
124
- return mh . toHexString ( this . id )
131
+ return base16 . encode ( this . id ) . substring ( 1 )
125
132
}
126
133
127
134
toBytes ( ) {
@@ -136,10 +143,10 @@ class PeerId {
136
143
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
137
144
toString ( ) {
138
145
if ( ! this . _idCIDString ) {
139
- const cid = new CID ( 1 , 'libp2p-key' , this . id , 'base32' )
146
+ const cid = CID . createV1 ( LIBP2P_KEY_CODE , Digest . decode ( this . id ) )
140
147
141
148
Object . defineProperty ( this , '_idCIDString' , {
142
- value : cid . toBaseEncodedString ( 'base32' ) ,
149
+ value : cid . toString ( ) ,
143
150
enumerable : false
144
151
} )
145
152
}
@@ -192,8 +199,9 @@ class PeerId {
192
199
*/
193
200
hasInlinePublicKey ( ) {
194
201
try {
195
- const decoded = mh . decode ( this . id )
196
- if ( decoded . name === 'identity' ) {
202
+ const decoded = Digest . decode ( this . id )
203
+
204
+ if ( decoded . code === identity . code ) {
197
205
return true
198
206
}
199
207
} catch ( _ ) {
@@ -213,7 +221,7 @@ exports = module.exports = PeerIdWithIs
213
221
214
222
const computeDigest = ( pubKey ) => {
215
223
if ( pubKey . bytes . length <= 42 ) {
216
- return mh . encode ( pubKey . bytes , 'identity' )
224
+ return Digest . create ( identity . code , pubKey . bytes ) . bytes
217
225
} else {
218
226
return pubKey . hash ( )
219
227
}
@@ -235,26 +243,46 @@ exports.create = async (opts) => {
235
243
}
236
244
237
245
exports . createFromHexString = ( str ) => {
238
- return new PeerIdWithIs ( mh . fromHexString ( str ) )
246
+ return new PeerIdWithIs ( base16 . decode ( 'f' + str ) )
239
247
}
240
248
241
249
exports . createFromBytes = ( buf ) => {
242
- return new PeerIdWithIs ( buf )
250
+ try {
251
+ const cid = CID . decode ( buf )
252
+
253
+ if ( ! validMulticodec ( cid ) ) {
254
+ throw new Error ( 'Supplied PeerID CID is invalid' )
255
+ }
256
+
257
+ return exports . createFromCID ( cid )
258
+ } catch {
259
+ const digest = Digest . decode ( buf )
260
+
261
+ if ( digest . code !== identity . code ) {
262
+ throw new Error ( 'Supplied PeerID CID is invalid' )
263
+ }
264
+
265
+ return new PeerIdWithIs ( buf )
266
+ }
243
267
}
244
268
245
269
exports . createFromB58String = ( str ) => {
246
- return exports . createFromCID ( str ) // B58String is CIDv0
270
+ return exports . createFromBytes ( base58btc . decode ( 'z' + str ) )
247
271
}
248
272
249
273
const validMulticodec = ( cid ) => {
250
274
// supported: 'libp2p-key' (CIDv1) and 'dag-pb' (CIDv0 converted to CIDv1)
251
- return cid . codec === 'libp2p-key' || cid . codec === 'dag-pb'
275
+ return cid . code === LIBP2P_KEY_CODE || cid . code === DAG_PB_CODE
252
276
}
253
277
254
278
exports . createFromCID = ( cid ) => {
255
- cid = CID . isCID ( cid ) ? cid : new CID ( cid )
256
- if ( ! validMulticodec ( cid ) ) throw new Error ( 'Supplied PeerID CID has invalid multicodec: ' + cid . codec )
257
- return new PeerIdWithIs ( cid . multihash )
279
+ cid = CID . asCID ( cid )
280
+
281
+ if ( ! cid || ! validMulticodec ( cid ) ) {
282
+ throw new Error ( 'Supplied PeerID CID is invalid' )
283
+ }
284
+
285
+ return new PeerIdWithIs ( cid . multihash . bytes )
258
286
}
259
287
260
288
// Public Key input will be a Uint8Array
@@ -288,7 +316,7 @@ exports.createFromPrivKey = async (key) => {
288
316
}
289
317
290
318
exports . createFromJSON = async ( obj ) => {
291
- const id = mh . fromB58String ( obj . id )
319
+ const id = base58btc . decode ( 'z' + obj . id )
292
320
const rawPrivKey = obj . privKey && uint8ArrayFromString ( obj . privKey , 'base64pad' )
293
321
const rawPubKey = obj . pubKey && uint8ArrayFromString ( obj . pubKey , 'base64pad' )
294
322
const pub = rawPubKey && await cryptoKeys . unmarshalPublicKey ( rawPubKey )
@@ -360,6 +388,16 @@ exports.createFromProtobuf = async (buf) => {
360
388
throw new Error ( 'Protobuf did not contain any usable key material' )
361
389
}
362
390
391
+ exports . parse = ( str ) => {
392
+ if ( str . charAt ( 0 ) === '1' ) {
393
+ // base58btc encoded public key
394
+ return exports . createFromBytes ( base58btc . decode ( `z${ str } ` ) )
395
+ }
396
+
397
+ // try to parse it as a regular base58btc multihash or base32 encoded CID
398
+ return exports . createFromCID ( CID . parse ( str ) )
399
+ }
400
+
363
401
exports . isPeerId = ( peerId ) => {
364
402
return Boolean ( typeof peerId === 'object' &&
365
403
peerId . _id &&
0 commit comments