mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-22 10:28:34 +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) {
|
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) {
|
||||||
@ -69,26 +69,17 @@ class ENS {
|
|||||||
// 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');
|
||||||
|
@ -258,6 +258,33 @@ function isHex(hex){
|
|||||||
return Web3.utils.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) {
|
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);
|
||||||
@ -284,13 +311,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() + ']');
|
||||||
}
|
}
|
||||||
@ -386,6 +421,9 @@ module.exports = {
|
|||||||
getJson,
|
getJson,
|
||||||
hexToNumber,
|
hexToNumber,
|
||||||
isHex,
|
isHex,
|
||||||
|
hashTo32ByteHexString,
|
||||||
|
isValidDomain,
|
||||||
|
isValidEthDomain,
|
||||||
pingEndpoint,
|
pingEndpoint,
|
||||||
decodeParams,
|
decodeParams,
|
||||||
runCmd,
|
runCmd,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user