mirror of https://github.com/embarklabs/embark.git
fix stuff and move functions to utils
This commit is contained in:
parent
609c050576
commit
25ffd15984
|
@ -425,7 +425,7 @@ class Embark {
|
||||||
if(!options.ensDomain) {
|
if(!options.ensDomain) {
|
||||||
return callback(null, hash);
|
return callback(null, hash);
|
||||||
}
|
}
|
||||||
engine.plugins.request("storage:ens:associate",
|
engine.events.request("storage:ens:associate",
|
||||||
{name: options.ensDomain, storageHash: hash}, (err) => {
|
{name: options.ensDomain, storageHash: hash}, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
|
@ -22,7 +22,7 @@ class ENS {
|
||||||
registerEvents() {
|
registerEvents() {
|
||||||
this.embark.registerActionForEvent("contracts:deploy:afterAll", this.setProviderAndRegisterDomains.bind(this));
|
this.embark.registerActionForEvent("contracts:deploy:afterAll", this.setProviderAndRegisterDomains.bind(this));
|
||||||
|
|
||||||
this.embark.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this));
|
this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
setProviderAndRegisterDomains(cb) {
|
setProviderAndRegisterDomains(cb) {
|
||||||
|
@ -65,30 +65,21 @@ class ENS {
|
||||||
}
|
}
|
||||||
|
|
||||||
associateStorageToEns(options, cb) {
|
associateStorageToEns(options, cb) {
|
||||||
const self =this;
|
const self = this;
|
||||||
// Code inspired by https://github.com/monkybrain/ipfs-to-ens
|
// Code inspired by https://github.com/monkybrain/ipfs-to-ens
|
||||||
const {name, storageHash} = options;
|
const {name, storageHash} = options;
|
||||||
|
|
||||||
if (!ENS.isValidDomain(name)) {
|
if (!utils.isValidEthDomain(name)) {
|
||||||
return cb('Invalid domain name ' + name);
|
return cb('Invalid domain name ' + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
let hashedName = namehash.hash(name);
|
let hashedName = namehash.hash(name);
|
||||||
|
|
||||||
// Convert IPFS multihash to 32 byte hex string
|
|
||||||
let contentHash;
|
let contentHash;
|
||||||
if (utils.isHex(storageHash)) {
|
|
||||||
contentHash = storageHash;
|
|
||||||
} else {
|
|
||||||
try {
|
try {
|
||||||
contentHash = ENS.hashTo32ByteHexString(storageHash);
|
contentHash = utils.hashTo32ByteHexString(storageHash);
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
return cb('Invalid IPFS hash');
|
return cb('Invalid IPFS hash');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (!contentHash.startsWith('0x')) {
|
|
||||||
contentHash = '0x' + contentHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set content
|
// Set content
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
|
@ -143,22 +134,6 @@ class ENS {
|
||||||
], cb);
|
], cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static isValidDomain(domain) {
|
|
||||||
const isValidDomain = require('is-valid-domain');
|
|
||||||
if (!isValidDomain(domain)) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return domain.substring(domain.lastIndexOf('.'), domain.length) === '.eth';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static hashTo32ByteHexString(storageHash) {
|
|
||||||
const multihash = require('multihashes');
|
|
||||||
let buf = multihash.fromB58String(storageHash);
|
|
||||||
let digest = multihash.decode(buf).digest;
|
|
||||||
return '0x' + multihash.toHexString(digest);
|
|
||||||
}
|
|
||||||
|
|
||||||
registerConfigDomains(config, cb) {
|
registerConfigDomains(config, cb) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const register = require('./register');
|
const register = require('./register');
|
||||||
|
|
|
@ -38,7 +38,7 @@ function checkIsAvailable(url, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function httpGetRequest(httpObj, url, callback) {
|
function httpGetRequest(httpObj, url, callback) {
|
||||||
httpObj.get(url, function(res) {
|
httpObj.get(url, function (res) {
|
||||||
let body = '';
|
let body = '';
|
||||||
res.on('data', function (d) {
|
res.on('data', function (d) {
|
||||||
body += d;
|
body += d;
|
||||||
|
@ -60,22 +60,22 @@ function httpsGet(url, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function httpGetJson(url, callback) {
|
function httpGetJson(url, callback) {
|
||||||
httpGetRequest(http, url, function(err, body) {
|
httpGetRequest(http, url, function (err, body) {
|
||||||
try {
|
try {
|
||||||
let parsed = JSON.parse(body);
|
let parsed = JSON.parse(body);
|
||||||
return callback(err, parsed);
|
return callback(err, parsed);
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
return callback(e);
|
return callback(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function httpsGetJson(url, callback) {
|
function httpsGetJson(url, callback) {
|
||||||
httpGetRequest(https, url, function(err, body) {
|
httpGetRequest(https, url, function (err, body) {
|
||||||
try {
|
try {
|
||||||
let parsed = JSON.parse(body);
|
let parsed = JSON.parse(body);
|
||||||
return callback(err, parsed);
|
return callback(err, parsed);
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
return callback(e);
|
return callback(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -107,7 +107,7 @@ function pingEndpoint(host, port, type, protocol, origin, callback) {
|
||||||
}
|
}
|
||||||
let req;
|
let req;
|
||||||
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
|
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
|
||||||
if (options.host.indexOf('/') > -1){
|
if (options.host.indexOf('/') > -1) {
|
||||||
options.host = options.host.split('/')[0];
|
options.host = options.host.split('/')[0];
|
||||||
}
|
}
|
||||||
if (protocol === 'https') {
|
if (protocol === 'https') {
|
||||||
|
@ -159,12 +159,12 @@ function exit(code) {
|
||||||
function downloadFile(url, dest, cb) {
|
function downloadFile(url, dest, cb) {
|
||||||
const o_fs = require('fs-extra');
|
const o_fs = require('fs-extra');
|
||||||
var file = o_fs.createWriteStream(dest);
|
var file = o_fs.createWriteStream(dest);
|
||||||
(url.substring(0,5) === 'https' ? https : http).get(url, function(response) {
|
(url.substring(0, 5) === 'https' ? https : http).get(url, function (response) {
|
||||||
response.pipe(file);
|
response.pipe(file);
|
||||||
file.on('finish', function() {
|
file.on('finish', function () {
|
||||||
file.close(cb);
|
file.close(cb);
|
||||||
});
|
});
|
||||||
}).on('error', function(err) {
|
}).on('error', function (err) {
|
||||||
o_fs.unlink(dest);
|
o_fs.unlink(dest);
|
||||||
if (cb) cb(err.message);
|
if (cb) cb(err.message);
|
||||||
});
|
});
|
||||||
|
@ -177,7 +177,7 @@ function extractTar(filename, packageDirectory, cb) {
|
||||||
tar.x({
|
tar.x({
|
||||||
strip: 1,
|
strip: 1,
|
||||||
C: packageDirectory
|
C: packageDirectory
|
||||||
}).on('end', function() {
|
}).on('end', function () {
|
||||||
cb();
|
cb();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -250,17 +250,44 @@ function getExternalContractUrl(file) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function hexToNumber(hex){
|
function hexToNumber(hex) {
|
||||||
const Web3 = require('web3');
|
const Web3 = require('web3');
|
||||||
return Web3.utils.hexToNumber(hex);
|
return Web3.utils.hexToNumber(hex);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isHex(hex){
|
function isHex(hex) {
|
||||||
const Web3 = require('web3');
|
const Web3 = require('web3');
|
||||||
return Web3.utils.isHex(hex);
|
return Web3.utils.isHex(hex);
|
||||||
}
|
}
|
||||||
|
|
||||||
function decodeParams(typesArray, hexString){
|
function hashTo32ByteHexString(hash) {
|
||||||
|
if (isHex(hash)) {
|
||||||
|
if (!hash.startsWith('0x')) {
|
||||||
|
hash = '0x' + hash;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
const multihash = require('multihashes');
|
||||||
|
let buf = multihash.fromB58String(hash);
|
||||||
|
let digest = multihash.decode(buf).digest;
|
||||||
|
return '0x' + multihash.toHexString(digest);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidDomain(domain) {
|
||||||
|
const isValidDomain = require('is-valid-domain');
|
||||||
|
return isValidDomain(domain);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidEthDomain(ethDomain) {
|
||||||
|
if (!isValidDomain(ethDomain)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return ethDomain.substring(ethDomain.lastIndexOf('.'), ethDomain.length) === '.eth';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeParams(typesArray, hexString) {
|
||||||
var Web3EthAbi = require('web3-eth-abi');
|
var Web3EthAbi = require('web3-eth-abi');
|
||||||
return Web3EthAbi.decodeParameters(typesArray, hexString);
|
return Web3EthAbi.decodeParameters(typesArray, hexString);
|
||||||
}
|
}
|
||||||
|
@ -286,13 +313,21 @@ function normalizeInput(input) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if (args.length === 1) {
|
if (args.length === 1) {
|
||||||
if (Array.isArray(args[0])) { return args[0].join(','); }
|
if (Array.isArray(args[0])) {
|
||||||
|
return args[0].join(',');
|
||||||
|
}
|
||||||
return args[0] || "";
|
return args[0] || "";
|
||||||
}
|
}
|
||||||
return ('[' + args.map((x) => {
|
return ('[' + args.map((x) => {
|
||||||
if (x === null) { return "null"; }
|
if (x === null) {
|
||||||
if (x === undefined) { return "undefined"; }
|
return "null";
|
||||||
if (Array.isArray(x)) { return x.join(','); }
|
}
|
||||||
|
if (x === undefined) {
|
||||||
|
return "undefined";
|
||||||
|
}
|
||||||
|
if (Array.isArray(x)) {
|
||||||
|
return x.join(',');
|
||||||
|
}
|
||||||
return x;
|
return x;
|
||||||
}).toString() + ']');
|
}).toString() + ']');
|
||||||
}
|
}
|
||||||
|
@ -308,9 +343,9 @@ function normalizeInput(input) {
|
||||||
* The URL port, default to empty string.
|
* The URL port, default to empty string.
|
||||||
* @returns {string} the constructued URL, with defaults
|
* @returns {string} the constructued URL, with defaults
|
||||||
*/
|
*/
|
||||||
function buildUrl (protocol, host, port){
|
function buildUrl(protocol, host, port) {
|
||||||
if(!host) throw new Error('utils.buildUrl: parameter \'host\' is required');
|
if (!host) throw new Error('utils.buildUrl: parameter \'host\' is required');
|
||||||
if(port) port = ':' + port;
|
if (port) port = ':' + port;
|
||||||
else port = '';
|
else port = '';
|
||||||
return `${protocol || 'http'}://${host}${port}`;
|
return `${protocol || 'http'}://${host}${port}`;
|
||||||
}
|
}
|
||||||
|
@ -324,9 +359,9 @@ function buildUrl (protocol, host, port){
|
||||||
* * port {String} (optional) The URL port, default to empty string.
|
* * port {String} (optional) The URL port, default to empty string.
|
||||||
* @returns {string} the constructued URL, with defaults
|
* @returns {string} the constructued URL, with defaults
|
||||||
*/
|
*/
|
||||||
function buildUrlFromConfig (configObj){
|
function buildUrlFromConfig(configObj) {
|
||||||
if(!configObj) throw new Error('[utils.buildUrlFromConfig]: config object must cannot be null');
|
if (!configObj) throw new Error('[utils.buildUrlFromConfig]: config object must cannot be null');
|
||||||
if(!configObj.host) throw new Error('[utils.buildUrlFromConfig]: object must contain a \'host\' property');
|
if (!configObj.host) throw new Error('[utils.buildUrlFromConfig]: object must contain a \'host\' property');
|
||||||
return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port);
|
return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,7 +409,7 @@ function compact(array) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function groupBy(array, key) {
|
function groupBy(array, key) {
|
||||||
return array.reduce(function(rv, x) {
|
return array.reduce(function (rv, x) {
|
||||||
(rv[x[key]] = rv[x[key]] || []).push(x);
|
(rv[x[key]] = rv[x[key]] || []).push(x);
|
||||||
return rv;
|
return rv;
|
||||||
}, {});
|
}, {});
|
||||||
|
@ -392,23 +427,23 @@ function interceptLogs(consoleContext, logger) {
|
||||||
let context = {};
|
let context = {};
|
||||||
context.console = consoleContext;
|
context.console = consoleContext;
|
||||||
|
|
||||||
context.console.log = function() {
|
context.console.log = function () {
|
||||||
logger.info(normalizeInput(arguments));
|
logger.info(normalizeInput(arguments));
|
||||||
};
|
};
|
||||||
context.console.warn = function() {
|
context.console.warn = function () {
|
||||||
logger.warn(normalizeInput(arguments));
|
logger.warn(normalizeInput(arguments));
|
||||||
};
|
};
|
||||||
context.console.info = function() {
|
context.console.info = function () {
|
||||||
logger.info(normalizeInput(arguments));
|
logger.info(normalizeInput(arguments));
|
||||||
};
|
};
|
||||||
context.console.debug = function() {
|
context.console.debug = function () {
|
||||||
// TODO: ue JSON.stringify
|
// TODO: ue JSON.stringify
|
||||||
logger.debug(normalizeInput(arguments));
|
logger.debug(normalizeInput(arguments));
|
||||||
};
|
};
|
||||||
context.console.trace = function() {
|
context.console.trace = function () {
|
||||||
logger.trace(normalizeInput(arguments));
|
logger.trace(normalizeInput(arguments));
|
||||||
};
|
};
|
||||||
context.console.dir = function() {
|
context.console.dir = function () {
|
||||||
logger.dir(normalizeInput(arguments));
|
logger.dir(normalizeInput(arguments));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -427,6 +462,9 @@ module.exports = {
|
||||||
getJson,
|
getJson,
|
||||||
hexToNumber,
|
hexToNumber,
|
||||||
isHex,
|
isHex,
|
||||||
|
hashTo32ByteHexString,
|
||||||
|
isValidDomain,
|
||||||
|
isValidEthDomain,
|
||||||
pingEndpoint,
|
pingEndpoint,
|
||||||
decodeParams,
|
decodeParams,
|
||||||
runCmd,
|
runCmd,
|
||||||
|
|
Loading…
Reference in New Issue