Verifying with orbit if the code is being processed

This commit is contained in:
Richard Ramos 2018-10-04 11:21:43 -04:00
parent 6fd1d2b53a
commit e93b1675b8
2 changed files with 91 additions and 40 deletions

View File

@ -7,6 +7,15 @@ import { sha3 } from 'ethereumjs-util';
import merkle from 'merkle-tree-solidity';
import SNTGiveaway from 'Embark/contracts/SNTGiveaway';
import SNT from 'Embark/contracts/SNT';
import IPFS from 'ipfs';
import OrbitDB from 'orbit-db';
import { start } from 'ipfs/src/core/components';
const ipfsOptions = {
EXPERIMENTAL: {
pubsub: true
}
};
window.SNTGiveaway = SNTGiveaway;
window.SNT = SNT;
@ -18,54 +27,95 @@ class App extends React.Component {
this.state = {
error: false,
errorMessage: ''
errorMessage: '',
ready: false
};
}
componentDidMount(){
EmbarkJS.onReady(async (error) => {
if(error){
alert("Error loading the DAPP");
return;
}
const code = location.hash.replace("#"); // QR code value, i.e.: a8cd4b33bc
const accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0];
if(!code) {
this.setState({error: true, errorMessage: "Code is required"});
return;
}
const merkleTree = new merkle(merkleData.elements);
const hashedCode = sha3(code); // Code converted to format used on merkletree
const proof = merkleTree.getProof(hashedCode);
if(error){
this.setState({error: true, errorMessage: "Error loading the DAPP - Contact your nearest Status Core Developer"});
console.error(error);
return;
}
const ipfs = new IPFS(ipfsOptions)
// TODO: Call the contract function validRequest(bytes32[] _proof, bytes5 _code, address _dest)
// The values to send are, a merkle proof, code, and the web3.eth.defaultAccount.
// The merkle proof function uses a bytes32 hash, so you need to use hashedCode
// TODO: if result is false, show error or something because the code is invalid or has been used
// TODO: you may use the sentToAddress(web3.eth.defaultAddress) or codeUsed("0x" + code) to identify as well if the code / address has been used before, and redirect dierctly to ENS
// TODO: if result is true the code hasn't been processed. Proceed to verify if the background service will process that transaction
// TODO: use orbit db, check if exist a value in a orbitdb keyvalue store `status-hackathon-transactions2` Address of db can be seen by executing node server/main.js
// the key to use is the code
// TODO: if a value exists, the background service will process this code, show a message indicating that transaction is being processed
// TODO: if it doesnt,
// let's add a value to the status-fund-requests2 orbitdb log store, with a json containing the data used to invoke the validRequest contract function (all values must be strings): {code, proof: proof: proof.map(x => '0x' + x.toString('hex')), address}
// show a message indicating that transaction is being processed
//
ipfs.on('error', (e) => {
this.setState({error: true, errorMessage: "Error loading the DAPP - Contact your nearest Status Core Developer"});
console.error(e);
});
// We'll deal with the redirect to ENS Registration after the previous code is implemented, and the background service is done processing the transaction
ipfs.on('ready', async () => {
try {
const orbitdb = new OrbitDB(ipfs);
start(orbitdb);
} catch (error) {
this.setState({error: true, errorMessage: "Error loading the DAPP - Contact your nearest Status Core Developer"});
console.error(error);
return;
}
});
});
}
async start(orbitdb){
const code = location.hash.replace("#"); // QR code value, i.e.: a8cd4b33bc
const accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0];
if(!code) {
this.setState({error: true, errorMessage: "Code is required"});
return;
}
const merkleTree = new merkle(merkleData.elements);
const hashedCode = sha3(code); // Code converted to format used on merkletree
const proof = merkleTree.getProof(hashedCode);
const sentToAddress = await contract.methods.sentToAddress(web3.eth.defaultAccount).call();
const usedCode = await contract.methods.usedCode( '0x' + code,).call();
const validRequest = await contract.methods.validRequest(proof, '0x' + code, web3.eth.defaultAccount).call();
if(sentToAddress || usedCode){
window.location = "http://status.im"; // TODO: redirect to ENS Registration DAPP URL
}
if(!validRequest){
this.setState({error: true, errorMessage: "Invalid Code"});
return;
}
// Create / Open a database
const transactionsDb = await orbitdb.keyvalue("/orbitdb/QmVL48QDwdagfGnwcasTxuGTynTxpzJbiZVgLwoiW7Cg7e/status-hackathon-transactions2");
await transactionsDb.load();
const transaction = await transactionStore.get(code);
if(transaction){
// TODO: show a message indicating that transaction is already being processed
} else {
// TODO: let's add a value to the status-fund-requests2 orbitdb log store, with a json containing the data used to invoke the validRequest contract function (all values must be strings): {code, proof: proof: proof.map(x => '0x' + x.toString('hex')), address}
// show a message indicating that transaction is being processed
// /orbitdb/QmQ7dJmRiCwWGNgjDoUKX9xhdVA8vcS92R1h8S4uj1sm6v/status-hackathon-fund-requests2
}
// Listen for updates
transactionsDb.events.on('replicated', (address) => {
// TODO: trigger check
// TODO: We'll deal here with the redirect to ENS Registration. Verify if the transaction is processed and redirect or something
});
this.setState({ready: true});
}
render(){
// TODO: manage errors
// TODO: state.error == true, show message
// TODO: state.ready == show result?
return <h1>Hello World</h1>
}
}

View File

@ -23,16 +23,17 @@
"@babel/plugin-syntax-import-meta": "^7.0.0"
},
"dependencies": {
"crypto": "^1.0.1",
"ethereumjs-util": "^5.2.0",
"ipfs-api": "^24.0.2",
"merkle-tree-solidity": "^1.0.8",
"orbit-db": "^0.19.9",
"web3": "^1.0.0-beta.34",
"@material-ui/core": "^3.0.0",
"@material-ui/icons": "^3.0.0",
"@material-ui/lab": "^1.0.0-alpha.12",
"crypto": "^1.0.1",
"ethereumjs-util": "^5.2.0",
"ipfs": "^0.32.3",
"ipfs-api": "^24.0.2",
"merkle-tree-solidity": "^1.0.8",
"orbit-db": "^0.19.9",
"react": "^16.4.2",
"react-dom": "^16.4.2"
"react-dom": "^16.4.2",
"web3": "^1.0.0-beta.34"
}
}