You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

quality.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.quality = void 0;
  4. const types_1 = require("./types");
  5. const decimal_js_1 = require("decimal.js");
  6. const bigInt = require("big-integer");
  7. const buffer_1 = require("buffer/");
  8. /**
  9. * class for encoding and decoding quality
  10. */
  11. class 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. }
  39. exports.quality = quality;
  40. //# sourceMappingURL=quality.js.map