@ocap/asset 1.17.11 → 1.17.14

Sign up to get free protection for your applications and to get access to all the features.
package/lib/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export declare type TPreMintResult = TMintResult & {
28
28
  variables: TInputMap;
29
29
  issuer: TIssuerInput;
30
30
  };
31
+ export declare const isValidNotation: (notation: string) => boolean;
31
32
  export declare const isValidHook: (hook: THook, quota?: $TSFixMe, throwOnError?: boolean) => boolean;
32
33
  export declare const isValidFactory: (props: $TSFixMe) => boolean;
33
34
  /**
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.formatFactoryState = exports.preMintFromFactory = exports.mintFromFactory = exports.findPrerenderKeys = exports.isValidFactory = exports.isValidHook = void 0;
6
+ exports.formatFactoryState = exports.preMintFromFactory = exports.mintFromFactory = exports.findPrerenderKeys = exports.isValidFactory = exports.isValidHook = exports.isValidNotation = void 0;
7
7
  /* eslint-disable @typescript-eslint/ban-ts-comment */
8
8
  const get_1 = __importDefault(require("lodash/get"));
9
9
  const set_1 = __importDefault(require("lodash/set"));
@@ -16,6 +16,7 @@ const is_absolute_url_1 = __importDefault(require("is-absolute-url"));
16
16
  const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
17
17
  const did_1 = require("@arcblock/did");
18
18
  const vc_1 = require("@arcblock/vc");
19
+ const validator_1 = require("@arcblock/validator");
19
20
  const did_util_1 = require("@arcblock/did-util");
20
21
  const mcrypto_1 = require("@ocap/mcrypto");
21
22
  const util_1 = require("@ocap/util");
@@ -24,6 +25,8 @@ const debug_1 = __importDefault(require("debug"));
24
25
  const debug = (0, debug_1.default)('@ocap/asset');
25
26
  const SUPPORTED_HOOK_NAMES = ['mint', 'postMint'];
26
27
  const SUPPORTED_HOOK_TYPES = ['contract', 'url'];
