-
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: use ethereum-cryptography for enr crypto (#285)
- Loading branch information
1 parent
594166c
commit 50cee57
Showing
11 changed files
with
114 additions
and
34 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
3 changes: 1 addition & 2 deletions
3
packages/enr/src/v4.ts → packages/discv5/src/enr/bcryptoV4Crypto.ts
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,6 @@ | ||
// This module has the side effect of setting the ENR crypto implementations to use bcrypto | ||
|
||
import { setV4Crypto } from "@chainsafe/enr"; | ||
import * as bcryptoV4Crypto from "./bcryptoV4Crypto.js"; | ||
|
||
setV4Crypto(bcryptoV4Crypto); |
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,22 @@ | ||
import { NodeId } from "./types.js"; | ||
import * as defaultCrypto from "./defaultCrypto.js"; | ||
|
||
/** | ||
* In order to support different environments (eg: browser vs high performance), a pluggable crypto interface is provided | ||
*/ | ||
export type V4Crypto = { | ||
publicKey(privKey: Uint8Array): Uint8Array; | ||
sign(privKey: Uint8Array, msg: Uint8Array): Uint8Array; | ||
verify(pubKey: Uint8Array, msg: Uint8Array, sig: Uint8Array): boolean; | ||
nodeId(pubKey: Uint8Array): NodeId; | ||
}; | ||
|
||
let v4: V4Crypto = defaultCrypto; | ||
|
||
export function setV4Crypto(crypto: V4Crypto): void { | ||
v4 = crypto; | ||
} | ||
|
||
export function getV4Crypto(): V4Crypto { | ||
return v4; | ||
} |
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,29 @@ | ||
import { keccak256 } from "ethereum-cryptography/keccak"; | ||
import { secp256k1 } from "ethereum-cryptography/secp256k1"; | ||
|
||
import { createNodeId } from "./create.js"; | ||
import { NodeId } from "./types.js"; | ||
|
||
export function hash(input: Uint8Array): Uint8Array { | ||
return keccak256(input); | ||
} | ||
|
||
export function publicKey(privKey: Uint8Array): Uint8Array { | ||
return secp256k1.getPublicKey(privKey, true); | ||
} | ||
|
||
export function sign(privKey: Uint8Array, msg: Uint8Array): Uint8Array { | ||
return secp256k1.sign(hash(msg), privKey).toCompactRawBytes(); | ||
} | ||
|
||
export function verify(pubKey: Uint8Array, msg: Uint8Array, sig: Uint8Array): boolean { | ||
return secp256k1.verify(sig, hash(msg), pubKey); | ||
} | ||
|
||
function uncompressPublicKey(pubKey: Uint8Array): Uint8Array { | ||
return secp256k1.ProjectivePoint.fromHex(pubKey).toRawBytes(false); | ||
} | ||
|
||
export function nodeId(pubKey: Uint8Array): NodeId { | ||
return createNodeId(Buffer.from(hash(uncompressPublicKey(pubKey).slice(1)))); | ||
} |
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