Skip to content
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

Zk sync plugin related changes #7174

Merged
merged 7 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 `pureSign` (#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 `pureSign` (#7174)
35 changes: 23 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,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 => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes more sense to Change pureSign to signMessageWithPrivateKey

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 +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,
};
};

Expand Down
Loading