Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
87 lines (63 sloc) 2.95 KB
pragma solidity ^0.4.11;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract Owned {
address public owner;
modifier onlyOwner {
if (msg.sender != owner) revert();
_;
}
function Owned() {
owner = msg.sender;
}
}
contract LedgerRandomProofVerifier is usingOraclize, Owned {
mapping(address=>bool) contractsWhitelist;
bytes expectedCodeHash;
function LedgerRandomProofVerifier() {
// Change here if we expect different code hash
expectedCodeHash = hex"f5557abbf544c3db784d84e777d3ca2894372d5ae761c74aa9266231225f156c";
}
modifier onlyWhitelisted {
require(contractsWhitelist[msg.sender]);
_;
}
function setWhiteListed(address _address, bool _toggle) onlyOwner {
contractsWhitelist[_address] = _toggle;
}
// What follows is imported from oraclizeAPI
// <ORACLIZE_API>
/*
Copyright (c) 2015-2016 Oraclize SRL
Copyright (c) 2016 Oraclize LTD
*/
function external_oraclize_randomDS_setCommitment(bytes32 queryId, bytes32 commitment) onlyWhitelisted public {
oraclize_randomDS_args[queryId] = commitment;
}
function oraclize_randomDS_proofVerify__sessionKeyValidity(bytes proof, uint sig2offset) internal returns (bool) {
bool sigok;
// Step 6: verify the attestation signature, APPKEY1 must sign the sessionKey from the correct ledger app (CODEHASH)
bytes memory sig2 = new bytes(uint(proof[sig2offset+1])+2);
copyBytes(proof, sig2offset, sig2.length, sig2, 0);
bytes memory appkey1_pubkey = new bytes(64);
copyBytes(proof, 3+1, 64, appkey1_pubkey, 0);
bytes memory tosign2 = new bytes(1+65+32);
tosign2[0] = 1; //role
copyBytes(proof, sig2offset-65, 65, tosign2, 1);
copyBytes(expectedCodeHash, 0, 32, tosign2, 1+65);
sigok = verifySig(sha256(tosign2), sig2, appkey1_pubkey);
if (sigok == false) return false;
// Step 7: verify the APPKEY1 provenance (must be signed by Ledger)
bytes memory LEDGERKEY = hex"7fb956469c5c9b89840d55b43537e66a98dd4811ea0a27224272c2e5622911e8537a2f8e86a46baec82864e98dd01e9ccc2f8bc5dfc9cbe5a91a290498dd96e4";
bytes memory tosign3 = new bytes(1+65);
tosign3[0] = 0xFE;
copyBytes(proof, 3, 65, tosign3, 1);
bytes memory sig3 = new bytes(uint(proof[3+65+1])+2);
copyBytes(proof, 3+65, sig3.length, sig3, 0);
sigok = verifySig(sha256(tosign3), sig3, LEDGERKEY);
return sigok;
}
// Same function as oraclizeAPI but we check if caller is a whitelisted contract
function external_oraclize_randomDS_proofVerify(bytes proof, bytes32 queryId, bytes result, string context_name) onlyWhitelisted returns (bool){
return oraclize_randomDS_proofVerify__main(proof, queryId, result, context_name);
}
}