-
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.
feat!: add pluggable enr crypto interface (#266)
- Loading branch information
1 parent
04152c1
commit 3475758
Showing
24 changed files
with
253 additions
and
236 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
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 |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import {itBench, setBenchOpts} from "@dapplion/benchmark"; | ||
import {createPeerIdFromKeypair, generateKeypair, KeypairType} from "../../src/keypair"; | ||
import {generateKeypair} from "../../src/keypair/index.js"; | ||
import { createPeerIdFromPrivateKey, createPeerIdFromPublicKey } from "../../src/enr/index.js"; | ||
|
||
describe("createPeerIdFromKeypair", function() { | ||
describe("createPeerIdFromPrivateKey", function() { | ||
setBenchOpts({runs: 4000}); | ||
|
||
const keypairWithPrivateKey = generateKeypair(KeypairType.secp256k1); | ||
const keypairWithoutPrivateKey = generateKeypair(KeypairType.secp256k1); | ||
delete (keypairWithoutPrivateKey as any)._privateKey; | ||
const keypair = generateKeypair("secp256k1"); | ||
|
||
itBench("createPeerIdFromKeypair - private key", () => { | ||
return createPeerIdFromKeypair(keypairWithPrivateKey); | ||
itBench("createPeerIdFromPrivateKey", () => { | ||
return createPeerIdFromPrivateKey(keypair.type, keypair.privateKey); | ||
}); | ||
itBench("createPeerIdFromKeypair - no private key", () => { | ||
return createPeerIdFromKeypair(keypairWithoutPrivateKey); | ||
itBench("createPeerIdFromPublicKey", () => { | ||
return createPeerIdFromPublicKey(keypair.type, keypair.publicKey); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import * as v4Crypto from "./v4.js"; | ||
export const v4 = v4Crypto; | ||
export * from "./constants.js"; | ||
export * from "./enr.js"; | ||
export * from "./types.js"; | ||
export * from "./create.js"; | ||
export * from "./peerId.js"; |
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,52 @@ | ||
import { PeerId } from "@libp2p/interface/peer-id"; | ||
import { peerIdFromKeys } from "@libp2p/peer-id"; | ||
import { keysPBM, supportedKeys } from "@libp2p/crypto/keys"; | ||
import { KeyType } from "@libp2p/interface/keys"; | ||
import { ERR_TYPE_NOT_IMPLEMENTED } from "../keypair/constants.js"; | ||
|
||
/** Translate to KeyType from keysPBM enum */ | ||
enum KeyTypeTranslator { | ||
RSA = "RSA", | ||
Ed25519 = "Ed25519", | ||
Secp256k1 = "secp256k1", | ||
} | ||
|
||
export async function createPeerIdFromPublicKey(type: KeyType, publicKey: Uint8Array): Promise<PeerId> { | ||
switch (type) { | ||
case "secp256k1": { | ||
const pubKey = new supportedKeys.secp256k1.Secp256k1PublicKey(publicKey); | ||
return peerIdFromKeys(pubKey.bytes); | ||
} | ||
default: | ||
throw new Error(ERR_TYPE_NOT_IMPLEMENTED); | ||
} | ||
} | ||
|
||
export async function createPeerIdFromPrivateKey(type: KeyType, privateKey: Uint8Array): Promise<PeerId> { | ||
switch (type) { | ||
case "secp256k1": { | ||
const privKey = new supportedKeys.secp256k1.Secp256k1PrivateKey(privateKey); | ||
return peerIdFromKeys(privKey.public.bytes, privKey.bytes); | ||
} | ||
default: | ||
throw new Error(ERR_TYPE_NOT_IMPLEMENTED); | ||
} | ||
} | ||
|
||
export function createPublicKeyFromPeerId(peerId: PeerId): { type: KeyType; publicKey: Uint8Array } { | ||
// pub/privkey bytes from peer-id are encoded in protobuf format | ||
if (!peerId.publicKey) { | ||
throw new Error("Public key required"); | ||
} | ||
const pub = keysPBM.PublicKey.decode(peerId.publicKey); | ||
return { type: KeyTypeTranslator[pub.Type!], publicKey: pub.Data! }; | ||
} | ||
|
||
export function createPrivateKeyFromPeerId(peerId: PeerId): { type: KeyType; privateKey: Uint8Array } { | ||
// pub/privkey bytes from peer-id are encoded in protobuf format | ||
if (!peerId.privateKey) { | ||
throw new Error("Private key required"); | ||
} | ||
const priv = keysPBM.PrivateKey.decode(peerId.privateKey); | ||
return { type: KeyTypeTranslator[priv.Type!], privateKey: priv.Data! }; | ||
} |
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,5 @@ | ||
// multiaddr 8.0.0 expects an Uint8Array with internal buffer starting at 0 offset | ||
export function toNewUint8Array(buf: Uint8Array): Uint8Array { | ||
const arrayBuffer = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); | ||
return new Uint8Array(arrayBuffer); | ||
} |
Oops, something went wrong.