Skip to content

Commit f23fdcc

Browse files
authored
Merge pull request #256 from Dstack-TEE/sdk-compat
jssdk: Add back TappdClient for compatibility
2 parents e722486 + 0cf4432 commit f23fdcc

File tree

7 files changed

+412
-45
lines changed

7 files changed

+412
-45
lines changed

sdk/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@phala/dstack-sdk",
3-
"version": "0.5.0",
3+
"version": "0.5.2",
44
"description": "Dstack SDK",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

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

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { expect, describe, it, vi } from 'vitest'
2-
import { DstackClient } from '../index'
2+
import { DstackClient, TappdClient } from '../index'
33

44
describe('DstackClient', () => {
5-
it('should able to derive key', async () => {
5+
it('should able to derive key in TappdClient', async () => {
6+
const client = new TappdClient()
7+
const result = await client.deriveKey('/', 'test')
8+
expect(result).toHaveProperty('key')
9+
expect(result).toHaveProperty('certificate_chain')
10+
})
11+
12+
it('should throws error in DstackClient', async () => {
13+
const client = new DstackClient()
14+
await expect(() => client.deriveKey('/', 'test')).rejects.toThrow('deriveKey is deprecated, please use getKey instead.')
15+
})
16+
17+
it('should able to get key', async () => {
618
const client = new DstackClient()
719
const result = await client.getKey('/', 'test')
820
expect(result).toHaveProperty('key')
@@ -139,21 +151,21 @@ describe('DstackClient', () => {
139151
expect(typeof isReachable).toBe('boolean')
140152
})
141153

142-
describe('deprecated methods', () => {
154+
describe('deprecated methods with TappdClient', () => {
143155
it('should support deprecated deriveKey method with warning', async () => {
144-
const client = new DstackClient()
156+
const client = new TappdClient()
145157
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
146158

147159
const result = await client.deriveKey('/', 'test')
148160
expect(result).toHaveProperty('key')
149-
expect(result).toHaveProperty('signature_chain')
161+
expect(result).toHaveProperty('certificate_chain')
150162
expect(consoleSpy).toHaveBeenCalledWith('deriveKey is deprecated, please use getKey instead')
151163

152164
consoleSpy.mockRestore()
153165
})
154166

155167
it('should support deprecated tdxQuote method with warning', async () => {
156-
const client = new DstackClient()
168+
const client = new TappdClient()
157169
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
158170

159171
const result = await client.tdxQuote('test data')
@@ -165,7 +177,7 @@ describe('DstackClient', () => {
165177
})
166178

167179
it('should support tdxQuote with hash algorithm parameter', async () => {
168-
const client = new DstackClient()
180+
const client = new TappdClient()
169181
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
170182

171183
const result = await client.tdxQuote('test data', 'sha256')
@@ -176,4 +188,54 @@ describe('DstackClient', () => {
176188
consoleSpy.mockRestore()
177189
})
178190
})
191+
192+
describe('deprecated methods with DstackClient', () => {
193+
it('should throws error in deriveKey method', async () => {
194+
const client = new DstackClient()
195+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
196+
197+
await expect(() => client.deriveKey('/', 'test')).rejects.toThrow('deriveKey is deprecated, please use getKey instead.')
198+
199+
consoleSpy.mockRestore()
200+
})
201+
202+
it('should throws error in tdxQuote method without hash algorithm parameter', async () => {
203+
const client = new DstackClient()
204+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
205+
206+
await expect(() => client.tdxQuote('test data')).rejects.toThrow('tdxQuote only supports raw hash algorithm.')
207+
208+
consoleSpy.mockRestore()
209+
})
210+
211+
it("should throws error in tdxQuote method with hash algorithm parameter other than raw", async () => {
212+
const client = new DstackClient()
213+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
214+
215+
await expect(() => client.tdxQuote('test data', 'sha256')).rejects.toThrow('tdxQuote only supports raw hash algorithm.')
216+
217+
consoleSpy.mockRestore()
218+
})
219+
220+
it('should able to get quote with plain report_data in tdxQuote method with warning', async () => {
221+
const client = new DstackClient()
222+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
223+
224+
const result = await client.tdxQuote('test data', "raw")
225+
expect(result).toHaveProperty('quote')
226+
expect(result).toHaveProperty('event_log')
227+
expect(consoleSpy).toHaveBeenCalledWith('tdxQuote is deprecated, please use getQuote instead')
228+
229+
consoleSpy.mockRestore()
230+
})
231+
232+
it('should throws error in tdxQuote with hash algorithm parameter', async () => {
233+
const client = new DstackClient()
234+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
235+
236+
await expect(() => client.tdxQuote('test data', 'sha256')).rejects.toThrow('tdxQuote only supports raw hash algorithm.')
237+
238+
consoleSpy.mockRestore()
239+
})
240+
})
179241
})
Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,96 @@
1-
import { expect, describe, it } from 'vitest'
1+
import crypto from 'crypto'
2+
import { expect, describe, it, vi } from 'vitest'
23
import { Keypair } from '@solana/web3.js'
34

4-
import { DstackClient } from '../index'
5-
import { toKeypair } from '../solana'
5+
import { DstackClient, TappdClient } from '../index'
6+
import { toKeypair, toKeypairSecure } from '../solana'
67

78
describe('solana support', () => {
8-
it('should able to get keypair from deriveKey', async () => {
9-
const client = new DstackClient()
10-
const result = await client.getKey('/', 'test')
11-
const keypair = toKeypair(result)
12-
expect(keypair).toBeInstanceOf(Keypair)
13-
console.log(keypair.publicKey.toBase58())
9+
describe('toKeypair (legacy)', () => {
10+
it('should able to get keypair from getKey with DstackClient', async () => {
11+
const client = new DstackClient()
12+
const result = await client.getKey('/', 'test')
13+
const keypair = toKeypair(result)
14+
expect(keypair).toBeInstanceOf(Keypair)
15+
expect(keypair.secretKey.length).toBe(64)
16+
})
17+
18+
it('should able to get keypair from deriveKey with TappdClient', async () => {
19+
const client = new TappdClient()
20+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
21+
22+
const result = await client.deriveKey('/', 'test')
23+
const keypair = toKeypair(result)
24+
expect(keypair).toBeInstanceOf(Keypair)
25+
expect(keypair.secretKey.length).toBe(64)
26+
expect(consoleSpy).toHaveBeenCalledWith('toKeypair: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
27+
28+
consoleSpy.mockRestore()
29+
})
30+
31+
it('should able to get keypair from getTlsKey with DstackClient', async () => {
32+
const client = new DstackClient()
33+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
34+
35+
const result = await client.getTlsKey()
36+
const keypair = toKeypair(result)
37+
expect(keypair).toBeInstanceOf(Keypair)
38+
expect(keypair.secretKey.length).toBe(64)
39+
expect(consoleSpy).toHaveBeenCalledWith('toKeypair: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
40+
41+
consoleSpy.mockRestore()
42+
})
43+
})
44+
45+
describe('toKeypairSecure', () => {
46+
it('should able to get keypair from getKey with DstackClient', async () => {
47+
const client = new DstackClient()
48+
const result = await client.getKey('/', 'test')
49+
const keypair = toKeypairSecure(result)
50+
expect(keypair).toBeInstanceOf(Keypair)
51+
expect(keypair.secretKey.length).toBe(64)
52+
})
53+
54+
it('should able to get keypair from deriveKey with TappdClient', async () => {
55+
const client = new TappdClient()
56+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
57+
58+
const result = await client.deriveKey('/', 'test')
59+
const keypair = toKeypairSecure(result)
60+
expect(keypair).toBeInstanceOf(Keypair)
61+
expect(keypair.secretKey.length).toBe(64)
62+
expect(consoleSpy).toHaveBeenCalledWith('toKeypairSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
63+
64+
consoleSpy.mockRestore()
65+
})
66+
67+
it('should able to get keypair from getTlsKey with DstackClient', async () => {
68+
const client = new DstackClient()
69+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
70+
71+
const result = await client.getTlsKey()
72+
const keypair = toKeypairSecure(result)
73+
expect(keypair).toBeInstanceOf(Keypair)
74+
expect(keypair.secretKey.length).toBe(64)
75+
expect(consoleSpy).toHaveBeenCalledWith('toKeypairSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
76+
77+
consoleSpy.mockRestore()
78+
})
79+
80+
it('should throw error when sha256 is not supported', async () => {
81+
const client = new DstackClient()
82+
const result = await client.getTlsKey()
83+
84+
// Mock crypto.createHash to simulate missing sha256 support
85+
const originalCreateHash = crypto.createHash
86+
crypto.createHash = () => {
87+
throw new Error('sha256 not supported')
88+
}
89+
90+
expect(() => toKeypairSecure(result)).toThrow('toKeypairSecure: missing sha256 support')
91+
92+
// Restore original createHash
93+
crypto.createHash = originalCreateHash
94+
})
1495
})
1596
})

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

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,106 @@
1-
import { expect, describe, it } from 'vitest'
2-
import { DstackClient } from '../index'
3-
import { toViemAccount } from '../viem'
1+
import crypto from 'crypto'
2+
import { expect, describe, it, vi } from 'vitest'
3+
import { DstackClient, TappdClient } from '../index'
4+
import { toViemAccount, toViemAccountSecure } from '../viem'
45

56
describe('viem support', () => {
6-
it('should able to get account from getKey', async () => {
7-
const client = new DstackClient()
8-
const result = await client.getKey('/', 'test')
9-
const account = toViemAccount(result)
10-
11-
expect(account.source).toBe('privateKey')
12-
expect(typeof account.sign).toBe('function')
13-
expect(typeof account.signMessage).toBe('function')
7+
describe('toViemAccount (legacy)', () => {
8+
it('should able to get account from getKey with DstackClient', async () => {
9+
const client = new DstackClient()
10+
const result = await client.getKey('/', 'test')
11+
const account = toViemAccount(result)
12+
13+
expect(account.source).toBe('privateKey')
14+
expect(typeof account.sign).toBe('function')
15+
expect(typeof account.signMessage).toBe('function')
16+
})
17+
18+
it('should able to get account from deriveKey with TappdClient', async () => {
19+
const client = new TappdClient()
20+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
21+
22+
const result = await client.deriveKey('/', 'test')
23+
const account = toViemAccount(result)
24+
25+
expect(account.source).toBe('privateKey')
26+
expect(typeof account.sign).toBe('function')
27+
expect(typeof account.signMessage).toBe('function')
28+
expect(consoleSpy).toHaveBeenCalledWith('toViemAccount: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
29+
30+
consoleSpy.mockRestore()
31+
})
32+
33+
it('should able to get account from getTlsKey with DstackClient', async () => {
34+
const client = new DstackClient()
35+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
36+
37+
const result = await client.getTlsKey()
38+
const account = toViemAccount(result)
39+
40+
expect(account.source).toBe('privateKey')
41+
expect(typeof account.sign).toBe('function')
42+
expect(typeof account.signMessage).toBe('function')
43+
expect(consoleSpy).toHaveBeenCalledWith('toViemAccount: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
44+
45+
consoleSpy.mockRestore()
46+
})
47+
})
48+
49+
describe('toViemAccountSecure', () => {
50+
it('should able to get account from getKey with DstackClient', async () => {
51+
const client = new DstackClient()
52+
const result = await client.getKey('/', 'test')
53+
const account = toViemAccountSecure(result)
54+
55+
expect(account.source).toBe('privateKey')
56+
expect(typeof account.sign).toBe('function')
57+
expect(typeof account.signMessage).toBe('function')
58+
})
59+
60+
it('should able to get account from deriveKey with TappdClient', async () => {
61+
const client = new TappdClient()
62+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
63+
64+
const result = await client.deriveKey('/', 'test')
65+
const account = toViemAccountSecure(result)
66+
67+
expect(account.source).toBe('privateKey')
68+
expect(typeof account.sign).toBe('function')
69+
expect(typeof account.signMessage).toBe('function')
70+
expect(consoleSpy).toHaveBeenCalledWith('toViemAccountSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
71+
72+
consoleSpy.mockRestore()
73+
})
74+
75+
it('should able to get account from getTlsKey with DstackClient', async () => {
76+
const client = new DstackClient()
77+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
78+
79+
const result = await client.getTlsKey()
80+
const account = toViemAccountSecure(result)
81+
82+
expect(account.source).toBe('privateKey')
83+
expect(typeof account.sign).toBe('function')
84+
expect(typeof account.signMessage).toBe('function')
85+
expect(consoleSpy).toHaveBeenCalledWith('toViemAccountSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
86+
87+
consoleSpy.mockRestore()
88+
})
89+
90+
it('should throw error when sha256 is not supported', async () => {
91+
const client = new DstackClient()
92+
const result = await client.getTlsKey()
93+
94+
// Mock crypto.createHash to simulate missing sha256 support
95+
const originalCreateHash = crypto.createHash
96+
crypto.createHash = () => {
97+
throw new Error('sha256 not supported')
98+
}
99+
100+
expect(() => toViemAccountSecure(result)).toThrow('toViemAccountSecure: missing sha256 support')
101+
102+
// Restore original createHash
103+
crypto.createHash = originalCreateHash
104+
})
14105
})
15106
})

0 commit comments

Comments
 (0)