Created
June 12, 2025 12:12
-
-
Save quetool/0fadd1fb038a8dec44cf04b8eff428bc to your computer and use it in GitHub Desktop.
siwe_example.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:reown_appkit/reown_appkit.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
// TODO Check out the docs on how to tweak the modal theme https://docs.reown.com/appkit/flutter/core/theming | |
return ReownAppKitModalTheme( | |
// isDarkMode: false | true, | |
// themeData: ReownAppKitModalThemeData( | |
// lightColors: ReownAppKitModalColors.lightMode.copyWith(), | |
// darkColors: ReownAppKitModalColors.darkMode.copyWith(), | |
// radiuses: ReownAppKitModalRadiuses.circular, | |
// ), | |
child: MaterialApp( | |
title: 'reown_test_app', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const HomePage(), | |
), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({super.key}); | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
late ReownAppKitModal _appKitModal; | |
SIWEConfig _createStaticSIWEConfig() => SIWEConfig( | |
getNonce: () async { | |
print('🔍 getNonce()'); | |
return SIWEUtils.generateNonce(); | |
}, | |
getMessageParams: () async { | |
// Provide everything that is needed to construct the SIWE message | |
debugPrint('[SIWEConfig] getMessageParams()'); | |
final uri = Uri.parse('https://reown_test_app.com'); | |
return SIWEMessageArgs( | |
domain: uri.authority, | |
uri: 'https://${uri.authority}/wallet', | |
statement: 'Welcome to AppKit $packageVersion for Flutter.', | |
methods: MethodsConstants.allMethods, | |
); | |
}, | |
createMessage: (SIWECreateMessageArgs args) { | |
// Create SIWE message to be signed. | |
// You can use our provided formatMessage() method of implement your own | |
// debugPrint('[SIWEConfig] createMessage()'); | |
final msg = SIWEUtils.formatMessage(args); | |
debugPrint('[SIWEConfig] createMessage() msg: $msg'); | |
return msg; | |
}, | |
verifyMessage: (SIWEVerifyMessageArgs args) async { | |
// Implement your verifyMessage to authenticate the user after it. | |
debugPrint('[SIWEConfig] verifyMessage()'); | |
final chainId = SIWEUtils.getChainIdFromMessage(args.message); | |
final address = SIWEUtils.getAddressFromMessage(args.message); | |
final cacaoSignature = args.cacao != null | |
? args.cacao!.s | |
: CacaoSignature( | |
t: CacaoSignature.EIP191, | |
s: args.signature, | |
); | |
return await SIWEUtils.verifySignature( | |
address, | |
args.message, | |
cacaoSignature, | |
chainId, | |
'50f81661a58229027394e0a19e9db752', // Your project ID | |
); | |
}, | |
getSession: () async { | |
// This will need to be handled by the instance when needed | |
// For now, return a basic session - the actual implementation | |
// should be handled in the instance methods | |
final address = _appKitModal.session!.getAddress('eip155')!; | |
return SIWESession(address: address, chains: ['eip155:1']); | |
}, | |
onSignIn: (SIWESession session) { | |
// Called after SIWE message is signed and verified | |
debugPrint('[SIWEConfig] onSignIn()'); | |
}, | |
signOut: () async { | |
// Called when user taps on disconnect button | |
return true; | |
}, | |
onSignOut: () { | |
// Called when disconnecting WalletConnect session was successfull | |
debugPrint('[SIWEConfig] onSignOut()'); | |
}, | |
enabled: true, | |
signOutOnDisconnect: true, | |
signOutOnAccountChange: false, | |
signOutOnNetworkChange: false, | |
); | |
@override | |
void initState() { | |
super.initState(); | |
// TODO check the docs at https://docs.reown.com/appkit/flutter/core/installation | |
ReownAppKitModalNetworks.removeSupportedNetworks('solana'); | |
_appKitModal = ReownAppKitModal( | |
context: context, | |
logLevel: LogLevel.all, | |
projectId: '50f81661a58229027394e0a19e9db752', | |
metadata: const PairingMetadata( | |
name: 'reown_test_app', | |
description: 'reown_test_app description', | |
url: 'https://reown_test_app.com', | |
icons: ['https://reown_test_app.com/logo.png'], | |
// TODO check the docs on how to configure the redirect object https://docs.reown.com/appkit/flutter/core/usage#redirect-to-your-dapp | |
redirect: Redirect( | |
native: 'reown_test_app://', | |
universal: 'https://reown_test_app.com/app', | |
// See https://docs.reown.com/appkit/flutter/core/link-mode on how to enable Link Mode | |
// linkMode: true, | |
), | |
), | |
// enableAnalytics: true, | |
// See https://docs.reown.com/appkit/flutter/core/siwe on how to enable SIWE | |
siweConfig: _createStaticSIWEConfig(), | |
// If you enable social features you will have to whitelist your bundleId/packageName under the Mobile Application IDs secion in https://cloud.reown.com/app | |
// Please also follow carefully the relevant doc section https://docs.reown.com/appkit/flutter/core/email | |
// You can delete this commented section if you don't want to enable Email/Social login | |
featuresConfig: FeaturesConfig( | |
socials: [ | |
AppKitSocialOption.Email, | |
AppKitSocialOption.X, | |
AppKitSocialOption.Google, | |
AppKitSocialOption.Apple, | |
// AppKitSocialOption.Discord, | |
// AppKitSocialOption.Facebook, | |
// AppKitSocialOption.GitHub, | |
// AppKitSocialOption.Telegram, | |
// AppKitSocialOption.Twitch, | |
], | |
), | |
// https://docs.reown.com/appkit/flutter/core/options#getbalancefallback%3A | |
// getBalanceFallback: () async {}, | |
// disconnectOnDispose: true, | |
); | |
_appKitModal.init().then((value) => setState(() {})); | |
// More events at https://docs.reown.com/appkit/flutter/core/events | |
_appKitModal.onModalConnect.subscribe(_onModalConnect); | |
_appKitModal.onModalDisconnect.subscribe(_onModalDisconnect); | |
} | |
void _onModalConnect(ModalConnect? event) { | |
setState(() {}); | |
} | |
void _onModalDisconnect(ModalDisconnect? event) { | |
setState(() {}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('reown_test_app'), | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
), | |
body: Center( | |
child: Column( | |
children: [ | |
AppKitModalNetworkSelectButton( | |
appKit: _appKitModal, | |
context: context, | |
), | |
AppKitModalConnectButton( | |
appKit: _appKitModal, | |
context: context, | |
), | |
Visibility( | |
visible: _appKitModal.isConnected, | |
child: Column( | |
children: [ | |
AppKitModalAccountButton( | |
appKitModal: _appKitModal, | |
context: context, | |
), | |
AppKitModalAddressButton( | |
appKitModal: _appKitModal, | |
onTap: () {}, | |
), | |
AppKitModalBalanceButton( | |
appKitModal: _appKitModal, | |
onTap: () {}, | |
), | |
ValueListenableBuilder<String>( | |
valueListenable: _appKitModal.balanceNotifier, | |
builder: (_, balance, __) { | |
return Text('My balance: $balance'); | |
}, | |
), | |
], | |
), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment