refactor (@embark/embark-utils): move hashTo32ByteHexStrin to embark-utils

This commit is contained in:
Iuri Matias 2019-03-29 17:56:29 -04:00
parent 47ba899c85
commit 7511e3c113
5 changed files with 25 additions and 25 deletions

View File

@ -46,7 +46,8 @@
}, },
"dependencies": { "dependencies": {
"@babel/runtime-corejs2": "7.3.1", "@babel/runtime-corejs2": "7.3.1",
"follow-redirects": "1.5.7" "follow-redirects": "1.5.7",
"multihashes": "0.4.14"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "7.2.3", "@babel/cli": "7.2.3",

View File

@ -15,6 +15,24 @@ function checkIsAvailable(url, callback) {
}); });
} }
function isHex(hex) {
const Web3 = require('web3');
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);
}
const Utils = { const Utils = {
joinPath: function() { joinPath: function() {
const path = require('path'); const path = require('path');
@ -26,7 +44,9 @@ const Utils = {
dockerHostSwap, dockerHostSwap,
isDocker, isDocker,
checkIsAvailable, checkIsAvailable,
findNextPort findNextPort,
hashTo32ByteHexString,
isHex
}; };
module.exports = Utils; module.exports = Utils;

View File

@ -125,7 +125,6 @@
"lodash.clonedeep": "4.5.0", "lodash.clonedeep": "4.5.0",
"merge": "1.2.1", "merge": "1.2.1",
"mocha": "5.2.0", "mocha": "5.2.0",
"multihashes": "0.4.14",
"neo-blessed": "0.2.0", "neo-blessed": "0.2.0",
"netcat": "1.3.5", "netcat": "1.3.5",
"node-http-proxy-json": "0.1.6", "node-http-proxy-json": "0.1.6",

View File

@ -1,4 +1,4 @@
import {joinPath} from 'embark-utils'; import {joinPath, hashTo32ByteHexString} from 'embark-utils';
const utils = require('../../utils/utils.js'); const utils = require('../../utils/utils.js');
const namehash = require('eth-ens-namehash'); const namehash = require('eth-ens-namehash');
const async = require('async'); const async = require('async');
@ -184,7 +184,7 @@ class ENS {
let hashedName = namehash.hash(name); let hashedName = namehash.hash(name);
let contentHash; let contentHash;
try { try {
contentHash = utils.hashTo32ByteHexString(storageHash); contentHash = hashTo32ByteHexString(storageHash);
} catch (e) { } catch (e) {
return cb(__('Invalid IPFS hash')); return cb(__('Invalid IPFS hash'));
} }

View File

@ -320,24 +320,6 @@ function hexToNumber(hex) {
return Web3.utils.hexToNumber(hex); return Web3.utils.hexToNumber(hex);
} }
function isHex(hex) {
const Web3 = require('web3');
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(v) { function isValidDomain(v) {
// from: https://github.com/miguelmota/is-valid-domain // from: https://github.com/miguelmota/is-valid-domain
if (typeof v !== 'string') return false; if (typeof v !== 'string') return false;
@ -638,8 +620,6 @@ module.exports = {
httpsGetJson, httpsGetJson,
getJson, getJson,
hexToNumber, hexToNumber,
isHex,
hashTo32ByteHexString,
isValidDomain, isValidDomain,
pingEndpoint, pingEndpoint,
decodeParams, decodeParams,