Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Fix cached chain id in domain separator #416

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion contracts/v2/EIP2612.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ abstract contract EIP2612 is AbstractFiatTokenV2, EIP712Domain {
deadline
);
require(
EIP712.recover(DOMAIN_SEPARATOR, v, r, s, data) == owner,
EIP712.recover(_domainSeparator(), v, r, s, data) == owner,
"EIP2612: invalid signature"
);

Expand Down
6 changes: 3 additions & 3 deletions contracts/v2/EIP3009.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ abstract contract EIP3009 is AbstractFiatTokenV2, EIP712Domain {
nonce
);
require(
EIP712.recover(DOMAIN_SEPARATOR, v, r, s, data) == from,
EIP712.recover(_domainSeparator(), v, r, s, data) == from,
"FiatTokenV2: invalid signature"
);

Expand Down Expand Up @@ -155,7 +155,7 @@ abstract contract EIP3009 is AbstractFiatTokenV2, EIP712Domain {
nonce
);
require(
EIP712.recover(DOMAIN_SEPARATOR, v, r, s, data) == from,
EIP712.recover(_domainSeparator(), v, r, s, data) == from,
"FiatTokenV2: invalid signature"
);

Expand Down Expand Up @@ -186,7 +186,7 @@ abstract contract EIP3009 is AbstractFiatTokenV2, EIP712Domain {
nonce
);
require(
EIP712.recover(DOMAIN_SEPARATOR, v, r, s, data) == authorizer,
EIP712.recover(_domainSeparator(), v, r, s, data) == authorizer,
"FiatTokenV2: invalid signature"
);

Expand Down
13 changes: 12 additions & 1 deletion contracts/v2/EIP712Domain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ pragma solidity 0.6.12;
* @title EIP712 Domain
*/
contract EIP712Domain {

// was originally DOMAIN_SEPARATOR
// but that has been moved to a method so we can override it in V2_2+
bytes32 internal _CACHED_DOMAIN_SEPARATOR;

/**
* @dev EIP712 Domain Separator
*/
bytes32 public DOMAIN_SEPARATOR;
function DOMAIN_SEPARATOR() external view returns (bytes32) {
return _domainSeparator();
}

function _domainSeparator() internal virtual view returns (bytes32) {
return _CACHED_DOMAIN_SEPARATOR;
}
}
2 changes: 1 addition & 1 deletion contracts/v2/FiatTokenV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract FiatTokenV2 is FiatTokenV1_1, EIP3009, EIP2612 {
// solhint-disable-next-line reason-string
require(initialized && _initializedVersion == 0);
name = newName;
DOMAIN_SEPARATOR = EIP712.makeDomainSeparator(newName, "2");
_CACHED_DOMAIN_SEPARATOR = EIP712.makeDomainSeparator(newName, "2");
_initializedVersion = 1;
}

Expand Down
67 changes: 67 additions & 0 deletions contracts/v2/FiatTokenV2_2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* SPDX-License-Identifier: MIT
*
* Copyright (c) 2018-2023 CENTRE SECZ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

pragma solidity 0.6.12;

import { FiatTokenV2_1 } from "./FiatTokenV2_1.sol";
import { EIP712 } from "../util/EIP712.sol";

// solhint-disable func-name-mixedcase

/**
* @title FiatToken V2.1
* @notice ERC20 Token backed by fiat reserves, version 2.1
*/
contract FiatTokenV2_2 is FiatTokenV2_1 {

uint256 private _CACHED_CHAIN_ID;

/**
* @notice Initialize v2.2
*/
function initializeV2_2() external {
// solhint-disable-next-line reason-string
require(_initializedVersion == 2);

_CACHED_CHAIN_ID = chainid();

_initializedVersion = 3;
}

function chainid() internal view returns (uint256) {
uint256 chainId;
assembly {
chainId := chainid()
}
return chainId;
}

function _domainSeparator() internal override view returns (bytes32) {
if (_CACHED_CHAIN_ID == chainid()) {
return _CACHED_DOMAIN_SEPARATOR;
} else {
return EIP712.makeDomainSeparator(name, "2");
}
}
}
19 changes: 19 additions & 0 deletions test/helpers/storageSlots.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { FiatTokenProxyInstance } from "../../@types/generated";
const FiatTokenProxy = artifacts.require("FiatTokenProxy");
const FiatTokenV1 = artifacts.require("FiatTokenV1");
const FiatTokenV1_1 = artifacts.require("FiatTokenV1_1");
const FiatTokenV2 = artifacts.require("FiatTokenV2");
const FiatTokenV2_1 = artifacts.require("FiatTokenV2_1");

export function usesOriginalStorageSlotPositions<
T extends Truffle.ContractInstance
Expand Down Expand Up @@ -35,10 +37,12 @@ export function usesOriginalStorageSlotPositions<
alice,
bob,
charlie,
lostAndFound,
] = accounts;

let fiatToken: T;
let proxy: FiatTokenProxyInstance;
let domainSeparator : String;

beforeEach(async () => {
fiatToken = await Contract.new();
Expand Down Expand Up @@ -72,6 +76,15 @@ export function usesOriginalStorageSlotPositions<
from: owner,
});
}
if (version >= 2) {
const proxyAsFiatTokenV2 = await FiatTokenV2.at(proxy.address);
await proxyAsFiatTokenV2.initializeV2(name);
domainSeparator = await proxyAsFiatTokenV2.DOMAIN_SEPARATOR();
}
if (version >= 2.1) {
const proxyAsFiatTokenV2_1 = await FiatTokenV2_1.at(proxy.address);
await proxyAsFiatTokenV2_1.initializeV2_1(lostAndFound);
}
});

it("retains original storage slots 0 through 13", async () => {
Expand Down Expand Up @@ -132,6 +145,12 @@ export function usesOriginalStorageSlotPositions<
expect(parseAddress(slot)).to.equal(rescuer);
});
}
if (version >= 2) {
it("retains slot 15 for DOMAIN_SEPARATOR", async()=> {
const slot = await readSlot(proxy.address, 15);
expect("0x" + slot).to.equal(domainSeparator);
});
}

it("retains original storage slots for blacklisted mapping", async () => {
// blacklisted[alice]
Expand Down
43 changes: 43 additions & 0 deletions test/v2/FiatTokenV2_2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { FiatTokenV22Instance } from "../../@types/generated";
import { expectRevert } from "../helpers";
import { behavesLikeFiatTokenV2 } from "./FiatTokenV2.test";

const FiatTokenV2_2 = artifacts.require("FiatTokenV2_2");

contract("FiatTokenV2_2", (accounts) => {
const fiatTokenOwner = accounts[9];
let fiatToken: FiatTokenV22Instance;

beforeEach(async () => {
fiatToken = await FiatTokenV2_2.new();
await fiatToken.initialize(
"USD Coin",
"USDC",
"USD",
6,
fiatTokenOwner,
fiatTokenOwner,
fiatTokenOwner,
fiatTokenOwner
);
await fiatToken.initializeV2("USD Coin", { from: fiatTokenOwner });
const [, , lostAndFound] = accounts;
await fiatToken.initializeV2_1(lostAndFound);
await fiatToken.initializeV2_2();
});

behavesLikeFiatTokenV2(accounts, () => fiatToken, fiatTokenOwner);

describe("initializeV2_2", () => {
const [, user, lostAndFound] = accounts;

beforeEach(async () => {
});

it("disallows calling initializeV2_2 twice", async () => {
await expectRevert(
fiatToken.initializeV2_2()
);
});
});
});