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.

npx create-next-app --example with-docker nextjs-docker

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.

npm install @phala/dstack-sdk viem dotenv

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.

ENV DSTACK_SIMULATOR_ENDPOINT="http://host.docker.internal:8090"

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.

touch ./pages/api/derivekey.js
touch ./pages/api/tdxquote.js

You should now have 2 empty files in the directory, and run ls ./pages/api to verify the files were created.

ls pages/api 
total 24
drwxr-xr-x  5 hashwarlock  staff   160B Nov  7 14:51 .
drwxr-xr-x  5 hashwarlock  staff   160B Nov  7 14:47 ..
-rw-r--r--@ 1 hashwarlock  staff   892B Nov  7 15:01 derivekey.js
-rw-r--r--  1 hashwarlock  staff   169B Nov  7 14:00 hello.js
-rw-r--r--@ 1 hashwarlock  staff   604B Nov  7 15:05 tdxquote.js

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.

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import {TappdClient} from '@phala/dstack-sdk'
import { privateKeyToAccount } from 'viem/accounts'
import {keccak256} from "viem";
import 'dotenv/config'

export const dynamic = 'force-dynamic'

const endpoint = process.env.DSTACK_SIMULATOR_ENDPOINT || 'http://localhost:8090'

export default async function derivekey(req, res) {
    // Get the Tappd client
    const client = new TappdClient(endpoint)
    const randomNumString = Math.random().toString();
    // Call the deriveKey function and pass in the root of trust to derive a key
    const randomDeriveKey = await client.deriveKey('/', randomNumString);
    // Hash the derivedKey uint8Array value
    const keccakPrivateKey = keccak256(randomDeriveKey.asUint8Array());
    // Get the private key account from the derived key hash
    const account = privateKeyToAccount(keccakPrivateKey);
    // Return derived key pair
    res.status(200).json({account: account.address, privateKey: keccakPrivateKey});
}

Let's move to implementing the api/tdxquote API call with the following snippet of code.

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import {TappdClient} from '@phala/dstack-sdk'
import 'dotenv/config'

export const dynamic = 'force-dynamic'

const endpoint = process.env.DSTACK_SIMULATOR_ENDPOINT || 'http://localhost:8090'

export default async function tdxquote(req, res) {
    // Get Tappd client
    const client = new TappdClient(endpoint)
    const randomNumString = Math.random().toString();
    // Generate Remote Attestation Quote based on a random string of data
    const getRemoteAttestation = await client.tdxQuote(randomNumString);
    // Return Remote Attestation result
    res.status(200).json({ getRemoteAttestation });
}

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.

docker build -t my-nextjs-app:latest .

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.

docker pull phalanetwork/tappd-simulator:latest
docker run --rm -p 8090:8090 phalanetwork/tappd-simulator:latest

With the simulator up and running, we can now start our docker app to test out the functionality.

docker run --rm -p 3000:3000 my-nextjs-app:latest

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

Logo

COPYRIGHT © 2024 PHALA.LTD ALL RIGHTS RESERVED. May Phala be with you!