Skip to content

Write a long lived client

JohnJoser3 edited this page Jan 12, 2023 · 2 revisions

Writing a long-lived client

Prerequisites

Complete the following tutorials.

Introduction

Each run is independent in the hello-world tutorial and the writing your first enclave tutorial. In such cases, the client doesn't need to maintain any state between runs.

However, in most real-world applications, the client and the enclave interact over an extended period. To support such applications, the client has to maintain its state and resume it successfully after a restart.

Add state to your client

EnclaveClient provides a save method to serialize the state to a byte array. You can call the EnclaveClient constructor with this byte array to restore the client.

enclaveClient = new EnclaveClient(Files.readAllBytes(stateFile));

// If a constraint was provided to the client, ensure that it matches the
// constraint loaded from the file.
EnclaveConstraint loadedConstraint = enclaveClient.getEnclaveConstraint();
if (providedConstraint != null && loadedConstraint != providedConstraint) {
    throw new IllegalArgumentException(
            "Constraint provided to client and constraint loaded from "
                    + stateFile.getFileName() + " are not the same."
    );
}
    
// Code where the client interacts with the enclave.

Files.write(stateFile, enclaveClient.save())

!!!Warning

If you are using a file to store the bytes, you must store it securely as it contains the client's private key.