From c74209346f30645786ef78b95367d39ae0c95c87 Mon Sep 17 00:00:00 2001 From: Pratap2018 Date: Wed, 14 Aug 2024 19:01:42 +0530 Subject: [PATCH] backword compitiblity --- src/credential/services/credential.service.ts | 100 +++++++++++---- src/did/services/did.service.ts | 119 +++++++++++++----- src/schema/services/schema.service.ts | 50 ++++++-- 3 files changed, 202 insertions(+), 67 deletions(-) diff --git a/src/credential/services/credential.service.ts b/src/credential/services/credential.service.ts index 9a332073..650ce769 100644 --- a/src/credential/services/credential.service.ts +++ b/src/credential/services/credential.service.ts @@ -28,6 +28,25 @@ export class CredentialService { private readonly txnService: TxSendModuleService, ) {} + async checkAllowence(address) { + const url = + this.config.get('HID_NETWORK_API') + + '/cosmos/feegrant/v1beta1/allowances/' + + address; + + const resp = await fetch(url); + + const res = await resp.json(); + if (resp.status === 200) { + if (res.allowances.length > 0) { + return true; + } else { + return false; + } + } + return false; + } + async create(createCredentialDto: CreateCredentialDto, appDetail) { Logger.log('create() method: starts....', 'CredentialService'); const { @@ -341,31 +360,47 @@ export class CredentialService { 'update() method: before calling hypersignVC.updateCredentialStatus to update cred status on chain', 'CredentialService', ); - const updateCredenital: any = await hypersignVC.updateCredentialStatus({ - credentialStatus, - issuerDid, - verificationMethodId, - privateKeyMultibase, - status: statusChange, - statusReason, - readonly: true, - }); - - console.log( - updateCredenital?.credentialStatus, - updateCredenital?.proofValue, - ); - await this.txnService.sendUpdateVC( - updateCredenital?.credentialStatus, - updateCredenital?.proofValue, + const { wallet, address } = await this.hidWallet.generateWallet( appMenemonic, ); + let updatedCredResult; + if (await this.checkAllowence(address)) { + const updateCredenital: any = await hypersignVC.updateCredentialStatus({ + credentialStatus, + issuerDid, + verificationMethodId, + privateKeyMultibase, + status: statusChange, + statusReason, + readonly: true, + }); + + await this.txnService.sendUpdateVC( + updateCredenital?.credentialStatus, + updateCredenital?.proofValue, + appMenemonic, + ); + } else { + updatedCredResult = await hypersignVC.updateCredentialStatus({ + credentialStatus, + issuerDid, + verificationMethodId, + privateKeyMultibase, + status: statusChange, + statusReason, + readonly: false, + }); + } - // await this.credentialRepository.findOneAndUpdate( - // { appId: appDetail.appId, credentialId: id }, - // { transactionHash: updatedCredResult.transactionHash }, - // ); + await this.credentialRepository.findOneAndUpdate( + { appId: appDetail.appId, credentialId: id }, + { + transactionHash: updatedCredResult?.transactionHash + ? updatedCredResult?.transactionHash + : '', + }, + ); Logger.log('update() method: ends....', 'CredentialService'); return await hypersignVC.resolveCredentialStatus({ @@ -461,13 +496,26 @@ export class CredentialService { ); const { proof } = credentialStatus; + delete credentialStatus['proof']; - // registeredVC = await hypersignVC.registerCredentialStatus({ - // credentialStatus, - // credentialStatusProof: proof, - // }); - await this.txnService.sendVCTxn(credentialStatus, proof, appMenemonic); + const { wallet, address } = await this.hidWallet.generateWallet( + appMenemonic, + ); + + const hypersignVC = await this.credentialSSIService.initateHypersignVC( + appMenemonic, + namespace, + ); + + if (await this.checkAllowence(address)) { + await this.txnService.sendVCTxn(credentialStatus, proof, appMenemonic); + } else { + registeredVC = await hypersignVC.registerCredentialStatus({ + credentialStatus, + credentialStatusProof: proof, + }); + } const registredCredential = await this.registrationStatus(credentialId); Logger.log('Registred Credential', registredCredential); diff --git a/src/did/services/did.service.ts b/src/did/services/did.service.ts index 7d28a1d8..4316994c 100644 --- a/src/did/services/did.service.ts +++ b/src/did/services/did.service.ts @@ -43,6 +43,25 @@ export class DidService { private readonly txnService: TxSendModuleService, ) {} + async checkAllowence(address) { + const url = + this.config.get('HID_NETWORK_API') + + '/cosmos/feegrant/v1beta1/allowances/' + + address; + + const resp = await fetch(url); + + const res = await resp.json(); + if (resp.status === 200) { + if (res.allowances.length > 0) { + return true; + } else { + return false; + } + } + return false; + } + // TODO: need to fix this once ed25519 is finished. async createByClientSpec(createDidDto: CreateDidDto, appDetail) { Logger.log('createByClientSpec() method: starts....', 'DidService'); @@ -369,29 +388,38 @@ export class DidService { seed: seed, }); const regDidDocument = registerDidDto.didDocument as Did; - const params = { - didDocument: regDidDocument, - privateKeyMultibase, - verificationMethodId: verificationMethodId, - }; + Logger.log( 'register() method: before calling hypersignDid.register ', 'DidService', ); - + const didDocPreserved = {}; + Object.assign(didDocPreserved, didDocument); const signInfos = await hypersignDid.createSignInfos({ didDocument, privateKeyMultibase, verificationMethodId: verificationMethodId, }); - await this.txnService.sendDIDTxn( - didDocument, - signInfos, - verificationMethodId, + const params = { + didDocument: didDocPreserved, + privateKeyMultibase, + verificationMethodId: verificationMethodId, + }; + + const { wallet, address } = await this.hidWallet.generateWallet( appMenemonic, ); + if (await this.checkAllowence(address)) { + await this.txnService.sendDIDTxn( + didDocument, + signInfos, + verificationMethodId, + appMenemonic, + ); + } else { + registerDidDoc = await hypersignDid.register(params); + } - // registerDidDoc = await hypersignDid.register(params); data = await this.didRepositiory.findOneAndUpdate( { did: didDocument['id'] }, { @@ -633,36 +661,67 @@ export class DidService { }); try { - updatedDid = await hypersignDid.update({ - didDocument: updateDidDto.didDocument as Did, - privateKeyMultibase, - verificationMethodId: resolvedDid['verificationMethod'][0].id, - versionId: updatedDidDocMetaData.versionId, - readonly: true, - }); + const { wallet, address } = await this.hidWallet.generateWallet( + appMenemonic, + ); + if (!updateDidDto.deactivate) { Logger.debug( 'updateDid() method: before calling hypersignDid.update to update did', 'DidService', ); - await this.txnService.sendDIDUpdate( - updatedDid.didDocument, - updatedDid.signInfos, - updatedDid.versionId, - appMenemonic, - ); + if ((await this.checkAllowence(address)) == false) { + updatedDid = await hypersignDid.update({ + didDocument: updateDidDto.didDocument as Did, + privateKeyMultibase, + verificationMethodId: resolvedDid['verificationMethod'][0].id, + versionId: updatedDidDocMetaData.versionId, + readonly: false, + }); + } else { + updatedDid = await hypersignDid.update({ + didDocument: updateDidDto.didDocument as Did, + privateKeyMultibase, + verificationMethodId: resolvedDid['verificationMethod'][0].id, + versionId: updatedDidDocMetaData.versionId, + readonly: true, + }); + await this.txnService.sendDIDUpdate( + updatedDid.didDocument, + updatedDid.signInfos, + updatedDid.versionId, + appMenemonic, + ); + } } else { Logger.debug( 'updateDid() method: before calling hypersignDid.deactivate to deactivate did', 'DidService', ); - await this.txnService.sendDIDDeactivate( - updatedDid.didDocument, - updatedDid.signInfos, - updatedDid.versionId, - appMenemonic, - ); + + if ((await this.checkAllowence(address)) == false) { + updatedDid = await hypersignDid.deactivate({ + didDocument: updateDidDto.didDocument as Did, + privateKeyMultibase, + verificationMethodId: resolvedDid['verificationMethod'][0].id, + versionId: updatedDidDocMetaData.versionId, + }); + } else { + updatedDid = await hypersignDid.update({ + didDocument: updateDidDto.didDocument as Did, + privateKeyMultibase, + verificationMethodId: resolvedDid['verificationMethod'][0].id, + versionId: updatedDidDocMetaData.versionId, + readonly: true, + }); + await this.txnService.sendDIDDeactivate( + updatedDid.didDocument, + updatedDid.signInfos, + updatedDid.versionId, + appMenemonic, + ); + } } } catch (error) { Logger.error( diff --git a/src/schema/services/schema.service.ts b/src/schema/services/schema.service.ts index 629ab8bd..78237a0c 100644 --- a/src/schema/services/schema.service.ts +++ b/src/schema/services/schema.service.ts @@ -32,6 +32,25 @@ export class SchemaService { private readonly didRepositiory: DidRepository, private readonly txnService: TxSendModuleService, ) {} + + async checkAllowence(address) { + const url = + this.config.get('HID_NETWORK_API') + + '/cosmos/feegrant/v1beta1/allowances/' + + address; + + const resp = await fetch(url); + + const res = await resp.json(); + if (resp.status === 200) { + if (res.allowances.length > 0) { + return true; + } else { + return false; + } + } + return false; + } async create( createSchemaDto: CreateSchemaDto, appDetail, @@ -94,17 +113,22 @@ export class SchemaService { 'SchemaService', ); - // const registeredSchema = await hypersignSchema.register({ - // schema: signedSchema, - // }); - - let registeredSchema; - - await this.txnService.sendSchemaTxn( - generatedSchema, - signedSchema.proof, + const { wallet, address } = await this.hidWallet.generateWallet( appMenemonic, ); + let registeredSchema; + + if (await this.checkAllowence(address)) { + await this.txnService.sendSchemaTxn( + generatedSchema, + signedSchema.proof, + appMenemonic, + ); + } else { + registeredSchema = await hypersignSchema.register({ + schema: signedSchema, + }); + } Logger.log( 'create() method: storing schema information to DB', @@ -114,13 +138,17 @@ export class SchemaService { schemaId: signedSchema.id, appId: appDetail.appId, authorDid: author, - transactionHash: '', + transactionHash: registeredSchema['transactionHash'] + ? registeredSchema['transactionHash'] + : '', }); Logger.log('create() method: ends', 'SchemaService'); return { schemaId: signedSchema.id, - transactionHash: '', + transactionHash: registeredSchema['transactionHash'] + ? registeredSchema['transactionHash'] + : '', }; } catch (error) { Logger.error(