GitHubUserReg.sol
1 /** 2 * GitHubUserReg.sol 3 * Registers GitHub user login to an address 4 * Ricardo Guilherme Schmidt <3esmit@gmail.com> 5 */ 6 import "GitHubAPIReg.sol"; 7 import "NameRegistry.sol"; 8 import "strings.sol"; 9 10 pragma solidity ^0.4.11; 11 12 contract GitHubUserReg is NameRegistry, GitHubAPIReg { 13 using strings for string; 14 using strings for strings.slice; 15 16 mapping (bytes32 => UserClaim) userClaim; //temporary db for oraclize user register queries 17 mapping (uint256 => User) users; 18 19 event RegisterUpdated(string name); 20 21 //stores temporary data for oraclize user register request 22 struct UserClaim { 23 address sender; 24 string login; 25 } 26 27 struct User { 28 address addr; 29 string login; 30 } 31 32 function register(string _github_user, string _gistid, string _cred) payable { 33 if(bytes(_cred).length == 0) _cred = cred; 34 bytes32 ocid = oraclize_query("nested", _query_script(_github_user, _gistid,_cred)); 35 userClaim[ocid] = UserClaim({sender: msg.sender, login: _github_user}); 36 } 37 38 function getAddr(uint256 _id) public constant returns(address addr) { 39 return users[_id].addr; 40 } 41 42 function getName(address _addr) public constant returns(string name){ 43 return users[indexes[sha3(_addr)]].login; 44 } 45 46 function getAddr(string _name) public constant returns(address addr) { 47 return users[indexes[sha3(_name)]].addr; 48 } 49 50 //oraclize response callback 51 function __callback(bytes32 myid, string result, bytes proof) { 52 OracleEvent(myid, result, proof); 53 if (msg.sender != oraclize.cbAddress()){ 54 throw; 55 }else { 56 _register(myid, result); 57 } 58 } 59 60 function _register(bytes32 myid, string result) internal { 61 bytes memory v = bytes(result); 62 uint8 pos = 0; 63 address addrLoaded; 64 string memory login; 65 uint256 userId; 66 (addrLoaded,pos) = getNextAddr(v,pos); 67 (login,pos) = getNextString(v,pos); 68 (userId,pos) = getNextUInt(v,pos); 69 if(userClaim[myid].sender == addrLoaded && sha3(userClaim[myid].login) == sha3(login)){ 70 RegisterUpdated(login); 71 if(users[userId].addr != 0x0){ 72 delete indexes[sha3(users[userId].login)]; 73 delete indexes[sha3(users[userId].addr)]; 74 } 75 indexes[sha3(addrLoaded)] = userId; 76 indexes[sha3(login)] = userId; 77 users[userId].addr = addrLoaded; 78 users[userId].login = login; 79 } 80 delete userClaim[myid]; //should always be deleted 81 } 82 83 function _query_script(string _github_user, string _gistid, string _cred) internal returns (string) { 84 strings.slice [] memory cm = new strings.slice[](8); 85 cm[0] = strings.toSlice("[identity] ${[URL] https://gist.githubusercontent.com/"); 86 cm[1] = _github_user.toSlice(); 87 cm[2] = strings.toSlice("/"); 88 cm[3] = _gistid.toSlice(); 89 cm[4] = strings.toSlice("/raw/register.txt}, ${[URL] json(https://api.github.com/gists/"); 90 cm[5] = _gistid.toSlice(); 91 cm[6] = _cred.toSlice(); 92 cm[7] = strings.toSlice(").owner.[login,id]}"); 93 return strings.toSlice("").join(cm); 94 } 95 96 } 97 98 library GHUserReg{ 99 100 function create() returns (GitHubUserReg){ 101 return new GitHubUserReg(); 102 } 103 104 }