├── .gitattributes
├── .github
└── workflows
│ └── test.yaml
├── .gitignore
├── LICENSE
├── README.md
├── contracts
├── solc-v0.4.25
│ ├── oraclizeAPI.sol
│ └── provableAPI.sol
├── solc-v0.5.x
│ ├── oraclizeAPI.sol
│ └── provableAPI.sol
├── solc-v0.6.x
│ └── provableAPI.sol
└── solc-v0.8.x
│ ├── interfaces
│ ├── provable-address-resolver-interface.sol
│ └── provable-interface.sol
│ ├── libraries
│ ├── BUFFER.sol
│ └── CBOR.sol
│ └── provableAPI.sol
├── hardhat.config.js
├── old-contracts
├── README.md
├── connectors
│ ├── README.md
│ ├── addressResolver.sol
│ └── oraclizeConnector.sol
├── lib-experimental
│ ├── README.md
│ ├── inlineDynamicHelper_lib.sol
│ └── oraclizeAPI_lib.sol
└── previous-api-contracts
│ ├── README.md
│ ├── oraclizeAPI.se
│ ├── oraclizeAPI_0.4.sol
│ └── oraclizeAPI_pre0.4.sol
├── oraclizeAPI.sol
├── package-lock.json
├── package.json
├── pnpm-lock.yaml
├── provableAPI.sol
└── test
├── run-tests.sh
└── test-compilation.sh
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.sol linguist-language=Solidity
2 |
--------------------------------------------------------------------------------
/.github/workflows/test.yaml:
--------------------------------------------------------------------------------
1 | name: Node.js CI
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | strategy:
15 | matrix:
16 | node-version: [16.x]
17 |
18 | steps:
19 | - uses: actions/checkout@v2
20 | - name: Use Node.js ${{ matrix.node-version }}
21 | uses: actions/setup-node@v2
22 | with:
23 | node-version: ${{ matrix.node-version }}
24 | cache: 'npm'
25 | cache-dependency-path: ./package-lock.json
26 | - run: npm run test
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.sw*
2 | **/node_modules
3 | **/cache
4 | **/artifacts
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2015-2016 Oraclize SRL
4 | Copyright (c) 2016-2019 Oraclize LTD
5 | Copyright (c) 2019 Provable Things Limited
6 |
7 | Permission is hereby granted, free of charge, to any person obtaining a copy
8 | of this software and associated documentation files (the "Software"), to deal
9 | in the Software without restriction, including without limitation the rights
10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | copies of the Software, and to permit persons to whom the Software is
12 | furnished to do so, subject to the following conditions:
13 |
14 | The above copyright notice and this permission notice shall be included in all
15 | copies or substantial portions of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 | SOFTWARE.
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Provable Ethereum API [](https://gitter.im/oraclize/ethereum-api?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](http://docs.provable.xyz) [](http://hits.dwyl.io/oraclize/ethereum-api) [](https://opensource.org/licenses/mit-license.php) [](https://cloud.drone.io/provable-things/ethereum-api)
2 |
3 |
4 |
5 | Thanks to this __Ethereum API__, enriching your smart-contracts with external data using __Provable__ is very easy!
6 |
7 | In Solidity it is as simple as inheriting the __`usingProvable`__ contract that you'll find in this repository.
8 |
9 | This will provide your contract with functions like __`provable_query(...)`__, which make it trivial for you to leverage our oracle technology straight away.
10 |
11 | If you're using the __[Remix IDE](http://remix.ethereum.org)__ it's even easier still - simply import the latest version of __Provable__ into your contract like so:
12 |
13 | ```solidity
14 |
15 | import "github.com/provable-things/ethereum-api/provableAPI.sol";
16 |
17 | ```
18 |
19 | Or if you need a specific version of the API, import that directly via:
20 |
21 | ```solidity
22 |
23 | import "github.com/provable-things/ethereum-api/contracts/solc-v0.8.x/provableAPI.sol";
24 |
25 | ```
26 |
27 |
28 | There are versions of the API targetting the following `solc` compilers:
29 |
30 | ```
31 | solc-v0.4.25
32 | solc-v0.5.x
33 | solc-v0.6.x
34 | solc-v0.8.x
35 |
36 | ```
37 |
38 | To learn more about the Provable technology, please refer to our __[documentation here](https://docs.provable.xyz)__.
39 |
40 |
41 |
42 | ***
43 |
44 |
45 |
46 | ### :computer: See It In Action!
47 |
48 | For working examples of how to integrate the __Provable__ API into your own smart-contracts, head on over to the __[Provable Ethereum Examples](https://github.com/provable-things/ethereum-examples)__ repository. Here you'll find various examples that use __Provable__ to feed smart-contracts with data from a variety of external sources.
49 |
50 | There are even __[some examples here](https://github.com/provable-things/ethereum-examples/tree/master/solidity/truffle-examples)__ showing you how you can use __Provable__ in a local Truffle development environment!
51 |
52 |
53 |
54 | ***
55 |
56 |
57 |
58 | ### :mega: __Get In Touch!__
59 |
60 | If you want to ask us something, or tell us something, there's loads of ways to get in touch:
61 |
62 | __❍__ We have a __[Twitter](https://twitter.com/provablethings)__
63 |
64 | __❍__ And a __[Gitter](https://gitter.im/oraclize/ethereum-api)__
65 |
66 | __❍__ Or a __[Website](https://provable.xyz)__
67 |
68 | __❍__ Alongside a __[Youtube](https://www.youtube.com/channel/UCjVjCheDbMel-x-JYeGazcQ)__
69 |
70 | __❍__ Plus a __[Github](https://github.com/provable-things)__
71 |
72 |
73 |
74 | ***
75 |
76 |
77 |
78 | ### :radioactive: __A Note Regarding Serpent:__
79 |
80 | :skull: __CAUTION__: It is highly recommended to avoid using Serpent, especially in production. The serpent version of the __Provable__ API herein remains for historical reasons but support for it is no longer maintained. Serpent is considered outdated and audits have shown it to be flawed. Use it at your own risk!
81 |
--------------------------------------------------------------------------------
/contracts/solc-v0.4.25/provableAPI.sol:
--------------------------------------------------------------------------------
1 | //
2 | // Release targetted at solc 0.4.25 to silence compiler warning/error messages, compatible down to 0.4.22
3 |
4 | /*
5 | Copyright (c) 2015-2016 Oraclize SRL
6 | Copyright (c) 2016-2019 Oraclize LTD
7 | Copyright (c) 2019 Provable Things Limited
8 |
9 |
10 |
11 | Permission is hereby granted, free of charge, to any person obtaining a copy
12 | of this software and associated documentation files (the "Software"), to deal
13 | in the Software without restriction, including without limitation the rights
14 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 | copies of the Software, and to permit persons to whom the Software is
16 | furnished to do so, subject to the following conditions:
17 |
18 |
19 |
20 | The above copyright notice and this permission notice shall be included in
21 | all copies or substantial portions of the Software.
22 |
23 |
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 | THE SOFTWARE.
32 | */
33 |
34 | pragma solidity >= 0.4.22 < 0.5;// Incompatible compiler version... please select one stated within pragma solidity or use different provableAPI version
35 |
36 | contract ProvableI {
37 | address public cbAddress;
38 | function query(uint _timestamp, string _datasource, string _arg) external payable returns (bytes32 _id);
39 | function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) external payable returns (bytes32 _id);
40 | function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) public payable returns (bytes32 _id);
41 | function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) external payable returns (bytes32 _id);
42 | function queryN(uint _timestamp, string _datasource, bytes _argN) public payable returns (bytes32 _id);
43 | function queryN_withGasLimit(uint _timestamp, string _datasource, bytes _argN, uint _gaslimit) external payable returns (bytes32 _id);
44 | function getPrice(string _datasource) public returns (uint _dsprice);
45 | function getPrice(string _datasource, uint gaslimit) public returns (uint _dsprice);
46 | function setProofType(byte _proofType) external;
47 | function setCustomGasPrice(uint _gasPrice) external;
48 | function randomDS_getSessionPubKeyHash() external constant returns(bytes32);
49 | }
50 |
51 | contract OracleAddrResolverI {
52 | function getAddress() public returns (address _addr);
53 | }
54 |
55 | /*
56 | Begin solidity-cborutils
57 |
58 | https://github.com/smartcontractkit/solidity-cborutils
59 |
60 | MIT License
61 |
62 | Copyright (c) 2018 SmartContract ChainLink, Ltd.
63 |
64 | Permission is hereby granted, free of charge, to any person obtaining a copy
65 | of this software and associated documentation files (the "Software"), to deal
66 | in the Software without restriction, including without limitation the rights
67 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
68 | copies of the Software, and to permit persons to whom the Software is
69 | furnished to do so, subject to the following conditions:
70 |
71 | The above copyright notice and this permission notice shall be included in all
72 | copies or substantial portions of the Software.
73 |
74 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
75 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
76 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
77 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
78 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
79 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
80 | SOFTWARE.
81 | */
82 |
83 | library Buffer {
84 | struct buffer {
85 | bytes buf;
86 | uint capacity;
87 | }
88 |
89 | function init(buffer memory buf, uint _capacity) internal pure {
90 | uint capacity = _capacity;
91 | if(capacity % 32 != 0) capacity += 32 - (capacity % 32);
92 | // Allocate space for the buffer data
93 | buf.capacity = capacity;
94 | assembly {
95 | let ptr := mload(0x40)
96 | mstore(buf, ptr)
97 | mstore(ptr, 0)
98 | mstore(0x40, add(ptr, capacity))
99 | }
100 | }
101 |
102 | function resize(buffer memory buf, uint capacity) private pure {
103 | bytes memory oldbuf = buf.buf;
104 | init(buf, capacity);
105 | append(buf, oldbuf);
106 | }
107 |
108 | function max(uint a, uint b) private pure returns(uint) {
109 | if(a > b) {
110 | return a;
111 | }
112 | return b;
113 | }
114 |
115 | /**
116 | * @dev Appends a byte array to the end of the buffer. Resizes if doing so
117 | * would exceed the capacity of the buffer.
118 | * @param buf The buffer to append to.
119 | * @param data The data to append.
120 | * @return The original buffer.
121 | */
122 | function append(buffer memory buf, bytes data) internal pure returns(buffer memory) {
123 | if(data.length + buf.buf.length > buf.capacity) {
124 | resize(buf, max(buf.capacity, data.length) * 2);
125 | }
126 |
127 | uint dest;
128 | uint src;
129 | uint len = data.length;
130 | assembly {
131 | // Memory address of the buffer data
132 | let bufptr := mload(buf)
133 | // Length of existing buffer data
134 | let buflen := mload(bufptr)
135 | // Start address = buffer address + buffer length + sizeof(buffer length)
136 | dest := add(add(bufptr, buflen), 32)
137 | // Update buffer length
138 | mstore(bufptr, add(buflen, mload(data)))
139 | src := add(data, 32)
140 | }
141 |
142 | // Copy word-length chunks while possible
143 | for(; len >= 32; len -= 32) {
144 | assembly {
145 | mstore(dest, mload(src))
146 | }
147 | dest += 32;
148 | src += 32;
149 | }
150 |
151 | // Copy remaining bytes
152 | uint mask = 256 ** (32 - len) - 1;
153 | assembly {
154 | let srcpart := and(mload(src), not(mask))
155 | let destpart := and(mload(dest), mask)
156 | mstore(dest, or(destpart, srcpart))
157 | }
158 |
159 | return buf;
160 | }
161 |
162 | /**
163 | * @dev Appends a byte to the end of the buffer. Resizes if doing so would
164 | * exceed the capacity of the buffer.
165 | * @param buf The buffer to append to.
166 | * @param data The data to append.
167 | * @return The original buffer.
168 | */
169 | function append(buffer memory buf, uint8 data) internal pure {
170 | if(buf.buf.length + 1 > buf.capacity) {
171 | resize(buf, buf.capacity * 2);
172 | }
173 |
174 | assembly {
175 | // Memory address of the buffer data
176 | let bufptr := mload(buf)
177 | // Length of existing buffer data
178 | let buflen := mload(bufptr)
179 | // Address = buffer address + buffer length + sizeof(buffer length)
180 | let dest := add(add(bufptr, buflen), 32)
181 | mstore8(dest, data)
182 | // Update buffer length
183 | mstore(bufptr, add(buflen, 1))
184 | }
185 | }
186 |
187 | /**
188 | * @dev Appends a byte to the end of the buffer. Resizes if doing so would
189 | * exceed the capacity of the buffer.
190 | * @param buf The buffer to append to.
191 | * @param data The data to append.
192 | * @return The original buffer.
193 | */
194 | function appendInt(buffer memory buf, uint data, uint len) internal pure returns(buffer memory) {
195 | if(len + buf.buf.length > buf.capacity) {
196 | resize(buf, max(buf.capacity, len) * 2);
197 | }
198 |
199 | uint mask = 256 ** len - 1;
200 | assembly {
201 | // Memory address of the buffer data
202 | let bufptr := mload(buf)
203 | // Length of existing buffer data
204 | let buflen := mload(bufptr)
205 | // Address = buffer address + buffer length + sizeof(buffer length) + len
206 | let dest := add(add(bufptr, buflen), len)
207 | mstore(dest, or(and(mload(dest), not(mask)), data))
208 | // Update buffer length
209 | mstore(bufptr, add(buflen, len))
210 | }
211 | return buf;
212 | }
213 | }
214 |
215 | library CBOR {
216 | using Buffer for Buffer.buffer;
217 |
218 | uint8 private constant MAJOR_TYPE_INT = 0;
219 | uint8 private constant MAJOR_TYPE_NEGATIVE_INT = 1;
220 | uint8 private constant MAJOR_TYPE_BYTES = 2;
221 | uint8 private constant MAJOR_TYPE_STRING = 3;
222 | uint8 private constant MAJOR_TYPE_ARRAY = 4;
223 | uint8 private constant MAJOR_TYPE_MAP = 5;
224 | uint8 private constant MAJOR_TYPE_CONTENT_FREE = 7;
225 |
226 | function encodeType(Buffer.buffer memory buf, uint8 major, uint value) private pure {
227 | if(value <= 23) {
228 | buf.append(uint8((major << 5) | value));
229 | } else if(value <= 0xFF) {
230 | buf.append(uint8((major << 5) | 24));
231 | buf.appendInt(value, 1);
232 | } else if(value <= 0xFFFF) {
233 | buf.append(uint8((major << 5) | 25));
234 | buf.appendInt(value, 2);
235 | } else if(value <= 0xFFFFFFFF) {
236 | buf.append(uint8((major << 5) | 26));
237 | buf.appendInt(value, 4);
238 | } else if(value <= 0xFFFFFFFFFFFFFFFF) {
239 | buf.append(uint8((major << 5) | 27));
240 | buf.appendInt(value, 8);
241 | }
242 | }
243 |
244 | function encodeIndefiniteLengthType(Buffer.buffer memory buf, uint8 major) private pure {
245 | buf.append(uint8((major << 5) | 31));
246 | }
247 |
248 | function encodeUInt(Buffer.buffer memory buf, uint value) internal pure {
249 | encodeType(buf, MAJOR_TYPE_INT, value);
250 | }
251 |
252 | function encodeInt(Buffer.buffer memory buf, int value) internal pure {
253 | if(value >= 0) {
254 | encodeType(buf, MAJOR_TYPE_INT, uint(value));
255 | } else {
256 | encodeType(buf, MAJOR_TYPE_NEGATIVE_INT, uint(-1 - value));
257 | }
258 | }
259 |
260 | function encodeBytes(Buffer.buffer memory buf, bytes value) internal pure {
261 | encodeType(buf, MAJOR_TYPE_BYTES, value.length);
262 | buf.append(value);
263 | }
264 |
265 | function encodeString(Buffer.buffer memory buf, string value) internal pure {
266 | encodeType(buf, MAJOR_TYPE_STRING, bytes(value).length);
267 | buf.append(bytes(value));
268 | }
269 |
270 | function startArray(Buffer.buffer memory buf) internal pure {
271 | encodeIndefiniteLengthType(buf, MAJOR_TYPE_ARRAY);
272 | }
273 |
274 | function startMap(Buffer.buffer memory buf) internal pure {
275 | encodeIndefiniteLengthType(buf, MAJOR_TYPE_MAP);
276 | }
277 |
278 | function endSequence(Buffer.buffer memory buf) internal pure {
279 | encodeIndefiniteLengthType(buf, MAJOR_TYPE_CONTENT_FREE);
280 | }
281 | }
282 |
283 | /*
284 | End solidity-cborutils
285 | */
286 |
287 | contract usingProvable {
288 | uint constant day = 60*60*24;
289 | uint constant week = 60*60*24*7;
290 | uint constant month = 60*60*24*30;
291 | byte constant proofType_NONE = 0x00;
292 | byte constant proofType_TLSNotary = 0x10;
293 | byte constant proofType_Ledger = 0x30;
294 | byte constant proofType_Android = 0x40;
295 | byte constant proofType_Native = 0xF0;
296 | byte constant proofStorage_IPFS = 0x01;
297 | uint8 constant networkID_auto = 0;
298 | uint8 constant networkID_mainnet = 1;
299 | uint8 constant networkID_testnet = 2;
300 | uint8 constant networkID_morden = 2;
301 | uint8 constant networkID_consensys = 161;
302 |
303 | OracleAddrResolverI OAR;
304 |
305 | ProvableI provable;
306 | modifier provableAPI {
307 | if((address(OAR)==0)||(getCodeSize(address(OAR))==0))
308 | provable_setNetwork(networkID_auto);
309 |
310 | if(address(provable) != OAR.getAddress())
311 | provable = ProvableI(OAR.getAddress());
312 |
313 | _;
314 | }
315 | modifier coupon(string code){
316 | provable = ProvableI(OAR.getAddress());
317 | _;
318 | }
319 |
320 | function provable_setNetwork(uint8 networkID) internal returns(bool){
321 | return provable_setNetwork();
322 | networkID; // silence the warning and remain backwards compatible
323 | }
324 | function provable_setNetwork() internal returns(bool){
325 | if (getCodeSize(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed)>0){ //mainnet
326 | OAR = OracleAddrResolverI(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed);
327 | provable_setNetworkName("eth_mainnet");
328 | return true;
329 | }
330 | if (getCodeSize(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1)>0){ //ropsten testnet
331 | OAR = OracleAddrResolverI(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1);
332 | provable_setNetworkName("eth_ropsten3");
333 | return true;
334 | }
335 | if (getCodeSize(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e)>0){ //kovan testnet
336 | OAR = OracleAddrResolverI(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e);
337 | provable_setNetworkName("eth_kovan");
338 | return true;
339 | }
340 | if (getCodeSize(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48)>0){ //rinkeby testnet
341 | OAR = OracleAddrResolverI(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48);
342 | provable_setNetworkName("eth_rinkeby");
343 | return true;
344 | }
345 | if (getCodeSize(0xa2998EFD205FB9D4B4963aFb70778D6354ad3A41)>0){ //goerli testnet
346 | OAR = OracleAddrResolverI(0xa2998EFD205FB9D4B4963aFb70778D6354ad3A41);
347 | provable_setNetworkName("eth_goerli");
348 | return true;
349 | }
350 | if (getCodeSize(0x90A0F94702c9630036FB9846B52bf31A1C991a84)>0){ //bsc mainnet
351 | OAR = OracleAddrResolverI(0x90A0F94702c9630036FB9846B52bf31A1C991a84);
352 | provable_setNetworkName("bsc_mainnet");
353 | return true;
354 | }
355 | if (getCodeSize(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609)>0){ //polygon mainnet
356 | OAR = OracleAddrResolverI(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609);
357 | provable_setNetworkName("polygon_mainnet");
358 | return true;
359 | }
360 | if (getCodeSize(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85)>0){ //sepolia mainnet
361 | OAR = OracleAddrResolverI(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85);
362 | provable_setNetworkName("sepolia_mainnet");
363 | return true;
364 | }
365 | if (getCodeSize(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475)>0){ //ethereum-bridge
366 | OAR = OracleAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475);
367 | return true;
368 | }
369 | if (getCodeSize(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF)>0){ //ether.camp ide
370 | OAR = OracleAddrResolverI(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF);
371 | return true;
372 | }
373 | if (getCodeSize(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA)>0){ //browser-solidity
374 | OAR = OracleAddrResolverI(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA);
375 | return true;
376 | }
377 | return false;
378 | }
379 | /**
380 | * @dev The following `__callback` functions are just placeholders ideally
381 | * meant to be defined in child contract when proofs are used.
382 | * The function bodies simply silence compiler warnings.
383 | */
384 | function __callback(bytes32 myid, string result) public {
385 | __callback(myid, result, new bytes(0));
386 | }
387 |
388 | function __callback(bytes32 myid, string result, bytes proof) public {
389 | return;
390 | myid; result; proof;
391 | provable_randomDS_args[bytes32(0)] = bytes32(0);
392 | }
393 |
394 | function provable_getPrice(string datasource) provableAPI internal returns (uint){
395 | return provable.getPrice(datasource);
396 | }
397 |
398 | function provable_getPrice(string datasource, uint gaslimit) provableAPI internal returns (uint){
399 | return provable.getPrice(datasource, gaslimit);
400 | }
401 |
402 | function provable_query(string datasource, string arg) provableAPI internal returns (bytes32 id){
403 | uint price = provable.getPrice(datasource);
404 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
405 | return provable.query.value(price)(0, datasource, arg);
406 | }
407 | function provable_query(uint timestamp, string datasource, string arg) provableAPI internal returns (bytes32 id){
408 | uint price = provable.getPrice(datasource);
409 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
410 | return provable.query.value(price)(timestamp, datasource, arg);
411 | }
412 | function provable_query(uint timestamp, string datasource, string arg, uint gaslimit) provableAPI internal returns (bytes32 id){
413 | uint price = provable.getPrice(datasource, gaslimit);
414 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
415 | return provable.query_withGasLimit.value(price)(timestamp, datasource, arg, gaslimit);
416 | }
417 | function provable_query(string datasource, string arg, uint gaslimit) provableAPI internal returns (bytes32 id){
418 | uint price = provable.getPrice(datasource, gaslimit);
419 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
420 | return provable.query_withGasLimit.value(price)(0, datasource, arg, gaslimit);
421 | }
422 | function provable_query(string datasource, string arg1, string arg2) provableAPI internal returns (bytes32 id){
423 | uint price = provable.getPrice(datasource);
424 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
425 | return provable.query2.value(price)(0, datasource, arg1, arg2);
426 | }
427 | function provable_query(uint timestamp, string datasource, string arg1, string arg2) provableAPI internal returns (bytes32 id){
428 | uint price = provable.getPrice(datasource);
429 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
430 | return provable.query2.value(price)(timestamp, datasource, arg1, arg2);
431 | }
432 | function provable_query(uint timestamp, string datasource, string arg1, string arg2, uint gaslimit) provableAPI internal returns (bytes32 id){
433 | uint price = provable.getPrice(datasource, gaslimit);
434 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
435 | return provable.query2_withGasLimit.value(price)(timestamp, datasource, arg1, arg2, gaslimit);
436 | }
437 | function provable_query(string datasource, string arg1, string arg2, uint gaslimit) provableAPI internal returns (bytes32 id){
438 | uint price = provable.getPrice(datasource, gaslimit);
439 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
440 | return provable.query2_withGasLimit.value(price)(0, datasource, arg1, arg2, gaslimit);
441 | }
442 | function provable_query(string datasource, string[] argN) provableAPI internal returns (bytes32 id){
443 | uint price = provable.getPrice(datasource);
444 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
445 | bytes memory args = stra2cbor(argN);
446 | return provable.queryN.value(price)(0, datasource, args);
447 | }
448 | function provable_query(uint timestamp, string datasource, string[] argN) provableAPI internal returns (bytes32 id){
449 | uint price = provable.getPrice(datasource);
450 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
451 | bytes memory args = stra2cbor(argN);
452 | return provable.queryN.value(price)(timestamp, datasource, args);
453 | }
454 | function provable_query(uint timestamp, string datasource, string[] argN, uint gaslimit) provableAPI internal returns (bytes32 id){
455 | uint price = provable.getPrice(datasource, gaslimit);
456 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
457 | bytes memory args = stra2cbor(argN);
458 | return provable.queryN_withGasLimit.value(price)(timestamp, datasource, args, gaslimit);
459 | }
460 | function provable_query(string datasource, string[] argN, uint gaslimit) provableAPI internal returns (bytes32 id){
461 | uint price = provable.getPrice(datasource, gaslimit);
462 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
463 | bytes memory args = stra2cbor(argN);
464 | return provable.queryN_withGasLimit.value(price)(0, datasource, args, gaslimit);
465 | }
466 | function provable_query(string datasource, string[1] args) provableAPI internal returns (bytes32 id) {
467 | string[] memory dynargs = new string[](1);
468 | dynargs[0] = args[0];
469 | return provable_query(datasource, dynargs);
470 | }
471 | function provable_query(uint timestamp, string datasource, string[1] args) provableAPI internal returns (bytes32 id) {
472 | string[] memory dynargs = new string[](1);
473 | dynargs[0] = args[0];
474 | return provable_query(timestamp, datasource, dynargs);
475 | }
476 | function provable_query(uint timestamp, string datasource, string[1] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
477 | string[] memory dynargs = new string[](1);
478 | dynargs[0] = args[0];
479 | return provable_query(timestamp, datasource, dynargs, gaslimit);
480 | }
481 | function provable_query(string datasource, string[1] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
482 | string[] memory dynargs = new string[](1);
483 | dynargs[0] = args[0];
484 | return provable_query(datasource, dynargs, gaslimit);
485 | }
486 |
487 | function provable_query(string datasource, string[2] args) provableAPI internal returns (bytes32 id) {
488 | string[] memory dynargs = new string[](2);
489 | dynargs[0] = args[0];
490 | dynargs[1] = args[1];
491 | return provable_query(datasource, dynargs);
492 | }
493 | function provable_query(uint timestamp, string datasource, string[2] args) provableAPI internal returns (bytes32 id) {
494 | string[] memory dynargs = new string[](2);
495 | dynargs[0] = args[0];
496 | dynargs[1] = args[1];
497 | return provable_query(timestamp, datasource, dynargs);
498 | }
499 | function provable_query(uint timestamp, string datasource, string[2] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
500 | string[] memory dynargs = new string[](2);
501 | dynargs[0] = args[0];
502 | dynargs[1] = args[1];
503 | return provable_query(timestamp, datasource, dynargs, gaslimit);
504 | }
505 | function provable_query(string datasource, string[2] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
506 | string[] memory dynargs = new string[](2);
507 | dynargs[0] = args[0];
508 | dynargs[1] = args[1];
509 | return provable_query(datasource, dynargs, gaslimit);
510 | }
511 | function provable_query(string datasource, string[3] args) provableAPI internal returns (bytes32 id) {
512 | string[] memory dynargs = new string[](3);
513 | dynargs[0] = args[0];
514 | dynargs[1] = args[1];
515 | dynargs[2] = args[2];
516 | return provable_query(datasource, dynargs);
517 | }
518 | function provable_query(uint timestamp, string datasource, string[3] args) provableAPI internal returns (bytes32 id) {
519 | string[] memory dynargs = new string[](3);
520 | dynargs[0] = args[0];
521 | dynargs[1] = args[1];
522 | dynargs[2] = args[2];
523 | return provable_query(timestamp, datasource, dynargs);
524 | }
525 | function provable_query(uint timestamp, string datasource, string[3] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
526 | string[] memory dynargs = new string[](3);
527 | dynargs[0] = args[0];
528 | dynargs[1] = args[1];
529 | dynargs[2] = args[2];
530 | return provable_query(timestamp, datasource, dynargs, gaslimit);
531 | }
532 | function provable_query(string datasource, string[3] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
533 | string[] memory dynargs = new string[](3);
534 | dynargs[0] = args[0];
535 | dynargs[1] = args[1];
536 | dynargs[2] = args[2];
537 | return provable_query(datasource, dynargs, gaslimit);
538 | }
539 |
540 | function provable_query(string datasource, string[4] args) provableAPI internal returns (bytes32 id) {
541 | string[] memory dynargs = new string[](4);
542 | dynargs[0] = args[0];
543 | dynargs[1] = args[1];
544 | dynargs[2] = args[2];
545 | dynargs[3] = args[3];
546 | return provable_query(datasource, dynargs);
547 | }
548 | function provable_query(uint timestamp, string datasource, string[4] args) provableAPI internal returns (bytes32 id) {
549 | string[] memory dynargs = new string[](4);
550 | dynargs[0] = args[0];
551 | dynargs[1] = args[1];
552 | dynargs[2] = args[2];
553 | dynargs[3] = args[3];
554 | return provable_query(timestamp, datasource, dynargs);
555 | }
556 | function provable_query(uint timestamp, string datasource, string[4] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
557 | string[] memory dynargs = new string[](4);
558 | dynargs[0] = args[0];
559 | dynargs[1] = args[1];
560 | dynargs[2] = args[2];
561 | dynargs[3] = args[3];
562 | return provable_query(timestamp, datasource, dynargs, gaslimit);
563 | }
564 | function provable_query(string datasource, string[4] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
565 | string[] memory dynargs = new string[](4);
566 | dynargs[0] = args[0];
567 | dynargs[1] = args[1];
568 | dynargs[2] = args[2];
569 | dynargs[3] = args[3];
570 | return provable_query(datasource, dynargs, gaslimit);
571 | }
572 | function provable_query(string datasource, string[5] args) provableAPI internal returns (bytes32 id) {
573 | string[] memory dynargs = new string[](5);
574 | dynargs[0] = args[0];
575 | dynargs[1] = args[1];
576 | dynargs[2] = args[2];
577 | dynargs[3] = args[3];
578 | dynargs[4] = args[4];
579 | return provable_query(datasource, dynargs);
580 | }
581 | function provable_query(uint timestamp, string datasource, string[5] args) provableAPI internal returns (bytes32 id) {
582 | string[] memory dynargs = new string[](5);
583 | dynargs[0] = args[0];
584 | dynargs[1] = args[1];
585 | dynargs[2] = args[2];
586 | dynargs[3] = args[3];
587 | dynargs[4] = args[4];
588 | return provable_query(timestamp, datasource, dynargs);
589 | }
590 | function provable_query(uint timestamp, string datasource, string[5] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
591 | string[] memory dynargs = new string[](5);
592 | dynargs[0] = args[0];
593 | dynargs[1] = args[1];
594 | dynargs[2] = args[2];
595 | dynargs[3] = args[3];
596 | dynargs[4] = args[4];
597 | return provable_query(timestamp, datasource, dynargs, gaslimit);
598 | }
599 | function provable_query(string datasource, string[5] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
600 | string[] memory dynargs = new string[](5);
601 | dynargs[0] = args[0];
602 | dynargs[1] = args[1];
603 | dynargs[2] = args[2];
604 | dynargs[3] = args[3];
605 | dynargs[4] = args[4];
606 | return provable_query(datasource, dynargs, gaslimit);
607 | }
608 | function provable_query(string datasource, bytes[] argN) provableAPI internal returns (bytes32 id){
609 | uint price = provable.getPrice(datasource);
610 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
611 | bytes memory args = ba2cbor(argN);
612 | return provable.queryN.value(price)(0, datasource, args);
613 | }
614 | function provable_query(uint timestamp, string datasource, bytes[] argN) provableAPI internal returns (bytes32 id){
615 | uint price = provable.getPrice(datasource);
616 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
617 | bytes memory args = ba2cbor(argN);
618 | return provable.queryN.value(price)(timestamp, datasource, args);
619 | }
620 | function provable_query(uint timestamp, string datasource, bytes[] argN, uint gaslimit) provableAPI internal returns (bytes32 id){
621 | uint price = provable.getPrice(datasource, gaslimit);
622 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
623 | bytes memory args = ba2cbor(argN);
624 | return provable.queryN_withGasLimit.value(price)(timestamp, datasource, args, gaslimit);
625 | }
626 | function provable_query(string datasource, bytes[] argN, uint gaslimit) provableAPI internal returns (bytes32 id){
627 | uint price = provable.getPrice(datasource, gaslimit);
628 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
629 | bytes memory args = ba2cbor(argN);
630 | return provable.queryN_withGasLimit.value(price)(0, datasource, args, gaslimit);
631 | }
632 | function provable_query(string datasource, bytes[1] args) provableAPI internal returns (bytes32 id) {
633 | bytes[] memory dynargs = new bytes[](1);
634 | dynargs[0] = args[0];
635 | return provable_query(datasource, dynargs);
636 | }
637 | function provable_query(uint timestamp, string datasource, bytes[1] args) provableAPI internal returns (bytes32 id) {
638 | bytes[] memory dynargs = new bytes[](1);
639 | dynargs[0] = args[0];
640 | return provable_query(timestamp, datasource, dynargs);
641 | }
642 | function provable_query(uint timestamp, string datasource, bytes[1] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
643 | bytes[] memory dynargs = new bytes[](1);
644 | dynargs[0] = args[0];
645 | return provable_query(timestamp, datasource, dynargs, gaslimit);
646 | }
647 | function provable_query(string datasource, bytes[1] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
648 | bytes[] memory dynargs = new bytes[](1);
649 | dynargs[0] = args[0];
650 | return provable_query(datasource, dynargs, gaslimit);
651 | }
652 |
653 | function provable_query(string datasource, bytes[2] args) provableAPI internal returns (bytes32 id) {
654 | bytes[] memory dynargs = new bytes[](2);
655 | dynargs[0] = args[0];
656 | dynargs[1] = args[1];
657 | return provable_query(datasource, dynargs);
658 | }
659 | function provable_query(uint timestamp, string datasource, bytes[2] args) provableAPI internal returns (bytes32 id) {
660 | bytes[] memory dynargs = new bytes[](2);
661 | dynargs[0] = args[0];
662 | dynargs[1] = args[1];
663 | return provable_query(timestamp, datasource, dynargs);
664 | }
665 | function provable_query(uint timestamp, string datasource, bytes[2] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
666 | bytes[] memory dynargs = new bytes[](2);
667 | dynargs[0] = args[0];
668 | dynargs[1] = args[1];
669 | return provable_query(timestamp, datasource, dynargs, gaslimit);
670 | }
671 | function provable_query(string datasource, bytes[2] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
672 | bytes[] memory dynargs = new bytes[](2);
673 | dynargs[0] = args[0];
674 | dynargs[1] = args[1];
675 | return provable_query(datasource, dynargs, gaslimit);
676 | }
677 | function provable_query(string datasource, bytes[3] args) provableAPI internal returns (bytes32 id) {
678 | bytes[] memory dynargs = new bytes[](3);
679 | dynargs[0] = args[0];
680 | dynargs[1] = args[1];
681 | dynargs[2] = args[2];
682 | return provable_query(datasource, dynargs);
683 | }
684 | function provable_query(uint timestamp, string datasource, bytes[3] args) provableAPI internal returns (bytes32 id) {
685 | bytes[] memory dynargs = new bytes[](3);
686 | dynargs[0] = args[0];
687 | dynargs[1] = args[1];
688 | dynargs[2] = args[2];
689 | return provable_query(timestamp, datasource, dynargs);
690 | }
691 | function provable_query(uint timestamp, string datasource, bytes[3] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
692 | bytes[] memory dynargs = new bytes[](3);
693 | dynargs[0] = args[0];
694 | dynargs[1] = args[1];
695 | dynargs[2] = args[2];
696 | return provable_query(timestamp, datasource, dynargs, gaslimit);
697 | }
698 | function provable_query(string datasource, bytes[3] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
699 | bytes[] memory dynargs = new bytes[](3);
700 | dynargs[0] = args[0];
701 | dynargs[1] = args[1];
702 | dynargs[2] = args[2];
703 | return provable_query(datasource, dynargs, gaslimit);
704 | }
705 |
706 | function provable_query(string datasource, bytes[4] args) provableAPI internal returns (bytes32 id) {
707 | bytes[] memory dynargs = new bytes[](4);
708 | dynargs[0] = args[0];
709 | dynargs[1] = args[1];
710 | dynargs[2] = args[2];
711 | dynargs[3] = args[3];
712 | return provable_query(datasource, dynargs);
713 | }
714 | function provable_query(uint timestamp, string datasource, bytes[4] args) provableAPI internal returns (bytes32 id) {
715 | bytes[] memory dynargs = new bytes[](4);
716 | dynargs[0] = args[0];
717 | dynargs[1] = args[1];
718 | dynargs[2] = args[2];
719 | dynargs[3] = args[3];
720 | return provable_query(timestamp, datasource, dynargs);
721 | }
722 | function provable_query(uint timestamp, string datasource, bytes[4] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
723 | bytes[] memory dynargs = new bytes[](4);
724 | dynargs[0] = args[0];
725 | dynargs[1] = args[1];
726 | dynargs[2] = args[2];
727 | dynargs[3] = args[3];
728 | return provable_query(timestamp, datasource, dynargs, gaslimit);
729 | }
730 | function provable_query(string datasource, bytes[4] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
731 | bytes[] memory dynargs = new bytes[](4);
732 | dynargs[0] = args[0];
733 | dynargs[1] = args[1];
734 | dynargs[2] = args[2];
735 | dynargs[3] = args[3];
736 | return provable_query(datasource, dynargs, gaslimit);
737 | }
738 | function provable_query(string datasource, bytes[5] args) provableAPI internal returns (bytes32 id) {
739 | bytes[] memory dynargs = new bytes[](5);
740 | dynargs[0] = args[0];
741 | dynargs[1] = args[1];
742 | dynargs[2] = args[2];
743 | dynargs[3] = args[3];
744 | dynargs[4] = args[4];
745 | return provable_query(datasource, dynargs);
746 | }
747 | function provable_query(uint timestamp, string datasource, bytes[5] args) provableAPI internal returns (bytes32 id) {
748 | bytes[] memory dynargs = new bytes[](5);
749 | dynargs[0] = args[0];
750 | dynargs[1] = args[1];
751 | dynargs[2] = args[2];
752 | dynargs[3] = args[3];
753 | dynargs[4] = args[4];
754 | return provable_query(timestamp, datasource, dynargs);
755 | }
756 | function provable_query(uint timestamp, string datasource, bytes[5] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
757 | bytes[] memory dynargs = new bytes[](5);
758 | dynargs[0] = args[0];
759 | dynargs[1] = args[1];
760 | dynargs[2] = args[2];
761 | dynargs[3] = args[3];
762 | dynargs[4] = args[4];
763 | return provable_query(timestamp, datasource, dynargs, gaslimit);
764 | }
765 | function provable_query(string datasource, bytes[5] args, uint gaslimit) provableAPI internal returns (bytes32 id) {
766 | bytes[] memory dynargs = new bytes[](5);
767 | dynargs[0] = args[0];
768 | dynargs[1] = args[1];
769 | dynargs[2] = args[2];
770 | dynargs[3] = args[3];
771 | dynargs[4] = args[4];
772 | return provable_query(datasource, dynargs, gaslimit);
773 | }
774 |
775 | function provable_cbAddress() provableAPI internal returns (address){
776 | return provable.cbAddress();
777 | }
778 | function provable_setProof(byte proofP) provableAPI internal {
779 | return provable.setProofType(proofP);
780 | }
781 | function provable_setCustomGasPrice(uint gasPrice) provableAPI internal {
782 | return provable.setCustomGasPrice(gasPrice);
783 | }
784 |
785 | function provable_randomDS_getSessionPubKeyHash() provableAPI internal returns (bytes32){
786 | return provable.randomDS_getSessionPubKeyHash();
787 | }
788 |
789 | function getCodeSize(address _addr) view internal returns(uint _size) {
790 | assembly {
791 | _size := extcodesize(_addr)
792 | }
793 | }
794 |
795 | function parseAddr(string _a) internal pure returns (address){
796 | bytes memory tmp = bytes(_a);
797 | uint160 iaddr = 0;
798 | uint160 b1;
799 | uint160 b2;
800 | for (uint i=2; i<2+2*20; i+=2){
801 | iaddr *= 256;
802 | b1 = uint160(tmp[i]);
803 | b2 = uint160(tmp[i+1]);
804 | if ((b1 >= 97)&&(b1 <= 102)) b1 -= 87;
805 | else if ((b1 >= 65)&&(b1 <= 70)) b1 -= 55;
806 | else if ((b1 >= 48)&&(b1 <= 57)) b1 -= 48;
807 | if ((b2 >= 97)&&(b2 <= 102)) b2 -= 87;
808 | else if ((b2 >= 65)&&(b2 <= 70)) b2 -= 55;
809 | else if ((b2 >= 48)&&(b2 <= 57)) b2 -= 48;
810 | iaddr += (b1*16+b2);
811 | }
812 | return address(iaddr);
813 | }
814 |
815 | function strCompare(string _a, string _b) internal pure returns (int) {
816 | bytes memory a = bytes(_a);
817 | bytes memory b = bytes(_b);
818 | uint minLength = a.length;
819 | if (b.length < minLength) minLength = b.length;
820 | for (uint i = 0; i < minLength; i ++)
821 | if (a[i] < b[i])
822 | return -1;
823 | else if (a[i] > b[i])
824 | return 1;
825 | if (a.length < b.length)
826 | return -1;
827 | else if (a.length > b.length)
828 | return 1;
829 | else
830 | return 0;
831 | }
832 |
833 | function indexOf(string _haystack, string _needle) internal pure returns (int) {
834 | bytes memory h = bytes(_haystack);
835 | bytes memory n = bytes(_needle);
836 | if(h.length < 1 || n.length < 1 || (n.length > h.length))
837 | return -1;
838 | else if(h.length > (2**128 -1))
839 | return -1;
840 | else
841 | {
842 | uint subindex = 0;
843 | for (uint i = 0; i < h.length; i ++)
844 | {
845 | if (h[i] == n[0])
846 | {
847 | subindex = 1;
848 | while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex])
849 | {
850 | subindex++;
851 | }
852 | if(subindex == n.length)
853 | return int(i);
854 | }
855 | }
856 | return -1;
857 | }
858 | }
859 |
860 | function strConcat(string _a, string _b, string _c, string _d, string _e) internal pure returns (string) {
861 | bytes memory _ba = bytes(_a);
862 | bytes memory _bb = bytes(_b);
863 | bytes memory _bc = bytes(_c);
864 | bytes memory _bd = bytes(_d);
865 | bytes memory _be = bytes(_e);
866 | string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
867 | bytes memory babcde = bytes(abcde);
868 | uint k = 0;
869 | for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
870 | for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
871 | for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
872 | for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
873 | for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
874 | return string(babcde);
875 | }
876 |
877 | function strConcat(string _a, string _b, string _c, string _d) internal pure returns (string) {
878 | return strConcat(_a, _b, _c, _d, "");
879 | }
880 |
881 | function strConcat(string _a, string _b, string _c) internal pure returns (string) {
882 | return strConcat(_a, _b, _c, "", "");
883 | }
884 |
885 | function strConcat(string _a, string _b) internal pure returns (string) {
886 | return strConcat(_a, _b, "", "", "");
887 | }
888 |
889 | // parseInt
890 | function parseInt(string _a) internal pure returns (uint) {
891 | return parseInt(_a, 0);
892 | }
893 |
894 | // parseInt(parseFloat*10^_b)
895 | function parseInt(string _a, uint _b) internal pure returns (uint) {
896 | bytes memory bresult = bytes(_a);
897 | uint mint = 0;
898 | bool decimals = false;
899 | for (uint i=0; i= 48)&&(bresult[i] <= 57)){
901 | if (decimals){
902 | if (_b == 0) break;
903 | else _b--;
904 | }
905 | mint *= 10;
906 | mint += uint(bresult[i]) - 48;
907 | } else if (bresult[i] == 46) decimals = true;
908 | }
909 | if (_b > 0) mint *= 10**_b;
910 | return mint;
911 | }
912 |
913 | function uint2str(uint i) internal pure returns (string){
914 | if (i == 0) return "0";
915 | uint j = i;
916 | uint len;
917 | while (j != 0){
918 | len++;
919 | j /= 10;
920 | }
921 | bytes memory bstr = new bytes(len);
922 | uint k = len - 1;
923 | while (i != 0){
924 | bstr[k--] = byte(48 + i % 10);
925 | i /= 10;
926 | }
927 | return string(bstr);
928 | }
929 |
930 | using CBOR for Buffer.buffer;
931 | function stra2cbor(string[] arr) internal pure returns (bytes) {
932 | safeMemoryCleaner();
933 | Buffer.buffer memory buf;
934 | Buffer.init(buf, 1024);
935 | buf.startArray();
936 | for (uint i = 0; i < arr.length; i++) {
937 | buf.encodeString(arr[i]);
938 | }
939 | buf.endSequence();
940 | return buf.buf;
941 | }
942 |
943 | function ba2cbor(bytes[] arr) internal pure returns (bytes) {
944 | safeMemoryCleaner();
945 | Buffer.buffer memory buf;
946 | Buffer.init(buf, 1024);
947 | buf.startArray();
948 | for (uint i = 0; i < arr.length; i++) {
949 | buf.encodeBytes(arr[i]);
950 | }
951 | buf.endSequence();
952 | return buf.buf;
953 | }
954 |
955 | string provable_network_name;
956 | function provable_setNetworkName(string _network_name) internal {
957 | provable_network_name = _network_name;
958 | }
959 |
960 | function provable_getNetworkName() internal view returns (string) {
961 | return provable_network_name;
962 | }
963 |
964 | function provable_newRandomDSQuery(uint _delay, uint _nbytes, uint _customGasLimit) internal returns (bytes32){
965 | require((_nbytes > 0) && (_nbytes <= 32));
966 | // Convert from seconds to ledger timer ticks
967 | _delay *= 10;
968 | bytes memory nbytes = new bytes(1);
969 | nbytes[0] = byte(_nbytes);
970 | bytes memory unonce = new bytes(32);
971 | bytes memory sessionKeyHash = new bytes(32);
972 | bytes32 sessionKeyHash_bytes32 = provable_randomDS_getSessionPubKeyHash();
973 | assembly {
974 | mstore(unonce, 0x20)
975 | // the following variables can be relaxed
976 | // check relaxed random contract under ethereum-examples repo
977 | // for an idea on how to override and replace comit hash vars
978 | mstore(add(unonce, 0x20), xor(blockhash(sub(number, 1)), xor(coinbase, timestamp)))
979 | mstore(sessionKeyHash, 0x20)
980 | mstore(add(sessionKeyHash, 0x20), sessionKeyHash_bytes32)
981 | }
982 | bytes memory delay = new bytes(32);
983 | assembly {
984 | mstore(add(delay, 0x20), _delay)
985 | }
986 |
987 | bytes memory delay_bytes8 = new bytes(8);
988 | copyBytes(delay, 24, 8, delay_bytes8, 0);
989 |
990 | bytes[4] memory args = [unonce, nbytes, sessionKeyHash, delay];
991 | bytes32 queryId = provable_query("random", args, _customGasLimit);
992 |
993 | bytes memory delay_bytes8_left = new bytes(8);
994 |
995 | assembly {
996 | let x := mload(add(delay_bytes8, 0x20))
997 | mstore8(add(delay_bytes8_left, 0x27), div(x, 0x100000000000000000000000000000000000000000000000000000000000000))
998 | mstore8(add(delay_bytes8_left, 0x26), div(x, 0x1000000000000000000000000000000000000000000000000000000000000))
999 | mstore8(add(delay_bytes8_left, 0x25), div(x, 0x10000000000000000000000000000000000000000000000000000000000))
1000 | mstore8(add(delay_bytes8_left, 0x24), div(x, 0x100000000000000000000000000000000000000000000000000000000))
1001 | mstore8(add(delay_bytes8_left, 0x23), div(x, 0x1000000000000000000000000000000000000000000000000000000))
1002 | mstore8(add(delay_bytes8_left, 0x22), div(x, 0x10000000000000000000000000000000000000000000000000000))
1003 | mstore8(add(delay_bytes8_left, 0x21), div(x, 0x100000000000000000000000000000000000000000000000000))
1004 | mstore8(add(delay_bytes8_left, 0x20), div(x, 0x1000000000000000000000000000000000000000000000000))
1005 |
1006 | }
1007 |
1008 | provable_randomDS_setCommitment(queryId, keccak256(abi.encodePacked(delay_bytes8_left, args[1], sha256(args[0]), args[2])));
1009 | return queryId;
1010 | }
1011 |
1012 | function provable_randomDS_setCommitment(bytes32 queryId, bytes32 commitment) internal {
1013 | provable_randomDS_args[queryId] = commitment;
1014 | }
1015 |
1016 | mapping(bytes32=>bytes32) provable_randomDS_args;
1017 | mapping(bytes32=>bool) provable_randomDS_sessionKeysHashVerified;
1018 |
1019 | function verifySig(bytes32 tosignh, bytes dersig, bytes pubkey) internal returns (bool){
1020 | bool sigok;
1021 | address signer;
1022 |
1023 | bytes32 sigr;
1024 | bytes32 sigs;
1025 |
1026 | bytes memory sigr_ = new bytes(32);
1027 | uint offset = 4+(uint(dersig[3]) - 0x20);
1028 | sigr_ = copyBytes(dersig, offset, 32, sigr_, 0);
1029 | bytes memory sigs_ = new bytes(32);
1030 | offset += 32 + 2;
1031 | sigs_ = copyBytes(dersig, offset+(uint(dersig[offset-1]) - 0x20), 32, sigs_, 0);
1032 |
1033 | assembly {
1034 | sigr := mload(add(sigr_, 32))
1035 | sigs := mload(add(sigs_, 32))
1036 | }
1037 |
1038 |
1039 | (sigok, signer) = safer_ecrecover(tosignh, 27, sigr, sigs);
1040 | if (address(keccak256(pubkey)) == signer) return true;
1041 | else {
1042 | (sigok, signer) = safer_ecrecover(tosignh, 28, sigr, sigs);
1043 | return (address(keccak256(pubkey)) == signer);
1044 | }
1045 | }
1046 |
1047 | function provable_randomDS_proofVerify__sessionKeyValidity(bytes proof, uint sig2offset) internal returns (bool) {
1048 | bool sigok;
1049 |
1050 | // Step 6: verify the attestation signature, APPKEY1 must sign the sessionKey from the correct ledger app (CODEHASH)
1051 | bytes memory sig2 = new bytes(uint(proof[sig2offset+1])+2);
1052 | copyBytes(proof, sig2offset, sig2.length, sig2, 0);
1053 |
1054 | bytes memory appkey1_pubkey = new bytes(64);
1055 | copyBytes(proof, 3+1, 64, appkey1_pubkey, 0);
1056 |
1057 | bytes memory tosign2 = new bytes(1+65+32);
1058 | tosign2[0] = byte(1); //role
1059 | copyBytes(proof, sig2offset-65, 65, tosign2, 1);
1060 | bytes memory CODEHASH = hex"fd94fa71bc0ba10d39d464d0d8f465efeef0a2764e3887fcc9df41ded20f505c";
1061 | copyBytes(CODEHASH, 0, 32, tosign2, 1+65);
1062 | sigok = verifySig(sha256(tosign2), sig2, appkey1_pubkey);
1063 |
1064 | if (sigok == false) return false;
1065 |
1066 |
1067 | // Step 7: verify the APPKEY1 provenance (must be signed by Ledger)
1068 | bytes memory LEDGERKEY = hex"7fb956469c5c9b89840d55b43537e66a98dd4811ea0a27224272c2e5622911e8537a2f8e86a46baec82864e98dd01e9ccc2f8bc5dfc9cbe5a91a290498dd96e4";
1069 |
1070 | bytes memory tosign3 = new bytes(1+65);
1071 | tosign3[0] = 0xFE;
1072 | copyBytes(proof, 3, 65, tosign3, 1);
1073 |
1074 | bytes memory sig3 = new bytes(uint(proof[3+65+1])+2);
1075 | copyBytes(proof, 3+65, sig3.length, sig3, 0);
1076 |
1077 | sigok = verifySig(sha256(tosign3), sig3, LEDGERKEY);
1078 |
1079 | return sigok;
1080 | }
1081 |
1082 | modifier provable_randomDS_proofVerify(bytes32 _queryId, string _result, bytes _proof) {
1083 | // Step 1: the prefix has to match 'LP\x01' (Ledger Proof version 1)
1084 | require((_proof[0] == "L") && (_proof[1] == "P") && (_proof[2] == 1));
1085 |
1086 | bool proofVerified = provable_randomDS_proofVerify__main(_proof, _queryId, bytes(_result), provable_getNetworkName());
1087 | require(proofVerified);
1088 |
1089 | _;
1090 | }
1091 |
1092 | function provable_randomDS_proofVerify__returnCode(bytes32 _queryId, string _result, bytes _proof) internal returns (uint8){
1093 | // Step 1: the prefix has to match 'LP\x01' (Ledger Proof version 1)
1094 | if ((_proof[0] != "L")||(_proof[1] != "P")||(_proof[2] != 1)) return 1;
1095 |
1096 | bool proofVerified = provable_randomDS_proofVerify__main(_proof, _queryId, bytes(_result), provable_getNetworkName());
1097 | if (proofVerified == false) return 2;
1098 |
1099 | return 0;
1100 | }
1101 |
1102 | function matchBytes32Prefix(bytes32 content, bytes prefix, uint n_random_bytes) internal pure returns (bool){
1103 | bool match_ = true;
1104 |
1105 | require(prefix.length == n_random_bytes);
1106 |
1107 | for (uint256 i=0; i< n_random_bytes; i++) {
1108 | if (content[i] != prefix[i]) match_ = false;
1109 | }
1110 |
1111 | return match_;
1112 | }
1113 |
1114 | function provable_randomDS_proofVerify__main(bytes proof, bytes32 queryId, bytes result, string context_name) internal returns (bool){
1115 |
1116 | // Step 2: the unique keyhash has to match with the sha256 of (context name + queryId)
1117 | uint ledgerProofLength = 3+65+(uint(proof[3+65+1])+2)+32;
1118 | bytes memory keyhash = new bytes(32);
1119 | copyBytes(proof, ledgerProofLength, 32, keyhash, 0);
1120 | if (!(keccak256(keyhash) == keccak256(abi.encodePacked(sha256(abi.encodePacked(context_name, queryId)))))) return false;
1121 |
1122 | bytes memory sig1 = new bytes(uint(proof[ledgerProofLength+(32+8+1+32)+1])+2);
1123 | copyBytes(proof, ledgerProofLength+(32+8+1+32), sig1.length, sig1, 0);
1124 |
1125 | // Step 3: we assume sig1 is valid (it will be verified during step 5) and we verify if 'result' is the prefix of sha256(sig1)
1126 | if (!matchBytes32Prefix(sha256(sig1), result, uint(proof[ledgerProofLength+32+8]))) return false;
1127 |
1128 | // Step 4: commitment match verification, keccak256(delay, nbytes, unonce, sessionKeyHash) == commitment in storage.
1129 | // This is to verify that the computed args match with the ones specified in the query.
1130 | bytes memory commitmentSlice1 = new bytes(8+1+32);
1131 | copyBytes(proof, ledgerProofLength+32, 8+1+32, commitmentSlice1, 0);
1132 |
1133 | bytes memory sessionPubkey = new bytes(64);
1134 | uint sig2offset = ledgerProofLength+32+(8+1+32)+sig1.length+65;
1135 | copyBytes(proof, sig2offset-64, 64, sessionPubkey, 0);
1136 |
1137 | bytes32 sessionPubkeyHash = sha256(sessionPubkey);
1138 | if (provable_randomDS_args[queryId] == keccak256(abi.encodePacked(commitmentSlice1, sessionPubkeyHash))){ //unonce, nbytes and sessionKeyHash match
1139 | delete provable_randomDS_args[queryId];
1140 | } else return false;
1141 |
1142 |
1143 | // Step 5: validity verification for sig1 (keyhash and args signed with the sessionKey)
1144 | bytes memory tosign1 = new bytes(32+8+1+32);
1145 | copyBytes(proof, ledgerProofLength, 32+8+1+32, tosign1, 0);
1146 | if (!verifySig(sha256(tosign1), sig1, sessionPubkey)) return false;
1147 |
1148 | // verify if sessionPubkeyHash was verified already, if not.. let's do it!
1149 | if (provable_randomDS_sessionKeysHashVerified[sessionPubkeyHash] == false){
1150 | provable_randomDS_sessionKeysHashVerified[sessionPubkeyHash] = provable_randomDS_proofVerify__sessionKeyValidity(proof, sig2offset);
1151 | }
1152 |
1153 | return provable_randomDS_sessionKeysHashVerified[sessionPubkeyHash];
1154 | }
1155 |
1156 | // the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
1157 | function copyBytes(bytes from, uint fromOffset, uint length, bytes to, uint toOffset) internal pure returns (bytes) {
1158 | uint minLength = length + toOffset;
1159 |
1160 | // Buffer too small
1161 | require(to.length >= minLength); // Should be a better way?
1162 |
1163 | // NOTE: the offset 32 is added to skip the `size` field of both bytes variables
1164 | uint i = 32 + fromOffset;
1165 | uint j = 32 + toOffset;
1166 |
1167 | while (i < (32 + fromOffset + length)) {
1168 | assembly {
1169 | let tmp := mload(add(from, i))
1170 | mstore(add(to, j), tmp)
1171 | }
1172 | i += 32;
1173 | j += 32;
1174 | }
1175 |
1176 | return to;
1177 | }
1178 |
1179 | // the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
1180 | // Duplicate Solidity's ecrecover, but catching the CALL return value
1181 | function safer_ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal returns (bool, address) {
1182 | // We do our own memory management here. Solidity uses memory offset
1183 | // 0x40 to store the current end of memory. We write past it (as
1184 | // writes are memory extensions), but don't update the offset so
1185 | // Solidity will reuse it. The memory used here is only needed for
1186 | // this context.
1187 |
1188 | // FIXME: inline assembly can't access return values
1189 | bool ret;
1190 | address addr;
1191 |
1192 | assembly {
1193 | let size := mload(0x40)
1194 | mstore(size, hash)
1195 | mstore(add(size, 32), v)
1196 | mstore(add(size, 64), r)
1197 | mstore(add(size, 96), s)
1198 |
1199 | // NOTE: we can reuse the request memory because we deal with
1200 | // the return code
1201 | ret := call(3000, 1, 0, size, 128, size, 32)
1202 | addr := mload(size)
1203 | }
1204 |
1205 | return (ret, addr);
1206 | }
1207 |
1208 | // the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
1209 | function ecrecovery(bytes32 hash, bytes sig) internal returns (bool, address) {
1210 | bytes32 r;
1211 | bytes32 s;
1212 | uint8 v;
1213 |
1214 | if (sig.length != 65)
1215 | return (false, 0);
1216 |
1217 | // The signature format is a compact form of:
1218 | // {bytes32 r}{bytes32 s}{uint8 v}
1219 | // Compact means, uint8 is not padded to 32 bytes.
1220 | assembly {
1221 | r := mload(add(sig, 32))
1222 | s := mload(add(sig, 64))
1223 |
1224 | // Here we are loading the last 32 bytes. We exploit the fact that
1225 | // 'mload' will pad with zeroes if we overread.
1226 | // There is no 'mload8' to do this, but that would be nicer.
1227 | v := byte(0, mload(add(sig, 96)))
1228 |
1229 | // Alternative solution:
1230 | // 'byte' is not working due to the Solidity parser, so lets
1231 | // use the second best option, 'and'
1232 | // v := and(mload(add(sig, 65)), 255)
1233 | }
1234 |
1235 | // albeit non-transactional signatures are not specified by the YP, one would expect it
1236 | // to match the YP range of [27, 28]
1237 | //
1238 | // geth uses [0, 1] and some clients have followed. This might change, see:
1239 | // https://github.com/ethereum/go-ethereum/issues/2053
1240 | if (v < 27)
1241 | v += 27;
1242 |
1243 | if (v != 27 && v != 28)
1244 | return (false, 0);
1245 |
1246 | return safer_ecrecover(hash, v, r, s);
1247 | }
1248 |
1249 | function safeMemoryCleaner() internal pure {
1250 | assembly {
1251 | let fmem := mload(0x40)
1252 | codecopy(fmem, codesize, sub(msize, fmem))
1253 | }
1254 | }
1255 |
1256 | }
1257 | //
1258 |
--------------------------------------------------------------------------------
/contracts/solc-v0.8.x/interfaces/provable-address-resolver-interface.sol:
--------------------------------------------------------------------------------
1 | // SPDX-License-Identifier: MIT
2 | pragma solidity >= 0.8.0 < 0.9.0;
3 |
4 | interface OracleAddrResolverI {
5 | function getAddress() external returns (address _address);
6 | }
7 |
--------------------------------------------------------------------------------
/contracts/solc-v0.8.x/interfaces/provable-interface.sol:
--------------------------------------------------------------------------------
1 | // SPDX-License-Identifier: MIT
2 | pragma solidity >= 0.8.0 < 0.9.0;
3 |
4 | interface ProvableI {
5 | function setProofType(bytes1 _proofType) external;
6 | function setCustomGasPrice(uint _gasPrice) external;
7 | function cbAddress() external returns (address _cbAddress);
8 | function randomDS_getSessionPubKeyHash() external view returns (bytes32 _sessionKeyHash);
9 |
10 | function getPrice(
11 | string calldata _datasource
12 | ) external returns (uint _dsprice);
13 |
14 | function getPrice(
15 | string calldata _datasource,
16 | uint _gasLimit
17 | ) external returns (uint _dsprice);
18 |
19 | function queryN(
20 | uint _timestamp,
21 | string calldata _datasource,
22 | bytes calldata _argN
23 | ) external payable returns (bytes32 _id);
24 |
25 | function query(
26 | uint _timestamp,
27 | string calldata _datasource,
28 | string calldata _arg
29 | ) external payable returns (bytes32 _id);
30 |
31 | function query2(
32 | uint _timestamp,
33 | string calldata _datasource,
34 | string calldata _arg1,
35 | string calldata _arg2
36 | ) external payable returns (bytes32 _id);
37 |
38 | function query_withGasLimit(
39 | uint _timestamp,
40 | string calldata _datasource,
41 | string calldata _arg,
42 | uint _gasLimit
43 | ) external payable returns (bytes32 _id);
44 |
45 | function queryN_withGasLimit(
46 | uint _timestamp,
47 | string calldata _datasource,
48 | bytes calldata _argN,
49 | uint _gasLimit
50 | ) external payable returns (bytes32 _id);
51 |
52 | function query2_withGasLimit(
53 | uint _timestamp,
54 | string calldata _datasource,
55 | string calldata _arg1,
56 | string calldata _arg2,
57 | uint _gasLimit
58 | ) external payable returns (bytes32 _id);
59 | }
60 |
--------------------------------------------------------------------------------
/contracts/solc-v0.8.x/libraries/BUFFER.sol:
--------------------------------------------------------------------------------
1 | // SPDX-License-Identifier: MIT
2 | pragma solidity >= 0.8.0 < 0.9.0;
3 |
4 | library Buffer {
5 |
6 | struct buffer {
7 | bytes buf;
8 | uint capacity;
9 | }
10 |
11 | function init(buffer memory _buf, uint _capacity) internal pure {
12 | uint capacity = _capacity;
13 | if (capacity % 32 != 0) {
14 | capacity += 32 - (capacity % 32);
15 | }
16 | _buf.capacity = capacity; // Allocate space for the buffer data
17 | assembly {
18 | let ptr := mload(0x40)
19 | mstore(_buf, ptr)
20 | mstore(ptr, 0)
21 | mstore(0x40, add(ptr, capacity))
22 | }
23 | }
24 |
25 | function resize(buffer memory _buf, uint _capacity) private pure {
26 | bytes memory oldbuf = _buf.buf;
27 | init(_buf, _capacity);
28 | append(_buf, oldbuf);
29 | }
30 |
31 | function max(uint _a, uint _b) private pure returns (uint _max) {
32 | if (_a > _b) {
33 | return _a;
34 | }
35 | return _b;
36 | }
37 | /**
38 | * @dev Appends a byte array to the end of the buffer. Resizes if doing so
39 | * would exceed the capacity of the buffer.
40 | * @param _buf The buffer to append to.
41 | * @param _data The data to append.
42 | * @return _buffer The original buffer.
43 | *
44 | */
45 | function append(buffer memory _buf, bytes memory _data) internal pure returns (buffer memory _buffer) {
46 | if (_data.length + _buf.buf.length > _buf.capacity) {
47 | resize(_buf, max(_buf.capacity, _data.length) * 2);
48 | }
49 | uint dest;
50 | uint src;
51 | uint len = _data.length;
52 | assembly {
53 | let bufptr := mload(_buf) // Memory address of the buffer data
54 | let buflen := mload(bufptr) // Length of existing buffer data
55 | dest := add(add(bufptr, buflen), 32) // Start address = buffer address + buffer length + sizeof(buffer length)
56 | mstore(bufptr, add(buflen, mload(_data))) // Update buffer length
57 | src := add(_data, 32)
58 | }
59 | for(; len >= 32; len -= 32) { // Copy word-length chunks while possible
60 | assembly {
61 | mstore(dest, mload(src))
62 | }
63 | dest += 32;
64 | src += 32;
65 | }
66 | uint mask = 256 ** (32 - len) - 1; // Copy remaining bytes
67 | assembly {
68 | let srcpart := and(mload(src), not(mask))
69 | let destpart := and(mload(dest), mask)
70 | mstore(dest, or(destpart, srcpart))
71 | }
72 | return _buf;
73 | }
74 | /**
75 | *
76 | * @dev Appends a byte to the end of the buffer. Resizes if doing so would
77 | * exceed the capacity of the buffer.
78 | * @param _buf The buffer to append to.
79 | * @param _data The data to append.
80 | *
81 | */
82 | function append(buffer memory _buf, uint8 _data) internal pure {
83 | if (_buf.buf.length + 1 > _buf.capacity) {
84 | resize(_buf, _buf.capacity * 2);
85 | }
86 | assembly {
87 | let bufptr := mload(_buf) // Memory address of the buffer data
88 | let buflen := mload(bufptr) // Length of existing buffer data
89 | let dest := add(add(bufptr, buflen), 32) // Address = buffer address + buffer length + sizeof(buffer length)
90 | mstore8(dest, _data)
91 | mstore(bufptr, add(buflen, 1)) // Update buffer length
92 | }
93 | }
94 | /**
95 | *
96 | * @dev Appends a byte to the end of the buffer. Resizes if doing so would
97 | * exceed the capacity of the buffer.
98 | * @param _buf The buffer to append to.
99 | * @param _data The data to append.
100 | * @return _buffer The original buffer.
101 | *
102 | */
103 | function appendInt(buffer memory _buf, uint _data, uint _len) internal pure returns (buffer memory _buffer) {
104 | if (_len + _buf.buf.length > _buf.capacity) {
105 | resize(_buf, max(_buf.capacity, _len) * 2);
106 | }
107 | uint mask = 256 ** _len - 1;
108 | assembly {
109 | let bufptr := mload(_buf) // Memory address of the buffer data
110 | let buflen := mload(bufptr) // Length of existing buffer data
111 | let dest := add(add(bufptr, buflen), _len) // Address = buffer address + buffer length + sizeof(buffer length) + len
112 | mstore(dest, or(and(mload(dest), not(mask)), _data))
113 | mstore(bufptr, add(buflen, _len)) // Update buffer length
114 | }
115 | return _buf;
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/contracts/solc-v0.8.x/libraries/CBOR.sol:
--------------------------------------------------------------------------------
1 | // SPDX-License-Identifier: MIT
2 | pragma solidity >= 0.8.0 < 0.9.0;
3 |
4 | import "./BUFFER.sol";
5 |
6 | library CBOR {
7 | using Buffer for Buffer.buffer;
8 |
9 | uint8 private constant MAJOR_TYPE_INT = 0;
10 | uint8 private constant MAJOR_TYPE_MAP = 5;
11 | uint8 private constant MAJOR_TYPE_BYTES = 2;
12 | uint8 private constant MAJOR_TYPE_ARRAY = 4;
13 | uint8 private constant MAJOR_TYPE_STRING = 3;
14 | uint8 private constant MAJOR_TYPE_NEGATIVE_INT = 1;
15 | uint8 private constant MAJOR_TYPE_CONTENT_FREE = 7;
16 |
17 | function encodeType(Buffer.buffer memory _buf, uint8 _major, uint _value) private pure {
18 | if (_value <= 23) {
19 | _buf.append(uint8((_major << 5) | _value));
20 | } else if (_value <= 0xFF) {
21 | _buf.append(uint8((_major << 5) | 24));
22 | _buf.appendInt(_value, 1);
23 | } else if (_value <= 0xFFFF) {
24 | _buf.append(uint8((_major << 5) | 25));
25 | _buf.appendInt(_value, 2);
26 | } else if (_value <= 0xFFFFFFFF) {
27 | _buf.append(uint8((_major << 5) | 26));
28 | _buf.appendInt(_value, 4);
29 | } else if (_value <= 0xFFFFFFFFFFFFFFFF) {
30 | _buf.append(uint8((_major << 5) | 27));
31 | _buf.appendInt(_value, 8);
32 | }
33 | }
34 |
35 | function encodeIndefiniteLengthType(Buffer.buffer memory _buf, uint8 _major) private pure {
36 | _buf.append(uint8((_major << 5) | 31));
37 | }
38 |
39 | function encodeUInt(Buffer.buffer memory _buf, uint _value) internal pure {
40 | encodeType(_buf, MAJOR_TYPE_INT, _value);
41 | }
42 |
43 | function encodeInt(Buffer.buffer memory _buf, int _value) internal pure {
44 | if (_value >= 0) {
45 | encodeType(_buf, MAJOR_TYPE_INT, uint(_value));
46 | } else {
47 | encodeType(_buf, MAJOR_TYPE_NEGATIVE_INT, uint(-1 - _value));
48 | }
49 | }
50 |
51 | function encodeBytes(Buffer.buffer memory _buf, bytes memory _value) internal pure {
52 | encodeType(_buf, MAJOR_TYPE_BYTES, _value.length);
53 | _buf.append(_value);
54 | }
55 |
56 | function encodeString(Buffer.buffer memory _buf, string memory _value) internal pure {
57 | encodeType(_buf, MAJOR_TYPE_STRING, bytes(_value).length);
58 | _buf.append(bytes(_value));
59 | }
60 |
61 | function startArray(Buffer.buffer memory _buf) internal pure {
62 | encodeIndefiniteLengthType(_buf, MAJOR_TYPE_ARRAY);
63 | }
64 |
65 | function startMap(Buffer.buffer memory _buf) internal pure {
66 | encodeIndefiniteLengthType(_buf, MAJOR_TYPE_MAP);
67 | }
68 |
69 | function endSequence(Buffer.buffer memory _buf) internal pure {
70 | encodeIndefiniteLengthType(_buf, MAJOR_TYPE_CONTENT_FREE);
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/contracts/solc-v0.8.x/provableAPI.sol:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2015-2016 Oraclize SRL
3 | Copyright (c) 2016-2019 Oraclize LTD
4 | Copyright (c) 2019-2022 Provable Things Limited
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 | The above copyright notice and this permission notice shall be included in
12 | all copies or substantial portions of the Software.
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 | */
21 |
22 | // SPDX-License-Identifier: MIT
23 | pragma solidity >= 0.8.0 < 0.9.0;
24 |
25 | import "./libraries/CBOR.sol";
26 | import "./interfaces/provable-interface.sol";
27 | import "./interfaces/provable-address-resolver-interface.sol";
28 |
29 | // Dummy contract only used to emit to end-user they are using wrong solc
30 | abstract contract solcChecker {
31 | /* INCOMPATIBLE SOLC: import the following instead: "github.com/oraclize/ethereum-api/oraclizeAPI_0.4.sol" */ function f(bytes calldata x) virtual external;
32 | }
33 |
34 | contract usingProvable {
35 |
36 | using CBOR for Buffer.buffer;
37 |
38 | ProvableI provable;
39 | OracleAddrResolverI OAR;
40 |
41 | uint constant day = 60 * 60 * 24;
42 | uint constant week = 60 * 60 * 24 * 7;
43 | uint constant month = 60 * 60 * 24 * 30;
44 |
45 | bytes1 constant proofType_NONE = 0x00;
46 | bytes1 constant proofType_Ledger = 0x30;
47 | bytes1 constant proofType_Native = 0xF0;
48 | bytes1 constant proofStorage_IPFS = 0x01;
49 | bytes1 constant proofType_Android = 0x40;
50 | bytes1 constant proofType_TLSNotary = 0x10;
51 |
52 | string provable_network_name;
53 | uint8 constant networkID_auto = 0;
54 | uint8 constant networkID_morden = 2;
55 | uint8 constant networkID_mainnet = 1;
56 | uint8 constant networkID_testnet = 2;
57 | uint8 constant networkID_consensys = 161;
58 |
59 | mapping(bytes32 => bytes32) provable_randomDS_args;
60 | mapping(bytes32 => bool) provable_randomDS_sessionKeysHashVerified;
61 |
62 | modifier provableAPI {
63 | if ((address(OAR) == address(0)) || (getCodeSize(address(OAR)) == 0)) {
64 | provable_setNetwork(networkID_auto);
65 | }
66 | if (address(provable) != OAR.getAddress()) {
67 | provable = ProvableI(OAR.getAddress());
68 | }
69 | _;
70 | }
71 |
72 | modifier provable_randomDS_proofVerify(bytes32 _queryId, string memory _result, bytes memory _proof) {
73 | // RandomDS Proof Step 1: The prefix has to match 'LP\x01' (Ledger Proof version 1)
74 | require((_proof[0] == "L") && (_proof[1] == "P") && (uint8(_proof[2]) == uint8(1)));
75 | bool proofVerified = provable_randomDS_proofVerify__main(_proof, _queryId, bytes(_result), provable_getNetworkName());
76 | require(proofVerified);
77 | _;
78 | }
79 |
80 | function provable_setNetwork(uint8 _networkID) internal returns (bool _networkSet) {
81 | _networkID; // NOTE: Silence the warning and remain backwards compatible
82 | return provable_setNetwork();
83 | }
84 |
85 | function provable_setNetworkName(string memory _network_name) internal {
86 | provable_network_name = _network_name;
87 | }
88 |
89 | function provable_getNetworkName() internal view returns (string memory _networkName) {
90 | return provable_network_name;
91 | }
92 |
93 | function provable_setNetwork() internal returns (bool _networkSet) {
94 | if (getCodeSize(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed) > 0) { //mainnet
95 | OAR = OracleAddrResolverI(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed);
96 | provable_setNetworkName("eth_mainnet");
97 | return true;
98 | }
99 | if (getCodeSize(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1) > 0) { //ropsten testnet
100 | OAR = OracleAddrResolverI(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1);
101 | provable_setNetworkName("eth_ropsten3");
102 | return true;
103 | }
104 | if (getCodeSize(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e) > 0) { //kovan testnet
105 | OAR = OracleAddrResolverI(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e);
106 | provable_setNetworkName("eth_kovan");
107 | return true;
108 | }
109 | if (getCodeSize(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48) > 0) { //rinkeby testnet
110 | OAR = OracleAddrResolverI(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48);
111 | provable_setNetworkName("eth_rinkeby");
112 | return true;
113 | }
114 | if (getCodeSize(0xa2998EFD205FB9D4B4963aFb70778D6354ad3A41) > 0) { //goerli testnet
115 | OAR = OracleAddrResolverI(0xa2998EFD205FB9D4B4963aFb70778D6354ad3A41);
116 | provable_setNetworkName("eth_goerli");
117 | return true;
118 | }
119 | if (getCodeSize(0x90A0F94702c9630036FB9846B52bf31A1C991a84) > 0){ //bsc mainnet
120 | OAR = OracleAddrResolverI(0x90A0F94702c9630036FB9846B52bf31A1C991a84);
121 | provable_setNetworkName("bsc_mainnet");
122 | return true;
123 | }
124 | if (getCodeSize(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609) > 0){ //polygon mainnet
125 | OAR = OracleAddrResolverI(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609);
126 | provable_setNetworkName("polygon_mainnet");
127 | return true;
128 | }
129 | if (getCodeSize(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85) > 0){ //sepolia mainnet
130 | OAR = OracleAddrResolverI(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85);
131 | provable_setNetworkName("sepolia_mainnet");
132 | return true;
133 | }
134 | if (getCodeSize(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475) > 0) { //ethereum-bridge
135 | OAR = OracleAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475);
136 | return true;
137 | }
138 | if (getCodeSize(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF) > 0) { //ether.camp ide
139 | OAR = OracleAddrResolverI(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF);
140 | return true;
141 | }
142 | if (getCodeSize(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA) > 0) { //browser-solidity
143 | OAR = OracleAddrResolverI(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA);
144 | return true;
145 | }
146 | return false;
147 | }
148 |
149 | function provable_getPrice(string memory _datasource) provableAPI internal returns (uint _queryPrice) {
150 | return provable.getPrice(_datasource);
151 | }
152 |
153 | function provable_getPrice(string memory _datasource, uint _gasLimit) provableAPI internal returns (uint _queryPrice) {
154 | return provable.getPrice(_datasource, _gasLimit);
155 | }
156 |
157 | function provable_query(string memory _datasource, string memory _arg) provableAPI internal returns (bytes32 _id) {
158 | uint price = provable.getPrice(_datasource);
159 | if (price > 1 ether + tx.gasprice * 200000) {
160 | return 0; // Unexpectedly high price
161 | }
162 | return provable.query{value: price}(0, _datasource, _arg);
163 | }
164 |
165 | function provable_query(uint _timestamp, string memory _datasource, string memory _arg) provableAPI internal returns (bytes32 _id) {
166 | uint price = provable.getPrice(_datasource);
167 | if (price > 1 ether + tx.gasprice * 200000) {
168 | return 0; // Unexpectedly high price
169 | }
170 | return provable.query{value: price}(_timestamp, _datasource, _arg);
171 | }
172 |
173 | function provable_query(uint _timestamp, string memory _datasource, string memory _arg, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
174 | uint price = provable.getPrice(_datasource,_gasLimit);
175 | if (price > 1 ether + tx.gasprice * _gasLimit) {
176 | return 0; // Unexpectedly high price
177 | }
178 | return provable.query_withGasLimit{value: price}(_timestamp, _datasource, _arg, _gasLimit);
179 | }
180 |
181 | function provable_query(string memory _datasource, string memory _arg, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
182 | uint price = provable.getPrice(_datasource, _gasLimit);
183 | if (price > 1 ether + tx.gasprice * _gasLimit) {
184 | return 0; // Unexpectedly high price
185 | }
186 | return provable.query_withGasLimit{value: price}(0, _datasource, _arg, _gasLimit);
187 | }
188 |
189 | function provable_query(string memory _datasource, string memory _arg1, string memory _arg2) provableAPI internal returns (bytes32 _id) {
190 | uint price = provable.getPrice(_datasource);
191 | if (price > 1 ether + tx.gasprice * 200000) {
192 | return 0; // Unexpectedly high price
193 | }
194 | return provable.query2{value: price}(0, _datasource, _arg1, _arg2);
195 | }
196 |
197 | function provable_query(uint _timestamp, string memory _datasource, string memory _arg1, string memory _arg2) provableAPI internal returns (bytes32 _id) {
198 | uint price = provable.getPrice(_datasource);
199 | if (price > 1 ether + tx.gasprice * 200000) {
200 | return 0; // Unexpectedly high price
201 | }
202 | return provable.query2{value: price}(_timestamp, _datasource, _arg1, _arg2);
203 | }
204 |
205 | function provable_query(uint _timestamp, string memory _datasource, string memory _arg1, string memory _arg2, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
206 | uint price = provable.getPrice(_datasource, _gasLimit);
207 | if (price > 1 ether + tx.gasprice * _gasLimit) {
208 | return 0; // Unexpectedly high price
209 | }
210 | return provable.query2_withGasLimit{value: price}(_timestamp, _datasource, _arg1, _arg2, _gasLimit);
211 | }
212 |
213 | function provable_query(string memory _datasource, string memory _arg1, string memory _arg2, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
214 | uint price = provable.getPrice(_datasource, _gasLimit);
215 | if (price > 1 ether + tx.gasprice * _gasLimit) {
216 | return 0; // Unexpectedly high price
217 | }
218 | return provable.query2_withGasLimit{value: price}(0, _datasource, _arg1, _arg2, _gasLimit);
219 | }
220 |
221 | function provable_query(string memory _datasource, string[] memory _argN) provableAPI internal returns (bytes32 _id) {
222 | uint price = provable.getPrice(_datasource);
223 | if (price > 1 ether + tx.gasprice * 200000) {
224 | return 0; // Unexpectedly high price
225 | }
226 | bytes memory args = stra2cbor(_argN);
227 | return provable.queryN{value: price}(0, _datasource, args);
228 | }
229 |
230 | function provable_query(uint _timestamp, string memory _datasource, string[] memory _argN) provableAPI internal returns (bytes32 _id) {
231 | uint price = provable.getPrice(_datasource);
232 | if (price > 1 ether + tx.gasprice * 200000) {
233 | return 0; // Unexpectedly high price
234 | }
235 | bytes memory args = stra2cbor(_argN);
236 | return provable.queryN{value: price}(_timestamp, _datasource, args);
237 | }
238 |
239 | function provable_query(uint _timestamp, string memory _datasource, string[] memory _argN, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
240 | uint price = provable.getPrice(_datasource, _gasLimit);
241 | if (price > 1 ether + tx.gasprice * _gasLimit) {
242 | return 0; // Unexpectedly high price
243 | }
244 | bytes memory args = stra2cbor(_argN);
245 | return provable.queryN_withGasLimit{value: price}(_timestamp, _datasource, args, _gasLimit);
246 | }
247 |
248 | function provable_query(string memory _datasource, string[] memory _argN, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
249 | uint price = provable.getPrice(_datasource, _gasLimit);
250 | if (price > 1 ether + tx.gasprice * _gasLimit) {
251 | return 0; // Unexpectedly high price
252 | }
253 | bytes memory args = stra2cbor(_argN);
254 | return provable.queryN_withGasLimit{value: price}(0, _datasource, args, _gasLimit);
255 | }
256 |
257 | function provable_query(string memory _datasource, string[1] memory _args) provableAPI internal returns (bytes32 _id) {
258 | string[] memory dynargs = new string[](1);
259 | dynargs[0] = _args[0];
260 | return provable_query(_datasource, dynargs);
261 | }
262 |
263 | function provable_query(uint _timestamp, string memory _datasource, string[1] memory _args) provableAPI internal returns (bytes32 _id) {
264 | string[] memory dynargs = new string[](1);
265 | dynargs[0] = _args[0];
266 | return provable_query(_timestamp, _datasource, dynargs);
267 | }
268 |
269 | function provable_query(uint _timestamp, string memory _datasource, string[1] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
270 | string[] memory dynargs = new string[](1);
271 | dynargs[0] = _args[0];
272 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
273 | }
274 |
275 | function provable_query(string memory _datasource, string[1] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
276 | string[] memory dynargs = new string[](1);
277 | dynargs[0] = _args[0];
278 | return provable_query(_datasource, dynargs, _gasLimit);
279 | }
280 |
281 | function provable_query(string memory _datasource, string[2] memory _args) provableAPI internal returns (bytes32 _id) {
282 | string[] memory dynargs = new string[](2);
283 | dynargs[0] = _args[0];
284 | dynargs[1] = _args[1];
285 | return provable_query(_datasource, dynargs);
286 | }
287 |
288 | function provable_query(uint _timestamp, string memory _datasource, string[2] memory _args) provableAPI internal returns (bytes32 _id) {
289 | string[] memory dynargs = new string[](2);
290 | dynargs[0] = _args[0];
291 | dynargs[1] = _args[1];
292 | return provable_query(_timestamp, _datasource, dynargs);
293 | }
294 |
295 | function provable_query(uint _timestamp, string memory _datasource, string[2] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
296 | string[] memory dynargs = new string[](2);
297 | dynargs[0] = _args[0];
298 | dynargs[1] = _args[1];
299 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
300 | }
301 |
302 | function provable_query(string memory _datasource, string[2] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
303 | string[] memory dynargs = new string[](2);
304 | dynargs[0] = _args[0];
305 | dynargs[1] = _args[1];
306 | return provable_query(_datasource, dynargs, _gasLimit);
307 | }
308 |
309 | function provable_query(string memory _datasource, string[3] memory _args) provableAPI internal returns (bytes32 _id) {
310 | string[] memory dynargs = new string[](3);
311 | dynargs[0] = _args[0];
312 | dynargs[1] = _args[1];
313 | dynargs[2] = _args[2];
314 | return provable_query(_datasource, dynargs);
315 | }
316 |
317 | function provable_query(uint _timestamp, string memory _datasource, string[3] memory _args) provableAPI internal returns (bytes32 _id) {
318 | string[] memory dynargs = new string[](3);
319 | dynargs[0] = _args[0];
320 | dynargs[1] = _args[1];
321 | dynargs[2] = _args[2];
322 | return provable_query(_timestamp, _datasource, dynargs);
323 | }
324 |
325 | function provable_query(uint _timestamp, string memory _datasource, string[3] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
326 | string[] memory dynargs = new string[](3);
327 | dynargs[0] = _args[0];
328 | dynargs[1] = _args[1];
329 | dynargs[2] = _args[2];
330 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
331 | }
332 |
333 | function provable_query(string memory _datasource, string[3] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
334 | string[] memory dynargs = new string[](3);
335 | dynargs[0] = _args[0];
336 | dynargs[1] = _args[1];
337 | dynargs[2] = _args[2];
338 | return provable_query(_datasource, dynargs, _gasLimit);
339 | }
340 |
341 | function provable_query(string memory _datasource, string[4] memory _args) provableAPI internal returns (bytes32 _id) {
342 | string[] memory dynargs = new string[](4);
343 | dynargs[0] = _args[0];
344 | dynargs[1] = _args[1];
345 | dynargs[2] = _args[2];
346 | dynargs[3] = _args[3];
347 | return provable_query(_datasource, dynargs);
348 | }
349 |
350 | function provable_query(uint _timestamp, string memory _datasource, string[4] memory _args) provableAPI internal returns (bytes32 _id) {
351 | string[] memory dynargs = new string[](4);
352 | dynargs[0] = _args[0];
353 | dynargs[1] = _args[1];
354 | dynargs[2] = _args[2];
355 | dynargs[3] = _args[3];
356 | return provable_query(_timestamp, _datasource, dynargs);
357 | }
358 |
359 | function provable_query(uint _timestamp, string memory _datasource, string[4] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
360 | string[] memory dynargs = new string[](4);
361 | dynargs[0] = _args[0];
362 | dynargs[1] = _args[1];
363 | dynargs[2] = _args[2];
364 | dynargs[3] = _args[3];
365 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
366 | }
367 |
368 | function provable_query(string memory _datasource, string[4] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
369 | string[] memory dynargs = new string[](4);
370 | dynargs[0] = _args[0];
371 | dynargs[1] = _args[1];
372 | dynargs[2] = _args[2];
373 | dynargs[3] = _args[3];
374 | return provable_query(_datasource, dynargs, _gasLimit);
375 | }
376 |
377 | function provable_query(string memory _datasource, string[5] memory _args) provableAPI internal returns (bytes32 _id) {
378 | string[] memory dynargs = new string[](5);
379 | dynargs[0] = _args[0];
380 | dynargs[1] = _args[1];
381 | dynargs[2] = _args[2];
382 | dynargs[3] = _args[3];
383 | dynargs[4] = _args[4];
384 | return provable_query(_datasource, dynargs);
385 | }
386 |
387 | function provable_query(uint _timestamp, string memory _datasource, string[5] memory _args) provableAPI internal returns (bytes32 _id) {
388 | string[] memory dynargs = new string[](5);
389 | dynargs[0] = _args[0];
390 | dynargs[1] = _args[1];
391 | dynargs[2] = _args[2];
392 | dynargs[3] = _args[3];
393 | dynargs[4] = _args[4];
394 | return provable_query(_timestamp, _datasource, dynargs);
395 | }
396 |
397 | function provable_query(uint _timestamp, string memory _datasource, string[5] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
398 | string[] memory dynargs = new string[](5);
399 | dynargs[0] = _args[0];
400 | dynargs[1] = _args[1];
401 | dynargs[2] = _args[2];
402 | dynargs[3] = _args[3];
403 | dynargs[4] = _args[4];
404 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
405 | }
406 |
407 | function provable_query(string memory _datasource, string[5] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
408 | string[] memory dynargs = new string[](5);
409 | dynargs[0] = _args[0];
410 | dynargs[1] = _args[1];
411 | dynargs[2] = _args[2];
412 | dynargs[3] = _args[3];
413 | dynargs[4] = _args[4];
414 | return provable_query(_datasource, dynargs, _gasLimit);
415 | }
416 |
417 | function provable_query(string memory _datasource, bytes[] memory _argN) provableAPI internal returns (bytes32 _id) {
418 | uint price = provable.getPrice(_datasource);
419 | if (price > 1 ether + tx.gasprice * 200000) {
420 | return 0; // Unexpectedly high price
421 | }
422 | bytes memory args = ba2cbor(_argN);
423 | return provable.queryN{value: price}(0, _datasource, args);
424 | }
425 |
426 | function provable_query(uint _timestamp, string memory _datasource, bytes[] memory _argN) provableAPI internal returns (bytes32 _id) {
427 | uint price = provable.getPrice(_datasource);
428 | if (price > 1 ether + tx.gasprice * 200000) {
429 | return 0; // Unexpectedly high price
430 | }
431 | bytes memory args = ba2cbor(_argN);
432 | return provable.queryN{value: price}(_timestamp, _datasource, args);
433 | }
434 |
435 | function provable_query(uint _timestamp, string memory _datasource, bytes[] memory _argN, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
436 | uint price = provable.getPrice(_datasource, _gasLimit);
437 | if (price > 1 ether + tx.gasprice * _gasLimit) {
438 | return 0; // Unexpectedly high price
439 | }
440 | bytes memory args = ba2cbor(_argN);
441 | return provable.queryN_withGasLimit{value: price}(_timestamp, _datasource, args, _gasLimit);
442 | }
443 |
444 | function provable_query(string memory _datasource, bytes[] memory _argN, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
445 | uint price = provable.getPrice(_datasource, _gasLimit);
446 | if (price > 1 ether + tx.gasprice * _gasLimit) {
447 | return 0; // Unexpectedly high price
448 | }
449 | bytes memory args = ba2cbor(_argN);
450 | return provable.queryN_withGasLimit{value: price}(0, _datasource, args, _gasLimit);
451 | }
452 |
453 | function provable_query(string memory _datasource, bytes[1] memory _args) provableAPI internal returns (bytes32 _id) {
454 | bytes[] memory dynargs = new bytes[](1);
455 | dynargs[0] = _args[0];
456 | return provable_query(_datasource, dynargs);
457 | }
458 |
459 | function provable_query(uint _timestamp, string memory _datasource, bytes[1] memory _args) provableAPI internal returns (bytes32 _id) {
460 | bytes[] memory dynargs = new bytes[](1);
461 | dynargs[0] = _args[0];
462 | return provable_query(_timestamp, _datasource, dynargs);
463 | }
464 |
465 | function provable_query(uint _timestamp, string memory _datasource, bytes[1] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
466 | bytes[] memory dynargs = new bytes[](1);
467 | dynargs[0] = _args[0];
468 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
469 | }
470 |
471 | function provable_query(string memory _datasource, bytes[1] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
472 | bytes[] memory dynargs = new bytes[](1);
473 | dynargs[0] = _args[0];
474 | return provable_query(_datasource, dynargs, _gasLimit);
475 | }
476 |
477 | function provable_query(string memory _datasource, bytes[2] memory _args) provableAPI internal returns (bytes32 _id) {
478 | bytes[] memory dynargs = new bytes[](2);
479 | dynargs[0] = _args[0];
480 | dynargs[1] = _args[1];
481 | return provable_query(_datasource, dynargs);
482 | }
483 |
484 | function provable_query(uint _timestamp, string memory _datasource, bytes[2] memory _args) provableAPI internal returns (bytes32 _id) {
485 | bytes[] memory dynargs = new bytes[](2);
486 | dynargs[0] = _args[0];
487 | dynargs[1] = _args[1];
488 | return provable_query(_timestamp, _datasource, dynargs);
489 | }
490 |
491 | function provable_query(uint _timestamp, string memory _datasource, bytes[2] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
492 | bytes[] memory dynargs = new bytes[](2);
493 | dynargs[0] = _args[0];
494 | dynargs[1] = _args[1];
495 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
496 | }
497 |
498 | function provable_query(string memory _datasource, bytes[2] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
499 | bytes[] memory dynargs = new bytes[](2);
500 | dynargs[0] = _args[0];
501 | dynargs[1] = _args[1];
502 | return provable_query(_datasource, dynargs, _gasLimit);
503 | }
504 |
505 | function provable_query(string memory _datasource, bytes[3] memory _args) provableAPI internal returns (bytes32 _id) {
506 | bytes[] memory dynargs = new bytes[](3);
507 | dynargs[0] = _args[0];
508 | dynargs[1] = _args[1];
509 | dynargs[2] = _args[2];
510 | return provable_query(_datasource, dynargs);
511 | }
512 |
513 | function provable_query(uint _timestamp, string memory _datasource, bytes[3] memory _args) provableAPI internal returns (bytes32 _id) {
514 | bytes[] memory dynargs = new bytes[](3);
515 | dynargs[0] = _args[0];
516 | dynargs[1] = _args[1];
517 | dynargs[2] = _args[2];
518 | return provable_query(_timestamp, _datasource, dynargs);
519 | }
520 |
521 | function provable_query(uint _timestamp, string memory _datasource, bytes[3] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
522 | bytes[] memory dynargs = new bytes[](3);
523 | dynargs[0] = _args[0];
524 | dynargs[1] = _args[1];
525 | dynargs[2] = _args[2];
526 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
527 | }
528 |
529 | function provable_query(string memory _datasource, bytes[3] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
530 | bytes[] memory dynargs = new bytes[](3);
531 | dynargs[0] = _args[0];
532 | dynargs[1] = _args[1];
533 | dynargs[2] = _args[2];
534 | return provable_query(_datasource, dynargs, _gasLimit);
535 | }
536 |
537 | function provable_query(string memory _datasource, bytes[4] memory _args) provableAPI internal returns (bytes32 _id) {
538 | bytes[] memory dynargs = new bytes[](4);
539 | dynargs[0] = _args[0];
540 | dynargs[1] = _args[1];
541 | dynargs[2] = _args[2];
542 | dynargs[3] = _args[3];
543 | return provable_query(_datasource, dynargs);
544 | }
545 |
546 | function provable_query(uint _timestamp, string memory _datasource, bytes[4] memory _args) provableAPI internal returns (bytes32 _id) {
547 | bytes[] memory dynargs = new bytes[](4);
548 | dynargs[0] = _args[0];
549 | dynargs[1] = _args[1];
550 | dynargs[2] = _args[2];
551 | dynargs[3] = _args[3];
552 | return provable_query(_timestamp, _datasource, dynargs);
553 | }
554 |
555 | function provable_query(uint _timestamp, string memory _datasource, bytes[4] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
556 | bytes[] memory dynargs = new bytes[](4);
557 | dynargs[0] = _args[0];
558 | dynargs[1] = _args[1];
559 | dynargs[2] = _args[2];
560 | dynargs[3] = _args[3];
561 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
562 | }
563 |
564 | function provable_query(string memory _datasource, bytes[4] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
565 | bytes[] memory dynargs = new bytes[](4);
566 | dynargs[0] = _args[0];
567 | dynargs[1] = _args[1];
568 | dynargs[2] = _args[2];
569 | dynargs[3] = _args[3];
570 | return provable_query(_datasource, dynargs, _gasLimit);
571 | }
572 |
573 | function provable_query(string memory _datasource, bytes[5] memory _args) provableAPI internal returns (bytes32 _id) {
574 | bytes[] memory dynargs = new bytes[](5);
575 | dynargs[0] = _args[0];
576 | dynargs[1] = _args[1];
577 | dynargs[2] = _args[2];
578 | dynargs[3] = _args[3];
579 | dynargs[4] = _args[4];
580 | return provable_query(_datasource, dynargs);
581 | }
582 |
583 | function provable_query(uint _timestamp, string memory _datasource, bytes[5] memory _args) provableAPI internal returns (bytes32 _id) {
584 | bytes[] memory dynargs = new bytes[](5);
585 | dynargs[0] = _args[0];
586 | dynargs[1] = _args[1];
587 | dynargs[2] = _args[2];
588 | dynargs[3] = _args[3];
589 | dynargs[4] = _args[4];
590 | return provable_query(_timestamp, _datasource, dynargs);
591 | }
592 |
593 | function provable_query(uint _timestamp, string memory _datasource, bytes[5] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
594 | bytes[] memory dynargs = new bytes[](5);
595 | dynargs[0] = _args[0];
596 | dynargs[1] = _args[1];
597 | dynargs[2] = _args[2];
598 | dynargs[3] = _args[3];
599 | dynargs[4] = _args[4];
600 | return provable_query(_timestamp, _datasource, dynargs, _gasLimit);
601 | }
602 |
603 | function provable_query(string memory _datasource, bytes[5] memory _args, uint _gasLimit) provableAPI internal returns (bytes32 _id) {
604 | bytes[] memory dynargs = new bytes[](5);
605 | dynargs[0] = _args[0];
606 | dynargs[1] = _args[1];
607 | dynargs[2] = _args[2];
608 | dynargs[3] = _args[3];
609 | dynargs[4] = _args[4];
610 | return provable_query(_datasource, dynargs, _gasLimit);
611 | }
612 |
613 | function provable_setProof(bytes1 _proofP) provableAPI internal {
614 | return provable.setProofType(_proofP);
615 | }
616 |
617 |
618 | function provable_cbAddress() provableAPI internal returns (address _callbackAddress) {
619 | return provable.cbAddress();
620 | }
621 |
622 | function getCodeSize(address _addr) view internal returns (uint _size) {
623 | assembly {
624 | _size := extcodesize(_addr)
625 | }
626 | }
627 |
628 | function provable_setCustomGasPrice(uint _gasPrice) provableAPI internal {
629 | return provable.setCustomGasPrice(_gasPrice);
630 | }
631 |
632 | function provable_randomDS_getSessionPubKeyHash() provableAPI internal returns (bytes32 _sessionKeyHash) {
633 | return provable.randomDS_getSessionPubKeyHash();
634 | }
635 |
636 | function parseAddr(string memory _a) internal pure returns (address _parsedAddress) {
637 | bytes memory tmp = bytes(_a);
638 | uint160 iaddr = 0;
639 | uint160 b1;
640 | uint160 b2;
641 | for (uint i = 2; i < 2 + 2 * 20; i += 2) {
642 | iaddr *= 256;
643 | b1 = uint160(uint8(tmp[i]));
644 | b2 = uint160(uint8(tmp[i + 1]));
645 | if ((b1 >= 97) && (b1 <= 102)) {
646 | b1 -= 87;
647 | } else if ((b1 >= 65) && (b1 <= 70)) {
648 | b1 -= 55;
649 | } else if ((b1 >= 48) && (b1 <= 57)) {
650 | b1 -= 48;
651 | }
652 | if ((b2 >= 97) && (b2 <= 102)) {
653 | b2 -= 87;
654 | } else if ((b2 >= 65) && (b2 <= 70)) {
655 | b2 -= 55;
656 | } else if ((b2 >= 48) && (b2 <= 57)) {
657 | b2 -= 48;
658 | }
659 | iaddr += (b1 * 16 + b2);
660 | }
661 | return address(iaddr);
662 | }
663 |
664 | function strCompare(string memory _a, string memory _b) internal pure returns (int _returnCode) {
665 | bytes memory a = bytes(_a);
666 | bytes memory b = bytes(_b);
667 | uint minLength = a.length;
668 | if (b.length < minLength) {
669 | minLength = b.length;
670 | }
671 | for (uint i = 0; i < minLength; i ++) {
672 | if (a[i] < b[i]) {
673 | return -1;
674 | } else if (a[i] > b[i]) {
675 | return 1;
676 | }
677 | }
678 | if (a.length < b.length) {
679 | return -1;
680 | } else if (a.length > b.length) {
681 | return 1;
682 | } else {
683 | return 0;
684 | }
685 | }
686 |
687 | function indexOf(string memory _haystack, string memory _needle) internal pure returns (int _returnCode) {
688 | bytes memory h = bytes(_haystack);
689 | bytes memory n = bytes(_needle);
690 | if (h.length < 1 || n.length < 1 || (n.length > h.length)) {
691 | return -1;
692 | } else if (h.length > (2 ** 128 - 1)) {
693 | return -1;
694 | } else {
695 | uint subindex = 0;
696 | for (uint i = 0; i < h.length; i++) {
697 | if (h[i] == n[0]) {
698 | subindex = 1;
699 | while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) {
700 | subindex++;
701 | }
702 | if (subindex == n.length) {
703 | return int(i);
704 | }
705 | }
706 | }
707 | return -1;
708 | }
709 | }
710 |
711 | function strConcat(string memory _a, string memory _b) internal pure returns (string memory _concatenatedString) {
712 | return strConcat(_a, _b, "", "", "");
713 | }
714 |
715 | function strConcat(string memory _a, string memory _b, string memory _c) internal pure returns (string memory _concatenatedString) {
716 | return strConcat(_a, _b, _c, "", "");
717 | }
718 |
719 | function strConcat(string memory _a, string memory _b, string memory _c, string memory _d) internal pure returns (string memory _concatenatedString) {
720 | return strConcat(_a, _b, _c, _d, "");
721 | }
722 |
723 | function strConcat(string memory _a, string memory _b, string memory _c, string memory _d, string memory _e) internal pure returns (string memory _concatenatedString) {
724 | bytes memory _ba = bytes(_a);
725 | bytes memory _bb = bytes(_b);
726 | bytes memory _bc = bytes(_c);
727 | bytes memory _bd = bytes(_d);
728 | bytes memory _be = bytes(_e);
729 | string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
730 | bytes memory babcde = bytes(abcde);
731 | uint k = 0;
732 | uint i = 0;
733 | for (i = 0; i < _ba.length; i++) {
734 | babcde[k++] = _ba[i];
735 | }
736 | for (i = 0; i < _bb.length; i++) {
737 | babcde[k++] = _bb[i];
738 | }
739 | for (i = 0; i < _bc.length; i++) {
740 | babcde[k++] = _bc[i];
741 | }
742 | for (i = 0; i < _bd.length; i++) {
743 | babcde[k++] = _bd[i];
744 | }
745 | for (i = 0; i < _be.length; i++) {
746 | babcde[k++] = _be[i];
747 | }
748 | return string(babcde);
749 | }
750 |
751 | function safeParseInt(string memory _a) internal pure returns (uint _parsedInt) {
752 | return safeParseInt(_a, 0);
753 | }
754 |
755 | function safeParseInt(string memory _a, uint _b) internal pure returns (uint _parsedInt) {
756 | bytes memory bresult = bytes(_a);
757 | uint mint = 0;
758 | bool decimals = false;
759 | for (uint i = 0; i < bresult.length; i++) {
760 | if ((uint(uint8(bresult[i])) >= 48) && (uint(uint8(bresult[i])) <= 57)) {
761 | if (decimals) {
762 | if (_b == 0) break;
763 | else _b--;
764 | }
765 | mint *= 10;
766 | mint += uint(uint8(bresult[i])) - 48;
767 | } else if (uint(uint8(bresult[i])) == 46) {
768 | require(!decimals, 'More than one decimal encountered in string!');
769 | decimals = true;
770 | } else {
771 | revert("Non-numeral character encountered in string!");
772 | }
773 | }
774 | if (_b > 0) {
775 | mint *= 10 ** _b;
776 | }
777 | return mint;
778 | }
779 |
780 | function parseInt(string memory _a) internal pure returns (uint _parsedInt) {
781 | return parseInt(_a, 0);
782 | }
783 |
784 | function parseInt(string memory _a, uint _b) internal pure returns (uint _parsedInt) {
785 | bytes memory bresult = bytes(_a);
786 | uint mint = 0;
787 | bool decimals = false;
788 | for (uint i = 0; i < bresult.length; i++) {
789 | if ((uint(uint8(bresult[i])) >= 48) && (uint(uint8(bresult[i])) <= 57)) {
790 | if (decimals) {
791 | if (_b == 0) {
792 | break;
793 | } else {
794 | _b--;
795 | }
796 | }
797 | mint *= 10;
798 | mint += uint(uint8(bresult[i])) - 48;
799 | } else if (uint(uint8(bresult[i])) == 46) {
800 | decimals = true;
801 | }
802 | }
803 | if (_b > 0) {
804 | mint *= 10 ** _b;
805 | }
806 | return mint;
807 | }
808 |
809 | function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
810 | if (_i == 0) {
811 | return "0";
812 | }
813 | uint j = _i;
814 | uint len;
815 | while (j != 0) {
816 | len++;
817 | j /= 10;
818 | }
819 | bytes memory bstr = new bytes(len);
820 | uint k = len - 1;
821 | while (_i != 0) {
822 | bstr[k--] = bytes1(uint8(48 + _i % 10));
823 | _i /= 10;
824 | }
825 | return string(bstr);
826 | }
827 |
828 | function stra2cbor(string[] memory _arr) internal pure returns (bytes memory _cborEncoding) {
829 | safeMemoryCleaner();
830 | Buffer.buffer memory buf;
831 | Buffer.init(buf, 1024);
832 | buf.startArray();
833 | for (uint i = 0; i < _arr.length; i++) {
834 | buf.encodeString(_arr[i]);
835 | }
836 | buf.endSequence();
837 | return buf.buf;
838 | }
839 |
840 | function ba2cbor(bytes[] memory _arr) internal pure returns (bytes memory _cborEncoding) {
841 | safeMemoryCleaner();
842 | Buffer.buffer memory buf;
843 | Buffer.init(buf, 1024);
844 | buf.startArray();
845 | for (uint i = 0; i < _arr.length; i++) {
846 | buf.encodeBytes(_arr[i]);
847 | }
848 | buf.endSequence();
849 | return buf.buf;
850 | }
851 |
852 | function provable_newRandomDSQuery(uint _delay, uint _nbytes, uint _customGasLimit) internal returns (bytes32 _queryId) {
853 | require((_nbytes > 0) && (_nbytes <= 32));
854 | _delay *= 10; // Convert from seconds to ledger timer ticks
855 | bytes memory nbytes = new bytes(1);
856 | nbytes[0] = bytes1(uint8(_nbytes));
857 | bytes memory unonce = new bytes(32);
858 | bytes memory sessionKeyHash = new bytes(32);
859 | bytes32 sessionKeyHash_bytes32 = provable_randomDS_getSessionPubKeyHash();
860 | assembly {
861 | mstore(unonce, 0x20)
862 | /*
863 | The following variables can be relaxed.
864 | Check the relaxed random contract at https://github.com/oraclize/ethereum-examples
865 | for an idea on how to override and replace commit hash variables.
866 | */
867 | mstore(add(unonce, 0x20), xor(blockhash(sub(number(), 1)), xor(coinbase(), timestamp())))
868 | mstore(sessionKeyHash, 0x20)
869 | mstore(add(sessionKeyHash, 0x20), sessionKeyHash_bytes32)
870 | }
871 | bytes memory delay = new bytes(32);
872 | assembly {
873 | mstore(add(delay, 0x20), _delay)
874 | }
875 | bytes memory delay_bytes8 = new bytes(8);
876 | copyBytes(delay, 24, 8, delay_bytes8, 0);
877 | bytes[4] memory args = [unonce, nbytes, sessionKeyHash, delay];
878 | bytes32 queryId = provable_query("random", args, _customGasLimit);
879 | bytes memory delay_bytes8_left = new bytes(8);
880 | assembly {
881 | let x := mload(add(delay_bytes8, 0x20))
882 | mstore8(add(delay_bytes8_left, 0x27), div(x, 0x100000000000000000000000000000000000000000000000000000000000000))
883 | mstore8(add(delay_bytes8_left, 0x26), div(x, 0x1000000000000000000000000000000000000000000000000000000000000))
884 | mstore8(add(delay_bytes8_left, 0x25), div(x, 0x10000000000000000000000000000000000000000000000000000000000))
885 | mstore8(add(delay_bytes8_left, 0x24), div(x, 0x100000000000000000000000000000000000000000000000000000000))
886 | mstore8(add(delay_bytes8_left, 0x23), div(x, 0x1000000000000000000000000000000000000000000000000000000))
887 | mstore8(add(delay_bytes8_left, 0x22), div(x, 0x10000000000000000000000000000000000000000000000000000))
888 | mstore8(add(delay_bytes8_left, 0x21), div(x, 0x100000000000000000000000000000000000000000000000000))
889 | mstore8(add(delay_bytes8_left, 0x20), div(x, 0x1000000000000000000000000000000000000000000000000))
890 | }
891 | provable_randomDS_setCommitment(queryId, keccak256(abi.encodePacked(delay_bytes8_left, args[1], sha256(args[0]), args[2])));
892 | return queryId;
893 | }
894 |
895 | function provable_randomDS_setCommitment(bytes32 _queryId, bytes32 _commitment) internal {
896 | provable_randomDS_args[_queryId] = _commitment;
897 | }
898 |
899 | function verifySig(bytes32 _tosignh, bytes memory _dersig, bytes memory _pubkey) internal returns (bool _sigVerified) {
900 | bool sigok;
901 | address signer;
902 | bytes32 sigr;
903 | bytes32 sigs;
904 | bytes memory sigr_ = new bytes(32);
905 | uint offset = 4 + (uint(uint8(_dersig[3])) - 0x20);
906 | sigr_ = copyBytes(_dersig, offset, 32, sigr_, 0);
907 | bytes memory sigs_ = new bytes(32);
908 | offset += 32 + 2;
909 | sigs_ = copyBytes(_dersig, offset + (uint(uint8(_dersig[offset - 1])) - 0x20), 32, sigs_, 0);
910 | assembly {
911 | sigr := mload(add(sigr_, 32))
912 | sigs := mload(add(sigs_, 32))
913 | }
914 | (sigok, signer) = safer_ecrecover(_tosignh, 27, sigr, sigs);
915 | if (address(uint160(uint256(keccak256(_pubkey)))) == signer) {
916 | return true;
917 | } else {
918 | (sigok, signer) = safer_ecrecover(_tosignh, 28, sigr, sigs);
919 | return (address(uint160(uint256(keccak256(_pubkey)))) == signer);
920 | }
921 | }
922 |
923 | function provable_randomDS_proofVerify__sessionKeyValidity(bytes memory _proof, uint _sig2offset) internal returns (bool _proofVerified) {
924 | bool sigok;
925 | // Random DS Proof Step 6: Verify the attestation signature, APPKEY1 must sign the sessionKey from the correct ledger app (CODEHASH)
926 | bytes memory sig2 = new bytes(uint(uint8(_proof[_sig2offset + 1])) + 2);
927 | copyBytes(_proof, _sig2offset, sig2.length, sig2, 0);
928 | bytes memory appkey1_pubkey = new bytes(64);
929 | copyBytes(_proof, 3 + 1, 64, appkey1_pubkey, 0);
930 | bytes memory tosign2 = new bytes(1 + 65 + 32);
931 | tosign2[0] = bytes1(uint8(1)); //role
932 | copyBytes(_proof, _sig2offset - 65, 65, tosign2, 1);
933 | bytes memory CODEHASH = hex"fd94fa71bc0ba10d39d464d0d8f465efeef0a2764e3887fcc9df41ded20f505c";
934 | copyBytes(CODEHASH, 0, 32, tosign2, 1 + 65);
935 | sigok = verifySig(sha256(tosign2), sig2, appkey1_pubkey);
936 | if (!sigok) {
937 | return false;
938 | }
939 | // Random DS Proof Step 7: Verify the APPKEY1 provenance (must be signed by Ledger)
940 | bytes memory LEDGERKEY = hex"7fb956469c5c9b89840d55b43537e66a98dd4811ea0a27224272c2e5622911e8537a2f8e86a46baec82864e98dd01e9ccc2f8bc5dfc9cbe5a91a290498dd96e4";
941 | bytes memory tosign3 = new bytes(1 + 65);
942 | tosign3[0] = 0xFE;
943 | copyBytes(_proof, 3, 65, tosign3, 1);
944 | bytes memory sig3 = new bytes(uint(uint8(_proof[3 + 65 + 1])) + 2);
945 | copyBytes(_proof, 3 + 65, sig3.length, sig3, 0);
946 | sigok = verifySig(sha256(tosign3), sig3, LEDGERKEY);
947 | return sigok;
948 | }
949 |
950 | function provable_randomDS_proofVerify__returnCode(bytes32 _queryId, string memory _result, bytes memory _proof) internal returns (uint8 _returnCode) {
951 | // Random DS Proof Step 1: The prefix has to match 'LP\x01' (Ledger Proof version 1)
952 | if ((_proof[0] != "L") || (_proof[1] != "P") || (uint8(_proof[2]) != uint8(1))) {
953 | return 1;
954 | }
955 | bool proofVerified = provable_randomDS_proofVerify__main(_proof, _queryId, bytes(_result), provable_getNetworkName());
956 | if (!proofVerified) {
957 | return 2;
958 | }
959 | return 0;
960 | }
961 |
962 | function matchBytes32Prefix(bytes32 _content, bytes memory _prefix, uint _nRandomBytes) internal pure returns (bool _matchesPrefix) {
963 | bool match_ = true;
964 | require(_prefix.length == _nRandomBytes);
965 | for (uint256 i = 0; i< _nRandomBytes; i++) {
966 | if (_content[i] != _prefix[i]) {
967 | match_ = false;
968 | }
969 | }
970 | return match_;
971 | }
972 |
973 | function provable_randomDS_proofVerify__main(bytes memory _proof, bytes32 _queryId, bytes memory _result, string memory _contextName) internal returns (bool _proofVerified) {
974 | // Random DS Proof Step 2: The unique keyhash has to match with the sha256 of (context name + _queryId)
975 | uint ledgerProofLength = 3 + 65 + (uint(uint8(_proof[3 + 65 + 1])) + 2) + 32;
976 | bytes memory keyhash = new bytes(32);
977 | copyBytes(_proof, ledgerProofLength, 32, keyhash, 0);
978 | if (!(keccak256(keyhash) == keccak256(abi.encodePacked(sha256(abi.encodePacked(_contextName, _queryId)))))) {
979 | return false;
980 | }
981 | bytes memory sig1 = new bytes(uint(uint8(_proof[ledgerProofLength + (32 + 8 + 1 + 32) + 1])) + 2);
982 | copyBytes(_proof, ledgerProofLength + (32 + 8 + 1 + 32), sig1.length, sig1, 0);
983 | // Random DS Proof Step 3: We assume sig1 is valid (it will be verified during step 5) and we verify if '_result' is the _prefix of sha256(sig1)
984 | if (!matchBytes32Prefix(sha256(sig1), _result, uint(uint8(_proof[ledgerProofLength + 32 + 8])))) {
985 | return false;
986 | }
987 | // Random DS Proof Step 4: Commitment match verification, keccak256(delay, nbytes, unonce, sessionKeyHash) == commitment in storage.
988 | // This is to verify that the computed args match with the ones specified in the query.
989 | bytes memory commitmentSlice1 = new bytes(8 + 1 + 32);
990 | copyBytes(_proof, ledgerProofLength + 32, 8 + 1 + 32, commitmentSlice1, 0);
991 | bytes memory sessionPubkey = new bytes(64);
992 | uint sig2offset = ledgerProofLength + 32 + (8 + 1 + 32) + sig1.length + 65;
993 | copyBytes(_proof, sig2offset - 64, 64, sessionPubkey, 0);
994 | bytes32 sessionPubkeyHash = sha256(sessionPubkey);
995 | if (provable_randomDS_args[_queryId] == keccak256(abi.encodePacked(commitmentSlice1, sessionPubkeyHash))) { //unonce, nbytes and sessionKeyHash match
996 | delete provable_randomDS_args[_queryId];
997 | } else return false;
998 | // Random DS Proof Step 5: Validity verification for sig1 (keyhash and args signed with the sessionKey)
999 | bytes memory tosign1 = new bytes(32 + 8 + 1 + 32);
1000 | copyBytes(_proof, ledgerProofLength, 32 + 8 + 1 + 32, tosign1, 0);
1001 | if (!verifySig(sha256(tosign1), sig1, sessionPubkey)) {
1002 | return false;
1003 | }
1004 | // Verify if sessionPubkeyHash was verified already, if not.. let's do it!
1005 | if (!provable_randomDS_sessionKeysHashVerified[sessionPubkeyHash]) {
1006 | provable_randomDS_sessionKeysHashVerified[sessionPubkeyHash] = provable_randomDS_proofVerify__sessionKeyValidity(_proof, sig2offset);
1007 | }
1008 | return provable_randomDS_sessionKeysHashVerified[sessionPubkeyHash];
1009 | }
1010 | /*
1011 | The following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
1012 | */
1013 | function copyBytes(bytes memory _from, uint _fromOffset, uint _length, bytes memory _to, uint _toOffset) internal pure returns (bytes memory _copiedBytes) {
1014 | uint minLength = _length + _toOffset;
1015 | require(_to.length >= minLength); // Buffer too small. Should be a better way?
1016 | uint i = 32 + _fromOffset; // NOTE: the offset 32 is added to skip the `size` field of both bytes variables
1017 | uint j = 32 + _toOffset;
1018 | while (i < (32 + _fromOffset + _length)) {
1019 | assembly {
1020 | let tmp := mload(add(_from, i))
1021 | mstore(add(_to, j), tmp)
1022 | }
1023 | i += 32;
1024 | j += 32;
1025 | }
1026 | return _to;
1027 | }
1028 | /*
1029 | The following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
1030 | Duplicate Solidity's ecrecover, but catching the CALL return value
1031 | */
1032 | function safer_ecrecover(bytes32 _hash, uint8 _v, bytes32 _r, bytes32 _s) internal returns (bool _success, address _recoveredAddress) {
1033 | /*
1034 | We do our own memory management here. Solidity uses memory offset
1035 | 0x40 to store the current end of memory. We write past it (as
1036 | writes are memory extensions), but don't update the offset so
1037 | Solidity will reuse it. The memory used here is only needed for
1038 | this context.
1039 | FIXME: inline assembly can't access return values
1040 | */
1041 | bool ret;
1042 | address addr;
1043 | assembly {
1044 | let size := mload(0x40)
1045 | mstore(size, _hash)
1046 | mstore(add(size, 32), _v)
1047 | mstore(add(size, 64), _r)
1048 | mstore(add(size, 96), _s)
1049 | ret := call(3000, 1, 0, size, 128, size, 32) // NOTE: we can reuse the request memory because we deal with the return code.
1050 | addr := mload(size)
1051 | }
1052 | return (ret, addr);
1053 | }
1054 | /*
1055 | The following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
1056 | */
1057 | function ecrecovery(bytes32 _hash, bytes memory _sig) internal returns (bool _success, address _recoveredAddress) {
1058 | bytes32 r;
1059 | bytes32 s;
1060 | uint8 v;
1061 | if (_sig.length != 65) {
1062 | return (false, address(0));
1063 | }
1064 | /*
1065 | The signature format is a compact form of:
1066 | {bytes32 r}{bytes32 s}{uint8 v}
1067 | Compact means, uint8 is not padded to 32 bytes.
1068 | */
1069 | assembly {
1070 | r := mload(add(_sig, 32))
1071 | s := mload(add(_sig, 64))
1072 | /*
1073 | Here we are loading the last 32 bytes. We exploit the fact that
1074 | 'mload' will pad with zeroes if we overread.
1075 | There is no 'mload8' to do this, but that would be nicer.
1076 | */
1077 | v := byte(0, mload(add(_sig, 96)))
1078 | /*
1079 | Alternative solution:
1080 | 'byte' is not working due to the Solidity parser, so lets
1081 | use the second best option, 'and'
1082 | v := and(mload(add(_sig, 65)), 255)
1083 | */
1084 | }
1085 | /*
1086 | albeit non-transactional signatures are not specified by the YP, one would expect it
1087 | to match the YP range of [27, 28]
1088 | geth uses [0, 1] and some clients have followed. This might change, see:
1089 | https://github.com/ethereum/go-ethereum/issues/2053
1090 | */
1091 | if (v < 27) {
1092 | v += 27;
1093 | }
1094 | if (v != 27 && v != 28) {
1095 | return (false, address(0));
1096 | }
1097 | return safer_ecrecover(_hash, v, r, s);
1098 | }
1099 |
1100 | function safeMemoryCleaner() internal pure {
1101 | assembly {
1102 | let fmem := mload(0x40)
1103 | codecopy(fmem, codesize(), sub(msize(), fmem))
1104 | }
1105 | }
1106 | }
1107 |
--------------------------------------------------------------------------------
/hardhat.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | solidity: {
3 | compilers: [
4 | { version: '0.8.0' },
5 | { version: '0.6.2' },
6 | { version: '0.5.0' },
7 | { version: '0.4.25' },
8 | ],
9 | },
10 | };
11 |
--------------------------------------------------------------------------------
/old-contracts/README.md:
--------------------------------------------------------------------------------
1 | __NOTE:__ These are older contracts once housed in this repo that are now only kept for historical reference. They cannot live in the `../contracts` dir otherwise hardhat will attempt to compile them and hardhat does not currently include a configurable way to ignore files. Many of these contracts need versions of `solc` older than the earliest (`v0.4.11`) version that hardhat can work with.
2 |
--------------------------------------------------------------------------------
/old-contracts/connectors/README.md:
--------------------------------------------------------------------------------
1 | This folder contains the code of the two contracts oraclizeAPI.sol is based on, please avoid using them directly, use oraclizeAPI methods instead!
2 |
--------------------------------------------------------------------------------
/old-contracts/connectors/addressResolver.sol:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2015-2016 Oraclize SRL
3 | Copyright (c) 2016 Oraclize LTD
4 | */
5 |
6 |
7 | pragma solidity ^0.4.11;
8 |
9 | contract OraclizeAddrResolver {
10 |
11 | address public addr;
12 |
13 | address owner;
14 |
15 | function OraclizeAddrResolver(){
16 | owner = msg.sender;
17 | }
18 |
19 | function changeOwner(address newowner){
20 | if (msg.sender != owner) throw;
21 | owner = newowner;
22 | }
23 |
24 | function getAddress() returns (address oaddr){
25 | return addr;
26 | }
27 |
28 | function setAddr(address newaddr){
29 | if (msg.sender != owner) throw;
30 | addr = newaddr;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/old-contracts/connectors/oraclizeConnector.sol:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2015-2016 Oraclize SRL
3 | Copyright (c) 2016-2017 Oraclize LTD
4 | */
5 |
6 | /*
7 | Oraclize Connector v1.2.0
8 | */
9 |
10 | // 'compressed' alternative, where all modifiers have been changed to FUNCTIONS
11 | // which is cheaper for deployment, potentially cheaper execution
12 |
13 | pragma solidity ^0.4.11;
14 |
15 | contract Oraclize {
16 | mapping (address => uint) reqc;
17 |
18 | mapping (address => byte) public cbAddresses;
19 |
20 | mapping (address => bool) public offchainPayment;
21 |
22 | event Log1(address sender, bytes32 cid, uint timestamp, string datasource, string arg, uint gaslimit, byte proofType, uint gasPrice);
23 | event Log2(address sender, bytes32 cid, uint timestamp, string datasource, string arg1, string arg2, uint gaslimit, byte proofType, uint gasPrice);
24 | event LogN(address sender, bytes32 cid, uint timestamp, string datasource, bytes args, uint gaslimit, byte proofType, uint gasPrice);
25 | event Log1_fnc(address sender, bytes32 cid, uint timestamp, string datasource, string arg, function() external callback, uint gaslimit, byte proofType, uint gasPrice);
26 | event Log2_fnc(address sender, bytes32 cid, uint timestamp, string datasource, string arg1, string arg2, function() external callback, uint gaslimit, byte proofType, uint gasPrice);
27 | event LogN_fnc(address sender, bytes32 cid, uint timestamp, string datasource, bytes args, function() external callback, uint gaslimit, byte proofType, uint gasPrice);
28 |
29 | event Emit_OffchainPaymentFlag(address indexed idx_sender, address sender, bool indexed idx_flag, bool flag);
30 |
31 | address owner;
32 | address paymentFlagger;
33 |
34 | function changeAdmin(address _newAdmin)
35 | external
36 | {
37 | onlyadmin();
38 | owner = _newAdmin;
39 | }
40 |
41 | function changePaymentFlagger(address _newFlagger)
42 | external
43 | {
44 | onlyadmin();
45 | paymentFlagger = _newFlagger;
46 | }
47 |
48 | function addCbAddress(address newCbAddress, byte addressType)
49 | external
50 | {
51 | onlyadmin();
52 | //bytes memory nil = '';
53 | addCbAddress(newCbAddress, addressType, hex'');
54 | }
55 |
56 | // proof is currently a placeholder for when associated proof for addressType is added
57 | function addCbAddress(address newCbAddress, byte addressType, bytes proof)
58 | public
59 | {
60 | onlyadmin();
61 | cbAddresses[newCbAddress] = addressType;
62 | }
63 |
64 | function removeCbAddress(address newCbAddress)
65 | external
66 | {
67 | onlyadmin();
68 | delete cbAddresses[newCbAddress];
69 | }
70 |
71 | function cbAddress()
72 | constant
73 | returns (address _cbAddress)
74 | {
75 | if (cbAddresses[tx.origin] != 0)
76 | _cbAddress = tx.origin;
77 | }
78 |
79 | function addDSource(string dsname, uint multiplier)
80 | external
81 | {
82 | addDSource(dsname, 0x00, multiplier);
83 | }
84 |
85 | function addDSource(string dsname, byte proofType, uint multiplier)
86 | public
87 | {
88 | onlyadmin();
89 | bytes32 dsname_hash = sha3(dsname, proofType);
90 | dsources[dsources.length++] = dsname_hash;
91 | price_multiplier[dsname_hash] = multiplier;
92 | }
93 |
94 | // Utilized by bridge
95 | function multiAddDSource(bytes32[] dsHash, uint256[] multiplier)
96 | external
97 | {
98 | onlyadmin();
99 | // dsHash -> sha3(DATASOURCE_NAME, PROOF_TYPE);
100 | for (uint i=0; i= price){
180 | uint diff = msg.value - price;
181 | if (diff > 0) {
182 | // added for correct query cost to be returned
183 | if(!msg.sender.send(diff)) {
184 | throw;
185 | }
186 | }
187 | } else throw;
188 | }
189 |
190 | mapping (address => byte) addr_proofType;
191 | mapping (address => uint) addr_gasPrice;
192 | uint public baseprice;
193 | mapping (bytes32 => uint) price;
194 | mapping (bytes32 => uint) price_multiplier;
195 | bytes32[] dsources;
196 |
197 | bytes32[] public randomDS_sessionPubKeysHash;
198 |
199 | function randomDS_updateSessionPubKeysHash(bytes32[] _newSessionPubKeysHash)
200 | external
201 | {
202 | onlyadmin();
203 | randomDS_sessionPubKeysHash.length = 0;
204 | for (uint i=0; i<_newSessionPubKeysHash.length; i++) randomDS_sessionPubKeysHash.push(_newSessionPubKeysHash[i]);
205 | }
206 |
207 | function randomDS_getSessionPubKeyHash()
208 | external
209 | constant
210 | returns (bytes32) {
211 | uint i = uint(sha3(reqc[msg.sender]))%randomDS_sessionPubKeysHash.length;
212 | return randomDS_sessionPubKeysHash[i];
213 | }
214 |
215 | function setProofType(byte _proofType)
216 | external
217 | {
218 | addr_proofType[msg.sender] = _proofType;
219 | }
220 |
221 | function setCustomGasPrice(uint _gasPrice)
222 | external
223 | {
224 | addr_gasPrice[msg.sender] = _gasPrice;
225 | }
226 |
227 | function getPrice(string _datasource)
228 | public
229 | returns (uint _dsprice)
230 | {
231 | return getPrice(_datasource, msg.sender);
232 | }
233 |
234 | function getPrice(string _datasource, uint _gaslimit)
235 | public
236 | returns (uint _dsprice)
237 | {
238 | return getPrice(_datasource, _gaslimit, msg.sender);
239 | }
240 |
241 | function getPrice(string _datasource, address _addr)
242 | private
243 | returns (uint _dsprice)
244 | {
245 | return getPrice(_datasource, 200000, _addr);
246 | }
247 |
248 | function getPrice(string _datasource, uint _gaslimit, address _addr)
249 | private
250 | returns (uint _dsprice)
251 | {
252 | uint gasprice_ = addr_gasPrice[_addr];
253 | if (
254 | (offchainPayment[_addr])
255 | ||(
256 | (_gaslimit <= 200000)&&
257 | (reqc[_addr] == 0)&&
258 | (gasprice_ <= gasprice)&&
259 | (tx.origin != cbAddress())
260 | )
261 | ) return 0;
262 |
263 | if (gasprice_ == 0) gasprice_ = gasprice;
264 | _dsprice = price[sha3(_datasource, addr_proofType[_addr])];
265 | _dsprice += _gaslimit*gasprice_;
266 | return _dsprice;
267 | }
268 |
269 | function getCodeSize(address _addr)
270 | private
271 | constant
272 | returns(uint _size)
273 | {
274 | assembly {
275 | _size := extcodesize(_addr)
276 | }
277 | }
278 |
279 | function query(string _datasource, string _arg)
280 | payable
281 | external
282 | returns (bytes32 _id)
283 | {
284 | return query1(0, _datasource, _arg, 200000);
285 | }
286 |
287 | function query1(string _datasource, string _arg)
288 | payable
289 | external
290 | returns (bytes32 _id)
291 | {
292 | return query1(0, _datasource, _arg, 200000);
293 | }
294 |
295 | function query2(string _datasource, string _arg1, string _arg2)
296 | payable
297 | external
298 | returns (bytes32 _id)
299 | {
300 | return query2(0, _datasource, _arg1, _arg2, 200000);
301 | }
302 |
303 | function queryN(string _datasource, bytes _args)
304 | payable
305 | external
306 | returns (bytes32 _id)
307 | {
308 | return queryN(0, _datasource, _args, 200000);
309 | }
310 |
311 | function query(uint _timestamp, string _datasource, string _arg)
312 | payable
313 | external
314 | returns (bytes32 _id)
315 | {
316 | return query1(_timestamp, _datasource, _arg, 200000);
317 | }
318 |
319 | function query1(uint _timestamp, string _datasource, string _arg)
320 | payable
321 | external
322 | returns (bytes32 _id)
323 | {
324 | return query1(_timestamp, _datasource, _arg, 200000);
325 | }
326 |
327 | function query2(uint _timestamp, string _datasource, string _arg1, string _arg2)
328 | payable
329 | external
330 | returns (bytes32 _id)
331 | {
332 | return query2(_timestamp, _datasource, _arg1, _arg2, 200000);
333 | }
334 |
335 | function queryN(uint _timestamp, string _datasource, bytes _args)
336 | payable
337 | external
338 | returns (bytes32 _id)
339 | {
340 | return queryN(_timestamp, _datasource, _args, 200000);
341 | }
342 |
343 | /* Needless?
344 | function query(uint _timestamp, string _datasource, string _arg, uint _gaslimit)
345 | payable
346 | external
347 | returns (bytes32 _id)
348 | {
349 | return query1(_timestamp, _datasource, _arg, _gaslimit);
350 | }
351 | */
352 | function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit)
353 | payable
354 | external
355 | returns (bytes32 _id)
356 | {
357 | return query1(_timestamp, _datasource, _arg, _gaslimit);
358 | }
359 |
360 | function query1_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit)
361 | payable
362 | external
363 | returns (bytes32 _id)
364 | {
365 | return query1(_timestamp, _datasource, _arg, _gaslimit);
366 | }
367 |
368 | function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit)
369 | payable
370 | external
371 | returns (bytes32 _id)
372 | {
373 | return query2(_timestamp, _datasource, _arg1, _arg2, _gaslimit);
374 | }
375 |
376 | function queryN_withGasLimit(uint _timestamp, string _datasource, bytes _args, uint _gaslimit)
377 | payable
378 | external
379 | returns (bytes32 _id)
380 | {
381 | return queryN(_timestamp, _datasource, _args, _gaslimit);
382 | }
383 |
384 | function query1(uint _timestamp, string _datasource, string _arg, uint _gaslimit)
385 | payable
386 | public
387 | returns (bytes32 _id)
388 | {
389 | costs(_datasource, _gaslimit);
390 | if ((_timestamp > now+3600*24*60)||(_gaslimit > block.gaslimit)) throw;
391 |
392 | _id = sha3(this, msg.sender, reqc[msg.sender]);
393 | reqc[msg.sender]++;
394 | Log1(msg.sender, _id, _timestamp, _datasource, _arg, _gaslimit, addr_proofType[msg.sender], addr_gasPrice[msg.sender]);
395 | return _id;
396 | }
397 |
398 | function query2(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit)
399 | payable
400 | public
401 | returns (bytes32 _id)
402 | {
403 | costs(_datasource, _gaslimit);
404 | if ((_timestamp > now+3600*24*60)||(_gaslimit > block.gaslimit)) throw;
405 |
406 | _id = sha3(this, msg.sender, reqc[msg.sender]);
407 | reqc[msg.sender]++;
408 | Log2(msg.sender, _id, _timestamp, _datasource, _arg1, _arg2, _gaslimit, addr_proofType[msg.sender], addr_gasPrice[msg.sender]);
409 | return _id;
410 | }
411 |
412 | function queryN(uint _timestamp, string _datasource, bytes _args, uint _gaslimit)
413 | payable
414 | public
415 | returns (bytes32 _id)
416 | {
417 | costs(_datasource, _gaslimit);
418 | if ((_timestamp > now+3600*24*60)||(_gaslimit > block.gaslimit)) throw;
419 |
420 | _id = sha3(this, msg.sender, reqc[msg.sender]);
421 | reqc[msg.sender]++;
422 | LogN(msg.sender, _id, _timestamp, _datasource, _args, _gaslimit, addr_proofType[msg.sender], addr_gasPrice[msg.sender]);
423 | return _id;
424 | }
425 |
426 | function query1_fnc(uint _timestamp, string _datasource, string _arg, function() external _fnc, uint _gaslimit)
427 | payable
428 | public
429 | returns (bytes32 _id)
430 | {
431 | costs(_datasource, _gaslimit);
432 | if ((_timestamp > now+3600*24*60)||(_gaslimit > block.gaslimit)||address(_fnc) != msg.sender) throw;
433 |
434 | _id = sha3(this, msg.sender, reqc[msg.sender]);
435 | reqc[msg.sender]++;
436 | Log1_fnc(msg.sender, _id, _timestamp, _datasource, _arg, _fnc, _gaslimit, addr_proofType[msg.sender], addr_gasPrice[msg.sender]);
437 | return _id;
438 | }
439 |
440 | function query2_fnc(uint _timestamp, string _datasource, string _arg1, string _arg2, function() external _fnc, uint _gaslimit)
441 | payable
442 | public
443 | returns (bytes32 _id)
444 | {
445 | costs(_datasource, _gaslimit);
446 | if ((_timestamp > now+3600*24*60)||(_gaslimit > block.gaslimit)||address(_fnc) != msg.sender) throw;
447 |
448 | _id = sha3(this, msg.sender, reqc[msg.sender]);
449 | reqc[msg.sender]++;
450 | Log2_fnc(msg.sender, _id, _timestamp, _datasource, _arg1, _arg2, _fnc, _gaslimit, addr_proofType[msg.sender], addr_gasPrice[msg.sender]);
451 | return _id;
452 | }
453 |
454 | function queryN_fnc(uint _timestamp, string _datasource, bytes _args, function() external _fnc, uint _gaslimit)
455 | payable
456 | public
457 | returns (bytes32 _id)
458 | {
459 | costs(_datasource, _gaslimit);
460 | if ((_timestamp > now+3600*24*60)||(_gaslimit > block.gaslimit)||address(_fnc) != msg.sender) throw;
461 |
462 | _id = sha3(this, msg.sender, reqc[msg.sender]);
463 | reqc[msg.sender]++;
464 | LogN_fnc(msg.sender, _id, _timestamp, _datasource, _args, _fnc, _gaslimit, addr_proofType[msg.sender], addr_gasPrice[msg.sender]);
465 | return _id;
466 | }
467 | }
--------------------------------------------------------------------------------
/old-contracts/lib-experimental/README.md:
--------------------------------------------------------------------------------
1 | ### Experimental Oraclize API Library implementation
2 |
3 | A library version of the oraclize API, allowing for much lower deployment gas costs of contracts leveraging Oraclize.
4 |
5 | Some features might be missing, like the helpers for the [randomDS](http://docs.oraclize.it/#data-sources-random), so if you need to use those please refer to the standard [oraclizeAPI](https://github.com/oraclize/ethereum-api) instead.
6 |
7 | #### Notes
8 |
9 | Following data is applicable to a previous iteration of the library but should still produce similar results.
10 |
11 | For the contract examples, they will not work with Javascript VM in browser-solidity due to some incompatibility of the VM with libraries. It will work fine with ganache, privatenets, testnets, mainnet.
12 |
13 | In the future, we will look to provide an already deployed library, which you may link your contracts to.
14 |
15 | #### Comparison
16 |
17 | (N.B. this is data from the previous version, but should still be applicable)
18 |
19 | Comparison of `KrakenPriceTicker.sol` deployment cost differences:
20 |
21 | * Regular Unoptimized
22 | > 1,867,173 gas
23 | * with Library Unoptimized
24 | > 733,111 gas
25 | * Regular Optimized
26 | > 1,429,716 gas
27 | * with Library Optimized
28 | > 712,380 gas
29 |
30 | As these figures show, it can save up to 50-60% of a contract's deployment costs. It is important to note, that the library will be deployed separately, and its optimized deployment gas cost is almost 3M. It is still very useful in a singular contract basis, if you wish to keep all code within a single contract, as it will allow that singular contract to stay under the network's gas limit. Where the library really shines is in the case of modular architectures with multiple contracts dependant upon the Oraclize service.
31 |
32 | #### Examples
33 |
34 | Contract sources and associated computation archive can be found at: https://github.com/oraclize/ethereum-examples/tree/master/solidity/lib-experimental
35 |
36 | Direct browser-solidity link to examples: https://dapps.oraclize.it/browser-solidity/#version=soljson-v0.4.19+commit.c4cbbb05.js&optimize=false&gist=ad3d1f6007942b727f5909b55e6445d2
37 |
38 | NOTE: if the `OffchainConcat.sol` contract returns an empty string as the result, ensure the multihash can be retrieved from IPFS, and if not, you must run your own IPFS daemon, serving the archive, or upload using Infura.
39 |
--------------------------------------------------------------------------------
/old-contracts/lib-experimental/inlineDynamicHelper_lib.sol:
--------------------------------------------------------------------------------
1 | pragma solidity ^0.4.6 < 0.5;
2 |
3 | /*
4 | Helper for declaring inline dynamic string arrays
5 | */
6 |
7 | library InlineDynamicHelper {
8 | function toDynamic(string[1] self)
9 | internal
10 | returns (string[]) {
11 | string[] memory dyna = new string[](self.length);
12 | for (uint i = 0; i < dyna.length; i++) {
13 | dyna[i] = self[i];
14 | }
15 | return dyna;
16 | }
17 | function toDynamic(string[2] self)
18 | internal
19 | returns (string[]) {
20 | string[] memory dyna = new string[](self.length);
21 | for (uint i = 0; i < dyna.length; i++) {
22 | dyna[i] = self[i];
23 | }
24 | return dyna;
25 | }
26 | function toDynamic(string[3] self)
27 | internal
28 | returns (string[]) {
29 | string[] memory dyna = new string[](self.length);
30 | for (uint i = 0; i < dyna.length; i++) {
31 | dyna[i] = self[i];
32 | }
33 | return dyna;
34 | }
35 | function toDynamic(string[4] self)
36 | internal
37 | returns (string[]) {
38 | string[] memory dyna = new string[](self.length);
39 | for (uint i = 0; i < dyna.length; i++) {
40 | dyna[i] = self[i];
41 | }
42 | return dyna;
43 | }
44 | function toDynamic(string[5] self)
45 | internal
46 | returns (string[]) {
47 | string[] memory dyna = new string[](self.length);
48 | for (uint i = 0; i < dyna.length; i++) {
49 | dyna[i] = self[i];
50 | }
51 | return dyna;
52 | }
53 | function toDynamic(string[6] self)
54 | internal
55 | returns (string[]) {
56 | string[] memory dyna = new string[](self.length);
57 | for (uint i = 0; i < dyna.length; i++) {
58 | dyna[i] = self[i];
59 | }
60 | return dyna;
61 | }
62 | function toDynamic(string[7] self)
63 | internal
64 | returns (string[]) {
65 | string[] memory dyna = new string[](self.length);
66 | for (uint i = 0; i < dyna.length; i++) {
67 | dyna[i] = self[i];
68 | }
69 | return dyna;
70 | }
71 | function toDynamic(string[8] self)
72 | internal
73 | returns (string[]) {
74 | string[] memory dyna = new string[](self.length);
75 | for (uint i = 0; i < dyna.length; i++) {
76 | dyna[i] = self[i];
77 | }
78 | return dyna;
79 | }
80 | function toDynamic(string[9] self)
81 | internal
82 | returns (string[]) {
83 | string[] memory dyna = new string[](self.length);
84 | for (uint i = 0; i < dyna.length; i++) {
85 | dyna[i] = self[i];
86 | }
87 | return dyna;
88 | }
89 | function toDynamic(string[10] self)
90 | internal
91 | returns (string[]) {
92 | string[] memory dyna = new string[](self.length);
93 | for (uint i = 0; i < dyna.length; i++) {
94 | dyna[i] = self[i];
95 | }
96 | return dyna;
97 | }
98 | function toDynamic(string[11] self)
99 | internal
100 | returns (string[]) {
101 | string[] memory dyna = new string[](self.length);
102 | for (uint i = 0; i < dyna.length; i++) {
103 | dyna[i] = self[i];
104 | }
105 | return dyna;
106 | }
107 | function toDynamic(string[12] self)
108 | internal
109 | returns (string[]) {
110 | string[] memory dyna = new string[](self.length);
111 | for (uint i = 0; i < dyna.length; i++) {
112 | dyna[i] = self[i];
113 | }
114 | return dyna;
115 | }
116 | function toDynamic(string[13] self)
117 | internal
118 | returns (string[]) {
119 | string[] memory dyna = new string[](self.length);
120 | for (uint i = 0; i < dyna.length; i++) {
121 | dyna[i] = self[i];
122 | }
123 | return dyna;
124 | }
125 | function toDynamic(string[14] self)
126 | internal
127 | returns (string[]) {
128 | string[] memory dyna = new string[](self.length);
129 | for (uint i = 0; i < dyna.length; i++) {
130 | dyna[i] = self[i];
131 | }
132 | return dyna;
133 | }
134 | function toDynamic(string[15] self)
135 | internal
136 | returns (string[]) {
137 | string[] memory dyna = new string[](self.length);
138 | for (uint i = 0; i < dyna.length; i++) {
139 | dyna[i] = self[i];
140 | }
141 | return dyna;
142 | }
143 | function toDynamic(string[16] self)
144 | internal
145 | returns (string[]) {
146 | string[] memory dyna = new string[](self.length);
147 | for (uint i = 0; i < dyna.length; i++) {
148 | dyna[i] = self[i];
149 | }
150 | return dyna;
151 | }
152 | }
153 |
154 | contract usingInlineDynamic {
155 | using InlineDynamicHelper for string[1];
156 | using InlineDynamicHelper for string[2];
157 | using InlineDynamicHelper for string[3];
158 | using InlineDynamicHelper for string[4];
159 | using InlineDynamicHelper for string[5];
160 | using InlineDynamicHelper for string[6];
161 | using InlineDynamicHelper for string[7];
162 | using InlineDynamicHelper for string[8];
163 | using InlineDynamicHelper for string[9];
164 | using InlineDynamicHelper for string[10];
165 | using InlineDynamicHelper for string[11];
166 | using InlineDynamicHelper for string[12];
167 | using InlineDynamicHelper for string[13];
168 | using InlineDynamicHelper for string[14];
169 | using InlineDynamicHelper for string[15];
170 | using InlineDynamicHelper for string[16];
171 | }
172 |
--------------------------------------------------------------------------------
/old-contracts/lib-experimental/oraclizeAPI_lib.sol:
--------------------------------------------------------------------------------
1 | //
2 | /*
3 | Copyright (c) 2015-2016 Oraclize SRL
4 | Copyright (c) 2016 Oraclize LTD
5 |
6 |
7 |
8 | Permission is hereby granted, free of charge, to any person obtaining a copy
9 | of this software and associated documentation files (the "Software"), to deal
10 | in the Software without restriction, including without limitation the rights
11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | copies of the Software, and to permit persons to whom the Software is
13 | furnished to do so, subject to the following conditions:
14 |
15 |
16 |
17 | The above copyright notice and this permission notice shall be included in
18 | all copies or substantial portions of the Software.
19 |
20 |
21 |
22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 | THE SOFTWARE.
29 | */
30 |
31 | pragma solidity ^0.4.19 <=0.4.20;// Incompatible compiler version... please select one stated within pragma solidity or use different oraclizeAPI version
32 |
33 | contract OraclizeI {
34 | address public cbAddress;
35 | function query(uint _timestamp, string _datasource, string _arg) external payable returns (bytes32 _id);
36 | function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) external payable returns (bytes32 _id);
37 | function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) public payable returns (bytes32 _id);
38 | function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) external payable returns (bytes32 _id);
39 | function queryN(uint _timestamp, string _datasource, bytes _argN) public payable returns (bytes32 _id);
40 | function queryN_withGasLimit(uint _timestamp, string _datasource, bytes _argN, uint _gaslimit) external payable returns (bytes32 _id);
41 | function getPrice(string _datasource) public view returns (uint _dsprice);
42 | function getPrice(string _datasource, uint gaslimit) public view returns (uint _dsprice);
43 | function setProofType(byte _proofType) external;
44 | function setCustomGasPrice(uint _gasPrice) external;
45 | function randomDS_getSessionPubKeyHash() external view returns(bytes32);
46 | }
47 |
48 | contract OraclizeAddrResolverI {
49 | function getAddress() public view returns (address _addr);
50 | }
51 |
52 | /*
53 | Begin solidity-cborutils
54 |
55 | https://github.com/smartcontractkit/solidity-cborutils
56 |
57 | MIT License
58 |
59 | Copyright (c) 2018 SmartContract ChainLink, Ltd.
60 |
61 | Permission is hereby granted, free of charge, to any person obtaining a copy
62 | of this software and associated documentation files (the "Software"), to deal
63 | in the Software without restriction, including without limitation the rights
64 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
65 | copies of the Software, and to permit persons to whom the Software is
66 | furnished to do so, subject to the following conditions:
67 |
68 | The above copyright notice and this permission notice shall be included in all
69 | copies or substantial portions of the Software.
70 |
71 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
72 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
73 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
74 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
75 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
76 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
77 | SOFTWARE.
78 | */
79 |
80 | library Buffer {
81 | struct buffer {
82 | bytes buf;
83 | uint capacity;
84 | }
85 |
86 | function init(buffer memory buf, uint capacity) internal pure {
87 | if(capacity % 32 != 0) capacity += 32 - (capacity % 32);
88 | // Allocate space for the buffer data
89 | buf.capacity = capacity;
90 | assembly {
91 | let ptr := mload(0x40)
92 | mstore(buf, ptr)
93 | mstore(0x40, add(ptr, capacity))
94 | }
95 | }
96 |
97 | function resize(buffer memory buf, uint capacity) private pure {
98 | bytes memory oldbuf = buf.buf;
99 | init(buf, capacity);
100 | append(buf, oldbuf);
101 | }
102 |
103 | function max(uint a, uint b) private pure returns(uint) {
104 | if(a > b) {
105 | return a;
106 | }
107 | return b;
108 | }
109 |
110 | /**
111 | * @dev Appends a byte array to the end of the buffer. Reverts if doing so
112 | * would exceed the capacity of the buffer.
113 | * @param buf The buffer to append to.
114 | * @param data The data to append.
115 | * @return The original buffer.
116 | */
117 | function append(buffer memory buf, bytes data) internal pure returns(buffer memory) {
118 | if(data.length + buf.buf.length > buf.capacity) {
119 | resize(buf, max(buf.capacity, data.length) * 2);
120 | }
121 |
122 | uint dest;
123 | uint src;
124 | uint len = data.length;
125 | assembly {
126 | // Memory address of the buffer data
127 | let bufptr := mload(buf)
128 | // Length of existing buffer data
129 | let buflen := mload(bufptr)
130 | // Start address = buffer address + buffer length + sizeof(buffer length)
131 | dest := add(add(bufptr, buflen), 32)
132 | // Update buffer length
133 | mstore(bufptr, add(buflen, mload(data)))
134 | src := add(data, 32)
135 | }
136 |
137 | // Copy word-length chunks while possible
138 | for(; len >= 32; len -= 32) {
139 | assembly {
140 | mstore(dest, mload(src))
141 | }
142 | dest += 32;
143 | src += 32;
144 | }
145 |
146 | // Copy remaining bytes
147 | uint mask = 256 ** (32 - len) - 1;
148 | assembly {
149 | let srcpart := and(mload(src), not(mask))
150 | let destpart := and(mload(dest), mask)
151 | mstore(dest, or(destpart, srcpart))
152 | }
153 |
154 | return buf;
155 | }
156 |
157 | /**
158 | * @dev Appends a byte to the end of the buffer. Reverts if doing so would
159 | * exceed the capacity of the buffer.
160 | * @param buf The buffer to append to.
161 | * @param data The data to append.
162 | * @return The original buffer.
163 | */
164 | function append(buffer memory buf, uint8 data) internal pure {
165 | if(buf.buf.length + 1 > buf.capacity) {
166 | resize(buf, buf.capacity * 2);
167 | }
168 |
169 | assembly {
170 | // Memory address of the buffer data
171 | let bufptr := mload(buf)
172 | // Length of existing buffer data
173 | let buflen := mload(bufptr)
174 | // Address = buffer address + buffer length + sizeof(buffer length)
175 | let dest := add(add(bufptr, buflen), 32)
176 | mstore8(dest, data)
177 | // Update buffer length
178 | mstore(bufptr, add(buflen, 1))
179 | }
180 | }
181 |
182 | /**
183 | * @dev Appends a byte to the end of the buffer. Reverts if doing so would
184 | * exceed the capacity of the buffer.
185 | * @param buf The buffer to append to.
186 | * @param data The data to append.
187 | * @return The original buffer.
188 | */
189 | function appendInt(buffer memory buf, uint data, uint len) internal pure returns(buffer memory) {
190 | if(len + buf.buf.length > buf.capacity) {
191 | resize(buf, max(buf.capacity, len) * 2);
192 | }
193 |
194 | uint mask = 256 ** len - 1;
195 | assembly {
196 | // Memory address of the buffer data
197 | let bufptr := mload(buf)
198 | // Length of existing buffer data
199 | let buflen := mload(bufptr)
200 | // Address = buffer address + buffer length + sizeof(buffer length) + len
201 | let dest := add(add(bufptr, buflen), len)
202 | mstore(dest, or(and(mload(dest), not(mask)), data))
203 | // Update buffer length
204 | mstore(bufptr, add(buflen, len))
205 | }
206 | return buf;
207 | }
208 | }
209 |
210 | library CBOR {
211 | using Buffer for Buffer.buffer;
212 |
213 | uint8 private constant MAJOR_TYPE_INT = 0;
214 | uint8 private constant MAJOR_TYPE_NEGATIVE_INT = 1;
215 | uint8 private constant MAJOR_TYPE_BYTES = 2;
216 | uint8 private constant MAJOR_TYPE_STRING = 3;
217 | uint8 private constant MAJOR_TYPE_ARRAY = 4;
218 | uint8 private constant MAJOR_TYPE_MAP = 5;
219 | uint8 private constant MAJOR_TYPE_CONTENT_FREE = 7;
220 |
221 | function encodeType(Buffer.buffer memory buf, uint8 major, uint value) private pure {
222 | if(value <= 23) {
223 | buf.append(uint8((major << 5) | value));
224 | } else if(value <= 0xFF) {
225 | buf.append(uint8((major << 5) | 24));
226 | buf.appendInt(value, 1);
227 | } else if(value <= 0xFFFF) {
228 | buf.append(uint8((major << 5) | 25));
229 | buf.appendInt(value, 2);
230 | } else if(value <= 0xFFFFFFFF) {
231 | buf.append(uint8((major << 5) | 26));
232 | buf.appendInt(value, 4);
233 | } else if(value <= 0xFFFFFFFFFFFFFFFF) {
234 | buf.append(uint8((major << 5) | 27));
235 | buf.appendInt(value, 8);
236 | }
237 | }
238 |
239 | function encodeIndefiniteLengthType(Buffer.buffer memory buf, uint8 major) private pure {
240 | buf.append(uint8((major << 5) | 31));
241 | }
242 |
243 | function encodeUInt(Buffer.buffer memory buf, uint value) internal pure {
244 | encodeType(buf, MAJOR_TYPE_INT, value);
245 | }
246 |
247 | function encodeInt(Buffer.buffer memory buf, int value) internal pure {
248 | if(value >= 0) {
249 | encodeType(buf, MAJOR_TYPE_INT, uint(value));
250 | } else {
251 | encodeType(buf, MAJOR_TYPE_NEGATIVE_INT, uint(-1 - value));
252 | }
253 | }
254 |
255 | function encodeBytes(Buffer.buffer memory buf, bytes value) internal pure {
256 | encodeType(buf, MAJOR_TYPE_BYTES, value.length);
257 | buf.append(value);
258 | }
259 |
260 | function encodeString(Buffer.buffer memory buf, string value) internal pure {
261 | encodeType(buf, MAJOR_TYPE_STRING, bytes(value).length);
262 | buf.append(bytes(value));
263 | }
264 |
265 | function startArray(Buffer.buffer memory buf) internal pure {
266 | encodeIndefiniteLengthType(buf, MAJOR_TYPE_ARRAY);
267 | }
268 |
269 | function startMap(Buffer.buffer memory buf) internal pure {
270 | encodeIndefiniteLengthType(buf, MAJOR_TYPE_MAP);
271 | }
272 |
273 | function endSequence(Buffer.buffer memory buf) internal pure {
274 | encodeIndefiniteLengthType(buf, MAJOR_TYPE_CONTENT_FREE);
275 | }
276 | }
277 |
278 | /*
279 | End solidity-cborutils
280 | */
281 |
282 | library oraclizeLib {
283 |
284 | function proofType_NONE()
285 | public
286 | pure
287 | returns (byte) {
288 | return 0x00;
289 | }
290 |
291 | function proofType_TLSNotary()
292 | public
293 | pure
294 | returns (byte) {
295 | return 0x10;
296 | }
297 |
298 | function proofType_Android()
299 | public
300 | pure
301 | returns (byte) {
302 | return 0x40;
303 | }
304 |
305 | function proofType_Ledger()
306 | public
307 | pure
308 | returns (byte) {
309 | return 0x30;
310 | }
311 |
312 | function proofType_Native()
313 | public
314 | pure
315 | returns (byte) {
316 | return 0xF0;
317 | }
318 |
319 | function proofStorage_IPFS()
320 | public
321 | pure
322 | returns (byte) {
323 | return 0x01;
324 | }
325 |
326 | //OraclizeAddrResolverI constant public OAR = oraclize_setNetwork();
327 |
328 | function OAR()
329 | public
330 | view
331 | returns (OraclizeAddrResolverI) {
332 | return oraclize_setNetwork();
333 | }
334 |
335 | //OraclizeI constant public oraclize = OraclizeI(OAR.getAddress());
336 |
337 | function oraclize()
338 | public
339 | view
340 | returns (OraclizeI) {
341 | return OraclizeI(OAR().getAddress());
342 | }
343 |
344 | function oraclize_setNetwork()
345 | public
346 | view
347 | returns(OraclizeAddrResolverI){
348 | if (getCodeSize(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed)>0){ //mainnet
349 | return OraclizeAddrResolverI(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed);
350 | }
351 | if (getCodeSize(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1)>0){ //ropsten testnet
352 | return OraclizeAddrResolverI(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1);
353 | }
354 | if (getCodeSize(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e)>0){ //kovan testnet
355 | return OraclizeAddrResolverI(0xB7A07BcF2Ba2f2703b24C0691b5278999C59AC7e);
356 | }
357 | if (getCodeSize(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48)>0){ //rinkeby testnet
358 | return OraclizeAddrResolverI(0x146500cfd35B22E4A392Fe0aDc06De1a1368Ed48);
359 | }
360 | if (getCodeSize(0x90A0F94702c9630036FB9846B52bf31A1C991a84)>0){ //bsc mainnet
361 | return OraclizeAddrResolverI(0x90A0F94702c9630036FB9846B52bf31A1C991a84);
362 | }
363 | if (getCodeSize(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609)>0){ //polygon mainnet
364 | return OraclizeAddrResolverI(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609);
365 | }
366 | if (getCodeSize(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85)>0){ //sepolia mainnet
367 | return OraclizeAddrResolverI(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85);
368 | }
369 | if (getCodeSize(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475)>0){ //ethereum-bridge
370 | return OraclizeAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475);
371 | }
372 | if (getCodeSize(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF)>0){ //ether.camp ide
373 | return OraclizeAddrResolverI(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF);
374 | }
375 | if (getCodeSize(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA)>0){ //browser-solidity
376 | return OraclizeAddrResolverI(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA);
377 | }
378 | }
379 |
380 | function oraclize_getPrice(string datasource)
381 | public
382 | view
383 | returns (uint){
384 | return oraclize().getPrice(datasource);
385 | }
386 |
387 | function oraclize_getPrice(string datasource, uint gaslimit)
388 | public
389 | view
390 | returns (uint){
391 | return oraclize().getPrice(datasource, gaslimit);
392 | }
393 |
394 | function oraclize_query(string datasource, string arg)
395 | public
396 | returns (bytes32 id){
397 | return oraclize_query(0, datasource, arg);
398 | }
399 |
400 | function oraclize_query(uint timestamp, string datasource, string arg)
401 | public
402 | returns (bytes32 id){
403 | OraclizeI oracle = oraclize();
404 | uint price = oracle.getPrice(datasource);
405 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
406 | return oracle.query.value(price)(timestamp, datasource, arg);
407 | }
408 |
409 | function oraclize_query(string datasource, string arg, uint gaslimit)
410 | public
411 | returns (bytes32 id){
412 | return oraclize_query(0, datasource, arg, gaslimit);
413 | }
414 |
415 | function oraclize_query(uint timestamp, string datasource, string arg, uint gaslimit)
416 | public
417 | returns (bytes32 id){
418 | OraclizeI oracle = oraclize();
419 | uint price = oracle.getPrice(datasource, gaslimit);
420 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
421 | return oracle.query_withGasLimit.value(price)(timestamp, datasource, arg, gaslimit);
422 | }
423 |
424 | function oraclize_query(string datasource, string arg1, string arg2)
425 | public
426 | returns (bytes32 id){
427 | return oraclize_query(0, datasource, arg1, arg2);
428 | }
429 |
430 | function oraclize_query(uint timestamp, string datasource, string arg1, string arg2)
431 | public
432 | returns (bytes32 id){
433 | OraclizeI oracle = oraclize();
434 | uint price = oracle.getPrice(datasource);
435 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
436 | return oracle.query2.value(price)(timestamp, datasource, arg1, arg2);
437 | }
438 |
439 | function oraclize_query(string datasource, string arg1, string arg2, uint gaslimit)
440 | public
441 | returns (bytes32 id){
442 | return oraclize_query(0, datasource, arg1, arg2, gaslimit);
443 | }
444 |
445 | function oraclize_query(uint timestamp, string datasource, string arg1, string arg2, uint gaslimit)
446 | public
447 | returns (bytes32 id){
448 | OraclizeI oracle = oraclize();
449 | uint price = oracle.getPrice(datasource, gaslimit);
450 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
451 | return oracle.query2_withGasLimit.value(price)(timestamp, datasource, arg1, arg2, gaslimit);
452 | }
453 |
454 | // internalize w/o experimental
455 | function oraclize_query(string datasource, string[] argN)
456 | internal
457 | returns (bytes32 id){
458 | return oraclize_query(0, datasource, argN);
459 | }
460 |
461 | // internalize w/o experimental
462 | function oraclize_query(uint timestamp, string datasource, string[] argN)
463 | internal
464 | returns (bytes32 id){
465 | OraclizeI oracle = oraclize();
466 | uint price = oracle.getPrice(datasource);
467 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
468 | bytes memory args = stra2cbor(argN);
469 | return oracle.queryN.value(price)(timestamp, datasource, args);
470 | }
471 |
472 | // internalize w/o experimental
473 | function oraclize_query(string datasource, string[] argN, uint gaslimit)
474 | internal
475 | returns (bytes32 id){
476 | return oraclize_query(0, datasource, argN, gaslimit);
477 | }
478 |
479 | // internalize w/o experimental
480 | function oraclize_query(uint timestamp, string datasource, string[] argN, uint gaslimit)
481 | internal
482 | returns (bytes32 id){
483 | OraclizeI oracle = oraclize();
484 | uint price = oracle.getPrice(datasource, gaslimit);
485 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
486 | bytes memory args = stra2cbor(argN);
487 | return oracle.queryN_withGasLimit.value(price)(timestamp, datasource, args, gaslimit);
488 | }
489 |
490 | function oraclize_cbAddress()
491 | public
492 | view
493 | returns (address){
494 | return oraclize().cbAddress();
495 | }
496 |
497 | function oraclize_setProof(byte proofP)
498 | public {
499 | return oraclize().setProofType(proofP);
500 | }
501 |
502 | function oraclize_setCustomGasPrice(uint gasPrice)
503 | public {
504 | return oraclize().setCustomGasPrice(gasPrice);
505 | }
506 |
507 | // setting to internal doesn't cause major increase in deployment and saves gas
508 | // per use, for this tiny function
509 | function getCodeSize(address _addr)
510 | public
511 | view
512 | returns(uint _size) {
513 | assembly {
514 | _size := extcodesize(_addr)
515 | }
516 | }
517 |
518 | // expects 0x prefix
519 | function parseAddr(string _a)
520 | public
521 | pure
522 | returns (address){
523 | bytes memory tmp = bytes(_a);
524 | uint160 iaddr = 0;
525 | uint160 b1;
526 | uint160 b2;
527 | for (uint i=2; i<2+2*20; i+=2){
528 | iaddr *= 256;
529 | b1 = uint160(tmp[i]);
530 | b2 = uint160(tmp[i+1]);
531 | if ((b1 >= 97)&&(b1 <= 102)) b1 -= 87;
532 | else if ((b1 >= 65)&&(b1 <= 70)) b1 -= 55;
533 | else if ((b1 >= 48)&&(b1 <= 57)) b1 -= 48;
534 | if ((b2 >= 97)&&(b2 <= 102)) b2 -= 87;
535 | else if ((b2 >= 65)&&(b2 <= 70)) b2 -= 55;
536 | else if ((b2 >= 48)&&(b2 <= 57)) b2 -= 48;
537 | iaddr += (b1*16+b2);
538 | }
539 | return address(iaddr);
540 | }
541 |
542 | function strCompare(string _a, string _b)
543 | public
544 | pure
545 | returns (int) {
546 | bytes memory a = bytes(_a);
547 | bytes memory b = bytes(_b);
548 | uint minLength = a.length;
549 | if (b.length < minLength) minLength = b.length;
550 | for (uint i = 0; i < minLength; i ++)
551 | if (a[i] < b[i])
552 | return -1;
553 | else if (a[i] > b[i])
554 | return 1;
555 | if (a.length < b.length)
556 | return -1;
557 | else if (a.length > b.length)
558 | return 1;
559 | else
560 | return 0;
561 | }
562 |
563 | function indexOf(string _haystack, string _needle)
564 | public
565 | pure
566 | returns (int) {
567 | bytes memory h = bytes(_haystack);
568 | bytes memory n = bytes(_needle);
569 | if(h.length < 1 || n.length < 1 || (n.length > h.length))
570 | return -1;
571 | else if(h.length > (2**128 -1))
572 | return -1;
573 | else
574 | {
575 | uint subindex = 0;
576 | for (uint i = 0; i < h.length; i ++)
577 | {
578 | if (h[i] == n[0])
579 | {
580 | subindex = 1;
581 | while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex])
582 | {
583 | subindex++;
584 | }
585 | if(subindex == n.length)
586 | return int(i);
587 | }
588 | }
589 | return -1;
590 | }
591 | }
592 |
593 | function strConcat(string _a, string _b, string _c, string _d, string _e)
594 | internal
595 | pure
596 | returns (string) {
597 | bytes memory _ba = bytes(_a);
598 | bytes memory _bb = bytes(_b);
599 | bytes memory _bc = bytes(_c);
600 | bytes memory _bd = bytes(_d);
601 | bytes memory _be = bytes(_e);
602 | string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
603 | bytes memory babcde = bytes(abcde);
604 | uint k = 0;
605 | for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
606 | for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
607 | for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
608 | for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
609 | for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
610 | return string(babcde);
611 | }
612 |
613 | function strConcat(string _a, string _b, string _c, string _d)
614 | internal
615 | pure
616 | returns (string) {
617 | return strConcat(_a, _b, _c, _d, "");
618 | }
619 |
620 | function strConcat(string _a, string _b, string _c)
621 | internal
622 | pure
623 | returns (string) {
624 | return strConcat(_a, _b, _c, "", "");
625 | }
626 |
627 | function strConcat(string _a, string _b)
628 | internal
629 | pure
630 | returns (string) {
631 | return strConcat(_a, _b, "", "", "");
632 | }
633 |
634 | // parseInt
635 | function parseInt(string _a)
636 | public
637 | pure
638 | returns (uint) {
639 | return parseInt(_a, 0);
640 | }
641 |
642 | // parseInt(parseFloat*10^_b)
643 | function parseInt(string _a, uint _b)
644 | public
645 | pure
646 | returns (uint) {
647 | bytes memory bresult = bytes(_a);
648 | uint mint = 0;
649 | bool decimals = false;
650 | for (uint i=0; i= 48)&&(bresult[i] <= 57)){
652 | if (decimals){
653 | if (_b == 0) break;
654 | else _b--;
655 | }
656 | mint *= 10;
657 | mint += uint(bresult[i]) - 48;
658 | } else if (bresult[i] == 46) decimals = true;
659 | }
660 | if (_b > 0) mint *= 10**_b;
661 | return mint;
662 | }
663 |
664 | function uint2str(uint i)
665 | internal
666 | pure
667 | returns (string){
668 | if (i == 0) return "0";
669 | uint j = i;
670 | uint len;
671 | while (j != 0){
672 | len++;
673 | j /= 10;
674 | }
675 | bytes memory bstr = new bytes(len);
676 | uint k = len - 1;
677 | while (i != 0){
678 | bstr[k--] = byte(48 + i % 10);
679 | i /= 10;
680 | }
681 | return string(bstr);
682 | }
683 |
684 | using CBOR for Buffer.buffer;
685 | function stra2cbor(string[] arr)
686 | internal
687 | pure
688 | returns (bytes) {
689 | Buffer.buffer memory buf;
690 | Buffer.init(buf, 1024);
691 | buf.startArray();
692 | for (uint i = 0; i < arr.length; i++) {
693 | buf.encodeString(arr[i]);
694 | }
695 | buf.endSequence();
696 | return buf.buf;
697 | }
698 |
699 | function ba2cbor(bytes[] arr)
700 | internal
701 | pure
702 | returns (bytes) {
703 | Buffer.buffer memory buf;
704 | Buffer.init(buf, 1024);
705 | buf.startArray();
706 | for (uint i = 0; i < arr.length; i++) {
707 | buf.encodeBytes(arr[i]);
708 | }
709 | buf.endSequence();
710 | return buf.buf;
711 | }
712 |
713 | function b2s(bytes _b)
714 | internal
715 | returns (string) {
716 | bytes memory output = new bytes(_b.length * 2);
717 | uint len = output.length;
718 |
719 | assembly {
720 | let i := 0
721 | let mem := 0
722 | loop:
723 | // isolate octet
724 | 0x1000000000000000000000000000000000000000000000000000000000000000
725 | exp(0x10, mod(i, 0x40))
726 | // change offset only if needed
727 | jumpi(skip, gt(mod(i, 0x40), 0))
728 | // save offset mem for reuse
729 | mem := mload(add(_b, add(mul(0x20, div(i, 0x40)), 0x20)))
730 | skip:
731 | mem
732 | mul
733 | div
734 | dup1
735 | // check if alpha or numerical, jump if numerical
736 | 0x0a
737 | swap1
738 | lt
739 | num
740 | jumpi
741 | // offset alpha char correctly
742 | 0x0a
743 | swap1
744 | sub
745 | alp:
746 | 0x61
747 | add
748 | jump(end)
749 | num:
750 | 0x30
751 | add
752 | end:
753 | add(output, add(0x20, i))
754 | mstore8
755 | i := add(i, 1)
756 | jumpi(loop, gt(len, i))
757 | }
758 |
759 | return string(output);
760 | }
761 | }
762 | //
763 |
--------------------------------------------------------------------------------
/old-contracts/previous-api-contracts/README.md:
--------------------------------------------------------------------------------
1 | __NOTE:__ Here are previous versions of the `ethereum-api` using solidity compilers prior to `0.4.11`, which is the earliest that `hardhat` can work with. As such, they're moved here since they're unlikely to be useful in future projects, but have been used successfully in previous and still-existing projects. Notice also the serpent version!
2 |
--------------------------------------------------------------------------------
/old-contracts/previous-api-contracts/oraclizeAPI.se:
--------------------------------------------------------------------------------
1 | #
2 |
3 | # IMPORTANT NOTICE: THIS ORACLIZE_API VERSION IS DEPRECATED AND IS NOT ACTIVELY MAINTAINED ANYMORE
4 | # THIS IS STAYING HERE FOR COMPATIBILITY/HISTORICAL REASONS WITH SERPENT
5 | # PLEASE ALWAYS REFER TO THE LATEST ORACLIZE-API CODE FOR SOLIDITY:
6 | # https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI.sol
7 |
8 | # Copyright (c) 2015-2016 Oraclize SRL
9 | # Copyright (c) 2016-2019 Oraclize LTD
10 | # Copyright (c) 2019 Provable Things Limited
11 |
12 |
13 |
14 | # Permission is hereby granted, free of charge, to any person obtaining a copy
15 | # of this software and associated documentation files (the "Software"), to deal
16 | # in the Software without restriction, including without limitation the rights
17 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 | # copies of the Software, and to permit persons to whom the Software is
19 | # furnished to do so, subject to the following conditions:
20 |
21 |
22 |
23 | # The above copyright notice and this permission notice shall be included in
24 | # all copies or substantial portions of the Software.
25 |
26 |
27 |
28 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 | # THE SOFTWARE.
35 |
36 | extern OraclizeAddrResolverI: [getAddress:[]:int256]
37 |
38 | extern OraclizeI: [cbAddress:[]:int256,query:[uint256,string,string]:bytes32,query2:[uint256,string,string,string]:bytes32,query_withGasLimit:[uint256,string,string,uint256]:bytes32,query2_withGasLimit:[uint256,string,string,string,uint256]:bytes32,getPrice:[string,uint256]:uint256,useCoupon:[string]:_,setProofType:[bytes1]:_]
39 |
40 | macro day: 60*60*24
41 | macro week: 60*60*24*7
42 | macro month: 60*60*24*30
43 |
44 | macro proofType_NONE: 0x0000000000000000000000000000000000000000000000000000000000000000
45 | macro proofType_TLSNotary: 0x1000000000000000000000000000000000000000000000000000000000000000
46 | macro proofStorage_IPFS: 0x0100000000000000000000000000000000000000000000000000000000000000
47 |
48 | macro networkID_mainnet: 1
49 | macro networkID_testnet: 2
50 | macro networkID_morden: 2
51 | macro networkID_ropsten: 2
52 | macro networkID_consensys: 161
53 | macro networkID_auto: 0
54 |
55 | data OAR
56 |
57 | macro oraclize_setNetwork(networkID_auto):
58 | if(~extcodesize(0x1d3b2638a7cc9f2cb3d298a3da7a90b67e5506ed)>0):
59 | self.OAR = 0x1d3b2638a7cc9f2cb3d298a3da7a90b67e5506ed
60 | elif(~extcodesize(0xc03a2615d5efaf5f49f60b7bb6583eaec212fdf1)>0):
61 | self.OAR = 0xc03a2615d5efaf5f49f60b7bb6583eaec212fdf1
62 | elif(~extcodesize(0x20e12a1f859b3feae5fb2a0a32c18f5a65555bbf)>0):
63 | self.OAR = 0x20e12a1f859b3feae5fb2a0a32c18f5a65555bbf
64 |
65 | macro oraclize_setNetwork(networkID_mainnet):
66 | self.OAR = 0x1d3b2638a7cc9f2cb3d298a3da7a90b67e5506ed
67 |
68 | macro oraclize_setNetwork(networkID_testnet):
69 | self.OAR = 0xc03a2615d5efaf5f49f60b7bb6583eaec212fdf1
70 |
71 | macro oraclize_setNetwork(networkID_consensys):
72 | self.OAR = 0x20e12a1f859b3feae5fb2a0a32c18f5a65555bbf
73 |
74 | macro oraclize_cbAddress():
75 | self.OAR.getAddress().cbAddress()
76 |
77 | macro oraclize_setProof($proofP):
78 | oraclize = self.OAR.getAddress()
79 | oraclize.setProofType($proofP)
80 |
81 | macro oraclize_query($datasource, $arg):
82 | oraclize = self.OAR.getAddress()
83 | price = oraclize.getPrice($datasource,200000)
84 | if (price > 10^18 + tx.gasprice*200000):
85 | 0 # unexpectedly high price
86 | else:
87 | oraclize.query(0, $datasource, $arg, value=price)
88 |
89 | macro oraclize_query($timestamp, $datasource, $arg):
90 | oraclize = self.OAR.getAddress()
91 | if(len($timestamp)==0 && len($arg)>0):
92 | price = oraclize.getPrice($datasource,200000)
93 | if (price > 10^18 + tx.gasprice*200000):
94 | 0 # unexpectedly high price
95 | else:
96 | oraclize.query($timestamp, $datasource, $arg, value=price)
97 | elif(len($timestamp)>0 && len($arg)==0):
98 | # $timestamp is datasource
99 | # $datasource is arg
100 | # $arg is gasLimit
101 | price = oraclize.getPrice($timestamp, $arg)
102 | if (price > 10^18 + tx.gasprice*$arg):
103 | 0 # unexpectedly high price
104 | else:
105 | oraclize.query_withGasLimit(0, $timestamp, $datasource, $arg, value=price)
106 | else:
107 | # $timestamp is datasource
108 | # $datasource is arg
109 | # $arg is 2nd arg
110 | price = oraclize.getPrice($timestamp,200000)
111 | if (price > 10^18 + tx.gasprice*200000):
112 | 0 # unexpectedly high price
113 | else:
114 | oraclize.query2(0, $timestamp, $datasource, $arg, value=price)
115 |
116 | macro oraclize_query($timestamp, $datasource, $arg, $gasLimit):
117 | oraclize = self.OAR.getAddress()
118 | if(len($timestamp)==0 && len($gasLimit)==0):
119 | price = oraclize.getPrice($datasource, $gasLimit)
120 | if (price > 10^18 + tx.gasprice*$gasLimit):
121 | 0 # unexpectedly high price
122 | else:
123 | oraclize.query_withGasLimit($timestamp, $datasource, $arg, $gasLimit, value=price)
124 | elif(len($timestamp)>0 && len($gasLimit)==0):
125 | # $timestamp is datasource
126 | # $datasource is arg
127 | # $arg is the 2nd arg
128 | # $gasLimit is gasLimit
129 | price = oraclize.getPrice($timestamp, $gasLimit)
130 | if (price > 10^18 + tx.gasprice*$gasLimit):
131 | 0 # unexpectedly high price
132 | else:
133 | oraclize.query2_withGasLimit(0, $timestamp, $datasource, $arg, $gasLimit, value=price)
134 | elif(len($gasLimit)>0 && len($timestamp)==0):
135 | price = oraclize.getPrice($datasource,200000)
136 | if (price > 10^18 + tx.gasprice*200000):
137 | 0 # unexpectedly high price
138 | else:
139 | # $timestamp is timestamp
140 | # $datasource is datasource
141 | # $arg is arg
142 | # $gasLimit is 2nd arg
143 | oraclize.query2($timestamp, $datasource, $arg, $gasLimit, value=price)
144 |
145 | macro oraclize_query($timestamp, $datasource, $arg1, $arg2, $gasLimit):
146 | oraclize = self.OAR.getAddress()
147 | price = oraclize.getPrice($datasource, $gasLimit)
148 | if (price > 10^18 + tx.gasprice*$gasLimit):
149 | 0 # unexpectedly high price
150 | else:
151 | oraclize.query2_withGasLimit($timestamp, $datasource, $arg1, $arg2, $gasLimit, value=price)
152 |
153 | macro strConcat($a, $b, $c, $d, $e):
154 | with $z = string(len($a) + len($b) + len($c) + len($d) + len($e)):
155 | mcopy($z, $a, len($a))
156 | mcopy($z + len($a), $b, len($b))
157 | mcopy($z + len($a) + len($b), $c, len($c))
158 | mcopy($z + len($a) + len($b) + len($c), $d, len($d))
159 | mcopy($z + len($a) + len($b) + len($c) + len($d), $e, len($e))
160 | $z
161 |
162 | macro strConcat($a, $b, $c, $d):
163 | strConcat($a, $b, $c, $d, text(""))
164 |
165 | macro strConcat($a, $b, $c):
166 | strConcat($a, $b, $c, text(""), text(""))
167 |
168 | macro strConcat($a, $b):
169 | strConcat($a, $b, text(""), text(""), text(""))
170 |
171 | #
172 |
--------------------------------------------------------------------------------
/old-contracts/previous-api-contracts/oraclizeAPI_pre0.4.sol:
--------------------------------------------------------------------------------
1 | //
2 |
3 | /*
4 | IMPORTANT NOTICE: THIS ORACLIZE_API VERSION IS DEPRECATED AND IS NOT ACTIVELY MAINTAINED ANYMORE
5 | THIS IS STAYING HERE FOR COMPATIBILITY/HISTORICAL REASONS (WORKS WITH SOLC <0.4)
6 |
7 | PLEASE ALWAYS REFER TO THE LATEST CODE: https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI.sol
8 | */
9 |
10 | /*
11 | Copyright (c) 2015-2016 Oraclize SRL
12 | Copyright (c) 2016 Oraclize LTD
13 |
14 |
15 |
16 | Permission is hereby granted, free of charge, to any person obtaining a copy
17 | of this software and associated documentation files (the "Software"), to deal
18 | in the Software without restriction, including without limitation the rights
19 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 | copies of the Software, and to permit persons to whom the Software is
21 | furnished to do so, subject to the following conditions:
22 |
23 |
24 |
25 | The above copyright notice and this permission notice shall be included in
26 | all copies or substantial portions of the Software.
27 |
28 |
29 |
30 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 | THE SOFTWARE.
37 | */
38 |
39 | contract OraclizeI {
40 | address public cbAddress;
41 | function query(uint _timestamp, string _datasource, string _arg) returns (bytes32 _id);
42 | function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) returns (bytes32 _id);
43 | function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) returns (bytes32 _id);
44 | function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) returns (bytes32 _id);
45 | function getPrice(string _datasource) returns (uint _dsprice);
46 | function getPrice(string _datasource, uint gaslimit) returns (uint _dsprice);
47 | function useCoupon(string _coupon);
48 | function setProofType(byte _proofType);
49 | function setCustomGasPrice(uint _gasPrice);
50 | }
51 | contract OraclizeAddrResolverI {
52 | function getAddress() returns (address _addr);
53 | }
54 | contract usingOraclize {
55 | uint constant day = 60*60*24;
56 | uint constant week = 60*60*24*7;
57 | uint constant month = 60*60*24*30;
58 | byte constant proofType_NONE = 0x00;
59 | byte constant proofType_TLSNotary = 0x10;
60 | byte constant proofStorage_IPFS = 0x01;
61 | uint8 constant networkID_auto = 0;
62 | uint8 constant networkID_mainnet = 1;
63 | uint8 constant networkID_testnet = 2;
64 | uint8 constant networkID_morden = 2;
65 | uint8 constant networkID_consensys = 161;
66 |
67 | OraclizeAddrResolverI OAR;
68 |
69 | OraclizeI oraclize;
70 | modifier oraclizeAPI {
71 | if(address(OAR)==0) oraclize_setNetwork(networkID_auto);
72 | oraclize = OraclizeI(OAR.getAddress());
73 | _
74 | }//please import oraclizeAPI_0.4.sol when solidity >= 0.4.0
75 | modifier coupon(string code){
76 | oraclize = OraclizeI(OAR.getAddress());
77 | oraclize.useCoupon(code);
78 | _
79 | }
80 |
81 | function oraclize_setNetwork(uint8 networkID) internal returns(bool){
82 | if (getCodeSize(0x1d3b2638a7cc9f2cb3d298a3da7a90b67e5506ed)>0){ //mainnet
83 | OAR = OraclizeAddrResolverI(0x1d3b2638a7cc9f2cb3d298a3da7a90b67e5506ed);
84 | return true;
85 | }
86 | if (getCodeSize(0xc03a2615d5efaf5f49f60b7bb6583eaec212fdf1)>0){ //ropsten testnet
87 | OAR = OraclizeAddrResolverI(0xc03a2615d5efaf5f49f60b7bb6583eaec212fdf1);
88 | return true;
89 | }
90 | if (getCodeSize(0x90A0F94702c9630036FB9846B52bf31A1C991a84)>0){ //bsc mainnet
91 | OAR = OraclizeAddrResolverI(0x90A0F94702c9630036FB9846B52bf31A1C991a84);
92 | return true;
93 | }
94 | if (getCodeSize(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609)>0){ //polygon mainnet
95 | OAR = OraclizeAddrResolverI(0x816ec2AF1b56183F82f8C05759E99FEc3c3De609);
96 | return true;
97 | }
98 | if (getCodeSize(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85)>0){ //sepolia testnet
99 | OAR = OraclizeAddrResolverI(0x14B31A1C66a9f3D18DFaC2d123FE8cE5847b7F85);
100 | return true;
101 | }
102 | if (getCodeSize(0x51efaf4c8b3c9afbd5ab9f4bbc82784ab6ef8faa)>0){ //browser-solidity
103 | OAR = OraclizeAddrResolverI(0x51efaf4c8b3c9afbd5ab9f4bbc82784ab6ef8faa);
104 | return true;
105 | }
106 | return false;
107 | }
108 |
109 | function oraclize_query(string datasource, string arg) oraclizeAPI internal returns (bytes32 id){
110 | uint price = oraclize.getPrice(datasource);
111 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
112 | return oraclize.query.value(price)(0, datasource, arg);
113 | }
114 | function oraclize_query(uint timestamp, string datasource, string arg) oraclizeAPI internal returns (bytes32 id){
115 | uint price = oraclize.getPrice(datasource);
116 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
117 | return oraclize.query.value(price)(timestamp, datasource, arg);
118 | }
119 | function oraclize_query(uint timestamp, string datasource, string arg, uint gaslimit) oraclizeAPI internal returns (bytes32 id){
120 | uint price = oraclize.getPrice(datasource, gaslimit);
121 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
122 | return oraclize.query_withGasLimit.value(price)(timestamp, datasource, arg, gaslimit);
123 | }
124 | function oraclize_query(string datasource, string arg, uint gaslimit) oraclizeAPI internal returns (bytes32 id){
125 | uint price = oraclize.getPrice(datasource, gaslimit);
126 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
127 | return oraclize.query_withGasLimit.value(price)(0, datasource, arg, gaslimit);
128 | }
129 | function oraclize_query(string datasource, string arg1, string arg2) oraclizeAPI internal returns (bytes32 id){
130 | uint price = oraclize.getPrice(datasource);
131 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
132 | return oraclize.query2.value(price)(0, datasource, arg1, arg2);
133 | }
134 | function oraclize_query(uint timestamp, string datasource, string arg1, string arg2) oraclizeAPI internal returns (bytes32 id){
135 | uint price = oraclize.getPrice(datasource);
136 | if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price
137 | return oraclize.query2.value(price)(timestamp, datasource, arg1, arg2);
138 | }
139 | function oraclize_query(uint timestamp, string datasource, string arg1, string arg2, uint gaslimit) oraclizeAPI internal returns (bytes32 id){
140 | uint price = oraclize.getPrice(datasource, gaslimit);
141 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
142 | return oraclize.query2_withGasLimit.value(price)(timestamp, datasource, arg1, arg2, gaslimit);
143 | }
144 | function oraclize_query(string datasource, string arg1, string arg2, uint gaslimit) oraclizeAPI internal returns (bytes32 id){
145 | uint price = oraclize.getPrice(datasource, gaslimit);
146 | if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
147 | return oraclize.query2_withGasLimit.value(price)(0, datasource, arg1, arg2, gaslimit);
148 | }
149 | function oraclize_cbAddress() oraclizeAPI internal returns (address){
150 | return oraclize.cbAddress();
151 | }
152 | function oraclize_setProof(byte proofP) oraclizeAPI internal {
153 | return oraclize.setProofType(proofP);
154 | }
155 | function oraclize_setCustomGasPrice(uint gasPrice) oraclizeAPI internal {
156 | return oraclize.setCustomGasPrice(gasPrice);
157 | }
158 | function oraclize_setConfig(bytes config) oraclizeAPI internal {
159 | //return oraclize.setConfig(config);
160 | }
161 |
162 | function getCodeSize(address _addr) constant internal returns(uint _size) {
163 | assembly {
164 | _size := extcodesize(_addr)
165 | }
166 | }
167 |
168 |
169 | function parseAddr(string _a) internal returns (address){
170 | bytes memory tmp = bytes(_a);
171 | uint160 iaddr = 0;
172 | uint160 b1;
173 | uint160 b2;
174 | for (uint i=2; i<2+2*20; i+=2){
175 | iaddr *= 256;
176 | b1 = uint160(tmp[i]);
177 | b2 = uint160(tmp[i+1]);
178 | if ((b1 >= 97)&&(b1 <= 102)) b1 -= 87;
179 | else if ((b1 >= 48)&&(b1 <= 57)) b1 -= 48;
180 | if ((b2 >= 97)&&(b2 <= 102)) b2 -= 87;
181 | else if ((b2 >= 48)&&(b2 <= 57)) b2 -= 48;
182 | iaddr += (b1*16+b2);
183 | }
184 | return address(iaddr);
185 | }
186 |
187 |
188 | function strCompare(string _a, string _b) internal returns (int) {
189 | bytes memory a = bytes(_a);
190 | bytes memory b = bytes(_b);
191 | uint minLength = a.length;
192 | if (b.length < minLength) minLength = b.length;
193 | for (uint i = 0; i < minLength; i ++)
194 | if (a[i] < b[i])
195 | return -1;
196 | else if (a[i] > b[i])
197 | return 1;
198 | if (a.length < b.length)
199 | return -1;
200 | else if (a.length > b.length)
201 | return 1;
202 | else
203 | return 0;
204 | }
205 |
206 | function indexOf(string _haystack, string _needle) internal returns (int)
207 | {
208 | bytes memory h = bytes(_haystack);
209 | bytes memory n = bytes(_needle);
210 | if(h.length < 1 || n.length < 1 || (n.length > h.length))
211 | return -1;
212 | else if(h.length > (2**128 -1))
213 | return -1;
214 | else
215 | {
216 | uint subindex = 0;
217 | for (uint i = 0; i < h.length; i ++)
218 | {
219 | if (h[i] == n[0])
220 | {
221 | subindex = 1;
222 | while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex])
223 | {
224 | subindex++;
225 | }
226 | if(subindex == n.length)
227 | return int(i);
228 | }
229 | }
230 | return -1;
231 | }
232 | }
233 |
234 | function strConcat(string _a, string _b, string _c, string _d, string _e) internal returns (string){
235 | bytes memory _ba = bytes(_a);
236 | bytes memory _bb = bytes(_b);
237 | bytes memory _bc = bytes(_c);
238 | bytes memory _bd = bytes(_d);
239 | bytes memory _be = bytes(_e);
240 | string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
241 | bytes memory babcde = bytes(abcde);
242 | uint k = 0;
243 | for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
244 | for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
245 | for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
246 | for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
247 | for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
248 | return string(babcde);
249 | }
250 |
251 | function strConcat(string _a, string _b, string _c, string _d) internal returns (string) {
252 | return strConcat(_a, _b, _c, _d, "");
253 | }
254 |
255 | function strConcat(string _a, string _b, string _c) internal returns (string) {
256 | return strConcat(_a, _b, _c, "", "");
257 | }
258 |
259 | function strConcat(string _a, string _b) internal returns (string) {
260 | return strConcat(_a, _b, "", "", "");
261 | }
262 |
263 | // parseInt
264 | function parseInt(string _a) internal returns (uint) {
265 | return parseInt(_a, 0);
266 | }
267 |
268 | // parseInt(parseFloat*10^_b)
269 | function parseInt(string _a, uint _b) internal returns (uint) {
270 | bytes memory bresult = bytes(_a);
271 | uint mint = 0;
272 | bool decimals = false;
273 | for (uint i=0; i= 48)&&(bresult[i] <= 57)){
275 | if (decimals){
276 | if (_b == 0) break;
277 | else _b--;
278 | }
279 | mint *= 10;
280 | mint += uint(bresult[i]) - 48;
281 | } else if (bresult[i] == 46) decimals = true;
282 | }
283 | if (_b > 0) mint *= 10**_b;
284 | return mint;
285 | }
286 |
287 |
288 | }
289 | //
290 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "provable-eth-api",
3 | "version": "0.7.0",
4 | "description": "The Provable (formerly Oraclize) API - Importable in your smart-contract in order to start leveraging the Provable oracle service quickly and easily!.",
5 | "repository": {
6 | "type": "git",
7 | "url": "git+https://github.com/provable-things/ethereum-api.git"
8 | },
9 | "license": "MIT",
10 | "bugs": {
11 | "url": "https://github.com/provable-things/ethereum-api/issues"
12 | },
13 | "homepage": "http://provable.xyz",
14 | "devDependencies": {
15 | "hardhat": "^2.10.2"
16 | },
17 | "scripts": {
18 | "tests": "npm run test",
19 | "test": "./test/run-tests.sh"
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/test/run-tests.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -e
3 | cd "$(dirname -- $0)"
4 |
5 | if [ ! -d "../node_modules" ]; then
6 | echo '✔ Installing dependencies...'
7 | cd ../ && npm ci && cd "$(dirname -- $0)"
8 | echo '✔ Dependencies installed!'
9 | else
10 | echo '✔ Dependencies already installed ∴ skipping installation!'
11 | fi
12 |
13 | echo '✔ Running tests...'
14 |
15 | ./test-compilation.sh
16 |
--------------------------------------------------------------------------------
/test/test-compilation.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -e
3 | cd "$(dirname -- $0)"
4 |
5 | artifactsPath="../artifacts"
6 |
7 | if [ -d $artifactsPath ]; then
8 | echo '✔ Removing compiled artifacts dir...'
9 | rm -r $artifactsPath
10 | echo '✔ Artifacts dir removed!'
11 | fi
12 |
13 | echo '✔ Testing Provable API compilation...'
14 | npx hardhat compile
15 |
--------------------------------------------------------------------------------