Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | 14x 34x 34x 2x 34x 1x 33x 33x 33x 33x 33x 115x 115x 115x 115x 115x 115x 460x 1x 114x 111x 114x 14x 1x 1x 1x 1x 1x 14x 4x 14x 2x 2x 1x 1x 2x 14x 11x 11x 14x 3x 3x 14x 72x 4x 4x 4x 68x 14x 17x 17x 14x 20x 14x 14x 73x 14x 77x 14x 5x 5x 3x 14x 14x 12x 12x 4x 8x 2x 6x 14x 7x 7x 1x 6x 14x 2x 2x 14x 2x 2x 14x 4x 4x 3x 1x 14x 2x 2x 6x 6x 3x 6x 2x 14x 14x 496x 496x 500x 496x 496x 4x 4x 14x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /** * @file common utils * @author atom-yang */ import BigNumber from 'bignumber.js'; import bs58 from 'bs58'; import { UNIT_MAP, UNSIGNED_256_INT } from '../common/constants'; import { Transaction } from './proto'; import { OUTPUT_TRANSFORMERS, encodeAddress, transform, transformArrayToMap } from './transform'; import sha256 from './sha256'; export const base58 = { encode(data, encoding = 'hex') { let result = data; if (typeof data === 'string') { result = Buffer.from(data, encoding); } if (!(result instanceof Buffer)) { throw new TypeError('"data" argument must be an Array of Buffers'); } let hash = result; hash = Buffer.from(sha256(result), 'hex'); hash = Buffer.from(sha256(hash), 'hex'); hash = Buffer.from(result.toString('hex') + hash.slice(0, 4).toString('hex'), 'hex'); return bs58.encode(hash); }, decode(str, encoding) { const buffer = Buffer.from(bs58.decode(str)); let data = buffer.slice(0, -4); let hash = data; hash = Buffer.from(sha256(hash), 'hex'); hash = Buffer.from(sha256(hash), 'hex'); buffer.slice(-4).forEach((check, index) => { if (check !== hash[index]) { throw new Error('Invalid checksum'); } }); if (encoding) { data = data.toString(encoding); } return data; } }; export const chainIdConvertor = { // chainIdToBase58 (int32 chainId) chainIdToBase58(chainId) { const bufferTemp = Buffer.alloc(4); bufferTemp.writeInt32LE(`0x${chainId.toString('16')}`, 0); const bytes = Buffer.concat([bufferTemp], 3); return bs58.encode(bytes); }, base58ToChainId(base58String) { return Buffer.concat([bs58.decode(base58String)], 4).readInt32LE(0); } }; const arrayBufferToHex = arrayBuffer => Array.prototype.map.call(new Uint8Array(arrayBuffer), n => `0${n.toString(16)}`.slice(-2)).join(''); export const arrayToHex = value => { let hex = ''; if (value instanceof Buffer) { hex = value.toString('hex'); } else { // Uint8Array hex = arrayBufferToHex(value); } return hex; }; /** * Should be called to pad string to expected length * * @method padLeft * @param {String} string to be padded * @param {Number} charLen that result string should have * @param {String} sign, by default 0 * @returns {String} right aligned string */ export const padLeft = (string, charLen, sign) => { const length = charLen - string.length + 1; return new Array(length < 0 ? 0 : length).join(sign || '0') + string; }; /** * Should be called to pad string to expected length * * @method padRight * @param {String} string to be padded * @param {Number} charLen that result string should have * @param {String} sign, by default 0 * @returns {String} right aligned string */ export const padRight = (string, charLen, sign) => { const length = charLen - string.length + 1; return string + new Array(length < 0 ? 0 : length).join(sign || '0'); }; /** * Returns a hex rep from the encoded address * * @method decodeAddressRep * @param {String} address * @return {String} */ export const decodeAddressRep = address => { if (address.indexOf('_') > -1) { const parts = address.split('_'); const b58rep = parts[1]; return base58.decode(b58rep, 'hex'); } return base58.decode(address, 'hex'); }; /** * Returns a encoded address from the hex rep * * @method encodeAddressRep * @param {String} hex * @return {String} */ export const encodeAddressRep = hex => { const buf = Buffer.from(hex.replace('0x', ''), 'hex'); return base58.encode(buf, 'hex'); }; /** * Returns true if object is BigNumber, otherwise false * * @method isBigNumber * @param {Object} object * @return {Boolean} */ export const isBigNumber = object => object instanceof BigNumber || (object && object.constructor && object.constructor.name === 'BigNumber'); /** * Returns true if object is string, otherwise false * * @method isString * @param {Object} object * @return {Boolean} */ export const isString = object => typeof object === 'string' || (object && object.constructor && object.constructor.name === 'String'); /** * Returns true if object is function, otherwise false * * @method isFunction * @param {Object} object * @return {Boolean} */ export const isFunction = object => typeof object === 'function'; /** * Returns true if object is Object, otherwise false * * @method isObject * @param {Object} object * @return {Boolean} */ export const isObject = object => object !== null && !Array.isArray(object) && typeof object === 'object'; /** * Returns true if object is boolean, otherwise false * * @method isBoolean * @param {Object} object * @return {Boolean} */ export const isBoolean = object => typeof object === 'boolean'; /** * Returns true if given string is valid json object * * @method isJson * @param {String} str * @return {Boolean} */ export const isJson = str => { try { return !!JSON.parse(str); } catch (e) { return false; } }; /** * Returns true if given number is valid number * * @method isNumber * @param {Number} number * @return {Boolean} */ export const isNumber = number => number === +number; /** * Takes an input and transforms it into an bignumber * * @method toBigNumber * @param {Number|String|BigNumber} number, a number, string, HEX string or BigNumber * @return {BigNumber} BigNumber */ export const toBigNumber = number => { const num = number || 0; if (isBigNumber(num)) { return num; } if (isString(num) && (num.indexOf('0x') === 0 || num.indexOf('-0x') === 0)) { return new BigNumber(num.replace('0x', ''), 16); } return new BigNumber(num.toString(10), 10); }; /** * Returns value of unit in Wei * * @method getValueOfUnit * @param {String} unit the unit to convert to, default ether * @returns {BigNumber} value of the unit (in Wei) * @throws error if the unit is not correct:w */ export const getValueOfUnit = unit => { const unitValue = UNIT_MAP[unit ? unit.toLowerCase() : 'ether']; if (unitValue === undefined) { // eslint-disable-next-line max-len throw new Error( `This unit doesn\'t exists, please use the one of the following units ${JSON.stringify(UNIT_MAP, null, 2)}` ); } return new BigNumber(unitValue, 10); }; /** * Takes a number of wei and converts it to any other ether unit. * * Possible units are: * SI Short SI Full Effigy Other * - kwei femtoether babbage * - mwei picoether lovelace * - gwei nanoether shannon nano * - -- microether szabo micro * - -- milliether finney milli * - ether -- -- * - kether -- grand * - mether * - gether * - tether * * @method fromWei * @param {Number|String} number can be a number, number string or a HEX of a decimal * @param {String} unit the unit to convert to, default ether * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number */ export const fromWei = (number, unit) => { const returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit)); return isBigNumber(number) ? returnValue : returnValue.toString(10); }; /** * Takes a number of a unit and converts it to wei. * * Possible units are: * SI Short SI Full Effigy Other * - kwei femtoether babbage * - mwei picoether lovelace * - gwei nanoether shannon nano * - -- microether szabo micro * - -- milliether finney milli * - ether -- -- * - kether -- grand * - mether * - gether * - tether * * @method toWei * @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal * @param {String} unit the unit to convert from, default ether * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number */ export const toWei = (number, unit) => { const returnValue = toBigNumber(number).times(getValueOfUnit(unit)); return isBigNumber(number) ? returnValue : returnValue.toString(10); }; /** * Takes and input transforms it into bignumber and if it is negative value, into two's complement * bignumber.js get rid of round + floor in 6.0 https://github.com/MikeMcl/bignumber.js/issues/139 * the method lessThan was named isLessThan after 6.0 https://github.com/MikeMcl/bignumber.js/issues/152 * @method toTwosComplement * @param {Number|String|BigNumber} number * @return {BigNumber} */ export const toTwosComplement = number => { const bigNumber = toBigNumber(number).integerValue(); if (bigNumber.isLessThan(0)) { return new BigNumber(UNSIGNED_256_INT, 16).plus(bigNumber).plus(1); } return bigNumber; }; /** * Returns hex * * @method uint8ArrayToHex * @param {Array} uint8Array * @return {String} */ export const uint8ArrayToHex = uint8Array => { let string = ''; uint8Array.forEach(item => { let hex = item.toString(16); if (hex.length <= 1) { hex = `0${hex}`; } string += hex; }); return string; }; /** * empty function */ export const noop = () => {}; /** * * @param {object} obj The object to modify * @param {string} path The path of the property to set * @param {*} value The value to set * @example * * const object = { 'a': [{ 'b': { 'c': 3 } }] } * * set(object, 'test.b.c', 4) * console.log(object.test.b.c) * // => 4 */ export const setPath = (obj, path, value) => { const paths = path.split('.'); paths.reduce((acc, p, index) => { if (index === paths.length - 1) { acc[p] = value; return acc; } acc[p] = {}; return acc[p]; }, obj); }; export const unpackSpecifiedTypeData = ({ data, dataType, encoding = 'hex' }) => { const buffer = Buffer.from(data, encoding); const decoded = dataType.decode(buffer); const result = dataType.toObject(decoded, { enums: String, // enums as string names longs: String, // longs as strings (requires long.js) bytes: String, // bytes as base64 encoded strings defaults: true, // includes default values arrays: true, // populates empty arrays (repeated fields) even if defaults=false objects: true, // populates empty objects (map fields) even if defaults=false oneofs: true // includes virtual oneof fields set to the present field's name }); return result; }; export function deserializeTransaction(rawTx, paramsDataType) { const { from, to, params, refBlockPrefix, signature, ...rest } = unpackSpecifiedTypeData({ data: rawTx, dataType: Transaction }); let methodParameters = unpackSpecifiedTypeData({ data: params, encoding: 'base64', dataType: paramsDataType }); methodParameters = transform(paramsDataType, methodParameters, OUTPUT_TRANSFORMERS); methodParameters = transformArrayToMap(paramsDataType, methodParameters); return { from: encodeAddress(from.value), to: encodeAddress(to.value), params: methodParameters, refBlockPrefix: Buffer.from(refBlockPrefix, 'base64').toString('hex'), signature: Buffer.from(signature, 'base64').toString('hex'), ...rest }; } /** * * @param {String} userName Username * @param {String} password Password * @return {any} Authorization information * * const authorization = getAuthorization('test','pass') * console.log(authorization) * // => Basic dGVzdDpwYXNz */ export function getAuthorization(userName, password) { const base = Buffer.from(`${userName}:${password}`).toString('base64'); return `Basic ${base}`; } /** * * Use rawTransaction to get transaction id * @param {String} rawTx rawTransaction * @return {String} string * * const txId = getTransactionId('0a220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd512220a2089ac786c8ad3b56f63a6f2767369a5273f801de2415b613c783cad3d148ce3ab18d5d3bb35220491cf6ba12a18537761704578616374546f6b656e73466f72546f6b656e73325008c0f7f27110bbe5947c1a09534752544553542d311a03454c4622220a2071a4dc8cdf109bd72913c90c3fc666c78d080cdda0da7f3abbc7105c6b651fd52a08088996ceb0061000320631323334353682f10441ec6ad50c4b210976ba0ba5c287ab6fabd0c444839e2505ecb1b5f52838095b290cb245ec1c97dade3bde6ac14c6892e526569e9b71240d3c120b1a6c8e41afba00'); * console.log(txId); * // => cf564f3169012cb173efcf5543b2a71b030b16fad3ddefe3e04a5c1e1bc0047d */ export function getTransactionId(rawTx) { const hash = Buffer.from(rawTx.replace('0x', ''), 'hex'); const decode = Transaction.decode(hash); decode.signature = null; const encode = Transaction.encode(decode).finish(); return sha256(encode); } // /** // * Converts value to it's hex representation // * // * @method fromDecimal // * @param {String|Number|BigNumber} // * @return {String} // */ // export const fromDecimal = value => { // const number = toBigNumber(value); // const result = number.toString(16); // // return number.lessThan(0) ? `-0x${result.substr(1)}` : `0x${result}`; // }; // // /** // * Should be called to get hex representation (prefixed by 0x) of utf8 string // * // * @method fromUtf8 // * @param {String} string // * @param {Boolean} allowZero to convert code point zero to 00 instead of end of string // * @returns {String} hex representation of input string // */ // export const fromUtf8 = (str, allowZero) => { // const encodeStr = utf8.encode(str); // let hex = ''; // for (let i = 0; i < encodeStr.length; i++) { // const code = encodeStr.charCodeAt(i); // if (code === 0) { // if (allowZero) { // hex += '00'; // } else { // break; // } // } else { // const n = code.toString(16); // hex += n.length < 2 ? `0${n}` : n; // } // } // return `0x${hex}`; // }; |