Privy’s exportWallet method requires a secure browser context and therefore can only be executed through the React SDK. Mobile apps built with React Native, Swift, Android, or Flutter can support key export by opening a hosted web page in a WebView. The user logs in on the hosted page and exports their key.
Privy integrated into the mobile app with users authenticating and embedded wallets created.
See the quickstart for React Native,
Swift, Android, or Flutter.
Create a hosted web page where the user can log in with Privy (if needed) and then tap a button to export their wallet key. The page uses the Privy React SDK and posts the result back to the native app via a messaging bridge.
When the user taps “Export key”, open the hosted export page URL in a WebView. Use incognito or non-persistent storage so that login and export data are not cached.
When the hosted page completes (or encounters an error), it posts a JSON message back to the native app. Parse this message to determine the outcome and dismiss the WebView.
React Native
Swift
Android
Flutter
Report incorrect code
Copy
Ask AI
const handleMessage = (event: any) => { const data = JSON.parse(event.nativeEvent.data); if (data.status === 'success') { // Key export completed successfully. Dismiss the WebView. setVisible(false); } else if (data.status === 'error') { // An error occurred during export. console.error('Export failed:', data.error); setVisible(false); }};
Report incorrect code
Copy
Ask AI
func userContentController( _ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { guard message.name == "exportResult", let body = message.body as? String, let data = body.data(using: .utf8), let result = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return } if result["status"] as? String == "success" { // Key export completed successfully. Dismiss the WebView. } else { // An error occurred during export. } webView.removeFromSuperview() dismiss(animated: true)}
Report incorrect code
Copy
Ask AI
inner class ExportBridge { @JavascriptInterface fun onExportResult(json: String) { val result = JSONObject(json) when (result.getString("status")) { "success" -> { // Key export completed successfully. Dismiss the activity. } "error" -> { // An error occurred during export. } } runOnUiThread { finish() } }}
Report incorrect code
Copy
Ask AI
void _onExportResult(String jsonString) { final data = jsonDecode(jsonString) as Map<String, dynamic>; if (data['status'] == 'success') { // Key export completed successfully. Dismiss the WebView. setState(() => _visible = false); } else if (data['status'] == 'error') { // An error occurred during export. debugPrint('Export failed: ${data['error']}'); setState(() => _visible = false); }}
Keep the following in mind when implementing the WebView-based export flow:
Ephemeral WebViews: Use incognito or non-persistent data store modes (shown in the examples
above) so that login and export data are not cached on the device.
Separate origin: The private key is assembled on a different origin from the app and the
hosted page. Neither the app nor the page can access the full private key.