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.

settings.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. const assert = __importStar(require("assert"));
  26. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  27. const utils = __importStar(require("./utils"));
  28. const validate = utils.common.validate;
  29. const AccountSetFlags = utils.common.constants.AccountSetFlags;
  30. const AccountFields = utils.common.constants.AccountFields;
  31. function setTransactionFlags(txJSON, values) {
  32. const keys = Object.keys(values).filter((key) => AccountSetFlags[key] != null);
  33. assert.ok(keys.length <= 1, 'ERROR: can only set one setting per transaction');
  34. const flagName = keys[0];
  35. const value = values[flagName];
  36. const index = AccountSetFlags[flagName];
  37. if (index != null) {
  38. if (value) {
  39. txJSON.SetFlag = index;
  40. }
  41. else {
  42. txJSON.ClearFlag = index;
  43. }
  44. }
  45. }
  46. function setTransactionFields(txJSON, input) {
  47. const fieldSchema = AccountFields;
  48. for (const fieldName in fieldSchema) {
  49. const field = fieldSchema[fieldName];
  50. let value = input[field.name];
  51. if (value === undefined) {
  52. continue;
  53. }
  54. if (value === null && field.hasOwnProperty('defaults')) {
  55. value = field.defaults;
  56. }
  57. if (field.encoding === 'hex' && !field.length) {
  58. value = Buffer.from(value, 'ascii').toString('hex').toUpperCase();
  59. }
  60. txJSON[fieldName] = value;
  61. }
  62. }
  63. function convertTransferRate(transferRate) {
  64. return new bignumber_js_1.default(transferRate).shiftedBy(9).toNumber();
  65. }
  66. function formatSignerEntry(signer) {
  67. return {
  68. SignerEntry: {
  69. Account: signer.address,
  70. SignerWeight: signer.weight
  71. }
  72. };
  73. }
  74. function createSettingsTransactionWithoutMemos(account, settings) {
  75. if (settings.regularKey !== undefined) {
  76. const removeRegularKey = {
  77. TransactionType: 'SetRegularKey',
  78. Account: account
  79. };
  80. if (settings.regularKey === null) {
  81. return removeRegularKey;
  82. }
  83. return Object.assign({}, removeRegularKey, {
  84. RegularKey: settings.regularKey
  85. });
  86. }
  87. if (settings.signers != null) {
  88. const setSignerList = {
  89. TransactionType: 'SignerListSet',
  90. Account: account,
  91. SignerEntries: [],
  92. SignerQuorum: settings.signers.threshold
  93. };
  94. if (settings.signers.weights != null) {
  95. setSignerList.SignerEntries = settings.signers.weights.map(formatSignerEntry);
  96. }
  97. return setSignerList;
  98. }
  99. const txJSON = {
  100. TransactionType: 'AccountSet',
  101. Account: account
  102. };
  103. const settingsWithoutMemos = Object.assign({}, settings);
  104. delete settingsWithoutMemos.memos;
  105. setTransactionFlags(txJSON, settingsWithoutMemos);
  106. setTransactionFields(txJSON, settings);
  107. if (txJSON.TransferRate != null) {
  108. txJSON.TransferRate = convertTransferRate(txJSON.TransferRate);
  109. }
  110. return txJSON;
  111. }
  112. function createSettingsTransaction(account, settings) {
  113. const txJSON = createSettingsTransactionWithoutMemos(account, settings);
  114. if (settings.memos != null) {
  115. txJSON.Memos = settings.memos.map(utils.convertMemo);
  116. }
  117. return txJSON;
  118. }
  119. function prepareSettings(address, settings, instructions = {}) {
  120. try {
  121. validate.prepareSettings({ address, settings, instructions });
  122. const txJSON = createSettingsTransaction(address, settings);
  123. return utils.prepareTransaction(txJSON, this, instructions);
  124. }
  125. catch (e) {
  126. return Promise.reject(e);
  127. }
  128. }
  129. exports.default = prepareSettings;
  130. //# sourceMappingURL=settings.js.map