Skip to content

Commit

Permalink
chainId check (#83)
Browse files Browse the repository at this point in the history
* chainId check

* test fix + cleanup
  • Loading branch information
axchu authored Nov 30, 2020
1 parent fa3c557 commit 66e55e8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ contract OVM_ECDSAContractAccount is iOVM_ECDSAContractAccount {

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

// Need to make sure that the transaction chainId is correct.
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
decodedTx.chainId == Lib_SafeExecutionManagerWrapper.safeCHAINID(),
"Transaction chainId does not match expected OVM chainId."
);

// Need to make sure that the transaction nonce is right.
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
decodedTx.nonce == Lib_SafeExecutionManagerWrapper.safeGETNONCE(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ contract mockOVM_ECDSAContractAccount is iOVM_ECDSAContractAccount {
bool isEthSign = _signatureType == Lib_OVMCodec.EOASignatureType.ETH_SIGNED_MESSAGE;
Lib_OVMCodec.EIP155Transaction memory decodedTx = Lib_OVMCodec.decodeEIP155Transaction(_transaction, isEthSign);

// Need to make sure that the transaction chainId is correct.
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
decodedTx.chainId == Lib_SafeExecutionManagerWrapper.safeCHAINID(),
"Transaction chainId does not match expected OVM chainId."
);

// Need to make sure that the transaction nonce is right.
Lib_SafeExecutionManagerWrapper.safeREQUIRE(
decodedTx.nonce == Lib_SafeExecutionManagerWrapper.safeGETNONCE(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,32 @@ describe('OVM_ECDSAContractAccount', () => {
'Transaction nonce does not match the expected nonce.'
)
})

it(`should revert on incorrect chainId`, async () => {
const alteredChainIdTx = {
...DEFAULT_EIP155_TX,
chainId : 421
}
const message = serializeNativeTransaction(alteredChainIdTx)
const sig = await signNativeTransaction(wallet, alteredChainIdTx)

await callPrecompile(
Helper_PrecompileCaller,
OVM_ECDSAContractAccount,
'execute',
[
message,
0, //isEthSignedMessage
`0x${sig.v}`, //v
`0x${sig.r}`, //r
`0x${sig.s}`, //s
]
)
const ovmREVERT: any =
Mock__OVM_ExecutionManager.smocked.ovmREVERT.calls[0]
expect(ethers.utils.toUtf8String(ovmREVERT._data)).to.equal(
'Transaction chainId does not match expected OVM chainId.'
)
})
})
})
2 changes: 1 addition & 1 deletion packages/contracts/test/helpers/codec/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const signNativeTransaction = async (
let [v, r, s] = getSignedComponents(transactionSignature).map((component) => {
return remove0x(component)
})
v = '0' + (parseInt(v, 16) - 420 * 2 - 8 - 27)
v = '0' + (parseInt(v, 16) - transaction.chainId * 2 - 8 - 27)
return {
messageHash,
v,
Expand Down

0 comments on commit 66e55e8

Please sign in to comment.