-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
427 lines (346 loc) · 10.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* Id is an object representation of a peer Id. a peer Id is a multihash
*/
'use strict'
const { CID } = require('multiformats/cid')
const b32 = require('multiformats/bases/base32')
const b36 = require('multiformats/bases/base36')
const b58 = require('multiformats/bases/base58')
const b64 = require('multiformats/bases/base64')
const { base58btc } = require('multiformats/bases/base58')
const { base32 } = require('multiformats/bases/base32')
const { base16 } = require('multiformats/bases/base16')
const Digest = require('multiformats/hashes/digest')
const cryptoKeys = require('libp2p-crypto/src/keys')
const withIs = require('class-is')
const { PeerIdProto } = require('./proto')
const { equals: uint8ArrayEquals } = require('uint8arrays/equals')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
const { identity } = require('multiformats/hashes/identity')
const bases = {
...b32,
...b36,
...b58,
...b64
}
const baseDecoder = Object.keys(bases).reduce(
(acc, curr) => acc.or(bases[curr]),
base32.decoder
)
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
const DAG_PB_CODE = 0x70
const LIBP2P_KEY_CODE = 0x72
class PeerId {
constructor (id, privKey, pubKey) {
if (!(id instanceof Uint8Array)) {
throw new Error('invalid id provided')
}
if (privKey && pubKey && !uint8ArrayEquals(privKey.public.bytes, pubKey.bytes)) {
throw new Error('inconsistent arguments')
}
this._id = id
this._idB58String = base58btc.encode(this.id).substring(1)
this._privKey = privKey
this._pubKey = pubKey
}
get id () {
return this._id
}
set id (val) {
throw new Error('Id is immutable')
}
get privKey () {
return this._privKey
}
set privKey (privKey) {
this._privKey = privKey
}
get pubKey () {
if (this._pubKey) {
return this._pubKey
}
if (this._privKey) {
return this._privKey.public
}
try {
const decoded = Digest.decode(this.id)
if (decoded.code === identity.code) {
this._pubKey = cryptoKeys.unmarshalPublicKey(decoded.digest)
}
} catch (_) {
// Ignore, there is no valid public key
}
return this._pubKey
}
set pubKey (pubKey) {
this._pubKey = pubKey
}
// Return the protobuf version of the public key, matching go ipfs formatting
marshalPubKey () {
if (this.pubKey) {
return cryptoKeys.marshalPublicKey(this.pubKey)
}
}
// Return the protobuf version of the private key, matching go ipfs formatting
marshalPrivKey () {
if (this.privKey) {
return cryptoKeys.marshalPrivateKey(this.privKey)
}
}
// Return the protobuf version of the peer-id
marshal (excludePriv) {
return PeerIdProto.encode({
id: this.toBytes(),
pubKey: this.marshalPubKey(),
privKey: excludePriv ? null : this.marshalPrivKey()
}).finish()
}
toPrint () {
let pid = this.toB58String()
// All sha256 nodes start with Qm
// We can skip the Qm to make the peer.ID more useful
if (pid.startsWith('Qm')) {
pid = pid.slice(2)
}
let maxRunes = 6
if (pid.length < maxRunes) {
maxRunes = pid.length
}
return '<peer.ID ' + pid.substr(0, maxRunes) + '>'
}
// return the jsonified version of the key, matching the formatting
// of go-ipfs for its config file
toJSON () {
return {
id: this.toB58String(),
privKey: toB64Opt(this.marshalPrivKey()),
pubKey: toB64Opt(this.marshalPubKey())
}
}
// encode/decode functions
toHexString () {
return base16.encode(this.id).substring(1)
}
toBytes () {
return this.id
}
toB58String () {
return this._idB58String
}
// return self-describing String representation
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
toString () {
if (!this._idCIDString) {
const cid = CID.createV1(LIBP2P_KEY_CODE, Digest.decode(this.id))
Object.defineProperty(this, '_idCIDString', {
value: cid.toString(),
enumerable: false
})
}
return this._idCIDString
}
/**
* Checks the equality of `this` peer against a given PeerId.
*
* @param {Uint8Array|PeerId} id
* @returns {boolean}
*/
equals (id) {
if (id instanceof Uint8Array) {
return uint8ArrayEquals(this.id, id)
} else if (id.id) {
return uint8ArrayEquals(this.id, id.id)
} else {
throw new Error('not valid Id')
}
}
/**
* Checks the equality of `this` peer against a given PeerId.
*
* @deprecated Use `.equals`
* @param {Uint8Array|PeerId} id
* @returns {boolean}
*/
isEqual (id) {
return this.equals(id)
}
/*
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
isValid () {
// TODO: needs better checking
return Boolean(this.privKey &&
this.privKey.public &&
this.privKey.public.bytes &&
this.pubKey.bytes instanceof Uint8Array &&
uint8ArrayEquals(this.privKey.public.bytes, this.pubKey.bytes))
}
/**
* Check if the PeerId has an inline public key.
*
* @returns {boolean}
*/
hasInlinePublicKey () {
try {
const decoded = Digest.decode(this.id)
if (decoded.code === identity.code) {
return true
}
} catch (_) {
// Ignore, there is no valid public key
}
return false
}
}
const PeerIdWithIs = withIs(PeerId, {
className: 'PeerId',
symbolName: '@libp2p/js-peer-id/PeerId'
})
exports = module.exports = PeerIdWithIs
const computeDigest = (pubKey) => {
if (pubKey.bytes.length <= 42) {
return Digest.create(identity.code, pubKey.bytes).bytes
} else {
return pubKey.hash()
}
}
const computePeerId = async (privKey, pubKey) => {
const digest = await computeDigest(pubKey)
return new PeerIdWithIs(digest, privKey, pubKey)
}
// generation
exports.create = async (opts) => {
opts = opts || {}
opts.bits = opts.bits || 2048
opts.keyType = opts.keyType || 'RSA'
const key = await cryptoKeys.generateKeyPair(opts.keyType, opts.bits)
return computePeerId(key, key.public)
}
exports.createFromHexString = (str) => {
return new PeerIdWithIs(base16.decode('f' + str))
}
exports.createFromBytes = (buf) => {
try {
const cid = CID.decode(buf)
if (!validMulticodec(cid)) {
throw new Error('Supplied PeerID CID is invalid')
}
return exports.createFromCID(cid)
} catch {
const digest = Digest.decode(buf)
if (digest.code !== identity.code) {
throw new Error('Supplied PeerID CID is invalid')
}
return new PeerIdWithIs(buf)
}
}
exports.createFromB58String = (str) => {
return exports.createFromBytes(base58btc.decode('z' + str))
}
const validMulticodec = (cid) => {
// supported: 'libp2p-key' (CIDv1) and 'dag-pb' (CIDv0 converted to CIDv1)
return cid.code === LIBP2P_KEY_CODE || cid.code === DAG_PB_CODE
}
exports.createFromCID = (cid) => {
cid = CID.asCID(cid)
if (!cid || !validMulticodec(cid)) {
throw new Error('Supplied PeerID CID is invalid')
}
return new PeerIdWithIs(cid.multihash.bytes)
}
// Public Key input will be a Uint8Array
exports.createFromPubKey = async (key) => {
let buf = key
if (typeof buf === 'string') {
buf = uint8ArrayFromString(key, 'base64pad')
}
if (!(buf instanceof Uint8Array)) {
throw new Error('Supplied key is neither a base64 string nor a Uint8Array')
}
const pubKey = await cryptoKeys.unmarshalPublicKey(buf)
return computePeerId(undefined, pubKey)
}
// Private key input will be a string
exports.createFromPrivKey = async (key) => {
if (typeof key === 'string') {
key = uint8ArrayFromString(key, 'base64pad')
}
if (!(key instanceof Uint8Array)) {
throw new Error('Supplied key is neither a base64 string nor a Uint8Array')
}
const privKey = await cryptoKeys.unmarshalPrivateKey(key)
return computePeerId(privKey, privKey.public)
}
exports.createFromJSON = async (obj) => {
const id = base58btc.decode('z' + obj.id)
const rawPrivKey = obj.privKey && uint8ArrayFromString(obj.privKey, 'base64pad')
const rawPubKey = obj.pubKey && uint8ArrayFromString(obj.pubKey, 'base64pad')
const pub = rawPubKey && await cryptoKeys.unmarshalPublicKey(rawPubKey)
if (!rawPrivKey) {
return new PeerIdWithIs(id, undefined, pub)
}
const privKey = await cryptoKeys.unmarshalPrivateKey(rawPrivKey)
const privDigest = await computeDigest(privKey.public)
let pubDigest
if (pub) {
pubDigest = await computeDigest(pub)
}
if (pub && !uint8ArrayEquals(privDigest, pubDigest)) {
throw new Error('Public and private key do not match')
}
if (id && !uint8ArrayEquals(privDigest, id)) {
throw new Error('Id and private key do not match')
}
return new PeerIdWithIs(id, privKey, pub)
}
exports.createFromProtobuf = async (buf) => {
if (typeof buf === 'string') {
buf = uint8ArrayFromString(buf, 'base16')
}
let { id, privKey, pubKey } = PeerIdProto.decode(buf)
privKey = privKey ? await cryptoKeys.unmarshalPrivateKey(privKey) : false
pubKey = pubKey ? await cryptoKeys.unmarshalPublicKey(pubKey) : false
let pubDigest
let privDigest
if (privKey) {
privDigest = await computeDigest(privKey.public)
}
if (pubKey) {
pubDigest = await computeDigest(pubKey)
}
if (privKey) {
if (pubKey) {
if (!uint8ArrayEquals(privDigest, pubDigest)) {
throw new Error('Public and private key do not match')
}
}
return new PeerIdWithIs(privDigest, privKey, privKey.public)
}
// TODO: val id and pubDigest
if (pubKey) {
return new PeerIdWithIs(pubDigest, undefined, pubKey)
}
if (id) {
return new PeerIdWithIs(id)
}
throw new Error('Protobuf did not contain any usable key material')
}
exports.parse = (str) => {
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
// identity hash ed25519 key or sha2-256 hash of rsa public key
// base58btc encoded either way
str = `z${str}`
}
return exports.createFromBytes(baseDecoder.decode(str))
}
exports.isPeerId = (peerId) => {
return Boolean(typeof peerId === 'object' &&
peerId._id &&
peerId._idB58String)
}
function toB64Opt (val) {
if (val) {
return uint8ArrayToString(val, 'base64pad')
}
}