Getting Network Information

When working with Bitcoin, it is essential to identify the active network. The testnet and regtest networks are intended for development and testing, while mainnet is used for real Bitcoin transactions with actual funds.

Getting Network from WalletConnect

Use the network property to retrieve the current network information. It returns null if disconnected, or a WalletConnectNetwork object when connected.

react
WalletConnectNetwork interface
export interface Network {
    wif: number; /* https://en.bitcoin.it/wiki/Wallet_import_format */
    bip32: Bip32; /* https://en.bitcoin.it/wiki/BIP_0032 */
    messagePrefix: string;
    bech32: string; /* https://en.bitcoin.it/wiki/Bech32 */
    bech32Opnet?: string;
    pubKeyHash: number;
    scriptHash: number;
}

export interface WalletConnectNetwork extends Network {
    chainType: WalletChainType;
    network: WalletNetwork;
}
react
Get current network from WalletConnect
import { useWalletConnect } from "@btc-vision/walletconnect";

function App() {
const { network } = useWalletConnect()

return (
<div>
    {network 
    ? `Your are connected to ${network.network}` 
    : `You are not connected`
    }
</div>
)}

Getting Network from walletInstance

Call the asynchronous getChain() method on the walletInstance to retrieve the current network information.

For Unisat-based wallets, this method returns a UnisatChainInfo object. Other wallet providers may return a different type.

react
UnisatChainInfo interface
export interface UnisatChainInfo {
    readonly enum: UnisatChainType; /* BITCOIN_REGTEST, BITCOIN_TESTNET, BITCOIN_MAINNET */
    readonly name: string; /* 'regtest', 'testnet', 'mainnet' */
    readonly network: UnisatNetwork; /* testnet, mainnet, regtest */
}
react
Get current network from walletInstance
import { useWalletConnect } from "@btc-vision/walletconnect";

function App() {
    const { walletInstance } = useWalletConnect()
    const [network, setNetwork] = useState<string|undefined>(undefined)

    useEffect(() => {
        async function getNetwork() {
            if (walletInstance) {
                const chain = await walletInstance.getChain()
                setNetwork(chain.network)
            }
        }
        void getNetwork()
    }, [walletInstance])

return (
    <div>
        {network 
        ? `Your are connected to ${network}` 
        : `You are not connected`
        }
    </div>
)}