Skip to content

Commit

Permalink
Refactor trasaction agent executeTransaction method for better readab…
Browse files Browse the repository at this point in the history
…ility
  • Loading branch information
ognjenkurtic committed Jul 3, 2024
1 parent 16c6bec commit 02e5224
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions examples/bri-3/src/bri/transactions/agents/transactions.agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -182,20 +184,10 @@ export class TransactionAgent {
): Promise<TransactionResult> {
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,
Expand All @@ -206,25 +198,17 @@ export class TransactionAgent {
} = this.constructCircuitPathsFromWorkstepName(workstep.name);

txResult.witness = await this.circuitService.createWitness(
circuitInputs,
await this.prepareCircuitInputs(tx, workstep.circuitInputsTranslationSchema),
circuitPath,
circuitProvingKeyPath,
circuitVerificatioKeyPath,
circuitWitnessCalculatorPath,
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;
Expand Down Expand Up @@ -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
Expand All @@ -308,6 +289,21 @@ export class TransactionAgent {
return name;
}

private async prepareCircuitInputs(
tx: Transaction,
circuitInputsTranslationSchema: string,
): Promise<object> {
const payloadAsCircuitInputs = await this.preparePayloadAsCircuitInputs(
tx.payload,
circuitInputsTranslationSchema,
);

return Object.assign(
payloadAsCircuitInputs,
await computeEddsaSigPublicInputs(tx),
);
}

private async preparePayloadAsCircuitInputs(
txPayload: string,
workstepTranslationSchema: string,
Expand All @@ -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
Expand Down

0 comments on commit 02e5224

Please sign in to comment.