UNPKG

@brtnetwork/brt-binary-codec

Version:
141 lines 4.41 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Currency = void 0; var hash_160_1 = require("./hash-160"); var buffer_1 = require("buffer/"); var ISO_REGEX = /^[A-Z0-9]{3}$/; var HEX_REGEX = /^[A-F0-9]{40}$/; /** * Convert an ISO code to a currency bytes representation */ function isoToBytes(iso) { var bytes = buffer_1.Buffer.alloc(20); if (iso !== "BRT") { var isoBytes = iso.split("").map(function (c) { return c.charCodeAt(0); }); bytes.set(isoBytes, 12); } return bytes; } /** * Tests if ISO is a valid iso code */ function isIsoCode(iso) { return iso.length === 3; } /** * Tests if hex is a valid hex-string */ function isHex(hex) { return HEX_REGEX.test(hex); } /** * Tests if a string is a valid representation of a currency */ function isStringRepresentation(input) { return isIsoCode(input) || isHex(input); } /** * Tests if a Buffer is a valid representation of a currency */ function isBytesArray(bytes) { return bytes.byteLength === 20; } /** * Ensures that a value is a valid representation of a currency */ function isValidRepresentation(input) { return input instanceof buffer_1.Buffer ? isBytesArray(input) : isStringRepresentation(input); } /** * Generate bytes from a string or buffer representation of a currency */ function bytesFromRepresentation(input) { if (!isValidRepresentation(input)) { throw new Error("Unsupported Currency representation: " + input); } return input.length === 3 ? isoToBytes(input) : buffer_1.Buffer.from(input, "hex"); } /** * Class defining how to encode and decode Currencies */ var Currency = /** @class */ (function (_super) { __extends(Currency, _super); function Currency(byteBuf) { var _this = _super.call(this, byteBuf !== null && byteBuf !== void 0 ? byteBuf : Currency.BRT.bytes) || this; var onlyISO = true; var bytes = _this.bytes; var code = _this.bytes.slice(12, 15); var iso = code.toString(); for (var i = bytes.length - 1; i >= 0; i--) { if (bytes[i] !== 0 && !(i === 12 || i === 13 || i === 14)) { onlyISO = false; break; } } var lossLessISO = onlyISO && iso !== "BRT" && ISO_REGEX.test(iso); _this._isNative = onlyISO && code.toString("hex") === "000000"; _this._iso = _this._isNative ? "BRT" : lossLessISO ? iso : undefined; return _this; } /** * Tells if this currency is native * * @returns true if native, false if not */ Currency.prototype.isNative = function () { return this._isNative; }; /** * Return the ISO code of this currency * * @returns ISO code if it exists, else undefined */ Currency.prototype.iso = function () { return this._iso; }; /** * Constructs a Currency object * * @param val Currency object or a string representation of a currency */ Currency.from = function (value) { if (value instanceof Currency) { return value; } if (typeof value === "string") { return new Currency(bytesFromRepresentation(value)); } throw new Error("Cannot construct Currency from value given"); }; /** * Gets the JSON representation of a currency * * @returns JSON representation */ Currency.prototype.toJSON = function () { var iso = this.iso(); if (iso !== undefined) { return iso; } return this.bytes.toString("hex").toUpperCase(); }; Currency.BRT = new Currency(buffer_1.Buffer.alloc(20)); return Currency; }(hash_160_1.Hash160)); exports.Currency = Currency; //# sourceMappingURL=currency.js.map