Skip to content

Commit 13d2594

Browse files
committed
jssdk: Add compatible deriveKey
1 parent 1ad75c0 commit 13d2594

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

sdk/js/src/__tests__/index.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { DstackClient } from '../index'
33

44
describe('DstackClient', () => {
55
it('should able to derive key', async () => {
6+
const client = new DstackClient()
7+
const result = await client.deriveKey('/', 'test')
8+
expect(result).toHaveProperty('key')
9+
expect(result).toHaveProperty('certificate_chain')
10+
})
11+
12+
it('should able to get key', async () => {
613
const client = new DstackClient()
714
const result = await client.getKey('/', 'test')
815
expect(result).toHaveProperty('key')
@@ -141,7 +148,7 @@ describe('DstackClient', () => {
141148

142149
const result = await client.deriveKey('/', 'test')
143150
expect(result).toHaveProperty('key')
144-
expect(result).toHaveProperty('signature_chain')
151+
expect(result).toHaveProperty('certificate_chain')
145152
expect(consoleSpy).toHaveBeenCalledWith('deriveKey is deprecated, please use getKey instead')
146153

147154
consoleSpy.mockRestore()

sdk/js/src/index.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,23 @@ export interface TlsKeyOptions {
126126

127127
export class DstackClient {
128128
private endpoint: string
129+
private tappdEndpoint: string
129130

130131
constructor(endpoint: string = '/var/run/dstack.sock') {
132+
var tappdEndpoint = '/var/run/tappd.sock'
131133
if (process.env.DSTACK_SIMULATOR_ENDPOINT) {
132134
console.warn(`Using simulator endpoint: ${process.env.DSTACK_SIMULATOR_ENDPOINT}`)
133135
endpoint = process.env.DSTACK_SIMULATOR_ENDPOINT
134136
}
137+
if (process.env.TAPPD_SIMULATOR_ENDPOINT) {
138+
console.warn(`Using tappd endpoint: ${process.env.TAPPD_SIMULATOR_ENDPOINT}`)
139+
tappdEndpoint = process.env.TAPPD_SIMULATOR_ENDPOINT
140+
}
135141
if (endpoint.startsWith('/') && !fs.existsSync(endpoint)) {
136142
throw new Error(`Unix socket file ${endpoint} does not exist`);
137143
}
138144
this.endpoint = endpoint
145+
this.tappdEndpoint = tappdEndpoint
139146
}
140147

141148
async getKey(path: string, purpose: string = ''): Promise<GetKeyResponse> {
@@ -251,8 +258,20 @@ export class DstackClient {
251258
* @param altNames The alternative names of the key.
252259
* @returns The key.
253260
*/
254-
async deriveKey(path?: string, subject?: string, altNames?: string[]): Promise<GetKeyResponse> {
255-
throw new Error('deriveKey is deprecated, please use getKey instead.')
261+
async deriveKey(path?: string, subject?: string, alt_names?: string[]): Promise<GetTlsKeyResponse> {
262+
console.warn('deriveKey is deprecated, please use getKey instead');
263+
let raw: Record<string, any> = { path: path || '', subject: subject || path || '' }
264+
if (alt_names && alt_names.length) {
265+
raw['alt_names'] = alt_names
266+
}
267+
const payload = JSON.stringify(raw)
268+
const result = await send_rpc_request<GetTlsKeyResponse>(this.tappdEndpoint, '/prpc/Tappd.DeriveKey', payload)
269+
Object.defineProperty(result, 'asUint8Array', {
270+
get: () => (length?: number) => x509key_to_uint8array(result.key, length),
271+
enumerable: true,
272+
configurable: false,
273+
})
274+
return Object.freeze(result)
256275
}
257276

258277
/**
@@ -262,11 +281,28 @@ export class DstackClient {
262281
* @returns The quote.
263282
*/
264283
async tdxQuote(report_data: string | Buffer | Uint8Array, hash_algorithm?: TdxQuoteHashAlgorithms): Promise<GetQuoteResponse> {
265-
console.warn('tdxQuote is deprecated, please use getQuote instead')
266-
if (hash_algorithm !== "raw") {
267-
throw new Error('tdxQuote only supports raw hash algorithm.')
284+
console.warn('tdxQuote is deprecated, please use getQuote instead');
285+
let hex = to_hex(report_data)
286+
if (hash_algorithm === 'raw') {
287+
if (hex.length > 128) {
288+
throw new Error(`Report data is too large, it should less then 64 bytes when hash_algorithm is raw.`)
289+
}
290+
if (hex.length < 128) {
291+
hex = hex.padStart(128, '0')
292+
}
268293
}
269-
return this.getQuote(report_data)
294+
const payload = JSON.stringify({ report_data: hex, hash_algorithm })
295+
const result = await send_rpc_request<GetQuoteResponse>(this.tappdEndpoint, '/prpc/Tappd.TdxQuote', payload)
296+
if ('error' in result) {
297+
const err = result['error'] as string
298+
throw new Error(err)
299+
}
300+
Object.defineProperty(result, 'replayRtmrs', {
301+
get: () => () => reply_rtmrs(JSON.parse(result.event_log) as EventLog[]),
302+
enumerable: true,
303+
configurable: false,
304+
})
305+
return Object.freeze(result)
270306
}
271307
}
272308

0 commit comments

Comments
 (0)