Tappd SDK
This SDK provides a JavaScript/TypeScript client for communicating with the Tappd server, which available inside DStack.
Installation
npm install @phala/dstack-sdk
Basic Usage
import { TappdClient } from '@phala/dstack-sdk';
const client = new TappdClient();
const httpClient = new TappdClient('http://localhost:8000');
await client.info();
const keyResult = await client.deriveKey('<unique-id>');
console.log(keyResult.key);
console.log(keyResult.certificate_chain);
const keyBytes = keyResult.asUint8Array();
const quoteResult = await client.tdxQuote('some-data', 'sha256');
console.log(quoteResult.quote);
console.log(quoteResult.event_log);
const rtmrs = quoteResult.replayRtmrs();
For tdxQuote
, it supports a range of hash algorithms, including:
sha256
: SHA-256 hash algorithmsha384
: SHA-384 hash algorithmsha512
: SHA-512 hash algorithmsha3-256
: SHA3-256 hash algorithmsha3-384
: SHA3-384 hash algorithmsha3-512
: SHA3-512 hash algorithmkeccak256
: Keccak-256 hash algorithmkeccak384
: Keccak-384 hash algorithmkeccak512
: Keccak-512 hash algorithmraw
: No hashing, use raw data (must be <= 64 bytes)
Viem Integration
The SDK provides integration with viem for Ethereum account management:
import { toViemAccount } from 'tappd-sdk/viem';
const keyResult = await client.deriveKey('<unique-id>');
const account = toViemAccount(keyResult);
Solana Integration
The SDK provides integration with Solana Web3.js for Solana account management:
import { toKeypair } from 'tappd-sdk/solana';
const keyResult = await client.deriveKey('<unique-id>');
const keypair = toKeypair(keyResult);
Environment Variables Encryption
The SDK includes utilities for encrypting environment variables using X25519 key exchange and AES-GCM. This feature is handy for interacting with the bare DStack Teepod API or the Phala Cloud API.
import { encryptEnvVars, type EnvVar } from 'tappd-sdk/encrypt-env-vars';
const envVars: EnvVar[] = [
{ key: 'API_KEY', value: 'secret123' },
{ key: 'DATABASE_URL', value: 'postgresql://...' }
];
const publicKeyHex = '0x...';
const encrypted = await encryptEnvVars(envVars, publicKeyHex);
API Reference
TappdClient
Constructor
new TappdClient(endpoint?: string)
endpoint
: Unix socket path or HTTP(S) URL. Defaults to '/var/run/tappd.sock'.- Uses
DSTACK_SIMULATOR_ENDPOINT
environment variable if set
NOTE: Leave it empty in production. You only need to add volumes
in your docker-compose file:
volumes:
- /var/run/tappd.sock:/var/run/tappd.sock
For local development without TDX devices, you can use the simulator available for download here:
https://github.com/Leechael/tappd-simulator/releases
Methods
deriveKey(path?: string, subject?: string, alt_names?: string[]): Promise<DeriveKeyResponse>
Derives a key for the given path and subject.
NOTE: Only the path affects the derived result. subject
& alt_names
are for the generated certificate and do not affect the derived result.
path
: Optional path for key derivationsubject
: Optional subject name (defaults to path)alt_names
: Optional alternative names for the certificate- Returns:
DeriveKeyResponse
containing key and certificate chain
tdxQuote(report_data: string | Buffer | Uint8Array, hash_algorithm?: TdxQuoteHashAlgorithms): Promise<TdxQuoteResponse>
Generates a TDX quote. The quote is returned in hex format, and you can paste your quote into https://proof.t16z.com/ to get the attestation report.
report_data
: Data to include in the quotehash_algorithm
: Hash algorithm to use (sha256, sha384, sha512, etc.)- Returns:
TdxQuoteResponse
containing quote and event log
info(): Promise<TappdInfoResponse>
Retrieves server information.
- Returns: Information about the Tappd instance
Types
interface DeriveKeyResponse {
key: string;
certificate_chain: string[];
asUint8Array: (max_length?: number) => Uint8Array;
}
type TdxQuoteHashAlgorithms =
'sha256' | 'sha384' | 'sha512' | 'sha3-256' | 'sha3-384' | 'sha3-512' |
'keccak256' | 'keccak384' | 'keccak512' | 'raw';
interface TdxQuoteResponse {
quote: Hex;
event_log: string;
replayRtmrs: () => string[];
}
interface TappdInfoResponse {
app_id: string;
instance_id: string;
app_cert: string;
tcb_info: string;
app_name: string;
public_logs: boolean;
public_sysinfo: boolean;
}
interface EnvVar {
key: string;
value: string;
}
License
Apache License