Disallow address zero in Ownable

This commit is contained in:
Alex Towle
2019-07-30 12:43:59 -07:00
parent 92a4556956
commit b4a3218b13
5 changed files with 37 additions and 12 deletions

View File

@@ -4,9 +4,13 @@ pragma solidity ^0.5.9;
library LibOwnableRichErrors {
// bytes4(keccak256("OnlyOwnerError(address,address)"))
bytes4 internal constant ONLY_OWNER_SELECTOR =
bytes4 internal constant ONLY_OWNER_ERROR_SELECTOR =
0x1de45ad1;
// bytes4(keccak256("TransferOwnerToZeroError()"))
bytes internal constant TRANSFER_OWNER_TO_ZERO_ERROR_SELECTOR =
hex"e69edc3e";
// solhint-disable func-name-mixedcase
function OnlyOwnerError(
address sender,
@@ -17,9 +21,17 @@ library LibOwnableRichErrors {
returns (bytes memory)
{
return abi.encodeWithSelector(
ONLY_OWNER_SELECTOR,
ONLY_OWNER_ERROR_SELECTOR,
sender,
owner
);
}
function TransferOwnerToZeroError()
internal
pure
returns (bytes memory)
{
return TRANSFER_OWNER_TO_ZERO_ERROR_SELECTOR;
}
}

View File

@@ -30,7 +30,9 @@ contract Ownable is
public
onlyOwner
{
if (newOwner != address(0)) {
if (newOwner == address(0)) {
LibRichErrors._rrevert(LibOwnableRichErrors.TransferOwnerToZeroError());
} else {
owner = newOwner;
}
}

View File

@@ -42,12 +42,10 @@ describe('Ownable', () => {
});
describe('transferOwnership', () => {
it('should not transfer ownership if the specified new owner is the zero address', async () => {
expect(
ownable.transferOwnership.sendTransactionAsync(constants.NULL_ADDRESS, { from: owner }),
).to.be.fulfilled('');
const updatedOwner = await ownable.owner.callAsync();
expect(updatedOwner).to.be.eq(owner);
it('should revert if the specified new owner is the zero address', async () => {
const expectedError = new OwnableRevertErrors.TransferOwnerToZeroError();
const tx = ownable.transferOwnership.sendTransactionAsync(constants.NULL_ADDRESS, { from: owner });
return expect(tx).to.revertWith(expectedError);
});
it('should transfer ownership if the specified new owner is not the zero address', async () => {

View File

@@ -1,5 +1,6 @@
import { RevertError } from './revert_error';
// tslint:disable:max-classes-per-file
export class OnlyOwnerError extends RevertError {
constructor(sender?: string, owner?: string) {
super('OnlyOwnerError', 'OnlyOwnerError(address sender, address owner)', {
@@ -9,5 +10,18 @@ export class OnlyOwnerError extends RevertError {
}
}
// Register the OnlyOwnerError type
RevertError.registerType(OnlyOwnerError);
export class TransferOwnerToZeroError extends RevertError {
constructor() {
super('TransferOwnerToZeroError', 'TransferOwnerToZeroError()', {});
}
}
const types = [
OnlyOwnerError,
TransferOwnerToZeroError,
];
// Register the types we've defined.
for (const type of types) {
RevertError.registerType(type);
}

View File

@@ -1,7 +1,6 @@
import { BigNumber } from './configured_bignumber';
import { RevertError } from './revert_error';
// tslint:disable:max-classes-per-file
export enum SafeMathErrorCodes {
Uint256AdditionOverflow,