-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz_schema.js
118 lines (94 loc) · 2.26 KB
/
z_schema.js
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
var ip = require('ip');
var bs58check = require('bs58check');
function schema(network){
this.z_schema = require('z-schema');
this.z_schema.registerFormat('hex', function (str) {
try {
new Buffer(str, 'hex');
} catch (e) {
return false;
}
return true;
});
this.z_schema.registerFormat('publicKey', function (str) {
if (str.length === 0) {
return true;
}
try {
var publicKey = new Buffer(str, 'hex');
return publicKey.length === 33;
} catch (e) {
return false;
}
});
this.z_schema.registerFormat('address', function (str) {
if (str.length === 0) {
return true;
}
var version = network.pubKeyHash;
try {
var decode = bs58check.decode(str);
return decode[0] == version;
} catch(e){
return false;
}
});
this.z_schema.registerFormat('vendorField', function (str) {
if (str.length === 0) {
return true;
}
try {
var vendorField = new Buffer(str);
return vendorField.length < 65;
} catch (e) {
return false;
}
});
this.z_schema.registerFormat('csv', function (str) {
try {
var a = str.split(',');
if (a.length > 0 && a.length <= 1000) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
});
this.z_schema.registerFormat('signature', function (str) {
if (str.length === 0) {
return true;
}
try {
var signature = new Buffer(str, 'hex');
return signature.length < 73;
} catch (e) {
return false;
}
});
this.z_schema.registerFormat('queryList', function (obj) {
obj.limit = 100;
return true;
});
this.z_schema.registerFormat('delegatesList', function (obj) {
obj.limit = 51;
return true;
});
this.z_schema.registerFormat('parsedInt', function (value) {
/*jslint eqeq: true*/
if (isNaN(value) || parseInt(value) != value || isNaN(parseInt(value, 10))) {
return false;
}
value = parseInt(value);
return true;
});
this.z_schema.registerFormat('ip', function (str) {
return ip.isV4Format(str);
});
}
// var registeredFormats = z_schema.getRegisteredFormats();
// console.log(registeredFormats);
// Exports
module.exports = schema;