-
Notifications
You must be signed in to change notification settings - Fork 808
Util: remove ecsign
method (instead directly use secp256k1.sign
from external ethereum-cryptography
package
#3948
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
Conversation
…um-cryptography external package)
c665e4e
to
ccea943
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
612d252
to
f17680d
Compare
Our original ecsign method was a wrapper method. If we want to keep the same signature, we have to use |
There's a warning in ethereum-cryptography for this compat: "Warning: use secp256k1 instead. This module is only for users who upgraded from ethereum-cryptography v0.1. It could be removed in the future." Will directly do that here, removing all these compat imports. |
I just did a fresh client run with datadir deleted and pkgs freshly installed to ensure that devp2p works, just imported blocks, so this looks fine :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. A couple of small nits but could be disregarded.
This reverts commit 99859e5.
We should indeed remove cryptoFunctions.ecsign = (msg: Uint8Array, pk: Uint8Array) => {
if (msg.length < 32) {
// WASM errors with `unreachable` if we try to pass in less than 32 bytes in the message
throw EthereumJSErrorWithoutCode('message length must be 32 bytes or greater')
}
const buf = secp256k1Sign(msg, pk)
const r = bytesToBigInt(buf.slice(0, 32))
const s = bytesToBigInt(buf.slice(32, 64))
const recovery = buf[64]
return { r, s, recovery }
}
cryptoFunctions.ecdsaSign = (hash: Uint8Array, pk: Uint8Array) => {
const sig = secp256k1Sign(hash, pk)
const r = bytesToBigInt(sig.slice(0, 32))
const s = bytesToBigInt(sig.slice(32, 64))
const recovery = Number(sig[64])
return {
r,
s,
recovery,
}
} (They are equivalent) Note: it was thus previously this wrapper which threw, not the method it seems? But in the new ecdsaSign the length check of assert.throws(
() => wasmSign(randomBytes(31), randomBytes(32)),
'message length must be 32 bytes or greater',
) Failed |
To be clear, at one point, the wasm version of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not so deep into the topic, so only some loose side-review without any approval decision.
packages/client/bin/utils.ts
Outdated
@@ -38,7 +38,8 @@ import { | |||
sha256 as wasmSha256, | |||
} from '@polkadot/wasm-crypto' | |||
import { keccak256 } from 'ethereum-cryptography/keccak.js' | |||
import { ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat.js' | |||
import { ecdsaRecover } from 'ethereum-cryptography/secp256k1-compat.js' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So here it is still necessary that we use this deprecated compat module https://github.com/ethereum/js-ethereum-cryptography/blob/main/src/secp256k1-compat.ts ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not want to tackle this here because I thought it would bloat the PR too much, but yes we can directly do here. It is also part of this issue #3966
@@ -18,9 +17,11 @@ import { | |||
waitReady, | |||
sha256 as wasmSha256, | |||
} from '@polkadot/wasm-crypto' | |||
import { secp256k1 } from 'ethereum-cryptography/secp256k1' | |||
import { ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat.js' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, still this compat thing in.
@@ -18,9 +17,11 @@ import { | |||
waitReady, | |||
sha256 as wasmSha256, | |||
} from '@polkadot/wasm-crypto' | |||
import { secp256k1 } from 'ethereum-cryptography/secp256k1' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not totally a blocker, but it's a bit semi-beautiful that we have a mixture of these .js
and non-.js
imports for the ethereum-cryptography
imports.
packages/tx/src/types.ts
Outdated
@@ -225,7 +225,7 @@ export interface TransactionInterface<T extends TransactionType = TransactionTyp | |||
errorStr(): string | |||
|
|||
addSignature( | |||
v: bigint, | |||
v: bigint, // TODO: change this to number? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This TODO should be removed.
signature: Uint8Array | ||
recid: number | ||
} | ||
protected _ecdsaSign: Required<CustomCrypto>['ecsign'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this ok from a TypeScript/Node.js compatibility perspective?
(if not AND this was not caught by any unit tests: we should really add this to CI in some form, so that similar code adoptions do not slip through in the future)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabrocheleau could you take a look here? How would we test this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as the other place, there really isn't any issue from a Nodejs compatibility perspective because none of these field declarations are in the compiled code. Is that what you're asking about?
const customEcSign = ( | ||
_msg: Uint8Array, | ||
_pk: Uint8Array, | ||
): Pick<ReturnType<typeof secp256k1.sign>, 'recovery' | 'r' | 's'> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also here: ok from TypeScript/Node.js compatibility perspective?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The interface gets compiled away so should have zero impact on Nodejs compatibility. So, in this case, this Pick<ReturnType>...
is duplicated from here. When you look in the compiled code, there's nothing in dist/esm/types.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have also confirmed that these fancy types do not cause an issue in Node 23 with node --conditions=typescript --experimental-strip-types...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…rom external `ethereum-cryptography` package (ethereumjs#3948) * util/monorepo: remove ecsign (use secp256k1.sign directly from ethereum-cryptography external package) * client: read wasm from util * tx: update auth sign to read from common crypto * devp2p: convert deprecated ecsign to secp256k1.sign * update customCrypto.ecsign return type * fix wasmCrypto test * switch v to number * Revert "switch v to number" This reverts commit 99859e5. * remove old comment * dedupe ecdsaSign * fix test * remove compat version of ecdsaRecover * tx: remove TODO * block/clique: ensure customCrypto and left padding is adhered --------- Co-authored-by: acolytec3 <[email protected]> Co-authored-by: Holger Drewes <[email protected]>
Part of the breaking series to remove methods from util which are almost directly supported from other dedicated external packages (
ethereum-cryptography
)TODO:
'ethereum-cryptography/secp256k1-compat.js'
. These are legacy imports and are deprecated. We should usesecp256k1
directly instead of this compat module. Partially done, other imports are part of this issue now Remove imports fromsecp256k1-compat.js
#3966secp256k1.sign
with ONLY the fieldsr
,s
,recovery
- [ ] Change tx type(Not workable becausev
tonumber
(thus alsoyParity
!)chainId
could be very large)ecsign
/ecdsaSign
incustomCrypto
interface