Skip to content
Merged
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
21 changes: 16 additions & 5 deletions contracts/utils/Governable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

contract Governable {
address private _governor;
uint32 public refreshNonce = 0;

event GovernanceOwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event RecoveredAddress(address indexed recovered);
Expand Down Expand Up @@ -63,30 +64,40 @@ contract Governable {
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public onlyGovernor {
function transferOwnership(address newOwner, uint32 nonce) public onlyGovernor {
require(refreshNonce < nonce, "Invalid nonce");
require(nonce <= refreshNonce + 1, "Nonce must increment by 1");
_transferOwnership(newOwner);
refreshNonce = nonce;
}

/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnershipWithSignature(address newOwner, bytes memory sig) public {
function transferOwnershipWithSignature(address newOwner, uint32 nonce, bytes memory sig) public {
require(refreshNonce < nonce, "Invalid nonce");
require(nonce <= refreshNonce + 1, "Nonce must increment by 1");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add checks that nonce doesn't increase tremendously (causing overflow attack).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I had it backwards. This looks good.

bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 newOwnerHash = keccak256(abi.encodePacked(newOwner));
bytes32 newOwnerHash = keccak256(abi.encodePacked(nonce, newOwner));
require(isSignatureFromGovernor(abi.encodePacked(prefix, abi.encodePacked(newOwnerHash)), sig), "Governable: caller is not the governor");
_transferOwnership(newOwner);
refreshNonce = nonce;
}

/**
* @dev Transfers ownership of the contract to a new account associated with the publicKey * input
*/
function transferOwnershipWithSignaturePubKey(bytes memory publicKey, bytes memory sig) public {
function transferOwnershipWithSignaturePubKey(bytes memory publicKey, uint32 nonce, bytes memory sig) public {
require(refreshNonce < nonce, "Invalid nonce");
require(nonce <= refreshNonce + 1, "Nonce must increment by 1");
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 pubKeyHash = keccak256(publicKey);
bytes32 pubKeyNonceHash = keccak256(abi.encodePacked(nonce, publicKey));
address newOwner = address(uint160(uint256(pubKeyHash)));
require(isSignatureFromGovernor(abi.encodePacked(prefix, abi.encodePacked(pubKeyHash)), sig), "Governable: caller is not the governor");
require(isSignatureFromGovernor(abi.encodePacked(prefix, abi.encodePacked(pubKeyNonceHash)), sig), "Governable: caller is not the governor");
_transferOwnership(newOwner);
refreshNonce = nonce;
}

function verify(bytes32 hash, uint8 v, bytes32 r, bytes32 s) public view returns(bool) {
Expand Down
25 changes: 0 additions & 25 deletions packages/contracts/src/AnchorBase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ interface AnchorBaseInterface extends ethers.utils.Interface {
"hasEdge(uint256)": FunctionFragment;
"hashLeftRight(address,bytes32,bytes32)": FunctionFragment;
"hasher()": FunctionFragment;
"insert(bytes32)": FunctionFragment;
"isKnownNeighborRoot(uint256,bytes32)": FunctionFragment;
"isKnownRoot(bytes32)": FunctionFragment;
"isSpent(bytes32)": FunctionFragment;
Expand Down Expand Up @@ -126,7 +125,6 @@ interface AnchorBaseInterface extends ethers.utils.Interface {
values: [string, BytesLike, BytesLike]
): string;
encodeFunctionData(functionFragment: "hasher", values?: undefined): string;
encodeFunctionData(functionFragment: "insert", values: [BytesLike]): string;
encodeFunctionData(
functionFragment: "isKnownNeighborRoot",
values: [BigNumberish, BytesLike]
Expand Down Expand Up @@ -231,7 +229,6 @@ interface AnchorBaseInterface extends ethers.utils.Interface {
data: BytesLike
): Result;
decodeFunctionResult(functionFragment: "hasher", data: BytesLike): Result;
decodeFunctionResult(functionFragment: "insert", data: BytesLike): Result;
decodeFunctionResult(
functionFragment: "isKnownNeighborRoot",
data: BytesLike
Expand Down Expand Up @@ -436,11 +433,6 @@ export class AnchorBase extends BaseContract {

hasher(overrides?: CallOverrides): Promise<[string]>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<ContractTransaction>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -596,11 +588,6 @@ export class AnchorBase extends BaseContract {

hasher(overrides?: CallOverrides): Promise<string>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<ContractTransaction>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -753,8 +740,6 @@ export class AnchorBase extends BaseContract {

hasher(overrides?: CallOverrides): Promise<string>;

insert(_commitment: BytesLike, overrides?: CallOverrides): Promise<number>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -950,11 +935,6 @@ export class AnchorBase extends BaseContract {

hasher(overrides?: CallOverrides): Promise<BigNumber>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<BigNumber>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -1104,11 +1084,6 @@ export class AnchorBase extends BaseContract {

hasher(overrides?: CallOverrides): Promise<PopulatedTransaction>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<PopulatedTransaction>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down
25 changes: 0 additions & 25 deletions packages/contracts/src/FixedDepositAnchor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ interface FixedDepositAnchorInterface extends ethers.utils.Interface {
"hasEdge(uint256)": FunctionFragment;
"hashLeftRight(address,bytes32,bytes32)": FunctionFragment;
"hasher()": FunctionFragment;
"insert(bytes32)": FunctionFragment;
"isKnownNeighborRoot(uint256,bytes32)": FunctionFragment;
"isKnownRoot(bytes32)": FunctionFragment;
"isSpent(bytes32)": FunctionFragment;
Expand Down Expand Up @@ -153,7 +152,6 @@ interface FixedDepositAnchorInterface extends ethers.utils.Interface {
values: [string, BytesLike, BytesLike]
): string;
encodeFunctionData(functionFragment: "hasher", values?: undefined): string;
encodeFunctionData(functionFragment: "insert", values: [BytesLike]): string;
encodeFunctionData(
functionFragment: "isKnownNeighborRoot",
values: [BigNumberish, BytesLike]
Expand Down Expand Up @@ -324,7 +322,6 @@ interface FixedDepositAnchorInterface extends ethers.utils.Interface {
data: BytesLike
): Result;
decodeFunctionResult(functionFragment: "hasher", data: BytesLike): Result;
decodeFunctionResult(functionFragment: "insert", data: BytesLike): Result;
decodeFunctionResult(
functionFragment: "isKnownNeighborRoot",
data: BytesLike
Expand Down Expand Up @@ -589,11 +586,6 @@ export class FixedDepositAnchor extends BaseContract {

hasher(overrides?: CallOverrides): Promise<[string]>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<ContractTransaction>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -821,11 +813,6 @@ export class FixedDepositAnchor extends BaseContract {

hasher(overrides?: CallOverrides): Promise<string>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<ContractTransaction>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -1047,8 +1034,6 @@ export class FixedDepositAnchor extends BaseContract {

hasher(overrides?: CallOverrides): Promise<string>;

insert(_commitment: BytesLike, overrides?: CallOverrides): Promise<number>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -1380,11 +1365,6 @@ export class FixedDepositAnchor extends BaseContract {

hasher(overrides?: CallOverrides): Promise<BigNumber>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<BigNumber>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down Expand Up @@ -1606,11 +1586,6 @@ export class FixedDepositAnchor extends BaseContract {

hasher(overrides?: CallOverrides): Promise<PopulatedTransaction>;

insert(
_commitment: BytesLike,
overrides?: PayableOverrides & { from?: string | Promise<string> }
): Promise<PopulatedTransaction>;

isKnownNeighborRoot(
neighborChainID: BigNumberish,
_root: BytesLike,
Expand Down
Loading