corda/docs/source/writing-cordapps.rst

141 lines
5.9 KiB
ReStructuredText
Raw Normal View History

2017-06-05 12:37:23 +00:00
Writing a CorDapp
=================
When writing a CorDapp, you are writing a set of files in a JVM language that defines one or more of the following
Corda components:
2017-06-05 12:37:23 +00:00
* States (i.e. classes implementing ``ContractState``)
* Contracts (i.e. classes implementing ``Contract``)
* Flows (i.e. classes extending ``FlowLogic``)
* Web APIs
* Services
CorDapp structure
-----------------
Your CorDapp project's structure should be based on the structure of the
`Java Template CorDapp <https://github.com/corda/cordapp-template-java>`_ or the
`Kotlin Template CorDapp <https://github.com/corda/cordapp-template-kotlin>`_, depending on which language you intend
to use.
2017-06-05 12:37:23 +00:00
The ``src`` directory of the Template CorDapp, where we define our CorDapp's source-code, has the following structure:
2017-06-05 12:37:23 +00:00
.. parsed-literal::
src
├── main
│ ├── java
│ │ └── com
│ │ └── template
│ │ ├── Main.java
│ │ ├── api
│ │ │ └── TemplateApi.java
│ │ ├── client
│ │ │ └── TemplateClientRPC.java
│ │ ├── contract
│ │ │ └── TemplateContract.java
│ │ ├── flow
│ │ │ └── TemplateFlow.java
│ │ ├── plugin
│ │ │ └── TemplatePlugin.java
│ │ ├── service
│ │ │ └── TemplateService.java
│ │ └── state
│ │ └── TemplateState.java
│ └── resources
│ ├── META-INF
│ │ └── services
│ │ ├── net.corda.core.node.CordaPluginRegistry
│ │ └── net.corda.webserver.services.WebServerPluginRegistry
2017-06-05 12:37:23 +00:00
│ ├── certificates
│ │ ├── sslkeystore.jks
│ │ └── truststore.jks
│ └──templateWeb
│ ├── index.html
│ └── js
│ └── template-js.js
└── test
└── java
└── com
└── template
└── contract
└── TemplateTests.java
The build file
--------------
At the root of the Template CorDapp, you will also find a ``build.gradle`` file. This file is useful for several
reasons:
2017-06-05 12:37:23 +00:00
Choosing your CorDapp version
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The following two lines of the ``build.gradle`` file define the Corda version used to build your CorDapp:
.. sourcecode:: groovy
ext.corda_release_version = '0.13.0'
ext.corda_gradle_plugins_version = '0.13.3'
In this case, our CorDapp will use the Milestone 13 release of Corda, and version 13.3 of the Corda gradle plugins. You
can find the latest published version of both here: https://bintray.com/r3/corda.
2017-06-05 12:37:23 +00:00
``corda_gradle_plugins_versions`` are given in the form ``major.minor.patch``. You should use the same ``major`` and
``minor`` versions as the Corda version you are using, and the latest ``patch`` version. A list of all the available
versions can be found here: https://bintray.com/r3/corda/cordformation.
2017-06-05 12:37:23 +00:00
In certain cases, you may also wish to build against the unstable Master branch. See :doc:`building-against-master`.
2017-06-05 12:37:23 +00:00
Project dependencies
^^^^^^^^^^^^^^^^^^^^
If your CorDapps have any additional external dependencies, they should be added to the ``dependencies`` section:
2017-06-05 12:37:23 +00:00
.. sourcecode:: groovy
2017-06-05 12:37:23 +00:00
dependencies {
2017-06-05 12:37:23 +00:00
...
2017-06-05 12:37:23 +00:00
// Cordapp dependencies
// Specify your cordapp's dependencies below, including dependent cordapps
}
2017-06-05 12:37:23 +00:00
For further information about managing dependencies, see
`the Gradle docs <https://docs.gradle.org/current/userguide/dependency_management.html>`_.
2017-06-05 12:37:23 +00:00
Build tasks
^^^^^^^^^^^
The build file also defines a number of build tasks that will allow us to package up our plugin. We will discuss these
later.
2017-06-05 12:37:23 +00:00
Defining plugins
----------------
Your CorDapp may need to define two types of plugins:
2017-06-05 12:37:23 +00:00
* ``CordaPluginRegistry`` subclasses, which define additional serializable classes and vault schemas
* ``WebServerPluginRegistry`` subclasses, which define the APIs and static web content served by your CorDapp
2017-06-05 12:37:23 +00:00
The fully-qualified class path of each ``CordaPluginRegistry`` subclass must then be added to the
``net.corda.core.node.CordaPluginRegistry`` file in the CorDapp's ``resources/META-INF/services`` folder. Meanwhile,
the fully-qualified class path of each ``WebServerPluginRegistry`` subclass must be added to the
``net.corda.webserver.services.WebServerPluginRegistry`` file, again in the CorDapp's ``resources/META-INF/services``
folder.
2017-06-05 12:37:23 +00:00
The ``CordaPluginRegistry`` class defines the following:
2017-06-05 12:37:23 +00:00
* ``customizeSerialization``, which can be overridden to provide a list of the classes to be whitelisted for object
serialisation, over and above those tagged with the ``@CordaSerializable`` annotation. See :doc:`serialization`
2017-06-05 12:37:23 +00:00
* ``requiredSchemas``, which can be overridden to return a set of the MappedSchemas to use for persistence and vault
queries
2017-06-05 12:37:23 +00:00
The ``WebServerPluginRegistry`` class defines the following:
* ``webApis``, which can be overridden to return a list of JAX-RS annotated REST access classes. These classes will be
constructed by the bundled web server and must have a single argument constructor taking a ``CordaRPCOps`` object.
This will allow the API to communicate with the node process via the RPC interface. These web APIs will not be
available if the bundled web server is not started
* ``staticServeDirs``, which can be overridden to map static web content to virtual paths and allow simple web demos to
be distributed within the CorDapp jars. This static content will not be available if the bundled web server is not
started
* The static web content itself should be placed inside the ``src/main/resources`` directory