Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update vc functionality implemented #31

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "ISC",
"dependencies": {
"@cord.network/sdk": "0.9.3-1rc4",
"@cord.network/vc-export": "0.9.3-1rc4",
"@cord.network/vc-export": "0.9.3-1rc5",
"body-parser": "^1.20.2",
"dotenv": "^16.0.3",
"express": "^4.18.2",
Expand Down
219 changes: 113 additions & 106 deletions src/controller/credential_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function issueVD(req: express.Request, res: express.Response) {
cred.fromDid = issuerDid.uri;
cred.credHash = newCredContent.credentialHash;
cred.newCredContent = newCredContent;
cred.vc = vc.proof;
cred.vc = vc;

if (statement) {
await getConnection().manager.save(cred);
Expand All @@ -98,6 +98,8 @@ export async function issueVD(req: express.Request, res: express.Response) {
throw new Error('Error in VD issuence');
}

// TODO: If holder id is set vc will be sent to wallet

// const url: any = WALLET_URL;

// if (url && data.type) {
Expand Down Expand Up @@ -142,108 +144,113 @@ export async function getCredById(req: express.Request, res: express.Response) {
}
}

// export async function updateCred(req: express.Request, res: express.Response) {
// const data = req.body;

// if (!data.property || typeof data.property !== 'object') {
// return res.status(400).json({
// error: '"property" is a required field and should be an object',
// });
// }

// try {
// const cred = await getConnection()
// .getRepository(Cred)
// .findOne({ identifier: req.params.id });

// if (!cred) {
// return res.status(400).json({ error: 'Cred not found' });
// }

// console.log(`\n❄️ Statement Updation `);
// let updateCredContent = cred.newCredContent;
// updateCredContent.issuanceDate = new Date().toISOString();
// updateCredContent.property = data.property;
// const serializedUpCred =
// Cord.Utils.Crypto.encodeObjectAsStr(updateCredContent);
// const upCredHash = Cord.Utils.Crypto.hashStr(serializedUpCred);

// const updatedStatementEntry = Cord.Statement.buildFromUpdateProperties(
// cred.credentialEntry.elementUri,
// upCredHash,
// CHAIN_SPACE_ID as `space:cord:${string}`,
// delegateDid.uri
// );

// console.dir(updatedStatementEntry, {
// depth: null,
// colors: true,
// });

// const updatedStatement = await Cord.Statement.dispatchUpdateToChain(
// updatedStatementEntry,
// delegateDid.uri,
// authorIdentity,
// delegateSpaceAuth as Cord.AuthorizationUri,
// async ({ data }) => ({
// signature: delegateKeysProperty.authentication.sign(data),
// keyType: delegateKeysProperty.authentication.type,
// })
// );
// console.log(`✅ Statement element registered - ${updatedStatement}`);

// if (updatedStatement) {
// cred.identifier = updatedStatement;
// cred.credHash = upCredHash;
// cred.newCredContent = updateCredContent;
// cred.credentialEntry = updatedStatementEntry;

// await getConnection().manager.save(cred);

// console.log('\n✅ Statement updated!');

// return res.status(200).json({
// result: 'Updated successufully',
// identifier: cred.identifier,
// });
// }
// return res.status(400).json({ error: 'Document not updated' });
// } catch (error) {
// console.log('error: ', error);
// throw new Error('Error in updating document');
// }
// }

// export async function revokeCred(req: express.Request, res: express.Response) {
// try {
// const cred = await getConnection()
// .getRepository(Cred)
// .findOne({ identifier: req.params.id });

// if (!cred) {
// return res.status(400).json({ error: 'Invalid identifier' });
// }

// await Cord.Statement.dispatchRevokeToChain(
// cred.credentialEntry.elementUri,
// delegateDid.uri,
// authorIdentity,
// delegateSpaceAuth as Cord.AuthorizationUri,
// async ({ data }) => ({
// signature: delegateKeysProperty.authentication.sign(data),
// keyType: delegateKeysProperty.authentication.type,
// })
// );

// cred.active = false;

// await getConnection().manager.save(cred);

// console.log(`✅ Statement revoked!`);

// return res.status(200).json({ result: 'Statement revoked Successfully' });
// } catch (error) {
// console.log('err: ', error);
// return res.status(400).json({ err: error });
// }
// }
export async function updateCred(req: express.Request, res: express.Response) {
const data = req.body;

if (!data.properties || typeof data.properties !== 'object') {
return res.status(400).json({
error: '"property" is a required field and should be an object',
});
}

try {
const cred = await getConnection()
.getRepository(Cred)
.findOne({ identifier: req.params.id });

if (!cred) {
return res.status(400).json({ error: 'Cred not found' });
}

console.log(`\n❄️ Statement Updation `);

const updatedCredContent = await Vc.updateVcFromContent(
data.properties,
cred.vc,
undefined
);

let updatedVc: any = await Vc.addProof(
updatedCredContent,
async (data) => ({
signature: await issuerKeysProperty.assertionMethod.sign(data),
keyType: issuerKeysProperty.assertionMethod.type,
keyUri: `${issuerDid.uri}${
issuerDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
}),
issuerDid,
{
spaceUri: CHAIN_SPACE_ID as `space:cord:${string}`,
schemaUri: cred.schemaId,
needSDR: true,
}
);

console.dir(updatedVc, {
depth: null,
colors: true,
});

const updatedStatement = await Cord.Statement.dispatchRegisterToChain(
updatedVc.proof[1],
issuerDid.uri,
authorIdentity,
CHAIN_SPACE_AUTH as `auth:cord:${string}`,
async ({ data }) => ({
signature: issuerKeysProperty.authentication.sign(data),
keyType: issuerKeysProperty.authentication.type,
})
);

console.log(`✅ UpdatedStatement element registered - ${updatedStatement}`);

if (updatedStatement) {
cred.identifier = updatedStatement;
cred.credHash = updatedCredContent.credentialHash;
cred.newCredContent = updatedCredContent;
cred.vc = updatedVc;

await getConnection().manager.save(cred);

console.log('\n✅ Statement updated!');

return res.status(200).json({
result: 'Updated successufully',
identifier: cred.identifier,
});
}
return res.status(400).json({ error: 'Document not updated' });
} catch (error) {
console.log('error: ', error);
throw new Error('Error in updating document');
}
}

export async function revokeCred(req: express.Request, res: express.Response) {
// try {
// const cred = await getConnection()
// .getRepository(Cred)
// .findOne({ identifier: req.params.id });
// if (!cred) {
// return res.status(400).json({ error: 'Invalid identifier' });
// }
// await Cord.Statement.dispatchRevokeToChain(
// cred.vc[1].elementUri,
// delegateDid.uri,
// authorIdentity,
// delegateSpaceAuth as Cord.AuthorizationUri,
// async ({ data }) => ({
// signature: delegateKeysProperty.authentication.sign(data),
// keyType: delegateKeysProperty.authentication.type,
// })
// );
// cred.active = false;
// await getConnection().manager.save(cred);
// console.log(`✅ Statement revoked!`);
// return res.status(200).json({ result: 'Statement revoked Successfully' });
// } catch (error) {
// console.log('err: ', error);
// return res.status(400).json({ err: error });
// }
}
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { addDelegateAsRegistryDelegate } from './init';
import {
getCredById,
issueVD,
// revokeCred,
// updateCred,
revokeCred,
updateCred,
} from './controller/credential_controller';

const app = express();
Expand All @@ -31,13 +31,13 @@ credentialRouter.get('/:id', async (req, res) => {
return await getCredById(req, res);
});

// credentialRouter.put('/update/:id', async (req, res) => {
// return await updateCred(req, res);
// });
credentialRouter.put('/update/:id', async (req, res) => {
return await updateCred(req, res);
});

// credentialRouter.post('/revoke/:id', async (req, res) => {
// return await revokeCred(req, res);
// });
credentialRouter.post('/revoke/:id', async (req, res) => {
return await revokeCred(req, res);
});

schemaRouter.post('/', async (req, res) => {
return await createSchema(req, res);
Expand Down
50 changes: 25 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@
tweetnacl "^1.0.3"
uuid "^9.0.1"

"@cord.network/[email protected]1rc4":
version "0.9.3-1rc4"
resolved "https://registry.yarnpkg.com/@cord.network/vc-export/-/vc-export-0.9.3-1rc4.tgz#3a7e4bd9541996b9a885890d9b4d8c929f4b1bbc"
integrity sha512-tQ5quLivf9Szab1Pubeg2pet8+qm6jIXjxMtfy2C2VqucIHlPZSW2mEU3qBEBrnWBsgSFvISqPjfIPgabfTphg==
"@cord.network/[email protected]1rc5":
version "0.9.3-1rc5"
resolved "https://registry.yarnpkg.com/@cord.network/vc-export/-/vc-export-0.9.3-1rc5.tgz#416a695687a4f5d4e397054e115567b0f335220d"
integrity sha512-gfY19z159k6Ro5ydxH0VrhnXE+SLZaqFRplRZhJq6R+AUCzbWmV2k0FuWcyrU7eoAFDAprVkBnjW8gA+iM28LA==
dependencies:
"@cord.network/sdk" "0.9.3-1rc4"
moment "^2.29.4"
Expand Down Expand Up @@ -232,9 +232,9 @@
wrap-ansi-cjs "npm:wrap-ansi@^7.0.0"

"@jridgewell/resolve-uri@^3.0.3":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz#c08679063f279615a3326583ba3a90d1d82cc721"
integrity sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==
version "3.1.2"
resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz#7a0ee601f60f99a20c7c7c5ff0c80388c1189bd6"
integrity sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==

"@jridgewell/sourcemap-codec@^1.4.10":
version "1.4.15"
Expand Down Expand Up @@ -697,9 +697,9 @@
integrity sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==

"@types/node@*", "@types/node@^20.11.0":
version "20.11.17"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.17.tgz#cdd642d0e62ef3a861f88ddbc2b61e32578a9292"
integrity sha512-QmgQZGWu1Yw9TDyAP9ZzpFJKynYNeOvwMJmaxABfieQoVoiVOS6MN1WSpqpRcbeA5+RW82kraAVxCCJg+780Qw==
version "20.11.20"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.20.tgz#f0a2aee575215149a62784210ad88b3a34843659"
integrity sha512-7/rR21OS+fq8IyHTgtLkDK949uzsa6n8BkziAKtPVpugIkO6D+/ooXMvzXxDnZrmtXVfjb1bKQafYpb8s89LOg==
dependencies:
undici-types "~5.26.4"

Expand Down Expand Up @@ -1116,9 +1116,9 @@ diff@^4.0.1:
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

dotenv@^16.0.3:
version "16.4.4"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.4.tgz#a26e7bb95ebd36272ebb56edb80b826aecf224c1"
integrity sha512-XvPXc8XAQThSjAbY6cQ/9PcBXmFoWuw1sQ3b8HqUCR6ziGXjkTi//kB9SWa2UwqlgdAIuRqAa/9hVljzPehbYg==
version "16.4.5"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==

dotenv@^8.2.0:
version "8.6.0"
Expand Down Expand Up @@ -1358,9 +1358,9 @@ has-property-descriptors@^1.0.1:
es-define-property "^1.0.0"

has-proto@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/has-proto/-/has-proto-1.0.1.tgz#1885c1305538958aff469fef37937c22795408e0"
integrity sha512-7qE+iP+O+bgF9clE5+UoBFzE65mlBiVj3tKCrlNQ0Ogwm0BjpT/gK4SlLYDMybDh5I3TCTKnPPa0oMG7JDYrhg==
version "1.0.3"
resolved "https://registry.yarnpkg.com/has-proto/-/has-proto-1.0.3.tgz#b31ddfe9b0e6e9914536a6ab286426d0214f77fd"
integrity sha512-SJ1amZAJUiZS+PhsVLf5tGydlaVB8EdFpaSO4gmiUKUOxk8qzn5AIy4ZeJUmh22znIdk/uMAUT2pl3FxzVUH+Q==

has-symbols@^1.0.3:
version "1.0.3"
Expand Down Expand Up @@ -1586,9 +1586,9 @@ [email protected]:
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

nock@^13.4.0:
version "13.5.1"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.5.1.tgz#4e40f9877ad0d43b7cdb474261c190f3715dd806"
integrity sha512-+s7b73fzj5KnxbKH4Oaqz07tQ8degcMilU4rrmnKvI//b0JMBU4wEXFQ8zqr+3+L4eWSfU3H/UoIVGUV0tue1Q==
version "13.5.3"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.5.3.tgz#9858adf5b840696a410baf98bda720d5fad4f075"
integrity sha512-2NlGmHIK2rTeyy7UaY1ZNg0YZfEJMxghXgZi0b4DBsUyoDNTTxZeCSG1nmirAWF44RkkoV8NnegLVQijgVapNQ==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
Expand Down Expand Up @@ -2053,9 +2053,9 @@ supports-color@^7.1.0:
has-flag "^4.0.0"

swagger-ui-dist@>=4.11.0:
version "5.11.3"
resolved "https://registry.yarnpkg.com/swagger-ui-dist/-/swagger-ui-dist-5.11.3.tgz#64c96e90b6a352e7b20a55b73b91fc0e0bed4f0a"
integrity sha512-vQ+Pe73xt7vMVbX40L6nHu4sDmNCM6A+eMVJPGvKrifHQ4LO3smH0jCiiefKzsVl7OlOcVEnrZ9IFzYwElfMkA==
version "5.11.7"
resolved "https://registry.yarnpkg.com/swagger-ui-dist/-/swagger-ui-dist-5.11.7.tgz#4765f2f2c4abe30b34d4aa03d625cf12833fb5b4"
integrity sha512-8xqfJFRVEEU3Zxe8vQvC4g827lDOqV9haougR3tnlgF/PkDN//9Y6kGV9qsT0A9vawlbdF8dF/R6HrlDyHvbEQ==

swagger-ui-express@^4.6.2:
version "4.6.3"
Expand Down Expand Up @@ -2203,9 +2203,9 @@ vary@~1.1.2:
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==

web-streams-polyfill@^3.0.3:
version "3.3.2"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.2.tgz#32e26522e05128203a7de59519be3c648004343b"
integrity sha512-3pRGuxRF5gpuZc0W+EpwQRmCD7gRqcDOMt688KmdlDAgAyaB1XlN0zq2njfDNm44XVdIouE7pZ6GzbdyH47uIQ==
version "3.3.3"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz#2073b91a2fdb1fbfbd401e7de0ac9f8214cecb4b"
integrity sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==

which@^2.0.1:
version "2.0.2"
Expand Down
Loading