@arcblock/validator 1.16.16 → 1.16.17
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/lib/extension/bn.d.ts +96 -0
- package/lib/extension/bn.js +133 -126
- package/lib/extension/did.d.ts +55 -0
- package/lib/extension/did.js +101 -96
- package/lib/index.d.ts +17 -0
- package/lib/index.js +37 -44
- package/package.json +22 -9
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Root, AnySchema } from 'joi';
|
|
2
|
+
import BN from 'bn.js';
|
|
3
|
+
export interface BNSchema extends AnySchema {
|
|
4
|
+
min(threshold: any): this;
|
|
5
|
+
gte(threshold: any): this;
|
|
6
|
+
max(threshold: any): this;
|
|
7
|
+
lte(threshold: any): this;
|
|
8
|
+
greater(threshold: any): this;
|
|
9
|
+
gt(threshold: any): this;
|
|
10
|
+
less(threshold: any): this;
|
|
11
|
+
lt(threshold: any): this;
|
|
12
|
+
positive(): this;
|
|
13
|
+
negative(): this;
|
|
14
|
+
}
|
|
15
|
+
export declare function BNExtension(root: Root): {
|
|
16
|
+
type: string;
|
|
17
|
+
base: AnySchema<any>;
|
|
18
|
+
messages: {
|
|
19
|
+
'bn.nan': string;
|
|
20
|
+
'bn.max': string;
|
|
21
|
+
'bn.min': string;
|
|
22
|
+
'bn.less': string;
|
|
23
|
+
'bn.greater': string;
|
|
24
|
+
'bn.positive': string;
|
|
25
|
+
'bn.negative': string;
|
|
26
|
+
};
|
|
27
|
+
prepare(value: any, helpers: any): {
|
|
28
|
+
value: BN;
|
|
29
|
+
errors?: undefined;
|
|
30
|
+
} | {
|
|
31
|
+
errors: any;
|
|
32
|
+
value?: undefined;
|
|
33
|
+
};
|
|
34
|
+
coerce(value: BN): {
|
|
35
|
+
value: string;
|
|
36
|
+
};
|
|
37
|
+
validate(value: any): {
|
|
38
|
+
value: any;
|
|
39
|
+
};
|
|
40
|
+
rules: {
|
|
41
|
+
gt: {
|
|
42
|
+
args: ({
|
|
43
|
+
name: string;
|
|
44
|
+
ref: boolean;
|
|
45
|
+
assert: (v: any) => boolean;
|
|
46
|
+
message: string;
|
|
47
|
+
normalize: (v: any) => BN;
|
|
48
|
+
} | {
|
|
49
|
+
name: string;
|
|
50
|
+
assert: (v: any) => boolean;
|
|
51
|
+
message: string;
|
|
52
|
+
ref?: undefined;
|
|
53
|
+
normalize?: undefined;
|
|
54
|
+
})[];
|
|
55
|
+
validate(value: any, helpers: any, args: any): any;
|
|
56
|
+
};
|
|
57
|
+
lt: {
|
|
58
|
+
args: ({
|
|
59
|
+
name: string;
|
|
60
|
+
ref: boolean;
|
|
61
|
+
assert: (v: any) => boolean;
|
|
62
|
+
message: string;
|
|
63
|
+
normalize: (v: any) => BN;
|
|
64
|
+
} | {
|
|
65
|
+
name: string;
|
|
66
|
+
assert: (v: any) => boolean;
|
|
67
|
+
message: string;
|
|
68
|
+
ref?: undefined;
|
|
69
|
+
normalize?: undefined;
|
|
70
|
+
})[];
|
|
71
|
+
validate(value: any, helpers: any, args: any): any;
|
|
72
|
+
};
|
|
73
|
+
min: {
|
|
74
|
+
alias: string;
|
|
75
|
+
method(threshold: any): BNSchema;
|
|
76
|
+
};
|
|
77
|
+
max: {
|
|
78
|
+
alias: string;
|
|
79
|
+
method(threshold: any): BNSchema;
|
|
80
|
+
};
|
|
81
|
+
greater: {
|
|
82
|
+
alias: string;
|
|
83
|
+
method(threshold: any): BNSchema;
|
|
84
|
+
};
|
|
85
|
+
less: {
|
|
86
|
+
alias: string;
|
|
87
|
+
method(threshold: any): BNSchema;
|
|
88
|
+
};
|
|
89
|
+
positive: {
|
|
90
|
+
method(): BNSchema;
|
|
91
|
+
};
|
|
92
|
+
negative: {
|
|
93
|
+
method(): BNSchema;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
};
|
package/lib/extension/bn.js
CHANGED
|
@@ -1,130 +1,137 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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),
|
|
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
|
+
exports.BNExtension = void 0;
|
|
7
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
8
|
+
function BNExtension(root) {
|
|
9
|
+
return {
|
|
10
|
+
type: 'BN',
|
|
11
|
+
base: root.any(),
|
|
12
|
+
messages: {
|
|
13
|
+
'bn.nan': '{{#label}} is not a big number',
|
|
14
|
+
'bn.max': '{{#label}} needs to be less than or equal to "{{#threshold}}"',
|
|
15
|
+
'bn.min': '{{#label}} needs to be greater than or equal to "{{#threshold}}"',
|
|
16
|
+
'bn.less': '{{#label}} needs to be less than "{{#threshold}}"',
|
|
17
|
+
'bn.greater': '{{#label}} needs to be greater than "{{#threshold}}"',
|
|
18
|
+
'bn.positive': '{{#label}} needs to be positive',
|
|
19
|
+
'bn.negative': '{{#label}} needs to be negative',
|
|
40
20
|
},
|
|
41
|
-
{
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
21
|
+
prepare(value, helpers) {
|
|
22
|
+
try {
|
|
23
|
+
return { value: new bn_js_1.default(value) };
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
return { errors: helpers.error('bn.nan') };
|
|
27
|
+
}
|
|
45
28
|
},
|
|
46
|
-
|
|
47
|
-
|
|
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),
|
|
29
|
+
coerce(value) {
|
|
30
|
+
return { value: value.toString(10) };
|
|
71
31
|
},
|
|
72
|
-
{
|
|
73
|
-
|
|
74
|
-
assert: (v) => typeof v === 'boolean',
|
|
75
|
-
message: 'must be a boolean',
|
|
32
|
+
validate(value) {
|
|
33
|
+
return { value };
|
|
76
34
|
},
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
35
|
+
rules: {
|
|
36
|
+
gt: {
|
|
37
|
+
args: [
|
|
38
|
+
{
|
|
39
|
+
name: 'threshold',
|
|
40
|
+
ref: true,
|
|
41
|
+
assert: (v) => bn_js_1.default.isBN(v),
|
|
42
|
+
message: 'must be a big number',
|
|
43
|
+
normalize: (v) => new bn_js_1.default(v),
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'equal',
|
|
47
|
+
assert: (v) => typeof v === 'boolean',
|
|
48
|
+
message: 'must be a boolean',
|
|
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 v = new bn_js_1.default(value);
|
|
55
|
+
if (args.equal && v.lt(args.threshold)) {
|
|
56
|
+
return helpers.error('bn.min', args);
|
|
57
|
+
}
|
|
58
|
+
if (!args.equal && v.lte(args.threshold)) {
|
|
59
|
+
return helpers.error('bn.greater', args);
|
|
60
|
+
}
|
|
61
|
+
// must return value when valid
|
|
62
|
+
return value;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
lt: {
|
|
66
|
+
args: [
|
|
67
|
+
{
|
|
68
|
+
name: 'threshold',
|
|
69
|
+
ref: true,
|
|
70
|
+
assert: (v) => bn_js_1.default.isBN(v),
|
|
71
|
+
message: 'must be a big number',
|
|
72
|
+
normalize: (v) => new bn_js_1.default(v),
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'equal',
|
|
76
|
+
assert: (v) => typeof v === 'boolean',
|
|
77
|
+
message: 'must be a boolean',
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
// The rule return structure is different from the root
|
|
81
|
+
// eslint-disable-next-line consistent-return
|
|
82
|
+
validate(value, helpers, args) {
|
|
83
|
+
const v = new bn_js_1.default(value);
|
|
84
|
+
if (args.equal && v.gt(args.threshold)) {
|
|
85
|
+
return helpers.error('bn.max', args);
|
|
86
|
+
}
|
|
87
|
+
if (!args.equal && v.gte(args.threshold)) {
|
|
88
|
+
return helpers.error('bn.less', args);
|
|
89
|
+
}
|
|
90
|
+
// must return value when valid
|
|
91
|
+
return value;
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
min: {
|
|
95
|
+
alias: 'gte',
|
|
96
|
+
method(threshold) {
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
return this.$_addRule({ name: 'gt', args: { threshold, equal: true } });
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
max: {
|
|
102
|
+
alias: 'lte',
|
|
103
|
+
method(threshold) {
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
return this.$_addRule({ name: 'lt', args: { threshold, equal: true } });
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
greater: {
|
|
109
|
+
alias: 'gt',
|
|
110
|
+
method(threshold) {
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
return this.$_addRule({ name: 'gt', args: { threshold, equal: false } });
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
less: {
|
|
116
|
+
alias: 'lt',
|
|
117
|
+
method(threshold) {
|
|
118
|
+
// @ts-ignore
|
|
119
|
+
return this.$_addRule({ name: 'lt', args: { threshold, equal: false } });
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
positive: {
|
|
123
|
+
method() {
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
return this.$_addRule({ name: 'gt', args: { threshold: 0, equal: false } });
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
negative: {
|
|
129
|
+
method() {
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
return this.$_addRule({ name: 'lt', args: { threshold: 0, equal: false } });
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
exports.BNExtension = BNExtension;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Root, StringSchema } from 'joi';
|
|
2
|
+
import { KeyType, HashType, RoleType } from '@ocap/mcrypto';
|
|
3
|
+
export interface DIDSchema extends StringSchema {
|
|
4
|
+
wallet(type: string): this;
|
|
5
|
+
pk(type: string): this;
|
|
6
|
+
hash(type: string): this;
|
|
7
|
+
role(type: string): this;
|
|
8
|
+
}
|
|
9
|
+
export declare function DIDExtension(root: Root): {
|
|
10
|
+
type: string;
|
|
11
|
+
base: StringSchema;
|
|
12
|
+
messages: {
|
|
13
|
+
'did.empty': string;
|
|
14
|
+
'did.invalid': string;
|
|
15
|
+
'did.wallet': string;
|
|
16
|
+
'did.pk': string;
|
|
17
|
+
'did.hash': string;
|
|
18
|
+
'did.role': string;
|
|
19
|
+
};
|
|
20
|
+
validate(value: any, helpers: any): {
|
|
21
|
+
errors: any;
|
|
22
|
+
value?: undefined;
|
|
23
|
+
} | {
|
|
24
|
+
value: string;
|
|
25
|
+
errors?: undefined;
|
|
26
|
+
};
|
|
27
|
+
rules: {
|
|
28
|
+
type: {
|
|
29
|
+
args: ({
|
|
30
|
+
name: string;
|
|
31
|
+
ref: boolean;
|
|
32
|
+
assert: (v: string) => boolean;
|
|
33
|
+
message: string;
|
|
34
|
+
} | {
|
|
35
|
+
name: string;
|
|
36
|
+
assert: (v: string) => boolean;
|
|
37
|
+
message: string;
|
|
38
|
+
ref?: undefined;
|
|
39
|
+
})[];
|
|
40
|
+
validate(value: string, helpers: any, args: any): any;
|
|
41
|
+
};
|
|
42
|
+
wallet: {
|
|
43
|
+
method(type: string): DIDSchema;
|
|
44
|
+
};
|
|
45
|
+
pk: {
|
|
46
|
+
method(type: KeyType): DIDSchema;
|
|
47
|
+
};
|
|
48
|
+
hash: {
|
|
49
|
+
method(type: HashType): DIDSchema;
|
|
50
|
+
};
|
|
51
|
+
role: {
|
|
52
|
+
method(type: RoleType): DIDSchema;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
};
|
package/lib/extension/did.js
CHANGED
|
@@ -1,100 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
exports.DIDExtension = void 0;
|
|
7
|
+
const isEqual_1 = __importDefault(require("lodash/isEqual"));
|
|
8
|
+
const mcrypto_1 = require("@ocap/mcrypto");
|
|
9
|
+
const did_1 = require("@arcblock/did");
|
|
5
10
|
const ruleTypes = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
wallet: ['arcblock', 'ethereum', 'default', 'eth'],
|
|
12
|
+
pk: Object.keys(mcrypto_1.types.KeyType),
|
|
13
|
+
hash: Object.keys(mcrypto_1.types.HashType),
|
|
14
|
+
role: Object.keys(mcrypto_1.types.RoleType),
|
|
10
15
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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(', ')}`,
|
|
16
|
+
function DIDExtension(root) {
|
|
17
|
+
return {
|
|
18
|
+
type: 'DID',
|
|
19
|
+
base: root.string().trim(),
|
|
20
|
+
messages: {
|
|
21
|
+
'did.empty': 'Expect {{#label}} to be non-empty string, got "{{#value}}"',
|
|
22
|
+
'did.invalid': 'Expect {{#label}} to be valid did, got "{{#value}}"',
|
|
23
|
+
'did.wallet': 'Expect wallet type of {{#label}} to be "{{#expected}}" wallet, got "{{#actual}}"',
|
|
24
|
+
'did.pk': 'Expect pk type of {{#label}} to be "{{#expected}}", got "{{#actual}}"',
|
|
25
|
+
'did.hash': 'Expect hash type of {{#label}} to be "{{#expected}}", got "{{#actual}}"',
|
|
26
|
+
'did.role': 'Expect role type of {{#label}} to be "{{#expected}}", got "{{#actual}}"',
|
|
27
|
+
},
|
|
28
|
+
validate(value, helpers) {
|
|
29
|
+
if (!value || typeof value !== 'string') {
|
|
30
|
+
return { errors: helpers.error('did.empty', { value }) };
|
|
31
|
+
}
|
|
32
|
+
if ((0, did_1.isValid)(value) === false) {
|
|
33
|
+
return { errors: helpers.error('did.invalid', { value }) };
|
|
34
|
+
}
|
|
35
|
+
return { value };
|
|
44
36
|
},
|
|
45
|
-
{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
rules: {
|
|
38
|
+
type: {
|
|
39
|
+
args: [
|
|
40
|
+
{
|
|
41
|
+
name: 'key',
|
|
42
|
+
ref: true,
|
|
43
|
+
assert: (v) => Object.keys(ruleTypes).includes(v),
|
|
44
|
+
message: `must be one of ${Object.keys(ruleTypes).join(', ')}`,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'expected',
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
assert: (v) => Object.keys(ruleTypes).some((x) => ruleTypes[x].includes(v)),
|
|
50
|
+
message: 'must be valid type',
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
// The rule return structure is different from the root
|
|
54
|
+
// eslint-disable-next-line consistent-return
|
|
55
|
+
validate(value, helpers, args) {
|
|
56
|
+
const type = (0, did_1.toTypeInfo)(value);
|
|
57
|
+
const typeStr = (0, did_1.toTypeInfoStr)(value);
|
|
58
|
+
if (args.key === 'wallet') {
|
|
59
|
+
if (['ethereum', 'eth'].includes(args.expected) && (0, isEqual_1.default)(type, did_1.DID_TYPE_ETHEREUM) === false) {
|
|
60
|
+
return helpers.error('did.wallet', Object.assign(Object.assign({}, args), { actual: JSON.stringify(typeStr) }));
|
|
61
|
+
}
|
|
62
|
+
if (['arcblock', 'default'].includes(args.expected) && (0, isEqual_1.default)(type, did_1.DID_TYPE_ARCBLOCK) === false) {
|
|
63
|
+
return helpers.error('did.wallet', Object.assign(Object.assign({}, args), { actual: JSON.stringify(typeStr) }));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (args.key === 'pk' && typeStr.pk !== args.expected) {
|
|
67
|
+
return helpers.error('did.pk', Object.assign(Object.assign({}, args), { actual: typeStr.pk }));
|
|
68
|
+
}
|
|
69
|
+
if (args.key === 'hash' && typeStr.hash !== args.expected) {
|
|
70
|
+
return helpers.error('did.hash', Object.assign(Object.assign({}, args), { actual: typeStr.hash }));
|
|
71
|
+
}
|
|
72
|
+
if (args.key === 'role' && typeStr.role !== args.expected) {
|
|
73
|
+
return helpers.error('did.role', Object.assign(Object.assign({}, args), { actual: typeStr.role }));
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
wallet: {
|
|
79
|
+
method(type) {
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
return this.$_addRule({ name: 'type', args: { key: 'wallet', expected: type } });
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
pk: {
|
|
85
|
+
method(type) {
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
return this.$_addRule({ name: 'type', args: { key: 'pk', expected: type } });
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
hash: {
|
|
91
|
+
method(type) {
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
return this.$_addRule({ name: 'type', args: { key: 'hash', expected: type } });
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
role: {
|
|
97
|
+
method(type) {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
return this.$_addRule({ name: 'type', args: { key: 'role', expected: type } });
|
|
100
|
+
},
|
|
101
|
+
},
|
|
49
102
|
},
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
});
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
exports.DIDExtension = DIDExtension;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import BaseJoi, { Root } from 'joi';
|
|
2
|
+
import { BNSchema } from './extension/bn';
|
|
3
|
+
import { DIDSchema } from './extension/did';
|
|
4
|
+
export interface ExtendedRoot extends Root {
|
|
5
|
+
DID(): DIDSchema;
|
|
6
|
+
BN(): BNSchema;
|
|
7
|
+
}
|
|
8
|
+
export declare const Joi: ExtendedRoot;
|
|
9
|
+
export declare const schemas: {
|
|
10
|
+
context: BaseJoi.ObjectSchema<any>;
|
|
11
|
+
tokenInput: BaseJoi.ObjectSchema<any>;
|
|
12
|
+
multiSig: BaseJoi.ArraySchema;
|
|
13
|
+
foreignToken: BaseJoi.ObjectSchema<any>;
|
|
14
|
+
};
|
|
15
|
+
export declare const patterns: {
|
|
16
|
+
txHash: RegExp;
|
|
17
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -1,51 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
+
exports.patterns = exports.schemas = exports.Joi = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("joi"));
|
|
8
|
+
const bn_1 = require("./extension/bn");
|
|
9
|
+
const did_1 = require("./extension/did");
|
|
10
|
+
exports.Joi = joi_1.default.extend(did_1.DIDExtension).extend(bn_1.BNExtension);
|
|
8
11
|
const txHash = /^(0x)?([A-Fa-f0-9]{64})$/;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
renaissanceTx: Joi.string().regex(txHash).required().allow(''),
|
|
12
|
+
const contextSchema = exports.Joi.object({
|
|
13
|
+
genesisTime: exports.Joi.date().iso().required().raw(),
|
|
14
|
+
genesisTx: exports.Joi.string().regex(txHash).required().allow(''),
|
|
15
|
+
renaissanceTime: exports.Joi.date().iso().required().raw(),
|
|
16
|
+
renaissanceTx: exports.Joi.string().regex(txHash).required().allow(''),
|
|
15
17
|
});
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
value: Joi.BN().positive().required(),
|
|
18
|
+
const tokenInputSchema = exports.Joi.object({
|
|
19
|
+
address: exports.Joi.DID().role('ROLE_TOKEN').required(),
|
|
20
|
+
value: exports.Joi.BN().positive().required(),
|
|
20
21
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
data: Joi.any().optional(),
|
|
22
|
+
const multiSigSchema = exports.Joi.array().items({
|
|
23
|
+
signer: exports.Joi.DID().required(),
|
|
24
|
+
pk: exports.Joi.any().required(),
|
|
25
|
+
signature: exports.Joi.any().required(),
|
|
26
|
+
delegator: exports.Joi.DID().valid('').optional(),
|
|
27
|
+
data: exports.Joi.any().optional(),
|
|
28
28
|
});
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
chainId: Joi.number().positive().required(),
|
|
29
|
+
const foreignTokenSchema = exports.Joi.object({
|
|
30
|
+
type: exports.Joi.string().min(1).max(32).required(),
|
|
31
|
+
contractAddress: exports.Joi.DID().wallet('ethereum').required(),
|
|
32
|
+
chainType: exports.Joi.string().min(1).max(32).required(),
|
|
33
|
+
chainName: exports.Joi.string().min(1).max(32).required(),
|
|
34
|
+
chainId: exports.Joi.number().positive().required(),
|
|
36
35
|
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
foreignToken: foreignTokenSchema,
|
|
36
|
+
exports.schemas = {
|
|
37
|
+
context: contextSchema,
|
|
38
|
+
tokenInput: tokenInputSchema,
|
|
39
|
+
multiSig: multiSigSchema,
|
|
40
|
+
foreignToken: foreignTokenSchema,
|
|
43
41
|
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
txHash,
|
|
42
|
+
exports.patterns = {
|
|
43
|
+
txHash,
|
|
47
44
|
};
|
|
48
|
-
|
|
49
|
-
module.exports = Joi;
|
|
50
|
-
module.exports.schemas = schemas;
|
|
51
|
-
module.exports.patterns = patterns;
|
package/package.json
CHANGED
|
@@ -3,30 +3,43 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.
|
|
6
|
+
"version": "1.16.17",
|
|
7
7
|
"description": "",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
|
+
"typings": "lib/index.d.ts",
|
|
9
10
|
"files": [
|
|
10
11
|
"lib"
|
|
11
12
|
],
|
|
12
13
|
"scripts": {
|
|
13
|
-
"lint": "eslint tests
|
|
14
|
-
"lint:fix": "eslint --fix tests
|
|
14
|
+
"lint": "eslint tests src",
|
|
15
|
+
"lint:fix": "eslint --fix tests src",
|
|
15
16
|
"test": "jest --forceExit --detectOpenHandles",
|
|
16
|
-
"coverage": "npm run test -- --coverage"
|
|
17
|
+
"coverage": "npm run test -- --coverage",
|
|
18
|
+
"clean": "rm -fr lib",
|
|
19
|
+
"prebuild": "npm run clean",
|
|
20
|
+
"build:watch": "npm run build -- -w",
|
|
21
|
+
"build": "tsc"
|
|
17
22
|
},
|
|
18
23
|
"dependencies": {
|
|
19
|
-
"@arcblock/did": "1.16.
|
|
20
|
-
"@ocap/mcrypto": "1.16.
|
|
21
|
-
"@ocap/util": "1.16.
|
|
24
|
+
"@arcblock/did": "1.16.17",
|
|
25
|
+
"@ocap/mcrypto": "1.16.17",
|
|
26
|
+
"@ocap/util": "1.16.17",
|
|
27
|
+
"bn.js": "5.2.1",
|
|
22
28
|
"joi": "^17.6.0",
|
|
23
29
|
"lodash": "^4.17.21"
|
|
24
30
|
},
|
|
31
|
+
"resolutions": {
|
|
32
|
+
"bn.js": "5.2.1"
|
|
33
|
+
},
|
|
25
34
|
"devDependencies": {
|
|
26
|
-
"
|
|
35
|
+
"@arcblock/eslint-config-ts": "0.2.2",
|
|
36
|
+
"eslint": "^8.17.0",
|
|
37
|
+
"jest": "^28.1.1",
|
|
38
|
+
"ts-jest": "^28.0.5",
|
|
39
|
+
"typescript": "^4.7.3"
|
|
27
40
|
},
|
|
28
41
|
"keywords": [],
|
|
29
42
|
"author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
|
|
30
43
|
"license": "MIT",
|
|
31
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "489ce5e03bce27ddcd535390228b11ab56e7a2e3"
|
|
32
45
|
}
|