|
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' |
4 | 5 |
|
5 | 6 | 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 | + }) |
14 | 105 | }) |
15 | 106 | }) |
0 commit comments