Writing the sample CorDapp¶
Corda is R3's open source enterprise blockchain platform. You can think of it as a peer to peer database in which user-written apps can be installed. Enclaves can be used inside "CorDapps" to provide multi-party computation over the peer to peer network. Or look at it the other way around: Corda can provide communication and identity services to your enclave.
The sample CorDapp builds on the hello-world sample allowing nodes to perform the reverse string task between two nodes, one of which loads the enclave. Smart contracts and identity aren't used in this sample, only flows are needed.
The sample divides the code into several pieces. The packages that don't have
samples in the name are
intended to be copy/pasted into your own code. They'll be turned into a full API in future. The code
expects a DCAP capable host, but if you want to use EPID you can edit the enclave startup code to make it use your
Intel API keys.
Configure your workflow CorDapp module¶
Both Conclave and Corda rely on Gradle build system plugins. Follow the instructions in the hello world tutorial to configure Gradle to include an enclave mode. Add the host libraries to your Corda workflows module:
1 2 3 4 5 6 7 8 9 10 11 12
You must use Gradle's
compile configurations for the host dependencies. If you use
implementation then you
will get errors about missing classes when the node starts up, due to issues with fat JARing. The host
section should look like above.
Write an enclave host service¶
The enclave will be loaded into a service object. This singleton will be available to flows for later usage, and lets
us integrate with the lifecycle of the node. The sample project contains a helper class called
you can copy into your project and sub-class. At the moment it isn't a complete or supported API so you may need to
edit it to make it work for your use case.
1 2 3 4 5 6
This will make a best effort to enable SGX support on the host if necessary, then it loads the sample
class (which is exactly the same as in the hello world tutorial). The
EnclaveHostService class exposes methods to
send and receive mail with the enclave, in a way that lets flows suspend waiting for the enclave to deliver mail.
Relaying mail from a flow to the enclave¶
In this tutorial, reversing a string involves two parties: one is the initiator that sends the secret string to reverse, the other is the responder that reverses the string inside the enclave.
To implement this we will need a flow used by clients, and a 'responder' flow used by the host node.
Here's the responder flow to get the host service, get the enclave attestation and send it to the other party for verification, get the other party's encrypted mail with the string to reverse, deliver it to the enclave, and retrieve the enclave encrypted mail to send to the other party for decryption.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
This flow starts by sending the remote attestation. Typically, an interaction with the enclave should start this way, although the attestation can be cached if you wish to add such logic.
We then implement a simple request/response type protocol by receiving a byte array and then using the
deliverAndPickUpMail method of the sample's
EnclaveHostService class. This returns an operation that can be passed
await. The flow will suspend (thus freeing up its thread), potentially for a long period. There is no requirement
that the enclave reply immediately. It can return from processing the delivered mail without replying. When it does
choose to reply, the flow will be re-awakened and the encrypted mail returned to the other side.
Although the Corda flow framework has built in support for it, this sample code does not handle node restarts.
And here's the initiator code. It makes use of a helper class you can find in the sample called
Again, you can copy this into your own projects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
DO NOT BLINDLY COPY THE
SEC:INSECURE PART INTO PRODUCTION CODE. IT DOES NOT TAKE SHERLOCK HOLMES TO DEDUCE THAT YOUR APP WILL BE
INSECURE IF YOU KEEP THAT BIT.
We start in the usual manner for a Corda flow. Once we've established a session with the hosting node, we instantiate
the helper class giving it the session and a constraint. Then we call
start to receive the
check it against the constraint. If no exception is thrown we can then use
The unit tests are completely normal for Corda. However, as the code above will load a real or simulated Linux enclave,
they won't run on Windows or macOS. You can change the
EnclaveHostService class to use the mock loading API to fix this
or on macOS, you can use the
container-gradle script to run the tests inside a Linux VM.