Skip to content

Commit fa8104d

Browse files
committed
fix(sdk/js): add SPDX header to tsup.config, fix EOF newlines, remove invalid sha256 mock tests
The sha256 tests mocked crypto.createHash but the implementation uses @noble/hashes (pure JS), so the mock had no effect and the tests always failed.
1 parent 8cd9f32 commit fa8104d

5 files changed

Lines changed: 31 additions & 59 deletions

File tree

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

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import crypto from 'crypto'
5+
66
import { expect, describe, it, vi } from 'vitest'
77
import { Keypair } from '@solana/web3.js'
88

@@ -22,26 +22,26 @@ describe('solana support', () => {
2222
it('should able to get keypair from deriveKey with TappdClient', async () => {
2323
const client = new TappdClient()
2424
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
25-
25+
2626
const result = await client.deriveKey('/', 'test')
2727
const keypair = toKeypair(result)
2828
expect(keypair).toBeInstanceOf(Keypair)
2929
expect(keypair.secretKey.length).toBe(64)
3030
expect(consoleSpy).toHaveBeenCalledWith('toKeypair: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
31-
31+
3232
consoleSpy.mockRestore()
3333
})
3434

3535
it('should able to get keypair from getTlsKey with DstackClient', async () => {
3636
const client = new DstackClient()
3737
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
38-
38+
3939
const result = await client.getTlsKey()
4040
const keypair = toKeypair(result)
4141
expect(keypair).toBeInstanceOf(Keypair)
4242
expect(keypair.secretKey.length).toBe(64)
4343
expect(consoleSpy).toHaveBeenCalledWith('toKeypair: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
44-
44+
4545
consoleSpy.mockRestore()
4646
})
4747
})
@@ -58,43 +58,27 @@ describe('solana support', () => {
5858
it('should able to get keypair from deriveKey with TappdClient', async () => {
5959
const client = new TappdClient()
6060
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
61-
61+
6262
const result = await client.deriveKey('/', 'test')
6363
const keypair = toKeypairSecure(result)
6464
expect(keypair).toBeInstanceOf(Keypair)
6565
expect(keypair.secretKey.length).toBe(64)
6666
expect(consoleSpy).toHaveBeenCalledWith('toKeypairSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
67-
67+
6868
consoleSpy.mockRestore()
6969
})
7070

7171
it('should able to get keypair from getTlsKey with DstackClient', async () => {
7272
const client = new DstackClient()
7373
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
74-
74+
7575
const result = await client.getTlsKey()
7676
const keypair = toKeypairSecure(result)
7777
expect(keypair).toBeInstanceOf(Keypair)
7878
expect(keypair.secretKey.length).toBe(64)
7979
expect(consoleSpy).toHaveBeenCalledWith('toKeypairSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
80-
81-
consoleSpy.mockRestore()
82-
})
8380

84-
it('should throw error when sha256 is not supported', async () => {
85-
const client = new DstackClient()
86-
const result = await client.getTlsKey()
87-
88-
// Mock crypto.createHash to simulate missing sha256 support
89-
const originalCreateHash = crypto.createHash
90-
crypto.createHash = () => {
91-
throw new Error('sha256 not supported')
92-
}
93-
94-
expect(() => toKeypairSecure(result)).toThrow('toKeypairSecure: missing sha256 support')
95-
96-
// Restore original createHash
97-
crypto.createHash = originalCreateHash
81+
consoleSpy.mockRestore()
9882
})
9983
})
100-
})
84+
})

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

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import crypto from 'crypto'
5+
66
import { expect, describe, it, vi } from 'vitest'
77
import { DstackClient, TappdClient } from '../index'
88
import { toViemAccount, toViemAccountSecure } from '../viem'
@@ -13,7 +13,7 @@ describe('viem support', () => {
1313
const client = new DstackClient()
1414
const result = await client.getKey('/', 'test')
1515
const account = toViemAccount(result)
16-
16+
1717
expect(account.source).toBe('privateKey')
1818
expect(typeof account.sign).toBe('function')
1919
expect(typeof account.signMessage).toBe('function')
@@ -22,30 +22,30 @@ describe('viem support', () => {
2222
it('should able to get account from deriveKey with TappdClient', async () => {
2323
const client = new TappdClient()
2424
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
25-
25+
2626
const result = await client.deriveKey('/', 'test')
2727
const account = toViemAccount(result)
28-
28+
2929
expect(account.source).toBe('privateKey')
3030
expect(typeof account.sign).toBe('function')
3131
expect(typeof account.signMessage).toBe('function')
3232
expect(consoleSpy).toHaveBeenCalledWith('toViemAccount: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
33-
33+
3434
consoleSpy.mockRestore()
3535
})
3636

3737
it('should able to get account from getTlsKey with DstackClient', async () => {
3838
const client = new DstackClient()
3939
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
40-
40+
4141
const result = await client.getTlsKey()
4242
const account = toViemAccount(result)
43-
43+
4444
expect(account.source).toBe('privateKey')
4545
expect(typeof account.sign).toBe('function')
4646
expect(typeof account.signMessage).toBe('function')
4747
expect(consoleSpy).toHaveBeenCalledWith('toViemAccount: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
48-
48+
4949
consoleSpy.mockRestore()
5050
})
5151
})
@@ -55,7 +55,7 @@ describe('viem support', () => {
5555
const client = new DstackClient()
5656
const result = await client.getKey('/', 'test')
5757
const account = toViemAccountSecure(result)
58-
58+
5959
expect(account.source).toBe('privateKey')
6060
expect(typeof account.sign).toBe('function')
6161
expect(typeof account.signMessage).toBe('function')
@@ -64,47 +64,31 @@ describe('viem support', () => {
6464
it('should able to get account from deriveKey with TappdClient', async () => {
6565
const client = new TappdClient()
6666
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
67-
67+
6868
const result = await client.deriveKey('/', 'test')
6969
const account = toViemAccountSecure(result)
70-
70+
7171
expect(account.source).toBe('privateKey')
7272
expect(typeof account.sign).toBe('function')
7373
expect(typeof account.signMessage).toBe('function')
7474
expect(consoleSpy).toHaveBeenCalledWith('toViemAccountSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
75-
75+
7676
consoleSpy.mockRestore()
7777
})
7878

7979
it('should able to get account from getTlsKey with DstackClient', async () => {
8080
const client = new DstackClient()
8181
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
82-
82+
8383
const result = await client.getTlsKey()
8484
const account = toViemAccountSecure(result)
85-
85+
8686
expect(account.source).toBe('privateKey')
8787
expect(typeof account.sign).toBe('function')
8888
expect(typeof account.signMessage).toBe('function')
8989
expect(consoleSpy).toHaveBeenCalledWith('toViemAccountSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
90-
91-
consoleSpy.mockRestore()
92-
})
9390

94-
it('should throw error when sha256 is not supported', async () => {
95-
const client = new DstackClient()
96-
const result = await client.getTlsKey()
97-
98-
// Mock crypto.createHash to simulate missing sha256 support
99-
const originalCreateHash = crypto.createHash
100-
crypto.createHash = () => {
101-
throw new Error('sha256 not supported')
102-
}
103-
104-
expect(() => toViemAccountSecure(result)).toThrow('toViemAccountSecure: missing sha256 support')
105-
106-
// Restore original createHash
107-
crypto.createHash = originalCreateHash
91+
consoleSpy.mockRestore()
10892
})
10993
})
11094
})

sdk/js/src/solana.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export function toKeypairSecure(keyResponse: GetTlsKeyResponse | GetKeyResponse)
3333
return Keypair.fromSeed(buf)
3434
}
3535
return Keypair.fromSeed(keyResponse.key)
36-
}
36+
}

sdk/js/src/viem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ export function toViemAccountSecure(keyResponse: GetKeyResponse | GetTlsKeyRespo
3535
}
3636
const hex = Array.from(keyResponse.key).map(b => b.toString(16).padStart(2, '0')).join('')
3737
return privateKeyToAccount(`0x${hex}`)
38-
}
38+
}

sdk/js/tsup.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// SPDX-FileCopyrightText: © 2025 Phala Network <dstack@phala.network>
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
15
import { defineConfig } from "tsup"
26

37
export default defineConfig({

0 commit comments

Comments
 (0)