Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/vault-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,33 @@ function decryptFromEnclave(
export type VaultClient = {
baseUrl: string;
publicKey: Uint8Array;
headers: Record<string, string>;
};

export async function createVaultClient({
baseUrl,
}: {
baseUrl: string;
export async function createVaultClient(clientOptions?: {
baseUrl?: string;
secretKey?: string;
}): Promise<VaultClient> {
const baseUrl = clientOptions?.baseUrl ?? "https://engine.thirdweb.com";
// Construct the full URL for the fetch call
const url = new URL("api/v1/enclave", baseUrl).toString();

type IntrospectionResponse = {
publicKey: string;
};

const headers = {
// Indicate we accept JSON responses
Accept: "application/json",
...(clientOptions?.secretKey
? { "x-secret-key": clientOptions?.secretKey }
: {}),
};

try {
const response = await fetch(url, {
method: "GET",
headers: {
// Indicate we accept JSON responses
Accept: "application/json",
},
headers,
});

// fetch doesn't throw on HTTP errors (like 4xx, 5xx) by default.
Expand All @@ -149,7 +155,8 @@ export async function createVaultClient({
const publicKeyBytes = hexToBytes(data.publicKey);

return {
baseUrl: baseUrl, // Store baseUrl
baseUrl, // Store baseUrl
headers,
publicKey: publicKeyBytes,
};
} catch (error) {
Expand Down Expand Up @@ -180,6 +187,7 @@ async function sendRequest<P extends Payload>({
const response = await fetch(url, {
method: "POST",
headers: {
...client.headers,
"Content-Type": "application/json",
Accept: "application/json", // Good practice to specify accept header
},
Expand Down
Loading