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": {
"@babel/runtime-corejs2": "7.3.1",
"follow-redirects": "1.5.7"
"follow-redirects": "1.5.7",
"multihashes": "0.4.14"
},
"devDependencies": {
"@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 = {
joinPath: function() {
const path = require('path');
@ -26,7 +44,9 @@ const Utils = {
dockerHostSwap,
isDocker,
checkIsAvailable,
findNextPort
findNextPort,
hashTo32ByteHexString,
isHex
};
module.exports = Utils;

View File

@ -125,7 +125,6 @@
"lodash.clonedeep": "4.5.0",
"merge": "1.2.1",
"mocha": "5.2.0",
"multihashes": "0.4.14",
"neo-blessed": "0.2.0",
"netcat": "1.3.5",
"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 namehash = require('eth-ens-namehash');
const async = require('async');
@ -184,7 +184,7 @@ class ENS {
let hashedName = namehash.hash(name);
let contentHash;
try {
contentHash = utils.hashTo32ByteHexString(storageHash);
contentHash = hashTo32ByteHexString(storageHash);
} catch (e) {
return cb(__('Invalid IPFS hash'));
}

View File

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