final fixed for chaincode-javascript

Signed-off-by: fraVlaca <ocsenarf@outlook.com>
This commit is contained in:
fraVlaca 2021-09-03 11:30:39 +01:00
parent b68d42cc2f
commit 5a93aa752e
2 changed files with 11 additions and 11 deletions

View file

@ -7,9 +7,9 @@
'use strict'; 'use strict';
//Deterministic JSON.stringify() // Deterministic JSON.stringify()
const stringify = require('json-stringify-deterministic'); const { stringify } = require('json-stringify-deterministic');
const sortKeysRecursive = require('sort-keys-recursive'); const { sortKeysRecursive } = require('sort-keys-recursive');
const { Contract } = require('fabric-contract-api'); const { Contract } = require('fabric-contract-api');
class AssetTransfer extends Contract { class AssetTransfer extends Contract {
@ -61,10 +61,10 @@ class AssetTransfer extends Contract {
]; ];
for (const asset of assets) { for (const asset of assets) {
//example of how to write to world state deterministically // example of how to write to world state deterministically
//use convetion of alphabetic order // use convetion of alphabetic order
//we insert data in alphabetic order using 'json-stringify-deterministic' and 'sort-keys-recursive' // we insert data in alphabetic order using 'json-stringify-deterministic' and 'sort-keys-recursive'
//when retrieving data, in any lang, the order of data will be the same and consequently also the corresonding hash // when retrieving data, in any lang, the order of data will be the same and consequently also the corresonding hash
await ctx.stub.putState(asset.ID, Buffer.from(stringify(sortKeysRecursive(asset)))); await ctx.stub.putState(asset.ID, Buffer.from(stringify(sortKeysRecursive(asset))));
console.info(`Asset ${asset.ID} initialized`); console.info(`Asset ${asset.ID} initialized`);
} }
@ -108,7 +108,7 @@ class AssetTransfer extends Contract {
Owner: owner, Owner: owner,
AppraisedValue: appraisedValue, AppraisedValue: appraisedValue,
}; };
//we insert data in alphabetic order using 'json-stringify-deterministic' and 'sort-keys-recursive' // we insert data in alphabetic order using 'json-stringify-deterministic' and 'sort-keys-recursive'
return ctx.stub.putState(id, Buffer.from(stringify(sortKeysRecursive(updatedAsset)))); return ctx.stub.putState(id, Buffer.from(stringify(sortKeysRecursive(updatedAsset))));
} }
@ -132,7 +132,7 @@ class AssetTransfer extends Contract {
const assetString = await this.ReadAsset(ctx, id); const assetString = await this.ReadAsset(ctx, id);
const asset = JSON.parse(assetString); const asset = JSON.parse(assetString);
asset.Owner = newOwner; asset.Owner = newOwner;
//we insert data in alphabetic order using 'json-stringify-deterministic' and 'sort-keys-recursive' // we insert data in alphabetic order using 'json-stringify-deterministic' and 'sort-keys-recursive'
return ctx.stub.putState(id, Buffer.from(stringify(sortKeysRecursive(asset)))); return ctx.stub.putState(id, Buffer.from(stringify(sortKeysRecursive(asset))));
} }

View file

@ -19,8 +19,8 @@
"dependencies": { "dependencies": {
"fabric-contract-api": "^2.0.0", "fabric-contract-api": "^2.0.0",
"fabric-shim": "^2.0.0", "fabric-shim": "^2.0.0",
"json-stringify-deterministic":"^1.0.0", "json-stringify-deterministic": "^1.0.1",
"sort-keys-recursive":"^2.0.0" "sort-keys-recursive": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.1.2", "chai": "^4.1.2",