@@ -10,7 +10,8 @@ import {
1010 type ReactNode ,
1111} from 'react'
1212import { setSignTransaction } from '@/lib/contract'
13- import { NETWORK } from '@/lib/stellar'
13+ import { type NetworkName , getNetworkConfig } from '@/lib/stellar'
14+ import { useNetwork } from './network-provider'
1415
1516// ─── Wallet options ───────────────────────────────────────────────────────────
1617
@@ -36,7 +37,7 @@ interface WalletContextValue {
3637 isConnected : boolean
3738 connect : ( walletId : string ) => Promise < void >
3839 disconnect : ( ) => void
39- signTransaction : ( xdr : string ) => Promise < string >
40+ signTransaction : ( xdr : string , network ?: NetworkName ) => Promise < string >
4041}
4142
4243const WalletContext = createContext < WalletContextValue | null > ( null )
@@ -56,10 +57,10 @@ async function connectFreighter(): Promise<string> {
5657 return result . address
5758}
5859
59- async function signWithFreighter ( xdr : string ) : Promise < string > {
60+ async function signWithFreighter ( xdr : string , networkPassphrase : string ) : Promise < string > {
6061 const { signTransaction } = await import ( '@stellar/freighter-api' )
6162 const result = await signTransaction ( xdr , {
62- networkPassphrase : NETWORK . passphrase ,
63+ networkPassphrase,
6364 } )
6465 if ( result . error ) throw new Error ( result . error )
6566 return result . signedTxXdr
@@ -82,19 +83,13 @@ async function connectWallet(id: string): Promise<string> {
8283 }
8384}
8485
85- async function signWithWallet ( id : string , xdr : string ) : Promise < string > {
86- switch ( id ) {
87- case 'freighter' : return signWithFreighter ( xdr )
88- default : throw new Error ( `Signing not implemented for ${ id } ` )
89- }
90- }
91-
9286// ─── Provider ─────────────────────────────────────────────────────────────────
9387
9488export function WalletProvider ( { children } : { children : ReactNode } ) {
9589 const [ address , setAddress ] = useState < string | null > ( null )
9690 const [ walletId , setWalletId ] = useState < string | null > ( null )
9791 const [ connecting , setConnecting ] = useState ( false )
92+ const { network } = useNetwork ( )
9893
9994 const connect = useCallback ( async ( id : string ) => {
10095 setConnecting ( true )
@@ -113,16 +108,20 @@ export function WalletProvider({ children }: { children: ReactNode }) {
113108 } , [ ] )
114109
115110 const signTransaction = useCallback (
116- async ( xdr : string ) : Promise < string > => {
111+ async ( xdr : string , customNetwork ?: NetworkName ) : Promise < string > => {
117112 if ( ! walletId ) throw new Error ( 'No wallet connected' )
118- return signWithWallet ( walletId , xdr )
113+ const config = getNetworkConfig ( customNetwork || network )
114+ switch ( walletId ) {
115+ case 'freighter' : return signWithFreighter ( xdr , config . passphrase )
116+ default : throw new Error ( `Signing not implemented for ${ walletId } ` )
117+ }
119118 } ,
120- [ walletId ] ,
119+ [ walletId , network ] ,
121120 )
122121
123122 // Keep contract layer in sync
124123 useEffect ( ( ) => {
125- setSignTransaction ( signTransaction )
124+ setSignTransaction ( ( xdr : string ) => signTransaction ( xdr ) )
126125 } , [ signTransaction ] )
127126
128127 const value = useMemo < WalletContextValue > (
0 commit comments