Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/tx-envelope-legacy-presign-chain-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ox": patch
---

Fixed `TxEnvelopeLegacy.hash` (with `presign`) and `TxEnvelopeLegacy.getSignPayload` to derive the chain ID from an EIP-155 `v` when the envelope has no `chainId`.
7 changes: 7 additions & 0 deletions src/core/TxEnvelopeLegacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,18 @@ export function hash<presign extends boolean = false>(
options: hash.Options<presign> = {},
): hash.ReturnType {
const { presign } = options
const { chainId, v } = envelope
return Hash.keccak256(
serialize({
...envelope,
...(presign
? {
// An EIP-155 `v` carries the chain ID when `chainId` is absent.
chainId:
chainId ??
(typeof v === 'number' && v >= 37
? Math.floor((v - 35) / 2)
: undefined),
r: undefined,
s: undefined,
yParity: undefined,
Expand Down
44 changes: 44 additions & 0 deletions src/core/_test/TransactionEnvelopeLegacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,50 @@ describe('hash', () => {
`"0x5e427e088ae00b084b41e198c52440aa43b2d6f8f1f01246fd25d4e9b6ebfdab"`,
)
})

test('options: presign: chain ID carried by `v`', () => {
const unsigned = {
gas: 21000n,
gasPrice: 1000000000n,
nonce: 665n,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
} as const
const payload = TxEnvelopeLegacy.getSignPayload(
TxEnvelopeLegacy.from({ ...unsigned, chainId: 1 }),
)
const { r, s, yParity } = Secp256k1.sign({
payload,
privateKey: accounts[0].privateKey,
})

// EIP-155 `v`, no `chainId`
const envelope = TxEnvelopeLegacy.from({
...unsigned,
r,
s,
v: 37 + yParity,
})
expect(
TxEnvelopeLegacy.hash(envelope, { presign: true }),
).toMatchInlineSnapshot(
`"0x4c1dec0d90aa1a17cb0aa735b5550e43a1bd27cd1cacb8987522576fa6220e46"`,
)
expect(TxEnvelopeLegacy.hash(envelope, { presign: true })).toBe(payload)
expect(TxEnvelopeLegacy.getSignPayload(envelope)).toBe(
TxEnvelopeLegacy.getSignPayload(
TxEnvelopeLegacy.deserialize(TxEnvelopeLegacy.serialize(envelope)),
),
)

// pre-EIP-155 `v`, no `chainId`
expect(
TxEnvelopeLegacy.hash(
TxEnvelopeLegacy.from({ ...unsigned, r, s, v: 27 + yParity }),
{ presign: true },
),
).toBe(TxEnvelopeLegacy.getSignPayload(TxEnvelopeLegacy.from(unsigned)))
})
})

describe('serialize', () => {
Expand Down
Loading