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.

schema-validator.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. Object.defineProperty(exports, "__esModule", { value: true });
  22. exports.isValidAddress = exports.isValidSecret = exports.schemaValidate = void 0;
  23. const _ = __importStar(require("lodash"));
  24. const assert = __importStar(require("assert"));
  25. const { Validator } = require('jsonschema');
  26. const errors_1 = require("./errors");
  27. const ripple_address_codec_1 = require("ripple-address-codec");
  28. const utils_1 = require("./utils");
  29. Object.defineProperty(exports, "isValidSecret", { enumerable: true, get: function () { return utils_1.isValidSecret; } });
  30. function loadSchemas() {
  31. const schemas = [
  32. require('./schemas/objects/tx-json.json'),
  33. require('./schemas/objects/transaction-type.json'),
  34. require('./schemas/objects/hash128.json'),
  35. require('./schemas/objects/hash256.json'),
  36. require('./schemas/objects/sequence.json'),
  37. require('./schemas/objects/ticket-sequence.json'),
  38. require('./schemas/objects/signature.json'),
  39. require('./schemas/objects/issue.json'),
  40. require('./schemas/objects/ledger-version.json'),
  41. require('./schemas/objects/max-adjustment.json'),
  42. require('./schemas/objects/memo.json'),
  43. require('./schemas/objects/memos.json'),
  44. require('./schemas/objects/public-key.json'),
  45. require('./schemas/objects/private-key.json'),
  46. require('./schemas/objects/uint32.json'),
  47. require('./schemas/objects/value.json'),
  48. require('./schemas/objects/source-adjustment.json'),
  49. require('./schemas/objects/destination-adjustment.json'),
  50. require('./schemas/objects/tag.json'),
  51. require('./schemas/objects/lax-amount.json'),
  52. require('./schemas/objects/lax-lax-amount.json'),
  53. require('./schemas/objects/min-adjustment.json'),
  54. require('./schemas/objects/source-exact-adjustment.json'),
  55. require('./schemas/objects/destination-exact-adjustment.json'),
  56. require('./schemas/objects/destination-address-tag.json'),
  57. require('./schemas/objects/transaction-hash.json'),
  58. require('./schemas/objects/address.json'),
  59. require('./schemas/objects/x-address.json'),
  60. require('./schemas/objects/classic-address.json'),
  61. require('./schemas/objects/adjustment.json'),
  62. require('./schemas/objects/quality.json'),
  63. require('./schemas/objects/amount.json'),
  64. require('./schemas/objects/amountbase.json'),
  65. require('./schemas/objects/balance.json'),
  66. require('./schemas/objects/blob.json'),
  67. require('./schemas/objects/currency.json'),
  68. require('./schemas/objects/signed-value.json'),
  69. require('./schemas/objects/orderbook.json'),
  70. require('./schemas/objects/instructions.json'),
  71. require('./schemas/objects/settings-plus-memos.json'),
  72. require('./schemas/specifications/settings.json'),
  73. require('./schemas/specifications/payment.json'),
  74. require('./schemas/specifications/get-payment.json'),
  75. require('./schemas/specifications/escrow-cancellation.json'),
  76. require('./schemas/specifications/order-cancellation.json'),
  77. require('./schemas/specifications/order.json'),
  78. require('./schemas/specifications/escrow-execution.json'),
  79. require('./schemas/specifications/escrow-creation.json'),
  80. require('./schemas/specifications/payment-channel-create.json'),
  81. require('./schemas/specifications/payment-channel-fund.json'),
  82. require('./schemas/specifications/payment-channel-claim.json'),
  83. require('./schemas/specifications/check-create.json'),
  84. require('./schemas/specifications/check-cash.json'),
  85. require('./schemas/specifications/check-cancel.json'),
  86. require('./schemas/specifications/trustline.json'),
  87. require('./schemas/specifications/deposit-preauth.json'),
  88. require('./schemas/specifications/account-delete.json'),
  89. require('./schemas/output/sign.json'),
  90. require('./schemas/output/submit.json'),
  91. require('./schemas/output/get-account-info.json'),
  92. require('./schemas/output/get-account-objects.json'),
  93. require('./schemas/output/get-balances.json'),
  94. require('./schemas/output/get-balance-sheet.json'),
  95. require('./schemas/output/get-ledger.json'),
  96. require('./schemas/output/get-orderbook.json'),
  97. require('./schemas/output/get-orders.json'),
  98. require('./schemas/output/order-change.json'),
  99. require('./schemas/output/get-payment-channel.json'),
  100. require('./schemas/output/prepare.json'),
  101. require('./schemas/output/ledger-event.json'),
  102. require('./schemas/output/get-paths.json'),
  103. require('./schemas/output/get-server-info.json'),
  104. require('./schemas/output/get-settings.json'),
  105. require('./schemas/output/orderbook-orders.json'),
  106. require('./schemas/output/outcome.json'),
  107. require('./schemas/output/get-transaction.json'),
  108. require('./schemas/output/get-transactions.json'),
  109. require('./schemas/output/get-trustlines.json'),
  110. require('./schemas/output/sign-payment-channel-claim.json'),
  111. require('./schemas/output/verify-payment-channel-claim.json'),
  112. require('./schemas/input/get-balances.json'),
  113. require('./schemas/input/get-balance-sheet.json'),
  114. require('./schemas/input/get-ledger.json'),
  115. require('./schemas/input/get-orders.json'),
  116. require('./schemas/input/get-orderbook.json'),
  117. require('./schemas/input/get-paths.json'),
  118. require('./schemas/input/get-payment-channel.json'),
  119. require('./schemas/input/api-options.json'),
  120. require('./schemas/input/get-settings.json'),
  121. require('./schemas/input/get-account-info.json'),
  122. require('./schemas/input/get-account-objects.json'),
  123. require('./schemas/input/get-transaction.json'),
  124. require('./schemas/input/get-transactions.json'),
  125. require('./schemas/input/get-trustlines.json'),
  126. require('./schemas/input/prepare-payment.json'),
  127. require('./schemas/input/prepare-order.json'),
  128. require('./schemas/input/prepare-trustline.json'),
  129. require('./schemas/input/prepare-order-cancellation.json'),
  130. require('./schemas/input/prepare-settings.json'),
  131. require('./schemas/input/prepare-escrow-creation.json'),
  132. require('./schemas/input/prepare-escrow-cancellation.json'),
  133. require('./schemas/input/prepare-escrow-execution.json'),
  134. require('./schemas/input/prepare-payment-channel-create.json'),
  135. require('./schemas/input/prepare-payment-channel-fund.json'),
  136. require('./schemas/input/prepare-payment-channel-claim.json'),
  137. require('./schemas/input/prepare-check-create.json'),
  138. require('./schemas/input/prepare-check-cash.json'),
  139. require('./schemas/input/prepare-check-cancel.json'),
  140. require('./schemas/input/prepare-ticket-create.json'),
  141. require('./schemas/input/compute-ledger-hash.json'),
  142. require('./schemas/input/sign.json'),
  143. require('./schemas/input/submit.json'),
  144. require('./schemas/input/generate-address.json'),
  145. require('./schemas/input/sign-payment-channel-claim.json'),
  146. require('./schemas/input/verify-payment-channel-claim.json'),
  147. require('./schemas/input/combine.json')
  148. ];
  149. const titles = schemas.map((schema) => schema.title);
  150. const duplicates = Object.keys(_.pickBy(_.countBy(titles), (count) => count > 1));
  151. assert.ok(duplicates.length === 0, 'Duplicate schemas for: ' + duplicates);
  152. const validator = new Validator();
  153. validator.customFormats.xAddress = function (instance) {
  154. if (instance == null) {
  155. return true;
  156. }
  157. return ripple_address_codec_1.isValidXAddress(instance);
  158. };
  159. validator.customFormats.classicAddress = function (instance) {
  160. if (instance == null) {
  161. return true;
  162. }
  163. return isValidAddress(instance);
  164. };
  165. validator.customFormats.secret = function (instance) {
  166. if (instance == null) {
  167. return true;
  168. }
  169. return utils_1.isValidSecret(instance);
  170. };
  171. schemas.forEach((schema) => validator.addSchema(schema, '/' + schema.title));
  172. return validator;
  173. }
  174. const schemaValidator = loadSchemas();
  175. function schemaValidate(schemaName, object) {
  176. const schema = schemaValidator.getSchema('/' + schemaName);
  177. if (schema == null) {
  178. throw new errors_1.ValidationError('no schema for ' + schemaName);
  179. }
  180. const result = schemaValidator.validate(object, schema);
  181. if (!result.valid) {
  182. throw new errors_1.ValidationError(result.errors.join());
  183. }
  184. }
  185. exports.schemaValidate = schemaValidate;
  186. function isValidAddress(address) {
  187. return ripple_address_codec_1.isValidXAddress(address) || ripple_address_codec_1.isValidClassicAddress(address);
  188. }
  189. exports.isValidAddress = isValidAddress;
  190. //# sourceMappingURL=schema-validator.js.map