replace key, recovery manager reset, remove unnecessary storage, lint

This commit is contained in:
Ricardo Guilherme Schmidt 2018-03-02 11:32:00 -03:00
parent ea59af8e1e
commit 9b17fbce88
1 changed files with 78 additions and 20 deletions

View File

@ -15,8 +15,8 @@ contract Identity is ERC725, ERC735 {
mapping (uint256 => uint8) minimumApprovalsByKeyType; mapping (uint256 => uint8) minimumApprovalsByKeyType;
bytes32[] pendingTransactions; bytes32[] pendingTransactions;
uint nonce = 0; uint nonce = 0;
bool recoverySet;
address recoveryContract; address recoveryContract;
address managerReset;
struct Transaction { struct Transaction {
address to; address to;
@ -24,22 +24,26 @@ contract Identity is ERC725, ERC735 {
bytes data; bytes data;
uint nonce; uint nonce;
uint approverCount; uint approverCount;
mapping(uint256 => uint8) approvalsByKeyType;
mapping(bytes32 => bool) approvals; mapping(bytes32 => bool) approvals;
} }
modifier managerOnly { modifier managerOnly {
require( require(
isKeyType(bytes32(msg.sender), MANAGEMENT_KEY) || isKeyType(bytes32(msg.sender), MANAGEMENT_KEY)
(recoverySet && msg.sender == address(recoveryContract))
); );
_; _;
} }
modifier selfOnly { modifier selfOnly {
require( require(
msg.sender == address(this) || msg.sender == address(this)
(recoverySet && msg.sender == address(recoveryContract)) );
_;
}
modifier recoveryOnly {
require(
(recoveryContract != address(0) && msg.sender == address(recoveryContract))
); );
_; _;
} }
@ -65,6 +69,38 @@ contract Identity is ERC725, ERC735 {
minimumApprovalsByKeyType[ACTION_KEY] = 1; minimumApprovalsByKeyType[ACTION_KEY] = 1;
} }
function managerReset(address _newKey)
external
recoveryOnly
{
managerReset = _newKey;
_addKey(managerReset, purpose, _newType);
minimumApprovalsByKeyType[MANAGEMENT_KEY] = keysByPurpose[MANAGEMENT_KEY].length;
}
function processManagerReset(uint limit)
external
{
require(managerReset != address(0));
bytes32 newKey = bytes32(managerReset);
bytes32[] memory managers = keysByPurpose[MANAGEMENT_KEY];
uint totalManagers = managers.lenght;
if (limit == 0) {
limit = totalManagers;
}
minimumApprovalsByKeyType[MANAGEMENT_KEY] = totalManagers - limit + 1;
for (uint i = 0; i < limit; i++) {
address manager = managers[i];
if (manager != newKey) {
_removeKey(manager, MANAGEMENT_KEY);
totalManagers--;
}
}
if (totalManagers == 1) {
managerReset = address(0);
}
}
function addKey( function addKey(
bytes32 _key, bytes32 _key,
uint256 _purpose, uint256 _purpose,
@ -78,6 +114,21 @@ contract Identity is ERC725, ERC735 {
return true; return true;
} }
function replaceKey(
bytes32 _oldKey,
bytes32 _newKey,
uint256 _newType
)
public
selfOnly
returns (bool success)
{
uint256 purpose = key[_oldKey].purpose;
_addKey(_newKey, purpose, _newType);
_removeKey(_oldKey, purpose);
return true;
}
function removeKey( function removeKey(
bytes32 _key, bytes32 _key,
uint256 _purpose uint256 _purpose
@ -111,7 +162,14 @@ contract Identity is ERC725, ERC735 {
return approveExecution(bytes32(msg.sender), _id, _approve); return approveExecution(bytes32(msg.sender), _id, _approve);
} }
function approveExecution(bytes32 _key, uint256 _id, bool _approve) internal returns(bool success) { function approveExecution(
bytes32 _key,
uint256 _id,
bool _approve
)
internal
returns(bool success)
{
Transaction storage trx = txx[_id]; Transaction storage trx = txx[_id];
bytes32 managerKeyHash = keccak256(_key, MANAGEMENT_KEY); bytes32 managerKeyHash = keccak256(_key, MANAGEMENT_KEY);
@ -125,12 +183,12 @@ contract Identity is ERC725, ERC735 {
if (trx.to == address(this)) { if (trx.to == address(this)) {
requiredKeyType = MANAGEMENT_KEY; requiredKeyType = MANAGEMENT_KEY;
if (keys[managerKeyHash].purpose == MANAGEMENT_KEY) { if (keys[managerKeyHash].purpose == MANAGEMENT_KEY) {
approvalCount = _calculateApprovals(managerKeyHash, MANAGEMENT_KEY, _approve, trx); approvalCount = _calculateApprovals(managerKeyHash, _approve, trx);
} }
} else { } else {
requiredKeyType = ACTION_KEY; requiredKeyType = ACTION_KEY;
if (keys[managerKeyHash].purpose == ACTION_KEY) { if (keys[managerKeyHash].purpose == ACTION_KEY) {
approvalCount = _calculateApprovals(actorKeyHash, ACTION_KEY, _approve, trx); approvalCount = _calculateApprovals(actorKeyHash, _approve, trx);
} }
} }
@ -153,21 +211,22 @@ contract Identity is ERC725, ERC735 {
function _calculateApprovals( function _calculateApprovals(
bytes32 _keyHash, bytes32 _keyHash,
uint256 _keyType,
bool _approve, bool _approve,
Transaction storage trx Transaction storage trx
) )
private private
returns (uint8 approvalCount) returns (uint8 approvalCount)
{ {
if (trx.approvals[_keyHash] == false && _approve) { require(trx.approvals[_keyHash] != _approve);
trx.approvalsByKeyType[_keyType]++;
} else if (trx.approvals[_keyHash] == true && !_approve && trx.approverCount > 0) {
trx.approvalsByKeyType[_keyType]--;
}
trx.approvals[_keyHash] = _approve; trx.approvals[_keyHash] = _approve;
if (_approve) {
trx.approverCount++; trx.approverCount++;
return trx.approvalsByKeyType[_keyType]; } else {
trx.approverCount--;
}
return trx.approverCount;
} }
function addClaim( function addClaim(
@ -480,9 +539,8 @@ contract Identity is ERC725, ERC735 {
public public
selfOnly selfOnly
{ {
require(recoverySet == false); require(recoveryContract == address(0));
recoveryContract = _recoveryContract; recoveryContract = _recoveryContract;
recoverySet = true;
} }
function () public payable { function () public payable {