Skip to main content

useWallet

The useWallet() hook provides a simple interface for managing a wallet instance in your React application. It exposes the current wallet, lets you create or import one from multiple sources (phrase, seed, xpriv, WIF, xpub, public key, or serialized backup), and cleanly separates logic using React context.

Quick setup

Wrap your app (or a subtree) with the <WalletProvider>:

import { WalletProvider } from 'chaingate-react'

function App() {
return (
<WalletProvider>
<YourComponent />
</WalletProvider>
)
}

Then use the hook in any child component:

import { useWallet } from 'chaingate-react'

function MyComponent() {
const { wallet, newWallet, importWallet, closeWallet } = useWallet()
// ...
}

What it gives you

  • wallet — The currently loaded wallet (or null if none).
  • newWallet(language?, numberOfWords?) — Creates a new wallet with a generated mnemonic phrase. Returns { phrase, wallet }. Synchronous.
  • importWallet(params) — Imports a wallet from a phrase, seed, xpriv, private key, xpub, or public key. Synchronous.
  • deserializeWallet(data, askForPassword?) — Restores a wallet from serialized data. Synchronous.
  • createWalletFromString(input) — Auto-detects the format of a string (phrase, xpriv, xpub, hex key, WIF) and creates the appropriate wallet. Synchronous.
  • closeWallet() — Removes the current wallet from context.

All methods automatically update the context, so the wallet value is immediately available to all components.

Example: Create a new wallet

import { useWallet } from 'chaingate-react'

function CreateWalletButton() {
const { newWallet } = useWallet()

const handleCreate = () => {
const { phrase, wallet } = newWallet()
console.log('Phrase:', phrase)
// wallet is now available in context
}

return <button onClick={handleCreate}>Create Wallet</button>
}

Example: Import a wallet

import { useWallet } from 'chaingate-react'

function ImportWallet() {
const { importWallet } = useWallet()

const handleImport = (phrase: string) => {
importWallet({ phrase })
// wallet is now available in context
}

return <button onClick={() => handleImport('abandon abandon ...')}>Import</button>
}

Example: Auto-detect format

const { createWalletFromString } = useWallet()

// Accepts phrases, xpriv, xpub, hex keys, WIF — auto-detected
createWalletFromString('abandon abandon abandon abandon ...')
createWalletFromString('xpub6CUGRUo...')
createWalletFromString('L4mEiExaMplE...')

Error handling

If you use useWallet() outside of a <WalletProvider>, it will throw an error:

Error: useWallet must be used within <WalletProvider>

Make sure your component is inside the provider tree.

Key points

  • All wallet operations auto-update the React context.
  • No need to pass wallet instances manually through props.
  • One global wallet per context — easy to reset with closeWallet().
  • Compatible with all wallet types: phrase, seed, xpriv, private key, xpub, public key, and serialized.