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.

utils.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. exports.isValidSecret = exports.iso8601ToRippleTime = exports.rippleTimeToISO8601 = exports.removeUndefined = exports.convertKeysFromSnakeCaseToCamelCase = exports.toRippledAmount = exports.xrpToDrops = exports.dropsToXrp = void 0;
  26. const _ = __importStar(require("lodash"));
  27. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  28. const ripple_keypairs_1 = require("ripple-keypairs");
  29. const errors_1 = require("./errors");
  30. const ripple_address_codec_1 = require("ripple-address-codec");
  31. function isValidSecret(secret) {
  32. try {
  33. ripple_keypairs_1.deriveKeypair(secret);
  34. return true;
  35. }
  36. catch (err) {
  37. return false;
  38. }
  39. }
  40. exports.isValidSecret = isValidSecret;
  41. function dropsToXrp(drops) {
  42. if (typeof drops === 'string') {
  43. if (!drops.match(/^-?[0-9]*\.?[0-9]*$/)) {
  44. throw new errors_1.ValidationError(`dropsToXrp: invalid value '${drops}',` +
  45. ` should be a number matching (^-?[0-9]*\\.?[0-9]*$).`);
  46. }
  47. else if (drops === '.') {
  48. throw new errors_1.ValidationError(`dropsToXrp: invalid value '${drops}',` +
  49. ` should be a BigNumber or string-encoded number.`);
  50. }
  51. }
  52. drops = new bignumber_js_1.default(drops).toString(10);
  53. if (drops.includes('.')) {
  54. throw new errors_1.ValidationError(`dropsToXrp: value '${drops}' has` + ` too many decimal places.`);
  55. }
  56. if (!drops.match(/^-?[0-9]+$/)) {
  57. throw new errors_1.ValidationError(`dropsToXrp: failed sanity check -` +
  58. ` value '${drops}',` +
  59. ` does not match (^-?[0-9]+$).`);
  60. }
  61. return new bignumber_js_1.default(drops).dividedBy(1000000.0).toString(10);
  62. }
  63. exports.dropsToXrp = dropsToXrp;
  64. function xrpToDrops(xrp) {
  65. if (typeof xrp === 'string') {
  66. if (!xrp.match(/^-?[0-9]*\.?[0-9]*$/)) {
  67. throw new errors_1.ValidationError(`xrpToDrops: invalid value '${xrp}',` +
  68. ` should be a number matching (^-?[0-9]*\\.?[0-9]*$).`);
  69. }
  70. else if (xrp === '.') {
  71. throw new errors_1.ValidationError(`xrpToDrops: invalid value '${xrp}',` +
  72. ` should be a BigNumber or string-encoded number.`);
  73. }
  74. }
  75. xrp = new bignumber_js_1.default(xrp).toString(10);
  76. if (!xrp.match(/^-?[0-9.]+$/)) {
  77. throw new errors_1.ValidationError(`xrpToDrops: failed sanity check -` +
  78. ` value '${xrp}',` +
  79. ` does not match (^-?[0-9.]+$).`);
  80. }
  81. const components = xrp.split('.');
  82. if (components.length > 2) {
  83. throw new errors_1.ValidationError(`xrpToDrops: failed sanity check -` +
  84. ` value '${xrp}' has` +
  85. ` too many decimal points.`);
  86. }
  87. const fraction = components[1] || '0';
  88. if (fraction.length > 6) {
  89. throw new errors_1.ValidationError(`xrpToDrops: value '${xrp}' has` + ` too many decimal places.`);
  90. }
  91. return new bignumber_js_1.default(xrp)
  92. .times(1000000.0)
  93. .integerValue(bignumber_js_1.default.ROUND_FLOOR)
  94. .toString(10);
  95. }
  96. exports.xrpToDrops = xrpToDrops;
  97. function toRippledAmount(amount) {
  98. if (typeof amount === 'string')
  99. return amount;
  100. if (amount.currency === 'XRP') {
  101. return xrpToDrops(amount.value);
  102. }
  103. if (amount.currency === 'drops') {
  104. return amount.value;
  105. }
  106. let issuer = amount.counterparty || amount.issuer;
  107. let tag = false;
  108. try {
  109. ({ classicAddress: issuer, tag } = ripple_address_codec_1.xAddressToClassicAddress(issuer));
  110. }
  111. catch (e) { }
  112. if (tag !== false) {
  113. throw new errors_1.ValidationError("Issuer X-address includes a tag");
  114. }
  115. return {
  116. currency: amount.currency,
  117. issuer,
  118. value: amount.value
  119. };
  120. }
  121. exports.toRippledAmount = toRippledAmount;
  122. function convertKeysFromSnakeCaseToCamelCase(obj) {
  123. if (typeof obj === 'object') {
  124. const accumulator = Array.isArray(obj) ? [] : {};
  125. let newKey;
  126. return Object.entries(obj).reduce((result, [key, value]) => {
  127. newKey = key;
  128. const FINDSNAKE = /([a-zA-Z]_[a-zA-Z])/g;
  129. if (FINDSNAKE.test(key)) {
  130. newKey = key.replace(FINDSNAKE, (r) => r[0] + r[2].toUpperCase());
  131. }
  132. result[newKey] = convertKeysFromSnakeCaseToCamelCase(value);
  133. return result;
  134. }, accumulator);
  135. }
  136. return obj;
  137. }
  138. exports.convertKeysFromSnakeCaseToCamelCase = convertKeysFromSnakeCaseToCamelCase;
  139. function removeUndefined(obj) {
  140. return _.omitBy(obj, value => value == null);
  141. }
  142. exports.removeUndefined = removeUndefined;
  143. function rippleToUnixTimestamp(rpepoch) {
  144. return (rpepoch + 0x386d4380) * 1000;
  145. }
  146. function unixToRippleTimestamp(timestamp) {
  147. return Math.round(timestamp / 1000) - 0x386d4380;
  148. }
  149. function rippleTimeToISO8601(rippleTime) {
  150. return new Date(rippleToUnixTimestamp(rippleTime)).toISOString();
  151. }
  152. exports.rippleTimeToISO8601 = rippleTimeToISO8601;
  153. function iso8601ToRippleTime(iso8601) {
  154. return unixToRippleTimestamp(Date.parse(iso8601));
  155. }
  156. exports.iso8601ToRippleTime = iso8601ToRippleTime;
  157. //# sourceMappingURL=utils.js.map