Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ethereum-cryptography from 1.2 to 2.0 #2641

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 85 additions & 87 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@ethereumjs/trie": "^5.0.4",
"@ethereumjs/tx": "^4.1.1",
"@ethereumjs/util": "^8.0.5",
"ethereum-cryptography": "^1.1.2"
"ethereum-cryptography": "^2.0.0"
},
"devDependencies": {
"@types/lru-cache": "^5.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@ethereumjs/util": "^8.0.5",
"abstract-level": "^1.0.3",
"debug": "^4.3.3",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"level": "^8.0.0",
"lru-cache": "^5.1.1",
"memory-level": "^1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"cors": "^2.8.5",
"c-kzg": "^1.0.8",
"debug": "^4.3.3",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"fs-extra": "^10.1.0",
"it-pipe": "^1.1.0",
"jayson": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/devp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@types/lru-cache": "^5.1.0",
"bl": "^1.1.2",
"debug": "^4.3.3",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"ip": "^1.1.3",
"k-bucket": "^5.0.0",
"lru-cache": "^5.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/devp2p/src/dpt/dpt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomBytes } from 'crypto'
import { getPublicKey } from 'ethereum-cryptography/secp256k1'
import { secp256k1 } from 'ethereum-cryptography/secp256k1'
import { EventEmitter } from 'events'
import ms = require('ms')

Expand Down Expand Up @@ -107,7 +107,7 @@ export class DPT extends EventEmitter {
super()

this.privateKey = Buffer.from(privateKey)
this._id = pk2id(Buffer.from(getPublicKey(this.privateKey, false)))
this._id = pk2id(Buffer.from(secp256k1.getPublicKey(this.privateKey, false)))
this._shouldFindNeighbours = options.shouldFindNeighbours ?? true
this._shouldGetDnsPeers = options.shouldGetDnsPeers ?? false
// By default, tries to connect to 12 new peers every 3s
Expand Down
6 changes: 3 additions & 3 deletions packages/devp2p/src/rlpx/ecies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RLP } from '@ethereumjs/rlp'
import { bufArrToArr } from '@ethereumjs/util'
import * as crypto from 'crypto'
import { debug as createDebugLogger } from 'debug'
import { getPublicKey } from 'ethereum-cryptography/secp256k1'
import { secp256k1 } from 'ethereum-cryptography/secp256k1'
import { ecdh, ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat'

import {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ECIES {

this._nonce = crypto.randomBytes(32)
this._ephemeralPrivateKey = genPrivateKey()
this._ephemeralPublicKey = Buffer.from(getPublicKey(this._ephemeralPrivateKey, false))
this._ephemeralPublicKey = Buffer.from(secp256k1.getPublicKey(this._ephemeralPrivateKey, false))
}

_encryptMessage(data: Buffer, sharedMacData: Buffer | null = null): Buffer | undefined {
Expand All @@ -108,7 +108,7 @@ export class ECIES {
.update(Buffer.concat([dataIV, sharedMacData]))
.digest()

const publicKey = getPublicKey(privateKey, false)
const publicKey = secp256k1.getPublicKey(privateKey, false)
return Buffer.concat([publicKey, dataIV, tag])
}

Expand Down
4 changes: 2 additions & 2 deletions packages/devp2p/src/rlpx/rlpx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { debug as createDebugLogger } from 'debug'
import { getPublicKey } from 'ethereum-cryptography/secp256k1'
import { secp256k1 } from 'ethereum-cryptography/secp256k1'
import { EventEmitter } from 'events'
import * as LRUCache from 'lru-cache'
import ms = require('ms')
Expand Down Expand Up @@ -59,7 +59,7 @@ export class RLPx extends EventEmitter {
super()

this._privateKey = Buffer.from(privateKey)
this._id = pk2id(Buffer.from(getPublicKey(this._privateKey, false)))
this._id = pk2id(Buffer.from(secp256k1.getPublicKey(this._privateKey, false)))

// options
this._timeout = options.timeout ?? ms('10s')
Expand Down
6 changes: 3 additions & 3 deletions packages/devp2p/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RLP } from '@ethereumjs/rlp'
import { arrToBufArr } from '@ethereumjs/util'
import { debug as createDebugLogger } from 'debug'
import { keccak256 as _keccak256 } from 'ethereum-cryptography/keccak'
import { utils } from 'ethereum-cryptography/secp256k1'
import { secp256k1 } from 'ethereum-cryptography/secp256k1'
import { publicKeyConvert } from 'ethereum-cryptography/secp256k1-compat'

import type { ETH } from './protocol/eth'
Expand All @@ -16,8 +16,8 @@ export function keccak256(...buffers: Buffer[]) {
}

export function genPrivateKey(): Buffer {
const privateKey = utils.randomPrivateKey()
return utils.isValidPrivateKey(privateKey) ? Buffer.from(privateKey) : genPrivateKey()
const privateKey = secp256k1.utils.randomPrivateKey()
return secp256k1.utils.isValidPrivateKey(privateKey) ? Buffer.from(privateKey) : genPrivateKey()
}

export function pk2id(pk: Buffer): Buffer {
Expand Down
2 changes: 1 addition & 1 deletion packages/ethash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@ethereumjs/util": "^8.0.5",
"abstract-level": "^1.0.3",
"bigint-crypto-utils": "^3.0.23",
"ethereum-cryptography": "^1.1.2"
"ethereum-cryptography": "^2.0.0"
},
"devDependencies": {
"@ethereumjs/common": "^3.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@ethereumjs/util": "^8.0.5",
"@ethersproject/providers": "^5.7.1",
"debug": "^4.3.3",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"mcl-wasm": "^0.7.1",
"rustbn.js": "~0.2.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/statemanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@ethereumjs/common": "^3.1.1",
"@ethereumjs/rlp": "^4.0.1",
"debug": "^4.3.3",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"ethers": "^5.7.1",
"js-sdsl": "^4.1.4"
},
Expand All @@ -56,7 +56,7 @@
"@types/tape": "^4.13.2",
"debug": "^4.3.3",
"eslint": "^8.0.0",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"functional-red-black-tree": "^1.0.1",
"karma": "^6.3.2",
"karma-chrome-launcher": "^3.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/trie/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@ethereumjs/rlp": "^4.0.1",
"@ethereumjs/util": "^8.0.5",
"@types/readable-stream": "^2.3.13",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"readable-stream": "^3.6.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@ethereumjs/common": "^3.1.1",
"@ethereumjs/rlp": "^4.0.1",
"@ethereumjs/util": "^8.0.5",
"ethereum-cryptography": "^1.1.2"
"ethereum-cryptography": "^2.0.0"
},
"peerDependencies": {
"c-kzg": "^1.0.8"
Expand Down
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"dependencies": {
"@chainsafe/ssz": "^0.11.0",
"@ethereumjs/rlp": "^4.0.1",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"micro-ftch": "^0.3.1"
},
"devDependencies": {
Expand Down
16 changes: 9 additions & 7 deletions packages/util/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RLP } from '@ethereumjs/rlp'
import { keccak256 } from 'ethereum-cryptography/keccak'
import { Point, utils } from 'ethereum-cryptography/secp256k1'
import { secp256k1 } from 'ethereum-cryptography/secp256k1'
import { bytesToHex } from 'ethereum-cryptography/utils'

import {
Expand Down Expand Up @@ -239,7 +239,7 @@ export const generateAddress2 = function (from: Buffer, salt: Buffer, initCode:
* Checks if the private key satisfies the rules of the curve secp256k1.
*/
export const isValidPrivate = function (privateKey: Buffer): boolean {
return utils.isValidPrivateKey(privateKey)
return secp256k1.utils.isValidPrivateKey(privateKey)
}

/**
Expand All @@ -254,7 +254,7 @@ export const isValidPublic = function (publicKey: Buffer, sanitize: boolean = fa
// Convert to SEC1 for secp256k1
// Automatically checks whether point is on curve
try {
Point.fromHex(Buffer.concat([Buffer.from([4]), publicKey]))
secp256k1.ProjectivePoint.fromHex(Buffer.concat([Buffer.from([4]), publicKey]))
return true
} catch (e) {
return false
Expand All @@ -266,7 +266,7 @@ export const isValidPublic = function (publicKey: Buffer, sanitize: boolean = fa
}

try {
Point.fromHex(publicKey)
secp256k1.ProjectivePoint.fromHex(publicKey)
return true
} catch (e) {
return false
Expand All @@ -282,7 +282,7 @@ export const isValidPublic = function (publicKey: Buffer, sanitize: boolean = fa
export const pubToAddress = function (pubKey: Buffer, sanitize: boolean = false): Buffer {
assertIsBuffer(pubKey)
if (sanitize && pubKey.length !== 64) {
pubKey = Buffer.from(Point.fromHex(pubKey).toRawBytes(false).slice(1))
pubKey = Buffer.from(secp256k1.ProjectivePoint.fromHex(pubKey).toRawBytes(false).slice(1))
}
if (pubKey.length !== 64) {
throw new Error('Expected pubKey to be of length 64')
Expand All @@ -299,7 +299,9 @@ export const publicToAddress = pubToAddress
export const privateToPublic = function (privateKey: Buffer): Buffer {
assertIsBuffer(privateKey)
// skip the type flag and use the X, Y points
return Buffer.from(Point.fromPrivateKey(privateKey).toRawBytes(false).slice(1))
return Buffer.from(
secp256k1.ProjectivePoint.fromPrivateKey(privateKey).toRawBytes(false).slice(1)
)
}

/**
Expand All @@ -316,7 +318,7 @@ export const privateToAddress = function (privateKey: Buffer): Buffer {
export const importPublic = function (publicKey: Buffer): Buffer {
assertIsBuffer(publicKey)
if (publicKey.length !== 64) {
publicKey = Buffer.from(Point.fromHex(publicKey).toRawBytes(false).slice(1))
publicKey = Buffer.from(secp256k1.ProjectivePoint.fromHex(publicKey).toRawBytes(false).slice(1))
}
return publicKey
}
Expand Down
6 changes: 3 additions & 3 deletions packages/util/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Buffer } from 'buffer'
import { CURVE } from 'ethereum-cryptography/secp256k1'
import { secp256k1 } from 'ethereum-cryptography/secp256k1'

/**
* 2^64-1
Expand All @@ -22,8 +22,8 @@ export const MAX_INTEGER_BIGINT = BigInt(
'115792089237316195423570985008687907853269984665640564039457584007913129639935'
)

export const SECP256K1_ORDER = CURVE.n
export const SECP256K1_ORDER_DIV_2 = CURVE.n / BigInt(2)
export const SECP256K1_ORDER = secp256k1.CURVE.n
export const SECP256K1_ORDER_DIV_2 = secp256k1.CURVE.n / BigInt(2)

/**
* 2^256
Expand Down
19 changes: 10 additions & 9 deletions packages/util/src/signature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keccak256 } from 'ethereum-cryptography/keccak'
import { recoverPublicKey, signSync } from 'ethereum-cryptography/secp256k1'
import { secp256k1 } from 'ethereum-cryptography/secp256k1'

import { bufferToBigInt, bufferToHex, bufferToInt, setLengthLeft, toBuffer } from './bytes'
import { SECP256K1_ORDER, SECP256K1_ORDER_DIV_2 } from './constants'
Expand All @@ -18,15 +18,15 @@ export interface ECDSASignature {
* accordingly, otherwise return a "static" `v` just derived from the `recovery` bit
*/
export function ecsign(msgHash: Buffer, privateKey: Buffer, chainId?: bigint): ECDSASignature {
const [signature, recovery] = signSync(msgHash, privateKey, { recovered: true, der: false })

const r = Buffer.from(signature.slice(0, 32))
const s = Buffer.from(signature.slice(32, 64))
const sig = secp256k1.sign(msgHash, privateKey)
const buf = sig.toCompactRawBytes()
const r = Buffer.from(buf.slice(0, 32))
const s = Buffer.from(buf.slice(32, 64))

const v =
chainId === undefined
? BigInt(recovery + 27)
: BigInt(recovery + 35) + BigInt(chainId) * BigInt(2)
? BigInt(sig.recovery! + 27)
: BigInt(sig.recovery! + 35) + BigInt(chainId) * BigInt(2)

return { r, s, v }
}
Expand Down Expand Up @@ -62,8 +62,9 @@ export const ecrecover = function (
throw new Error('Invalid signature v value')
}

const senderPubKey = recoverPublicKey(msgHash, signature, Number(recovery))
return Buffer.from(senderPubKey.slice(1))
const sig = secp256k1.Signature.fromCompact(signature).addRecoveryBit(Number(recovery))
const senderPubKey = sig.recoverPublicKey(msgHash)
return Buffer.from(senderPubKey.toRawBytes(false).slice(1))
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@ethereumjs/tx": "^4.1.1",
"@ethereumjs/util": "^8.0.5",
"debug": "^4.3.3",
"ethereum-cryptography": "^1.1.2",
"ethereum-cryptography": "^2.0.0",
"mcl-wasm": "^0.7.1",
"rustbn.js": "~0.2.0"
},
Expand Down