@arcblock/validator 1.14.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2018-2019 ArcBlock
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # OCAP State
2
+
3
+ Defines core data structures for states in OCAP server.
@@ -0,0 +1,130 @@
1
+ const { BN } = require('@ocap/util');
2
+
3
+ module.exports = (Joi) => ({
4
+ type: 'BN',
5
+ base: Joi.any(),
6
+ messages: {
7
+ 'bn.nan': '{{#label}} is not a big number',
8
+ 'bn.max': '{{#label}} needs to be less than or equal to "{{#threshold}}"',
9
+ 'bn.min': '{{#label}} needs to be greater than or equal to "{{#threshold}}"',
10
+ 'bn.less': '{{#label}} needs to be less than "{{#threshold}}"',
11
+ 'bn.greater': '{{#label}} needs to be greater than "{{#threshold}}"',
12
+ 'bn.positive': '{{#label}} needs to be positive',
13
+ 'bn.negative': '{{#label}} needs to be negative',
14
+ },
15
+
16
+ prepare(value, helpers) {
17
+ try {
18
+ return { value: new BN(value) };
19
+ } catch (err) {
20
+ return { errors: helpers.error('bn.nan') };
21
+ }
22
+ },
23
+ coerce(value) {
24
+ return { value: value.toString(10) };
25
+ },
26
+
27
+ validate(value) {
28
+ return { value };
29
+ },
30
+
31
+ rules: {
32
+ gt: {
33
+ args: [
34
+ {
35
+ name: 'threshold',
36
+ ref: true,
37
+ assert: (v) => BN.isBN(v),
38
+ message: 'must be a big number',
39
+ normalize: (v) => new BN(v),
40
+ },
41
+ {
42
+ name: 'equal',
43
+ assert: (v) => typeof v === 'boolean',
44
+ message: 'must be a boolean',
45
+ },
46
+ ],
47
+ // The rule return structure is different from the root
48
+ // eslint-disable-next-line consistent-return
49
+ validate(value, helpers, args) {
50
+ const v = new BN(value);
51
+ if (args.equal && v.lt(args.threshold)) {
52
+ return helpers.error('bn.min', args);
53
+ }
54
+ if (!args.equal && v.lte(args.threshold)) {
55
+ return helpers.error('bn.greater', args);
56
+ }
57
+
58
+ // must return value when valid
59
+ return value;
60
+ },
61
+ },
62
+
63
+ lt: {
64
+ args: [
65
+ {
66
+ name: 'threshold',
67
+ ref: true,
68
+ assert: (v) => BN.isBN(v),
69
+ message: 'must be a big number',
70
+ normalize: (v) => new BN(v),
71
+ },
72
+ {
73
+ name: 'equal',
74
+ assert: (v) => typeof v === 'boolean',
75
+ message: 'must be a boolean',
76
+ },
77
+ ],
78
+ // The rule return structure is different from the root
79
+ // eslint-disable-next-line consistent-return
80
+ validate(value, helpers, args) {
81
+ const v = new BN(value);
82
+ if (args.equal && v.gt(args.threshold)) {
83
+ return helpers.error('bn.max', args);
84
+ }
85
+ if (!args.equal && v.gte(args.threshold)) {
86
+ return helpers.error('bn.less', args);
87
+ }
88
+
89
+ // must return value when valid
90
+ return value;
91
+ },
92
+ },
93
+
94
+ min: {
95
+ alias: 'gte',
96
+ method(threshold) {
97
+ return this.$_addRule({ name: 'gt', args: { threshold, equal: true } });
98
+ },
99
+ },
100
+ max: {
101
+ alias: 'lte',
102
+ method(threshold) {
103
+ return this.$_addRule({ name: 'lt', args: { threshold, equal: true } });
104
+ },
105
+ },
106
+ greater: {
107
+ alias: 'gt',
108
+ method(threshold) {
109
+ return this.$_addRule({ name: 'gt', args: { threshold, equal: false } });
110
+ },
111
+ },
112
+ less: {
113
+ alias: 'lt',
114
+ method(threshold) {
115
+ return this.$_addRule({ name: 'lt', args: { threshold, equal: false } });
116
+ },
117
+ },
118
+
119
+ positive: {
120
+ method() {
121
+ return this.$_addRule({ name: 'gt', args: { threshold: 0, equal: false } });
122
+ },
123
+ },
124
+ negative: {
125
+ method() {
126
+ return this.$_addRule({ name: 'lt', args: { threshold: 0, equal: false } });
127
+ },
128
+ },
129
+ },
130
+ });
@@ -0,0 +1,100 @@
1
+ const isEqual = require('lodash/isEqual');
2
+ const { types } = require('@ocap/mcrypto');
3
+ const { isValid, toTypeInfo, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM } = require('@arcblock/did');
4
+
5
+ const ruleTypes = {
6
+ wallet: ['arcblock', 'ethereum', 'default', 'eth'],
7
+ pk: Object.keys(types.KeyType),
8
+ hash: Object.keys(types.HashType),
9
+ role: Object.keys(types.RoleType),
10
+ };
11
+
12
+ module.exports = (Joi) => ({
13
+ type: 'DID',
14
+ base: Joi.string().trim(),
15
+ messages: {
16
+ 'did.empty': 'Expect {{#label}} to be non-empty string, got "{{#value}}"',
17
+ 'did.invalid': 'Expect {{#label}} to be valid did, got "{{#value}}"',
18
+ 'did.wallet': 'Expect wallet type of {{#label}} to be "{{#expected}}" wallet, got "{{#actual}}"',
19
+ 'did.pk': 'Expect pk type of {{#label}} to be "{{#expected}}", got "{{#actual}}"',
20
+ 'did.hash': 'Expect hash type of {{#label}} to be "{{#expected}}", got "{{#actual}}"',
21
+ 'did.role': 'Expect role type of {{#label}} to be "{{#expected}}", got "{{#actual}}"',
22
+ },
23
+
24
+ validate(value, helpers) {
25
+ if (!value || typeof value !== 'string') {
26
+ return { errors: helpers.error('did.empty', { value }) };
27
+ }
28
+
29
+ if (isValid(value) === false) {
30
+ return { errors: helpers.error('did.invalid', { value }) };
31
+ }
32
+
33
+ return { value };
34
+ },
35
+
36
+ rules: {
37
+ type: {
38
+ args: [
39
+ {
40
+ name: 'key',
41
+ ref: true,
42
+ assert: (v) => Object.keys(ruleTypes).includes(v),
43
+ message: `must be one of ${Object.keys(ruleTypes).join(', ')}`,
44
+ },
45
+ {
46
+ name: 'expected',
47
+ assert: (v) => Object.keys(ruleTypes).some((x) => ruleTypes[x].includes(v)),
48
+ message: 'must be valid type',
49
+ },
50
+ ],
51
+ // The rule return structure is different from the root
52
+ // eslint-disable-next-line consistent-return
53
+ validate(value, helpers, args) {
54
+ const type = toTypeInfo(value);
55
+ const typeStr = toTypeInfo(value, true);
56
+
57
+ if (args.key === 'wallet') {
58
+ if (['ethereum', 'eth'].includes(args.expected) && isEqual(type, DID_TYPE_ETHEREUM) === false) {
59
+ return helpers.error('did.wallet', { ...args, actual: JSON.stringify(typeStr) });
60
+ }
61
+ if (['arcblock', 'default'].includes(args.expected) && isEqual(type, DID_TYPE_ARCBLOCK) === false) {
62
+ return helpers.error('did.wallet', { ...args, actual: JSON.stringify(typeStr) });
63
+ }
64
+ }
65
+ if (args.key === 'pk' && typeStr.pk !== args.expected) {
66
+ return helpers.error('did.pk', { ...args, actual: typeStr.pk });
67
+ }
68
+ if (args.key === 'hash' && typeStr.hash !== args.expected) {
69
+ return helpers.error('did.hash', { ...args, actual: typeStr.hash });
70
+ }
71
+ if (args.key === 'role' && typeStr.role !== args.expected) {
72
+ return helpers.error('did.role', { ...args, actual: typeStr.role });
73
+ }
74
+
75
+ return value;
76
+ },
77
+ },
78
+
79
+ wallet: {
80
+ method(type) {
81
+ return this.$_addRule({ name: 'type', args: { key: 'wallet', expected: type } });
82
+ },
83
+ },
84
+ pk: {
85
+ method(type) {
86
+ return this.$_addRule({ name: 'type', args: { key: 'pk', expected: type } });
87
+ },
88
+ },
89
+ hash: {
90
+ method(type) {
91
+ return this.$_addRule({ name: 'type', args: { key: 'hash', expected: type } });
92
+ },
93
+ },
94
+ role: {
95
+ method(type) {
96
+ return this.$_addRule({ name: 'type', args: { key: 'role', expected: type } });
97
+ },
98
+ },
99
+ },
100
+ });
package/lib/index.js ADDED
@@ -0,0 +1,51 @@
1
+ const JOI = require('joi');
2
+
3
+ const bnExtension = require('./extension/bn');
4
+ const didExtension = require('./extension/did');
5
+
6
+ const Joi = JOI.extend(didExtension).extend(bnExtension);
7
+
8
+ const txHash = /^(0x)?([A-Fa-f0-9]{64})$/;
9
+
10
+ const contextSchema = Joi.object({
11
+ genesisTime: Joi.date().iso().required().raw(),
12
+ genesisTx: Joi.string().regex(txHash).required().allow(''),
13
+ renaissanceTime: Joi.date().iso().required().raw(),
14
+ renaissanceTx: Joi.string().regex(txHash).required().allow(''),
15
+ });
16
+
17
+ const tokenInputSchema = Joi.object({
18
+ address: Joi.DID().role('ROLE_TOKEN').required(),
19
+ value: Joi.BN().positive().required(),
20
+ });
21
+
22
+ const multiSigSchema = Joi.array().items({
23
+ signer: Joi.DID().required(),
24
+ pk: Joi.any().required(),
25
+ signature: Joi.any().required(),
26
+ delegator: Joi.DID().valid('').optional(),
27
+ data: Joi.any().optional(),
28
+ });
29
+
30
+ const foreignTokenSchema = Joi.object({
31
+ type: Joi.string().min(1).max(32).required(),
32
+ contractAddress: Joi.DID().wallet('ethereum').required(),
33
+ chainType: Joi.string().min(1).max(32).required(),
34
+ chainName: Joi.string().min(1).max(32).required(),
35
+ chainId: Joi.number().positive().required(),
36
+ });
37
+
38
+ const schemas = {
39
+ context: contextSchema,
40
+ tokenInput: tokenInputSchema,
41
+ multiSig: multiSigSchema,
42
+ foreignToken: foreignTokenSchema,
43
+ };
44
+
45
+ const patterns = {
46
+ txHash,
47
+ };
48
+
49
+ module.exports = Joi;
50
+ module.exports.schemas = schemas;
51
+ module.exports.patterns = patterns;
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@arcblock/validator",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "1.14.10",
7
+ "description": "",
8
+ "main": "lib/index.js",
9
+ "files": [
10
+ "lib"
11
+ ],
12
+ "scripts": {
13
+ "lint": "eslint tests lib",
14
+ "lint:fix": "eslint --fix tests lib",
15
+ "test": "jest --forceExit --detectOpenHandles",
16
+ "coverage": "npm run test -- --coverage"
17
+ },
18
+ "dependencies": {
19
+ "@arcblock/did": "1.14.10",
20
+ "@ocap/mcrypto": "1.14.10",
21
+ "@ocap/util": "1.14.10",
22
+ "joi": "^17.4.2",
23
+ "lodash": "^4.17.21"
24
+ },
25
+ "devDependencies": {
26
+ "jest": "^27.3.1"
27
+ },
28
+ "keywords": [],
29
+ "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
30
+ "license": "MIT",
31
+ "gitHead": "3475efca85274bbd44bf4edd775189f9cabe2188"
32
+ }