gitpivot/contracts/UserOracle.sol

77 lines
2.7 KiB
Solidity
Raw Normal View History

2017-11-22 23:14:38 +00:00
pragma solidity ^0.4.17;
import "./common/strings.sol";
2017-11-22 23:14:38 +00:00
import "./deploy/KillableModel.sol";
import "./JSONHelper.sol";
import "./oraclize/oraclizeAPI_0.4.sol";
import "./common/Controlled.sol";
import "./management/RegistryIndex.sol";
2017-11-22 23:14:38 +00:00
/**
* @title GitHubUserReg.sol
* Registers GitHub user login to an address
* @author Ricardo Guilherme Schmidt (Status Research & Development GmbH)]
*/
2017-11-22 23:14:38 +00:00
contract UserOracle is KillableModel, Controlled, RegistryIndex, JSONHelper, usingOraclize {
2017-05-19 19:06:53 +00:00
using strings for string;
using strings for strings.slice;
2017-11-22 23:14:38 +00:00
string private cred = "";
2017-11-22 23:14:38 +00:00
mapping (bytes32 => UserClaim) userClaim; //temporary db for oraclize user register queries
2017-05-11 06:38:15 +00:00
event RegisterUpdated(string name);
//stores temporary data for oraclize user register request
struct UserClaim {
address sender;
string login;
}
function register(string _githubUser, string _gistId, string _cred) public payable {
2017-11-22 23:14:38 +00:00
require(watchdog != 0x0);
if (bytes(_cred).length == 0) {
_cred = cred;
}
bytes32 ocid = oraclize_query("nested", _queryScript(_githubUser, _gistId, _cred));
userClaim[ocid] = UserClaim({sender: msg.sender, login: _githubUser});
2017-05-11 06:38:15 +00:00
}
//oraclize response callback
function __callback(bytes32 myid, string result, bytes proof) public {
2017-11-22 23:14:38 +00:00
//OracleEvent(myid, result, proof);
require(msg.sender == oraclize.cbAddress());
_register(myid, result);
2017-05-11 06:38:15 +00:00
}
function _register(bytes32 myid, string result) internal {
bytes memory v = bytes(result);
2017-11-22 23:14:38 +00:00
uint256 pos = 0;
2017-05-11 06:38:15 +00:00
address addrLoaded;
string memory login;
uint256 userId;
(addrLoaded, pos) = getNextAddr(v, pos);
(login, pos) = getNextString(v, pos);
(userId, pos) = getNextUInt(v, pos);
if (userClaim[myid].sender == addrLoaded && keccak256(userClaim[myid].login) == keccak256(login)) {
2017-05-11 06:38:15 +00:00
RegisterUpdated(login);
setRegistry(userId, addrLoaded, login);
2017-05-11 06:38:15 +00:00
}
delete userClaim[myid];
2017-05-11 06:38:15 +00:00
}
function _queryScript(string _githubUser, string _gistId, string _cred) internal returns (string) {
strings.slice[] memory cm = new strings.slice[](8);
cm[0] = strings.toSlice("[identity] ${[URL] https://gist.githubusercontent.com/");
cm[1] = _githubUser.toSlice();
cm[2] = strings.toSlice("/");
cm[3] = _gistId.toSlice();
cm[4] = strings.toSlice("/raw/register.txt}, ${[URL] json(https://api.github.com/gists/");
cm[5] = _gistId.toSlice();
cm[6] = _cred.toSlice();
cm[7] = strings.toSlice(").owner.[login,id]}");
return strings.toSlice("").join(cm);
2017-05-19 19:06:53 +00:00
}
}