Creating record on the db to be picked up by background service

This commit is contained in:
Richard Ramos 2018-10-04 13:31:50 -04:00
parent e93b1675b8
commit 750d84a175
2 changed files with 61 additions and 38 deletions

View File

@ -24,7 +24,6 @@ class App extends React.Component {
constructor(props) {
super(props);
this.state = {
error: false,
errorMessage: '',
@ -32,7 +31,6 @@ class App extends React.Component {
};
}
componentDidMount(){
EmbarkJS.onReady(async (error) => {
@ -52,7 +50,7 @@ class App extends React.Component {
ipfs.on('ready', async () => {
try {
const orbitdb = new OrbitDB(ipfs);
start(orbitdb);
await this.start(orbitdb);
} catch (error) {
this.setState({error: true, errorMessage: "Error loading the DAPP - Contact your nearest Status Core Developer"});
console.error(error);
@ -62,8 +60,16 @@ class App extends React.Component {
});
}
async redirectIfProcessed(code){
const sentToAddress = await SNTGiveaway.methods.sentToAddress(web3.eth.defaultAccount).call();
const usedCode = await SNTGiveaway.methods.codeUsed( '0x' + code,).call();
if(sentToAddress || usedCode){
window.location = "http://status.im"; // TODO: redirect to ENS Registration DAPP URL
}
}
async start(orbitdb){
const code = location.hash.replace("#"); // QR code value, i.e.: a8cd4b33bc
const code = location.hash.replace("#", ''); // QR code value, i.e.: a8cd4b33bc
const accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0];
@ -72,21 +78,24 @@ class App extends React.Component {
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 hashedCode = sha3('0x' + code);
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
let proof;
try {
proof = merkleTree.getProof(hashedCode);
} catch(error){
this.setState({error: true, errorMessage: "Invalid Code"});
console.log(error);
return;
}
this.redirectIfProcessed(code);
const validRequest = await SNTGiveaway.methods.validRequest(proof, '0x' + code, web3.eth.defaultAccount).call();
if(!validRequest){
this.setState({error: true, errorMessage: "Invalid Code"});
console.error("Code not found in contract's merkle root");
return;
}
@ -94,29 +103,43 @@ class App extends React.Component {
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
const transaction = await transactionsDb.get(code);
if(!transaction){
const fundRequestsDB = await orbitdb.log("/orbitdb/QmQ7dJmRiCwWGNgjDoUKX9xhdVA8vcS92R1h8S4uj1sm6v/status-hackathon-fund-requests2");
await fundRequestsDB.load();
const record = {
code,
address: web3.eth.defaultAccount,
proof: proof.map(x => '0x' + x.toString('hex'))
}
const hash = await fundRequestsDB.add(record)
console.log(hash);
// TODO: avoid people spamming with localstorage
}
// 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.redirectIfProcessed(code);
});
this.setState({ready: true});
}
render(){
// TODO: state.error == true, show message
// TODO: state.ready == show result?
return <h1>Hello World</h1>
const {error, errorMessage, ready} = this.state;
return <Fragment>
{ error && <h2>{errorMessage}</h2> }
{ !ready && !error && <h2>Add a loading message / indicator that the dapp is checking whether the code is valid or not </h2> }
{ ready && <h2>Add a message indicating that the code has been received, and we're processing the trx</h2> }
</Fragment>
}
}

View File

@ -1,16 +1,16 @@
module.exports = {
merkleRoot: '0x96c8e9fb478bf43f8318f07efee57abe6a68a18a5272f3faf72541d9d40f5d6f',
merkleRoot: '0xa6bd1cdde9c5fe6db1dd812433bf6ebe41f6e259b8892c8c92a258227bbf93ad',
elements: [
'0x9f1ac28e80f76d8e8efb505d27f8ba4fb5e01346e93831cf84f797fcc3e9460e',
'0x4ef2f1158e4768187b2b96c5237c9a2f9ce759dba7e38a71b1dc50c5f28dff8f',
'0x4a5d99667f6d19d9ef1bd10297e72056d7e5abd15ff5c6ec8a0b7c5207ed3098',
'0x194045822500d6b14288ed030e83ce30e4575b6088834e40d329a7e941683de3',
'0xda0a712ed84148058af21cf9279e7d361bfd00b5577da5999abbdb84a9aa37f6',
'0xf2ab923cf961b882baf26975ded9de8455424b94953a63afc6c05eab5789329a',
'0xaa9cfa97553045e79dc59cd1735c84eb3fb44570a6140034f9790f484bdbe203',
'0xe9d6063d3a65f26651f1f50a3ae57318ba101cdab52c0b620276220d0307f32d',
'0x828236a898cdaf046410c315d5dd1ae850752951e352420bac97293cf97bb56e',
'0xb4c110e5d58e713c33d9048f531f1446dec82dac9d63d360aac1bf4c51050349'
'0x658a5f41ccf71065d4cedabe8458ceabdf6a7d508737d5078010efb772fa8249',
'0x291bacb9dfffd3dbc60a9e11b2eb6dbe9f0e99b568b1e19c9f96b77cf82f9b7f',
'0x45558eec332150201515162cb3a6c8ff546706a7c457bf7bab7803e725bed712',
'0x71ec3dbe5325c9bc7c8f040980e9d870d067fb321c194b54461de278426d1256',
'0xa9e19ca4a996ba26e1deae04a09ef4fe0703016ac84d7f9f95eece0a28b6d324',
'0xffa57ce0eab2a1821ad7413e2845ad71abe450018c59929c20ec191405ca7a7f',
'0x46c363009a53928a040f83fc5cb1866d3508605cb588058611f1e5e20ef15a50',
'0xd97558a75d18180cc7ccb8b9e4667969836c6d64fb29ef30fe98462f37679f29',
'0x90e0369cb3f7025517a50d074e721327ab57a1c49441144ad002557b62d8d725',
'0x3c806f847adc5a77c4eb02900e8a8e00e060451936b46f43ffed94a7096b431e'
]
};