Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 2706805

Browse files
avkosluu-alex
andauthored
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 <luu.alex98@gmail.com>
1 parent 61e9e06 commit 2706805

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2648,4 +2648,10 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
26482648

26492649
- Remove redundant constructor of contractBuilder (#7150)
26502650

2651-
## [Unreleased]
2651+
## [Unreleased]
2652+
2653+
### Added
2654+
2655+
#### web3-eth-accounts
2656+
2657+
- Added public function `signMessageWithPrivateKey` (#7174)

packages/web3-eth-accounts/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,7 @@ Documentation:
168168

169169
- baseTransaction method updated (#7095)
170170

171-
## [Unreleased]
171+
## [Unreleased]
172+
### Added
173+
174+
- Added public function `signMessageWithPrivateKey` (#7174)

packages/web3-eth-accounts/src/account.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ export const hashMessage = (message: string): string => {
170170
return sha3Raw(ethMessage); // using keccak in web3-utils.sha3Raw instead of SHA3 (NIST Standard) as both are different
171171
};
172172

173+
/**
174+
* Takes a hash of a message and a private key, signs the message using the SECP256k1 elliptic curve algorithm, and returns the signature components.
175+
* @param hash - The hash of the message to be signed, represented as a hexadecimal string.
176+
* @param privateKey - The private key used to sign the message, represented as a byte array.
177+
* @returns - The signature Object containing the message, messageHash, signature r, s, v
178+
*/
179+
export const signMessageWithPrivateKey = (hash: HexString, privateKey: Bytes): SignResult => {
180+
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey);
181+
182+
const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array);
183+
const signatureBytes = signature.toCompactRawBytes();
184+
const r = signature.r.toString(16).padStart(64, '0');
185+
const s = signature.s.toString(16).padStart(64, '0');
186+
const v = signature.recovery! + 27;
187+
188+
return {
189+
messageHash: hash,
190+
v: numberToHex(v),
191+
r: `0x${r}`,
192+
s: `0x${s}`,
193+
signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`,
194+
};
195+
};
173196
/**
174197
* Signs arbitrary data with a given private key.
175198
* :::info
@@ -193,23 +216,17 @@ export const hashMessage = (message: string): string => {
193216
* ```
194217
*/
195218
export const sign = (data: string, privateKey: Bytes): SignResult => {
196-
const privateKeyUint8Array = parseAndValidatePrivateKey(privateKey);
197-
198219
const hash = hashMessage(data);
199220

200-
const signature = secp256k1.sign(hash.substring(2), privateKeyUint8Array);
201-
const signatureBytes = signature.toCompactRawBytes();
202-
const r = signature.r.toString(16).padStart(64, '0');
203-
const s = signature.s.toString(16).padStart(64, '0');
204-
const v = signature.recovery! + 27;
221+
const { messageHash, v, r, s, signature } = signMessageWithPrivateKey(hash, privateKey);
205222

206223
return {
207224
message: data,
208-
messageHash: hash,
209-
v: numberToHex(v),
210-
r: `0x${r}`,
211-
s: `0x${s}`,
212-
signature: `${bytesToHex(signatureBytes)}${v.toString(16)}`,
225+
messageHash,
226+
v,
227+
r,
228+
s,
229+
signature,
213230
};
214231
};
215232

0 commit comments

Comments
 (0)