'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _slicedToArray = function () {function sliceIterator(arr, i) {var _arr = [];var _n = true;var _d = false;var _e = undefined;try {for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {_arr.push(_s.value);if (i && _arr.length === i) break;}} catch (err) {_d = true;_e = err;} finally {try {if (!_n && _i["return"]) _i["return"]();} finally {if (_d) throw _e;}}return _arr;}return function (arr, i) {if (Array.isArray(arr)) {return arr;} else if (Symbol.iterator in Object(arr)) {return sliceIterator(arr, i);} else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}(); // 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/>.
exports.


















subscribeToChanges = subscribeToChanges;exports.














































fetchBuiltinApps = fetchBuiltinApps;exports.


















fetchLocalApps = fetchLocalApps;exports.















fetchRegistryAppIds = fetchRegistryAppIds;exports.























fetchRegistryApp = fetchRegistryApp;exports.







































fetchManifest = fetchManifest;var _bignumber = require('bignumber.js');var _bignumber2 = _interopRequireDefault(_bignumber);var _lodash = require('lodash');var _format = require('@parity/api/lib/util/format');var _ui = require('@parity/ui');var _dappsBuiltin = require('../config/dappsBuiltin.json');var _dappsBuiltin2 = _interopRequireDefault(_dappsBuiltin);var _dappsViews = require('../config/dappsViews.json');var _dappsViews2 = _interopRequireDefault(_dappsViews);var _contracts = require('../contracts');var _contracts2 = _interopRequireDefault(_contracts);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var builtinApps = [].concat(_dappsViews2.default.map(function (app) {app.isView = true;return app;}), _dappsBuiltin2.default.filter(function (app) {return app.id;}));function subscribeToChanges(api, dappReg, callback) {return dappReg.getContract().then(function (dappRegContract) {var dappRegInstance = dappRegContract.instance;var signatures = ['MetaChanged', 'OwnerChanged', 'Registered'].map(function (event) {return dappRegInstance[event].signature;});return api.eth.newFilter({ fromBlock: '0', toBlock: 'latest', address: dappRegInstance.address, topics: [signatures] }).then(function (filterId) {return api.subscribe('eth_blockNumber', function () {if (filterId > -1) {api.eth.getFilterChanges(filterId).then(function (logs) {return dappRegContract.parseEventLogs(logs);}).then(function (events) {if (events.length === 0) {return [];} // Return uniq IDs which changed meta-data
            var ids = (0, _lodash.uniq)(events.map(function (event) {return (0, _format.bytesToHex)(event.params.id.value);}));callback(ids);});}}).then(function (blockSubId) {return { block: blockSubId, filter: filterId };});});});}function fetchBuiltinApps(api) {var _Contracts$get = _contracts2.default.get(api),dappReg = _Contracts$get.dappReg;return Promise.all(builtinApps.map(function (app) {return dappReg.getImage(app.id);})).then(function (imageIds) {return builtinApps.map(function (app, index) {app.type = app.isView ? 'view' : 'builtin';app.image = _ui.IconCache.hashToImage(imageIds[index]);return app;});}).catch(function (error) {console.warn('DappsStore:fetchBuiltinApps', error);});}function fetchLocalApps(api) {return api.parity.dappsList().then(function (apps) {return apps.map(function (app) {app.type = 'local';app.visible = true;return app;}).filter(function (app) {return app.id && !['ui'].includes(app.id);});}).catch(function (error) {console.warn('DappsStore:fetchLocal', error);});}function fetchRegistryAppIds(api) {var _Contracts$get2 = _contracts2.default.get(api),dappReg = _Contracts$get2.dappReg;return dappReg.count().then(function (count) {var promises = (0, _lodash.range)(0, count.toNumber()).map(function (index) {return dappReg.at(index);});return Promise.all(promises);}).then(function (appsInfo) {var appIds = appsInfo.map(function (_ref) {var _ref2 = _slicedToArray(_ref, 2),appId = _ref2[0],owner = _ref2[1];return (0, _format.bytesToHex)(appId);}).filter(function (appId) {return new _bignumber2.default(appId).gt(0) && !builtinApps.find(function (app) {return app.id === appId;});});return (0, _lodash.uniq)(appIds);}).catch(function (error) {console.warn('DappsStore:fetchRegistryAppIds', error);});}function fetchRegistryApp(api, dappReg, appId) {return Promise.all([dappReg.getImage(appId), dappReg.getContent(appId), dappReg.getManifest(appId)]).then(function (_ref3) {var _ref4 = _slicedToArray(_ref3, 3),imageId = _ref4[0],contentId = _ref4[1],manifestId = _ref4[2];var app = { id: appId, image: _ui.IconCache.hashToImage(imageId), contentHash: (0, _format.bytesToHex)(contentId).substr(2), manifestHash: (0, _format.bytesToHex)(manifestId).substr(2), type: 'network', visible: true };return fetchManifest(api, app.manifestHash).then(function (manifest) {if (manifest) {app.manifestHash = null; // Add usefull manifest fields to app
        Object.assign(app, (0, _lodash.pick)(manifest, ['author', 'description', 'name', 'version']));}return app;});}).then(function (app) {// Keep dapps that has a Manifest File and an Id
    var dapp = app.manifestHash || !app.id ? null : app;return dapp;}).catch(function (error) {console.warn('DappsStore:fetchRegistryApp', error);});}function fetchManifest(api, manifestHash) {if (/^(0x)?0+/.test(manifestHash)) {return Promise.resolve(null);}

  return fetch('/api/content/' +
  manifestHash + '/',
  { redirect: 'follow', mode: 'cors' }).

  then(function (response) {
    return response.ok ?
    response.json() :
    null;
  }).
  then(function (manifest) {
    return manifest;
  }).
  catch(function (error) {
    console.warn('DappsStore:fetchManifest', error);
    return null;
  });
}