Skip to content

Commit

Permalink
feat: finternet service support
Browse files Browse the repository at this point in the history
Signed-off-by: vikastc <[email protected]>
  • Loading branch information
Vikastc committed Oct 31, 2024
1 parent cf148e7 commit caa91e9
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 99 deletions.
25 changes: 14 additions & 11 deletions src/controller/credential_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ 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);
Expand All @@ -40,11 +40,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 +62,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 +103,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 +193,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
75 changes: 57 additions & 18 deletions src/controller/did_controller.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,73 @@
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 } 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 { mnemonic, document } = await createDid(didName);

return res.status(200).json({
result: {
message: 'Successfully created did',
mnemonic,
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 encryptMnemonic(
req: express.Request,
res: express.Response
) {
try {
const { issuerMnemonic } = req.body;

const encryptedMnemonic = JSON.stringify(
await studio_encrypt(issuerMnemonic)
);

return res.status(200).json({
result: { message: 'Encryption Successfully', encryptedMnemonic },
});
} catch (error) {
console.error('Error in encryption', 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

0 comments on commit caa91e9

Please sign in to comment.