如何在 Corda 中定义一个未共享的事实

How to define one unshared fact in Corda

目前我们计划有一个 "Draft" 版本的合约,不会发送给交易对手,发起者可以在发送到网络之前进行任何更改,所以这应该是一个 "unshared fact"。正如我们所知 Corda 和 vault 用于共享事实,所以在这里我不确定我们是否仍然可以使用 vault 来存储这种类型的 "unshared fact",我的想法如下,我已经可以在我的本地工作基于教程 CorDapp,但想从其他 Corda team/experts.

获得一些输入

主要变化在启动器流程中:

下面是上述几点的代码。

override fun call(): SignedTransaction  {
    // We create a transaction builder
    val txBuilder = TransactionBuilder()
    val notaryIdentity = serviceHub.networkMapCache.getAnyNotary()
    txBuilder.notary = notaryIdentity

    // We create the transaction's components.
    val ourIdentity = serviceHub.myInfo.legalIdentity
    val iou = TemplateState(iouValue, ourIdentity, ourIdentity)
    val txCommand = Command(TemplateContract.Create(), listOf(ourIdentity.owningKey))

    // Adding the item's to the builder.
    txBuilder.withItems(iou, txCommand)

    // Verifying the transaction.
    txBuilder.toWireTransaction().toLedgerTransaction(serviceHub).verify()

    // Signing the transaction.
    val partSignedTx = serviceHub.signInitialTransaction(txBuilder)

    // Finalising the transaction.
    return subFlow(FinalityFlow(partSignedTx)).single()
}

你确实可以在 Corda 中创建一个不共享的事实!这里的关键在州的 participants 列表中。只需将您自己添加到参与者列表中,并且只用您的 public 键输入命令。这是一个简单的例子:

//Contract and State.
class UnsharedFact: Contract {
    override val legalContractReference: SecureHash = SecureHash.zeroHash
    override fun verify(tx: TransactionForContract) = Unit // Stubbed out.
    class Create: CommandData

    data class State(val parties: Set<Party>): ContractState {
        override val contract: Contract get() = UnsharedFact()
        override val participants: List<AbstractParty> get() = parties.toList()
        fun addParty(newParty: Party) = copy(parties = parties + newParty)
    }
}

// Create flow.
@InitiatingFlow
@StartableByRPC
class CreateUnsharedFact(): FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {
        val me = serviceHub.myInfo.legalIdentity
        val notary = serviceHub.networkMapCache.getAnyNotary()
        val state = UnsharedFact.State(setOf(me))
        val command = Command(UnsharedFact.Create(), listOf(me.owningKey))
        val utx = TransactionBuilder(notary = notary).withItems(state, command)
        val stx = serviceHub.signInitialTransaction(utx)
        return subFlow(FinalityFlow(stx)).single()
    }
}

调用FinalityFlow时,您将是唯一接收输出状态的节点。

如果您希望随后让另一方参与进来,那么您可以使用 UnsharedFact.State 上的 addParty 方法创建一个新版本的状态。然后,创建一个新交易,将原始状态添加为输入,将新版本(与新方)添加为输出。当此交易完成(公证)后,双方将在各自的保险库中拥有一份副本。现在,我猜名字 'UnsharedFact' 不合适 :)

您也可以使用类似的方法删除派对。