Skip to content

Commit

Permalink
fix: WalletConnect issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alienc0der committed Jun 21, 2023
1 parent 9ebcae8 commit 3749c49
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 69 deletions.
107 changes: 48 additions & 59 deletions lib/services/wallet_connect_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,25 @@ class WalletConnectService {

Future<void> initClient() async {
if (kWcProjectId.isNotEmpty) {
try {
_wcClient = await Web3Wallet.createInstance(
projectId: kWcProjectId,
metadata: const PairingMetadata(
name: 's y r i u s',
description: 'A wallet for interacting with Zenon Network',
url: 'https://zenon.network',
// TODO: add Zenon icon
icons: ['https://avatars.githubusercontent.com/u/37784886'],
),
).onError((error, stackTrace) {
throw 'WalletConnect init failed';
});
} catch (e, stackTrace) {
_wcClient = await Web3Wallet.createInstance(
projectId: kWcProjectId,
metadata: const PairingMetadata(
name: 's y r i u s',
description: 'A wallet for interacting with Zenon Network',
url: 'https://zenon.network',
// TODO: add Zenon icon
icons: ['https://avatars.githubusercontent.com/u/37784886'],
),
).onError((e, stackTrace) {
Logger('WalletConnectService')
.log(Level.SEVERE, 'createInstance ', e, stackTrace);
return;
}
.log(Level.SEVERE, 'initClient onError ', e, stackTrace);
if (e != null) {
NotificationUtils.sendNotificationError(
e, 'WalletConnect initialization failed');
}
throw 'WalletConnect init failed';
});

for (var pairingInfo in pairings) {
dAppsActiveSessions
.addAll(getSessionsForPairing(pairingInfo.topic).values);
Expand All @@ -76,18 +77,9 @@ class WalletConnectService {
return;
}

Web3Wallet getWeb3Wallet() {
return _wcClient;
}

Future<PairingInfo> pair(Uri uri) => _wcClient.pair(uri: uri);

void _initListeners() {
_wcClient.onSessionPing.subscribe((args) {
Logger('WalletConnectService')
.log(Level.INFO, 'onSessionPing triggered', args.toString());
});

_wcClient.core.relayClient.onRelayClientDisconnect.subscribe((args) {
Logger('WalletConnectService').log(
Level.INFO, 'onRelayClientDisconnect triggered', args.toString());
Expand Down Expand Up @@ -126,6 +118,11 @@ class WalletConnectService {
.log(Level.INFO, 'onPairingExpire triggered', args.toString());
});

_wcClient.onSessionPing.subscribe((args) {
Logger('WalletConnectService')
.log(Level.INFO, 'onSessionPing triggered', args.toString());
});

_wcClient.onSessionDelete.subscribe((args) {
Logger('WalletConnectService')
.log(Level.INFO, 'onSessionDelete triggered', args.toString());
Expand All @@ -142,21 +139,16 @@ class WalletConnectService {
sl.get<WalletConnectSessionsBloc>().refreshResults();
});

_wcClient.onSessionPing.subscribe((args) {
Logger('WalletConnectService')
.log(Level.INFO, 'onSessionPing triggered', args.toString());
});

_wcClient.onSessionProposal.subscribe(onSessionProposal);

_wcClient.onAuthRequest.subscribe((args) {
_wcClient.onSessionRequest.subscribe((SessionRequestEvent? request) async {
Logger('WalletConnectService')
.log(Level.INFO, 'onAuthRequest triggered', args.toString());
.log(Level.INFO, 'onSessionRequest triggered', request.toString());
});

_wcClient.onSessionRequest.subscribe((SessionRequestEvent? request) async {
_wcClient.onAuthRequest.subscribe((args) {
Logger('WalletConnectService')
.log(Level.INFO, 'onSessionRequest triggered', request.toString());
.log(Level.INFO, 'onAuthRequest triggered', args.toString());
});

_wcClient.registerRequestHandler(
Expand All @@ -176,6 +168,7 @@ class WalletConnectService {
if (_context.mounted) {
final actionWasAccepted = await showDialogWithNoAndYesOptions(
context: _context,
isBarrierDismissible: false,
title: '${dAppMetadata.name} - Information',
content: Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -203,12 +196,8 @@ class WalletConnectService {
),
],
),
onYesButtonPressed: () async {
Navigator.pop(_context, true);
},
onNoButtonPressed: () {
Navigator.pop(_context, false);
},
onYesButtonPressed: () async {},
onNoButtonPressed: () {},
);

if (actionWasAccepted) {
Expand All @@ -218,6 +207,9 @@ class WalletConnectService {
'chainId': getChainIdentifier(),
};
} else {
NotificationUtils.sendNotificationError(
Errors.getSdkError(Errors.USER_REJECTED),
'You have rejected the WalletConnect request');
throw Errors.getSdkError(Errors.USER_REJECTED);
}
} else {
Expand Down Expand Up @@ -252,6 +244,7 @@ class WalletConnectService {
if (_context.mounted) {
final actionWasAccepted = await showDialogWithNoAndYesOptions(
context: _context,
isBarrierDismissible: false,
title: '${dAppMetadata.name} - Sign Message',
content: Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -279,17 +272,16 @@ class WalletConnectService {
),
],
),
onYesButtonPressed: () async {
Navigator.pop(_context, true);
},
onNoButtonPressed: () {
Navigator.pop(_context, false);
},
onYesButtonPressed: () async {},
onNoButtonPressed: () {},
);

if (actionWasAccepted) {
return await walletSign(message.codeUnits);
} else {
NotificationUtils.sendNotificationError(
Errors.getSdkError(Errors.USER_REJECTED),
'You have rejected the WalletConnect request');
throw Errors.getSdkError(Errors.USER_REJECTED);
}
} else {
Expand Down Expand Up @@ -331,6 +323,7 @@ class WalletConnectService {
if (_context.mounted) {
final wasActionAccepted = await showDialogWithNoAndYesOptions(
context: _context,
isBarrierDismissible: false,
title: '${dAppMetadata.name} - Send Payment',
content: Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -362,12 +355,8 @@ class WalletConnectService {
description: 'Are you sure you want to transfer '
'$amount ${token.symbol} to '
'$toAddress ?',
onYesButtonPressed: () {
Navigator.pop(_context, true);
},
onNoButtonPressed: () {
Navigator.pop(_context, false);
},
onYesButtonPressed: () {},
onNoButtonPressed: () {},
);

if (wasActionAccepted) {
Expand All @@ -382,6 +371,9 @@ class WalletConnectService {

return result!;
} else {
NotificationUtils.sendNotificationError(
Errors.getSdkError(Errors.USER_REJECTED),
'You have rejected the WalletConnect request');
throw Errors.getSdkError(Errors.USER_REJECTED);
}
} else {
Expand Down Expand Up @@ -555,6 +547,7 @@ class WalletConnectService {

final actionWasAccepted = await showDialogWithNoAndYesOptions(
context: _context,
isBarrierDismissible: false,
title: 'Approve session',
content: Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -582,12 +575,8 @@ class WalletConnectService {
),
],
),
onYesButtonPressed: () async {
Navigator.pop(_context, true);
},
onNoButtonPressed: () {
Navigator.pop(_context, false);
},
onYesButtonPressed: () async {},
onNoButtonPressed: () {},
);

if (actionWasAccepted) {
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/modular_widgets/bridge_widgets/swap_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class _SwapCardState extends State<SwapCard> {

void _onSwapButtonPressed() {
showDialogWithNoAndYesOptions(
isBarrierDismissible: true,
context: context,
title: 'Swap',
description: 'Are you sure you want to swap ${_amountController.text} '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class _SendLargeCardState extends State<SendLargeCard> {
if (_recipientKey.currentState!.validate() &&
_amountKey.currentState!.validate()) {
showDialogWithNoAndYesOptions(
isBarrierDismissible: false,
context: context,
title: 'Send Payment',
description: 'Are you sure you want to transfer '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class _SendMediumCardState extends State<SendMediumCard> {
_amountKey.currentState!.validate()) {
showDialogWithNoAndYesOptions(
context: context,
isBarrierDismissible: true,
title: 'Send Payment',
description: 'Are you sure you want to transfer '
'${_amountController.text} ${_selectedToken.symbol} to '
Expand Down
19 changes: 10 additions & 9 deletions lib/widgets/reusable_widgets/dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,37 @@ showDialogWithNoAndYesOptions({
required BuildContext context,
required String title,
required VoidCallback onYesButtonPressed,
required isBarrierDismissible,
VoidCallback? onNoButtonPressed,
Widget? content,
String? description,
}) =>
showDialog(
barrierDismissible: false,
barrierDismissible: isBarrierDismissible,
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: content ?? Text(description!),
actions: [
TextButton(
onPressed: () {
onNoButtonPressed?.call();
Navigator.pop(context, false);
},
child: Text(
'No',
style: Theme.of(context).textTheme.bodyLarge,
),
onPressed: () {
onNoButtonPressed?.call();
Navigator.pop(context);
},
),
TextButton(
onPressed: () {
onYesButtonPressed.call();
Navigator.pop(context);
},
style: Theme.of(context).textButtonTheme.style!.copyWith(
backgroundColor: MaterialStateColor.resolveWith(
(states) => AppColors.errorColor),
),
onPressed: () {
onYesButtonPressed.call();
Navigator.pop(context, true);
},
child: Text(
'Yes',
style: Theme.of(context).textTheme.bodyLarge,
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/reusable_widgets/settings_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class _SettingsNodeState extends State<SettingsNode> {
child: MaterialIconButton(
onPressed: () {
showDialogWithNoAndYesOptions(
isBarrierDismissible: true,
context: context,
title: 'Node Management',
description: 'Are you sure you want to delete '
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies:
launch_at_startup: ^0.2.1
app_links: ^3.4.3
logging: ^1.2.0
walletconnect_flutter_v2: ^2.0.9
walletconnect_flutter_v2: ^2.0.10
preference_list: ^0.0.1
screen_capturer: ^0.1.2
uni_ocr: ^0.1.0
Expand Down

0 comments on commit 3749c49

Please sign in to comment.