From a1dde449965445e7cbfdceadba28a6388962d447 Mon Sep 17 00:00:00 2001 From: Pratap2018 Date: Mon, 5 Aug 2024 15:42:45 +0530 Subject: [PATCH] credential txn --- src/credential/credential.module.ts | 3 +- src/credential/services/credential.service.ts | 14 +- .../services/credential.ssi.service.ts | 5 +- src/tx-send-module/tx-send-module.service.ts | 136 ++++++++++++++---- 4 files changed, 122 insertions(+), 36 deletions(-) diff --git a/src/credential/credential.module.ts b/src/credential/credential.module.ts index 5cc59bfc..3f2641b3 100644 --- a/src/credential/credential.module.ts +++ b/src/credential/credential.module.ts @@ -16,9 +16,10 @@ import { WhitelistSSICorsMiddleware } from 'src/utils/middleware/cors.middleware import { TrimMiddleware } from 'src/utils/middleware/trim.middleware'; import { credentialProviders } from './providers/credential.provider'; import { databaseProviders } from '../mongoose/tenant-mongoose-connections'; +import { TxSendModuleModule } from 'src/tx-send-module/tx-send-module.module'; @Module({ - imports: [EdvModule, HidWalletModule, DidModule], + imports: [EdvModule, HidWalletModule, DidModule, TxSendModuleModule], controllers: [CredentialController], providers: [ CredentialService, diff --git a/src/credential/services/credential.service.ts b/src/credential/services/credential.service.ts index 529385e4..348ffd6d 100644 --- a/src/credential/services/credential.service.ts +++ b/src/credential/services/credential.service.ts @@ -15,6 +15,7 @@ import { HypersignDID, HypersignVerifiableCredential } from 'hs-ssi-sdk'; import { VerifyCredentialDto } from '../dto/verify-credential.dto'; import { RegisterCredentialStatusDto } from '../dto/register-credential.dto'; import { getAppVault, getAppMenemonic } from '../../utils/app-vault-service'; +import { TxSendModuleService } from 'src/tx-send-module/tx-send-module.service'; @Injectable() export class CredentialService { @@ -24,6 +25,7 @@ export class CredentialService { private readonly hidWallet: HidWalletService, private credentialRepository: CredentialRepository, private readonly didRepositiory: DidRepository, + private readonly txnService: TxSendModuleService, ) {} async create(createCredentialDto: CreateCredentialDto, appDetail) { @@ -437,10 +439,12 @@ export class CredentialService { ); const { proof } = credentialStatus; delete credentialStatus['proof']; - registeredVC = await hypersignVC.registerCredentialStatus({ - credentialStatus, - credentialStatusProof: proof, - }); + // registeredVC = await hypersignVC.registerCredentialStatus({ + // credentialStatus, + // credentialStatusProof: proof, + // }); + + await this.txnService.sendVCTxn(credentialStatus, proof, appMenemonic); } catch (e) { Logger.error( `registerCredentialStatus() method: Error ${e.message}`, @@ -452,6 +456,6 @@ export class CredentialService { 'registerCredentialStatus() method: ends....', 'CredentialService', ); - return { transactionHash: registeredVC.transactionHash }; + return { transactionHash: registeredVC?.transactionHash }; } } diff --git a/src/credential/services/credential.ssi.service.ts b/src/credential/services/credential.ssi.service.ts index 91b68e85..d74c9ece 100644 --- a/src/credential/services/credential.ssi.service.ts +++ b/src/credential/services/credential.ssi.service.ts @@ -10,7 +10,10 @@ export class CredentialSSIService { private readonly config: ConfigService, private readonly hidWallet: HidWalletService, ) {} - async initateHypersignVC(mnemonic: string, namespace: string) { + async initateHypersignVC( + mnemonic: string, + namespace: string, + ): Promise { Logger.log('InitateHypersignVC(): starts....', 'CredentialSSIService'); const nodeRpcEndpoint = this.config.get('HID_NETWORK_RPC'); const nodeRestEndpoint = this.config.get('HID_NETWORK_API'); diff --git a/src/tx-send-module/tx-send-module.service.ts b/src/tx-send-module/tx-send-module.service.ts index 1d08587d..b3a4ccbe 100644 --- a/src/tx-send-module/tx-send-module.service.ts +++ b/src/tx-send-module/tx-send-module.service.ts @@ -1,7 +1,10 @@ import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import amqp, { ChannelWrapper } from 'amqp-connection-manager'; -import { MsgRegisterDID } from 'hs-ssi-sdk/build/libs/generated/ssi/tx'; +import { + MsgRegisterDID, + MsgRegisterCredentialStatus, +} from 'hs-ssi-sdk/build/libs/generated/ssi/tx'; import { DidSSIService } from 'src/did/services/did.ssi.service'; import { HidWalletService } from 'src/hid-wallet/services/hid-wallet.service'; import { StdFee } from '@cosmjs/stargate'; @@ -14,7 +17,7 @@ import { @Injectable() export class TxSendModuleService { private channel: ChannelWrapper; - + private granterAddress; constructor( private readonly configService: ConfigService, private readonly hidWalletService: HidWalletService, @@ -23,6 +26,32 @@ export class TxSendModuleService { this.connect(); } + async invokeTxnController(address, granteeMnemonic) { + const podENV = { + RMQ_URL: this.configService.get('RABBIT_MQ_URI'), + DID_QUEUE_NAME: 'DID_TXN_QUEUE_' + address, + CRED_QUEUE_NAME: 'CRED_TXN_QUEUE_' + address, + NODE_RPC_URL: this.configService.get('HID_NETWORK_RPC'), + GRANTEE_MNEMONIC: granteeMnemonic, + GRANTER_ADDRESS: this.granterAddress, + DID_REGISTER_FIXED_FEE: '50', + CRED_REGISTER_FIXED_FEE: '50', + SCHEMA_CREATE_FIXED_FEE: '50', + ESTIMATE_GAS_PRICE: '155303', + podName: 'txn-dynamic', + granteeWalletAddress: address, + }; + + await this.channel.assertQueue('GLOBAL_TXN_CONTROLLER_QUEUE', { + durable: false, + }); + const sendToQueue2 = await this.channel.sendToQueue( + this.configService.get('GLOBAL_TXN_CONTROLLER_QUEUE'), + Buffer.from(JSON.stringify(podENV)), + ); + console.log(sendToQueue2); + } + async prepareMsgCreateDID( didDocument, didDocumentSigned, @@ -71,9 +100,82 @@ export class TxSendModuleService { this.configService.get('RABBIT_MQ_URI'), ); this.channel = await connection.createChannel(); + const { address: granterAddress } = + await this.hidWalletService.generateWallet( + this.configService.get('MNEMONIC'), + ); + this.granterAddress = granterAddress; Logger.log('Connected Rabbit'); } + async prepareRegisterCredentialStatus( + credentialStatus, + credentialStatusProof, + txAuthor, + ) { + return MsgRegisterCredentialStatus.fromPartial({ + credentialStatusDocument: credentialStatus, + credentialStatusProof: credentialStatusProof, + txAuthor, + }); + } + + async sendVCTxn(credentialStatus, credentialStatusProof, granteeMnemonic) { + if (!this.channel) { + await this.connect(); + } + const { wallet, address } = await this.hidWalletService.generateWallet( + granteeMnemonic, + ); + + const msgRegisterCredentialStatus = + await this.prepareRegisterCredentialStatus( + credentialStatus, + credentialStatusProof, + address, + ); + const authExecMsg: MsgExec = { + grantee: address, + msgs: [ + { + typeUrl: '/hypersign.ssi.v1.MsgRegisterCredentialStatus', + value: MsgRegisterCredentialStatus.encode( + msgRegisterCredentialStatus, + ).finish(), + }, + ], + }; + + const fee = { + amount: [ + { + denom: 'uhid', + amount: '100', + }, + ], + gas: '500000', + granter: this.granterAddress, // NOTE: It is VERY IMPORTANT to explicitly pass granter's address + }; + + const txMsg = { + typeUrl: '/cosmos.authz.v1beta1.MsgExec', + value: authExecMsg, + }; + + const queue = 'CRED_TXN_QUEUE_' + address; + await this.channel.assertQueue(queue, { + durable: false, + }); + + const sendToQueue1 = await this.channel.sendToQueue( + queue, + Buffer.from(JSON.stringify(txMsg)), + ); + + await this.invokeTxnController(address, granteeMnemonic); + console.log(sendToQueue1); + } + async sendDIDTxn( didDocument, didDocumentSigned, @@ -94,11 +196,6 @@ export class TxSendModuleService { address, ); - const { address: granterAddress } = - await this.hidWalletService.generateWallet( - this.configService.get('MNEMONIC'), - ); - const authExecMsg: MsgExec = { grantee: address, msgs: [ @@ -116,7 +213,7 @@ export class TxSendModuleService { }, ], gas: '500000', - granter: granterAddress, // NOTE: It is VERY IMPORTANT to explicitly pass granter's address + granter: this.granterAddress, // NOTE: It is VERY IMPORTANT to explicitly pass granter's address }; const txMsg = { typeUrl: '/cosmos.authz.v1beta1.MsgExec', @@ -131,26 +228,7 @@ export class TxSendModuleService { queue, Buffer.from(JSON.stringify(txMsg)), ); - - const podENV = { - RMQ_URL: this.configService.get('RABBIT_MQ_URI'), - DID_QUEUE_NAME: 'DID_TXN_QUEUE_' + address, - NODE_RPC_URL: this.configService.get('HID_NETWORK_RPC'), - GRANTEE_MNEMONIC: granteeMnemonic, - GRANTER_ADDRESS: granterAddress, - FIXED_FEE: '50', - ESTIMATE_GAS_PRICE: '155303', - podName: 'did-txn-dynamic', - granteeWalletAddress: address, - }; - - await this.channel.assertQueue('GLOBAL_TXN_CONTROLLER_QUEUE', { - durable: false, - }); - const sendToQueue2 = await this.channel.sendToQueue( - this.configService.get('GLOBAL_TXN_CONTROLLER_QUEUE'), - Buffer.from(JSON.stringify(podENV)), - ); - console.log(sendToQueue1, sendToQueue2); + await this.invokeTxnController(address, granteeMnemonic); + console.log(sendToQueue1); } }