Skip to content

Commit

Permalink
credential txn
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratap2018 committed Aug 5, 2024
1 parent 558add7 commit a1dde44
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/credential/credential.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 9 additions & 5 deletions src/credential/services/credential.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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}`,
Expand All @@ -452,6 +456,6 @@ export class CredentialService {
'registerCredentialStatus() method: ends....',
'CredentialService',
);
return { transactionHash: registeredVC.transactionHash };
return { transactionHash: registeredVC?.transactionHash };
}
}
5 changes: 4 additions & 1 deletion src/credential/services/credential.ssi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HypersignVerifiableCredential> {
Logger.log('InitateHypersignVC(): starts....', 'CredentialSSIService');
const nodeRpcEndpoint = this.config.get('HID_NETWORK_RPC');
const nodeRestEndpoint = this.config.get('HID_NETWORK_API');
Expand Down
136 changes: 107 additions & 29 deletions src/tx-send-module/tx-send-module.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,7 +17,7 @@ import {
@Injectable()
export class TxSendModuleService {
private channel: ChannelWrapper;

private granterAddress;
constructor(
private readonly configService: ConfigService,
private readonly hidWalletService: HidWalletService,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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: [
Expand All @@ -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',
Expand All @@ -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);
}
}

0 comments on commit a1dde44

Please sign in to comment.