Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit 0aa6e3a

Browse files
authored
chainId check (#83)
* chainId check * test fix + cleanup
1 parent d98b195 commit 0aa6e3a

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

contracts/optimistic-ethereum/OVM/accounts/OVM_ECDSAContractAccount.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ contract OVM_ECDSAContractAccount is iOVM_ECDSAContractAccount {
6464

6565
Lib_OVMCodec.EIP155Transaction memory decodedTx = Lib_OVMCodec.decodeEIP155Transaction(_transaction, isEthSign);
6666

67+
// Need to make sure that the transaction chainId is correct.
68+
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
69+
decodedTx.chainId == Lib_SafeExecutionManagerWrapper.safeCHAINID(),
70+
"Transaction chainId does not match expected OVM chainId."
71+
);
72+
6773
// Need to make sure that the transaction nonce is right.
6874
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
6975
decodedTx.nonce == Lib_SafeExecutionManagerWrapper.safeGETNONCE(),

contracts/optimistic-ethereum/mockOVM/accounts/mockOVM_ECDSAContractAccount.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ contract mockOVM_ECDSAContractAccount is iOVM_ECDSAContractAccount {
4646
bool isEthSign = _signatureType == Lib_OVMCodec.EOASignatureType.ETH_SIGNED_MESSAGE;
4747
Lib_OVMCodec.EIP155Transaction memory decodedTx = Lib_OVMCodec.decodeEIP155Transaction(_transaction, isEthSign);
4848

49+
// Need to make sure that the transaction chainId is correct.
50+
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
51+
decodedTx.chainId == Lib_SafeExecutionManagerWrapper.safeCHAINID(),
52+
"Transaction chainId does not match expected OVM chainId."
53+
);
54+
4955
// Need to make sure that the transaction nonce is right.
5056
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
5157
decodedTx.nonce == Lib_SafeExecutionManagerWrapper.safeGETNONCE(),

test/contracts/OVM/accounts/OVM_ECDSAContractAccount.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,32 @@ describe('OVM_ECDSAContractAccount', () => {
200200
'Transaction nonce does not match the expected nonce.'
201201
)
202202
})
203+
204+
it(`should revert on incorrect chainId`, async () => {
205+
const alteredChainIdTx = {
206+
...DEFAULT_EIP155_TX,
207+
chainId : 421
208+
}
209+
const message = serializeNativeTransaction(alteredChainIdTx)
210+
const sig = await signNativeTransaction(wallet, alteredChainIdTx)
211+
212+
await callPrecompile(
213+
Helper_PrecompileCaller,
214+
OVM_ECDSAContractAccount,
215+
'execute',
216+
[
217+
message,
218+
0, //isEthSignedMessage
219+
`0x${sig.v}`, //v
220+
`0x${sig.r}`, //r
221+
`0x${sig.s}`, //s
222+
]
223+
)
224+
const ovmREVERT: any =
225+
Mock__OVM_ExecutionManager.smocked.ovmREVERT.calls[0]
226+
expect(ethers.utils.toUtf8String(ovmREVERT._data)).to.equal(
227+
'Transaction chainId does not match expected OVM chainId.'
228+
)
229+
})
203230
})
204231
})

test/helpers/codec/encoding.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const signNativeTransaction = async (
121121
let [v, r, s] = getSignedComponents(transactionSignature).map((component) => {
122122
return remove0x(component)
123123
})
124-
v = '0' + (parseInt(v, 16) - 420 * 2 - 8 - 27)
124+
v = '0' + (parseInt(v, 16) - transaction.chainId * 2 - 8 - 27)
125125
return {
126126
messageHash,
127127
v,

0 commit comments

Comments
 (0)