Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/profile contracts #14

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
110 changes: 110 additions & 0 deletions truffle/contracts/Profile/ClaimHolder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
pragma solidity ^0.5.11;

import "./ERC735.sol";
import "./KeyHolder.sol";

// **Warning!** This file is a protoype version of our work around ERC 725.
// This file is now out of date and **should not be used**.
// Our current identity contracts are here:
// https://github.com/OriginProtocol/origin/tree/master/origin-contracts/contracts/identity

contract ClaimHolder is KeyHolder, ERC735 {

mapping (bytes32 => Claim) claims;
mapping (uint256 => bytes32[]) claimsByType;

function addClaim(
uint256 _claimType,
uint256 _scheme,
address _issuer,
bytes memory _signature,
bytes memory _data,
string memory _uri
)
public
returns (bytes32 claimRequestId)
{
bytes32 claimId = keccak256(abi.encodePacked(_issuer, _claimType));

if (msg.sender != address(this)) {
require(keyHasPurpose(keccak256(abi.encodePacked(msg.sender)), 3), "Sender does not have claim signer key");
}

if (claims[claimId].issuer != _issuer) {
claimsByType[_claimType].push(claimId);
}

claims[claimId].claimType = _claimType;
claims[claimId].scheme = _scheme;
claims[claimId].issuer = _issuer;
claims[claimId].signature = _signature;
claims[claimId].data = _data;
claims[claimId].uri = _uri;

emit ClaimAdded(
claimId,
_claimType,
_scheme,
_issuer,
_signature,
_data,
_uri
);

return claimId;
}

function removeClaim(bytes32 _claimId) public returns (bool success) {
if (msg.sender != address(this)) {
require(keyHasPurpose(keccak256(abi.encodePacked(msg.sender)), 1), "Sender does not have management key");
}

/* uint index; */
/* (index, ) = claimsByType[claims[_claimId].claimType].indexOf(_claimId);
claimsByType[claims[_claimId].claimType].removeByIndex(index); */

emit ClaimRemoved(
_claimId,
claims[_claimId].claimType,
claims[_claimId].scheme,
claims[_claimId].issuer,
claims[_claimId].signature,
claims[_claimId].data,
claims[_claimId].uri
);

delete claims[_claimId];
return true;
}

function getClaim(bytes32 _claimId)
public
view
returns(
uint256 claimType,
uint256 scheme,
address issuer,
bytes memory signature,
bytes memory data,
string memory uri
)
{
return (
claims[_claimId].claimType,
claims[_claimId].scheme,
claims[_claimId].issuer,
claims[_claimId].signature,
claims[_claimId].data,
claims[_claimId].uri
);
}

function getClaimIdsByType(uint256 _claimType)
public
view
returns(bytes32[] memory claimIds)
{
return claimsByType[_claimType];
}

}
94 changes: 94 additions & 0 deletions truffle/contracts/Profile/ClaimVerifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
pragma solidity ^0.5.11;

import "./ClaimHolder.sol";

// **Warning!** This file is a protoype version of our work around ERC 725.
// This file is now out of date and **should not be used**.
// Our current identity contracts are here:
// https://github.com/OriginProtocol/origin/tree/master/origin-contracts/contracts/identity

contract ClaimVerifier {

event ClaimValid(ClaimHolder _identity, uint256 claimType);
event ClaimInvalid(ClaimHolder _identity, uint256 claimType);

ClaimHolder public trustedClaimHolder;

constructor (address _trustedClaimHolder) public {
trustedClaimHolder = ClaimHolder(_trustedClaimHolder);
}

function checkClaim(ClaimHolder _identity, uint256 claimType)
public
returns (bool claimValid)
{
if (claimIsValid(_identity, claimType)) {
emit ClaimValid(_identity, claimType);
return true;
} else {
emit ClaimInvalid(_identity, claimType);
return false;
}
}

function claimIsValid(ClaimHolder _identity, uint256 claimType)
public
view
returns (bool claimValid)
{
uint256 foundClaimType;
uint256 scheme;
address issuer;
bytes memory sig;
bytes memory data;

// Construct claimId (identifier + claim type)
bytes32 claimId = keccak256(abi.encodePacked(trustedClaimHolder, claimType));

// Fetch claim from user
( foundClaimType, scheme, issuer, sig, data, ) = _identity.getClaim(claimId);

bytes32 dataHash = keccak256(abi.encodePacked(_identity, claimType, data));
bytes32 prefixedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", dataHash));

// Recover address of data signer
address recovered = getRecoveredAddress(sig, prefixedHash);

// Take hash of recovered address
bytes32 hashedAddr = keccak256(abi.encodePacked(recovered));

// Does the trusted identifier have they key which signed the user's claim?
return trustedClaimHolder.keyHasPurpose(hashedAddr, 3);
}

function getRecoveredAddress(bytes memory sig, bytes32 dataHash)
public
pure
returns (address addr)
{
bytes32 ra;
bytes32 sa;
uint8 va;

// Check the signature length
if (sig.length != 65) {
return (address(0x0));
}

// Divide the signature in r, s and v variables
assembly {
ra := mload(add(sig, 32))
sa := mload(add(sig, 64))
va := byte(0, mload(add(sig, 96)))
}

if (va < 27) {
va += 27;
}

address recoveredAddress = ecrecover(dataHash, va, ra, sa);

return (recoveredAddress);
}

}
33 changes: 33 additions & 0 deletions truffle/contracts/Profile/ERC725.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pragma solidity ^0.5.11;

