Skip to content

Commit

Permalink
chore: upgrade sdk to v0.35 (#81)
Browse files Browse the repository at this point in the history
## fixes KILTProtocol/ticket#NoTicket

Upgrades to SDK version v0.35

It also fixes a bug that stoped the `did-configuration` from running
succesfully.
  • Loading branch information
kilted-andres authored May 6, 2024
1 parent 0882d58 commit 2ee5d7d
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 158 deletions.
6 changes: 3 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"build": "tsc"
},
"dependencies": {
"@kiltprotocol/sdk-js": "^0.32.0",
"@kiltprotocol/types": "^0.32.0",
"@kiltprotocol/vc-export": "^0.32.0",
"@kiltprotocol/sdk-js": "^0.35.0",
"@kiltprotocol/types": "^0.35.0",
"@kiltprotocol/vc-export": "^0.35.0",
"body-parser": "^1.20.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
Expand Down
51 changes: 23 additions & 28 deletions backend/src/credentials/verifySubmittedCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { readCredentialCookie } from './readCredentialCookie'
export async function verifySubmittedCredential(
request: Request,
response: Response,
cTypeRequested: Kilt.IRequestCredentialContent
cTypesRequested: Kilt.IRequestCredentialContent
): Promise<Kilt.ICredentialPresentation> {
const encryptedMessage = request.body
console.log(
Expand All @@ -23,7 +23,7 @@ export async function verifySubmittedCredential(
2
)}`
)
const api = await getApi()
await getApi()

const { keyAgreement } = generateKeyPairs(DAPP_DID_MNEMONIC)
const decryptedMessage = await Kilt.Message.decrypt(
Expand All @@ -44,51 +44,46 @@ export async function verifySubmittedCredential(

console.log('Decrypted Credential being verify: \n', credential)

const chosenCType = cTypesRequested.cTypes.find(
(ctype) => ctype.cTypeHash === credential.claim.cTypeHash
)

if (!chosenCType) {
throw new Error(
"The User did not complied to the Credential Request. The Server does not accept the submitted Credential's Type."
)
}

// Know against to what structure you want to compare to:
const requestedCTypeHash = cTypeRequested.cTypes[0].cTypeHash
const requestedCTypeDetailed = await Kilt.CType.fetchFromChain(
const requestedCTypeHash = chosenCType.cTypeHash
const { cType: requestedCType } = await Kilt.CType.fetchFromChain(
`kilt:ctype:${requestedCTypeHash}`
)

// The function Credential.verifyPresentation can check against a specific cType structure.
// This cType needs to match the ICType-interface.
// To fullfil this structure we need to remove the 'creator' and 'createdAt' properties from our fetched object.
const { $id, $schema, title, properties, type } = requestedCTypeDetailed
const requestedCType = { $id, $schema, title, properties, type }

const challengeOnRequest = await readCredentialCookie(
request,
response,
JWT_SIGNER_SECRET
)

await Kilt.Credential.verifyPresentation(credential, {
challenge: challengeOnRequest,
ctype: requestedCType
})

const attestationChain = await api.query.attestation.attestations(
credential.rootHash
)

const attestation = Kilt.Attestation.fromChain(
attestationChain,
credential.rootHash
const verifiedCredential = await Kilt.Credential.verifyPresentation(
credential,
{
challenge: challengeOnRequest,
ctype: requestedCType
}
)

if (attestation.revoked) {
if (verifiedCredential.revoked) {
throw new Error("Credential has been revoked and hence it's not valid.")
}

// Check if the credentials was issued by one of our "trusted attesters"
const attesterOfTheirCredential = attestation.owner
const ourTrustedAttesters = cTypeRequested.cTypes.find((ctype) => {
ctype.cTypeHash === credential.claim.cTypeHash
})?.trustedAttesters
const ourTrustedAttesters = chosenCType.trustedAttesters

// If you don't include a list of trusted attester on the credential-request, this check would be skipped
if (ourTrustedAttesters) {
if (!ourTrustedAttesters.includes(attesterOfTheirCredential)) {
if (!ourTrustedAttesters.includes(verifiedCredential.attester)) {
throw new Error(
`The Credential was not issued by any of the trusted Attesters that the dApp relies on. \n List of trusted attesters: ${ourTrustedAttesters}`
)
Expand Down
5 changes: 3 additions & 2 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Kilt from '@kiltprotocol/sdk-js'
import express, { Express, NextFunction, Request, Response } from 'express'
import cors from 'cors'
import bodyParser from 'body-parser'
Expand All @@ -23,6 +22,7 @@ import {

import { logout } from './access/logout'
import { checkAccessCookie } from './access/checkAccessCookie'
import { getApi } from './utils/connection'

const app: Express = express()

Expand Down Expand Up @@ -117,7 +117,8 @@ validateEnvironmentConstants()

async function connectToKiltWebSocket() {
try {
await Kilt.connect(WSS_ADDRESS)
// internally calls Kilt.connect(WSS_ADDRESS)
await getApi()
console.log(
`🔗[websocket]: Connected to WebSocket server at ${WSS_ADDRESS}`
)
Expand Down
2 changes: 0 additions & 2 deletions backend/src/utils/fetchDidDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ export async function fetchDidDocument(): Promise<Kilt.DidDocument> {
)
}

Kilt.disconnect()

return didDocument
}
6 changes: 3 additions & 3 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"private": true,
"proxy": "http://localhost:2525/",
"dependencies": {
"@kiltprotocol/sdk-js": "^0.32.0",
"@kiltprotocol/types": "^0.31.0",
"@kiltprotocol/vc-export": "^0.31.0",
"@kiltprotocol/sdk-js": "^0.35.0",
"@kiltprotocol/types": "^0.35.0",
"@kiltprotocol/vc-export": "^0.35.0",
"@polkadot/util": "^10.4.2",
"kilt-extension-api": "0.1.0",
"react": "^18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
"typescript": "^5.0.4"
},
"dependencies": {
"@kiltprotocol/sdk-js": "^0.32.0",
"@kiltprotocol/types": "^0.32.0",
"@kiltprotocol/vc-export": "^0.32.0",
"@kiltprotocol/sdk-js": "^0.35.0",
"@kiltprotocol/types": "^0.35.0",
"@kiltprotocol/vc-export": "^0.35.0",
"@polkadot/util": "10.4.2",
"@polkadot/util-crypto": "10.4.2",
"@types/valid-url": "^1.0.7",
Expand Down
2 changes: 1 addition & 1 deletion scripts/wellKnownDIDConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function createCredential(
// Make sure that `ctypeDomainLinkage` contains the correct Domain Linkage CType.
// Extensions expect this specific CTypes other CTypes will not be recognized.
const cTypeUri =
'kilt:ctype:0x9d271c790775ee831352291f01c5d04c7979713a5896dcf5e81708184cc5c643'
'kilt:ctype:0xb08800a574c436831a2b9fce00fd16e9df489b2b3695e88a0895d148eca0311e'
if (ctypeDomainLinkage.$id !== cTypeUri) {
console.log(
'The following CType URI is not the expected one:',
Expand Down
Loading

0 comments on commit 2ee5d7d

Please sign in to comment.