Disallow address zero in Ownable
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,9 @@ contract Ownable is
|
||||
public
|
||||
onlyOwner
|
||||
{
|
||||
if (newOwner != address(0)) {
|
||||
if (newOwner == address(0)) {
|
||||
LibRichErrors._rrevert(LibOwnableRichErrors.TransferOwnerToZeroError());
|
||||
} else {
|
||||
owner = newOwner;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { BigNumber } from './configured_bignumber';
|
||||
import { RevertError } from './revert_error';
|
||||
|
||||
// tslint:disable:max-classes-per-file
|
||||
|
||||
export enum SafeMathErrorCodes {
|
||||
Uint256AdditionOverflow,
|
||||
|
||||
Reference in New Issue
Block a user