1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const ajv_keywords_1 = __importDefault(require("ajv-keywords"));
|
7 | const address_1 = require("../identities/address");
|
8 | const managers_1 = require("../managers");
|
9 | const utils_1 = require("../utils");
|
10 | const 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 | };
|
28 | const 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 | };
|
42 | const 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 | };
|
55 | const 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 | };
|
104 | const 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 |
|
115 |
|
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 | };
|
137 | const 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 | };
|
150 | exports.keywords = [bignumber, blockId, maxBytes, network, transactionType, addressOnNetwork];
|
151 |
|
\ | No newline at end of file |