mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-21 09:58:30 +00:00
fix stuff and move functions to utils
This commit is contained in:
parent
daaeb30262
commit
aadc4062fb
@ -428,7 +428,7 @@ class Embark {
|
||||
if(!options.ensDomain) {
|
||||
return callback(null, hash);
|
||||
}
|
||||
engine.plugins.request("storage:ens:associate",
|
||||
engine.events.request("storage:ens:associate",
|
||||
{name: options.ensDomain, storageHash: hash}, (err) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
|
@ -22,7 +22,7 @@ class ENS {
|
||||
registerEvents() {
|
||||
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) {
|
||||
@ -69,26 +69,17 @@ class ENS {
|
||||
// Code inspired by https://github.com/monkybrain/ipfs-to-ens
|
||||
const {name, storageHash} = options;
|
||||
|
||||
if (!ENS.isValidDomain(name)) {
|
||||
if (!utils.isValidEthDomain(name)) {
|
||||
return cb('Invalid domain name ' + name);
|
||||
}
|
||||
|
||||
let hashedName = namehash.hash(name);
|
||||
|
||||
// Convert IPFS multihash to 32 byte hex string
|
||||
let contentHash;
|
||||
if (utils.isHex(storageHash)) {
|
||||
contentHash = storageHash;
|
||||
} else {
|
||||
try {
|
||||
contentHash = ENS.hashTo32ByteHexString(storageHash);
|
||||
contentHash = utils.hashTo32ByteHexString(storageHash);
|
||||
} catch (e) {
|
||||
return cb('Invalid IPFS hash');
|
||||
}
|
||||
}
|
||||
if (!contentHash.startsWith('0x')) {
|
||||
contentHash = '0x' + contentHash;
|
||||
}
|
||||
|
||||
// Set content
|
||||
async.waterfall([
|
||||
@ -143,22 +134,6 @@ class ENS {
|
||||
], 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) {
|
||||
const self = this;
|
||||
const register = require('./register');
|
||||
|
@ -258,6 +258,33 @@ function isHex(hex){
|
||||
return Web3.utils.isHex(hex);
|
||||
}
|
||||
|
||||
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');
|
||||
return Web3EthAbi.decodeParameters(typesArray, hexString);
|
||||
@ -284,13 +311,21 @@ function normalizeInput(input) {
|
||||
return "";
|
||||
}
|
||||
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.map((x) => {
|
||||
if (x === null) { return "null"; }
|
||||
if (x === undefined) { return "undefined"; }
|
||||
if (Array.isArray(x)) { return x.join(','); }
|
||||
if (x === null) {
|
||||
return "null";
|
||||
}
|
||||
if (x === undefined) {
|
||||
return "undefined";
|
||||
}
|
||||
if (Array.isArray(x)) {
|
||||
return x.join(',');
|
||||
}
|
||||
return x;
|
||||
}).toString() + ']');
|
||||
}
|
||||
@ -386,6 +421,9 @@ module.exports = {
|
||||
getJson,
|
||||
hexToNumber,
|
||||
isHex,
|
||||
hashTo32ByteHexString,
|
||||
isValidDomain,
|
||||
isValidEthDomain,
|
||||
pingEndpoint,
|
||||
decodeParams,
|
||||
runCmd,
|
||||
|
Loading…
x
Reference in New Issue
Block a user