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.

flags.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.setTransactionFlagsToNumber = exports.parseAccountRootFlags = void 0;
  4. const errors_1 = require("../../errors");
  5. const AccountRoot_1 = require("../ledger/AccountRoot");
  6. const accountSet_1 = require("../transactions/accountSet");
  7. const offerCreate_1 = require("../transactions/offerCreate");
  8. const payment_1 = require("../transactions/payment");
  9. const paymentChannelClaim_1 = require("../transactions/paymentChannelClaim");
  10. const trustSet_1 = require("../transactions/trustSet");
  11. const _1 = require(".");
  12. function parseAccountRootFlags(flags) {
  13. const flagsInterface = {};
  14. Object.keys(AccountRoot_1.AccountRootFlags).forEach((flag) => {
  15. if ((0, _1.isFlagEnabled)(flags, AccountRoot_1.AccountRootFlags[flag])) {
  16. flagsInterface[flag] = true;
  17. }
  18. });
  19. return flagsInterface;
  20. }
  21. exports.parseAccountRootFlags = parseAccountRootFlags;
  22. function setTransactionFlagsToNumber(tx) {
  23. if (tx.Flags == null) {
  24. tx.Flags = 0;
  25. return;
  26. }
  27. if (typeof tx.Flags === 'number') {
  28. return;
  29. }
  30. switch (tx.TransactionType) {
  31. case 'AccountSet':
  32. tx.Flags = convertAccountSetFlagsToNumber(tx.Flags);
  33. return;
  34. case 'OfferCreate':
  35. tx.Flags = convertOfferCreateFlagsToNumber(tx.Flags);
  36. return;
  37. case 'PaymentChannelClaim':
  38. tx.Flags = convertPaymentChannelClaimFlagsToNumber(tx.Flags);
  39. return;
  40. case 'Payment':
  41. tx.Flags = convertPaymentTransactionFlagsToNumber(tx.Flags);
  42. return;
  43. case 'TrustSet':
  44. tx.Flags = convertTrustSetFlagsToNumber(tx.Flags);
  45. return;
  46. default:
  47. tx.Flags = 0;
  48. }
  49. }
  50. exports.setTransactionFlagsToNumber = setTransactionFlagsToNumber;
  51. function convertAccountSetFlagsToNumber(flags) {
  52. return reduceFlags(flags, accountSet_1.AccountSetTfFlags);
  53. }
  54. function convertOfferCreateFlagsToNumber(flags) {
  55. return reduceFlags(flags, offerCreate_1.OfferCreateFlags);
  56. }
  57. function convertPaymentChannelClaimFlagsToNumber(flags) {
  58. return reduceFlags(flags, paymentChannelClaim_1.PaymentChannelClaimFlags);
  59. }
  60. function convertPaymentTransactionFlagsToNumber(flags) {
  61. return reduceFlags(flags, payment_1.PaymentFlags);
  62. }
  63. function convertTrustSetFlagsToNumber(flags) {
  64. return reduceFlags(flags, trustSet_1.TrustSetFlags);
  65. }
  66. function reduceFlags(flags, flagEnum) {
  67. return Object.keys(flags).reduce((resultFlags, flag) => {
  68. if (flagEnum[flag] == null) {
  69. throw new errors_1.ValidationError(`flag ${flag} doesn't exist in flagEnum: ${JSON.stringify(flagEnum)}`);
  70. }
  71. return flags[flag] ? resultFlags | flagEnum[flag] : resultFlags;
  72. }, 0);
  73. }
  74. //# sourceMappingURL=flags.js.map