From 0d59dc45226ddd22db77feb0f94a55a4e4047d5a Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Thu, 25 Jul 2024 21:53:41 +0300 Subject: [PATCH 1/6] Update account.ts --- packages/web3-eth-accounts/src/account.ts | 35 +++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index a2d1fffe191..e43b9e82c93 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -170,6 +170,23 @@ export const hashMessage = (message: string): string => { return sha3Raw(ethMessage); // using keccak in web3-utils.sha3Raw instead of SHA3 (NIST Standard) as both are different }; +export const pureSign = (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 +210,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 } = pureSign(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, }; }; From 358a79ca1393e2a6298e0469f419e662794c180e Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Thu, 25 Jul 2024 21:53:44 +0300 Subject: [PATCH 2/6] Update CHANGELOG.md --- packages/web3-eth-accounts/CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth-accounts/CHANGELOG.md b/packages/web3-eth-accounts/CHANGELOG.md index 2b5d1c1ca6a..792b613ac51 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 `pureSign` (#) From a8a0f48302ef1221df87bba66725744ae59f63a4 Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Thu, 25 Jul 2024 21:56:28 +0300 Subject: [PATCH 3/6] changelog --- CHANGELOG.md | 8 +++++++- packages/web3-eth-accounts/CHANGELOG.md | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25645330a25..97c22e77c2d 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 `pureSign` (#7174) diff --git a/packages/web3-eth-accounts/CHANGELOG.md b/packages/web3-eth-accounts/CHANGELOG.md index 792b613ac51..27c1ab90dd2 100644 --- a/packages/web3-eth-accounts/CHANGELOG.md +++ b/packages/web3-eth-accounts/CHANGELOG.md @@ -171,4 +171,4 @@ Documentation: ## [Unreleased] ### Added -- Added public function `pureSign` (#) +- Added public function `pureSign` (#7174) From e0f6e499bf3336cded6319c63665d1074b65b69b Mon Sep 17 00:00:00 2001 From: luu-alex <98a.lexluu@gmail.com> Date: Tue, 30 Jul 2024 11:46:32 -0400 Subject: [PATCH 4/6] update --- packages/web3-eth-accounts/CHANGELOG.md | 2 +- packages/web3-eth-accounts/src/account.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/web3-eth-accounts/CHANGELOG.md b/packages/web3-eth-accounts/CHANGELOG.md index 27c1ab90dd2..b28e2c9b047 100644 --- a/packages/web3-eth-accounts/CHANGELOG.md +++ b/packages/web3-eth-accounts/CHANGELOG.md @@ -171,4 +171,4 @@ Documentation: ## [Unreleased] ### Added -- Added public function `pureSign` (#7174) +- Added public function `signMessageWithPrivateKey` (#7174) diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index e43b9e82c93..92796bbaff2 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -170,7 +170,7 @@ export const hashMessage = (message: string): string => { return sha3Raw(ethMessage); // using keccak in web3-utils.sha3Raw instead of SHA3 (NIST Standard) as both are different }; -export const pureSign = (hash: HexString, privateKey: Bytes): SignResult => { +export const signMessageWithPrivateKey = (hash: HexString, privateKey: Bytes): SignResult => { const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey); const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array); @@ -212,7 +212,7 @@ export const pureSign = (hash: HexString, privateKey: Bytes): SignResult => { export const sign = (data: string, privateKey: Bytes): SignResult => { const hash = hashMessage(data); - const { messageHash, v, r, s, signature } = pureSign(hash, privateKey); + const { messageHash, v, r, s, signature } = signMessageWithPrivateKey(hash, privateKey); return { message: data, From 3f277985e054ea2df4d95601ae8598d0159970eb Mon Sep 17 00:00:00 2001 From: luu-alex <98a.lexluu@gmail.com> Date: Tue, 30 Jul 2024 11:47:31 -0400 Subject: [PATCH 5/6] change changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97c22e77c2d..1f9695c527f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2654,4 +2654,4 @@ If there are any bugs, improvements, optimizations or any new feature proposal f #### web3-eth-accounts -- Added public function `pureSign` (#7174) +- Added public function `signMessageWithPrivateKey` (#7174) From 8ba17dde94a081dcbe07c32d4df1832afb1da9d0 Mon Sep 17 00:00:00 2001 From: luu-alex <98a.lexluu@gmail.com> Date: Fri, 2 Aug 2024 09:58:57 -0400 Subject: [PATCH 6/6] update --- packages/web3-eth-accounts/src/account.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index 92796bbaff2..ac8e0adcfc6 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -170,6 +170,12 @@ 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);