message signing verifications, use same storage, abstracted modifiers

This commit is contained in:
Ricardo Guilherme Schmidt 2018-02-26 19:22:04 -03:00
parent 0e2015bf3f
commit ebbfbeb1a0
1 changed files with 23 additions and 46 deletions

View File

@ -13,7 +13,6 @@ contract Identity is ERC725, ERC735 {
mapping (bytes32 => uint256) indexes; mapping (bytes32 => uint256) indexes;
mapping (uint => Transaction) txx; mapping (uint => Transaction) txx;
mapping (uint256 => uint8) minimumApprovalsByKeyType; mapping (uint256 => uint8) minimumApprovalsByKeyType;
mapping (bytes32 => bytes) publicKeys;
bytes32[] pendingTransactions; bytes32[] pendingTransactions;
uint nonce = 0; uint nonce = 0;
@ -27,25 +26,25 @@ contract Identity is ERC725, ERC735 {
mapping(bytes32 => bool) approvals; mapping(bytes32 => bool) approvals;
} }
modifier managerOnly { modifier managerOnly(bytes32 _key) {
require(isKeyType(bytes32(msg.sender), MANAGEMENT_KEY)); require(isKeyType(_key, MANAGEMENT_KEY));
_; _;
} }
modifier selfOnly { modifier selfOnly {
// require(msg.sender == address(this)); require(msg.sender == address(this));
_; _;
} }
modifier actorOnly { modifier actorOnly(bytes32 _key) {
require(isKeyType(bytes32(msg.sender), ACTION_KEY)); require(isKeyType(_key, ACTION_KEY));
_; _;
} }
modifier managerOrActor { modifier managerOrActor(bytes32 _key) {
require( require(
isKeyType(bytes32(msg.sender), MANAGEMENT_KEY) || isKeyType(_key, MANAGEMENT_KEY) ||
isKeyType(bytes32(msg.sender), ACTION_KEY) isKeyType(_key, ACTION_KEY)
); );
_; _;
} }
@ -68,17 +67,6 @@ contract Identity is ERC725, ERC735 {
return true; return true;
} }
function addPublicKey(bytes32 _key, bytes _publicKey)
public
selfOnly
{
publicKeys[_key] = _publicKey;
}
function getPublicKey(bytes32 _key) public constant returns (bytes _publicKey) {
return publicKeys[_key];
}
function removeKey( function removeKey(
bytes32 _key, bytes32 _key,
uint256 _purpose uint256 _purpose
@ -97,7 +85,7 @@ contract Identity is ERC725, ERC735 {
bytes _data bytes _data
) )
public public
managerOrActor managerOrActor(bytes32(msg.sender))
returns (uint256 executionId) returns (uint256 executionId)
{ {
executionId = _execute(_to, _value, _data); executionId = _execute(_to, _value, _data);
@ -106,19 +94,18 @@ contract Identity is ERC725, ERC735 {
function approve(uint256 _id, bool _approve) function approve(uint256 _id, bool _approve)
public public
managerOrActor managerOrActor(bytes32(msg.sender))
returns (bool success) returns (bool success)
{ {
approveExecution(_id, _approve); approveExecution(bytes32(msg.sender), _id, _approve);
} }
function approveExecution(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(bytes32(msg.sender), MANAGEMENT_KEY); bytes32 managerKeyHash = keccak256(_key, MANAGEMENT_KEY);
bytes32 actorKeyHash = keccak256(bytes32(msg.sender), ACTION_KEY); bytes32 actorKeyHash = keccak256(_key, ACTION_KEY);
uint8 approvalCount; uint8 approvalCount;
uint256 requiredKeyType; uint256 requiredKeyType;
@ -345,7 +332,6 @@ contract Identity is ERC725, ERC735 {
} }
delete keys[keyHash]; delete keys[keyHash];
delete publicKeys[keyHash];
// MUST only be done by keys of purpose 1, or the identity itself. // MUST only be done by keys of purpose 1, or the identity itself.
// TODO If its the identity itself, the approval process will determine its approval. // TODO If its the identity itself, the approval process will determine its approval.
@ -439,51 +425,42 @@ contract Identity is ERC725, ERC735 {
bytes32 _key, bytes32 _key,
bytes32 signHash, bytes32 signHash,
uint8 v, bytes32 r, bytes32 s) { uint8 v, bytes32 r, bytes32 s) {
require( require(address(_key) == ecrecover(keccak256("\x19Ethereum Signed Message:\n32", signHash), v, r, s));
uint(keccak256(publicKeys[_key]) & 0x00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) require(keys[_key].purpose != 0);
== uint(ecrecover(signHash, v, r, s)));
_; _;
} }
function approveECDSA(uint256 _id, bool _approve, function approveECDSA(uint256 _id, bool _approve,
bytes32 _key, bytes32 _key,
bytes32 signHash,
uint8 v, uint8 v,
bytes32 r, bytes32 r,
bytes32 s) bytes32 s)
public public
validECDSAKey(_key, signHash, v, r, s) validECDSAKey(_key, keccak256(address(this), bytes4(keccak256("approve(uint256,bool)")), _id, _approve), v, r, s)
managerOrActor(_key)
returns (bool success) returns (bool success)
{ {
return approveExecution(_key, _id, _approve);
approveExecution(_id, _approve);
} }
function executeECDSA( function executeECDSA(
address _to, address _to,
uint256 _value, uint256 _value,
bytes _data, bytes _data,
uint _nonce,
bytes32 _key, bytes32 _key,
bytes32 signHash,
uint8 v, uint8 v,
bytes32 r, bytes32 r,
bytes32 s bytes32 s
) )
public public
validECDSAKey(_key, signHash, v, r, s) validECDSAKey(_key, keccak256(address(this), bytes4(keccak256("execute(address,uint256,bytes)")), _to, _value, _data, _nonce), v, r, s)
managerOrActor(_key)
returns (uint256 executionId) returns (uint256 executionId)
{ {
require(
isKeyType(_key, MANAGEMENT_KEY) ||
isKeyType(_key, ACTION_KEY)
);
executionId = _execute(_to, _value, _data); executionId = _execute(_to, _value, _data);
approve(executionId, true); approveExecution(_key, executionId, true);
} }