Skip to content

Commit

Permalink
fix: serialize zero r/s correctly (#2398)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom authored Jun 12, 2024
1 parent dd6cccc commit f2695cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-tables-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed an issue where zero `r` and `s` values were not being serialized correctly.
22 changes: 22 additions & 0 deletions src/utils/transaction/serializeTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,4 +964,26 @@ describe('github', () => {
'0x6ed21df69b02678dfb290ef2a43d490303562eb387f70795766b37bfa9d09bd2',
)
})

test('https://github.com/wevm/viem/issues/2394', async () => {
const serialized = serializeTransaction(
{
chainId: 17000,
gas: BigInt('0x52080'),
maxFeePerGas: BigInt('0x0'),
maxPriorityFeePerGas: BigInt('0x0'),
nonce: 0,
to: '0xc000000000000000000000000000000000000000',
value: BigInt('0x0'),
},
{
r: '0x0',
s: '0x0',
yParity: 0,
},
)
expect(serialized).toEqual(
'0x02e58242688080808305208094c0000000000000000000000000000000000000008080c0808080',
)
})
})
16 changes: 11 additions & 5 deletions src/utils/transaction/serializeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,25 @@ function serializeTransactionLegacy(

export function toYParitySignatureArray(
transaction: TransactionSerializableGeneric,
signature?: Signature | undefined,
signature_?: Signature | undefined,
) {
const { r, s, v, yParity } = signature ?? transaction
if (typeof r === 'undefined') return []
if (typeof s === 'undefined') return []
const signature = signature_ ?? transaction
const { v, yParity } = signature

if (typeof signature.r === 'undefined') return []
if (typeof signature.s === 'undefined') return []
if (typeof v === 'undefined' && typeof yParity === 'undefined') return []

const r = trim(signature.r)
const s = trim(signature.s)

const yParity_ = (() => {
if (typeof yParity === 'number') return yParity ? toHex(1) : '0x'
if (v === 0n) return '0x'
if (v === 1n) return toHex(1)

return v === 27n ? '0x' : toHex(1)
})()
return [yParity_, trim(r), trim(s)]

return [yParity_, r === '0x00' ? '0x' : r, s === '0x00' ? '0x' : s]
}

0 comments on commit f2695cf

Please sign in to comment.