线性状态的Corda转移,收集签名

时间:2019-05-05 19:31:13

标签: corda

我已经实现了几个命令,其中公证签名可以满足用例的要求。尽管有2个问题,但我有未解决的问题:

  • 我有一个从输入/输出状态更改的所有者字段,一切都很好,但是我希望新所有者在其上注销,例如我需要一种机制来通过保管库检索状态(最后未消费?),我想我需要对参与者进行身份验证和过滤,并让新所有者对其进行签名,例如“按下按钮”,我该怎么做?

  • 如何确定线性状态,例如以后不能再参与任何形式的交易/命令了?

@InitiatingFlow
@StartableByRPC
class DeviceFlow(private val deviceState: DeviceState,
                 private val stateAndRef: Optional<StateAndRef<DeviceState>>,
                 private val command: DeviceContract.Commands)
    : FlowLogic<SignedTransaction>() {
    override val progressTracker = tracker()

    companion object {
        object GENERATING_TRANSACTION : Step("Generating transaction based on new IOU.")
        object VERIFYING_TRANSACTION : Step("Verifying contract constraints.")
        object SIGNING_TRANSACTION : Step("Signing transaction with our private key.")
        object GATHERING_SIGS : Step("Gathering the counterparty's signature.") {
            override fun childProgressTracker() = CollectSignaturesFlow.tracker()
        }

        object FINALISING_TRANSACTION : Step("Obtaining notary signature and recording transaction.") {
            override fun childProgressTracker() = FinalityFlow.tracker()
        }

        fun tracker() = ProgressTracker(
                GENERATING_TRANSACTION,
                VERIFYING_TRANSACTION,
                SIGNING_TRANSACTION,
                GATHERING_SIGS,
                FINALISING_TRANSACTION
        )
    }

    @Suspendable
    override fun call(): SignedTransaction {
        progressTracker.currentStep = GENERATING_TRANSACTION
        val notary = serviceHub.networkMapCache.getNotary(CordaX500Name("Notary", "London", "GB"))
        val transactionBuilder = TransactionBuilder(notary = notary)
                .addCommand(command, deviceState.participants.map { it.owningKey })
                .addOutputState(deviceState, DeviceContract.ID)
        if (stateAndRef.isPresent)
            transactionBuilder.addInputState(stateAndRef.get())
        progressTracker.currentStep = VERIFYING_TRANSACTION
        transactionBuilder.verify(serviceHub)
        progressTracker.currentStep = SIGNING_TRANSACTION
        val signedTransaction = serviceHub.signInitialTransaction(transactionBuilder)
        val otherPartySession = initiateFlow(notary!!)
        val fullySignedTx = subFlow(
                CollectSignaturesFlow(signedTransaction, setOf(otherPartySession), GATHERING_SIGS.childProgressTracker()))
        progressTracker.currentStep = FINALISING_TRANSACTION
        return subFlow(FinalityFlow(fullySignedTx, setOf(otherPartySession), FINALISING_TRANSACTION.childProgressTracker()))
    }
}

@InitiatedBy(DeviceFlow::class)
class DeviceCreateFlowResponder(val flowSession: FlowSession) : FlowLogic<SignedTransaction>() {
    @Suspendable
    override fun call(): SignedTransaction {
        val signTransactionFlow = object : SignTransactionFlow(flowSession) {
            override fun checkTransaction(stx: SignedTransaction) = requireThat {
                val output = stx.tx.outputs.single().data
                "This must be a Device transaction." using (output is DeviceState)
            }
        }
        val txId = subFlow(signTransactionFlow).id
        return subFlow(ReceiveFinalityFlow(flowSession, expectedTxId = txId))
    }
}

1 个答案:

答案 0 :(得分:0)

新所有者退出

  • 您将从库中检索状态的最新版本。最新版本应该未使用。
  • 创建一个TransactionBuilder并将此状态添加为输入。
  • 创建此状态的副本,但更改owner属性以反映新所有者。将此作为输出添加到TransactionBuilder。
  • 确保您管理国家的合同规则强制要求以前的所有者和新所有者都是必需的签署人。

最终确定线性(或任何状态)

  • 您可以向州添加属性,并让您的合同强制要求一旦设置了该属性,州就不能再发展。
  • 您可以通过将状态作为输入添加到事务中而不添加输出来消耗状态。 (同样,您的联系人必须允许这样做)