gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:32:32 24 4
gpt4 key购买 nike

目前我们计划有一个“草稿”版本的合约,不会发送给交易对手,发起人可以在发送到网络之前进行任何更改,因此这应该是一个“未共享的事实”。正如我们所知 Corda 和 vault 用于共享事实,所以在这里我不确定我们是否仍然可以使用 vault 来存储这种类型的“非共享事实”,我的想法如下,我已经可以在我的本地工作了在教程 CorDapp 上,但希望从其他 Corda 团队/专家那里获得一些意见。

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

  • 仅使用发起者的 key 发起创建命令
  • 不要调用“CollectSignaturesFlow”,这样就不会将其发送给任何其他人
  • 在 verify() 之后调用“FinalityFlow”,因此这将被提交到账本

下面是上述几点的代码。

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 列表中。只需将您自己添加到参与者列表中,并且只将您的公钥添加到命令中。这是一个简单的例子:

//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”这个名字不合适:)

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

关于corda - 如何在 Corda 中定义一个未共享的事实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45078817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com