Skip to content

Commit

Permalink
Merge pull request hashgraph#2163 from hashgraph/feature/1498
Browse files Browse the repository at this point in the history
upload and verify vc
  • Loading branch information
simvalery authored May 23, 2023
2 parents 8bda64d + 92fd2d3 commit 012421c
Show file tree
Hide file tree
Showing 23 changed files with 1,268 additions and 24 deletions.
44 changes: 44 additions & 0 deletions common/src/document-loader/dry-run-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { DidRootKey, DocumentLoader, IDocumentFormat } from '../hedera-modules';
import { DataBaseHelper } from '../helpers';
import { DidDocument, DryRun } from '../entity';

/**
* Dry Run loader
*/
export class DryRunLoader extends DocumentLoader {
/**
* Has context
* @param iri
*/
public async has(iri: string): Promise<boolean> {
const did = DidRootKey.create(iri).getController();
const document= await new DataBaseHelper(DryRun).findOne({did, dryRunClass: 'DidDocumentCollection'});
return !!document;
}

/**
* Get formatted document
* @param iri
*/
public async get(iri: string): Promise<IDocumentFormat> {
const did = DidRootKey.create(iri).getController();
const document= await new DataBaseHelper(DryRun).findOne({did, dryRunClass: 'DidDocumentCollection'});
return {
documentUrl: iri,
document: document.document
};
}

/**
* Get document
* @param iri
*/
public async getDocument(iri: string): Promise<any> {
const did = DidRootKey.create(iri).getController();
const didDocuments = await new DataBaseHelper(DidDocument).findOne({ did });
if (didDocuments) {
return didDocuments.document;
}
throw new Error(`DID not found: ${iri}`);
}
}
78 changes: 78 additions & 0 deletions common/src/document-loader/hedera-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { DidRootKey, DocumentLoader, IDocumentFormat } from '../hedera-modules';
import { DataBaseHelper, IPFS, Workers } from '../helpers';
import { DidDocument } from '../entity';
import { WorkerTaskType } from '@guardian/interfaces';

/**
* Hedera loader
*/
export class HederaLoader extends DocumentLoader {
/**
* Has context
* @param iri
*/
public async has(iri: string): Promise<boolean> {
if (!iri.startsWith('did:hedera:')) {
return false;
}
const document = await this.getDocument(iri);
return !document;

}

/**
* Get formatted document
* @param iri
*/
public async get(iri: string): Promise<IDocumentFormat> {
const did = DidRootKey.create(iri).getController();
const splittedDid = did.split('_');
const topicId = splittedDid[splittedDid.length - 1];

const messages = await new Workers().addRetryableTask(
{
type: WorkerTaskType.GET_TOPIC_MESSAGES,
data: {
operatorId: null,
operatorKey: null,
dryRun: false,
topic: topicId,
},
},
10
);
const didMessage = messages
.map(m => {
try {
return JSON.parse(m.message);
} catch (e) {
return undefined;
}
})
.find(m => {
return (m.type === 'DID-Document') && (m.did === did)
});
if (!didMessage) {
return null
}
const didDocument = await IPFS.getFile(didMessage.cid, 'json')

return {
documentUrl: iri,
document: didDocument
};
}

/**
* Get document
* @param iri
*/
public async getDocument(iri: string): Promise<any> {
const did = DidRootKey.create(iri).getController();
const didDocuments = await new DataBaseHelper(DidDocument).findOne({ did });
if (didDocuments) {
return didDocuments.document;
}
return false;
}
}
2 changes: 2 additions & 0 deletions common/src/document-loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './did-document-loader';
export * from './schema-document-loader';
export * from './subject-schema-loader';
export * from './vc-schema-loader';
export * from './dry-run-loader';
export * from './hedera-loader';
9 changes: 7 additions & 2 deletions common/src/hedera-modules/vcjs/vcjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,21 @@ export class VCJS {
*
* @param {HcsVcDocument<VcSubject>} vcDocument - VC Document
*
* @param loader
* @returns {Promise<boolean>} - is verified
*/
public async verifyVC(vcDocument: VcDocument | any): Promise<boolean> {
public async verifyVC(vcDocument: VcDocument | any, loader?: DocumentLoaderFunction): Promise<boolean> {
let vc: IVC;
if (vcDocument && typeof vcDocument.toJsonTree === 'function') {
vc = vcDocument.toJsonTree();
} else {
vc = vcDocument;
}
return await this.verify(vc, this.loader);
if (!loader) {
return await this.verify(vc, this.loader);
} else {
return await this.verify(vc, loader)
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions common/src/helpers/vc-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
VCSchemaLoader,
SubjectSchemaLoader,
DIDDocumentLoader,
ContextDocumentLoader,
ContextDocumentLoader, DryRunLoader, HederaLoader,
} from '../document-loader';
import {
ICredentialSubject,
Expand All @@ -35,14 +35,18 @@ export class VcHelper extends VCJS {
constructor() {
super();
const defaultDocumentLoader = new DefaultDocumentLoader();
const dryRunLoader = new DryRunLoader();
const didDocumentLoader = new DIDDocumentLoader();
const hederaLoader = new HederaLoader();
const schemaDocumentLoader = new SchemaDocumentLoader();
const contextDocumentLoader = new ContextDocumentLoader('');

const vcSchemaObjectLoader = new VCSchemaLoader('');
const subjectSchemaObjectLoader = new SubjectSchemaLoader('');

this.addDocumentLoader(defaultDocumentLoader);
this.addDocumentLoader(dryRunLoader);
this.addDocumentLoader(hederaLoader);
this.addDocumentLoader(didDocumentLoader);
this.addDocumentLoader(schemaDocumentLoader);
this.addDocumentLoader(contextDocumentLoader);
Expand Down Expand Up @@ -235,4 +239,4 @@ export class VcHelper extends VCJS {
}
return await super.createVP(did, key, vcs, uuid);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@
<mat-icon class="icon-link">add_link</mat-icon> Bind Group
</td>
<td class="propRowCell">
<select-block
<select-block
[root]="module"
[(value)]="field.bindGroup"
[readonly]="readonly"
[(value)]="field.bindGroup"
[readonly]="readonly"
[blocks]="allBlocks"
></select-block>
</td>
Expand All @@ -125,6 +125,7 @@
<mat-select [(value)]="field.action" [disabled]="readonly" (change)="onSave()">
<mat-option value="link">LINK</mat-option>
<mat-option value="dialog">DIALOG</mat-option>
<mat-option value="download">DOWNLOAD</mat-option>
</mat-select>
</td>
</tr>
Expand All @@ -138,9 +139,9 @@
Bind Block
</td>
<td class="propRowCell">
<select-block
<select-block
[root]="module"
[(value)]="field.bindBlock"
[(value)]="field.bindBlock"
[readonly]="readonly"
[blocks]="allBlocks"
></select-block>
Expand Down Expand Up @@ -181,9 +182,9 @@
Bind Block
</td>
<td class="propRowCell">
<select-block
<select-block
[root]="module"
[(value)]="field.bindBlock"
[(value)]="field.bindBlock"
[readonly]="readonly"
[blocks]="allBlocks"
></select-block>
Expand All @@ -201,14 +202,14 @@
Bind Block
</td>
<td class="propRowCell">
<select-block
<select-block
[root]="module"
[(value)]="field.bindBlock"
[readonly]="readonly"
[(value)]="field.bindBlock"
[readonly]="readonly"
[blocks]="allBlocks"
></select-block>
</td>
</tr>
</ng-container>
</ng-container>
</table>
</table>
Loading

0 comments on commit 012421c

Please sign in to comment.