createWalletConnectSession method
Implementation
Future<void> createWalletConnectSession(BuildContext context) async {
// Create a new session
if (walletConnect.connected) {
statusMessage =
'Already connected to ${walletConnect.session.peerMeta?.name} \n${walletConnect.session.peerMeta?.description}\n${walletConnect.session.peerMeta?.url}';
curStatus =
'createWalletConnectSession - WalletConnect Already connected to ${walletConnect.session.peerMeta?.name} with minter: $account, chainId $chainId. Ignored.';
return;
}
// IOS users will need to be prompted which wallet to use.
// if (Platform.isIOS) {
// List<WalletConnectRegistryListing> listings =
// await readWalletRegistry(limit: 4);
// await showModalBottomSheet(
// context: context,
// builder: (context) {
// return showIOSWalletSelectionDialog(
// context, listings, setWalletListing);
// },
// isScrollControlled: true,
// isDismissible: false,
// shape:
// RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.0)),
// );
// }
curStatus = 'createWalletConnectSession';
SessionStatus session;
try {
session = await walletConnect.createSession(
chainId: 44787,
onDisplayUri: (uri) async {
// _displayUri = uri;
curStatus = ('_displayUri updated with $uri');
// Open any registered wallet via wc: intent
bool? result;
// IOS users have already chosen wallet, so customize the launcher
if (Platform.isIOS) {
uri =
'${walletListing.mobile.universal}/wc?uri=${Uri.encodeComponent(uri)}';
}
// Else
// - Android users will choose their walled from the OS prompt
curStatus = ('launching uri: $uri');
try {
result = await launchUrl(Uri.parse(uri),
mode: LaunchMode.externalApplication);
if (result == false) {
// launch alternative method
curStatus =
('Initial launchuri failed. Fallback launch with forceSafariVC true');
result = await launchUrl(Uri.parse(uri));
if (result == false) {
curStatus = ('Could not launch $uri');
}
}
if (result) {
statusMessage = 'Launched wallet app, requesting session.';
}
} on PlatformException catch (e) {
if (e.code == 'ACTIVITY_NOT_FOUND') {
curStatus = ('No wallets available - do nothing!');
statusMessage =
'ERROR - No WalletConnect compatible wallets found.';
return;
}
curStatus = ('launch returned $result');
curStatus =
('Unexpected PlatformException error: ${e.message}, code: ${e.code}, details: ${e.details}');
} on Exception catch (e) {
curStatus = ('launch returned $result');
curStatus = ('url launcher other error e: $e');
}
});
} catch (e) {
curStatus = ('Unable to connect - killing the session on our side.');
statusMessage = 'Unable to connect - killing the session on our side.';
walletConnect.killSession();
return;
}
if (session.accounts.isEmpty) {
statusMessage =
'Failed to connect to wallet. Bridge Overloaded? Could not Connect?';
// wc:f54c5bca-7712-4187-908c-9a92aa70d8db@1?bridge=https%3A%2F%2Fz.bridge.walletconnect.org&key=155ca05ffc2ab197772a5bd56a5686728f9fcc2b6eee5ffcb6fd07e46337888c
curStatus =
('Failed to connect to wallet. Bridge Overloaded? Could not Connect?');
}
}