Skip to content

Commit

Permalink
Add coinType to prevent crosschain replay attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflau committed Dec 20, 2023
1 parent 27ad314 commit d2315a9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
9 changes: 5 additions & 4 deletions contracts/reverseRegistrar/L2ReverseRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ error SignatureOutOfDate();
error Unauthorised();
error NotOwnerOfContract();

// Note on inception date
// @note Inception date
// The inception date is in milliseconds, and so will be divided by 1000
// when comparing to block.timestamp. This means that the date will be
// rounded down to the nearest second.
Expand Down Expand Up @@ -95,8 +95,9 @@ contract L2ReverseRegistrar is
uint256 inceptionDate,
bytes memory signature
) internal view returns (bool) {
bytes32 message = keccak256(abi.encodePacked(hash, addr, inceptionDate))
.toEthSignedMessageHash();
bytes32 message = keccak256(
abi.encodePacked(hash, addr, inceptionDate, coinType)
).toEthSignedMessageHash();
bytes32 node = _getNamehash(addr);

if (!SignatureChecker.isValidSignatureNow(addr, message, signature)) {
Expand All @@ -119,7 +120,7 @@ contract L2ReverseRegistrar is
bytes memory signature
) internal view returns (bool) {
bytes32 message = keccak256(
abi.encodePacked(hash, addr, owner, inceptionDate)
abi.encodePacked(hash, addr, owner, inceptionDate, coinType)
).toEthSignedMessageHash();
bytes32 node = _getNamehash(addr);

Expand Down
42 changes: 26 additions & 16 deletions test/reverseRegistrar/TestL2ReverseRegistrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('L2ReverseRegistrar', function () {
'setTextForAddrWithSignature(address,string,string,uint256,bytes)'
let setTextForAddrWithSignatureAndOwnableFuncSig =
'setTextForAddrWithSignatureAndOwnable(address,address,string,string,uint256,bytes)'
let coinType = 123

before(async function () {
signers = await ethers.getSigners()
Expand All @@ -31,7 +32,7 @@ describe('L2ReverseRegistrar', function () {
)
L2ReverseRegistrar = await L2ReverseRegistrarFactory.deploy(
namehash('optimism.reverse'),
123,
coinType,
)

const MockSmartContractWalletFactory = await ethers.getContractFactory(
Expand Down Expand Up @@ -87,11 +88,12 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(['bytes4', 'string'], [funcId, 'hello.eth']),
account,
inceptionDate,
coinType,
],
),
),
Expand Down Expand Up @@ -148,11 +150,12 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(['bytes4', 'string'], [funcId, 'hello.eth']),
account,
inceptionDate,
coinType,
],
),
),
Expand All @@ -172,11 +175,12 @@ describe('L2ReverseRegistrar', function () {
const signature2 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(['bytes4', 'string'], [funcId, 'hello.eth']),
account,
inceptionDate2,
coinType,
],
),
),
Expand Down Expand Up @@ -206,12 +210,13 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'address', 'uint256'],
['bytes32', 'address', 'address', 'uint256', 'uint256'],
[
keccak256(['bytes4', 'string'], [funcId, 'ownable.eth']),
MockOwnable.address,
MockSmartContractWallet.address,
inceptionDate,
coinType,
],
),
),
Expand Down Expand Up @@ -257,14 +262,15 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
),
account,
inceptionDate,
coinType,
],
),
),
Expand Down Expand Up @@ -329,14 +335,15 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
),
account,
inceptionDate,
coinType,
],
),
),
Expand All @@ -360,14 +367,15 @@ describe('L2ReverseRegistrar', function () {
const signature2 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(
['bytes4', 'string', 'string'],
[funcId, 'url', 'http://ens.domains'],
),
account,
inceptionDate2,
coinType,
],
),
),
Expand Down Expand Up @@ -398,7 +406,7 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'address', 'uint256'],
['bytes32', 'address', 'address', 'uint256', 'uint256'],
[
keccak256(
['bytes4', 'string', 'string'],
Expand All @@ -407,6 +415,7 @@ describe('L2ReverseRegistrar', function () {
MockOwnable.address,
MockSmartContractWallet.address,
inceptionDate,
coinType,
],
),
),
Expand Down Expand Up @@ -468,18 +477,18 @@ describe('L2ReverseRegistrar', function () {
const block = await ethers.provider.getBlock('latest')
const inceptionDate = block.timestamp

console.log('inceptionDate', inceptionDate)
const signature1 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(
['bytes4', 'string', 'string'],
[funcId1, 'url', 'http://ens.domains'],
),
account,
inceptionDate,
coinType,
],
),
),
Expand All @@ -488,11 +497,12 @@ describe('L2ReverseRegistrar', function () {
const signature2 = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
['bytes32', 'address', 'uint256', 'uint256'],
[
keccak256(['bytes4', 'string'], [funcId2, 'hello.eth']),
account,
inceptionDate + 1,
coinType,
],
),
),
Expand Down Expand Up @@ -553,8 +563,8 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
[keccak256(['bytes4'], [funcId]), account, inceptionDate],
['bytes32', 'address', 'uint256', 'uint256'],
[keccak256(['bytes4'], [funcId]), account, inceptionDate, coinType],
),
),
)
Expand Down Expand Up @@ -587,8 +597,8 @@ describe('L2ReverseRegistrar', function () {
const signature = await signers[0].signMessage(
ethers.utils.arrayify(
keccak256(
['bytes32', 'address', 'uint256'],
[keccak256(['bytes4'], [funcId]), account, inceptionDate],
['bytes32', 'address', 'uint256', 'uint256'],
[keccak256(['bytes4'], [funcId]), account, inceptionDate, coinType],
),
),
)
Expand Down

0 comments on commit d2315a9

Please sign in to comment.