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: finternet service support #53

Merged
merged 2 commits into from
Nov 4, 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
26 changes: 15 additions & 11 deletions src/controller/credential_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const { CHAIN_SPACE_ID, CHAIN_SPACE_AUTH } = process.env;
export async function issueVC(req: express.Request, res: express.Response) {
let data = req.body;
const api = Cord.ConfigService.get('api');
if (!authorIdentity) {
await addDelegateAsRegistryDelegate();
}
// if (!authorIdentity) {
// await addDelegateAsRegistryDelegate();
// }

try {
const validationError = validateCredential(data);

if (validationError) {
return res.status(400).json({ error: validationError });
}
Expand All @@ -40,11 +41,12 @@ export async function issueVC(req: express.Request, res: express.Response) {

const parsedSchema = JSON.parse(schema?.cordSchema as string);

let holder = issuerDid.uri;
let holder = delegateDid?.uri || '';
if (data.properties.id) {
holder = data.properties.id;
delete data.properties.id;
}

const newCredContent = await Vc.buildVcFromContent(
parsedSchema.schema,
data.properties,
Expand All @@ -61,8 +63,9 @@ export async function issueVC(req: express.Request, res: express.Response) {
async (data) => ({
signature: await issuerKeysProperty.assertionMethod.sign(data),
keyType: issuerKeysProperty.assertionMethod.type,
keyUri: `${issuerDid.uri}${issuerDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
keyUri: `${issuerDid.uri}${
issuerDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
}),
issuerDid,
api,
Expand Down Expand Up @@ -101,9 +104,9 @@ export async function issueVC(req: express.Request, res: express.Response) {

if (statement) {
await dataSource.manager.save(cred);
return res
.status(200)
.json({ result: 'success', identifier: cred.identifier, vc: vc });
return res.status(200).json({
result: { message: 'SUCCESS', identifier: cred.identifier, vc },
});
} else {
return res.status(400).json({ error: 'Credential not issued' });
}
Expand Down Expand Up @@ -191,8 +194,9 @@ export async function updateCred(req: express.Request, res: express.Response) {
async (data) => ({
signature: await issuerKeysProperty.assertionMethod.sign(data),
keyType: issuerKeysProperty.assertionMethod.type,
keyUri: `${issuerDid.uri}${issuerDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
keyUri: `${issuerDid.uri}${
issuerDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
}),
issuerDid,
api,
Expand Down
69 changes: 51 additions & 18 deletions src/controller/did_controller.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,67 @@
import * as Cord from '@cord.network/sdk';
import express from 'express';
import 'reflect-metadata';
import { processServiceData } from '../utils/DidValidationUtils'
import { mnemonicGenerate } from '@polkadot/util-crypto';
import {
addDelegateAsRegistryDelegate,
authorIdentity,
createDid
} from '../init';

import { createDid, getDidDocFromName } from '../init';
import { studio_encrypt } from '../identity/org';

export async function generateDid(
export async function generateDid(req: express.Request, res: express.Response) {
const { didName } = req.body;

try {
const { document } = await createDid(didName);

return res.status(200).json({
result: {
message: 'Successfully created did',
document,
},
});
} catch (error) {
console.log('err: ', error);
return res.status(400).json({ error: 'Did not created' });
}
}

export async function didNameNewCheck(
req: express.Request,
res: express.Response
) {
const id = req.params.id;
const api = Cord.ConfigService.get('api');

try {
if (!authorIdentity) {
await addDelegateAsRegistryDelegate();
}
const serviceData = req.body.services[0];
const processedService = processServiceData(serviceData);
const { mnemonic, delegateKeys, document } = await createDid(authorIdentity, processedService);

return res.status(200).json({ mnemonic, delegateKeys, document });
const encodedDidNameOwner = await api.call.didApi.queryByName(id);

// Check if the DID has a linked URI
const hasUri = encodedDidNameOwner?.isSome
? Boolean(
Cord.Did.linkedInfoFromChain(encodedDidNameOwner)?.document?.uri
)
: false;

return res.status(200).json({ result: hasUri });
} catch (error) {
console.log('err: ', error);
return res.status(500).json({ error: 'Did not created' });
console.error('Error querying DID name:', error);
return res
.status(400)
.json({ success: false, message: 'Internal server error' });
}
}

export async function getDidDoc(req: express.Request, res: express.Response) {
try {
const didName = req.params.id;

const didUri = await getDidDocFromName(didName);

return res.status(200).json({
result: { message: 'Did Successfully fetched', didUri },
});
} catch (error) {
console.error('Error in did fetch', error);
return res
.status(400)
.json({ success: false, message: 'Internal server error' });
}
}
9 changes: 4 additions & 5 deletions src/controller/schema_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export async function createSchema(
res: express.Response
) {
try {
if (!authorIdentity) {
await addDelegateAsRegistryDelegate();
}
// if (!authorIdentity) {
// await addDelegateAsRegistryDelegate();
// }

let data = req.body.schema?.schema || req.body.schema || null;

Expand Down Expand Up @@ -70,8 +70,7 @@ export async function createSchema(

await dataSource.manager.save(schemaData);
return res.status(200).json({
result: 'SUCCESS',
schemaId: schemaData.identifier,
result: { message: 'SUCCESS', schemaId: schemaData.identifier },
});
}
return res.status(400).json({ error: 'SchemaDetails not created' });
Expand Down
99 changes: 99 additions & 0 deletions src/identity/org.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { Keyring } from '@polkadot/keyring';
import { KeyringInstance, KeyringPair } from '@polkadot/keyring/types';
import {
mnemonicToMiniSecret,
mnemonicGenerate,
naclDecrypt,
naclEncrypt,
} from '@polkadot/util-crypto';
import { stringToU8a, u8aToString, u8aToHex, hexToU8a } from '@polkadot/util';
import nacl, { BoxKeyPair } from 'tweetnacl';

export type Identity = {
key: KeyringPair;
boxPair: BoxKeyPair;
};

export type EncryptedString = {
encrypt: string;
nonce: string;
};

let studio_identity: Identity;

let keyring: KeyringInstance;

async function keyringInit() {
await cryptoWaitReady();

keyring = new Keyring({ type: 'sr25519', ss58Format: 29 });
}

async function generateNewKey(phrase: string) {
if (!keyring) {
await keyringInit();
}
const seed = mnemonicToMiniSecret(phrase);
return {
key: keyring.addFromSeed(seed),
boxPair: nacl.box.keyPair.fromSecretKey(seed),
};
}

export async function studio_identity_init(mnemonic: string) {
studio_identity = await generateNewKey(mnemonic);
}

export async function org_identity_create() {
const mnemonic = mnemonicGenerate();
const org: [string, Identity] = [mnemonic, await generateNewKey(mnemonic)];
return org;
}

export async function encrypt(key: Identity, u8data: Buffer) {
//const u8data = stringToU8a(data);
const { encrypted, nonce } = naclEncrypt(u8data, key.boxPair.secretKey);
return { encrypt: u8aToHex(encrypted), nonce: u8aToHex(nonce) };
}

export async function decrypt(key: Identity, encrypted: EncryptedString) {
const decrypt = naclDecrypt(
hexToU8a(encrypted.encrypt),
hexToU8a(encrypted.nonce),
key.boxPair.secretKey
);
return decrypt;
}

export async function studio_encrypt(mnemonic: string) {
const u8data = stringToU8a(mnemonic);
const { encrypted, nonce } = naclEncrypt(
u8data,
studio_identity.boxPair.secretKey
);
return { encrypt: u8aToHex(encrypted), nonce: u8aToHex(nonce) };
}

export async function studio_decrypt(encrypted: EncryptedString) {
const decrypt = naclDecrypt(
hexToU8a(encrypted.encrypt),
hexToU8a(encrypted.nonce),
studio_identity.boxPair.secretKey
);
return u8aToString(decrypt);
}

export async function org_identity_from_mnemonic(mnemonic: string) {
return await generateNewKey(mnemonic);
}

export async function org_identity_from_encrypted_mnemonic(
encrypted: EncryptedString
) {
const mnemonic = await studio_decrypt(encrypted);
if (mnemonic) {
return await generateNewKey(mnemonic);
}
return null;
}
Loading