Skip to content

Commit

Permalink
Merge pull request #1256 from OriginTrail/prerelease/mainnet
Browse files Browse the repository at this point in the history
OriginTrail Mainnet Release v4.1.1
  • Loading branch information
djordjekovac authored May 13, 2020
2 parents 3e5434b + 131cdd7 commit 5172831
Show file tree
Hide file tree
Showing 64 changed files with 1,131 additions and 503 deletions.
3 changes: 0 additions & 3 deletions modules/DVService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ class DVService {
*/
constructor({
blockchain, web3, config, graphStorage, logger, remoteControl,
notifyError,
}) {
this.blockchain = blockchain;
this.web3 = web3;
this.config = config;
this.graphStorage = graphStorage;
this.log = logger;
this.remoteControl = remoteControl;
this.notifyError = notifyError;
}

async handleEncryptedPaddedKey(message) {
Expand Down Expand Up @@ -149,7 +147,6 @@ class DVService {
await networkQuery.save({ fields: ['status'] });
} catch (err) {
this.log.warn(`Invalid purchase decryption key, Reply ID ${id}, wallet ${wallet}, import ID ${importId}.`);
this.notifyError(err);
if (await this._litigatePurchase(importId, wallet, nodeId, m1, m2, e)) {
networkQuery.status = 'FINISHED';
await networkQuery.save({ fields: ['status'] });
Expand Down
3 changes: 1 addition & 2 deletions modules/Database/GraphStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ class GraphStorage {
* @param logger
* @param selectedDatabase Selected graph database
*/
constructor(selectedDatabase, logger, notifyError) {
constructor(selectedDatabase, logger) {
this.logger = logger;
this.selectedDatabase = selectedDatabase;
this._allowedClasses = ['Location', 'Actor', 'Product', 'Transport',
'Transformation', 'Observation', 'Ownership'];
this.notifyError = notifyError;
}

/**
Expand Down
24 changes: 0 additions & 24 deletions modules/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class EventEmitter {
profileService,
dcService,
dvController,
notifyError,
commandExecutor,
} = this.ctx;

Expand All @@ -114,7 +113,6 @@ class EventEmitter {
data.response.send(res);
}).catch((error) => {
logger.error(`Failed to get trail for query ${JSON.stringify(data.query)}`);
notifyError(error);
data.response.status(500);
data.response.send({
message: error,
Expand All @@ -140,7 +138,6 @@ class EventEmitter {
data.response.send(res);
}).catch((error) => {
logger.error(`Failed to get trail for query ${JSON.stringify(data.query)}`);
notifyError(error);
data.response.status(500);
data.response.send({
message: error,
Expand Down Expand Up @@ -197,7 +194,6 @@ class EventEmitter {
}
} catch (error) {
logger.error(`Failed to get vertices for data-set ID ${dataSetId}.`);
notifyError(error);
data.response.status(500);
data.response.send({
message: error,
Expand Down Expand Up @@ -240,7 +236,6 @@ class EventEmitter {
data.response.send(res);
}).catch((error) => {
logger.error(`Failed to get vertices for query ${JSON.stringify(data.query)}`);
notifyError(error);
data.response.status(500);
data.response.send({
message: `Failed to get vertices for query ${JSON.stringify(data.query)}`,
Expand Down Expand Up @@ -332,7 +327,6 @@ class EventEmitter {
}
} catch (error) {
logger.error(`Failed to get vertices for data set ID ${dataSetId}. ${error}.${error.stack}`);
notifyError(error);
data.response.status(500);
data.response.send({
message: error.toString(),
Expand All @@ -357,7 +351,6 @@ class EventEmitter {
// (`Successfully withdrawn ${trac_amount} TRAC`);
} catch (error) {
logger.error(`Failed to withdraw tokens. ${error}.`);
notifyError(error);
data.response.status(400);
data.response.send({
message: `Failed to withdraw tokens. ${error}.`,
Expand Down Expand Up @@ -442,7 +435,6 @@ class EventEmitter {
dcService,
dvController,
dcController,
notifyError,
networkService,
} = this.ctx;

Expand Down Expand Up @@ -474,7 +466,6 @@ class EventEmitter {
} catch (error) {
const errorMessage = `Failed to process data location request. ${error}.`;
logger.warn(errorMessage);
notifyError(error);
}
});

Expand Down Expand Up @@ -502,7 +493,6 @@ class EventEmitter {
} catch (error) {
const errorMessage = `Failed to handle replication request. ${error}.`;
logger.warn(errorMessage);
notifyError(error);

try {
await transport.sendResponse(response, {
Expand Down Expand Up @@ -533,7 +523,6 @@ class EventEmitter {
} catch (error) {
const errorMessage = `Failed to handle replacement replication request. ${error}.`;
logger.warn(errorMessage);
notifyError(error);

try {
await transport.sendResponse(response, {
Expand Down Expand Up @@ -582,7 +571,6 @@ class EventEmitter {
} catch (e) {
const errorMessage = `Failed to handle replication finished request. ${e}.`;
logger.warn(errorMessage);
notifyError(e);
}
});

Expand All @@ -600,7 +588,6 @@ class EventEmitter {
} catch (e) {
const errorMessage = `Failed to handle replacement replication finished request. ${e}.`;
logger.warn(errorMessage);
notifyError(e);
}
});

Expand Down Expand Up @@ -646,7 +633,6 @@ class EventEmitter {
);
} catch (error) {
logger.error(`Failed to get data. ${error}.`);
notifyError(error);
}
});

Expand Down Expand Up @@ -685,7 +671,6 @@ class EventEmitter {
);
} catch (error) {
logger.error(`Failed to get data. ${error}.`);
notifyError(error);
}
});

Expand All @@ -705,7 +690,6 @@ class EventEmitter {
await dvController.handleDataLocationResponse(message);
} catch (error) {
logger.error(`Failed to process location response. ${error}.`);
notifyError(error);
}
});

Expand Down Expand Up @@ -754,7 +738,6 @@ class EventEmitter {
await dvController.handleDataReadResponseFree(message);
} catch (error) {
logger.warn(`Failed to process data read response. ${error}.`);
notifyError(error);
}
});

Expand Down Expand Up @@ -789,7 +772,6 @@ class EventEmitter {
await dvController.handlePermissionedDataReadResponse(message);
} catch (error) {
logger.warn(`Failed to process permissioned data read response. ${error}.`);
notifyError(error);
}
});

Expand All @@ -815,7 +797,6 @@ class EventEmitter {
await dcController.handleNetworkPurchaseRequest(message);
} catch (error) {
logger.warn(`Failed to process data purchase request. ${error}.`);
notifyError(error);
}
});

Expand All @@ -840,7 +821,6 @@ class EventEmitter {
await dvController.handleNetworkPurchaseResponse(message);
} catch (error) {
logger.warn(`Failed to process data purchase response. ${error}.`);
notifyError(error);
}
});

Expand All @@ -867,7 +847,6 @@ class EventEmitter {
await dcController.handleNetworkPriceRequest(message);
} catch (error) {
logger.warn(`Failed to process data price request. ${error}.`);
notifyError(error);
}
});

Expand All @@ -892,7 +871,6 @@ class EventEmitter {
await dvController.handlePermissionedDataPriceResponse(message);
} catch (error) {
logger.warn(`Failed to process data price response. ${error}.`);
notifyError(error);
}
});

Expand All @@ -918,7 +896,6 @@ class EventEmitter {
} catch (error) {
const errorMessage = `Failed to process encrypted key response. ${error}.`;
logger.warn(errorMessage);
notifyError(error);
await transport.sendEncryptedKeyProcessResult({
status: 'FAIL',
message: error.message,
Expand Down Expand Up @@ -948,7 +925,6 @@ class EventEmitter {
} catch (error) {
const errorMessage = `Failed to send public key data. ${error}.`;
logger.warn(errorMessage);
notifyError(error);
await transport.sendResponse(response, {
status: 'FAIL',
message: error.message,
Expand Down
58 changes: 23 additions & 35 deletions modules/ImportUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Graph = require('./Graph');
const Encryption = require('./RSAEncryption');
const { normalizeGraph } = require('./Database/graph-converter');
const Models = require('../models');
const OtJsonUtilities = require('./OtJsonUtilities');

const data_constants = {
vertexType: {
Expand Down Expand Up @@ -138,11 +139,12 @@ class ImportUtilities {
return graph;
}

static prepareDataset(document, config, web3) {
static prepareDataset(originalDocument, config, web3) {
const document = originalDocument; // todo add otJsonService
const graph = document['@graph'];
const datasetHeader = document.datasetHeader ? document.datasetHeader : {};
ImportUtilities.calculateGraphPermissionedDataHashes(graph);
const id = ImportUtilities.calculateGraphPublicHash(graph);
ImportUtilities.calculateGraphPermissionedDataHashes(document['@graph']);
const id = ImportUtilities.calculateGraphPublicHash(document);

const header = ImportUtilities.createDatasetHeader(
config, null,
Expand All @@ -158,7 +160,7 @@ class ImportUtilities {
'@graph': graph,
};

const rootHash = ImportUtilities.calculateDatasetRootHash(dataset['@graph'], id, header.dataCreator);
const rootHash = ImportUtilities.calculateDatasetRootHash(dataset);
dataset.datasetHeader.dataIntegrity.proofs[0].proofValue = rootHash;

const signed = ImportUtilities.signDataset(dataset, config, web3);
Expand Down Expand Up @@ -449,14 +451,15 @@ class ImportUtilities {
);
}

static calculateDatasetRootHash(graph, datasetId, datasetCreator) {
const publicGraph = Utilities.copyObject(graph);
ImportUtilities.removeGraphPermissionedData(publicGraph);

ImportUtilities.sortGraphRecursively(publicGraph);
static calculateDatasetRootHash(dataset) {
const datasetClone = Utilities.copyObject(dataset);
ImportUtilities.removeGraphPermissionedData(datasetClone['@graph']);
const sortedDataset = OtJsonUtilities.prepareDatasetForGeneratingRootHash(datasetClone);
const datasetId = sortedDataset['@id'];
const datasetCreator = sortedDataset.datasetHeader.dataCreator;

const merkle = ImportUtilities.createDistributionMerkleTree(
publicGraph,
sortedDataset['@graph'],
datasetId,
datasetCreator,
);
Expand Down Expand Up @@ -581,26 +584,15 @@ class ImportUtilities {
return transactionHash;
}

/**
* Create SHA256 Hash of graph
* @param graph
* @returns {string}
*/
static calculateGraphHash(graph) {
const sorted = this.sortGraphRecursively(graph);
return `0x${sha3_256(sorted, null, 0)}`;
}

/**
* Create SHA256 Hash of public part of one graph
* @param graph
* @param dataset
* @returns {string}
*/
static calculateGraphPublicHash(graph) {
const public_data = Utilities.copyObject(graph);
ImportUtilities.removeGraphPermissionedData(public_data);
const sorted = ImportUtilities.sortGraphRecursively(public_data);
return `0x${sha3_256(sorted, null, 0)}`;
static calculateGraphPublicHash(dataset) {
const sortedDataset = OtJsonUtilities.prepareDatasetForGeneratingGraphHash(dataset);
ImportUtilities.removeGraphPermissionedData(sortedDataset['@graph']);
return `0x${sha3_256(JSON.stringify(sortedDataset['@graph']), null, 0)}`;
}

/**
Expand Down Expand Up @@ -639,19 +631,17 @@ class ImportUtilities {
* @static
*/
static signDataset(otjson, config, web3) {
const completeGraph = Utilities.copyObject(otjson['@graph']);
ImportUtilities.removeGraphPermissionedData(otjson['@graph']);
const stringifiedOtjson = this.sortStringifyDataset(otjson);
const sortedOTJson = OtJsonUtilities.prepareDatasetForGeneratingSignature(otjson);
ImportUtilities.removeGraphPermissionedData(sortedOTJson['@graph']);
const { signature } = web3.eth.accounts.sign(
stringifiedOtjson,
JSON.stringify(sortedOTJson),
Utilities.normalizeHex(config.node_private_key),
);
otjson.signature = {
value: signature,
type: 'ethereum-signature',
};

otjson['@graph'] = completeGraph;
return otjson;
}

Expand All @@ -660,11 +650,9 @@ class ImportUtilities {
* @static
*/
static extractDatasetSigner(otjson, web3) {
const strippedOtjson = Object.assign({}, otjson);
const strippedOtjson = OtJsonUtilities.prepareDatasetForGeneratingSignature(otjson);
delete strippedOtjson.signature;

const stringifiedOtjson = this.sortStringifyDataset(strippedOtjson);
return web3.eth.accounts.recover(stringifiedOtjson, otjson.signature.value);
return web3.eth.accounts.recover(JSON.stringify(strippedOtjson), otjson.signature.value);
}


Expand Down
Loading

0 comments on commit 5172831

Please sign in to comment.