@ocap/tx-protocols 1.14.8 → 1.14.9
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/protocols/asset/update.js +31 -6
- package/package.json +14 -13
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const deepDiff = require('deep-diff');
|
|
1
2
|
const Error = require('@ocap/util/lib/error');
|
|
2
3
|
const Joi = require('@ocap/validator');
|
|
3
4
|
const { Runner, pipes } = require('@ocap/tx-pipeline');
|
|
@@ -18,19 +19,23 @@ const schema = Joi.object({
|
|
|
18
19
|
moniker: Joi.string().min(2).max(255).required(),
|
|
19
20
|
data: Joi.any().optional(),
|
|
20
21
|
}).options({ stripUnknown: true, noDefaults: false });
|
|
21
|
-
runner.use((
|
|
22
|
+
runner.use((context, next) => {
|
|
23
|
+
const { itx } = context;
|
|
22
24
|
const { error } = schema.validate(itx);
|
|
23
25
|
if (error) {
|
|
24
26
|
return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
|
|
25
27
|
}
|
|
28
|
+
|
|
29
|
+
context.newData = decodeAnySafe(itx.data);
|
|
26
30
|
return next();
|
|
27
31
|
});
|
|
28
32
|
|
|
29
|
-
// Ensure asset exist
|
|
30
|
-
runner.use(pipes.ExtractState({ from: 'itx.address', to: 'assetState', status: 'INVALID_ASSET' }));
|
|
33
|
+
// Ensure asset exist
|
|
34
|
+
runner.use(pipes.ExtractState({ from: 'itx.address', to: 'assetState', status: 'INVALID_ASSET', table: 'asset' }));
|
|
35
|
+
runner.use(pipes.ExtractState({ from: 'assetState.issuer', to: 'issuerState', status: 'OK', table: 'account' }));
|
|
31
36
|
|
|
32
37
|
// Ensure sender exist
|
|
33
|
-
runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE' }));
|
|
38
|
+
runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })); // prettier-ignore
|
|
34
39
|
runner.use(pipes.VerifyAccountMigration({ senderKey: 'senderState' }));
|
|
35
40
|
|
|
36
41
|
// Ensure asset owned by sender and can be modified
|
|
@@ -46,10 +51,30 @@ runner.use(
|
|
|
46
51
|
])
|
|
47
52
|
);
|
|
48
53
|
|
|
54
|
+
// Ensure we are in append-only mode update proposed by issuer
|
|
55
|
+
runner.use(async (context, next) => {
|
|
56
|
+
const { itx, newData, senderState, assetState, issuerState } = context;
|
|
57
|
+
if (issuerState && senderState.address === issuerState.address) {
|
|
58
|
+
if (itx.moniker !== assetState.moniker) {
|
|
59
|
+
return next(new Error('FORBIDDEN', 'Asset moniker can only be updated by owner'));
|
|
60
|
+
}
|
|
61
|
+
if (newData.type !== assetState.data.type) {
|
|
62
|
+
return next(new Error('FORBIDDEN', 'Asset data type can only be updated by owner'));
|
|
63
|
+
}
|
|
64
|
+
const dataDiff = deepDiff(assetState.data.value, newData.value);
|
|
65
|
+
const appendOnly = dataDiff.every((x) => x.kind === 'N' || (x.kind === 'A' && x.item.kind === 'N'));
|
|
66
|
+
if (appendOnly === false) {
|
|
67
|
+
return next(new Error('APPEND_ONLY', 'Asset data value can only be updated in append only mode'));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return next();
|
|
72
|
+
});
|
|
73
|
+
|
|
49
74
|
// Update asset state
|
|
50
75
|
runner.use(
|
|
51
76
|
async (context, next) => {
|
|
52
|
-
const { tx, itx, statedb, senderState, assetState } = context;
|
|
77
|
+
const { tx, itx, newData, statedb, senderState, assetState } = context;
|
|
53
78
|
|
|
54
79
|
const [newSenderState, newAssetState] = await Promise.all([
|
|
55
80
|
// update owner state
|
|
@@ -62,7 +87,7 @@ runner.use(
|
|
|
62
87
|
// update asset state
|
|
63
88
|
statedb.asset.update(
|
|
64
89
|
itx.address,
|
|
65
|
-
asset.update(assetState, { moniker: itx.moniker, data:
|
|
90
|
+
asset.update(assetState, { moniker: itx.moniker, data: newData }, context),
|
|
66
91
|
context
|
|
67
92
|
),
|
|
68
93
|
]);
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.14.
|
|
6
|
+
"version": "1.14.9",
|
|
7
7
|
"description": "Predefined tx pipeline sets to execute certain type of transactions",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -21,18 +21,19 @@
|
|
|
21
21
|
"author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@arcblock/did": "1.14.
|
|
25
|
-
"@arcblock/did-util": "1.14.
|
|
26
|
-
"@ocap/asset": "1.14.
|
|
27
|
-
"@ocap/mcrypto": "1.14.
|
|
28
|
-
"@ocap/merkle-tree": "1.14.
|
|
29
|
-
"@ocap/message": "1.14.
|
|
30
|
-
"@ocap/state": "1.14.
|
|
31
|
-
"@ocap/tx-pipeline": "1.14.
|
|
32
|
-
"@ocap/util": "1.14.
|
|
33
|
-
"@ocap/validator": "1.14.
|
|
34
|
-
"@ocap/wallet": "1.14.
|
|
24
|
+
"@arcblock/did": "1.14.9",
|
|
25
|
+
"@arcblock/did-util": "1.14.9",
|
|
26
|
+
"@ocap/asset": "1.14.9",
|
|
27
|
+
"@ocap/mcrypto": "1.14.9",
|
|
28
|
+
"@ocap/merkle-tree": "1.14.9",
|
|
29
|
+
"@ocap/message": "1.14.9",
|
|
30
|
+
"@ocap/state": "1.14.9",
|
|
31
|
+
"@ocap/tx-pipeline": "1.14.9",
|
|
32
|
+
"@ocap/util": "1.14.9",
|
|
33
|
+
"@ocap/validator": "1.14.9",
|
|
34
|
+
"@ocap/wallet": "1.14.9",
|
|
35
35
|
"debug": "^4.3.2",
|
|
36
|
+
"deep-diff": "^1.0.2",
|
|
36
37
|
"empty-value": "^1.0.1",
|
|
37
38
|
"lodash": "^4.17.21",
|
|
38
39
|
"url-join": "^4.0.1"
|
|
@@ -45,5 +46,5 @@
|
|
|
45
46
|
"jest": "^27.3.1",
|
|
46
47
|
"start-server-and-test": "^1.14.0"
|
|
47
48
|
},
|
|
48
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "19fd75486457b972da40d7d1d043f389738aa1da"
|
|
49
50
|
}
|