-
Notifications
You must be signed in to change notification settings - Fork 24
ups package tracker #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Frippo40
wants to merge
5
commits into
iExecBlockchainComputing:master
Choose a base branch
from
Frippo40:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| **/iexec_in | ||
| **/iexec_out | ||
| ups-package-tracker/app/node_modules | ||
| ups-package-tracker/smart-contract/node_modules | ||
| ups-package-tracker/build |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,59 @@ | ||
| #!/usr/bin/python3 | ||
| import shutil | ||
| import json | ||
| import os | ||
| from py_essentials.hashing import fileChecksum | ||
| from web3.auto import w3 | ||
| ### DEBUG | ||
| # enclave private = "0x564a9db84969c8159f7aa3d5393c5ecd014fce6a375842a45b12af6677b12407" | ||
| # enclave address = 0x3cB738D98D7A70e81e81B0811Fae2452BcA049Bc | ||
| # digest = w3.soliditySha3([ 'string' ], [ 'iExec the wanderer' ]).hex() | ||
| if __name__ == '__main__': | ||
| taskid = os.environ.get('TASKID' ) # or '0x8b43265c231bd32d1b249c1ce58bf0c77a9ebc6a30da37e961063a073a921e06' | ||
| worker = os.environ.get('WORKER' ) # or '0x748e091bf16048cb5103E0E10F9D5a8b7fBDd860' | ||
| keyfile = os.environ.get('KEYFILE') or '/app/priv_key' | ||
| if not taskid: raise ValueError('Missing TASKID') | ||
| if not w3.isAddress(worker): raise ValueError('Invalid worker address') | ||
| worker = w3.toChecksumAddress(worker) | ||
| print("Genrating result and consensus.iexec in /iexec ...") | ||
| shutil.copy("/app/result.txt", "/iexec/result.txt") | ||
| shutil.copy("/app/result.txt", "/iexec/consensus.iexec") | ||
| with open(keyfile) as f: | ||
| private = f.read().splitlines()[0] | ||
| digest = "0x" + fileChecksum("/iexec/consensus.iexec", "sha256") # hexstring | ||
| hash = w3.soliditySha3([ 'bytes32', 'bytes32' ], [ taskid, digest ]) | ||
| seal = w3.soliditySha3([ 'address', 'bytes32', 'bytes32' ], [ worker, taskid, digest ]) | ||
| contrib = w3.soliditySha3([ 'bytes32', 'bytes32' ], [ hash, seal ]) | ||
| message = w3.soliditySha3([ 'bytes' ], [ b'\x19Ethereum Signed Message:\n32' + contrib ]) | ||
| signature = w3.eth.account.signHash(message, private) | ||
| with open("/iexec/enclaveSig.iexec", 'w') as f: | ||
| json.dump({ | ||
| 'digest': digest, | ||
| 'hash': w3.toHex(hash), | ||
| 'seal': w3.toHex(seal), | ||
| 'sign': | ||
| { | ||
| 'v': signature.v, | ||
| 'r': w3.toHex(signature.r), | ||
| 's': w3.toHex(signature.s) | ||
| } | ||
| }, f, ensure_ascii=False) | ||
| print("------ Additional log -------") | ||
| print("Listing files in /iexec:") | ||
| for entry in os.listdir("/iexec"): | ||
| print("- {}".format(entry)) | ||
| print("Content of /iexec/enclaveSig.iexec") | ||
| with open("/iexec/enclaveSig.iexec") as f: | ||
| for line in f: | ||
| print(f, end='') | ||
| #!/usr/bin/python3 | ||
| import shutil | ||
| import json | ||
| import os | ||
|
|
||
| from py_essentials.hashing import fileChecksum | ||
| from web3.auto import w3 | ||
|
|
||
| ### DEBUG | ||
| # enclave private = "0x564a9db84969c8159f7aa3d5393c5ecd014fce6a375842a45b12af6677b12407" | ||
| # enclave address = 0x3cB738D98D7A70e81e81B0811Fae2452BcA049Bc | ||
| # digest = w3.soliditySha3([ 'string' ], [ 'iExec the wanderer' ]).hex() | ||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| taskid = os.environ.get('TASKID' ) # or '0x8b43265c231bd32d1b249c1ce58bf0c77a9ebc6a30da37e961063a073a921e06' | ||
| worker = os.environ.get('WORKER' ) # or '0x748e091bf16048cb5103E0E10F9D5a8b7fBDd860' | ||
| keyfile = os.environ.get('KEYFILE') or '/app/priv_key' | ||
|
|
||
| if not taskid: raise ValueError('Missing TASKID') | ||
| if not w3.isAddress(worker): raise ValueError('Invalid worker address') | ||
|
|
||
| worker = w3.toChecksumAddress(worker) | ||
|
|
||
| print("Genrating result and consensus.iexec in /iexec ...") | ||
| shutil.copy("/app/result.txt", "/iexec/result.txt") | ||
| shutil.copy("/app/result.txt", "/iexec/consensus.iexec") | ||
|
|
||
| with open(keyfile) as f: | ||
| private = f.read().splitlines()[0] | ||
|
|
||
| digest = "0x" + fileChecksum("/iexec/consensus.iexec", "sha256") # hexstring | ||
| hash = w3.soliditySha3([ 'bytes32', 'bytes32' ], [ taskid, digest ]) | ||
| seal = w3.soliditySha3([ 'address', 'bytes32', 'bytes32' ], [ worker, taskid, digest ]) | ||
| contrib = w3.soliditySha3([ 'bytes32', 'bytes32' ], [ hash, seal ]) | ||
| message = w3.soliditySha3([ 'bytes' ], [ b'\x19Ethereum Signed Message:\n32' + contrib ]) | ||
| signature = w3.eth.account.signHash(message, private) | ||
|
|
||
| with open("/iexec/enclaveSig.iexec", 'w') as f: | ||
| json.dump({ | ||
| 'digest': digest, | ||
| 'hash': w3.toHex(hash), | ||
| 'seal': w3.toHex(seal), | ||
| 'sign': | ||
| { | ||
| 'v': signature.v, | ||
| 'r': w3.toHex(signature.r), | ||
| 's': w3.toHex(signature.s) | ||
| } | ||
| }, f, ensure_ascii=False) | ||
|
|
||
| print("------ Additional log -------") | ||
| print("Listing files in /iexec:") | ||
| for entry in os.listdir("/iexec"): | ||
| print("- {}".format(entry)) | ||
| print("Content of /iexec/enclaveSig.iexec") | ||
| with open("/iexec/enclaveSig.iexec") as f: | ||
| for line in f: | ||
| print(f, end='') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ./smart-contract/build | ||
| ./smart-contract/daemon/node_modules | ||
| ./smart-contract/node_modules | ||
| ./app/node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| FROM node:11-alpine | ||
| COPY package-tracker.js /src/package-tracker.js | ||
| COPY entrypoint.sh /entrypoint.sh | ||
| RUN npm i https ethers fs | ||
| RUN chmod +x /entrypoint.sh | ||
| ENTRYPOINT ["/entrypoint.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #!/bin/sh | ||
| node src/package-tracker.js $@ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| const https = require('https'); | ||
| const ethers = require('ethers'); | ||
| const fs = require('fs'); | ||
|
|
||
| const root = 'iexec_out'; | ||
| const determinismFilePath = `${root}/determinism.iexec`; | ||
| const callbackFilePath = `${root}/callback.iexec`; | ||
| const errorFilePath = `${root}/error.iexec`; | ||
|
|
||
|
|
||
| /***************************************************************************** | ||
| * TOOLS * | ||
| *****************************************************************************/ | ||
| const sleep = (ms) => { | ||
| return new Promise(resolve => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| const cat = (path) => { | ||
| try { return fs.readFileSync(path).toString(); } catch (e) { return null; } | ||
| } | ||
|
|
||
| /***************************************************************************** | ||
| * CONFIG * | ||
| *****************************************************************************/ | ||
|
|
||
| // UPS api key | ||
| const APIKEY = '7D6C7B558EF337F2'; | ||
|
|
||
| // random delay | ||
| const WAIT_MIN = parseInt(process.env.WAIT_MIN) || 0; // in ms | ||
| const WAIT_MAX = parseInt(process.env.WAIT_MAX) || 0; // in ms | ||
|
|
||
| /***************************************************************************** | ||
| * ARGUMENTS * | ||
| *****************************************************************************/ | ||
|
|
||
| var [ UPS_username, UPS_password, trackingNumber ] = process.argv.slice(2); | ||
|
|
||
| /***************************************************************************** | ||
| * HTTP QUERY * | ||
| *****************************************************************************/ | ||
|
|
||
| const post_data = JSON.stringify({ | ||
| "UPSSecurity": { | ||
| "UsernameToken": { | ||
| "Username": UPS_username, | ||
| "Password": UPS_password | ||
| }, | ||
| "ServiceAccessToken": { | ||
| "AccessLicenseNumber": APIKEY | ||
| } | ||
| }, | ||
| "TrackRequest": { | ||
| "Request": { | ||
| "RequestOption": "1", | ||
| "TransactionReference": { | ||
| "CustomerContext": "Your Test Case Summary Description" | ||
| } | ||
| }, | ||
| "InquiryNumber": trackingNumber | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| const options = { | ||
| method: 'POST', | ||
| host: 'onlinetools.ups.com', | ||
| path: '/rest/Track', | ||
| headers: {'Content-Type': 'application/json', 'Content-Length': post_data.length}, | ||
| }; | ||
|
|
||
| /***************************************************************************** | ||
| * EXECUTE * | ||
| *****************************************************************************/ | ||
| new Promise(async (resolve, reject) => { | ||
|
|
||
| const delay = (WAIT_MAX-WAIT_MIN) * Math.random() + WAIT_MIN; | ||
| console.log(`- Waiting for ${delay} ms.`); | ||
| await sleep(delay); | ||
|
|
||
| console.log(`making request`); | ||
| let chunks = []; | ||
| let request = https.request(options, res => { | ||
| res.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
| res.on('end', () => { | ||
| if (chunks.length) | ||
| { | ||
| resolve(chunks.join('')); | ||
| } | ||
| else | ||
| { | ||
| reject(`[HTTP ERROR]\nstatusCode: ${res.statusCode}`); | ||
| } | ||
| }); | ||
| }); | ||
| request.write(post_data); | ||
| request.on('error', reject); | ||
| request.end(); | ||
| }) | ||
| .then(data => { | ||
| console.log("parsing JSON"); | ||
| let results = JSON.parse(data.toString()); | ||
| // console.log(results); | ||
|
|
||
| if (results.Fault !== undefined) | ||
| { | ||
| console.log("error"); | ||
| throw new Error(JSON.stringify(results.Fault.detail)); | ||
| } | ||
| let deliveryStatus = results.TrackResponse.Shipment.Package.Activity[0].Status.Description; | ||
|
|
||
| var iexeccallback = ethers.utils.defaultAbiCoder.encode(['string'], [deliveryStatus]); | ||
| var iexecconsensus = ethers.utils.keccak256(iexeccallback); | ||
| fs.writeFile(callbackFilePath, iexeccallback , (err) => {}); | ||
| fs.writeFile(determinismFilePath, iexecconsensus, (err) => {}); | ||
|
|
||
| console.log(`- Success: ${deliveryStatus}`); | ||
| }) | ||
| .catch(error => { | ||
| fs.writeFile( | ||
| errorFilePath, | ||
| error.toString(), | ||
| (err) => {} | ||
| ); | ||
| fs.writeFile( | ||
| determinismFilePath, | ||
| ethers.utils.solidityKeccak256(['string'],[error.toString()]), | ||
| (err) => {} | ||
| ); | ||
| console.log(error.toString()); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How many possible delivery status are there? Wouldn't it make more sens to create an enum describing them, and return an uint256? This would reduce the gas costs and improve usability of the oracle