UNPKG

1.32 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.quality = void 0;
4const types_1 = require("./types");
5const decimal_js_1 = require("decimal.js");
6const bigInt = require("big-integer");
7const buffer_1 = require("buffer/");
8/**
9 * class for encoding and decoding quality
10 */
11class quality {
12 /**
13 * Encode quality amount
14 *
15 * @param arg string representation of an amount
16 * @returns Serialized quality
17 */
18 static encode(quality) {
19 const decimal = new decimal_js_1.Decimal(quality);
20 const exponent = decimal.e - 15;
21 const qualityString = decimal.times(`1e${-exponent}`).abs().toString();
22 const bytes = types_1.coreTypes.UInt64.from(bigInt(qualityString)).toBytes();
23 bytes[0] = exponent + 100;
24 return bytes;
25 }
26 /**
27 * Decode quality amount
28 *
29 * @param arg hex-string denoting serialized quality
30 * @returns deserialized quality
31 */
32 static decode(quality) {
33 const bytes = buffer_1.Buffer.from(quality, 'hex').slice(-8);
34 const exponent = bytes[0] - 100;
35 const mantissa = new decimal_js_1.Decimal(`0x${bytes.slice(1).toString('hex')}`);
36 return mantissa.times(`1e${exponent}`);
37 }
38}
39exports.quality = quality;
40//# sourceMappingURL=quality.js.map
\No newline at end of file