Skip to content

Commit

Permalink
Merge pull request #2 from gunet/wwwallet-sync
Browse files Browse the repository at this point in the history
Wwwallet sync
  • Loading branch information
kkmanos authored Nov 25, 2024
2 parents f5242e0 + 6e64ab5 commit 971aeb0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
25 changes: 25 additions & 0 deletions src/entities/VerifiableCredential.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export class VerifiableCredentialEntity {

@Column({ type: "varchar", nullable: false, default: "" })
credentialIssuerIdentifier: string = "";

@Column({ type: "smallint", nullable: false, default: 0 })
instanceId: number = 0;

@Column({ type: "smallint", nullable: false, default: 0 })
sigCount: number = 0;
}


Expand Down Expand Up @@ -68,6 +74,24 @@ async function createVerifiableCredential(createVc: Partial<VerifiableCredential
}
}


async function updateVerifiableCredential(credential: Partial<VerifiableCredentialEntity>) {
try {
console.log("Updating VC...")
await AppDataSource
.createQueryBuilder()
.update(VerifiableCredentialEntity)
.set({ ...credential })
.where("credentialIdentifier = :cred_id and instanceId = :instance_id", { cred_id: credential.credentialIdentifier, instance_id: credential.instanceId })
.execute();
return Ok({});
}
catch(e) {
console.log(e);
return Err(CreateVerifiableCredentialErr.DB_ERR);
}
}

async function deleteVerifiableCredential(holderDID:string, credentialId: string) {
try {
console.log("Deleting VPs containing the VC", credentialId);
Expand Down Expand Up @@ -162,6 +186,7 @@ export {
DeleteVerifiableCredentialErr,
getAllVerifiableCredentials,
createVerifiableCredential,
updateVerifiableCredential,
deleteVerifiableCredential,
getVerifiableCredentialByCredentialIdentifier,
deleteAllCredentialsWithHolderDID
Expand Down
2 changes: 1 addition & 1 deletion src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class UserEntity {
@Column({ type: "bool", default: false })
isAdmin: boolean = false;

@Column({ type: "blob", nullable: false })
@Column({ type: "mediumblob", nullable: false })
privateData: Buffer;


Expand Down
56 changes: 40 additions & 16 deletions src/routers/storage.router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import express, { Request, Response, Router } from "express";
import { getAllVerifiableCredentials, getVerifiableCredentialByCredentialIdentifier, deleteVerifiableCredential, createVerifiableCredential } from "../entities/VerifiableCredential.entity";
import { getAllVerifiableCredentials, getVerifiableCredentialByCredentialIdentifier, deleteVerifiableCredential, createVerifiableCredential, updateVerifiableCredential, VerifiableCredentialEntity } from "../entities/VerifiableCredential.entity";
import { createVerifiablePresentation, deletePresentationsByCredentialId, getAllVerifiablePresentations, getPresentationByIdentifier } from "../entities/VerifiablePresentation.entity";
import { sendPushNotification } from "../lib/firebase";
import { getUser } from "../entities/user.entity";


const storageRouter: Router = express.Router();

storageRouter.post('/vc', storeCredential);
storageRouter.post('/vc', storeCredentials);
storageRouter.post('/vc/update', updateCredential);

storageRouter.get('/vc', getAllVerifiableCredentialsController);
storageRouter.get('/vc/:credential_identifier', getVerifiableCredentialByCredentialIdentifierController);
storageRouter.delete('/vc/:credential_identifier', deleteVerifiableCredentialController);
Expand All @@ -16,20 +18,23 @@ storageRouter.get('/vp', getAllVerifiablePresentationsController);
storageRouter.get('/vp/:presentation_identifier', getPresentationByPresentationIdentifierController);


async function storeCredential(req: Request, res: Response) {
createVerifiableCredential({
holderDID: req.user.did,
issuanceDate: new Date(),
...req.body,
}).then(async () => {
// inform all installed instances of the wallet that a credential has been received

const u = await getUser(req.user.id);
if (u.err) {
return res.send({});
}
async function storeCredentials(req: Request, res: Response) {
const u = await getUser(req.user.id);
if (u.err) {
return res.status(400).send({ error: "Unauthenticated user" });
}
const user = u.unwrap();

const user = u.unwrap();
if (!req.body.credentials || !(req.body.credentials instanceof Array)) {
return res.status(400).send({ error: "Missing or invalid 'credentials' body param" });
}
Promise.all(req.body.credentials.map(async (storableCredential) => {
createVerifiableCredential({
holderDID: req.user.did,
issuanceDate: new Date(),
...storableCredential,
});
})).then(() => {
if (user.fcmTokenList) {
for (const fcmToken of user.fcmTokenList) {
sendPushNotification(fcmToken.value, "New Credential", "A new verifiable credential is in your wallet").catch(err => {
Expand All @@ -38,10 +43,29 @@ async function storeCredential(req: Request, res: Response) {
});
}
}
})
});
res.send({});
}

async function updateCredential(req: Request, res: Response) {
try {
const u = await getUser(req.user.id);
if (u.err) {
return res.status(400).send({ error: "Unauthenticated user" });
}
const user = u.unwrap();
if (!req.body.credential) {
return res.status(400).send({ error: "Missing or invalid 'credential' body param" });
}
await updateVerifiableCredential(req.body.credential);
return res.status(200).send({});
}
catch (err) {
console.error(err);
return res.status(400).send({ error: JSON.stringify(err) });
}
}

async function getAllVerifiableCredentialsController(req: Request, res: Response) {
const holderDID = req.user.did;
console.log("Holder did", holderDID)
Expand Down

0 comments on commit 971aeb0

Please sign in to comment.