UNPKG

@tgrospic/rnode-http-js

Version:
63 lines 1.92 kB
var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; import base58 from 'bs58'; /** * Encode bytes to base 16 string. */ export var encodeBase16 = function (bytes) { return Array.from(bytes).map(function (x) { return (x & 0xff).toString(16).padStart(2, '0'); }).join(''); }; /** * Decode base 16 string to bytes. */ export var decodeBase16 = function (hexStr) { var removed0x = hexStr.replace(/^0x/, ''); var byte2hex = function (_a, x) { var _b = __read(_a, 2), arr = _b[0], bhi = _b[1]; return (bhi ? [__spread(arr, [parseInt("" + bhi + x, 16)]), ''] : [arr, x]); }; var _a = __read(Array.from(removed0x).reduce(byte2hex, [[], '']), 1), resArr = _a[0]; return Uint8Array.from(resArr); }; /** * Encode base 16 string to base 58. */ export var encodeBase58 = function (hexStr) { var bytes = decodeBase16(hexStr); return base58.encode(bytes); }; /** * Decode base 58 string (handle errors). */ export var decodeBase58safe = function (str) { try { return base58.decode(str); } catch (_a) { return void 666; } }; /** * Decode ASCII string to bytes. */ export var decodeAscii = function (str) { if (str === void 0) { str = ''; } return Array.from(str).map(function (x) { return ("" + x).charCodeAt(0); }); }; //# sourceMappingURL=codecs.js.map