From cefc0dd4a1d42c4ffaeba6ae3e6aaec124aebf91 Mon Sep 17 00:00:00 2001 From: ashwin275 <110539449+ashwin275@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:19:51 +0530 Subject: [PATCH 1/2] refactore create did endpoint --- src/controller/did_controller.ts | 46 ++------------------------------ src/init.ts | 7 +++-- 2 files changed, 5 insertions(+), 48 deletions(-) diff --git a/src/controller/did_controller.ts b/src/controller/did_controller.ts index 4f9ed3b..8c398fc 100644 --- a/src/controller/did_controller.ts +++ b/src/controller/did_controller.ts @@ -6,7 +6,7 @@ import { mnemonicGenerate } from '@polkadot/util-crypto'; import { addDelegateAsRegistryDelegate, authorIdentity, - createDidName, + createDid } from '../init'; @@ -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) { diff --git a/src/init.ts b/src/init.ts index 7731dc1..dffe052 100644 --- a/src/init.ts +++ b/src/init.ts @@ -38,6 +38,7 @@ export async function createDidName( export async function createDid( submitterAccount: Cord.CordKeyringPair, + service?: Cord.DidServiceEndpoint[], didName?: string | undefined ): Promise<{ mnemonic: string; @@ -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'], @@ -105,10 +105,8 @@ 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); @@ -116,6 +114,7 @@ export async function createDid( } } + export async function checkDidAndIdentities(mnemonic: string): Promise { if (!mnemonic) return null; From a360d589873d07d99175b2698880de06f70517bd Mon Sep 17 00:00:00 2001 From: ashwin275 <110539449+ashwin275@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:20:45 +0530 Subject: [PATCH 2/2] refactore create did endpoint --- src/controller/did_controller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controller/did_controller.ts b/src/controller/did_controller.ts index 8c398fc..eac7642 100644 --- a/src/controller/did_controller.ts +++ b/src/controller/did_controller.ts @@ -21,7 +21,7 @@ export async function generateDid( } const serviceData = req.body.services[0]; const processedService = processServiceData(serviceData); - const { mnemonic, delegateKeys, document } = await createDid(authorIdentity,processedService); + const { mnemonic, delegateKeys, document } = await createDid(authorIdentity, processedService); return res.status(200).json({ mnemonic, delegateKeys, document }); } catch (error) {