Skip to content

Commit

Permalink
feat: bubble up errors to top level calls to handle
Browse files Browse the repository at this point in the history
  • Loading branch information
satello committed Oct 25, 2018
1 parent 52e7a83 commit 52b2278
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 42 deletions.
75 changes: 46 additions & 29 deletions src/standards/Arbitrable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 26 additions & 13 deletions src/utils/hashing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 52b2278

Please sign in to comment.