Last active
December 2, 2018 11:55
-
-
Save gcyber911/30bff154e8b2e1922c176d90ef29b2cf to your computer and use it in GitHub Desktop.
nkcjaskjfakj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22; | |
contract OraclizeI { | |
address public cbAddress; | |
function query(uint _timestamp, string _datasource, string _arg) external returns (bytes32 _id); | |
function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) external returns (bytes32 _id); | |
function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) public returns (bytes32 _id); | |
function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) external returns (bytes32 _id); | |
function queryN(uint _timestamp, string _datasource, bytes _argN) public returns (bytes32 _id); | |
function queryN_withGasLimit(uint _timestamp, string _datasource, bytes _argN, uint _gaslimit) external returns (bytes32 _id); | |
function setProofType(byte _proofType) external; | |
function setCustomGasPrice(uint _gasPrice) external; | |
function randomDS_getSessionPubKeyHash() external constant returns(bytes32); | |
} | |
contract OraclizeAddrResolverI { | |
function getAddress() public returns (address _addr); | |
} | |
library Buffer { | |
struct buffer { | |
bytes buf; | |
uint capacity; | |
} | |
function init(buffer memory buf, uint _capacity) internal pure { | |
uint capacity = _capacity; | |
if(capacity % 32 != 0) capacity += 32 - (capacity % 32); | |
// Allocate space for the buffer data | |
buf.capacity = capacity; | |
assembly { | |
let ptr := mload(0x40) | |
mstore(buf, ptr) | |
mstore(ptr, 0) | |
mstore(0x40, add(ptr, capacity)) | |
} | |
} | |
function resize(buffer memory buf, uint capacity) private pure { | |
bytes memory oldbuf = buf.buf; | |
init(buf, capacity); | |
append(buf, oldbuf); | |
} | |
function max(uint a, uint b) private pure returns(uint) { | |
if(a > b) { | |
return a; | |
} | |
return b; | |
} | |
/** | |
* @dev Appends a byte array to the end of the buffer. Resizes if doing so | |
* would exceed the capacity of the buffer. | |
* @param buf The buffer to append to. | |
* @param data The data to append. | |
* @return The original buffer. | |
*/ | |
function append(buffer memory buf, bytes data) internal pure returns(buffer memory) { | |
if(data.length + buf.buf.length > buf.capacity) { | |
resize(buf, max(buf.capacity, data.length) * 2); | |
} | |
uint dest; | |
uint src; | |
uint len = data.length; | |
assembly { | |
// Memory address of the buffer data | |
let bufptr := mload(buf) | |
// Length of existing buffer data | |
let buflen := mload(bufptr) | |
// Start address = buffer address + buffer length + sizeof(buffer length) | |
dest := add(add(bufptr, buflen), 32) | |
// Update buffer length | |
mstore(bufptr, add(buflen, mload(data))) | |
src := add(data, 32) | |
} | |
// Copy word-length chunks while possible | |
for(; len >= 32; len -= 32) { | |
assembly { | |
mstore(dest, mload(src)) | |
} | |
dest += 32; | |
src += 32; | |
} | |
// Copy remaining bytes | |
uint mask = 256 ** (32 - len) - 1; | |
assembly { | |
let srcpart := and(mload(src), not(mask)) | |
let destpart := and(mload(dest), mask) | |
mstore(dest, or(destpart, srcpart)) | |
} | |
return buf; | |
} | |
/** | |
* @dev Appends a byte to the end of the buffer. Resizes if doing so would | |
* exceed the capacity of the buffer. | |
* @param buf The buffer to append to. | |
* @param data The data to append. | |
* @return The original buffer. | |
*/ | |
function append(buffer memory buf, uint8 data) internal pure { | |
if(buf.buf.length + 1 > buf.capacity) { | |
resize(buf, buf.capacity * 2); | |
} | |
assembly { | |
// Memory address of the buffer data | |
let bufptr := mload(buf) | |
// Length of existing buffer data | |
let buflen := mload(bufptr) | |
// Address = buffer address + buffer length + sizeof(buffer length) | |
let dest := add(add(bufptr, buflen), 32) | |
mstore8(dest, data) | |
// Update buffer length | |
mstore(bufptr, add(buflen, 1)) | |
} | |
} | |
/** | |
* @dev Appends a byte to the end of the buffer. Resizes if doing so would | |
* exceed the capacity of the buffer. | |
* @param buf The buffer to append to. | |
* @param data The data to append. | |
* @return The original buffer. | |
*/ | |
function appendInt(buffer memory buf, uint data, uint len) internal pure returns(buffer memory) { | |
if(len + buf.buf.length > buf.capacity) { | |
resize(buf, max(buf.capacity, len) * 2); | |
} | |
uint mask = 256 ** len - 1; | |
assembly { | |
// Memory address of the buffer data | |
let bufptr := mload(buf) | |
// Length of existing buffer data | |
let buflen := mload(bufptr) | |
// Address = buffer address + buffer length + sizeof(buffer length) + len | |
let dest := add(add(bufptr, buflen), len) | |
mstore(dest, or(and(mload(dest), not(mask)), data)) | |
// Update buffer length | |
mstore(bufptr, add(buflen, len)) | |
} | |
return buf; | |
} | |
} | |
library CBOR { | |
using Buffer for Buffer.buffer; | |
uint8 private constant MAJOR_TYPE_INT = 0; | |
uint8 private constant MAJOR_TYPE_NEGATIVE_INT = 1; | |
uint8 private constant MAJOR_TYPE_BYTES = 2; | |
uint8 private constant MAJOR_TYPE_STRING = 3; | |
uint8 private constant MAJOR_TYPE_ARRAY = 4; | |
uint8 private constant MAJOR_TYPE_MAP = 5; | |
uint8 private constant MAJOR_TYPE_CONTENT_FREE = 7; | |
function encodeType(Buffer.buffer memory buf, uint8 major, uint value) private pure { | |
if(value <= 23) { | |
buf.append(uint8((major << 5) | value)); | |
} else if(value <= 0xFF) { | |
buf.append(uint8((major << 5) | 24)); | |
buf.appendInt(value, 1); | |
} else if(value <= 0xFFFF) { | |
buf.append(uint8((major << 5) | 25)); | |
buf.appendInt(value, 2); | |
} else if(value <= 0xFFFFFFFF) { | |
buf.append(uint8((major << 5) | 26)); | |
buf.appendInt(value, 4); | |
} else if(value <= 0xFFFFFFFFFFFFFFFF) { | |
buf.append(uint8((major << 5) | 27)); | |
buf.appendInt(value, 8); | |
} | |
} | |
function encodeIndefiniteLengthType(Buffer.buffer memory buf, uint8 major) private pure { | |
buf.append(uint8((major << 5) | 31)); | |
} | |
function encodeUInt(Buffer.buffer memory buf, uint value) internal pure { | |
encodeType(buf, MAJOR_TYPE_INT, value); | |
} | |
function encodeInt(Buffer.buffer memory buf, int value) internal pure { | |
if(value >= 0) { | |
encodeType(buf, MAJOR_TYPE_INT, uint(value)); | |
} else { | |
encodeType(buf, MAJOR_TYPE_NEGATIVE_INT, uint(-1 - value)); | |
} | |
} | |
function encodeBytes(Buffer.buffer memory buf, bytes value) internal pure { | |
encodeType(buf, MAJOR_TYPE_BYTES, value.length); | |
buf.append(value); | |
} | |
function encodeString(Buffer.buffer memory buf, string value) internal pure { | |
encodeType(buf, MAJOR_TYPE_STRING, bytes(value).length); | |
buf.append(bytes(value)); | |
} | |
function startArray(Buffer.buffer memory buf) internal pure { | |
encodeIndefiniteLengthType(buf, MAJOR_TYPE_ARRAY); | |
} | |
function startMap(Buffer.buffer memory buf) internal pure { | |
encodeIndefiniteLengthType(buf, MAJOR_TYPE_MAP); | |
} | |
function endSequence(Buffer.buffer memory buf) internal pure { | |
encodeIndefiniteLengthType(buf, MAJOR_TYPE_CONTENT_FREE); | |
} | |
} | |
/* | |
End solidity-cborutils | |
*/ | |
contract usingOraclize { | |
uint constant day = 60*60*24; | |
uint constant week = 60*60*24*7; | |
uint constant month = 60*60*24*30; | |
byte constant proofType_NONE = 0x00; | |
byte constant proofType_TLSNotary = 0x10; | |
byte constant proofType_Ledger = 0x30; | |
byte constant proofType_Android = 0x40; | |
byte constant proofType_Native = 0xF0; | |
byte constant proofStorage_IPFS = 0x01; | |
uint8 constant networkID_auto = 0; | |
OraclizeAddrResolverI OAR; | |
OraclizeI oraclize; | |
modifier oraclizeAPI { | |
if(address(oraclize) != OAR.getAddress()) | |
oraclize = OraclizeI(OAR.getAddress()); | |
_; | |
} | |
function __callback(bytes32 myid, string result) public { | |
__callback(myid, result, new bytes(0)); | |
} | |
function __callback(bytes32 myid, string result, bytes proof) public { | |
return; | |
// Following should never be reached with a preceding return, however | |
// this is just a placeholder function, ideally meant to be defined in | |
// child contract when proofs are used | |
myid; result; proof; // Silence compiler warnings | |
oraclize = OraclizeI(0); // Additional compiler silence about making function pure/view. | |
} | |
function oraclize_query(string datasource, string arg) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query(0, datasource, arg); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query(timestamp, datasource, arg); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query_withGasLimit(timestamp, datasource, arg, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query_withGasLimit(0, datasource, arg, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg1, string arg2) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query2(0, datasource, arg1, arg2); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg1, string arg2) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query2(timestamp, datasource, arg1, arg2); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg1, string arg2, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query2_withGasLimit(timestamp, datasource, arg1, arg2, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg1, string arg2, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
return oraclize.query2_withGasLimit(0, datasource, arg1, arg2, gaslimit); | |
} | |
function oraclize_query(string datasource, string[] argN) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = stra2cbor(argN); | |
return oraclize.queryN(0, datasource, args); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[] argN) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = stra2cbor(argN); | |
return oraclize.queryN(timestamp, datasource, args); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[] argN, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = stra2cbor(argN); | |
return oraclize.queryN_withGasLimit(timestamp, datasource, args, gaslimit); | |
} | |
function oraclize_query(string datasource, string[] argN, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = stra2cbor(argN); | |
return oraclize.queryN_withGasLimit(0, datasource, args, gaslimit); | |
} | |
function oraclize_query(string datasource, string[1] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[1] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[1] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[1] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[2] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[2] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[2] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[2] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[3] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[3] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[3] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[3] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[4] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[4] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[4] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[4] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[5] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[5] args) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[5] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, string[5] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
string[] memory dynargs = new string[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[] argN) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = ba2cbor(argN); | |
return oraclize.queryN(0, datasource, args); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[] argN) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = ba2cbor(argN); | |
return oraclize.queryN(timestamp, datasource, args); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[] argN, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = ba2cbor(argN); | |
return oraclize.queryN_withGasLimit(timestamp, datasource, args, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[] argN, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
bytes memory args = ba2cbor(argN); | |
return oraclize.queryN_withGasLimit(0, datasource, args, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[1] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[1] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[1] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[1] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](1); | |
dynargs[0] = args[0]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[2] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[2] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[2] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[2] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](2); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[3] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[3] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[3] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[3] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](3); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[4] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[4] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[4] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[4] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](4); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[5] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[5] args) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(timestamp, datasource, dynargs); | |
} | |
function oraclize_query(uint timestamp, string datasource, bytes[5] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(timestamp, datasource, dynargs, gaslimit); | |
} | |
function oraclize_query(string datasource, bytes[5] args, uint gaslimit) oraclizeAPI internal returns (bytes32 id) { | |
bytes[] memory dynargs = new bytes[](5); | |
dynargs[0] = args[0]; | |
dynargs[1] = args[1]; | |
dynargs[2] = args[2]; | |
dynargs[3] = args[3]; | |
dynargs[4] = args[4]; | |
return oraclize_query(datasource, dynargs, gaslimit); | |
} | |
function oraclize_cbAddress() oraclizeAPI internal returns (address){ | |
return oraclize.cbAddress(); | |
} | |
function getCodeSize(address _addr) view internal returns(uint _size) { | |
assembly { | |
_size := extcodesize(_addr) | |
} | |
} | |
function parseAddr(string _a) internal pure returns (address){ | |
bytes memory tmp = bytes(_a); | |
uint160 iaddr = 0; | |
uint160 b1; | |
uint160 b2; | |
for (uint i=2; i<2+2*20; i+=2){ | |
iaddr *= 256; | |
b1 = uint160(tmp[i]); | |
b2 = uint160(tmp[i+1]); | |
if ((b1 >= 97)&&(b1 <= 102)) b1 -= 87; | |
else if ((b1 >= 65)&&(b1 <= 70)) b1 -= 55; | |
else if ((b1 >= 48)&&(b1 <= 57)) b1 -= 48; | |
if ((b2 >= 97)&&(b2 <= 102)) b2 -= 87; | |
else if ((b2 >= 65)&&(b2 <= 70)) b2 -= 55; | |
else if ((b2 >= 48)&&(b2 <= 57)) b2 -= 48; | |
iaddr += (b1*16+b2); | |
} | |
return address(iaddr); | |
} | |
function strCompare(string _a, string _b) internal pure returns (int) { | |
bytes memory a = bytes(_a); | |
bytes memory b = bytes(_b); | |
uint minLength = a.length; | |
if (b.length < minLength) minLength = b.length; | |
for (uint i = 0; i < minLength; i ++) | |
if (a[i] < b[i]) | |
return -1; | |
else if (a[i] > b[i]) | |
return 1; | |
if (a.length < b.length) | |
return -1; | |
else if (a.length > b.length) | |
return 1; | |
else | |
return 0; | |
} | |
function indexOf(string _haystack, string _needle) internal pure returns (int) { | |
bytes memory h = bytes(_haystack); | |
bytes memory n = bytes(_needle); | |
if(h.length < 1 || n.length < 1 || (n.length > h.length)) | |
return -1; | |
else if(h.length > (2**128 -1)) | |
return -1; | |
else | |
{ | |
uint subindex = 0; | |
for (uint i = 0; i < h.length; i ++) | |
{ | |
if (h[i] == n[0]) | |
{ | |
subindex = 1; | |
while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) | |
{ | |
subindex++; | |
} | |
if(subindex == n.length) | |
return int(i); | |
} | |
} | |
return -1; | |
} | |
} | |
function strConcat(string _a, string _b, string _c, string _d, string _e) internal pure returns (string) { | |
bytes memory _ba = bytes(_a); | |
bytes memory _bb = bytes(_b); | |
bytes memory _bc = bytes(_c); | |
bytes memory _bd = bytes(_d); | |
bytes memory _be = bytes(_e); | |
string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length); | |
bytes memory babcde = bytes(abcde); | |
uint k = 0; | |
for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i]; | |
for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i]; | |
for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i]; | |
for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i]; | |
for (i = 0; i < _be.length; i++) babcde[k++] = _be[i]; | |
return string(babcde); | |
} | |
function strConcat(string _a, string _b, string _c, string _d) internal pure returns (string) { | |
return strConcat(_a, _b, _c, _d, ""); | |
} | |
function strConcat(string _a, string _b, string _c) internal pure returns (string) { | |
return strConcat(_a, _b, _c, "", ""); | |
} | |
function strConcat(string _a, string _b) internal pure returns (string) { | |
return strConcat(_a, _b, "", "", ""); | |
} | |
// parseInt | |
function parseInt(string _a) internal pure returns (uint) { | |
return parseInt(_a, 0); | |
} | |
// parseInt(parseFloat*10^_b) | |
function parseInt(string _a, uint _b) internal pure returns (uint) { | |
bytes memory bresult = bytes(_a); | |
uint mint = 0; | |
bool decimals = false; | |
for (uint i=0; i<bresult.length; i++){ | |
if ((bresult[i] >= 48)&&(bresult[i] <= 57)){ | |
if (decimals){ | |
if (_b == 0) break; | |
else _b--; | |
} | |
mint *= 10; | |
mint += uint(bresult[i]) - 48; | |
} else if (bresult[i] == 46) decimals = true; | |
} | |
if (_b > 0) mint *= 10**_b; | |
return mint; | |
} | |
function uint2str(uint i) internal pure returns (string){ | |
if (i == 0) return "0"; | |
uint j = i; | |
uint len; | |
while (j != 0){ | |
len++; | |
j /= 10; | |
} | |
bytes memory bstr = new bytes(len); | |
uint k = len - 1; | |
while (i != 0){ | |
bstr[k--] = byte(48 + i % 10); | |
i /= 10; | |
} | |
return string(bstr); | |
} | |
using CBOR for Buffer.buffer; | |
function stra2cbor(string[] arr) internal pure returns (bytes) { | |
safeMemoryCleaner(); | |
Buffer.buffer memory buf; | |
Buffer.init(buf, 1024); | |
buf.startArray(); | |
for (uint i = 0; i < arr.length; i++) { | |
buf.encodeString(arr[i]); | |
} | |
buf.endSequence(); | |
return buf.buf; | |
} | |
function ba2cbor(bytes[] arr) internal pure returns (bytes) { | |
safeMemoryCleaner(); | |
Buffer.buffer memory buf; | |
Buffer.init(buf, 1024); | |
buf.startArray(); | |
for (uint i = 0; i < arr.length; i++) { | |
buf.encodeBytes(arr[i]); | |
} | |
buf.endSequence(); | |
return buf.buf; | |
} | |
// the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license | |
function copyBytes(bytes from, uint fromOffset, uint length, bytes to, uint toOffset) internal pure returns (bytes) { | |
uint minLength = length + toOffset; | |
// Buffer too small | |
require(to.length >= minLength); // Should be a better way? | |
// NOTE: the offset 32 is added to skip the `size` field of both bytes variables | |
uint i = 32 + fromOffset; | |
uint j = 32 + toOffset; | |
while (i < (32 + fromOffset + length)) { | |
assembly { | |
let tmp := mload(add(from, i)) | |
mstore(add(to, j), tmp) | |
} | |
i += 32; | |
j += 32; | |
} | |
return to; | |
} | |
// the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license | |
// Duplicate Solidity's ecrecover, but catching the CALL return value | |
function safer_ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal returns (bool, address) { | |
// We do our own memory management here. Solidity uses memory offset | |
// 0x40 to store the current end of memory. We write past it (as | |
// writes are memory extensions), but don't update the offset so | |
// Solidity will reuse it. The memory used here is only needed for | |
// this context. | |
// FIXME: inline assembly can't access return values | |
bool ret; | |
address addr; | |
assembly { | |
let size := mload(0x40) | |
mstore(size, hash) | |
mstore(add(size, 32), v) | |
mstore(add(size, 64), r) | |
mstore(add(size, 96), s) | |
// NOTE: we can reuse the request memory because we deal with | |
// the return code | |
ret := call(3000, 1, 0, size, 128, size, 32) | |
addr := mload(size) | |
} | |
return (ret, addr); | |
} | |
// the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license | |
function ecrecovery(bytes32 hash, bytes sig) internal returns (bool, address) { | |
bytes32 r; | |
bytes32 s; | |
uint8 v; | |
if (sig.length != 65) | |
return (false, 0); | |
// The signature format is a compact form of: | |
// {bytes32 r}{bytes32 s}{uint8 v} | |
// Compact means, uint8 is not padded to 32 bytes. | |
assembly { | |
r := mload(add(sig, 32)) | |
s := mload(add(sig, 64)) | |
// Here we are loading the last 32 bytes. We exploit the fact that | |
// 'mload' will pad with zeroes if we overread. | |
// There is no 'mload8' to do this, but that would be nicer. | |
v := byte(0, mload(add(sig, 96))) | |
// Alternative solution: | |
// 'byte' is not working due to the Solidity parser, so lets | |
// use the second best option, 'and' | |
// v := and(mload(add(sig, 65)), 255) | |
} | |
// albeit non-transactional signatures are not specified by the YP, one would expect it | |
// to match the YP range of [27, 28] | |
// | |
// geth uses [0, 1] and some clients have followed. This might change, see: | |
// https://github.com/ethereum/go-ethereum/issues/2053 | |
if (v < 27) | |
v += 27; | |
if (v != 27 && v != 28) | |
return (false, 0); | |
return safer_ecrecover(hash, v, r, s); | |
} | |
function safeMemoryCleaner() internal pure { | |
assembly { | |
let fmem := mload(0x40) | |
codecopy(fmem, codesize, sub(msize, fmem)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment