Corda:如何在网络上显示双方之间的协议?

时间:2018-02-21 10:23:02

标签: sharing corda

Alice和Bob有一个IOU(我欠你)的协议。如果她想证明她真的与鲍勃达成协议,爱丽丝是否有可能与鲍勃向查理展示IOU协议? Charlie如何验证Alice和Bob之间的IOU是真实的而不是假的?这个用例的流程是什么?

1 个答案:

答案 0 :(得分:2)

Alice可以向Charlie证明她与Bob签订了有效协议,将创建协议的交易发送给Charlie。

然后查理将检查交易是否有效,它是否拥有所有必需的签名者,以及是否由Alice和Bob签署。

以下流量对将实现此目的:

/**
 * Sends a transaction with the given [txHash] to the [counterparty]:
 *
 * @param txHash the transaction to send.
 * @param counterparty the counterparty to send the transaction to.
 */
@InitiatingFlow
@StartableByRPC
class Initiator(val txHash: SecureHash,
                val counterparty: Party) : FlowLogic<Unit>() {

    @Suspendable
    override fun call() {
        val tx = serviceHub.validatedTransactions.getTransaction(txHash)!!
        val counterpartySession = initiateFlow(counterparty)
        counterpartySession.send(tx)
    }
}

/**
 * Receives a transaction from the counterparty and checks that:
 *  - It is valid
 *  - It has valid signatures from all required signers
 *  - The [requiredSigners] have all signed the transaction
 *
 * @param counterpartySession a session with the party sending the transaction to be checked.
 * @param requiredSigners the parties who are required to have signed the transaction.
 */
@InitiatedBy(Initiator::class)
class Acceptor(val counterpartySession: FlowSession,
               val requiredSigners: List<CordaX500Name>) : FlowLogic<SignedTransaction>() {

    @Suspendable
    override fun call(): SignedTransaction {
        return counterpartySession.receive<SignedTransaction>().unwrap { tx ->
            // Transaction is valid and all the required signatures are present and valid.
            tx.verify(serviceHub)

            val requiredSigningParties = requiredSigners.map { serviceHub.networkMapCache.getPeerByLegalName(it)!! }
            val allHaveSigned = requiredSigningParties.all { party -> tx.sigs.any { sig -> sig.by == party.owningKey } }

            if (!allHaveSigned) {
                throw FlowException("The required parties have not signed the transaction.")
            }

            // TODO: Additional checking of the transaction (e.g. the properties of the inputs and outputs).

            tx
        }
    }
}