You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Corda实现三方(A、B、C)多笔交易的示例请求

嘿,完全可以实现三方(A、B、C)之间的多笔交易!Corda从设计上就支持多方参与的交易场景,核心是确保所有相关节点都能参与交易签署和状态同步。我给你整理了一个完整的示例,一步步来:

三方交易实现方案

一、可行性说明

当然可行!Corda的交易模型允许任意数量的参与方,只要交易中的ContractState包含所有参与方的身份,并且所有参与方完成签名,交易就能在各方节点上完成提交和存储。你遇到的论坛讨论没结果可能是没找到针对性的示例,下面的代码可以直接参考落地。

二、核心实现示例

1. 定义三方共享状态

首先需要创建一个包含所有三方的状态类,确保participants字段包含A、B、C,这样每个节点都会将该状态存储到本地账本:

import net.corda.core.contracts.ContractState
import net.corda.core.identity.Party
import net.corda.core.contracts.Amount

data class TriPartyState(
    val partyA: Party,
    val partyB: Party,
    val partyC: Party,
    val amount: Amount<Currency>,
    val transactionDesc: String,
    override val participants: List<Party> = listOf(partyA, partyB, partyC)
) : ContractState

2. 合约验证规则

合约需要确保交易必须获得所有三方的签名,同时可以添加业务规则验证(比如金额合法性):

import net.corda.core.contracts.*
import net.corda.core.transactions.LedgerTransaction

class TriPartyContract : Contract {
    companion object {
        val ID = "com.yourcompany.contracts.TriPartyContract"
    }

    override fun verify(tx: LedgerTransaction) {
        // 校验交易命令
        val createCommand = tx.commandsOfType<Commands.Create>().single()
        
        // 验证所有参与方都已签名
        val requiredSigners = tx.outputsOfType<TriPartyState>().single().participants.map { it.owningKey }.toSet()
        require(createCommand.signers.toSet() == requiredSigners) { "All three parties must sign this transaction" }
        
        // 业务规则:交易金额必须为正
        val transactionAmount = tx.outputsOfType<TriPartyState>().single().amount
        require(transactionAmount.quantity > 0) { "Transaction amount cannot be zero or negative" }
    }

    interface Commands : CommandData {
        class Create : Commands
    }
}

3. 交易流实现

发起方(比如A)需要发起交易,收集B和C的签名,最终完成全节点提交:

import net.corda.core.flows.*
import net.corda.core.transactions.SignedTransaction
import net.corda.core.transactions.TransactionBuilder
import net.corda.core.utilities.ProgressTracker

// 发起方流(由A节点调用)
@InitiatingFlow
@StartableByRPC
class InitiateTriPartyFlow(
    private val partyB: Party,
    private val partyC: Party,
    private val amount: Amount<Currency>,
    private val desc: String
) : FlowLogic<SignedTransaction>() {
    override val progressTracker = ProgressTracker()

    override fun call(): SignedTransaction {
        // 1. 构建交易输出状态
        val outputState = TriPartyState(
            partyA = ourIdentity,
            partyB = partyB,
            partyC = partyC,
            amount = amount,
            transactionDesc = desc
        )

        // 2. 创建交易构建器,指定公证人
        val notary = serviceHub.networkMapCache.notaryIdentities.first()
        val command = Command(TriPartyContract.Commands.Create(), outputState.participants.map { it.owningKey })
        val txBuilder = TransactionBuilder(notary)
            .addOutputState(outputState, TriPartyContract.ID)
            .addCommand(command)

        // 3. 发起方完成初始签名
        val partSignedTx = serviceHub.signInitialTransaction(txBuilder)

        // 4. 与B、C建立会话,收集他们的签名
        val bSession = initiateFlow(partyB)
        val cSession = initiateFlow(partyC)
        val fullySignedTx = subFlow(CollectSignaturesFlow(partSignedTx, listOf(bSession, cSession)))

        // 5. 提交交易到所有参与节点的账本
        return subFlow(FinalityFlow(fullySignedTx, listOf(bSession, cSession)))
    }
}

// 响应方流(B、C节点会自动触发)
@InitiatedBy(InitiateTriPartyFlow::class)
class TriPartyResponderFlow(private val counterpartySession: FlowSession) : FlowLogic<SignedTransaction>() {
    override fun call(): SignedTransaction {
        // 验证交易合法性并签名
        val signFlow = object : SignTransactionFlow(counterpartySession) {
            override fun checkTransaction(stx: SignedTransaction) {
                val state = stx.tx.outputsOfType<TriPartyState>().single()
                // 校验当前节点是交易参与者
                require(state.participants.contains(ourIdentity)) { "This node is not part of the transaction" }
                // 可以添加自定义业务校验,比如确认交易描述符合约定
            }
        }
        val signedTxId = subFlow(signFlow)
        // 接收最终交易并存储到本地账本
        return subFlow(ReceiveFinalityFlow(counterpartySession, signedTxId.id))
    }
}

关键注意点

  • 状态参与者:必须确保ContractState的participants包含所有交易相关方,否则非参与者节点不会存储该状态
  • 签名收集:使用CollectSignaturesFlow确保所有参与方完成签名,避免交易只在部分节点生效
  • 合约验证:通过合约规则强制所有参与方签名,从底层保障交易的合法性

内容的提问来源于stack exchange,提问作者scala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:21:46