1 | var pkgVersion = require("./package.json").version;
|
2 | var Ajv = require("ajv");
|
3 | var util = require("util");
|
4 |
|
5 | var contractObjectSchema = require("./spec/contract-object.spec.json");
|
6 | var networkObjectSchema = require("./spec/network-object.spec.json");
|
7 | var abiSchema = require("./spec/abi.spec.json");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const sanitizedValue = dirtyValueArray => {
|
34 | let sanitizedValueArray = [];
|
35 | dirtyValueArray.forEach(item => {
|
36 | let sanitizedItem = Object.assign({}, item);
|
37 | delete sanitizedItem.signature;
|
38 | sanitizedValueArray.push(sanitizedItem);
|
39 | });
|
40 | return sanitizedValueArray;
|
41 | };
|
42 |
|
43 |
|
44 | const sanitizeEvent = dirtyEvent =>
|
45 | Object.entries(dirtyEvent).reduce(
|
46 | (acc, [property, value]) =>
|
47 | property === "signature"
|
48 | ? acc
|
49 | : Object.assign(acc, { [property]: value }),
|
50 | {}
|
51 | );
|
52 |
|
53 |
|
54 | const sanitizeAllEvents = dirtyEvents =>
|
55 | Object.entries(dirtyEvents).reduce(
|
56 | (acc, [property, event]) =>
|
57 | Object.assign(acc, { [property]: sanitizeEvent(event) }),
|
58 | {}
|
59 | );
|
60 |
|
61 | var properties = {
|
62 | contractName: {
|
63 | sources: ["contractName", "contract_name"]
|
64 | },
|
65 | abi: {
|
66 | sources: ["abi", "interface"],
|
67 | transform: function (value) {
|
68 | if (typeof value === "string") {
|
69 | try {
|
70 | value = JSON.parse(value);
|
71 | } catch (_) {
|
72 | value = undefined;
|
73 | }
|
74 | }
|
75 | if (Array.isArray(value)) {
|
76 | return sanitizedValue(value);
|
77 | }
|
78 | return value;
|
79 | }
|
80 | },
|
81 | metadata: {
|
82 | sources: ["metadata"]
|
83 | },
|
84 | bytecode: {
|
85 | sources: ["bytecode", "binary", "unlinked_binary", "evm.bytecode.object"],
|
86 | transform: function (value) {
|
87 | if (value && value.indexOf("0x") !== 0) {
|
88 | value = "0x" + value;
|
89 | }
|
90 | return value;
|
91 | }
|
92 | },
|
93 | deployedBytecode: {
|
94 | sources: [
|
95 | "deployedBytecode",
|
96 | "runtimeBytecode",
|
97 | "evm.deployedBytecode.object"
|
98 | ],
|
99 | transform: function (value) {
|
100 | if (value && value.indexOf("0x") !== 0) {
|
101 | value = "0x" + value;
|
102 | }
|
103 | return value;
|
104 | }
|
105 | },
|
106 | immutableReferences: {},
|
107 | generatedSources: {},
|
108 | deployedGeneratedSources: {},
|
109 | sourceMap: {
|
110 | transform: function (value) {
|
111 | if (typeof value === "string") {
|
112 | try {
|
113 | return JSON.parse(value);
|
114 | } catch (_) {
|
115 | return value;
|
116 | }
|
117 | } else {
|
118 | return value;
|
119 | }
|
120 | },
|
121 | sources: ["sourceMap", "srcmap", "evm.bytecode.sourceMap"]
|
122 | },
|
123 | deployedSourceMap: {
|
124 | transform: function (value) {
|
125 | if (typeof value === "string") {
|
126 | try {
|
127 | return JSON.parse(value);
|
128 | } catch (_) {
|
129 | return value;
|
130 | }
|
131 | } else {
|
132 | return value;
|
133 | }
|
134 | },
|
135 | sources: [
|
136 | "deployedSourceMap",
|
137 | "srcmapRuntime",
|
138 | "evm.deployedBytecode.sourceMap"
|
139 | ]
|
140 | },
|
141 | source: {},
|
142 | sourcePath: {},
|
143 | ast: {},
|
144 | legacyAST: {
|
145 | transform: function (value, obj) {
|
146 | if (value) {
|
147 | return value;
|
148 | } else {
|
149 | return obj.ast;
|
150 | }
|
151 | }
|
152 | },
|
153 | compiler: {},
|
154 | networks: {
|
155 | |
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | transform: function (value = {}, obj) {
|
165 |
|
166 | Object.keys(value).forEach(networkId => {
|
167 | if (value[networkId].events) {
|
168 | value[networkId].events = sanitizeAllEvents(value[networkId].events);
|
169 | }
|
170 | });
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | const networkId = obj.network_id;
|
176 | if (networkId && value.hasOwnProperty(networkId)) {
|
177 | value[networkId].links = obj.links;
|
178 | value[networkId].events = sanitizeAllEvents(obj.events);
|
179 | }
|
180 |
|
181 | return value;
|
182 | }
|
183 | },
|
184 | schemaVersion: {
|
185 | sources: ["schemaVersion", "schema_version"]
|
186 | },
|
187 | updatedAt: {
|
188 | sources: ["updatedAt", "updated_at"],
|
189 | transform: function (value) {
|
190 | if (typeof value === "number") {
|
191 | value = new Date(value).toISOString();
|
192 | }
|
193 | return value;
|
194 | }
|
195 | },
|
196 | networkType: {},
|
197 | devdoc: {},
|
198 | userdoc: {},
|
199 | db: {}
|
200 | };
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | function getter(key, transform) {
|
209 | if (transform === undefined) {
|
210 | transform = function (x) {
|
211 | return x;
|
212 | };
|
213 | }
|
214 |
|
215 | return function (obj) {
|
216 | try {
|
217 | return transform(obj[key]);
|
218 | } catch (_) {
|
219 | return undefined;
|
220 | }
|
221 | };
|
222 | }
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | function chain() {
|
235 | var getters = Array.prototype.slice.call(arguments);
|
236 | return function (obj) {
|
237 | return getters.reduce(function (cur, get) {
|
238 | return get(cur);
|
239 | }, obj);
|
240 | };
|
241 | }
|
242 |
|
243 | // Schema module
|
244 | //
|
245 |
|
246 | var TruffleContractSchema = {
|
247 |
|
248 |
|
249 |
|
250 | validate: function (contractObj) {
|
251 | var ajv = new Ajv({ verbose: true });
|
252 | ajv.addSchema(abiSchema);
|
253 | ajv.addSchema(networkObjectSchema);
|
254 | ajv.addSchema(contractObjectSchema);
|
255 | if (ajv.validate("contract-object.spec.json", contractObj)) {
|
256 | return contractObj;
|
257 | } else {
|
258 | const message = `Schema validation failed. Errors:\n\n${ajv.errors
|
259 | .map(
|
260 | ({
|
261 | keyword,
|
262 | dataPath,
|
263 | schemaPath,
|
264 | params,
|
265 | message,
|
266 | data,
|
267 | parentSchema
|
268 | }) =>
|
269 | util.format(
|
270 | "%s (%s):\n%s\n",
|
271 | message,
|
272 | keyword,
|
273 | util.inspect(
|
274 | {
|
275 | dataPath,
|
276 | schemaPath,
|
277 | params,
|
278 | data,
|
279 | parentSchema
|
280 | },
|
281 | { depth: 5 }
|
282 | )
|
283 | )
|
284 | )
|
285 | .join("\n")}`;
|
286 | const error = new Error(message);
|
287 | error.errors = ajv.errors;
|
288 | throw error;
|
289 | }
|
290 | },
|
291 |
|
292 |
|
293 |
|
294 | normalize: function (objDirty, options) {
|
295 | options = options || {};
|
296 | var normalized = {};
|
297 |
|
298 |
|
299 | Object.keys(properties).forEach(function (key) {
|
300 | var property = properties[key];
|
301 | var value;
|
302 |
|
303 |
|
304 |
|
305 | var sources = property.sources || [key];
|
306 |
|
307 |
|
308 | for (var i = 0; value === undefined && i < sources.length; i++) {
|
309 | var source = sources[i];
|
310 |
|
311 |
|
312 | if (typeof source === "string") {
|
313 | var traversals = source.split(".").map(function (k) {
|
314 | return getter(k);
|
315 | });
|
316 | source = chain.apply(null, traversals);
|
317 | }
|
318 |
|
319 |
|
320 |
|
321 | value = source(objDirty);
|
322 | }
|
323 |
|
324 |
|
325 |
|
326 | if (property.transform) {
|
327 | value = property.transform(value, objDirty);
|
328 | }
|
329 |
|
330 |
|
331 | normalized[key] = value;
|
332 | });
|
333 |
|
334 |
|
335 | Object.keys(objDirty).forEach(function (key) {
|
336 | if (key.indexOf("x-") === 0) {
|
337 | normalized[key] = getter(key)(objDirty);
|
338 | }
|
339 | });
|
340 |
|
341 |
|
342 | normalized.schemaVersion = pkgVersion;
|
343 |
|
344 | if (options.validate) {
|
345 | this.validate(normalized);
|
346 | }
|
347 |
|
348 | return normalized;
|
349 | }
|
350 | };
|
351 |
|
352 | module.exports = TruffleContractSchema;
|