From 52b2278d33219af274eab04389e65c027dd4d34a Mon Sep 17 00:00:00 2001 From: Sam Vitello Date: Thu, 25 Oct 2018 15:45:36 -0600 Subject: [PATCH] feat: bubble up errors to top level calls to handle --- src/standards/Arbitrable.js | 75 +++++++++++++++++++++++-------------- src/utils/hashing.js | 39 ++++++++++++------- 2 files changed, 72 insertions(+), 42 deletions(-) diff --git a/src/standards/Arbitrable.js b/src/standards/Arbitrable.js index c3fafa8..70c1c6f 100644 --- a/src/standards/Arbitrable.js +++ b/src/standards/Arbitrable.js @@ -65,14 +65,21 @@ class Arbitrable extends StandardContract { customHashFn: options.customHashFn }) - const { isValid: fileValid } = evidenceJSON.fileURI - ? await validateFileFromURI(evidenceJSON.fileURI, { - evidence: true, - strictHashes: options.strictHashes, - hash: evidenceJSON.fileHash, - customHashFn: options.customHashFn - }) - : { isValid: null } + let fileValid + + try { + fileValid = evidenceJSON.fileURI + ? (await validateFileFromURI(evidenceJSON.fileURI, { + evidence: true, + strictHashes: options.strictHashes, + hash: evidenceJSON.fileHash, + customHashFn: options.customHashFn + })).isValid + : null + } catch (err) { + if (options.strictHashes) throw new Error(err) + fileValid = false + } const submittedAt = (await new Promise((resolve, reject) => { this.web3.eth.getBlock(evidenceLog.blockNumber, (error, result) => { @@ -147,29 +154,39 @@ class Arbitrable extends StandardContract { customHashFn: options.customHashFn }) - // validate file hash - const { isValid: fileValid } = metaEvidenceJSON.fileURI - ? await validateFileFromURI(metaEvidenceJSON.fileURI, { - evidence: true, - strictHashes: options.strictHashes, - hash: metaEvidenceJSON.fileHash, - customHashFn: options.customHashFn - }) - : { isValid: null } - - // validate file hash - const { - isValid: interfaceValid - } = metaEvidenceJSON.evidenceDisplayInterfaceURL - ? await validateFileFromURI( - metaEvidenceJSON.evidenceDisplayInterfaceURL, - { + let fileValid + try { + // validate file hash + fileValid = metaEvidenceJSON.fileURI + ? (await validateFileFromURI(metaEvidenceJSON.fileURI, { + evidence: true, strictHashes: options.strictHashes, - hash: metaEvidenceJSON.evidenceDisplayInterfaceHash, + hash: metaEvidenceJSON.fileHash, customHashFn: options.customHashFn - } - ) - : { isValid: null } + })).isValid + : null + } catch (err) { + if (options.strictHashes) throw new Error(err) + fileValid = false + } + + // validate file hash + let interfaceValid + try { + interfaceValid = metaEvidenceJSON.evidenceDisplayInterfaceURL + ? (await validateFileFromURI( + metaEvidenceJSON.evidenceDisplayInterfaceURL, + { + strictHashes: options.strictHashes, + hash: metaEvidenceJSON.evidenceDisplayInterfaceHash, + customHashFn: options.customHashFn + } + )).isValid + : { isValid: null } + } catch (err) { + if (options.strictHashes) throw new Error(err) + interfaceValid = false + } return { metaEvidenceJSON, diff --git a/src/utils/hashing.js b/src/utils/hashing.js index 83610be..0283106 100644 --- a/src/utils/hashing.js +++ b/src/utils/hashing.js @@ -44,22 +44,24 @@ export const validateFileFromURI = async ( selfHash = _selfHash } - let isValid = true - - if ( - !validMultihash( + let isValid + // If there is an error validating the hash just assume it is invalid so that legacy disputes can be fetched + try { + isValid = validMultihash( options.hash || selfHash || getURISuffix(fileURI), fileContent, options.customHashFn ) - ) { + } catch (err) { + console.error(err) isValid = false - if (options.strictHashes) - throw new Error( - errorConstants.VALIDATION_ERROR(`Evidence hash validation failed`) - ) } + if (!isValid && options.strictHashes) + throw new Error( + errorConstants.VALIDATION_ERROR(`Evidence hash validation failed`) + ) + return { file: fileContent, isValid @@ -80,14 +82,25 @@ export const validMultihash = ( ) => { if (typeof file === 'object') file = JSON.stringify(file) // Decode hash to get hashing algorithm - const decodedHash = multihash.decode(multihash.fromB58String(multihashHex)) + + let decodedHash + try { + decodedHash = multihash.decode(multihash.fromB58String(multihashHex)) + } catch (err) { + console.error(err) + throw new Error( + errorConstants.VALIDATION_ERROR( + 'Unable to decode multihash hex. Is your hash base58?' + ) + ) + } const hashFn = customHashFn || hashFunctions[decodedHash.code] if (!hashFn) throw new Error( - `Hash validation error: No hash function for multicode ${ - decodedHash.code - }` + errorConstants.VALIDATION_ERROR( + `No hash function for multicode ${decodedHash.code}` + ) ) // Hash the original object let fileHash = hashFn(file)