-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from ChainSafe/cayman/fast-create-peer-id
Optimize createPeerIdFromKeypair
- Loading branch information
Showing
6 changed files
with
874 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {itBench, setBenchOpts} from "@dapplion/benchmark"; | ||
import {createPeerIdFromKeypair, generateKeypair, KeypairType} from "../../src/keypair"; | ||
|
||
describe("createPeerIdFromKeypair", function() { | ||
setBenchOpts({runs: 4000}); | ||
|
||
const keypairWithPrivateKey = generateKeypair(KeypairType.secp256k1); | ||
const keypairWithoutPrivateKey = generateKeypair(KeypairType.secp256k1); | ||
delete (keypairWithoutPrivateKey as any)._privateKey; | ||
|
||
itBench("createPeerIdFromKeypair - private key", () => { | ||
return createPeerIdFromKeypair(keypairWithPrivateKey); | ||
}); | ||
itBench("createPeerIdFromKeypair - no private key", () => { | ||
return createPeerIdFromKeypair(keypairWithoutPrivateKey); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect } from "chai"; | ||
import PeerId from "peer-id"; | ||
import { keys } from "libp2p-crypto"; | ||
import { createPeerIdFromKeypair, generateKeypair, KeypairType } from "../../src/keypair"; | ||
const { keysPBM, supportedKeys } = keys; | ||
|
||
describe("createPeerIdFromKeypair", function() { | ||
it("should properly create a PeerId from a secp256k1 keypair with private key", async function() { | ||
const keypair = generateKeypair(KeypairType.secp256k1); | ||
const privKey = new supportedKeys.secp256k1.Secp256k1PrivateKey(keypair.privateKey, keypair.publicKey); | ||
|
||
const expectedPeerId = await PeerId.createFromPrivKey(privKey.bytes); | ||
const actualPeerId = await createPeerIdFromKeypair(keypair); | ||
|
||
expect(actualPeerId).to.be.deep.equal(expectedPeerId); | ||
}); | ||
it("should properly create a PeerId from a secp256k1 keypair without private key", async function() { | ||
const keypair = generateKeypair(KeypairType.secp256k1); | ||
delete (keypair as any)._privateKey; | ||
const pubKey = new supportedKeys.secp256k1.Secp256k1PublicKey(keypair.publicKey); | ||
|
||
const expectedPeerId = await PeerId.createFromPubKey(pubKey.bytes); | ||
const actualPeerId = await createPeerIdFromKeypair(keypair); | ||
|
||
expect(actualPeerId).to.be.deep.equal(expectedPeerId); | ||
}); | ||
}); |
Oops, something went wrong.