Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
445 changes: 445 additions & 0 deletions packages/blockchains/aztec/src/__tests__/client.test.ts

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions packages/blockchains/aztec/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { vi } from 'vitest'
import type { TrainApiClient } from '@train-protocol/sdk'

export const SAMPLE_ADDRESS = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
export const SAMPLE_LP_ADDRESS = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd'
export const SAMPLE_CONTRACT = '0x9999999999999999999999999999999999999999999999999999999999999999'
export const SAMPLE_TOKEN = '0x5555555555555555555555555555555555555555555555555555555555555555'
export const SAMPLE_HASHLOCK = '0x' + 'aa'.repeat(32)
export const SAMPLE_TX_HASH = '0x' + 'bb'.repeat(32)

export function createMockWallet(): Record<string, ReturnType<typeof vi.fn>> {
return {
getAccounts: vi.fn().mockResolvedValue([{ item: SAMPLE_ADDRESS }]),
registerContract: vi.fn().mockResolvedValue(undefined),
registerSender: vi.fn().mockResolvedValue(undefined),
}
}

export function createMockSigner(): { wallet: Record<string, ReturnType<typeof vi.fn>>; address: string } {
return {
wallet: createMockWallet(),
address: SAMPLE_ADDRESS,
}
}

export function createMockApiClient(): TrainApiClient {
return {
revealSecret: vi.fn().mockResolvedValue(undefined),
} as unknown as TrainApiClient
}
44 changes: 44 additions & 0 deletions packages/blockchains/aztec/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, it, expect } from 'vitest'
import { stringToBytes } from '../utils.js'

describe('stringToBytes', () => {
it('converts ASCII string to byte array of specified length', () => {
const result = stringToBytes('abc', 5)
expect(result).toHaveLength(5)
// 'abc' is 3 bytes, left-padded with 2 spaces
expect(result[0]).toBe(32) // space
expect(result[1]).toBe(32) // space
expect(result[2]).toBe(97) // 'a'
expect(result[3]).toBe(98) // 'b'
expect(result[4]).toBe(99) // 'c'
})

it('left-pads with spaces when string is shorter', () => {
const result = stringToBytes('x', 4)
expect(result).toHaveLength(4)
// 3 spaces + 'x'
expect(result[0]).toBe(32)
expect(result[1]).toBe(32)
expect(result[2]).toBe(32)
expect(result[3]).toBe(120) // 'x'
})

it('handles exact length string', () => {
const result = stringToBytes('hi', 2)
expect(result).toHaveLength(2)
expect(result[0]).toBe(104) // 'h'
expect(result[1]).toBe(105) // 'i'
})

it('truncates when encoded bytes exceed length', () => {
const result = stringToBytes('hello world', 5)
expect(result).toHaveLength(5)
})

it('handles empty string', () => {
const result = stringToBytes('', 3)
expect(result).toHaveLength(3)
// All spaces
expect(result.every(b => b === 32)).toBe(true)
})
})
51 changes: 51 additions & 0 deletions packages/blockchains/aztec/src/__tests__/wallet-sign.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'

vi.mock('@train-protocol/sdk', () => ({
deriveKeyMaterial: vi.fn().mockReturnValue(new Uint8Array(32).fill(0xab)),
IDENTITY_SALT: 'train-identity-salt',
}))

vi.mock('@aztec/aztec.js/fields', () => ({
Fr: {
fromBuffer: vi.fn().mockReturnValue({ toBuffer: () => Buffer.alloc(32) }),
},
}))

vi.mock('@aztec/aztec.js/addresses', () => ({
AztecAddress: {
fromString: vi.fn((s: string) => ({ toString: () => s })),
},
}))

import { deriveKeyFromAztecWallet, type AztecWalletLike } from '../login/wallet-sign.js'
import { deriveKeyMaterial } from '@train-protocol/sdk'

function createMockWallet(overrides?: Partial<AztecWalletLike>): AztecWalletLike {
return {
createAuthWit: vi.fn().mockResolvedValue({
toBuffer: () => Buffer.from('mockwitness'),
}),
...overrides,
}
}

describe('deriveKeyFromAztecWallet', () => {
beforeEach(() => {
vi.resetAllMocks()
;(deriveKeyMaterial as any).mockReturnValue(new Uint8Array(32).fill(0xab))
})

it('returns Buffer from deriveKeyMaterial', async () => {
const wallet = createMockWallet()
const result = await deriveKeyFromAztecWallet(wallet, '0xaddr')

expect(Buffer.isBuffer(result)).toBe(true)
expect(deriveKeyMaterial).toHaveBeenCalled()
})

it('throws when wallet is falsy', async () => {
await expect(
deriveKeyFromAztecWallet(null as any, '0xaddr')
).rejects.toThrow('Aztec wallet not connected')
})
})
Loading