UNPKG

incubed

Version:

Typescript-version of the incubed client

193 lines 6.74 kB
"use strict"; /*********************************************************** * This file is part of the Slock.it IoT Layer. * * The Slock.it IoT Layer contains: * * - USN (Universal Sharing Network) * * - INCUBED (Trustless INcentivized remote Node Network) * ************************************************************ * Copyright (C) 2016 - 2018 Slock.it GmbH * * All Rights Reserved. * ************************************************************ * You may use, distribute and modify this code under the * * terms of the license contract you have concluded with * * Slock.it GmbH. * * For information about liability, maintenance etc. also * * refer to the contract concluded with Slock.it GmbH. * ************************************************************ * For more information, please refer to https://slock.it * * For questions, please contact info@slock.it * ***********************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); // this is eded in order to run in browsers const Buffer = require('buffer').Buffer; const ethUtil = require("ethereumjs-util"); const BN = ethUtil.BN; const fixLength = (hex) => hex.length % 2 ? '0' + hex : hex; /** * * simple promisy-function */ function promisify(self, fn, ...args) { return new Promise((resolve, reject) => { fn.apply(self, [...args, (err, res) => { if (err) reject(err); else resolve(res); }]); }); } exports.promisify = promisify; function toUtf8(val) { if (!val) return val; if (typeof val === 'string') return val.startsWith('0x') ? Buffer.from(val.substr(2), 'hex').toString('utf8') : val; return val.toString('utf8'); } exports.toUtf8 = toUtf8; /** * check a RPC-Response for errors and rejects the promise if found */ function checkForError(res) { if (Array.isArray(res)) return res.find(_ => !!_.error) ? Promise.reject(new Error(res.find(_ => !!_.error).error)) : res; return res.error ? Promise.reject(new Error(res.error)) : res; } exports.checkForError = checkForError; /** * convert to BigNumber */ function toBN(val) { if (BN.isBN(val)) return val; if (typeof val === 'number') return new BN(Math.round(val).toString()); if (Buffer.isBuffer(val)) return new BN(val); return new BN(toHex(val).substr(2), 16); } exports.toBN = toBN; /** * converts any value as hex-string */ function toHex(val, bytes) { if (val === undefined) return undefined; let hex; if (typeof val === 'string') hex = val.startsWith('0x') ? val.substr(2) : (parseInt(val[0]) ? new BN(val).toString(16) : Buffer.from(val, 'utf8').toString('hex')); else if (typeof val === 'number') hex = val.toString(16); else if (BN.isBN(val)) hex = val.toString(16); else hex = ethUtil.bufferToHex(val).substr(2); if (bytes) hex = padStart(hex, bytes * 2, '0'); // workarounf for ts-error in older js if (hex.length % 2) hex = '0' + hex; return '0x' + hex.toLowerCase(); } exports.toHex = toHex; /** * converts to a js-number */ function toNumber(val) { switch (typeof val) { case 'number': return val; case 'string': return parseInt(val); default: if (Buffer.isBuffer(val)) return val.length == 0 ? 0 : parseInt(toMinHex(val)); else if (BN.isBN(val)) return val.bitLength() > 53 ? toNumber(val.toArrayLike(Buffer)) : val.toNumber(); else if (val === undefined || val === null) return 0; throw new Error('can not convert a ' + (typeof val) + ' to number'); } } exports.toNumber = toNumber; /** * converts any value as Buffer * if len === 0 it will return an empty Buffer if the value is 0 or '0x00', since this is the way rlpencode works wit 0-values. */ function toBuffer(val, len = -1) { if (typeof val == 'string') val = val.startsWith('0x') ? Buffer.from((val.length % 2 ? '0' : '') + val.substr(2), 'hex') : val.length && (parseInt(val) || val == '0') ? new BN(val).toArrayLike(Buffer) : Buffer.from(val, 'utf8'); else if (typeof val == 'number') val = val === 0 && len === 0 ? Buffer.allocUnsafe(0) : Buffer.from(fixLength(val.toString(16)), 'hex'); else if (BN.isBN(val)) val = val.toArrayLike(Buffer); if (!val) val = Buffer.allocUnsafe(0); // since rlp encodes an empty array for a 0 -value we create one if the required len===0 if (len == 0 && val.length == 1 && val[0] === 0) return Buffer.allocUnsafe(0); // if we have a defined length, we should padLeft 00 or cut the left content to ensure length if (len > 0 && Buffer.isBuffer(val) && val.length !== len) return val.length < len ? Buffer.concat([Buffer.alloc(len - val.length), val]) : val.slice(val.length - len); return val; } exports.toBuffer = toBuffer; /** * removes all leading 0 in a hex-string */ function toSimpleHex(val) { let hex = val.replace('0x', ''); while (hex.startsWith('00') && hex.length > 2) hex = hex.substr(2); return '0x' + hex; } exports.toSimpleHex = toSimpleHex; /** * returns a address from a private key */ function getAddress(pk) { const key = toBuffer(pk); return ethUtil.toChecksumAddress(ethUtil.privateToAddress(key).toString('hex')); } exports.getAddress = getAddress; /** removes all leading 0 in the hexstring */ function toMinHex(key) { if (typeof key === 'number') key = toHex(key); if (typeof key === 'string') { for (let i = 2; i < key.length; i++) { if (key[i] !== '0') return '0x' + key.substr(i); } } else if (Buffer.isBuffer(key)) { const hex = key.toString('hex'); for (let i = 0; i < hex.length; i++) { if (hex[i] !== '0') return '0x' + hex.substr(i); } } return '0x0'; } exports.toMinHex = toMinHex; /** padStart for legacy */ function padStart(val, minLength, fill = ' ') { while (val.length < minLength) val = fill + val; return val; } exports.padStart = padStart; /** padEnd for legacy */ function padEnd(val, minLength, fill = ' ') { while (val.length < minLength) val = val + fill; return val; } exports.padEnd = padEnd; //# sourceMappingURL=util.js.map