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.

payment.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.validatePayment = exports.PaymentFlags = void 0;
  4. const errors_1 = require("../../errors");
  5. const utils_1 = require("../utils");
  6. const common_1 = require("./common");
  7. var PaymentFlags;
  8. (function (PaymentFlags) {
  9. PaymentFlags[PaymentFlags["tfNoDirectRipple"] = 65536] = "tfNoDirectRipple";
  10. PaymentFlags[PaymentFlags["tfPartialPayment"] = 131072] = "tfPartialPayment";
  11. PaymentFlags[PaymentFlags["tfLimitQuality"] = 262144] = "tfLimitQuality";
  12. })(PaymentFlags = exports.PaymentFlags || (exports.PaymentFlags = {}));
  13. function validatePayment(tx) {
  14. (0, common_1.validateBaseTransaction)(tx);
  15. if (tx.Amount === undefined) {
  16. throw new errors_1.ValidationError('PaymentTransaction: missing field Amount');
  17. }
  18. if (!(0, common_1.isAmount)(tx.Amount)) {
  19. throw new errors_1.ValidationError('PaymentTransaction: invalid Amount');
  20. }
  21. if (tx.Destination === undefined) {
  22. throw new errors_1.ValidationError('PaymentTransaction: missing field Destination');
  23. }
  24. if (!(0, common_1.isAmount)(tx.Destination)) {
  25. throw new errors_1.ValidationError('PaymentTransaction: invalid Destination');
  26. }
  27. if (tx.DestinationTag != null && typeof tx.DestinationTag !== 'number') {
  28. throw new errors_1.ValidationError('PaymentTransaction: DestinationTag must be a number');
  29. }
  30. if (tx.InvoiceID !== undefined && typeof tx.InvoiceID !== 'string') {
  31. throw new errors_1.ValidationError('PaymentTransaction: InvoiceID must be a string');
  32. }
  33. if (tx.Paths !== undefined &&
  34. !isPaths(tx.Paths)) {
  35. throw new errors_1.ValidationError('PaymentTransaction: invalid Paths');
  36. }
  37. if (tx.SendMax !== undefined && !(0, common_1.isAmount)(tx.SendMax)) {
  38. throw new errors_1.ValidationError('PaymentTransaction: invalid SendMax');
  39. }
  40. checkPartialPayment(tx);
  41. }
  42. exports.validatePayment = validatePayment;
  43. function checkPartialPayment(tx) {
  44. var _a;
  45. if (tx.DeliverMin != null) {
  46. if (tx.Flags == null) {
  47. throw new errors_1.ValidationError('PaymentTransaction: tfPartialPayment flag required with DeliverMin');
  48. }
  49. const flags = tx.Flags;
  50. const isTfPartialPayment = typeof flags === 'number'
  51. ? (0, utils_1.isFlagEnabled)(flags, PaymentFlags.tfPartialPayment)
  52. : (_a = flags.tfPartialPayment) !== null && _a !== void 0 ? _a : false;
  53. if (!isTfPartialPayment) {
  54. throw new errors_1.ValidationError('PaymentTransaction: tfPartialPayment flag required with DeliverMin');
  55. }
  56. if (!(0, common_1.isAmount)(tx.DeliverMin)) {
  57. throw new errors_1.ValidationError('PaymentTransaction: invalid DeliverMin');
  58. }
  59. }
  60. }
  61. function isPathStep(pathStep) {
  62. if (pathStep.account !== undefined && typeof pathStep.account !== 'string') {
  63. return false;
  64. }
  65. if (pathStep.currency !== undefined &&
  66. typeof pathStep.currency !== 'string') {
  67. return false;
  68. }
  69. if (pathStep.issuer !== undefined && typeof pathStep.issuer !== 'string') {
  70. return false;
  71. }
  72. if (pathStep.account !== undefined &&
  73. pathStep.currency === undefined &&
  74. pathStep.issuer === undefined) {
  75. return true;
  76. }
  77. if (pathStep.currency !== undefined || pathStep.issuer !== undefined) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. function isPath(path) {
  83. for (const pathStep of path) {
  84. if (!isPathStep(pathStep)) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. function isPaths(paths) {
  91. if (!Array.isArray(paths) || paths.length === 0) {
  92. return false;
  93. }
  94. for (const path of paths) {
  95. if (!Array.isArray(path) || path.length === 0) {
  96. return false;
  97. }
  98. if (!isPath(path)) {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. //# sourceMappingURL=payment.js.map