Skip to content

Commit

Permalink
Merge pull request #3483 from OriginTrail/fix/private
Browse files Browse the repository at this point in the history
private
  • Loading branch information
Mihajlo-Pavlovic authored Dec 10, 2024
2 parents 6405b18 + e52e898 commit a103f3b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ export const ZERO_ADDRESS = ethers.constants.AddressZero;

export const SCHEMA_CONTEXT = 'http://schema.org/';

export const PRIVATE_ASSERTION_PREDICATE =
'https://ontology.origintrail.io/dkg/1.0#privateMerkleRoot';

export const TRIPLE_ANNOTATION_LABEL_PREDICATE = 'https://ontology.origintrail.io/dkg/1.0#label';

export const PRIVATE_RESOURCE_PREDICATE =
'https://ontology.origintrail.io/dkg/1.0#representsPrivateResource';

export const PRIVATE_HASH_SUBJECT_PREFIX = 'https://ontology.origintrail.io/dkg/1.0#metadata-hash:';

export const UAL_PREDICATE = '<https://ontology.origintrail.io/dkg/1.0#UAL>';

export const COMMIT_BLOCK_DURATION_IN_BLOCKS = 5;
Expand Down
15 changes: 12 additions & 3 deletions src/service/data-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from 'ethers';
import { kcTools } from 'assertion-tools';
import { XML_DATA_TYPES } from '../constants/constants.js';
import { XML_DATA_TYPES, PRIVATE_HASH_SUBJECT_PREFIX } from '../constants/constants.js';

class DataService {
constructor(ctx) {
Expand Down Expand Up @@ -69,8 +69,16 @@ class DataService {
}
}

// Asumes nobody is using PRIVATE_HASH_SUBJECT_PREFIX subject in assertion
quadsContainsPrivateRepresentations(quads) {
return (
quads[0].split(' ')[0].startsWith(`<${PRIVATE_HASH_SUBJECT_PREFIX}`) ||
quads[quads.length - 1].split(' ')[0].startsWith(`<${PRIVATE_HASH_SUBJECT_PREFIX}`)
);
}

generateHashFromString(string) {
return ethers.utils.sha256(ethers.util.solidityPack(['string'], [string]));
return ethers.utils.sha256(ethers.utils.solidityPack(['string'], [string]));
}

splitConnectedArrays(publicTriples) {
Expand All @@ -82,7 +90,8 @@ class DataService {
let currentKA = [publicTriples[0]];

for (let i = 1; i < publicTriples.length; i += 1) {
const subject = publicTriples[i].split(' ')[0];
const [subject] = publicTriples[i].split(' ');

const subjectHash = subject.startsWith('<private-hash:0x')
? subject
: `<private-hash:${this.generateHashFromString(subject.slice(1, -1))}>`;
Expand Down
44 changes: 22 additions & 22 deletions src/service/triple-store-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ class TripleStoreService {
// knowledgeAssetsUALs.map((ual) => `<${ual}>`),
// );
// const unifiedGraphTriples = [...triples, ...tripleAnnotations];
const publicKnowledgeAssetsTriples = this.dataService.groupTriplesBySubject(
const publicKnowledgeAssetsTriples = this.dataService.splitConnectedArrays(
triples.public ?? triples,
);

const publicKnowledgeAssetsUALs = [];
for (let i = 1; i <= (triples.public ?? triples).length; i += 1) {

for (let i = 1; i <= publicKnowledgeAssetsTriples.length; i += 1) {
publicKnowledgeAssetsUALs.push(`${knowledgeCollectionUAL}/${i}`);
}
const promises = [];
Expand All @@ -70,26 +71,25 @@ class TripleStoreService {
const privateKnowledgeAssetsTriples = this.dataService.groupTriplesBySubject(
triples.private,
);

const publicSubjectsMap = new Map(
publicKnowledgeAssetsTriples.map(([triple], index) => {
const [subject] = triple.split(' ');
return [subject, index];
}),
);

const privateKnowledgeAssetsUALs = privateKnowledgeAssetsTriples.reduce(
(result, [triple]) => {
const [privateSubject] = triple.split(' '); // groupTriplesBySubject guarantees format
if (publicSubjectsMap.has(privateSubject)) {
result.push(
publicKnowledgeAssetsUALs[publicSubjectsMap.get(privateSubject)],
);
}
return result;
},
[],
);
const privateKnowledgeAssetsUALs = [];
let publicIndex = 0;
let privateIndex = 0;
while (
publicIndex < publicKnowledgeAssetsTriples.length &&
privateIndex < privateKnowledgeAssetsTriples.length
) {
// const [privateSubject] = privateKnowledgeAssetsTriples[privateIndex][0].split(' ');
// Check if first triple is content or private existence identifier
if (
this.dataService.quadsContainsPrivateRepresentations(
publicKnowledgeAssetsTriples[publicIndex],
)
) {
privateKnowledgeAssetsUALs.push(publicKnowledgeAssetsUALs[publicIndex]);
privateIndex += 1;
}
publicIndex += 1;
}

if (privateKnowledgeAssetsUALs.length > 0) {
promises.push(
Expand Down

0 comments on commit a103f3b

Please sign in to comment.