From 270680596db7e8c2e0ee09148a042233a415c106 Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Fri, 2 Aug 2024 17:31:52 +0300 Subject: [PATCH] Zk sync plugin related changes (#7174) * Update account.ts * Update CHANGELOG.md * changelog * update * change changelog * update --------- Co-authored-by: luu-alex <98a.lexluu@gmail.com> Co-authored-by: Alex --- CHANGELOG.md | 8 ++++- packages/web3-eth-accounts/CHANGELOG.md | 5 ++- packages/web3-eth-accounts/src/account.ts | 41 ++++++++++++++++------- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25645330a25..1f9695c527f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2648,4 +2648,10 @@ If there are any bugs, improvements, optimizations or any new feature proposal f - Remove redundant constructor of contractBuilder (#7150) -## [Unreleased] \ No newline at end of file +## [Unreleased] + +### Added + +#### web3-eth-accounts + +- Added public function `signMessageWithPrivateKey` (#7174) diff --git a/packages/web3-eth-accounts/CHANGELOG.md b/packages/web3-eth-accounts/CHANGELOG.md index 2b5d1c1ca6a..b28e2c9b047 100644 --- a/packages/web3-eth-accounts/CHANGELOG.md +++ b/packages/web3-eth-accounts/CHANGELOG.md @@ -168,4 +168,7 @@ Documentation: - baseTransaction method updated (#7095) -## [Unreleased] \ No newline at end of file +## [Unreleased] +### Added + +- Added public function `signMessageWithPrivateKey` (#7174) diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index a2d1fffe191..ac8e0adcfc6 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -170,6 +170,29 @@ export const hashMessage = (message: string): string => { return sha3Raw(ethMessage); // using keccak in web3-utils.sha3Raw instead of SHA3 (NIST Standard) as both are different }; +/** + * Takes a hash of a message and a private key, signs the message using the SECP256k1 elliptic curve algorithm, and returns the signature components. + * @param hash - The hash of the message to be signed, represented as a hexadecimal string. + * @param privateKey - The private key used to sign the message, represented as a byte array. + * @returns - The signature Object containing the message, messageHash, signature r, s, v + */ +export const signMessageWithPrivateKey = (hash: HexString, privateKey: Bytes): SignResult => { + const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); + + const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array); + const signatureBytes = signature.toCompactRawBytes(); + const r = signature.r.toString(16).padStart(64, '0'); + const s = signature.s.toString(16).padStart(64, '0'); + const v = signature.recovery! + 27; + + return { + messageHash: hash, + v: numberToHex(v), + r: `0x${r}`, + s: `0x${s}`, + signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`, + }; +}; /** * Signs arbitrary data with a given private key. * :::info @@ -193,23 +216,17 @@ export const hashMessage = (message: string): string => { * ``` */ export const sign = (data: string, privateKey: Bytes): SignResult => { - const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); - const hash = hashMessage(data); - const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array); - const signatureBytes = signature.toCompactRawBytes(); - const r = signature.r.toString(16).padStart(64, '0'); - const s = signature.s.toString(16).padStart(64, '0'); - const v = signature.recovery! + 27; + const { messageHash, v, r, s, signature } = signMessageWithPrivateKey(hash, privateKey); return { message: data, - messageHash: hash, - v: numberToHex(v), - r: `0x${r}`, - s: `0x${s}`, - signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`, + messageHash, + v, + r, + s, + signature, }; };