Allow entry of url or hash for DappReg meta (#5360)

* Allow DappReg to handle contentHash & url inputs

* Add tests
This commit is contained in:
Jaco Greeff 2017-03-31 23:33:55 +02:00 committed by Gav Wood
parent 73dc52980e
commit cbaa7fdee6
2 changed files with 42 additions and 3 deletions

View File

@ -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;
};

View File

@ -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 <http://www.gnu.org/licenses/>.
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;
});
});
});