diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6936044..2f2e7df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,3 +8,10 @@ jobs: - uses: actions/setup-node@v4 with: {node-version: 20} - run: npm ci && npm run build + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: {node-version: 20} + - run: npm ci && npm test diff --git a/src/context/WalletContext.test.tsx b/src/context/WalletContext.test.tsx index 5086977..8d75416 100644 --- a/src/context/WalletContext.test.tsx +++ b/src/context/WalletContext.test.tsx @@ -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' @@ -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, + }) + }) +}) diff --git a/src/context/WalletContext.tsx b/src/context/WalletContext.tsx index bfb9473..90258aa 100644 --- a/src/context/WalletContext.tsx +++ b/src/context/WalletContext.tsx @@ -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) @@ -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,