@libp2p/peer-id 4.2.4 → 5.0.0-18dd3cb26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +30 -1
- package/dist/src/index.d.ts +11 -17
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +58 -222
- package/dist/src/index.js.map +1 -1
- package/dist/src/peer-id.d.ts +97 -0
- package/dist/src/peer-id.d.ts.map +1 -0
- package/dist/src/peer-id.js +158 -0
- package/dist/src/peer-id.js.map +1 -0
- package/package.json +3 -2
- package/src/index.ts +67 -286
- package/src/peer-id.ts +213 -0
- package/dist/typedoc-urls.json +0 -14
package/src/index.ts
CHANGED
|
@@ -14,333 +14,114 @@
|
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
17
|
+
import { publicKeyFromMultihash } from '@libp2p/crypto/keys'
|
|
18
|
+
import { InvalidCIDError, InvalidMultihashError, InvalidParametersError, UnsupportedKeyTypeError } from '@libp2p/interface'
|
|
19
19
|
import { base58btc } from 'multiformats/bases/base58'
|
|
20
|
-
import {
|
|
21
|
-
import { CID } from 'multiformats/cid'
|
|
20
|
+
import { type CID, type MultibaseDecoder } from 'multiformats/cid'
|
|
22
21
|
import * as Digest from 'multiformats/hashes/digest'
|
|
23
22
|
import { identity } from 'multiformats/hashes/identity'
|
|
24
23
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
25
|
-
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
26
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
27
24
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
28
|
-
import
|
|
25
|
+
import { RSAPeerId as RSAPeerIdClass, Ed25519PeerId as Ed25519PeerIdClass, Secp256k1PeerId as Secp256k1PeerIdClass, URLPeerId as URLPeerIdClass } from './peer-id.js'
|
|
26
|
+
import type { Ed25519PeerId, RSAPeerId, URLPeerId, Secp256k1PeerId, PeerId, PublicKey, Ed25519PublicKey, Secp256k1PublicKey, RSAPublicKey, Ed25519PrivateKey, Secp256k1PrivateKey, RSAPrivateKey, PrivateKey } from '@libp2p/interface'
|
|
29
27
|
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
30
28
|
|
|
31
|
-
const inspect = Symbol.for('nodejs.util.inspect.custom')
|
|
32
|
-
|
|
33
|
-
const baseDecoder = Object
|
|
34
|
-
.values(bases)
|
|
35
|
-
.map(codec => codec.decoder)
|
|
36
|
-
// @ts-expect-error https://github.com/multiformats/js-multiformats/issues/141
|
|
37
|
-
.reduce((acc, curr) => acc.or(curr), bases.identity.decoder)
|
|
38
|
-
|
|
39
29
|
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
40
30
|
const LIBP2P_KEY_CODE = 0x72
|
|
41
|
-
|
|
42
|
-
const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36
|
|
43
|
-
const MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH = 37
|
|
44
|
-
|
|
45
|
-
interface PeerIdInit {
|
|
46
|
-
type: PeerIdType
|
|
47
|
-
multihash: MultihashDigest
|
|
48
|
-
privateKey?: Uint8Array
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
interface RSAPeerIdInit {
|
|
52
|
-
multihash: MultihashDigest
|
|
53
|
-
privateKey?: Uint8Array
|
|
54
|
-
publicKey?: Uint8Array
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface Ed25519PeerIdInit {
|
|
58
|
-
multihash: MultihashDigest
|
|
59
|
-
privateKey?: Uint8Array
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
interface Secp256k1PeerIdInit {
|
|
63
|
-
multihash: MultihashDigest
|
|
64
|
-
privateKey?: Uint8Array
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
class PeerIdImpl {
|
|
68
|
-
public type: PeerIdType
|
|
69
|
-
public readonly multihash: MultihashDigest
|
|
70
|
-
public readonly privateKey?: Uint8Array
|
|
71
|
-
public readonly publicKey?: Uint8Array
|
|
72
|
-
private string?: string
|
|
73
|
-
|
|
74
|
-
constructor (init: PeerIdInit) {
|
|
75
|
-
this.type = init.type
|
|
76
|
-
this.multihash = init.multihash
|
|
77
|
-
this.privateKey = init.privateKey
|
|
78
|
-
|
|
79
|
-
// mark string cache as non-enumerable
|
|
80
|
-
Object.defineProperty(this, 'string', {
|
|
81
|
-
enumerable: false,
|
|
82
|
-
writable: true
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
get [Symbol.toStringTag] (): string {
|
|
87
|
-
return `PeerId(${this.toString()})`
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
readonly [peerIdSymbol] = true
|
|
91
|
-
|
|
92
|
-
toString (): string {
|
|
93
|
-
if (this.string == null) {
|
|
94
|
-
this.string = base58btc.encode(this.multihash.bytes).slice(1)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return this.string
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// return self-describing String representation
|
|
101
|
-
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
|
|
102
|
-
toCID (): CID {
|
|
103
|
-
return CID.createV1(LIBP2P_KEY_CODE, this.multihash)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
toBytes (): Uint8Array {
|
|
107
|
-
return this.multihash.bytes
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Returns Multiaddr as a JSON string
|
|
112
|
-
*/
|
|
113
|
-
toJSON (): string {
|
|
114
|
-
return this.toString()
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Checks the equality of `this` peer against a given PeerId
|
|
119
|
-
*/
|
|
120
|
-
equals (id?: PeerId | Uint8Array | string): boolean {
|
|
121
|
-
if (id == null) {
|
|
122
|
-
return false
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (id instanceof Uint8Array) {
|
|
126
|
-
return uint8ArrayEquals(this.multihash.bytes, id)
|
|
127
|
-
} else if (typeof id === 'string') {
|
|
128
|
-
return peerIdFromString(id).equals(this as PeerId)
|
|
129
|
-
} else if (id?.multihash?.bytes != null) {
|
|
130
|
-
return uint8ArrayEquals(this.multihash.bytes, id.multihash.bytes)
|
|
131
|
-
} else {
|
|
132
|
-
throw new Error('not valid Id')
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Returns PeerId as a human-readable string
|
|
138
|
-
* https://nodejs.org/api/util.html#utilinspectcustom
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```TypeScript
|
|
142
|
-
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
143
|
-
*
|
|
144
|
-
* console.info(peerIdFromString('QmFoo'))
|
|
145
|
-
* // 'PeerId(QmFoo)'
|
|
146
|
-
* ```
|
|
147
|
-
*/
|
|
148
|
-
[inspect] (): string {
|
|
149
|
-
return `PeerId(${this.toString()})`
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
class RSAPeerIdImpl extends PeerIdImpl implements RSAPeerId {
|
|
154
|
-
public readonly type = 'RSA'
|
|
155
|
-
public readonly publicKey?: Uint8Array
|
|
156
|
-
|
|
157
|
-
constructor (init: RSAPeerIdInit) {
|
|
158
|
-
super({ ...init, type: 'RSA' })
|
|
159
|
-
|
|
160
|
-
this.publicKey = init.publicKey
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
class Ed25519PeerIdImpl extends PeerIdImpl implements Ed25519PeerId {
|
|
165
|
-
public readonly type = 'Ed25519'
|
|
166
|
-
public readonly publicKey: Uint8Array
|
|
167
|
-
|
|
168
|
-
constructor (init: Ed25519PeerIdInit) {
|
|
169
|
-
super({ ...init, type: 'Ed25519' })
|
|
170
|
-
|
|
171
|
-
this.publicKey = init.multihash.digest
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
class Secp256k1PeerIdImpl extends PeerIdImpl implements Secp256k1PeerId {
|
|
176
|
-
public readonly type = 'secp256k1'
|
|
177
|
-
public readonly publicKey: Uint8Array
|
|
178
|
-
|
|
179
|
-
constructor (init: Secp256k1PeerIdInit) {
|
|
180
|
-
super({ ...init, type: 'secp256k1' })
|
|
181
|
-
|
|
182
|
-
this.publicKey = init.multihash.digest
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
187
31
|
const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920
|
|
188
32
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
readonly multihash: MultihashDigest
|
|
192
|
-
readonly privateKey?: Uint8Array
|
|
193
|
-
readonly publicKey?: Uint8Array
|
|
194
|
-
readonly url: string
|
|
195
|
-
|
|
196
|
-
constructor (url: URL) {
|
|
197
|
-
this.url = url.toString()
|
|
198
|
-
this.multihash = identity.digest(uint8ArrayFromString(this.url))
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
[inspect] (): string {
|
|
202
|
-
return `PeerId(${this.url})`
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
readonly [peerIdSymbol] = true
|
|
206
|
-
|
|
207
|
-
toString (): string {
|
|
208
|
-
return this.toCID().toString()
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
toCID (): CID {
|
|
212
|
-
return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.multihash)
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
toBytes (): Uint8Array {
|
|
216
|
-
return this.toCID().bytes
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
equals (other?: PeerId | Uint8Array | string): boolean {
|
|
220
|
-
if (other == null) {
|
|
221
|
-
return false
|
|
222
|
-
}
|
|
33
|
+
export function peerIdFromString (str: string, decoder?: MultibaseDecoder<any>): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId {
|
|
34
|
+
let multihash: MultihashDigest
|
|
223
35
|
|
|
224
|
-
|
|
225
|
-
|
|
36
|
+
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
|
37
|
+
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
|
38
|
+
// rsa public key - base58btc encoded either way
|
|
39
|
+
multihash = Digest.decode(base58btc.decode(`z${str}`))
|
|
40
|
+
} else {
|
|
41
|
+
if (decoder == null) {
|
|
42
|
+
throw new InvalidParametersError('Please pass a multibase decoder for strings that do not start with "1" or "Q"')
|
|
226
43
|
}
|
|
227
44
|
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export function createPeerId (init: PeerIdInit): Ed25519PeerId | Secp256k1PeerId | RSAPeerId {
|
|
233
|
-
if (init.type === 'RSA') {
|
|
234
|
-
return new RSAPeerIdImpl(init)
|
|
45
|
+
multihash = Digest.decode(decoder.decode(str))
|
|
235
46
|
}
|
|
236
47
|
|
|
237
|
-
|
|
238
|
-
return new Ed25519PeerIdImpl(init)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (init.type === 'secp256k1') {
|
|
242
|
-
return new Secp256k1PeerIdImpl(init)
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
throw new CodeError('Type must be "RSA", "Ed25519" or "secp256k1"', 'ERR_INVALID_PARAMETERS')
|
|
48
|
+
return peerIdFromMultihash(multihash)
|
|
246
49
|
}
|
|
247
50
|
|
|
248
|
-
export function
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (
|
|
254
|
-
return new
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
51
|
+
export function peerIdFromPublicKey (publicKey: Ed25519PublicKey): Ed25519PeerId
|
|
52
|
+
export function peerIdFromPublicKey (publicKey: Secp256k1PublicKey): Secp256k1PeerId
|
|
53
|
+
export function peerIdFromPublicKey (publicKey: RSAPublicKey): RSAPeerId
|
|
54
|
+
export function peerIdFromPublicKey (publicKey: PublicKey): PeerId
|
|
55
|
+
export function peerIdFromPublicKey (publicKey: PublicKey): PeerId {
|
|
56
|
+
if (publicKey.type === 'Ed25519') {
|
|
57
|
+
return new Ed25519PeerIdClass({
|
|
58
|
+
multihash: publicKey.toCID().multihash,
|
|
59
|
+
publicKey
|
|
60
|
+
})
|
|
61
|
+
} else if (publicKey.type === 'secp256k1') {
|
|
62
|
+
return new Secp256k1PeerIdClass({
|
|
63
|
+
multihash: publicKey.toCID().multihash,
|
|
64
|
+
publicKey
|
|
65
|
+
})
|
|
66
|
+
} else if (publicKey.type === 'RSA') {
|
|
67
|
+
return new RSAPeerIdClass({
|
|
68
|
+
multihash: publicKey.toCID().multihash,
|
|
69
|
+
publicKey
|
|
70
|
+
})
|
|
259
71
|
}
|
|
260
72
|
|
|
261
|
-
throw new
|
|
73
|
+
throw new UnsupportedKeyTypeError()
|
|
262
74
|
}
|
|
263
75
|
|
|
264
|
-
export function
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const multihash = Digest.decode(base58btc.decode(`z${str}`))
|
|
271
|
-
|
|
272
|
-
if (str.startsWith('12D')) {
|
|
273
|
-
return new Ed25519PeerIdImpl({ multihash })
|
|
274
|
-
} else if (str.startsWith('16U')) {
|
|
275
|
-
return new Secp256k1PeerIdImpl({ multihash })
|
|
276
|
-
} else {
|
|
277
|
-
return new RSAPeerIdImpl({ multihash })
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return peerIdFromBytes(baseDecoder.decode(str))
|
|
76
|
+
export function peerIdFromPrivateKey (privateKey: Ed25519PrivateKey): Ed25519PeerId
|
|
77
|
+
export function peerIdFromPrivateKey (privateKey: Secp256k1PrivateKey): Secp256k1PeerId
|
|
78
|
+
export function peerIdFromPrivateKey (privateKey: RSAPrivateKey): RSAPeerId
|
|
79
|
+
export function peerIdFromPrivateKey (privateKey: PrivateKey): PeerId
|
|
80
|
+
export function peerIdFromPrivateKey (privateKey: PrivateKey): PeerId {
|
|
81
|
+
return peerIdFromPublicKey(privateKey.publicKey)
|
|
282
82
|
}
|
|
283
83
|
|
|
284
|
-
export function
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
return new
|
|
84
|
+
export function peerIdFromMultihash (multihash: MultihashDigest): PeerId {
|
|
85
|
+
if (isSha256Multihash(multihash)) {
|
|
86
|
+
return new RSAPeerIdClass({ multihash })
|
|
87
|
+
} else if (isIdentityMultihash(multihash)) {
|
|
88
|
+
try {
|
|
89
|
+
const publicKey = publicKeyFromMultihash(multihash)
|
|
90
|
+
|
|
91
|
+
if (publicKey.type === 'Ed25519') {
|
|
92
|
+
return new Ed25519PeerIdClass({ multihash, publicKey })
|
|
93
|
+
} else if (publicKey.type === 'secp256k1') {
|
|
94
|
+
return new Secp256k1PeerIdClass({ multihash, publicKey })
|
|
293
95
|
}
|
|
294
|
-
}
|
|
96
|
+
} catch (err) {
|
|
97
|
+
// was not Ed or secp key, try URL
|
|
98
|
+
const url = uint8ArrayToString(multihash.digest)
|
|
295
99
|
|
|
296
|
-
|
|
297
|
-
return new RSAPeerIdImpl({ multihash })
|
|
100
|
+
return new URLPeerIdClass(new URL(url))
|
|
298
101
|
}
|
|
299
|
-
} catch {
|
|
300
|
-
return peerIdFromCID(CID.decode(buf))
|
|
301
102
|
}
|
|
302
103
|
|
|
303
|
-
throw new
|
|
104
|
+
throw new InvalidMultihashError('Supplied PeerID Multihash is invalid')
|
|
304
105
|
}
|
|
305
106
|
|
|
306
107
|
export function peerIdFromCID (cid: CID): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId {
|
|
307
108
|
if (cid?.multihash == null || cid.version == null || (cid.version === 1 && (cid.code !== LIBP2P_KEY_CODE) && cid.code !== TRANSPORT_IPFS_GATEWAY_HTTP_CODE)) {
|
|
308
|
-
throw new
|
|
109
|
+
throw new InvalidCIDError('Supplied PeerID CID is invalid')
|
|
309
110
|
}
|
|
310
111
|
|
|
311
112
|
if (cid.code === TRANSPORT_IPFS_GATEWAY_HTTP_CODE) {
|
|
312
113
|
const url = uint8ArrayToString(cid.multihash.digest)
|
|
313
114
|
|
|
314
|
-
return new
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const multihash = cid.multihash
|
|
318
|
-
|
|
319
|
-
if (multihash.code === sha256.code) {
|
|
320
|
-
return new RSAPeerIdImpl({ multihash: cid.multihash })
|
|
321
|
-
} else if (multihash.code === identity.code) {
|
|
322
|
-
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
323
|
-
return new Ed25519PeerIdImpl({ multihash: cid.multihash })
|
|
324
|
-
} else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
|
|
325
|
-
return new Secp256k1PeerIdImpl({ multihash: cid.multihash })
|
|
326
|
-
}
|
|
115
|
+
return new URLPeerIdClass(new URL(url))
|
|
327
116
|
}
|
|
328
117
|
|
|
329
|
-
|
|
118
|
+
return peerIdFromMultihash(cid.multihash)
|
|
330
119
|
}
|
|
331
120
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
*/
|
|
336
|
-
export async function peerIdFromKeys (publicKey: Uint8Array, privateKey?: Uint8Array): Promise<Ed25519PeerId | Secp256k1PeerId | RSAPeerId> {
|
|
337
|
-
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
338
|
-
return new Ed25519PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (publicKey.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
|
|
342
|
-
return new Secp256k1PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
343
|
-
}
|
|
121
|
+
function isIdentityMultihash (multihash: MultihashDigest): multihash is MultihashDigest<0x0> {
|
|
122
|
+
return multihash.code === identity.code
|
|
123
|
+
}
|
|
344
124
|
|
|
345
|
-
|
|
125
|
+
function isSha256Multihash (multihash: MultihashDigest): multihash is MultihashDigest<0x12> {
|
|
126
|
+
return multihash.code === sha256.code
|
|
346
127
|
}
|
package/src/peer-id.ts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* An implementation of a peer id
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```TypeScript
|
|
9
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
10
|
+
* const peer = peerIdFromString('k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s')
|
|
11
|
+
*
|
|
12
|
+
* console.log(peer.toCID()) // CID(bafzaa...)
|
|
13
|
+
* console.log(peer.toString()) // "12D3K..."
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { peerIdSymbol } from '@libp2p/interface'
|
|
18
|
+
import { base58btc } from 'multiformats/bases/base58'
|
|
19
|
+
import { CID } from 'multiformats/cid'
|
|
20
|
+
import { identity } from 'multiformats/hashes/identity'
|
|
21
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
22
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
23
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
24
|
+
import type { Ed25519PeerId as Ed25519PeerIdInterface, PeerIdType, RSAPeerId as RSAPeerIdInterface, URLPeerId as URLPeerIdInterface, Secp256k1PeerId as Secp256k1PeerIdInterface, PeerId, PublicKey, Ed25519PublicKey, Secp256k1PublicKey, RSAPublicKey } from '@libp2p/interface'
|
|
25
|
+
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
26
|
+
|
|
27
|
+
const inspect = Symbol.for('nodejs.util.inspect.custom')
|
|
28
|
+
|
|
29
|
+
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
30
|
+
const LIBP2P_KEY_CODE = 0x72
|
|
31
|
+
|
|
32
|
+
interface PeerIdInit <DigestCode extends number> {
|
|
33
|
+
type: PeerIdType
|
|
34
|
+
multihash: MultihashDigest<DigestCode>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface RSAPeerIdInit {
|
|
38
|
+
multihash: MultihashDigest<0x12>
|
|
39
|
+
publicKey?: RSAPublicKey
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface Ed25519PeerIdInit {
|
|
43
|
+
multihash: MultihashDigest<0x0>
|
|
44
|
+
publicKey: Ed25519PublicKey
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface Secp256k1PeerIdInit {
|
|
48
|
+
multihash: MultihashDigest<0x0>
|
|
49
|
+
publicKey: Secp256k1PublicKey
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class PeerIdImpl <DigestCode extends number> {
|
|
53
|
+
public type: PeerIdType
|
|
54
|
+
private readonly multihash: MultihashDigest<DigestCode>
|
|
55
|
+
public readonly publicKey?: PublicKey
|
|
56
|
+
private string?: string
|
|
57
|
+
|
|
58
|
+
constructor (init: PeerIdInit<DigestCode>) {
|
|
59
|
+
this.type = init.type
|
|
60
|
+
this.multihash = init.multihash
|
|
61
|
+
|
|
62
|
+
// mark string cache as non-enumerable
|
|
63
|
+
Object.defineProperty(this, 'string', {
|
|
64
|
+
enumerable: false,
|
|
65
|
+
writable: true
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get [Symbol.toStringTag] (): string {
|
|
70
|
+
return `PeerId(${this.toString()})`
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
readonly [peerIdSymbol] = true
|
|
74
|
+
|
|
75
|
+
toString (): string {
|
|
76
|
+
if (this.string == null) {
|
|
77
|
+
this.string = base58btc.encode(this.multihash.bytes).slice(1)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return this.string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
toMultihash (): MultihashDigest<DigestCode> {
|
|
84
|
+
return this.multihash
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// return self-describing String representation
|
|
88
|
+
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
|
|
89
|
+
toCID (): CID<Uint8Array, 0x72, DigestCode, 1> {
|
|
90
|
+
return CID.createV1(LIBP2P_KEY_CODE, this.multihash)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
toJSON (): string {
|
|
94
|
+
return this.toString()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Checks the equality of `this` peer against a given PeerId
|
|
99
|
+
*/
|
|
100
|
+
equals (id?: PeerId | Uint8Array | string): boolean {
|
|
101
|
+
if (id == null) {
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (id instanceof Uint8Array) {
|
|
106
|
+
return uint8ArrayEquals(this.multihash.bytes, id)
|
|
107
|
+
} else if (typeof id === 'string') {
|
|
108
|
+
return this.toString() === id
|
|
109
|
+
} else if (id?.toMultihash()?.bytes != null) {
|
|
110
|
+
return uint8ArrayEquals(this.multihash.bytes, id.toMultihash().bytes)
|
|
111
|
+
} else {
|
|
112
|
+
throw new Error('not valid Id')
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Returns PeerId as a human-readable string
|
|
118
|
+
* https://nodejs.org/api/util.html#utilinspectcustom
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```TypeScript
|
|
122
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
123
|
+
*
|
|
124
|
+
* console.info(peerIdFromString('QmFoo'))
|
|
125
|
+
* // 'PeerId(QmFoo)'
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
[inspect] (): string {
|
|
129
|
+
return `PeerId(${this.toString()})`
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export class RSAPeerId extends PeerIdImpl<0x12> implements RSAPeerIdInterface {
|
|
134
|
+
public readonly type = 'RSA'
|
|
135
|
+
public readonly publicKey?: RSAPublicKey
|
|
136
|
+
|
|
137
|
+
constructor (init: RSAPeerIdInit) {
|
|
138
|
+
super({ ...init, type: 'RSA' })
|
|
139
|
+
|
|
140
|
+
this.publicKey = init.publicKey
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class Ed25519PeerId extends PeerIdImpl<0x0> implements Ed25519PeerIdInterface {
|
|
145
|
+
public readonly type = 'Ed25519'
|
|
146
|
+
public readonly publicKey: Ed25519PublicKey
|
|
147
|
+
|
|
148
|
+
constructor (init: Ed25519PeerIdInit) {
|
|
149
|
+
super({ ...init, type: 'Ed25519' })
|
|
150
|
+
|
|
151
|
+
this.publicKey = init.publicKey
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export class Secp256k1PeerId extends PeerIdImpl<0x0> implements Secp256k1PeerIdInterface {
|
|
156
|
+
public readonly type = 'secp256k1'
|
|
157
|
+
public readonly publicKey: Secp256k1PublicKey
|
|
158
|
+
|
|
159
|
+
constructor (init: Secp256k1PeerIdInit) {
|
|
160
|
+
super({ ...init, type: 'secp256k1' })
|
|
161
|
+
|
|
162
|
+
this.publicKey = init.publicKey
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
167
|
+
const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920
|
|
168
|
+
|
|
169
|
+
export class URLPeerId implements URLPeerIdInterface {
|
|
170
|
+
readonly type = 'url'
|
|
171
|
+
readonly multihash: MultihashDigest<0x0>
|
|
172
|
+
readonly publicKey: undefined
|
|
173
|
+
readonly url: string
|
|
174
|
+
|
|
175
|
+
constructor (url: URL) {
|
|
176
|
+
this.url = url.toString()
|
|
177
|
+
this.multihash = identity.digest(uint8ArrayFromString(this.url))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
[inspect] (): string {
|
|
181
|
+
return `PeerId(${this.url})`
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
readonly [peerIdSymbol] = true
|
|
185
|
+
|
|
186
|
+
toString (): string {
|
|
187
|
+
return this.toCID().toString()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
toMultihash (): MultihashDigest<0x0> {
|
|
191
|
+
return this.multihash
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
toCID (): CID<Uint8Array, 0x0920, 0x0, 1> {
|
|
195
|
+
return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.toMultihash())
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
toJSON (): string {
|
|
199
|
+
return this.toString()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
equals (other?: PeerId | Uint8Array | string): boolean {
|
|
203
|
+
if (other == null) {
|
|
204
|
+
return false
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (other instanceof Uint8Array) {
|
|
208
|
+
other = uint8ArrayToString(other)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return other.toString() === this.toString()
|
|
212
|
+
}
|
|
213
|
+
}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"createPeerId": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.createPeerId.html",
|
|
3
|
-
".:createPeerId": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.createPeerId.html",
|
|
4
|
-
"peerIdFromBytes": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromBytes.html",
|
|
5
|
-
".:peerIdFromBytes": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromBytes.html",
|
|
6
|
-
"peerIdFromCID": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromCID.html",
|
|
7
|
-
".:peerIdFromCID": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromCID.html",
|
|
8
|
-
"peerIdFromKeys": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromKeys.html",
|
|
9
|
-
".:peerIdFromKeys": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromKeys.html",
|
|
10
|
-
"peerIdFromPeerId": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromPeerId.html",
|
|
11
|
-
".:peerIdFromPeerId": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromPeerId.html",
|
|
12
|
-
"peerIdFromString": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromString.html",
|
|
13
|
-
".:peerIdFromString": "https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_id.peerIdFromString.html"
|
|
14
|
-
}
|