Skip to content

Commit

Permalink
Zk sync plugin related changes (#7174)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Aug 2, 2024
1 parent 61e9e06 commit 2706805
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2648,4 +2648,10 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

- Remove redundant constructor of contractBuilder (#7150)

## [Unreleased]
## [Unreleased]

### Added

#### web3-eth-accounts

- Added public function `signMessageWithPrivateKey` (#7174)
5 changes: 4 additions & 1 deletion packages/web3-eth-accounts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@ Documentation:

- baseTransaction method updated (#7095)

## [Unreleased]
## [Unreleased]
### Added

- Added public function `signMessageWithPrivateKey` (#7174)
41 changes: 29 additions & 12 deletions packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
};
};

Expand Down

1 comment on commit 2706805

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2706805 Previous: 61e9e06 Ratio
processingTx 9037 ops/sec (±3.98%) 8729 ops/sec (±4.67%) 0.97
processingContractDeploy 39479 ops/sec (±7.28%) 37973 ops/sec (±6.91%) 0.96
processingContractMethodSend 16433 ops/sec (±7.94%) 15996 ops/sec (±6.56%) 0.97
processingContractMethodCall 28728 ops/sec (±5.91%) 27488 ops/sec (±6.50%) 0.96
abiEncode 44544 ops/sec (±7.67%) 43224 ops/sec (±7.07%) 0.97
abiDecode 30083 ops/sec (±7.18%) 29694 ops/sec (±7.87%) 0.99
sign 1568 ops/sec (±0.49%) 1501 ops/sec (±3.13%) 0.96
verify 366 ops/sec (±2.62%) 368 ops/sec (±0.51%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.