From 02e52247761d80abbb3c05499ee0e465d40d6d8a Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Fri, 28 Jun 2024 22:33:24 +0200 Subject: [PATCH] Refactor trasaction agent executeTransaction method for better readability --- .../transactions/agents/transactions.agent.ts | 62 +++++++++++-------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/examples/bri-3/src/bri/transactions/agents/transactions.agent.ts b/examples/bri-3/src/bri/transactions/agents/transactions.agent.ts index 4f221ff9d..7bc3d78ee 100644 --- a/examples/bri-3/src/bri/transactions/agents/transactions.agent.ts +++ b/examples/bri-3/src/bri/transactions/agents/transactions.agent.ts @@ -7,6 +7,8 @@ import { import { Transaction } from '../models/transaction'; import { TransactionStatus } from '../models/transactionStatus.enum'; +import MerkleTree from 'merkletreejs'; +import { Witness } from 'src/bri/zeroKnowledgeProof/models/witness'; import { AuthAgent } from '../../auth/agent/auth.agent'; import { BpiSubjectAccount } from '../../identity/bpiSubjectAccounts/models/bpiSubjectAccount'; import { PublicKeyType } from '../../identity/bpiSubjects/models/publicKey'; @@ -182,20 +184,10 @@ export class TransactionAgent { ): Promise { const txResult = new TransactionResult(); - const merkelizedPayload = this.merkleTreeService.merkelizePayload( + txResult.merkelizedPayload = this.merkleTreeService.merkelizePayload( JSON.parse(tx.payload), `${process.env.MERKLE_TREE_HASH_ALGH}`, ); - txResult.merkelizedPayload = merkelizedPayload; - - const payloadAsCircuitInputs = await this.preparePayloadAsCircuitInputs( - tx.payload, - workstep.circuitInputsTranslationSchema, - ); - const circuitInputs = Object.assign( - payloadAsCircuitInputs, - await computeEddsaSigPublicInputs(tx), - ); const { circuitProvingKeyPath, @@ -206,7 +198,7 @@ export class TransactionAgent { } = this.constructCircuitPathsFromWorkstepName(workstep.name); txResult.witness = await this.circuitService.createWitness( - circuitInputs, + await this.prepareCircuitInputs(tx, workstep.circuitInputsTranslationSchema), circuitPath, circuitProvingKeyPath, circuitVerificatioKeyPath, @@ -214,17 +206,9 @@ export class TransactionAgent { circuitWitnessFilePath, ); - const hashFn = this.merkleTreeService.createHashFunction( - `${process.env.MERKLE_TREE_HASH_ALGH}`, - ); - - const merkelizedInvoiceRoot = merkelizedPayload.getRoot().toString('hex'); - const witnessHash = hashFn(JSON.stringify(txResult.witness)).toString( - 'hex', - ); - - txResult.hash = hashFn(`${merkelizedInvoiceRoot}${witnessHash}`).toString( - 'hex', + txResult.hash = this.constructTxHash( + txResult.merkelizedPayload, + txResult.witness, ); return txResult; @@ -287,10 +271,7 @@ export class TransactionAgent { }; } - // TODO: #744 ChatGPT generated only for the purposes of temporary convention - // to connect worksteps with circuits on the file system. private convertStringToSnakeCase(name: string): string { - // Remove any leading or trailing spaces name = name.trim(); // Replace spaces, hyphens, and underscores with a single underscore @@ -308,6 +289,21 @@ export class TransactionAgent { return name; } + private async prepareCircuitInputs( + tx: Transaction, + circuitInputsTranslationSchema: string, + ): Promise { + const payloadAsCircuitInputs = await this.preparePayloadAsCircuitInputs( + tx.payload, + circuitInputsTranslationSchema, + ); + + return Object.assign( + payloadAsCircuitInputs, + await computeEddsaSigPublicInputs(tx), + ); + } + private async preparePayloadAsCircuitInputs( txPayload: string, workstepTranslationSchema: string, @@ -329,6 +325,20 @@ export class TransactionAgent { return parsedInputs; } + + private constructTxHash( + merkelizedPayload: MerkleTree, + witness: Witness, + ): string { + const hashFn = this.merkleTreeService.createHashFunction( + `${process.env.MERKLE_TREE_HASH_ALGH}`, + ); + + const merkelizedInvoiceRoot = merkelizedPayload.getRoot().toString('hex'); + const witnessHash = hashFn(JSON.stringify(witness)).toString('hex'); + + return hashFn(`${merkelizedInvoiceRoot}${witnessHash}`).toString('hex'); + } } // TODO: Example input preparation for other workstep circuits from the example use-case, to be used // to properly setup dynamic mappings and to delete afterwards