2017-07-17 11:23:14 +00:00
|
|
|
.. highlight:: kotlin
|
|
|
|
.. raw:: html
|
|
|
|
|
|
|
|
<script type="text/javascript" src="_static/jquery.js"></script>
|
|
|
|
<script type="text/javascript" src="_static/codesets.js"></script>
|
|
|
|
|
|
|
|
Updating the flow
|
|
|
|
=================
|
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
We now need to update our flow to achieve three things:
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
* Verifying that the transaction proposal we build fulfills the ``IOUContract`` constraints
|
|
|
|
* Updating the lender's side of the flow to request the borrower's signature
|
|
|
|
* Creating a response flow for the borrower that responds to the signature request from the lender
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
We'll do this by modifying the flow we wrote in the previous tutorial.
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
Verifying the transaction
|
|
|
|
-------------------------
|
2018-11-08 10:19:03 +00:00
|
|
|
In ``IOUFlow.java``/``Flows.kt``, change the imports block to the following:
|
2017-07-17 11:23:14 +00:00
|
|
|
|
|
|
|
.. container:: codeset
|
|
|
|
|
2018-09-24 14:00:31 +00:00
|
|
|
.. literalinclude:: example-code/src/main/kotlin/net/corda/docs/kotlin/tutorial/twoparty/IOUFlow.kt
|
2017-10-16 13:39:28 +00:00
|
|
|
:language: kotlin
|
|
|
|
:start-after: DOCSTART 01
|
|
|
|
:end-before: DOCEND 01
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-10-16 13:39:28 +00:00
|
|
|
.. literalinclude:: example-code/src/main/java/net/corda/docs/java/tutorial/twoparty/IOUFlow.java
|
|
|
|
:language: java
|
|
|
|
:start-after: DOCSTART 01
|
|
|
|
:end-before: DOCEND 01
|
2017-08-17 11:02:44 +00:00
|
|
|
|
2018-11-13 11:50:24 +00:00
|
|
|
And update ``IOUFlow.call`` to the following:
|
2017-10-02 08:08:59 +00:00
|
|
|
|
2017-10-16 13:39:28 +00:00
|
|
|
.. container:: codeset
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2018-09-24 14:00:31 +00:00
|
|
|
.. literalinclude:: example-code/src/main/kotlin/net/corda/docs/kotlin/tutorial/twoparty/IOUFlow.kt
|
2017-10-16 13:39:28 +00:00
|
|
|
:language: kotlin
|
|
|
|
:start-after: DOCSTART 02
|
|
|
|
:end-before: DOCEND 02
|
|
|
|
:dedent: 8
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-10-16 13:39:28 +00:00
|
|
|
.. literalinclude:: example-code/src/main/java/net/corda/docs/java/tutorial/twoparty/IOUFlow.java
|
|
|
|
:language: java
|
|
|
|
:start-after: DOCSTART 02
|
|
|
|
:end-before: DOCEND 02
|
|
|
|
:dedent: 8
|
2017-08-17 11:02:44 +00:00
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
In the original CorDapp, we automated the process of notarising a transaction and recording it in every party's vault
|
|
|
|
by invoking a built-in flow called ``FinalityFlow`` as a subflow. We're going to use another pre-defined flow,
|
|
|
|
``CollectSignaturesFlow``, to gather the borrower's signature.
|
|
|
|
|
2018-05-14 11:09:53 +00:00
|
|
|
First, we need to update the command. We are now using ``IOUContract.Create``, rather than
|
2017-11-16 15:31:52 +00:00
|
|
|
``TemplateContract.Commands.Action``. We also want to make the borrower a required signer, as per the contract
|
|
|
|
constraints. This is as simple as adding the borrower's public key to the transaction's command.
|
|
|
|
|
|
|
|
We also need to add the output state to the transaction using a reference to the ``IOUContract``, instead of to the old
|
|
|
|
``TemplateContract``.
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
Now that our state is governed by a real contract, we'll want to check that our transaction proposal satisfies these
|
|
|
|
requirements before kicking off the signing process. We do this by calling ``TransactionBuilder.verify`` on our
|
|
|
|
transaction proposal before finalising it by adding our signature.
|
|
|
|
|
|
|
|
Requesting the borrower's signature
|
|
|
|
-----------------------------------
|
|
|
|
|
2018-11-14 14:16:22 +00:00
|
|
|
Previously we wrote a responder flow for the borrower in order to receive the finalised transaction from the lender.
|
|
|
|
We use this same flow to first request their signature over the transaction.
|
2017-10-02 08:08:59 +00:00
|
|
|
|
2018-11-14 14:16:22 +00:00
|
|
|
We gather the borrower's signature using ``CollectSignaturesFlow``, which takes:
|
2017-10-02 08:08:59 +00:00
|
|
|
|
|
|
|
* A transaction signed by the flow initiator
|
|
|
|
* A list of flow-sessions between the flow initiator and the required signers
|
|
|
|
|
|
|
|
And returns a transaction signed by all the required signers.
|
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
We can then pass this fully-signed transaction into ``FinalityFlow``.
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2018-11-14 14:16:22 +00:00
|
|
|
Updating the borrower's flow
|
2017-08-17 11:02:44 +00:00
|
|
|
----------------------------
|
2018-11-14 14:16:22 +00:00
|
|
|
On the lender's side, we used ``CollectSignaturesFlow`` to automate the collection of signatures. To allow the borrower
|
|
|
|
to respond, we need to update its responder flow to first receive the partially signed transaction for signing. Update
|
|
|
|
``IOUFlowResponder.call`` to be the following:
|
2017-07-17 11:23:14 +00:00
|
|
|
|
|
|
|
.. container:: codeset
|
|
|
|
|
2018-09-24 14:00:31 +00:00
|
|
|
.. literalinclude:: example-code/src/main/kotlin/net/corda/docs/kotlin/tutorial/twoparty/IOUFlowResponder.kt
|
2017-10-16 13:39:28 +00:00
|
|
|
:language: kotlin
|
2019-04-16 10:06:05 +00:00
|
|
|
:start-after: DOCSTART 1
|
|
|
|
:end-before: DOCEND 1
|
|
|
|
:dedent: 4
|
2017-10-16 13:39:28 +00:00
|
|
|
|
|
|
|
.. literalinclude:: example-code/src/main/java/net/corda/docs/java/tutorial/twoparty/IOUFlowResponder.java
|
|
|
|
:language: java
|
2019-04-16 10:06:05 +00:00
|
|
|
:start-after: DOCSTART 1
|
|
|
|
:end-before: DOCEND 1
|
|
|
|
:dedent: 4
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
We could write our own flow to handle this process. However, there is also a pre-defined flow called
|
|
|
|
``SignTransactionFlow`` that can handle the process automatically. The only catch is that ``SignTransactionFlow`` is an
|
|
|
|
abstract class - we must subclass it and override ``SignTransactionFlow.checkTransaction``.
|
2017-07-17 11:23:14 +00:00
|
|
|
|
|
|
|
CheckTransactions
|
2017-08-17 11:02:44 +00:00
|
|
|
^^^^^^^^^^^^^^^^^
|
2017-07-17 11:23:14 +00:00
|
|
|
``SignTransactionFlow`` will automatically verify the transaction and its signatures before signing it. However, just
|
2017-11-16 15:31:52 +00:00
|
|
|
because a transaction is contractually valid doesn't mean we necessarily want to sign. What if we don't want to deal
|
|
|
|
with the counterparty in question, or the value is too high, or we're not happy with the transaction's structure?
|
2017-07-17 11:23:14 +00:00
|
|
|
|
|
|
|
Overriding ``SignTransactionFlow.checkTransaction`` allows us to define these additional checks. In our case, we are
|
|
|
|
checking that:
|
|
|
|
|
|
|
|
* The transaction involves an ``IOUState`` - this ensures that ``IOUContract`` will be run to verify the transaction
|
|
|
|
* The IOU's value is less than some amount (100 in this case)
|
|
|
|
|
|
|
|
If either of these conditions are not met, we will not sign the transaction - even if the transaction and its
|
2017-11-16 15:31:52 +00:00
|
|
|
signatures are contractually valid.
|
|
|
|
|
|
|
|
Once we've defined the ``SignTransactionFlow`` subclass, we invoke it using ``FlowLogic.subFlow``, and the
|
|
|
|
communication with the borrower's and the lender's flow is conducted automatically.
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2018-11-14 14:16:22 +00:00
|
|
|
``SignedTransactionFlow`` returns the newly signed transaction. We pass in the transaction's ID to ``ReceiveFinalityFlow``
|
|
|
|
to ensure we are recording the correct notarised transaction from the lender.
|
|
|
|
|
2017-07-17 11:23:14 +00:00
|
|
|
Conclusion
|
|
|
|
----------
|
2017-11-16 15:31:52 +00:00
|
|
|
We have now updated our flow to verify the transaction and gather the lender's signature, in line with the constraints
|
|
|
|
defined in ``IOUContract``. We can now re-run our updated CorDapp, using the
|
|
|
|
:doc:`same instructions as before <hello-world-running>`.
|
|
|
|
|
|
|
|
Our CorDapp now imposes restrictions on the issuance of IOUs. Most importantly, IOU issuance now requires agreement
|
2018-11-13 11:50:24 +00:00
|
|
|
from both the lender and the borrower before an IOU can be created on the blockchain. This prevents either the lender or
|
2017-11-16 15:31:52 +00:00
|
|
|
the borrower from unilaterally updating the ledger in a way that only benefits themselves.
|
|
|
|
|
2017-12-13 16:22:40 +00:00
|
|
|
After completing this tutorial, your CorDapp should look like this:
|
|
|
|
|
|
|
|
* Java: https://github.com/corda/corda-tut2-solution-java
|
|
|
|
* Kotlin: https://github.com/corda/corda-tut2-solution-kotlin
|
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
You should now be ready to develop your own CorDapps. You can also find a list of sample CorDapps
|
2019-08-23 12:08:53 +00:00
|
|
|
`here <https://www.corda.net/samples/>`_. You are now ready to learn more about the Corda API.
|
2017-07-17 11:23:14 +00:00
|
|
|
|
2017-11-16 15:31:52 +00:00
|
|
|
If you get stuck at any point, please reach out on `Slack <https://slack.corda.net/>`_ or
|
2018-05-14 11:09:53 +00:00
|
|
|
`Stack Overflow <https://stackoverflow.com/questions/tagged/corda>`_.
|