Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit f80d1ea

Browse files
authored
fix: human readable peer ids in console.log (#36)
Add [inspect](https://nodejs.org/api/util.html#utilinspectcustom) method so when printing PeerIds in the console they are readble. Old: ``` { peer: RSAPeerIdImpl [PeerId(QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN)] { type: 'RSA', multihash: Digest { code: 18, size: 32, digest: [Uint8Array], bytes: [Uint8Array] }, privateKey: undefined, publicKey: <Buffer 08 00 12 a6 02 30 82 01 22 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 82 01 0f 00 30 82 01 0a 02 82 01 01 00 d7 ab 01 ee 41 35 d7 ce cd 99 63 5b ... 249 more bytes> }, name: 'DIALING_PEER', type: 7 } { to: RSAPeerIdImpl [PeerId(QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN)] { type: 'RSA', multihash: Digest { code: 18, size: 32, digest: [Uint8Array], bytes: [Uint8Array] }, privateKey: undefined, publicKey: <Buffer 08 00 12 a6 02 30 82 01 22 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 82 01 0f 00 30 82 01 0a 02 82 01 01 00 d7 ab 01 ee 41 35 d7 ce cd 99 63 5b ... 249 more bytes> }, type: 0, name: 'SENDING_QUERY', messageName: 'ADD_PROVIDER', messageType: 2 } { from: RSAPeerIdImpl [PeerId(QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN)] { type: 'RSA', multihash: Digest { code: 18, size: 32, digest: [Uint8Array], bytes: [Uint8Array] }, privateKey: undefined, publicKey: <Buffer 08 00 12 a6 02 30 82 01 22 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 82 01 0f 00 30 82 01 0a 02 82 01 01 00 d7 ab 01 ee 41 35 d7 ce cd 99 63 5b ... 249 more bytes> }, messageType: 'ADD_PROVIDER', name: 'PEER_RESPONSE', type: 1, messageName: 'ADD_PROVIDER', closer: [], providers: [] } ``` New: ``` { peer: PeerId(QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt), name: 'DIALING_PEER', type: 7 } { to: PeerId(QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt), type: 0, name: 'SENDING_QUERY', messageName: 'ADD_PROVIDER', messageType: 2 } { from: PeerId(QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt), messageType: 'ADD_PROVIDER', name: 'PEER_RESPONSE', type: 1, messageName: 'ADD_PROVIDER', closer: [], providers: [] } ```
1 parent f5bbf38 commit f80d1ea

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

packages/libp2p-peer-id/src/index.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { identity } from 'multiformats/hashes/identity'
66
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
77
import { sha256 } from 'multiformats/hashes/sha2'
88
import errcode from 'err-code'
9-
import { Ed25519PeerId, RSAPeerId, Secp256k1PeerId, symbol } from '@libp2p/interface-peer-id'
9+
import { Ed25519PeerId, PeerIdType, RSAPeerId, Secp256k1PeerId, symbol } from '@libp2p/interface-peer-id'
1010
import type { MultibaseDecoder } from 'multiformats/bases/interface'
1111
import type { MultihashDigest } from 'multiformats/hashes/interface'
1212
import type { PeerId } from '@libp2p/interface-peer-id'
1313

14+
const inspect = Symbol.for('nodejs.util.inspect.custom')
15+
1416
const baseDecoder = Object
1517
.values(bases)
1618
.map(codec => codec.decoder)
@@ -24,7 +26,7 @@ const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36
2426
const MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH = 37
2527

2628
interface PeerIdInit {
27-
type: 'RSA' | 'Ed25519' | 'secp256k1'
29+
type: PeerIdType
2830
multihash: MultihashDigest
2931
privateKey?: Uint8Array
3032
}
@@ -46,7 +48,7 @@ interface Secp256k1PeerIdInit {
4648
}
4749

4850
class PeerIdImpl {
49-
public type: 'RSA' | 'Ed25519' | 'secp256k1'
51+
public type: PeerIdType
5052
public readonly multihash: MultihashDigest
5153
public readonly privateKey?: Uint8Array
5254
public readonly publicKey?: Uint8Array
@@ -111,6 +113,22 @@ class PeerIdImpl {
111113
throw new Error('not valid Id')
112114
}
113115
}
116+
117+
/**
118+
* Returns PeerId as a human-readable string
119+
* https://nodejs.org/api/util.html#utilinspectcustom
120+
*
121+
* @example
122+
* ```js
123+
* import { peerIdFromString } from '@libp2p/peer-id'
124+
*
125+
* console.info(peerIdFromString('QmFoo'))
126+
* // 'PeerId(QmFoo)'
127+
* ```
128+
*/
129+
[inspect] (): string {
130+
return `PeerId(${this.toString()})`
131+
}
114132
}
115133

116134
class RSAPeerIdImpl extends PeerIdImpl implements RSAPeerId {
@@ -146,8 +164,20 @@ class Secp256k1PeerIdImpl extends PeerIdImpl implements Secp256k1PeerId {
146164
}
147165
}
148166

149-
export function createPeerId (init: PeerIdInit) {
150-
return new PeerIdImpl(init)
167+
export function createPeerId (init: PeerIdInit): PeerId {
168+
if (init.type === 'RSA') {
169+
return new RSAPeerIdImpl(init)
170+
}
171+
172+
if (init.type === 'Ed25519') {
173+
return new Ed25519PeerIdImpl(init)
174+
}
175+
176+
if (init.type === 'secp256k1') {
177+
return new Secp256k1PeerIdImpl(init)
178+
}
179+
180+
throw errcode(new Error('Type must be "RSA", "Ed25519" or "secp256k1"'), 'ERR_INVALID_PARAMETERS')
151181
}
152182

153183
export function peerIdFromPeerId (other: any): PeerId {

0 commit comments

Comments
 (0)