Skip to content

Commit

Permalink
feat: add entropy to sign - resolves #3028
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 20, 2024
1 parent 0c98d99 commit 459e444
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-suits-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Improved security of signature generation. Resolves #3028.
10 changes: 5 additions & 5 deletions site/pages/op-stack/guides/deposits.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ import { publicClientL2 } from './config'

// Build parameters for the transaction on the L2.
const args = await publicClientL2.buildDepositTransaction({
mint: parseEther('1')
mint: parseEther('1'),
to: account.address,
})
```
Expand Down Expand Up @@ -265,7 +265,7 @@ import { account, publicClientL2, walletClientL1 } from './config'

// Build parameters for the transaction on the L2.
const args = await publicClientL2.buildDepositTransaction({
mint: parseEther('1')
mint: parseEther('1'),
to: account.address,
})

Expand Down Expand Up @@ -350,7 +350,7 @@ import {

// Build parameters for the transaction on the L2.
const args = await publicClientL2.buildDepositTransaction({
mint: parseEther('1')
mint: parseEther('1'),
to: account.address,
})

Expand Down Expand Up @@ -432,7 +432,7 @@ import {

// Build parameters for the transaction on the L2.
const args = await publicClientL2.buildDepositTransaction({
mint: parseEther('1')
mint: parseEther('1'),
to: account.address,
})

Expand Down Expand Up @@ -519,7 +519,7 @@ import {

// Build parameters for the transaction on the L2.
const args = await publicClientL2.buildDepositTransaction({
mint: parseEther('1')
mint: parseEther('1'),
to: account.address,
})

Expand Down
13 changes: 12 additions & 1 deletion src/accounts/utils/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export type SignReturnType<to extends To = 'object'> =

export type SignErrorType = NumberToHexErrorType | ErrorType

let extraEntropy: Hex | boolean = true

/** @internal */
export function setSignEntropy(entropy: Hex | boolean) {
extraEntropy = entropy
}

/**
* @description Signs a hash with a given private key.
*
Expand All @@ -38,7 +45,11 @@ export async function sign<to extends To = 'object'>({
privateKey,
to = 'object',
}: SignParameters<to>): Promise<SignReturnType<to>> {
const { r, s, recovery } = secp256k1.sign(hash.slice(2), privateKey.slice(2))
const { r, s, recovery } = secp256k1.sign(
hash.slice(2),
privateKey.slice(2),
{ lowS: true, extraEntropy },
)
const signature = {
r: numberToHex(r, { size: 32 }),
s: numberToHex(s, { size: 32 }),
Expand Down
4 changes: 3 additions & 1 deletion src/zksync/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export const formatters = {
return {
blockNumber: hexToBigInt(l2ToL1Log.blockHash),
blockHash: l2ToL1Log.blockHash,
l1BatchNumber: l2ToL1Log.l1BatchNumber ? hexToBigInt(l2ToL1Log.l1BatchNumber) : null,
l1BatchNumber: l2ToL1Log.l1BatchNumber
? hexToBigInt(l2ToL1Log.l1BatchNumber)
: null,
transactionIndex: hexToBigInt(l2ToL1Log.transactionIndex),
shardId: hexToBigInt(l2ToL1Log.shardId),
isService: l2ToL1Log.isService,
Expand Down
2 changes: 2 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { cleanupCache, listenersCache } from '~viem/utils/observe.js'
import { promiseCache, responseCache } from '~viem/utils/promise/withCache.js'
import { socketClientCache } from '~viem/utils/rpc/socket.js'

import { setSignEntropy } from '../src/accounts/utils/sign.js'
import { setErrorConfig } from '../src/errors/base.js'
import * as instances from './src/anvil.js'

Expand All @@ -19,6 +20,7 @@ beforeAll(() => {
},
version: '[email protected]',
})
setSignEntropy(false)
vi.mock('../src/errors/utils.ts', () => ({
getContractAddress: vi
.fn()
Expand Down
3 changes: 3 additions & 0 deletions vectors/src/transaction.vectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { signTransaction } from '../../src/accounts/utils/signTransaction.js'
import { parseTransaction } from '../../src/index.js'
import { serializeTransaction } from '../../src/utils/transaction/serializeTransaction.js'
import { readGzippedJson } from '../utils.js'
import { setSignEntropy } from '../../src/accounts/utils/sign.js'

setSignEntropy(false)

const transactions_ = await readGzippedJson(
join(import.meta.dir, './transaction.json.gz'),
Expand Down

0 comments on commit 459e444

Please sign in to comment.