UNPKG

5.39 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const ajv_keywords_1 = __importDefault(require("ajv-keywords"));
7const address_1 = require("../identities/address");
8const managers_1 = require("../managers");
9const utils_1 = require("../utils");
10const maxBytes = (ajv) => {
11 ajv.addKeyword("maxBytes", {
12 type: "string",
13 compile(schema, parentSchema) {
14 return data => {
15 if (parentSchema.type !== "string") {
16 return false;
17 }
18 return Buffer.from(data, "utf8").byteLength <= schema;
19 };
20 },
21 errors: false,
22 metaSchema: {
23 type: "integer",
24 minimum: 0,
25 },
26 });
27};
28const transactionType = (ajv) => {
29 ajv.addKeyword("transactionType", {
30 compile(schema) {
31 return data => {
32 return data === schema;
33 };
34 },
35 errors: false,
36 metaSchema: {
37 type: "integer",
38 minimum: 0,
39 },
40 });
41};
42const network = (ajv) => {
43 ajv.addKeyword("network", {
44 compile(schema) {
45 return data => {
46 return schema && data === managers_1.configManager.get("network.pubKeyHash");
47 };
48 },
49 errors: false,
50 metaSchema: {
51 type: "boolean",
52 },
53 });
54};
55const bignumber = (ajv) => {
56 const instanceOf = ajv_keywords_1.default.get("instanceof").definition;
57 instanceOf.CONSTRUCTORS.BigNumber = utils_1.BigNumber;
58 ajv.addKeyword("bignumber", {
59 compile(schema) {
60 return (data, dataPath, parentObject, property) => {
61 const minimum = typeof schema.minimum !== "undefined" ? schema.minimum : 0;
62 const maximum = typeof schema.maximum !== "undefined" ? schema.maximum : Number.MAX_SAFE_INTEGER;
63 const bignum = utils_1.BigNumber.make(data);
64 if (!bignum.isInteger()) {
65 return false;
66 }
67 let bypassGenesis = false;
68 if (schema.bypassGenesis) {
69 if (parentObject.id) {
70 if (schema.block) {
71 bypassGenesis = parentObject.height === 1;
72 }
73 else {
74 bypassGenesis = utils_1.isGenesisTransaction(parentObject.id);
75 }
76 }
77 }
78 if (bignum.isLessThan(minimum) && !(bignum.isZero() && bypassGenesis)) {
79 return false;
80 }
81 if (bignum.isGreaterThan(maximum) && !bypassGenesis) {
82 return false;
83 }
84 if (parentObject && property) {
85 parentObject[property] = bignum;
86 }
87 return true;
88 };
89 },
90 errors: false,
91 modifying: true,
92 metaSchema: {
93 type: "object",
94 properties: {
95 minimum: { type: "integer" },
96 maximum: { type: "integer" },
97 bypassGenesis: { type: "boolean" },
98 block: { type: "boolean" },
99 },
100 additionalItems: false,
101 },
102 });
103};
104const blockId = (ajv) => {
105 ajv.addKeyword("blockId", {
106 compile(schema) {
107 return (data, dataPath, parentObject) => {
108 if (parentObject && parentObject.height === 1 && schema.allowNullWhenGenesis) {
109 return !data || Number(data) === 0;
110 }
111 if (typeof data !== "string") {
112 return false;
113 }
114 // Partial SHA256 block id (old/legacy), before the switch to full SHA256.
115 // 8 byte integer either decimal without leading zeros or hex with leading zeros.
116 const isPartial = /^[0-9]{1,20}$/.test(data) || /^[0-9a-f]{16}$/i.test(data);
117 const isFullSha256 = /^[0-9a-f]{64}$/i.test(data);
118 if (parentObject && parentObject.height) {
119 const height = schema.isPreviousBlock ? parentObject.height - 1 : parentObject.height;
120 const constants = managers_1.configManager.getMilestone(height);
121 return constants.block.idFullSha256 ? isFullSha256 : isPartial;
122 }
123 return isPartial || isFullSha256;
124 };
125 },
126 errors: false,
127 metaSchema: {
128 type: "object",
129 properties: {
130 allowNullWhenGenesis: { type: "boolean" },
131 isPreviousBlock: { type: "boolean" },
132 },
133 additionalItems: false,
134 },
135 });
136};
137const addressOnNetwork = (ajv) => {
138 ajv.addKeyword("addressOnNetwork", {
139 compile(schema) {
140 return data => {
141 return schema && address_1.Address.validate(data);
142 };
143 },
144 errors: false,
145 metaSchema: {
146 type: "boolean",
147 },
148 });
149};
150exports.keywords = [bignumber, blockId, maxBytes, network, transactionType, addressOnNetwork];
151//# sourceMappingURL=keywords.js.map
\No newline at end of file