Start From Scratch
This tutorial will step you through setting up a project from scratch with Dstack SDK. The following have a couple ways to get started with
Create a next.js application written in JavaScript
Build a Python backend application
Before You Start
Make sure you have Docker Desktop or OrbStack started before you begin.
Installation
For this example, we will be starting from scratch and will need to generate a new project. For simplicity, I will use a template for a next.js docker app.
This should create a new project called nextjs-docker
in the current working directory. Now let's add the @phala/dstack-sdk
to the project along with viem
and dotenv
to get our project compatible with the TEE simulator.
Make Calls to Dstack SDK Functions
Now that we have the packages installed, we can begin to configure our existing project to make calls to the deriveKey(path, data)
and tdxQuote(data)
functions in the Dstack SDK.
Edit Dockerfile
First lets make 1 small change to the Dockerfile
and add the line to line 66 in the file.
Next, lets create the API calls for both of the functions.
Create API Calls
Create 2 new files called derivekey.js
tdxquote.js
in the pages/api/
folder.
You should now have 2 empty files in the directory, and run ls ./pages/api
to verify the files were created.
Now that the API calls are created, let's implement the api/derivekey
function first. We can copy and paste the following code snippet to generate a random ECDSA Keypair.
Let's move to implementing the api/tdxquote
API call with the following snippet of code.
Now that these API calls are done, we can start with the fun of testing this locally.
Testing Locally
With the basic implementation done, let's do a quick test to see if the code works by building the docker image and testing locally.
Now that the docker image is built, let's start testing against the TEE Simulator. First we must pull the latest TEE Simulator in docker, and run it with the following commands.
With the simulator up and running, we can now start our docker app to test out the functionality.
Now we can go to http://localhost:3000
and see our deployed application.
Call the APIs
We can test to see if our functions work by calling the API calls with
http://localhost:3000/api/derivekey
http://localhost:3000/api/tdxquote
We should see the results similar to the following screenshots.
Conclusion
Now you have the skills to start building on the Dstack SDK from scratch. Checkout the other tutorials where we start from a template or integrate the SDK into an existing project. For more info on how to setup your project to be deployed on the real TEE hardware, reach out to the Phala Team for more information.
Last updated