Skip to main content

moonx_flutter 0.1.0-beta.7 copy "moonx_flutter: ^0.1.0-beta.7" to clipboard
moonx_flutter: ^0.1.0-beta.7 copied to clipboard

MoonX embedded wallets and authentication for Flutter apps on iOS and Android. Headless, so your app owns the UI, with native passkeys and MPC signing.

example/lib/main.dart

// Minimal moonx_flutter integration: email-code sign-in, an Ethereum wallet,
// and a signed message. The app owns every screen; MoonXHost only appears
// for key export and interactive CAPTCHA challenges.
//
// Run with your app's publishable key:
//   flutter run --dart-define=MOONX_PUBLISHABLE_KEY=moon_pk_live_...

import 'package:flutter/material.dart';
import 'package:moonx_flutter/moonx_flutter.dart';

const _publishableKey = String.fromEnvironment('MOONX_PUBLISHABLE_KEY');

void main() {
  final client = MoonXClient(
    publishableKey: _publishableKey,
    config: const MoonXConfig(oauthRedirectUri: 'moonxexample://oauth'),
  );
  runApp(
    MaterialApp(
      title: 'MoonX example',
      // Above the Navigator, so the export sheet covers every route.
      builder: (context, child) => MoonXHost(client: client, child: child!),
      home: ExampleScreen(client: client),
    ),
  );
}

class ExampleScreen extends StatefulWidget {
  const ExampleScreen({super.key, required this.client});

  final MoonXClient client;

  @override
  State<ExampleScreen> createState() => _ExampleScreenState();
}

class _ExampleScreenState extends State<ExampleScreen> {
  final _email = TextEditingController();
  final _code = TextEditingController();
  bool _codeSent = false;
  MoonXWallet? _wallet;
  String? _status;

  MoonXClient get _client => widget.client;

  Future<void> _run(Future<String> Function() action) async {
    setState(() => _status = 'Working...');
    try {
      final message = await action();
      if (mounted) setState(() => _status = message);
    } on MoonXException catch (error) {
      if (mounted) setState(() => _status = error.message);
    }
  }

  @override
  void dispose() {
    _email.dispose();
    _code.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('MoonX example')),
      body: ListenableBuilder(
        listenable: _client,
        builder: (context, _) => ListView(
          padding: const EdgeInsets.all(16),
          children: [
            if (_client.initializationError case final error?)
              Text(error.message)
            else if (!_client.ready)
              const Text('Connecting...')
            else if (!_client.isAuthenticated &&
                _client.sessionStatus == SessionStatus.restoring)
              const Text('Restoring session...')
            else if (!_client.isAuthenticated &&
                _client.sessionStatus == SessionStatus.error)
              TextButton(
                onPressed: _client.retrySession,
                child: const Text('Retry session check'),
              )
            else if (!_client.isAuthenticated) ...[
              TextField(
                controller: _email,
                decoration: const InputDecoration(labelText: 'Email'),
                keyboardType: TextInputType.emailAddress,
              ),
              if (_codeSent)
                TextField(
                  controller: _code,
                  decoration: const InputDecoration(labelText: 'Code'),
                  keyboardType: TextInputType.number,
                ),
              FilledButton(
                onPressed: () => _run(() async {
                  if (!_codeSent) {
                    await _client.sendCode(email: _email.text.trim());
                    setState(() => _codeSent = true);
                    return 'Code sent';
                  }
                  await _client.loginWithCode(code: _code.text.trim());
                  return 'Signed in';
                }),
                child: Text(_codeSent ? 'Verify code' : 'Send code'),
              ),
            ] else ...[
              FilledButton(
                onPressed: () => _run(() async {
                  final wallet = await _client.wallets.create(
                    WalletChain.ethereum,
                  );
                  setState(() => _wallet = wallet);
                  return 'Wallet ${wallet.publicAddress}';
                }),
                child: const Text('Create Ethereum wallet'),
              ),
              if (_wallet case final wallet?)
                FilledButton(
                  onPressed: () => _run(() async {
                    final result = await _client.ethereum.signMessage(
                      message: 'Hello from Flutter',
                      wallet: wallet,
                    );
                    return 'Signature ${result.signature}';
                  }),
                  child: const Text('Sign message'),
                ),
              OutlinedButton(
                onPressed: () => _run(() async {
                  await _client.logout();
                  setState(() {
                    _codeSent = false;
                    _wallet = null;
                  });
                  return 'Signed out';
                }),
                child: const Text('Log out'),
              ),
            ],
            if (_status case final status?)
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: SelectableText(status),
              ),
          ],
        ),
      ),
    );
  }
}
0
likes
150
points
497
downloads

Documentation

API reference

Publisher

verified publishermoonpay.com

Weekly Downloads

MoonX embedded wallets and authentication for Flutter apps on iOS and Android. Headless, so your app owns the UI, with native passkeys and MPC signing.

Homepage

Topics

#wallet #web3 #mpc #passkeys

License

MIT (license)

Dependencies

flutter, flutter_secure_storage, flutter_web_auth_2, http, passkeys, pointycastle, shared_preferences, webview_flutter, webview_flutter_android, webview_flutter_wkwebview

More

Packages that depend on moonx_flutter

Packages that implement moonx_flutter