@ocap/state 1.13.64 → 1.13.65
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/contexts/state.js +4 -3
- package/lib/joi/index.js +2 -2
- package/lib/states/account.js +51 -19
- package/lib/states/stake.js +2 -2
- package/package.json +7 -7
package/lib/contexts/state.js
CHANGED
|
@@ -2,15 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
const create = ({ txHash, txTime }) => ({
|
|
4
4
|
genesisTime: txTime || '',
|
|
5
|
-
genesisTx: txHash ||
|
|
5
|
+
genesisTx: txHash || '',
|
|
6
6
|
renaissanceTime: txTime || '',
|
|
7
|
-
renaissanceTx: txHash ||
|
|
7
|
+
renaissanceTx: txHash || '',
|
|
8
8
|
});
|
|
9
9
|
|
|
10
10
|
const update = (context, { txHash, txTime }) => ({
|
|
11
11
|
...context,
|
|
12
|
+
genesisTx: context.genesisTx || '',
|
|
12
13
|
renaissanceTime: txTime || '',
|
|
13
|
-
renaissanceTx: txHash ||
|
|
14
|
+
renaissanceTx: txHash || '',
|
|
14
15
|
});
|
|
15
16
|
|
|
16
17
|
module.exports = { create, update };
|
package/lib/joi/index.js
CHANGED
|
@@ -9,9 +9,9 @@ const hashRegexp = /^(0x)?([A-Fa-f0-9]{64})$/;
|
|
|
9
9
|
|
|
10
10
|
const contextSchema = Joi.object({
|
|
11
11
|
genesisTime: Joi.date().iso().required().raw(),
|
|
12
|
-
genesisTx: Joi.string().regex(hashRegexp).required(),
|
|
12
|
+
genesisTx: Joi.string().regex(hashRegexp).required().allow(''),
|
|
13
13
|
renaissanceTime: Joi.date().iso().required().raw(),
|
|
14
|
-
renaissanceTx: Joi.string().regex(hashRegexp).required(),
|
|
14
|
+
renaissanceTx: Joi.string().regex(hashRegexp).required().allow(''),
|
|
15
15
|
});
|
|
16
16
|
|
|
17
17
|
const tokenInputSchema = Joi.object({
|
package/lib/states/account.js
CHANGED
|
@@ -1,35 +1,40 @@
|
|
|
1
1
|
const pick = require('lodash/pick');
|
|
2
2
|
const uniq = require('lodash/uniq');
|
|
3
3
|
const flatten = require('lodash/flatten');
|
|
4
|
-
const {
|
|
4
|
+
const { isFromPublicKey } = require('@arcblock/did');
|
|
5
|
+
const { toBase58, BN } = require('@ocap/util');
|
|
5
6
|
|
|
7
|
+
const Joi = require('../joi');
|
|
6
8
|
const { create: createStateContext, update: updateStateContext } = require('../contexts/state');
|
|
7
9
|
|
|
10
|
+
const schema = Joi.object({
|
|
11
|
+
address: Joi.DID().required(),
|
|
12
|
+
pk: Joi.string().allow(''),
|
|
13
|
+
issuer: Joi.DID().allow(''),
|
|
14
|
+
moniker: Joi.string().trim().min(2).max(40).allow(''),
|
|
15
|
+
nonce: Joi.number().min(0).default(0),
|
|
16
|
+
tokens: Joi.object().pattern(Joi.DID().role('ROLE_TOKEN'), Joi.BN().min(0)).default({}),
|
|
17
|
+
migratedTo: Joi.array().items(Joi.DID()).default([]),
|
|
18
|
+
migratedFrom: Joi.array().items(Joi.DID()).default([]),
|
|
19
|
+
context: Joi.contextSchema,
|
|
20
|
+
data: Joi.any().optional(),
|
|
21
|
+
}).options({ stripUnknown: true, noDefaults: false });
|
|
22
|
+
|
|
8
23
|
const create = (attrs, context) => {
|
|
9
24
|
const account = {
|
|
10
|
-
balance: '0',
|
|
11
|
-
gasBalance: '0',
|
|
12
25
|
nonce: 0,
|
|
13
|
-
numTxs: 0,
|
|
14
|
-
numAssets: 0,
|
|
15
26
|
migratedTo: [],
|
|
16
27
|
migratedFrom: [],
|
|
17
|
-
stake: null,
|
|
18
28
|
tokens: {},
|
|
19
29
|
context: createStateContext(context),
|
|
20
30
|
...pick(attrs, ['address', 'pk', 'issuer', 'moniker', 'data', 'nonce', 'migratedFrom', 'tokens']),
|
|
21
31
|
};
|
|
22
32
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
account.pk = toBase64(toUint8Array(account.pk));
|
|
33
|
+
if (!account.moniker) {
|
|
34
|
+
account.moniker = [account.address.slice(0, 6), account.address.slice(-5)].join('-');
|
|
26
35
|
}
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
account.data = null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return account;
|
|
37
|
+
return validate(account);
|
|
33
38
|
};
|
|
34
39
|
|
|
35
40
|
const update = (state, attrs, context) => {
|
|
@@ -37,20 +42,47 @@ const update = (state, attrs, context) => {
|
|
|
37
42
|
throw new Error('nonce must be greater in newer transactions');
|
|
38
43
|
}
|
|
39
44
|
|
|
45
|
+
// ensure we are updating the correct pk
|
|
46
|
+
if (attrs.pk && isFromPublicKey(state.address, attrs.pk) === false) {
|
|
47
|
+
delete attrs.pk;
|
|
48
|
+
}
|
|
49
|
+
|
|
40
50
|
const account = {
|
|
41
51
|
...state,
|
|
42
|
-
...pick(attrs, ['moniker', 'data', 'migratedTo', 'nonce', 'tokens']),
|
|
52
|
+
...pick(attrs, ['moniker', 'data', 'migratedTo', 'nonce', 'tokens', 'pk']),
|
|
43
53
|
migratedTo: uniq(flatten(attrs.migratedTo ? [attrs.migratedTo].concat(state.migratedTo) : state.migratedTo)),
|
|
44
54
|
context: updateStateContext(state.context, context),
|
|
45
55
|
};
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
return validate(account);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const updateOrCreate = (state, attrs, context) => {
|
|
61
|
+
if (state) {
|
|
62
|
+
return update(state, attrs, context);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return create(attrs, context);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const validate = (state) => {
|
|
69
|
+
// ensure we have correct pk
|
|
70
|
+
if (state.pk && typeof state.pk !== 'string') {
|
|
71
|
+
state.pk = toBase58(state.pk);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { value, error } = schema.validate(state);
|
|
75
|
+
if (error) {
|
|
76
|
+
throw new Error(`Invalid account: ${error.details.map((x) => x.message).join(', ')}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!value.data) {
|
|
80
|
+
value.data = null;
|
|
49
81
|
}
|
|
50
82
|
|
|
51
|
-
return
|
|
83
|
+
return value;
|
|
52
84
|
};
|
|
53
85
|
|
|
54
86
|
const isMigrated = (state) => (state.migratedTo || []).length > 0;
|
|
55
87
|
|
|
56
|
-
module.exports = { create, update, isMigrated };
|
|
88
|
+
module.exports = { create, update, updateOrCreate, validate, isMigrated };
|
package/lib/states/stake.js
CHANGED
|
@@ -7,12 +7,12 @@ const schema = Joi.object({
|
|
|
7
7
|
address: Joi.DID().role('ROLE_STAKE').trim().required(),
|
|
8
8
|
sender: Joi.DID().trim().required(),
|
|
9
9
|
receiver: Joi.DID().trim().required(),
|
|
10
|
-
tokens: Joi.object().default({}),
|
|
10
|
+
tokens: Joi.object({}).pattern(Joi.DID().role('ROLE_TOKEN'), Joi.BN().min(0)).default({}),
|
|
11
11
|
assets: Joi.array().items(Joi.DID().role('ROLE_ASSET')).default([]),
|
|
12
12
|
revocable: Joi.boolean().default(true),
|
|
13
13
|
message: Joi.string().trim().min(1).max(256).required(),
|
|
14
14
|
revokeWaitingPeriod: Joi.number().integer().min(0).default(0),
|
|
15
|
-
revokedTokens: Joi.object().default({}),
|
|
15
|
+
revokedTokens: Joi.object({}).pattern(Joi.DID().role('ROLE_TOKEN'), Joi.BN().min(0)).default({}),
|
|
16
16
|
revokedAssets: Joi.array().items(Joi.DID().role('ROLE_ASSET')).default([]),
|
|
17
17
|
context: Joi.contextSchema,
|
|
18
18
|
data: Joi.any().optional(),
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.13.
|
|
6
|
+
"version": "1.13.65",
|
|
7
7
|
"description": "",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -16,21 +16,21 @@
|
|
|
16
16
|
"coverage": "npm run test -- --coverage"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@arcblock/did": "1.13.
|
|
20
|
-
"@ocap/contract": "1.13.
|
|
21
|
-
"@ocap/message": "1.13.
|
|
22
|
-
"@ocap/util": "1.13.
|
|
19
|
+
"@arcblock/did": "1.13.65",
|
|
20
|
+
"@ocap/contract": "1.13.65",
|
|
21
|
+
"@ocap/message": "1.13.65",
|
|
22
|
+
"@ocap/util": "1.13.65",
|
|
23
23
|
"bloom-filters": "^1.3.1",
|
|
24
24
|
"debug": "^4.3.2",
|
|
25
25
|
"joi": "^17.4.2",
|
|
26
26
|
"lodash": "^4.17.21"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@ocap/mcrypto": "1.13.
|
|
29
|
+
"@ocap/mcrypto": "1.13.65",
|
|
30
30
|
"jest": "^27.3.1"
|
|
31
31
|
},
|
|
32
32
|
"keywords": [],
|
|
33
33
|
"author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
|
|
34
34
|
"license": "MIT",
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "4011996e1800845142aa5c889b58726129a99ec3"
|
|
36
36
|
}
|