Skip to content

Commit

Permalink
Add private root validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihajlo-Pavlovic committed Dec 16, 2024
1 parent df14f91 commit 5f84fa7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,22 @@ class GetRequestCommand extends ProtocolRequestCommand {
// Only whole collection can be validated not particular KA
if (!knowledgeAssetId) {
try {
const isValid = await this.validationService.validateDatasetOnBlockchain(
await this.validationService.validateDatasetOnBlockchain(
responseData.assertion.public,
blockchain,
contract,
knowledgeCollectionId,
);
if (!isValid) {
return this.handleNack(command, {
errorMessage:
"Merkle root of received assertion doesn't match on chain merkle root",
});
}
if (responseData.assertion?.private?.length)
await this.validationService.validatePrivateMerkleRoot(
responseData.assertion.public,
responseData.assertion.private,
);
} catch (e) {
return this.handleNack(command, {
errorMessage: e.message,
});
}

// TODO: Validate private part
}
await this.operationService.processResponse(
command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ERROR_TYPE,
LOCAL_STORE_TYPES,
PARANET_ACCESS_POLICY,
PRIVATE_ASSERTION_PREDICATE,
} from '../../../../constants/constants.js';

class PublishValidateAssetCommand extends ValidateAssetCommand {
Expand Down Expand Up @@ -62,18 +61,11 @@ class PublishValidateAssetCommand extends ValidateAssetCommand {
);
await this.validationService.validateDatasetRoot(cachedData.dataset.public, datasetRoot);

const privateAssertionTriple = cachedData.dataset.public.find((triple) =>
triple.includes(PRIVATE_ASSERTION_PREDICATE),
);

if (privateAssertionTriple) {
const privateAssertionRoot = privateAssertionTriple.split(' ')[2].slice(1, -1);

await this.validationService.validateDatasetRoot(
if (cachedData.dataset?.private?.length)
await this.validationService.validatePrivateMerkleRoot(
cachedData.dataset.public,
cachedData.dataset.private,
privateAssertionRoot,
);
}

this.operationIdService.emitChangeEvent(
OPERATION_ID_STATUS.PUBLISH.PUBLISH_VALIDATE_DATASET_ROOT_END,
Expand Down
36 changes: 30 additions & 6 deletions src/service/validation-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZERO_ADDRESS } from '../constants/constants.js';
import { ZERO_ADDRESS, PRIVATE_ASSERTION_PREDICATE } from '../constants/constants.js';

class ValidationService {
constructor(ctx) {
Expand Down Expand Up @@ -39,7 +39,7 @@ class ValidationService {
}

async validateDatasetRootOnBlockchain(
assertionId,
knowledgeCollectionMerkleRoot,
blockchain,
assetStorageContractAddress,
knowledgeCollectionId,
Expand All @@ -51,7 +51,11 @@ class ValidationService {
knowledgeCollectionId,
);

return assertionId === blockchainAssertionRoot;
if (knowledgeCollectionMerkleRoot !== blockchainAssertionRoot) {
throw new Error(
`Merkle Root validation failed. Merkle Root on chain: ${blockchainAssertionRoot}; Calculated Merkle Root: ${knowledgeCollectionMerkleRoot}`,
);
}
}

// Used to validate assertion node received through network get
Expand All @@ -61,10 +65,12 @@ class ValidationService {
assetStorageContractAddress,
knowledgeCollectionId,
) {
const assertionId = await this.validationModuleManager.calculateRoot(assertion);
const knowledgeCollectionMerkleRoot = await this.validationModuleManager.calculateRoot(
assertion,
);

return this.validateDatasetRootOnBlockchain(
assertionId,
await this.validateDatasetRootOnBlockchain(
knowledgeCollectionMerkleRoot,
blockchain,
assetStorageContractAddress,
knowledgeCollectionId,
Expand All @@ -80,6 +86,24 @@ class ValidationService {
);
}
}

async validatePrivateMerkleRoot(publicAssertion, privateAssertion) {
const privateAssertionTriple = publicAssertion.find((triple) =>
triple.includes(PRIVATE_ASSERTION_PREDICATE),
);

if (privateAssertionTriple) {
const privateAssertionRoot = privateAssertionTriple.split(' ')[2].slice(1, -1);

await this.validationService.validateDatasetRoot(
privateAssertion,
privateAssertionRoot,
);
}
throw new Error(
`Merkle Root validation failed. Private Merkle Root not present in public assertion.`,
);
}
}

export default ValidationService;

0 comments on commit 5f84fa7

Please sign in to comment.