Created
June 6, 2017 16:10
-
-
Save anonymous/979a94d70a9130d0bc2c4ea99a8034a5 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js&optimize=false&gist=
This file contains hidden or 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
/* | |
Kraken-based ETH/XBT price ticker | |
This contract keeps in storage an updated ETH/XBT price, | |
which is updated every ~60 seconds. | |
*/ | |
pragma solidity ^0.4.0; | |
import "oraclizeLib.sol"; | |
contract KrakenPriceTicker { | |
string public ETHXBT; | |
event newOraclizeQuery(string description); | |
event newKrakenPriceTicker(string price); | |
function KrakenPriceTicker() { | |
oraclizeLib.oraclize_setProof(oraclizeLib.proofType_TLSNotary() | oraclizeLib.proofStorage_IPFS()); | |
update(); | |
} | |
function __callback(bytes32 myid, string result, bytes proof) { | |
if (msg.sender != oraclizeLib.oraclize_cbAddress()) throw; | |
ETHXBT = result; | |
newKrakenPriceTicker(ETHXBT); | |
update(); | |
} | |
function update() payable { | |
if (oraclizeLib.oraclize_getPrice("URL") > this.balance) { | |
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee"); | |
} else { | |
newOraclizeQuery("Oraclize query was sent, standing by for the answer.."); | |
oraclizeLib.oraclize_query(60, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT).result.XETHXXBT.c.0"); | |
} | |
} | |
} |
This file contains hidden or 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
/* | |
Computation N args query example | |
Takes up to 16 string args and concatenates them | |
off-chain with a python script | |
Also makes use of InlineDynamicHelper library | |
for converting inline fixed string array declarations | |
to dynamic-sized arrays, which is what the Oraclize library expects | |
*/ | |
// NOTE, the computations here may crash remix's JavascriptVM | |
// Ensure you are loading in an incognito window, and in a single session | |
// only deploying and then calling the update function | |
pragma solidity ^0.4.0; | |
import "oraclizeLib.sol"; | |
import "InlineDynamicHelper.sol"; | |
contract OffchainConcat is usingInlineDynamic { | |
string public CONCATENATED; | |
address public OAR = oraclizeLib.getOAR(); | |
OraclizeI public localOrclInstance = oraclizeLib.getCON(); | |
uint constant public base = localOrclInstance.getPrice("URL"); | |
event newOraclizeQuery(string description); | |
event emitConcatMsg(string msg); | |
function OffchainConcat() { | |
oraclizeLib.oraclize_setProof(oraclizeLib.proofType_NONE()); | |
} | |
function __callback(bytes32 myid, string result) { | |
if (msg.sender != oraclizeLib.oraclize_cbAddress()) throw; | |
CONCATENATED = result; | |
emitConcatMsg(result); | |
} | |
function update() payable { | |
if (oraclizeLib.oraclize_getPrice("computation") > this.balance) { | |
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee"); | |
} else { | |
newOraclizeQuery("Oraclize query was sent, standing by for the answer.."); | |
oraclizeLib.oraclize_query("computation", | |
["QmQ4kKevJhmPfB3bNHh4xBVo4EE8fd3L7yoTVKPe6DCFus", | |
"Last", | |
"entry", | |
"will", | |
"be", | |
"bytes:", | |
oraclizeLib.b2s(hex'DEADBEEF1001') // bytes to string equivalent conversion | |
].toDynamic() | |
); | |
} | |
} | |
} |
This file contains hidden or 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.6; | |
library InlineDynamicHelper { | |
function toDynamic(string[1] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[2] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[3] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[4] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[5] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[6] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[7] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[8] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[9] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[10] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[11] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[12] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[13] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[14] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[15] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
function toDynamic(string[16] self) | |
internal | |
returns (string[]) { | |
string[] memory dyna = new string[](self.length); | |
for (uint i = 0; i < dyna.length; i++) { | |
dyna[i] = self[i]; | |
} | |
return dyna; | |
} | |
} | |
contract usingInlineDynamic { | |
using InlineDynamicHelper for string[1]; | |
using InlineDynamicHelper for string[2]; | |
using InlineDynamicHelper for string[3]; | |
using InlineDynamicHelper for string[4]; | |
using InlineDynamicHelper for string[5]; | |
using InlineDynamicHelper for string[6]; | |
using InlineDynamicHelper for string[7]; | |
using InlineDynamicHelper for string[8]; | |
using InlineDynamicHelper for string[9]; | |
using InlineDynamicHelper for string[10]; | |
using InlineDynamicHelper for string[11]; | |
using InlineDynamicHelper for string[12]; | |
using InlineDynamicHelper for string[13]; | |
using InlineDynamicHelper for string[14]; | |
using InlineDynamicHelper for string[15]; | |
using InlineDynamicHelper for string[16]; | |
} |
This file contains hidden or 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
// <ORACLIZE_API> | |
/* | |
Copyright (c) 2015-2016 Oraclize SRL | |
Copyright (c) 2016 Oraclize LTD | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
pragma solidity ^0.4.0; | |
contract OraclizeI { | |
address public cbAddress; | |
function query(uint _timestamp, string _datasource, string _arg) payable returns (bytes32 _id); | |
function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) payable returns (bytes32 _id); | |
function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) payable returns (bytes32 _id); | |
function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) payable returns (bytes32 _id); | |
function queryN(uint _timestamp, string _datasource, bytes _argN) payable returns (bytes32 _id); | |
function queryN_withGasLimit(uint _timestamp, string _datasource, bytes _argN, uint _gaslimit) payable returns (bytes32 _id); | |
function getPrice(string _datasource) returns (uint _dsprice); | |
function getPrice(string _datasource, uint gaslimit) returns (uint _dsprice); | |
function setProofType(byte _proofType); | |
function setConfig(bytes32 _config); | |
function setCustomGasPrice(uint _gasPrice); | |
} | |
contract OraclizeAddrResolverI { | |
function getAddress() returns (address _addr); | |
} | |
library oraclizeLib { | |
//byte constant internal proofType_NONE = 0x00; | |
function proofType_NONE() | |
constant | |
returns (byte) { | |
return 0x00; | |
} | |
//byte constant internal proofType_TLSNotary = 0x10; | |
function proofType_TLSNotary() | |
constant | |
returns (byte) { | |
return 0x10; | |
} | |
//byte constant internal proofStorage_IPFS = 0x01; | |
function proofStorage_IPFS() | |
constant | |
returns (byte) { | |
return 0x01; | |
} | |
// *******TRUFFLE + BRIDGE********* | |
//OraclizeAddrResolverI constant public OAR = OraclizeAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475); | |
// *****REALNET DEPLOYMENT****** | |
OraclizeAddrResolverI constant public OAR = oraclize_setNetwork(); // constant means dont store and re-eval on each call | |
function getOAR() | |
constant | |
returns (OraclizeAddrResolverI) { | |
return OAR; | |
} | |
OraclizeI constant public oraclize = OraclizeI(OAR.getAddress()); | |
function getCON() | |
constant | |
returns (OraclizeI) { | |
return oraclize; | |
} | |
function oraclize_setNetwork() | |
public | |
returns(OraclizeAddrResolverI){ | |
if (getCodeSize(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed)>0){ //mainnet | |
return OraclizeAddrResolverI(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed); | |
} | |
if (getCodeSize(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1)>0){ //ropsten testnet | |
return OraclizeAddrResolverI(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1); | |
} | |
if (getCodeSize(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e)>0){ //kovan testnet | |
return OraclizeAddrResolverI(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e); | |
} | |
if (getCodeSize(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48)>0){ //rinkeby testnet | |
return OraclizeAddrResolverI(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48); | |
} | |
if (getCodeSize(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475)>0){ //ethereum-bridge | |
return OraclizeAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475); | |
} | |
if (getCodeSize(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF)>0){ //ether.camp ide | |
return OraclizeAddrResolverI(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF); | |
} | |
if (getCodeSize(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA)>0){ //browser-solidity | |
return OraclizeAddrResolverI(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA); | |
} | |
} | |
function oraclize_getPrice(string datasource) | |
public | |
returns (uint){ | |
return oraclize.getPrice(datasource); | |
} | |
function oraclize_getPrice(string datasource, uint gaslimit) | |
public | |
returns (uint){ | |
return oraclize.getPrice(datasource, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg) | |
public | |
returns (bytes32 id){ | |
return oraclize_query(0, datasource, arg); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg) | |
public | |
returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource); | |
if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price | |
return oraclize.query.value(price)(timestamp, datasource, arg); | |
} | |
function oraclize_query(string datasource, string arg, uint gaslimit) | |
public | |
returns (bytes32 id){ | |
return oraclize_query(0, datasource, arg, gaslimit); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg, uint gaslimit) | |
public | |
returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource, gaslimit); | |
if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price | |
return oraclize.query_withGasLimit.value(price)(timestamp, datasource, arg, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg1, string arg2) | |
public | |
returns (bytes32 id){ | |
return oraclize_query(0, datasource, arg1, arg2); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg1, string arg2) | |
public | |
returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource); | |
if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price | |
return oraclize.query2.value(price)(timestamp, datasource, arg1, arg2); | |
} | |
function oraclize_query(string datasource, string arg1, string arg2, uint gaslimit) | |
public | |
returns (bytes32 id){ | |
return oraclize_query(0, datasource, arg1, arg2, gaslimit); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg1, string arg2, uint gaslimit) | |
public | |
returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource, gaslimit); | |
if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price | |
return oraclize.query2_withGasLimit.value(price)(timestamp, datasource, arg1, arg2, gaslimit); | |
} | |
function oraclize_query(string datasource, string[] argN) | |
internal | |
returns (bytes32 id){ | |
return oraclize_query(0, datasource, argN); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[] argN) | |
internal | |
returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource); | |
if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price | |
bytes memory args = stra2cbor(argN); | |
return oraclize.queryN.value(price)(timestamp, datasource, args); | |
} | |
function oraclize_query(string datasource, string[] argN, uint gaslimit) | |
internal | |
returns (bytes32 id){ | |
return oraclize_query(0, datasource, argN, gaslimit); | |
} | |
function oraclize_query(uint timestamp, string datasource, string[] argN, uint gaslimit) | |
internal | |
returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource, gaslimit); | |
if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price | |
bytes memory args = stra2cbor(argN); | |
return oraclize.queryN_withGasLimit.value(price)(timestamp, datasource, args, gaslimit); | |
} | |
function oraclize_cbAddress() | |
public | |
constant | |
returns (address){ | |
return oraclize.cbAddress(); | |
} | |
function oraclize_setProof(byte proofP) | |
public { | |
return oraclize.setProofType(proofP); | |
} | |
function oraclize_setCustomGasPrice(uint gasPrice) | |
public { | |
return oraclize.setCustomGasPrice(gasPrice); | |
} | |
function oraclize_setConfig(bytes32 config) | |
public { | |
return oraclize.setConfig(config); | |
} | |
function getCodeSize(address _addr) | |
public | |
returns(uint _size) { | |
assembly { | |
_size := extcodesize(_addr) | |
} | |
} | |
function parseAddr(string _a) | |
public | |
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) | |
public | |
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) | |
public | |
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 | |
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 | |
returns (string) { | |
return strConcat(_a, _b, _c, _d, ""); | |
} | |
function strConcat(string _a, string _b, string _c) | |
internal | |
returns (string) { | |
return strConcat(_a, _b, _c, "", ""); | |
} | |
function strConcat(string _a, string _b) | |
internal | |
returns (string) { | |
return strConcat(_a, _b, "", "", ""); | |
} | |
// parseInt | |
function parseInt(string _a) | |
public | |
constant | |
returns (uint) { | |
return parseInt(_a, 0); | |
} | |
// parseInt(parseFloat*10^_b) | |
function parseInt(string _a, uint _b) | |
public | |
constant | |
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 | |
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); | |
} | |
function stra2cbor(string[] arr) | |
internal | |
returns (bytes) { | |
uint arrlen = arr.length; | |
// get correct cbor output length | |
uint outputlen = 0; | |
bytes[] memory elemArray = new bytes[](arrlen); | |
for (uint i = 0; i < arrlen; i++) { | |
elemArray[i] = (bytes(arr[i])); | |
outputlen += elemArray[i].length + (elemArray[i].length - 1)/23 + 3; //+3 accounts for paired identifier types | |
} | |
uint ctr = 0; | |
uint cborlen = arrlen + 0x80; | |
outputlen += byte(cborlen).length; | |
bytes memory res = new bytes(outputlen); | |
while (byte(cborlen).length > ctr) { | |
res[ctr] = byte(cborlen)[ctr]; | |
ctr++; | |
} | |
for (i = 0; i < arrlen; i++) { | |
res[ctr] = 0x5F; | |
ctr++; | |
for (uint x = 0; x < elemArray[i].length; x++) { | |
// if there's a bug with larger strings, this may be the culprit | |
if (x % 23 == 0) { | |
uint elemcborlen = elemArray[i].length - x >= 24 ? 23 : elemArray[i].length - x; | |
elemcborlen += 0x40; | |
uint lctr = ctr; | |
while (byte(elemcborlen).length > ctr - lctr) { | |
res[ctr] = byte(elemcborlen)[ctr - lctr]; | |
ctr++; | |
} | |
} | |
res[ctr] = elemArray[i][x]; | |
ctr++; | |
} | |
res[ctr] = 0xFF; | |
ctr++; | |
} | |
return res; | |
} | |
function b2s(bytes _b) | |
internal | |
returns (string) { | |
bytes memory output = new bytes(_b.length * 2); | |
uint len = output.length; | |
assembly { | |
let i := 0 | |
let mem := 0 | |
loop: | |
// isolate octet | |
0x1000000000000000000000000000000000000000000000000000000000000000 | |
exp(0x10, mod(i, 0x40)) | |
// change offset only if needed | |
jumpi(skip, gt(mod(i, 0x40), 0)) | |
// save offset mem for reuse | |
mem := mload(add(_b, add(mul(0x20, div(i, 0x40)), 0x20))) | |
skip: | |
mem | |
mul | |
div | |
dup1 | |
// check if alpha or numerical, jump if numerical | |
0x0a | |
swap1 | |
lt | |
num | |
jumpi | |
// offset alpha char correctly | |
0x0a | |
swap1 | |
sub | |
alp: | |
0x61 | |
add | |
jump(end) | |
num: | |
0x30 | |
add | |
end: | |
add(output, add(0x20, i)) | |
mstore8 | |
i := add(i, 1) | |
jumpi(loop, gt(len, i)) | |
} | |
return string(output); | |
} | |
} | |
// </ORACLIZE_API> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment