Skip to content

Commit

Permalink
Merge pull request #52 from ashwin275/feat/create/did
Browse files Browse the repository at this point in the history
Feat/create/did
  • Loading branch information
ashwin275 authored Oct 25, 2024
2 parents e8d0d3b + a360d58 commit cf148e7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 48 deletions.
46 changes: 2 additions & 44 deletions src/controller/did_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mnemonicGenerate } from '@polkadot/util-crypto';
import {
addDelegateAsRegistryDelegate,
authorIdentity,
createDidName,
createDid
} from '../init';


Expand All @@ -19,51 +19,9 @@ export async function generateDid(
if (!authorIdentity) {
await addDelegateAsRegistryDelegate();
}
const api = Cord.ConfigService.get('api');
const mnemonic = mnemonicGenerate(24);

const delegateKeys = Cord.Utils.Keys.generateKeypairs(mnemonic, 'sr25519');
const {
authentication,
keyAgreement,
assertionMethod,
capabilityDelegation,
} = delegateKeys;

const didUri = Cord.Did.getDidUriFromKey(authentication);

const serviceData = req.body.services[0];
const processedService = processServiceData(serviceData);

const didCreationTx = await Cord.Did.getStoreTx(
{
authentication: [authentication],
keyAgreement: [keyAgreement],
assertionMethod: [assertionMethod],
capabilityDelegation: [capabilityDelegation],
service: processedService.length > 0 ? processedService : [
{
id: '#my-service',
type: ['service-type'],
serviceEndpoint: ['https://www.example.com'],
},
],
},
authorIdentity.address,
async ({ data }) => ({
signature: authentication.sign(data),
keyType: authentication.type,
})
);

await Cord.Chain.signAndSubmitTx(didCreationTx, authorIdentity);

const encodedDid = await api.call.didApi.query(Cord.Did.toChain(didUri));
const { document } = Cord.Did.linkedInfoFromChain(encodedDid);

if (!document) {
throw new Error('DID was not successfully created.');
}
const { mnemonic, delegateKeys, document } = await createDid(authorIdentity, processedService);

return res.status(200).json({ mnemonic, delegateKeys, document });
} catch (error) {
Expand Down
7 changes: 3 additions & 4 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function createDidName(

export async function createDid(
submitterAccount: Cord.CordKeyringPair,
service?: Cord.DidServiceEndpoint[],
didName?: string | undefined
): Promise<{
mnemonic: string;
Expand Down Expand Up @@ -65,8 +66,7 @@ export async function createDid(
keyAgreement: [keyAgreement],
assertionMethod: [assertionMethod],
capabilityDelegation: [capabilityDelegation],
// Example service.
service: [
service: Array.isArray(service) && service.length > 0 ? service : [
{
id: '#my-service',
type: ['service-type'],
Expand Down Expand Up @@ -105,17 +105,16 @@ export async function createDid(
if (!document) {
throw new Error('DID was not successfully created.');
}

delegateDid = document;
delegateKeysProperty = delegateKeys;

return { mnemonic, delegateKeys, document };
} catch (err) {
console.log('Error: ', err);
throw new Error('Failed to create delegate DID');
}
}


export async function checkDidAndIdentities(mnemonic: string): Promise<any> {
if (!mnemonic) return null;

Expand Down

0 comments on commit cf148e7

Please sign in to comment.