Skip to content

Commit

Permalink
Merge pull request #603 from safe-global/feature/bytecode-shaving-2
Browse files Browse the repository at this point in the history
Feature: bytecode size reduction by removing public `getChainId` function, making encodeTransactionData private
  • Loading branch information
mmv08 committed Jul 10, 2023
2 parents 35312cd + 5da8714 commit db34e78
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
21 changes: 7 additions & 14 deletions contracts/Safe.sol
Original file line number Diff line number Diff line change
Expand Up @@ -350,25 +350,18 @@ contract Safe is
}

/**
* @notice Returns the ID of the chain the contract is currently deployed on.
* @return The ID of the current chain as a uint256.
* @dev Returns the domain separator for this contract, as defined in the EIP-712 standard.
* @return bytes32 The domain separator hash.
*/
function getChainId() public view returns (uint256) {
uint256 id;
function domainSeparator() public view returns (bytes32) {
uint256 chainId;
// solhint-disable-next-line no-inline-assembly
/// @solidity memory-safe-assembly
assembly {
id := chainid()
chainId := chainid()
}
return id;
}

/**
* @dev Returns the domain separator for this contract, as defined in the EIP-712 standard.
* @return bytes32 The domain separator hash.
*/
function domainSeparator() public view returns (bytes32) {
return keccak256(abi.encode(DOMAIN_SEPARATOR_TYPEHASH, getChainId(), this));
return keccak256(abi.encode(DOMAIN_SEPARATOR_TYPEHASH, chainId, this));
}

/**
Expand Down Expand Up @@ -396,7 +389,7 @@ contract Safe is
address gasToken,
address refundReceiver,
uint256 _nonce
) public view returns (bytes memory) {
) private view returns (bytes memory) {
bytes32 safeTxHash = keccak256(
abi.encode(
SAFE_TX_TYPEHASH,
Expand Down
7 changes: 0 additions & 7 deletions test/core/Safe.Signatures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ describe("Safe", async () => {
});
});

describe("getChainId", async () => {
it("should return correct id", async () => {
const { safe } = await setupTests();
expect(await safe.getChainId()).to.be.eq(await chainId());
});
});

describe("approveHash", async () => {
it("approving should only be allowed for owners", async () => {
const { safe } = await setupTests();
Expand Down
8 changes: 4 additions & 4 deletions test/integration/Safe.0xExploit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe("Safe", async () => {
const nonce = await safe.nonce();

// Use off-chain Safe signature
const messageData = await safe.encodeTransactionData(to, value, data, operation, 0, 0, 0, AddressZero, AddressZero, nonce);
const messageHash = await messageHandler.getMessageHash(ethers.utils.keccak256(messageData));
const transactionHash = await safe.getTransactionHash(to, value, data, operation, 0, 0, 0, AddressZero, AddressZero, nonce);
const messageHash = await messageHandler.getMessageHash(transactionHash);
const ownerSigs = await buildSignatureBytes([await signHash(user1, messageHash), await signHash(user2, messageHash)]);
const encodedOwnerSigns = defaultAbiCoder.encode(["bytes"], [ownerSigs]).slice(66);

Expand Down Expand Up @@ -111,8 +111,8 @@ describe("Safe", async () => {
const nonce = await safe.nonce();

// Use off-chain Safe signature
const messageData = await safe.encodeTransactionData(to, value, data, operation, 0, 0, 0, AddressZero, AddressZero, nonce);
const messageHash = await messageHandler.getMessageHash(messageData);
const transactionHash = await safe.getTransactionHash(to, value, data, operation, 0, 0, 0, AddressZero, AddressZero, nonce);
const messageHash = await messageHandler.getMessageHash(transactionHash);
const ownerSigs = await buildSignatureBytes([await signHash(user1, messageHash), await signHash(user2, messageHash)]);
const encodedOwnerSigns = defaultAbiCoder.encode(["bytes"], [ownerSigs]).slice(66);

Expand Down

0 comments on commit db34e78

Please sign in to comment.