libp2p / js-peer-id

peer-id implementation in JavaScript. Deprecated; use https://github.com/libp2p/js-libp2p-peer-id instead.
https://github.com/libp2p/js-libp2p-peer-id
MIT License
80 stars 44 forks source link

Use undefined instead of null privKey #154

Closed wemeetagain closed 3 years ago

wemeetagain commented 3 years ago

Respect the PeerId#privKey typescript type, which is PrivateKey | undefined, not PrivateKey | null

Found this in the course of manually creating a peer id using typescript.

import {expect} from "chai";
import PeerId from "peer-id";
import mh from "multihashes";
import { keys } from "libp2p-crypto";
const { supportedKeys } = keys;

async function test () {
  const privKey = await supportedKeys.secp256k1.generateKeyPair();
  const pubKey = privKey.public;
  const id = mh.encode(pubKey.bytes, "identity");

  // these two should be identical
  const manualPeerId = new PeerId(id, undefined, pubKey); 
  const peerId = await PeerId.createFromPubKey(pubKey.bytes);

  // but they aren't
  expect(manualPeerId).to.deep.equal(peerId);
  // replace 'undefined' with 'null as any' to avoid error
}

test();