From 12040dd59f9b0f5eb53411dced47339b1c6f0f7d Mon Sep 17 00:00:00 2001 From: Sam Vitello Date: Fri, 13 Jul 2018 15:39:50 -0600 Subject: [PATCH] feat: submit evidence uri use evidenceIndex --- src/contracts/abstractions/Arbitrable.js | 24 +++++++++++------ .../arbitrable/ArbitrableTransaction.js | 1 + src/utils/StoreProviderWrapper.js | 27 ++++++++++++------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/contracts/abstractions/Arbitrable.js b/src/contracts/abstractions/Arbitrable.js index fd4658b..d150dfa 100644 --- a/src/contracts/abstractions/Arbitrable.js +++ b/src/contracts/abstractions/Arbitrable.js @@ -41,7 +41,7 @@ class ArbitrableContract extends AbstractContract { const web3Provider = this._contractImplementation.getWeb3Provider() // determine the contract address WARNING if the nonce changes this will produce a different address const contractAddress = getContractAddress(account, web3Provider) - const metaEvidenceUri = this._StoreProvider.getMetaEvidenceUri(contractAddress) + const metaEvidenceUri = this._StoreProvider.getMetaEvidenceUri(account, contractAddress) const contractInstance = await this._contractImplementation.constructor.deploy( account, @@ -79,29 +79,37 @@ class ArbitrableContract extends AbstractContract { } /** - * Submit evidence. + * Submit evidence. FIXME should we determine the hash for the user? * @param {string} account - ETH address of user. * @param {string} name - Name of evidence. * @param {string} description - Description of evidence. * @param {string} url - A link to an evidence using its URI. + * @param {string} hash - A hash of the evidence at the URI. No hash if content is dynamic * @returns {string} - txHash Hash transaction. */ - submitEvidence = async (account, name, description = '', url) => { - const txHash = await this._contractImplementation.submitEvidence( + submitEvidence = async (account, name, description, url, hash) => { + const contractAddress = this._contractImplementation.contractAddress + // get the index of the new evidence + const evidenceIndex = await this._StoreProvider.addEvidenceContract( + contractAddress, account, name, description, - url + url, + hash ) + // construct the unique URI + const evidenceUri = this._StoreProvider.getEvidenceUri(account, contractAddress, evidenceIndex) - await this._StoreProvider.addEvidenceContract( - this._contractImplementation.contractAddress, + const txHash = await this._contractImplementation.submitEvidence( account, name, description, - url + evidenceUri ) + + return txHash } diff --git a/src/contracts/implementations/arbitrable/ArbitrableTransaction.js b/src/contracts/implementations/arbitrable/ArbitrableTransaction.js index 97d0f08..49b8777 100644 --- a/src/contracts/implementations/arbitrable/ArbitrableTransaction.js +++ b/src/contracts/implementations/arbitrable/ArbitrableTransaction.js @@ -116,6 +116,7 @@ class ArbitrableTransaction extends ContractImplementation { { _disputeID: disputeId, _arbitrator: arbitratorAddress } ) + // TODO verify hash and data are valid if hash exists return evidenceLogs.map(async evidenceLog => { const evidenceURI = evidenceLog.args._evidence const evidence = await httpRequest( diff --git a/src/utils/StoreProviderWrapper.js b/src/utils/StoreProviderWrapper.js index f7e2da7..88b65d5 100644 --- a/src/utils/StoreProviderWrapper.js +++ b/src/utils/StoreProviderWrapper.js @@ -39,8 +39,12 @@ class StoreProviderWrapper { this._storeQueue.fetch(() => httpRequest('GET', uri)) - getMetaEvidenceUri = address => ( - `${this._storeUri}/${userAddress}/contracts/${address}/meta-evidence` + getMetaEvidenceUri = userAddress, contractAddress => ( + `${this._storeUri}/${userAddress}/contracts/${contractAddress}/meta-evidence` + ) + + getEvidenceUri = (address, contractAddress, evidenceIndex) => ( + `${this._storeUri}/${userAddress}/contracts/${contractAddress}/evidence/evidenceIndex` ) // **************************** // @@ -239,35 +243,38 @@ class StoreProviderWrapper { * @param {string} name - Name of evidence. * @param {string} description - Description of evidence. * @param {string} url - A link to the evidence. - * @returns {Promise} - The resulting evidence data. + * @returns {number} - The index of the evidence */ addEvidenceContract = ( contractAddress, userAddress, name, description, - url + url, + hash ) => { - // get timestamp for submission - const submittedAt = new Date().getTime() - const getBodyFn = () => new Promise(resolve => resolve( JSON.stringify({ name, description, - url, - submittedAt + URI: url, + hash }) ) ) - return this.queueWriteRequest( + const response = await this.queueWriteRequest( getBodyFn, 'POST', `${this._storeUri}/${userAddress}/contracts/${contractAddress}/evidence` ) + + if (response.status !== 200) + throw new Error(errorConstants.REQUEST_FAILED('Unable to submit evidence') + + return response.body.evidenceIndex } /**