28
+ const isValidNotation = (notation) => ['ctx', 'data', 'input'].includes(notation.split('.').shift());
29
+ exports.isValidNotation = isValidNotation;
27
30
  const isValidHook = (hook, quota, throwOnError = false) => {
28
31
  if (SUPPORTED_HOOK_TYPES.includes(hook.type) === false) {
29
32
  return false;
@@ -53,68 +56,53 @@ const isValidHook = (hook, quota, throwOnError = false) => {
53
56
  return false;
54
57
  };
55
58
  exports.isValidHook = isValidHook;
56
- // FIXME: we should use Joi to validate the schema
57
59
  // @link https://github.com/improbable-eng/ts-protoc-gen
58
60
  const isValidFactory = (props) => {
59
61
  if (!props) {
60
62
  throw new Error('Factory props should not be empty');
61
63
  }
62
- // required props
63
- const missingProp = ['name', 'description', 'input', 'output', 'settlement'].find((x) => !props[x]);
64
- if (missingProp) {
65
- throw new Error(`Factory ${missingProp} prop should not be empty`);
66
- }
67
- if (['instant', 'periodic'].includes(props.settlement) === false) {
68
- throw new Error('Factory settlement prop should only be instant or periodic');
64
+ const { value, error } = validator_1.schemas.factorySchema.validate(props);
65
+ if (error) {
66
+ throw new Error(`Invalid factory: ${error.details.map((x) => x.message).join(', ')}`);
69
67
  }
70
68
  // input.tokens and input.assets should not be empty
71
- if (['value', 'tokens', 'assets'].every((x) => (0, isEmpty_1.default)(props.input[x]))) {
69
+ if (['tokens', 'assets'].every((x) => (0, isEmpty_1.default)(value.input[x])) && value.input.value <= 0) {
72
70
  throw new Error('Factory input should contain at least one token or asset');
73
71
  }
74
- if ((0, uniqBy_1.default)(props.input.tokens, 'address').length !== props.input.tokens.length) {
72
+ if ((0, uniqBy_1.default)(value.input.tokens, 'address').length !== value.input.tokens.length) {
75
73
  throw new Error('Factory token input should not contains duplicate address');
76
74
  }
77
- if ((0, uniq_1.default)(props.input.assets).length !== props.input.assets.length) {
75
+ if ((0, uniq_1.default)(value.input.assets).length !== value.input.assets.length) {
78
76
  throw new Error('Factory asset input should not contains duplicate address');
79
77
  }
80
- for (const token of props.input.tokens) {
81
- try {
82
- // eslint-disable-next-line no-new
83
- new util_1.BN(token.value);
84
- }
85
- catch (err) {
86
- throw new Error(`Factory token ${token.address} is invalid: token value is not valid big number`);
87
- }
88
- }
89
- // validate output
90
- if (!props.output || typeof props.output !== 'object') {
91
- throw new Error('Factory output should be an object');
92
- }
93
78
  try {
94
79
  // should be a valid mustache template when serialized as json
95
- const template = JSON.stringify(props.output);
96
- mustache_1.default.parse(template);
80
+ const template = JSON.stringify(value.output, null, 2);
81
+ const result = mustache_1.default.parse(template);
82
+ if (result.filter(([type]) => type === 'name').some(([, notation]) => (0, exports.isValidNotation)(notation) === false)) {
83
+ throw new Error('Invalid tags found in the output template');
84
+ }
97
85
  }
98
86
  catch (err) {
99
87
  throw new Error('Factory output should be a valid mustache template when serialized as json');
100
88
  }
101
89
  // validate hooks
102
- const quota = (0, contract_1.getQuota)(props.input);
103
- if (Array.isArray(props.hooks)) {
104
- const invalidHook = props.hooks.find((x) => (0, exports.isValidHook)(x, quota, true) === false);
90
+ const quota = (0, contract_1.getQuota)(value.input);
91
+ if (Array.isArray(value.hooks)) {
92
+ const invalidHook = value.hooks.find((x) => (0, exports.isValidHook)(x, quota, true) === false);
105
93
  if (invalidHook) {
106
94
  throw new Error(`Factory hook ${invalidHook.name} is invalid`);
107
95
  }
108
96
  }
109
97
  // ensure input and hook are zero-sum for factories that consumes token
110
- if (props.settlement === 'instant') {
98
+ if (value.settlement === 'instant') {
111
99
  if (quota.value <= 0 && Object.keys(quota.tokens).every((x) => quota[x] <= 0)) {
112
100
  return true;
113
101
  }
114
- if ((0, isEmpty_1.default)(props.hooks)) {
102
+ if ((0, isEmpty_1.default)(value.hooks)) {
115
103
  throw new Error('Factory hooks should not be empty for instant settlement');
116
104
  }
117
- const mintHook = props.hooks.find((x) => x.name === 'mint');
105
+ const mintHook = value.hooks.find((x) => x.name === 'mint');
118
106
  if (!mintHook) {
119
107
  throw new Error('Factory hook mint should not be empty for instant settlement that consumes token');
120
108
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/asset",
3
3
  "description": "Utility to work with asset and factory on ArcBlock blockchain",
4
- "version": "1.17.11",
4
+ "version": "1.17.14",
5
5
  "author": {
6
6
  "name": "wangshijun",
7
7
  "email": "shijun@arcblock.io",
@@ -18,14 +18,15 @@
18
18
  "wangshijun <shijun@arcblock.io> (https://github.com/wangshijun)"
19
19
  ],
20
20
  "dependencies": {
21
- "@arcblock/did": "1.17.11",
22
- "@arcblock/did-util": "1.17.11",
23
- "@arcblock/vc": "1.17.11",
24
- "@ocap/contract": "1.17.11",
25
- "@ocap/mcrypto": "1.17.11",
26
- "@ocap/types": "1.17.11",
27
- "@ocap/util": "1.17.11",
28
- "@ocap/wallet": "1.17.11",
21
+ "@arcblock/did": "1.17.14",
22
+ "@arcblock/did-util": "1.17.14",
23
+ "@arcblock/validator": "1.17.14",
24
+ "@arcblock/vc": "1.17.14",
25
+ "@ocap/contract": "1.17.14",
26
+ "@ocap/mcrypto": "1.17.14",
27
+ "@ocap/types": "1.17.14",
28
+ "@ocap/util": "1.17.14",
29
+ "@ocap/wallet": "1.17.14",
29
30
  "debug": "^4.3.4",
30
31
  "flat": "^5.0.2",
31
32
  "is-absolute-url": "^3.0.3",
@@ -35,7 +36,7 @@
35
36
  "type-fest": "^2.18.0"
36
37
  },
37
38
  "devDependencies": {
38
- "@arcblock/eslint-config-ts": "0.2.2",
39
+ "@arcblock/eslint-config-ts": "0.2.3",
39
40
  "@types/flat": "^5.0.2",
40
41
  "@types/jest": "^28.1.6",
41
42
  "@types/mustache": "^4.2.1",
@@ -73,5 +74,5 @@
73
74
  "build": "tsc",
74
75
  "build:watch": "npm run build -- -w"
75
76
  },
76
- "gitHead": "32ee93b1f1026709bb1b00a58b4b3dc5d551f94a"
77
+ "gitHead": "3e0c14fb2ca3f56639599aec7929c3de54ee61c7"
77
78
  }