Overview

The global wallet interface does not provide a way to export the wallet key. If you’d like users to be able to export their global wallet key, you can provide an easy interface for them to log into their wallet and export the key.
You should prompt the user to log in with exactly the same credentials as they use to log in to the global wallet.

Pre-requisites

  • Check the ‘default wallet version’ in the embedded wallet section of the dashboard
  • Ensure you are using the same environment ID as you do for your global wallet implementation.

Enabling Signin

Exporting

V3 Wallets

Use the exportPrivateKey method
  1. Get the primary wallet
import { useDynamicContext } from "@dynamic-labs/sdk-react-core";

const { primaryWallet } = useDynamicContext();
  1. Guard against no primary wallet or no embedded wallet
const handleExportPrivateKey = async () => {
  if (!primaryWallet?.address) {
    setErrorMessage("Please create a wallet first");
    return;
  }

  if (!primaryWallet.isEmbeddedWallet) {
    setErrorMessage("You do not have a global wallet");
    return;
  }
};
  1. Get the connector
import { DynamicWaasEVMConnectors } from '@dynamic-labs/waas-evm';

const connector = primaryWallet?.connector as DynamicWaasEVMConnector;
For EVM, Solana, and Sui wallets, you can use the DynamicWaasEVMConnector, DynamicWaasSolanaConnector, and DynamicWaasSuiConnector respectively.
import { DynamicWaasEVMConnectors } from '@dynamic-labs/waas-evm';
import { DynamicWaasSolanaConnectors } from '@dynamic-labs/waas-solana';
import { DynamicWaasSuiConnectors } from '@dynamic-labs/waas-sui';

const connector = primaryWallet?.connector as DynamicWaasEVMConnector;
const connector = primaryWallet?.connector as DynamicWaasSolanaConnector;
const connector = primaryWallet?.connector as DynamicWaasSuiConnector;
  1. Define the display container
const displayContainer = document.createElement('iframe');
  1. Export the private key
  const privateKey = await connector.exportPrivateKey({
    accountAddress: primaryWallet?.address,
    displayContainer: document.createElement('iframe'), // Replace with your secure container
  });
};

Full Example

import { useDynamicContext } from "@dynamic-labs/sdk-react-core";

const { primaryWallet } = useDynamicContext();

const handleExportPrivateKey = async () => {
  if (!primaryWallet?.address) {
    setErrorMessage("Please create a wallet first");
    return;
  }

  if (!primaryWallet.isEmbeddedWallet) {
    setErrorMessage("You do not have a global wallet");
    return;
  }

  const connector = primaryWallet?.connector as DynamicWaasEVMConnector;
  const privateKey = await connector.exportPrivateKey({
    accountAddress: primaryWallet?.address,
    displayContainer: document.createElement('iframe'), // Replace with your secure container
  });
};

V2 Wallets

Use the useEmbeddedReveal hook.
import { useEmbeddedReveal } from "@dynamic-labs/sdk-react-core";

const { initExportProcess } = useEmbeddedReveal();

<button onClick={() => initExportProcess()}>Export Wallet</button>;