diff --git a/common/mock-enclave/src/main/kotlin/com/r3/conclave/encryptedtx/enclave/LedgerTxHelper.kt b/common/mock-enclave/src/main/kotlin/com/r3/conclave/encryptedtx/enclave/LedgerTxHelper.kt new file mode 100644 index 0000000000..4d3a0232fc --- /dev/null +++ b/common/mock-enclave/src/main/kotlin/com/r3/conclave/encryptedtx/enclave/LedgerTxHelper.kt @@ -0,0 +1,66 @@ +package com.r3.conclave.encryptedtx.enclave + +import com.r3.conclave.encryptedtx.dto.ConclaveLedgerTxModel +import net.corda.core.contracts.CommandWithParties +import net.corda.core.contracts.ComponentGroupEnum +import net.corda.core.contracts.ContractState +import net.corda.core.contracts.StateRef +import net.corda.core.contracts.TransactionResolutionException +import net.corda.core.contracts.TransactionState +import net.corda.core.crypto.SecureHash +import net.corda.core.internal.SerializedStateAndRef +import net.corda.core.serialization.SerializedBytes +import net.corda.core.transactions.LedgerTransaction +import net.corda.core.transactions.SignedTransaction + +class LedgerTxHelper { + companion object { + @JvmStatic + fun toLedgerTxInternal(conclaveLedgerTxModel: ConclaveLedgerTxModel, dependencies: Set): LedgerTransaction { + val wireTransaction = conclaveLedgerTxModel.signedTransaction.tx + val dependencyMap = dependencies.associateBy { it.tx.id } + + val serializedResolvedInputs = conclaveLedgerTxModel.inputStates.map { + resolveStateRefBinaryComponent(it.ref, dependencyMap) ?: throw TransactionResolutionException(it.ref.txhash) + } + + val serializedResolvedReferences = conclaveLedgerTxModel.references.map { + resolveStateRefBinaryComponent(it.ref, dependencyMap) ?: throw TransactionResolutionException(it.ref.txhash) + } + + return LedgerTransaction.createForConclaveVerify( + inputs = conclaveLedgerTxModel.inputStates.toList(), + outputs = wireTransaction.outputs, + commands = wireTransaction.commands.map { + CommandWithParties(it.signers, emptyList(), it.value) + }, + attachments = conclaveLedgerTxModel.attachments.toList(), + id = wireTransaction.id, + notary = wireTransaction.notary, + timeWindow = wireTransaction.timeWindow, + privacySalt = wireTransaction.privacySalt, + networkParameters = conclaveLedgerTxModel.networkParameters, + references = conclaveLedgerTxModel.references.toList(), + componentGroups = wireTransaction.componentGroups, + serializedInputs = serializedResolvedInputs, + serializedReferences = serializedResolvedReferences, + digestService = wireTransaction.digestService + ) + } + + @JvmStatic + fun resolveStateRefBinaryComponent(stateRef: StateRef, dependencyMap: Map) + : SerializedStateAndRef? { + + val wireTransaction = dependencyMap[stateRef.txhash]?.tx + ?: throw TransactionResolutionException(stateRef.txhash) + + val resolvedState = wireTransaction.componentGroups + .firstOrNull { it.groupIndex == ComponentGroupEnum.OUTPUTS_GROUP.ordinal } + ?.components + ?.get(stateRef.index) as SerializedBytes>? + + return resolvedState?.let { SerializedStateAndRef(it, stateRef) } + } + } +} \ No newline at end of file