Skip to content
Open
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
32 changes: 32 additions & 0 deletions src/context/WalletContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const apiMocks = vi.hoisted(() => ({
vi.mock('@/lib/api', () => apiMocks)

import { WalletProvider, useWalletContext, WALLET_POLL_INTERVAL_MS } from './WalletContext'
import { getNetworkPassphrase } from '@/lib/stellar'

const PUBLIC_KEY_A = 'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5'
const PUBLIC_KEY_B = 'GDRREYWHQWJDICNH4SAH4TT2JPVYWIX6JEWAHE2W6BZDJBIJ4VSX227Z'
Expand Down Expand Up @@ -155,3 +156,34 @@ describe('Freighter account-drift polling', () => {
expect(result.current.wallet.publicKey).toBe(PUBLIC_KEY_A)
}, WALLET_POLL_INTERVAL_MS + 5_000)
})

describe('signTransaction', () => {
it('calls Freighter signTransaction with SDK getNetworkPassphrase for testnet and mainnet', async () => {
const { result } = await connectWallet(PUBLIC_KEY_A)
freighterMocks.signTransaction.mockResolvedValue('signed-xdr-output')

// Test on testnet
let signed = await act(async () => {
return await result.current.signTransaction('dummy-xdr-testnet')
})
expect(signed).toBe('signed-xdr-output')
expect(freighterMocks.signTransaction).toHaveBeenCalledWith('dummy-xdr-testnet', {
networkPassphrase: getNetworkPassphrase('testnet'),
accountToSign: PUBLIC_KEY_A,
})

// Switch to mainnet
act(() => {
result.current.setNetwork('mainnet')
})

signed = await act(async () => {
return await result.current.signTransaction('dummy-xdr-mainnet')
})
expect(signed).toBe('signed-xdr-output')
expect(freighterMocks.signTransaction).toHaveBeenCalledWith('dummy-xdr-mainnet', {
networkPassphrase: getNetworkPassphrase('mainnet'),
accountToSign: PUBLIC_KEY_A,
})
})
})
6 changes: 2 additions & 4 deletions src/context/WalletContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import type { Network, WalletState, AccountInfo } from '@/types'
import { DEFAULT_SETTINGS } from '@/types'
import { fetchAccountFromHorizon } from '@/lib/api'
import { getNetworkPassphrase } from '@/lib/stellar'

// Freighter's own popup lets the user switch accounts entirely outside this
// app. Poll for that drift on an interval distinct from (and shorter than)
Expand Down Expand Up @@ -246,10 +247,7 @@ export function WalletProvider({ children }: { children: React.ReactNode }) {
// Dynamic import to avoid SSR issues
const { signTransaction: freighterSign } = await import('@stellar/freighter-api')

const networkPassphrase =
wallet.network === 'testnet'
? 'Test SDF Network ; September 2015'
: 'Public Global Stellar Network ; September 2015'
const networkPassphrase = getNetworkPassphrase(wallet.network)

const result = await freighterSign(xdr, {
networkPassphrase,
Expand Down