From 2edf6628676a6a3ab1f83cca2766a5526abc1ec1 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 8 Jun 2022 13:05:08 -0600 Subject: [PATCH 01/13] devp2p: import ethereum-cryptography package --- packages/devp2p/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/devp2p/package.json b/packages/devp2p/package.json index 0f67c2db79..6267dd18aa 100644 --- a/packages/devp2p/package.json +++ b/packages/devp2p/package.json @@ -49,6 +49,7 @@ "bl": "^1.1.2", "debug": "^4.3.3", "@ethereumjs/util": "^8.0.0", + "ethereum-cryptography": "^1.0.3", "hi-base32": "^0.5.0", "inherits": "^2.0.1", "ip": "^1.1.3", From 678fe75e6cb07b74dc46d2df062f81db7ad0a3ae Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 8 Jun 2022 13:05:38 -0600 Subject: [PATCH 02/13] devp2p: update utils to use ethereum-cryptography --- packages/devp2p/src/util.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/devp2p/src/util.ts b/packages/devp2p/src/util.ts index 1c27da4e79..516d047ad3 100644 --- a/packages/devp2p/src/util.ts +++ b/packages/devp2p/src/util.ts @@ -1,8 +1,7 @@ -import { randomBytes } from 'crypto' -import { privateKeyVerify, publicKeyConvert } from 'secp256k1' -import createKeccakHash from 'keccak' import { arrToBufArr } from '@ethereumjs/util' import RLP from 'rlp' +import { getPublicKey, utils } from 'ethereum-cryptography/secp256k1' +import { keccak256 as _keccak256 } from 'ethereum-cryptography/keccak' import { ETH } from './protocol/eth' import { LES } from './protocol/les' import { debug as createDebugLogger } from 'debug' @@ -11,17 +10,17 @@ export const devp2pDebug = createDebugLogger('devp2p') export function keccak256(...buffers: Buffer[]) { const buffer = Buffer.concat(buffers) - return createKeccakHash('keccak256').update(buffer).digest() + return Buffer.from(_keccak256(buffer)) } export function genPrivateKey(): Buffer { - const privateKey = randomBytes(32) - return privateKeyVerify(privateKey) ? privateKey : genPrivateKey() + const privateKey = utils.randomPrivateKey() + return utils.isValidPrivateKey(privateKey) ? Buffer.from(privateKey) : genPrivateKey() } export function pk2id(pk: Buffer): Buffer { if (pk.length === 33) { - pk = Buffer.from(publicKeyConvert(pk, false)) + pk = Buffer.from(getPublicKey(pk, false)) } return pk.slice(1) } From 735cead26a84725d583a5e4ab8c0e8f572b9aea8 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 8 Jun 2022 13:06:37 -0600 Subject: [PATCH 03/13] devp2p: update src components to use ethereum-cryptography --- packages/devp2p/src/dns/enr.ts | 2 +- packages/devp2p/src/dpt/dpt.ts | 4 ++-- packages/devp2p/src/dpt/message.ts | 5 +++-- packages/devp2p/src/rlpx/ecies.ts | 11 ++++++----- packages/devp2p/src/rlpx/rlpx.ts | 4 ++-- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/devp2p/src/dns/enr.ts b/packages/devp2p/src/dns/enr.ts index ca6d80e0bd..d673844686 100644 --- a/packages/devp2p/src/dns/enr.ts +++ b/packages/devp2p/src/dns/enr.ts @@ -1,12 +1,12 @@ import * as base32 from 'hi-base32' import { sscanf } from 'scanf' -import { ecdsaVerify } from 'secp256k1' import { Multiaddr } from 'multiaddr' import base64url from 'base64url' import { arrToBufArr, bufArrToArr } from '@ethereumjs/util' import RLP from 'rlp' import { PeerInfo } from '../dpt' import { toNewUint8Array, keccak256 } from '../util' +import { ecdsaVerify } from 'ethereum-cryptography/secp256k1-compat' const Convert = require('multiaddr/src/convert') diff --git a/packages/devp2p/src/dpt/dpt.ts b/packages/devp2p/src/dpt/dpt.ts index 274204d9dd..a87eeb4af1 100644 --- a/packages/devp2p/src/dpt/dpt.ts +++ b/packages/devp2p/src/dpt/dpt.ts @@ -1,6 +1,6 @@ import ms from 'ms' import { EventEmitter } from 'events' -import { publicKeyCreate } from 'secp256k1' +import { getPublicKey } from 'ethereum-cryptography/secp256k1' import { randomBytes } from 'crypto' // import { debug as createDebugLogger } from 'debug' import { devp2pDebug } from '../util' @@ -106,7 +106,7 @@ export class DPT extends EventEmitter { super() this.privateKey = Buffer.from(privateKey) - this._id = pk2id(Buffer.from(publicKeyCreate(this.privateKey, false))) + this._id = pk2id(Buffer.from(getPublicKey(this.privateKey, false))) this._shouldFindNeighbours = options.shouldFindNeighbours === false ? false : true this._shouldGetDnsPeers = options.shouldGetDnsPeers ?? false // By default, tries to connect to 12 new peers every 3s diff --git a/packages/devp2p/src/dpt/message.ts b/packages/devp2p/src/dpt/message.ts index fefd42bdec..b147f4091e 100644 --- a/packages/devp2p/src/dpt/message.ts +++ b/packages/devp2p/src/dpt/message.ts @@ -3,6 +3,7 @@ import ip from 'ip' import secp256k1 from 'secp256k1' import { bufArrToArr } from '@ethereumjs/util' import RLP from 'rlp' +import { ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat' import { keccak256, int2buffer, buffer2int, assertEq, unstrictDecode } from '../util' import { PeerInfo } from './dpt' @@ -176,7 +177,7 @@ export function encode(typename: string, data: T, privateKey: Buffer) { ]) const sighash = keccak256(typedata) - const sig = secp256k1.ecdsaSign(sighash, privateKey) + const sig = ecdsaSign(sighash, privateKey) const hashdata = Buffer.concat([Buffer.from(sig.signature), Buffer.from([sig.recid]), typedata]) const hash = keccak256(hashdata) return Buffer.concat([hash, hashdata]) @@ -195,7 +196,7 @@ export function decode(buffer: Buffer) { const sighash = keccak256(typedata) const signature = buffer.slice(32, 96) const recoverId = buffer[96] - const publicKey = Buffer.from(secp256k1.ecdsaRecover(signature, recoverId, sighash, false)) + const publicKey = Buffer.from(ecdsaRecover(signature, recoverId, sighash, false)) return { typename, data, publicKey } } diff --git a/packages/devp2p/src/rlpx/ecies.ts b/packages/devp2p/src/rlpx/ecies.ts index 06488088ef..e7d08d93d2 100644 --- a/packages/devp2p/src/rlpx/ecies.ts +++ b/packages/devp2p/src/rlpx/ecies.ts @@ -3,6 +3,7 @@ import { debug as createDebugLogger } from 'debug' import { publicKeyCreate, ecdh, ecdsaRecover, ecdsaSign } from 'secp256k1' import { bufArrToArr } from '@ethereumjs/util' import RLP from 'rlp' +import { getPublicKey } from 'ethereum-cryptography/secp256k1' import { unstrictDecode } from '../util' import { MAC } from './mac' @@ -17,6 +18,7 @@ import { buffer2int, zfill, } from '../util' +import { ecdsaSign, ecdsaRecover, ecdh } from 'ethereum-cryptography/secp256k1-compat' const debug = createDebugLogger('devp2p:rlpx:peer') @@ -26,10 +28,9 @@ function ecdhX(publicKey: Buffer, privateKey: Buffer) { const pubKey = new Uint8Array(33) pubKey[0] = (y[31] & 1) === 0 ? 0x02 : 0x03 pubKey.set(x, 1) - return pubKey + return pubKey.slice(1) } - // @ts-ignore - return Buffer.from(ecdh(publicKey, privateKey, { hashfn }, Buffer.alloc(33)).slice(1)) + return Buffer.from(ecdh(publicKey, privateKey, { hashfn: hashfn }, Buffer.alloc(32))) } // a straigth rip from python interop w/go ecies implementation @@ -80,7 +81,7 @@ export class ECIES { this._nonce = crypto.randomBytes(32) this._ephemeralPrivateKey = genPrivateKey() - this._ephemeralPublicKey = Buffer.from(publicKeyCreate(this._ephemeralPrivateKey, false)) + this._ephemeralPublicKey = Buffer.from(getPublicKey(this._ephemeralPrivateKey, false)) } _encryptMessage(data: Buffer, sharedMacData: Buffer | null = null): Buffer | undefined { @@ -106,7 +107,7 @@ export class ECIES { .update(Buffer.concat([dataIV, sharedMacData])) .digest() - const publicKey = publicKeyCreate(privateKey, false) + const publicKey = getPublicKey(privateKey, false) return Buffer.concat([publicKey, dataIV, tag]) } diff --git a/packages/devp2p/src/rlpx/rlpx.ts b/packages/devp2p/src/rlpx/rlpx.ts index fe2fb73790..a16969bacb 100644 --- a/packages/devp2p/src/rlpx/rlpx.ts +++ b/packages/devp2p/src/rlpx/rlpx.ts @@ -1,7 +1,7 @@ import * as net from 'net' import * as os from 'os' import ms from 'ms' -import { publicKeyCreate } from 'secp256k1' +import { getPublicKey } from 'ethereum-cryptography/secp256k1' import { EventEmitter } from 'events' import { debug as createDebugLogger, Debugger } from 'debug' import { devp2pDebug } from '../util' @@ -54,7 +54,7 @@ export class RLPx extends EventEmitter { super() this._privateKey = Buffer.from(privateKey) - this._id = pk2id(Buffer.from(publicKeyCreate(this._privateKey, false))) + this._id = pk2id(Buffer.from(getPublicKey(this._privateKey, false))) // options this._timeout = options.timeout ?? ms('10s') From 4d76a8044b5bfa88b08127689b8be2bf91f11049 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 8 Jun 2022 13:06:59 -0600 Subject: [PATCH 04/13] devp2p: update tests to use ethereum-cryptography --- packages/devp2p/test/dpt-message.ts | 4 ++-- packages/devp2p/test/rlpx-ecies.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/devp2p/test/dpt-message.ts b/packages/devp2p/test/dpt-message.ts index df5b245271..40534b7265 100644 --- a/packages/devp2p/test/dpt-message.ts +++ b/packages/devp2p/test/dpt-message.ts @@ -1,12 +1,12 @@ +import { publicKeyCreate } from 'ethereum-cryptography/secp256k1-compat' import test from 'tape' -import * as secp256k1 from 'secp256k1' import * as message from '../src/dpt/message' const privateKey = Buffer.from( 'b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291', 'hex' ) -const publicKey = Buffer.from(secp256k1.publicKeyCreate(privateKey, false)) +const publicKey = Buffer.from(publicKeyCreate(privateKey, false)) test('ping packet with version 4, additional list elements', (t) => { const buffer = Buffer.from( diff --git a/packages/devp2p/test/rlpx-ecies.ts b/packages/devp2p/test/rlpx-ecies.ts index f99bb012da..854654e862 100644 --- a/packages/devp2p/test/rlpx-ecies.ts +++ b/packages/devp2p/test/rlpx-ecies.ts @@ -1,10 +1,10 @@ import { randomBytes } from 'crypto' -import * as secp256k1 from 'secp256k1' import test, { Test } from 'tape' import * as util from '../src/util' import { ECIES } from '../src/rlpx/ecies' import testdata from './testdata.json' +import { publicKeyCreate } from 'ethereum-cryptography/secp256k1-compat' declare module 'tape' { export interface Test { @@ -16,8 +16,8 @@ function randomBefore(fn: Function) { return (t: Test) => { const privateKey1 = util.genPrivateKey() const privateKey2 = util.genPrivateKey() - const publicKey1 = Buffer.from(secp256k1.publicKeyCreate(privateKey1, false)) - const publicKey2 = Buffer.from(secp256k1.publicKeyCreate(privateKey2, false)) + const publicKey1 = Buffer.from(publicKeyCreate(privateKey1, false)) + const publicKey2 = Buffer.from(publicKeyCreate(privateKey2, false)) t.context = { a: new ECIES(privateKey1, util.pk2id(publicKey1), util.pk2id(publicKey2)), b: new ECIES(privateKey2, util.pk2id(publicKey2), util.pk2id(publicKey1)), From ebebc5c4c28eda807d34165e33180c7320fe3fb8 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 8 Jun 2022 13:07:16 -0600 Subject: [PATCH 05/13] devp2p: remove secp256k1 dependency --- packages/devp2p/package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/devp2p/package.json b/packages/devp2p/package.json index 6267dd18aa..7ba5bb5734 100644 --- a/packages/devp2p/package.json +++ b/packages/devp2p/package.json @@ -60,7 +60,6 @@ "multiaddr": "^10.0.1", "rlp": "^3.0.0", "scanf": "^1.1.2", - "secp256k1": "^4.0.2", "snappyjs": "^0.6.1" }, "devDependencies": { @@ -72,7 +71,6 @@ "@types/keccak": "^3.0.1", "@types/ms": "^0.7.30", "@types/node": "^16.11.7", - "@types/secp256k1": "^4.0.1", "@types/tape": "^4.13.2", "chalk": "^2.4.2", "eslint": "^6.8.0", From 4713cb0b67ffe41732315a06f79a5b84f2ef98ca Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Sat, 11 Jun 2022 22:06:17 -0400 Subject: [PATCH 06/13] Rebase on master --- package-lock.json | 6 ++---- packages/devp2p/src/rlpx/ecies.ts | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7139986ecd..c6a627f864 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19370,6 +19370,7 @@ "base64url": "^3.0.1", "bl": "^1.1.2", "debug": "^4.3.3", + "ethereum-cryptography": "^1.0.3", "hi-base32": "^0.5.0", "inherits": "^2.0.1", "ip": "^1.1.3", @@ -19380,7 +19381,6 @@ "multiaddr": "^10.0.1", "rlp": "^3.0.0", "scanf": "^1.1.2", - "secp256k1": "^4.0.2", "snappyjs": "^0.6.1" }, "devDependencies": { @@ -19392,7 +19392,6 @@ "@types/keccak": "^3.0.1", "@types/ms": "^0.7.30", "@types/node": "^16.11.7", - "@types/secp256k1": "^4.0.1", "@types/tape": "^4.13.2", "chalk": "^2.4.2", "eslint": "^6.8.0", @@ -20296,13 +20295,13 @@ "@types/lru-cache": "^5.1.0", "@types/ms": "^0.7.30", "@types/node": "^16.11.7", - "@types/secp256k1": "^4.0.1", "@types/tape": "^4.13.2", "base64url": "^3.0.1", "bl": "^1.1.2", "chalk": "^2.4.2", "debug": "^4.3.3", "eslint": "^6.8.0", + "ethereum-cryptography": "^1.0.3", "hi-base32": "^0.5.0", "inherits": "^2.0.1", "ip": "^1.1.3", @@ -20315,7 +20314,6 @@ "prettier": "^2.0.5", "rlp": "^3.0.0", "scanf": "^1.1.2", - "secp256k1": "^4.0.2", "snappyjs": "^0.6.1", "tape": "^5.3.1", "testdouble": "^3.8.2", diff --git a/packages/devp2p/src/rlpx/ecies.ts b/packages/devp2p/src/rlpx/ecies.ts index e7d08d93d2..e1053b4a82 100644 --- a/packages/devp2p/src/rlpx/ecies.ts +++ b/packages/devp2p/src/rlpx/ecies.ts @@ -1,6 +1,5 @@ import crypto, { Decipher } from 'crypto' import { debug as createDebugLogger } from 'debug' -import { publicKeyCreate, ecdh, ecdsaRecover, ecdsaSign } from 'secp256k1' import { bufArrToArr } from '@ethereumjs/util' import RLP from 'rlp' import { getPublicKey } from 'ethereum-cryptography/secp256k1' From 3c616995d755b8c5b2f02c25b1c9e8ee97c3c860 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 15 Jun 2022 00:02:37 -0600 Subject: [PATCH 07/13] devp2p: replace 'hi-base32' dependency with '@scure/base' --- packages/devp2p/package.json | 2 +- packages/devp2p/src/dns/enr.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/devp2p/package.json b/packages/devp2p/package.json index 7ba5bb5734..3b5528d396 100644 --- a/packages/devp2p/package.json +++ b/packages/devp2p/package.json @@ -42,6 +42,7 @@ }, "dependencies": { "@ethereumjs/common": "^2.6.4", + "@scure/base": "1.1.0", "@types/bl": "^2.1.0", "@types/k-bucket": "^5.0.0", "@types/lru-cache": "^5.1.0", @@ -50,7 +51,6 @@ "debug": "^4.3.3", "@ethereumjs/util": "^8.0.0", "ethereum-cryptography": "^1.0.3", - "hi-base32": "^0.5.0", "inherits": "^2.0.1", "ip": "^1.1.3", "k-bucket": "^5.0.0", diff --git a/packages/devp2p/src/dns/enr.ts b/packages/devp2p/src/dns/enr.ts index d673844686..3a231daf25 100644 --- a/packages/devp2p/src/dns/enr.ts +++ b/packages/devp2p/src/dns/enr.ts @@ -1,4 +1,4 @@ -import * as base32 from 'hi-base32' +import { base32 } from '@scure/base' import { sscanf } from 'scanf' import { Multiaddr } from 'multiaddr' import base64url from 'base64url' @@ -109,7 +109,7 @@ export class ENR { if (!rootVals.seq) throw new Error("Could not parse 'seq' value from ENR root entry") if (!rootVals.signature) throw new Error("Could not parse 'sig' value from ENR root entry") - const decodedPublicKey = base32.decode.asBytes(publicKey) + const decodedPublicKey = [...base32.decode(publicKey + '===').values()] // The signature is a 65-byte secp256k1 over the keccak256 hash // of the record content, excluding the `sig=` part, encoded as URL-safe base64 string From 765c27233919da59193b434eb34fd6ddfd4b3aef Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 15 Jun 2022 00:13:25 -0600 Subject: [PATCH 08/13] devp2p: delete unused import --- packages/devp2p/src/dpt/message.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/devp2p/src/dpt/message.ts b/packages/devp2p/src/dpt/message.ts index b147f4091e..8c3d376dcf 100644 --- a/packages/devp2p/src/dpt/message.ts +++ b/packages/devp2p/src/dpt/message.ts @@ -1,6 +1,5 @@ import { debug as createDebugLogger } from 'debug' import ip from 'ip' -import secp256k1 from 'secp256k1' import { bufArrToArr } from '@ethereumjs/util' import RLP from 'rlp' import { ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat' From 5acdcad5c8a056bcb4954cc16d4c65141c67c596 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 15 Jun 2022 00:56:45 -0600 Subject: [PATCH 09/13] devp2p: replace `keccak` use with `@noble/hashes` --- packages/devp2p/package.json | 3 +-- packages/devp2p/src/rlpx/mac.ts | 9 +++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/devp2p/package.json b/packages/devp2p/package.json index 3b5528d396..1c2013a5e6 100644 --- a/packages/devp2p/package.json +++ b/packages/devp2p/package.json @@ -51,10 +51,10 @@ "debug": "^4.3.3", "@ethereumjs/util": "^8.0.0", "ethereum-cryptography": "^1.0.3", + "@noble/hashes": "1.1.1", "inherits": "^2.0.1", "ip": "^1.1.3", "k-bucket": "^5.0.0", - "keccak": "^3.0.1", "lru-cache": "^5.1.1", "ms": "^0.7.1", "multiaddr": "^10.0.1", @@ -68,7 +68,6 @@ "@types/chalk": "^2.2.0", "@types/debug": "^4.1.4", "@types/ip": "^1.1.0", - "@types/keccak": "^3.0.1", "@types/ms": "^0.7.30", "@types/node": "^16.11.7", "@types/tape": "^4.13.2", diff --git a/packages/devp2p/src/rlpx/mac.ts b/packages/devp2p/src/rlpx/mac.ts index b9a6800a5d..1e7ac04494 100644 --- a/packages/devp2p/src/rlpx/mac.ts +++ b/packages/devp2p/src/rlpx/mac.ts @@ -1,12 +1,13 @@ import { createCipheriv } from 'crypto' -import createKeccakHash from 'keccak' +import { Keccak, keccak_256 } from '@noble/hashes/sha3' import { xor } from '../util' +import { Hash } from '@noble/hashes/utils' export class MAC { - _hash: any + _hash: Hash _secret: Buffer constructor(secret: Buffer) { - this._hash = createKeccakHash('keccak256') + this._hash = keccak_256.create() this._secret = secret } @@ -29,6 +30,6 @@ export class MAC { } digest() { - return this._hash._clone().digest().slice(0, 16) + return Buffer.from(this._hash.clone().digest().slice(0, 16)) } } From 3af0ea6a2a799495f426fd355aacbfe367ca068f Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 15 Jun 2022 23:14:36 -0600 Subject: [PATCH 10/13] devp2p: replace base64url dependency with base64url from '@scure/base' --- packages/devp2p/package.json | 1 - packages/devp2p/src/dns/enr.ts | 10 ++++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/devp2p/package.json b/packages/devp2p/package.json index 1c2013a5e6..be3b583c6a 100644 --- a/packages/devp2p/package.json +++ b/packages/devp2p/package.json @@ -46,7 +46,6 @@ "@types/bl": "^2.1.0", "@types/k-bucket": "^5.0.0", "@types/lru-cache": "^5.1.0", - "base64url": "^3.0.1", "bl": "^1.1.2", "debug": "^4.3.3", "@ethereumjs/util": "^8.0.0", diff --git a/packages/devp2p/src/dns/enr.ts b/packages/devp2p/src/dns/enr.ts index 3a231daf25..9ce64c1525 100644 --- a/packages/devp2p/src/dns/enr.ts +++ b/packages/devp2p/src/dns/enr.ts @@ -1,7 +1,6 @@ -import { base32 } from '@scure/base' +import { base32, base64url } from '@scure/base' import { sscanf } from 'scanf' import { Multiaddr } from 'multiaddr' -import base64url from 'base64url' import { arrToBufArr, bufArrToArr } from '@ethereumjs/util' import RLP from 'rlp' import { PeerInfo } from '../dpt' @@ -52,7 +51,7 @@ export class ENR { throw new Error(`String encoded ENR must start with '${this.RECORD_PREFIX}'`) // ENRs are RLP encoded and written to DNS TXT entries as base64 url-safe strings - const base64BufferEnr = base64url.toBuffer(enr.slice(this.RECORD_PREFIX.length)) + const base64BufferEnr = Buffer.from(base64url.decode(enr.slice(this.RECORD_PREFIX.length))) const decoded = arrToBufArr(RLP.decode(Uint8Array.from(base64BufferEnr))) as Buffer[] const [signature, seq, ...kvs] = decoded @@ -116,7 +115,10 @@ export class ENR { // (Trailing recovery bit must be trimmed to pass `ecdsaVerify` method) const signedComponent = root.split(' sig')[0] const signedComponentBuffer = Buffer.from(signedComponent) - const signatureBuffer = base64url.toBuffer(rootVals.signature).slice(0, 64) + const signatureBuffer = Buffer.from( + [...base64url.decode(rootVals.signature + '=').values()].slice(0, 64) + ) + const keyBuffer = Buffer.from(decodedPublicKey) const isVerified = ecdsaVerify(signatureBuffer, keccak256(signedComponentBuffer), keyBuffer) From 2f48b9ceef2450aa44712103c4925e2ea2867554 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 15 Jun 2022 23:15:24 -0600 Subject: [PATCH 11/13] devp2p: use publicKeyConvert instead of getPublicKey --- packages/devp2p/src/util.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/devp2p/src/util.ts b/packages/devp2p/src/util.ts index 516d047ad3..7bff5468f5 100644 --- a/packages/devp2p/src/util.ts +++ b/packages/devp2p/src/util.ts @@ -1,6 +1,7 @@ import { arrToBufArr } from '@ethereumjs/util' import RLP from 'rlp' -import { getPublicKey, utils } from 'ethereum-cryptography/secp256k1' +import { utils } from 'ethereum-cryptography/secp256k1' +import { publicKeyConvert } from 'ethereum-cryptography/secp256k1-compat' import { keccak256 as _keccak256 } from 'ethereum-cryptography/keccak' import { ETH } from './protocol/eth' import { LES } from './protocol/les' @@ -20,7 +21,7 @@ export function genPrivateKey(): Buffer { export function pk2id(pk: Buffer): Buffer { if (pk.length === 33) { - pk = Buffer.from(getPublicKey(pk, false)) + pk = Buffer.from(publicKeyConvert(pk, false)) } return pk.slice(1) } From 788b9ddc88081c3ca5d939d6e9afb3dbb35d2627 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 15 Jun 2022 23:16:13 -0600 Subject: [PATCH 12/13] devp2p: use exported keccak256.create() from ethereum-cryptography define Hash type based on return type --- packages/devp2p/src/rlpx/mac.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/devp2p/src/rlpx/mac.ts b/packages/devp2p/src/rlpx/mac.ts index 1e7ac04494..e14c3b3de8 100644 --- a/packages/devp2p/src/rlpx/mac.ts +++ b/packages/devp2p/src/rlpx/mac.ts @@ -1,13 +1,14 @@ import { createCipheriv } from 'crypto' -import { Keccak, keccak_256 } from '@noble/hashes/sha3' +import { keccak256 } from 'ethereum-cryptography/keccak' import { xor } from '../util' -import { Hash } from '@noble/hashes/utils' + +export type Hash = ReturnType export class MAC { - _hash: Hash + _hash: Hash _secret: Buffer constructor(secret: Buffer) { - this._hash = keccak_256.create() + this._hash = keccak256.create() this._secret = secret } From 430cfcca0d76ffa64b8cb9da9a1eefb6605134ac Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 15 Jun 2022 23:16:35 -0600 Subject: [PATCH 13/13] devp2p: update ethereum-cryptography dependency to 1.1.0 --- packages/devp2p/package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/devp2p/package.json b/packages/devp2p/package.json index be3b583c6a..3aae73ed2d 100644 --- a/packages/devp2p/package.json +++ b/packages/devp2p/package.json @@ -42,15 +42,14 @@ }, "dependencies": { "@ethereumjs/common": "^2.6.4", - "@scure/base": "1.1.0", + "@scure/base": "1.1.1", "@types/bl": "^2.1.0", "@types/k-bucket": "^5.0.0", "@types/lru-cache": "^5.1.0", "bl": "^1.1.2", "debug": "^4.3.3", "@ethereumjs/util": "^8.0.0", - "ethereum-cryptography": "^1.0.3", - "@noble/hashes": "1.1.1", + "ethereum-cryptography": "^1.1.0", "inherits": "^2.0.1", "ip": "^1.1.3", "k-bucket": "^5.0.0",