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.

common.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseAmountValue = exports.validateBaseTransaction = exports.isAmount = exports.isIssuedCurrency = void 0;
  4. const ripple_binary_codec_1 = require("ripple-binary-codec");
  5. const errors_1 = require("../../errors");
  6. const utils_1 = require("../utils");
  7. const MEMO_SIZE = 3;
  8. function isMemo(obj) {
  9. if (obj.Memo == null) {
  10. return false;
  11. }
  12. const memo = obj.Memo;
  13. const size = Object.keys(memo).length;
  14. const validData = memo.MemoData == null || typeof memo.MemoData === 'string';
  15. const validFormat = memo.MemoFormat == null || typeof memo.MemoFormat === 'string';
  16. const validType = memo.MemoType == null || typeof memo.MemoType === 'string';
  17. return (size >= 1 &&
  18. size <= MEMO_SIZE &&
  19. validData &&
  20. validFormat &&
  21. validType &&
  22. (0, utils_1.onlyHasFields)(memo, ['MemoFormat', 'MemoData', 'MemoType']));
  23. }
  24. const SIGNER_SIZE = 3;
  25. function isSigner(obj) {
  26. const signerWrapper = obj;
  27. if (signerWrapper.Signer == null) {
  28. return false;
  29. }
  30. const signer = signerWrapper.Signer;
  31. return (Object.keys(signer).length === SIGNER_SIZE &&
  32. typeof signer.Account === 'string' &&
  33. typeof signer.TxnSignature === 'string' &&
  34. typeof signer.SigningPubKey === 'string');
  35. }
  36. const ISSUED_CURRENCY_SIZE = 3;
  37. function isRecord(value) {
  38. return value !== null && typeof value === 'object';
  39. }
  40. function isIssuedCurrency(input) {
  41. return (isRecord(input) &&
  42. Object.keys(input).length === ISSUED_CURRENCY_SIZE &&
  43. typeof input.value === 'string' &&
  44. typeof input.issuer === 'string' &&
  45. typeof input.currency === 'string');
  46. }
  47. exports.isIssuedCurrency = isIssuedCurrency;
  48. function isAmount(amount) {
  49. return typeof amount === 'string' || isIssuedCurrency(amount);
  50. }
  51. exports.isAmount = isAmount;
  52. function validateBaseTransaction(common) {
  53. if (common.Account === undefined) {
  54. throw new errors_1.ValidationError('BaseTransaction: missing field Account');
  55. }
  56. if (typeof common.Account !== 'string') {
  57. throw new errors_1.ValidationError('BaseTransaction: Account not string');
  58. }
  59. if (common.TransactionType === undefined) {
  60. throw new errors_1.ValidationError('BaseTransaction: missing field TransactionType');
  61. }
  62. if (typeof common.TransactionType !== 'string') {
  63. throw new errors_1.ValidationError('BaseTransaction: TransactionType not string');
  64. }
  65. if (!ripple_binary_codec_1.TRANSACTION_TYPES.includes(common.TransactionType)) {
  66. throw new errors_1.ValidationError('BaseTransaction: Unknown TransactionType');
  67. }
  68. if (common.Fee !== undefined && typeof common.Fee !== 'string') {
  69. throw new errors_1.ValidationError('BaseTransaction: invalid Fee');
  70. }
  71. if (common.Sequence !== undefined && typeof common.Sequence !== 'number') {
  72. throw new errors_1.ValidationError('BaseTransaction: invalid Sequence');
  73. }
  74. if (common.AccountTxnID !== undefined &&
  75. typeof common.AccountTxnID !== 'string') {
  76. throw new errors_1.ValidationError('BaseTransaction: invalid AccountTxnID');
  77. }
  78. if (common.LastLedgerSequence !== undefined &&
  79. typeof common.LastLedgerSequence !== 'number') {
  80. throw new errors_1.ValidationError('BaseTransaction: invalid LastLedgerSequence');
  81. }
  82. const memos = common.Memos;
  83. if (memos !== undefined && !memos.every(isMemo)) {
  84. throw new errors_1.ValidationError('BaseTransaction: invalid Memos');
  85. }
  86. const signers = common.Signers;
  87. if (signers !== undefined &&
  88. (signers.length === 0 || !signers.every(isSigner))) {
  89. throw new errors_1.ValidationError('BaseTransaction: invalid Signers');
  90. }
  91. if (common.SourceTag !== undefined && typeof common.SourceTag !== 'number') {
  92. throw new errors_1.ValidationError('BaseTransaction: invalid SourceTag');
  93. }
  94. if (common.SigningPubKey !== undefined &&
  95. typeof common.SigningPubKey !== 'string') {
  96. throw new errors_1.ValidationError('BaseTransaction: invalid SigningPubKey');
  97. }
  98. if (common.TicketSequence !== undefined &&
  99. typeof common.TicketSequence !== 'number') {
  100. throw new errors_1.ValidationError('BaseTransaction: invalid TicketSequence');
  101. }
  102. if (common.TxnSignature !== undefined &&
  103. typeof common.TxnSignature !== 'string') {
  104. throw new errors_1.ValidationError('BaseTransaction: invalid TxnSignature');
  105. }
  106. }
  107. exports.validateBaseTransaction = validateBaseTransaction;
  108. function parseAmountValue(amount) {
  109. if (!isAmount(amount)) {
  110. return NaN;
  111. }
  112. if (typeof amount === 'string') {
  113. return parseFloat(amount);
  114. }
  115. return parseFloat(amount.value);
  116. }
  117. exports.parseAmountValue = parseAmountValue;
  118. //# sourceMappingURL=common.js.map