diff --git a/js/src/dapps/dappreg/utils.js b/js/src/dapps/dappreg/utils.js index 4e13373e1..74ece1b95 100644 --- a/js/src/dapps/dappreg/utils.js +++ b/js/src/dapps/dappreg/utils.js @@ -19,6 +19,10 @@ import { api } from './parity'; export const INVALID_URL_HASH = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; +export function isContentHash (url) { + return /^0x[0-9a-f]{64}/.test(url); +} + /** * Convert the given URL to a content hash, * and checks if it is already registered in GHH @@ -28,8 +32,11 @@ export const urlToHash = (api, instance, url) => { return Promise.resolve(null); } - return api.parity - .hashContent(url) + const hashPromise = isContentHash(url) + ? Promise.resolve(url) + : api.parity.hashContent(url); + + return hashPromise .catch((error) => { const message = error.text || error.message || error.toString(); @@ -181,4 +188,3 @@ export const updateDapp = (dappId, dappOwner, updates, dappRegInstance, ghhRegIn return promises; }; - diff --git a/js/src/dapps/dappreg/utils.spec.js b/js/src/dapps/dappreg/utils.spec.js new file mode 100644 index 000000000..6101ff976 --- /dev/null +++ b/js/src/dapps/dappreg/utils.spec.js @@ -0,0 +1,33 @@ +// Copyright 2015-2017 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { INVALID_URL_HASH, ZERO_ADDRESS, isContentHash } from './utils'; + +describe('dapps/dappreg/utils', () => { + describe('isContentHash', () => { + it('returns true on valid hashes', () => { + expect(isContentHash(INVALID_URL_HASH)).to.be.true; + }); + + it('returns false on valid hex, invalid hash', () => { + expect(isContentHash(ZERO_ADDRESS)).to.be.false; + }); + + it('returns false on invalid hex', () => { + expect(isContentHash('something')).to.be.false; + }); + }); +});