Skip to content

Commit

Permalink
fix: human readable peer ids in console.log (libp2p#36)
Browse files Browse the repository at this point in the history
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: []
}
```
  • Loading branch information
achingbrain authored Dec 8, 2022
1 parent f5bbf38 commit f80d1ea
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions packages/libp2p-peer-id/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { identity } from 'multiformats/hashes/identity'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { sha256 } from 'multiformats/hashes/sha2'
import errcode from 'err-code'
import { Ed25519PeerId, RSAPeerId, Secp256k1PeerId, symbol } from '@libp2p/interface-peer-id'
import { Ed25519PeerId, PeerIdType, RSAPeerId, Secp256k1PeerId, symbol } from '@libp2p/interface-peer-id'
import type { MultibaseDecoder } from 'multiformats/bases/interface'
import type { MultihashDigest } from 'multiformats/hashes/interface'
import type { PeerId } from '@libp2p/interface-peer-id'

const inspect = Symbol.for('nodejs.util.inspect.custom')

const baseDecoder = Object
.values(bases)
.map(codec => codec.decoder)
Expand All @@ -24,7 +26,7 @@ const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36
const MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH = 37

interface PeerIdInit {
type: 'RSA' | 'Ed25519' | 'secp256k1'
type: PeerIdType
multihash: MultihashDigest
privateKey?: Uint8Array
}
Expand All @@ -46,7 +48,7 @@ interface Secp256k1PeerIdInit {
}

class PeerIdImpl {
public type: 'RSA' | 'Ed25519' | 'secp256k1'
public type: PeerIdType
public readonly multihash: MultihashDigest
public readonly privateKey?: Uint8Array
public readonly publicKey?: Uint8Array
Expand Down Expand Up @@ -111,6 +113,22 @@ class PeerIdImpl {
throw new Error('not valid Id')
}
}

/**
* Returns PeerId as a human-readable string
* https://nodejs.org/api/util.html#utilinspectcustom
*
* @example
* ```js
* import { peerIdFromString } from '@libp2p/peer-id'
*
* console.info(peerIdFromString('QmFoo'))
* // 'PeerId(QmFoo)'
* ```
*/
[inspect] (): string {
return `PeerId(${this.toString()})`
}
}

class RSAPeerIdImpl extends PeerIdImpl implements RSAPeerId {
Expand Down Expand Up @@ -146,8 +164,20 @@ class Secp256k1PeerIdImpl extends PeerIdImpl implements Secp256k1PeerId {
}
}

export function createPeerId (init: PeerIdInit) {
return new PeerIdImpl(init)
export function createPeerId (init: PeerIdInit): PeerId {
if (init.type === 'RSA') {
return new RSAPeerIdImpl(init)
}

if (init.type === 'Ed25519') {
return new Ed25519PeerIdImpl(init)
}

if (init.type === 'secp256k1') {
return new Secp256k1PeerIdImpl(init)
}

throw errcode(new Error('Type must be "RSA", "Ed25519" or "secp256k1"'), 'ERR_INVALID_PARAMETERS')
}

export function peerIdFromPeerId (other: any): PeerId {
Expand Down

0 comments on commit f80d1ea

Please sign in to comment.