// **Warning!** This file is a protoype version of our work around ERC 725.
// This file is now out of date and **should not be used**.
// Our current identity contracts are here:
// https://github.com/OriginProtocol/origin/tree/master/origin-contracts/contracts/identity

contract ERC725 {

uint256 constant MANAGEMENT_KEY = 1;
uint256 constant ACTION_KEY = 2;
uint256 constant CLAIM_SIGNER_KEY = 3;
uint256 constant ENCRYPTION_KEY = 4;

event KeyAdded(bytes32 indexed key, uint256 indexed purpose, uint256 indexed keyType);
event KeyRemoved(bytes32 indexed key, uint256 indexed purpose, uint256 indexed keyType);
event ExecutionRequested(uint256 indexed executionId, address indexed to, uint256 indexed value, bytes data);
event Executed(uint256 indexed executionId, address indexed to, uint256 indexed value, bytes data);
event Approved(uint256 indexed executionId, bool approved);

struct Key {
uint256 purpose; //e.g., MANAGEMENT_KEY = 1, ACTION_KEY = 2, etc.
uint256 keyType; // e.g. 1 = ECDSA, 2 = RSA, etc.
bytes32 key;
}

function getKey(bytes32 _key) public view returns(uint256 purpose, uint256 keyType, bytes32 key);
function getKeyPurpose(bytes32 _key) public view returns(uint256 purpose);
function getKeysByPurpose(uint256 _purpose) public view returns(bytes32[] memory keys);
function addKey(bytes32 _key, uint256 _purpose, uint256 _keyType) public returns (bool success);
function execute(address _to, uint256 _value, bytes memory _data) public returns (uint256 executionId);
function approve(uint256 _id, bool _approve) public returns (bool success);
}
43 changes: 43 additions & 0 deletions truffle/contracts/Profile/ERC735.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pragma solidity ^0.5.11;

// **Warning!** This file is a protoype version of our work around ERC 725.
// This file is now out of date and **should not be used**.
// Our current identity contracts are here:
// https://github.com/OriginProtocol/origin/tree/master/origin-contracts/contracts/identity

contract ERC735 {

event ClaimRequested(uint256 indexed claimRequestId, uint256 indexed claimType, uint256 scheme,
address indexed issuer, bytes signature, bytes data, string uri);

event ClaimAdded(bytes32 indexed claimId, uint256 indexed claimType, address indexed issuer,
uint256 signatureType, bytes32 signature, bytes claim, string uri);

event ClaimAdded(bytes32 indexed claimId, uint256 indexed claimType, uint256 scheme,
address indexed issuer, bytes signature, bytes data, string uri);

event ClaimRemoved(bytes32 indexed claimId, uint256 indexed claimType, uint256 scheme,
address indexed issuer, bytes signature, bytes data, string uri);

event ClaimChanged(bytes32 indexed claimId, uint256 indexed claimType, uint256 scheme,
address indexed issuer, bytes signature, bytes data, string uri);

struct Claim {
uint256 claimType;
uint256 scheme;
address issuer; // msg.sender
bytes signature; // this.address + claimType + data
bytes data;
string uri;
}

function getClaim(bytes32 _claimId) public view returns(uint256 claimType, uint256 scheme,
address issuer, bytes memory signature, bytes memory data, string memory uri);

function getClaimIdsByType(uint256 _claimType) public view returns(bytes32[] memory claimIds);

function addClaim(uint256 _claimType, uint256 _scheme, address issuer, bytes memory _signature,
bytes memory _data, string memory _uri) public returns (bytes32 claimRequestId);

function removeClaim(bytes32 _claimId) public returns (bool success);
}
Loading