Skip to content

Commit

Permalink
upgrade SDK and others (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
arty-name committed Jun 9, 2023
1 parent cb29640 commit 95ccb34
Show file tree
Hide file tree
Showing 6 changed files with 810 additions and 963 deletions.
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,43 @@
"dev-start": "supervisor --watch dist/backend/ -- --enable-source-maps dist/backend/server.js"
},
"dependencies": {
"@kiltprotocol/sdk-js": "^0.32.0",
"@kiltprotocol/vc-export": "^0.32.0",
"@polkadot/util-crypto": "^10.4.1",
"@kiltprotocol/sdk-js": "^0.33.0",
"@kiltprotocol/vc-export": "^0.33.0",
"@polkadot/util-crypto": "^12.2.2",
"body-parser": "^1.20.2",
"dotenv": "^16.0.3",
"dotenv": "^16.1.4",
"express": "^4.18.2",
"http-status-codes": "^2.2.0",
"ky": "^0.33.2",
"ky": "^0.33.3",
"node-cache": "^5.1.2",
"pino": "^8.10.0",
"pino-pretty": "^9.4.0",
"pino": "^8.14.1",
"pino-pretty": "^10.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/express": "^4.17.17",
"@types/node": "^18.11.14",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-typescript": "^3.5.3",
"@types/react": "^18.2.9",
"@types/react-dom": "^18.2.4",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"parcel": "^2.7.0",
"postcss": "^8.4.21",
"prettier": "^2.8.4",
"postcss": "^8.4.24",
"prettier": "^2.8.8",
"process": "^0.11.10",
"stylelint": "^15.2.0",
"stylelint-config-standard": "^30.0.1",
"stylelint": "^15.7.0",
"stylelint-config-standard": "^33.0.0",
"supervisor": "^0.12.0",
"typescript": "^4.9.5"
"typescript": "^5.1.3"
},
"targets": {
"frontend": {
Expand Down
51 changes: 23 additions & 28 deletions src/backend/endpoints/verify.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import type {
IAttestation,
ICredentialPresentation,
} from '@kiltprotocol/sdk-js';
import {
Attestation,
ConfigService,
Credential,
CType,
Message,
} from '@kiltprotocol/sdk-js';
import type { DidUri, ICredentialPresentation } from '@kiltprotocol/sdk-js';
import { Credential, CType, Message } from '@kiltprotocol/sdk-js';
import { Request, Response, Router } from 'express';
import { StatusCodes } from 'http-status-codes';

import { Session, sessionMiddleware } from '../utilities/sessionStorage';
import { logger } from '../utilities/logger';
import { decrypt } from '../utilities/cryptoCallbacks';
import { trustedAttesters } from '../utilities/trustedAttesters';
import { socialCTypeIds } from '../utilities/supportedCType';
import { socialCTypeIds, supportedCTypes } from '../utilities/supportedCType';

import { paths } from './paths';

interface Output {
presentation: ICredentialPresentation;
isAttested: boolean;
attestation?: IAttestation;
revoked?: boolean;
attester?: DidUri;
}

async function handler(request: Request, response: Response): Promise<void> {
Expand Down Expand Up @@ -55,31 +47,34 @@ async function handler(request: Request, response: Response): Promise<void> {

const presentation = messageBody.content[0];
try {
await Credential.verifyPresentation(presentation, { challenge });
const cTypeId = CType.hashToId(presentation.claim.cTypeHash);
if (!socialCTypeIds.includes(cTypeId)) {
response.status(StatusCodes.FORBIDDEN).send('Not a CType we requested');
return;
}

const ctype = Object.values(supportedCTypes).find(
({ $id }) => $id === cTypeId,
);

const api = ConfigService.get('api');
const attestation = Attestation.fromChain(
await api.query.attestation.attestations(presentation.rootHash),
presentation.rootHash,
const { revoked, attester } = await Credential.verifyPresentation(
presentation,
{
ctype,
challenge,
},
);

const isAttested =
!attestation.revoked &&
attestation.cTypeHash === presentation.claim.cTypeHash;
const isAttested = !revoked;

if (!trustedAttesters.includes(attestation.owner)) {
if (!trustedAttesters.includes(attester)) {
response
.status(StatusCodes.FORBIDDEN)
.send('Not an attester we requested');
return;
}

if (!socialCTypeIds.includes(CType.hashToId(attestation.cTypeHash))) {
response.status(StatusCodes.FORBIDDEN).send('Not a CType we requested');
return;
}

response.send({ presentation, isAttested, attestation } as Output);
response.send({ presentation, isAttested, attester, revoked } as Output);
} catch {
response.send({ presentation, isAttested: false } as Output);
} finally {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/utilities/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { cwd } from 'node:process';
import path from 'node:path';

import dotenv from 'dotenv';
import { config } from 'dotenv';
import { pino } from 'pino';
import { DidUri } from '@kiltprotocol/sdk-js';

dotenv.config();
config();

class ConfigurationError extends Error {
constructor(message: string) {
Expand Down
19 changes: 9 additions & 10 deletions src/backend/utilities/cryptoCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { naclOpen, naclSeal } from '@polkadot/util-crypto';
import {
DecryptCallback,
DidResourceUri,
EncryptCallback,
Utils,
} from '@kiltprotocol/sdk-js';

import { keypairsPromise } from './keypairs';
Expand Down Expand Up @@ -30,37 +30,36 @@ export async function encrypt({
const { did, keyAgreementKey } = await didDocumentPromise;
const keyUri: DidResourceUri = `${did}${keyAgreementKey.id}`;

const { sealed, nonce } = naclSeal(
const { box, nonce } = Utils.Crypto.encryptAsymmetric(
data,
keyAgreement.secretKey,
peerPublicKey,
keyAgreement.secretKey,
);

return {
data: sealed,
data: box,
nonce,
keyUri,
};
}

export async function decrypt({
data,
data: box,
nonce,
peerPublicKey,
}: Parameters<DecryptCallback>[0]) {
const { keyAgreement } = await keypairsPromise;

const decrypted = naclOpen(
data,
nonce,
const data = Utils.Crypto.decryptAsymmetric(
{ box, nonce },
peerPublicKey,
keyAgreement.secretKey,
);
if (!decrypted) {
if (!data) {
throw new Error('Failed to decrypt with given key');
}

return {
data: decrypted,
data,
};
}
17 changes: 10 additions & 7 deletions src/frontend/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
IAttestation,
DidUri,
ICredentialPresentation,
IEncryptedMessage,
} from '@kiltprotocol/sdk-js';
Expand Down Expand Up @@ -34,7 +34,8 @@ function App() {
const [error, setError] = useState<'closed' | 'rejected' | 'unknown'>();

const [presentation, setPresentation] = useState<ICredentialPresentation>();
const [attestation, setAttestation] = useState<IAttestation>();
const [attester, setAttester] = useState<DidUri>();
const [revoked, setRevoked] = useState<boolean>();
const [isAttested, setIsAttested] = useState<boolean>(false);

const { kilt } = apiWindow;
Expand Down Expand Up @@ -69,12 +70,14 @@ function App() {
const result: {
presentation: ICredentialPresentation;
isAttested: boolean;
attestation?: IAttestation;
revoked?: boolean;
attester?: DidUri;
// decrypt the message and verify credential in the backend:
} = await ky.post(paths.verify, { headers, json: message }).json();

setPresentation(result.presentation);
setAttestation(result.attestation);
setAttester(result.attester);
setRevoked(result.revoked);
setIsAttested(result.isAttested);
});

Expand Down Expand Up @@ -133,11 +136,11 @@ function App() {

{processing && <p>Connecting…</p>}

{presentation && !isAttested && attestation?.revoked && (
{presentation && !isAttested && revoked && (
<h3>❌ You’ve shared a revoked credential</h3>
)}

{presentation && !isAttested && !attestation?.revoked && (
{presentation && !isAttested && !revoked && (
<h3>❓ You’ve shared a not yet attested credential</h3>
)}

Expand All @@ -156,7 +159,7 @@ function App() {
<summary>Credential details 🔍</summary>

<h4>Attester:</h4>
<p>SocialKYC ✅ ({attestation?.owner})</p>
<p>SocialKYC ✅ ({attester})</p>

<h4>Your DID:</h4>
<p>{presentation.claim.owner}</p>
Expand Down
Loading

0 comments on commit 95ccb34

Please sign in to comment.