Skip to content

Commit

Permalink
addressed bug bash feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Sep 3, 2024
1 parent 1bef4d1 commit b2f0f8c
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 108 deletions.
36 changes: 26 additions & 10 deletions example/dapp/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import Flutter

methodsChannel = FlutterMethodChannel(name: AppDelegate.METHODS_CHANNEL, binaryMessenger: controller.binaryMessenger)
methodsChannel?.setMethodCallHandler({ [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
if (call.method == "initialLink") {
if (call.method == "initialLink") {
if let link = self?.initialLink {
self?.initialLink = nil
let _ = self?.linkStreamHandler.handleLink(link)
return
} else {
result("")
let handled = self?.linkStreamHandler.handleLink(link)
if (handled == true) {
self?.initialLink = nil
}
}
}
})
Expand All @@ -37,6 +36,13 @@ import Flutter
if let url = launchOptions?[.url] as? URL {
self.initialLink = url.absoluteString
}

if let userActivityDictionary = launchOptions?[.userActivityDictionary] as? [String: Any],
let userActivity = userActivityDictionary["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity,
userActivity.activityType == NSUserActivityTypeBrowsingWeb {

handleIncomingUniversalLink(userActivity: userActivity)
}

return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
Expand All @@ -46,13 +52,23 @@ import Flutter
}

override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// Handle universal links
if let url = userActivity.webpageURL {
self.initialLink = url.absoluteString
return linkStreamHandler.handleLink(self.initialLink!)
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
handleIncomingUniversalLink(userActivity: userActivity)
return true
}
return false
}

private func handleIncomingUniversalLink(userActivity: NSUserActivity) {
if let url = userActivity.webpageURL {
// Handle the URL, navigate to appropriate screen
print("App launched with Universal Link: \(url.absoluteString)")
let handled = linkStreamHandler.handleLink(url.absoluteString)
if (!handled){
self.initialLink = url.absoluteString
}
}
}
}

class LinkStreamHandler: NSObject, FlutterStreamHandler {
Expand Down
2 changes: 0 additions & 2 deletions example/dapp/ios/fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ platform :ios do
changelog: changelog,
distribute_external: true,
notify_external_testers: true,
distribute_external: true,
notify_external_testers: true,
skip_waiting_for_build_processing: false,
groups: ["External Testers"]
)
Expand Down
2 changes: 1 addition & 1 deletion example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class _MyHomePageState extends State<MyHomePage> {
metadata: PairingMetadata(
name: 'Sample dApp Flutter',
description: 'WalletConnect\'s sample dapp with Flutter',
url: 'https://walletconnect.com/',
url: _universalLink(),
icons: [
'https://images.prismic.io/wallet-connect/65785a56531ac2845a260732_WalletConnect-App-Logo-1024X1024.png'
],
Expand Down
7 changes: 2 additions & 5 deletions example/dapp/lib/utils/deep_link_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ class DeepLinkHandler {
_web3app = web3app;
}

static void checkInitialLink() {
static void checkInitialLink() async {
if (kIsWeb) return;
try {
_methodChannel.invokeMethod('initialLink').then(
_onLink,
onError: _onError,
);
_methodChannel.invokeMethod('initialLink');
} catch (e) {
debugPrint('[SampleWallet] [DeepLinkHandler] checkInitialLink $e');
}
Expand Down
35 changes: 25 additions & 10 deletions example/wallet/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import Flutter
methodsChannel?.setMethodCallHandler({ [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
if (call.method == "initialLink") {
if let link = self?.initialLink {
self?.initialLink = nil
let _ = self?.linkStreamHandler.handleLink(link)
return
} else {
result("")
let handled = self?.linkStreamHandler.handleLink(link)
if (handled == true) {
self?.initialLink = nil
}
}
}
})
Expand All @@ -38,6 +37,13 @@ import Flutter
self.initialLink = url.absoluteString
}

if let userActivityDictionary = launchOptions?[.userActivityDictionary] as? [String: Any],
let userActivity = userActivityDictionary["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity,
userActivity.activityType == NSUserActivityTypeBrowsingWeb {

handleIncomingUniversalLink(userActivity: userActivity)
}

return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

Expand All @@ -46,14 +52,23 @@ import Flutter
}

override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// Handle universal links
if let url = userActivity.webpageURL {
self.initialLink = url.absoluteString
return linkStreamHandler.handleLink(self.initialLink!)
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
handleIncomingUniversalLink(userActivity: userActivity)
return true
}
return false
}


private func handleIncomingUniversalLink(userActivity: NSUserActivity) {
if let url = userActivity.webpageURL {
// Handle the URL, navigate to appropriate screen
print("App launched with Universal Link: \(url.absoluteString)")
let handled = linkStreamHandler.handleLink(url.absoluteString)
if (!handled){
self.initialLink = url.absoluteString
}
}
}
}

class LinkStreamHandler: NSObject, FlutterStreamHandler {
Expand Down
2 changes: 0 additions & 2 deletions example/wallet/ios/fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ platform :ios do
changelog: changelog,
distribute_external: true,
notify_external_testers: true,
distribute_external: true,
notify_external_testers: true,
skip_waiting_for_build_processing: false,
groups: ["External Testers"]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class EVMService {
chainId: pRequest.chainId,
address: address,
transportType: pRequest.transportType.name,
verifyContext: pRequest.verifyContext,
)) {
try {
// Load the private key
Expand Down Expand Up @@ -152,6 +153,7 @@ class EVMService {
if (await MethodsUtils.requestApproval(
message,
transportType: pRequest.transportType.name,
verifyContext: pRequest.verifyContext,
)) {
try {
// Load the private key
Expand Down Expand Up @@ -196,6 +198,7 @@ class EVMService {
if (await MethodsUtils.requestApproval(
data,
transportType: pRequest.transportType.name,
verifyContext: pRequest.verifyContext,
)) {
try {
final keys = GetIt.I<IKeyService>().getKeysForChain(
Expand Down Expand Up @@ -236,6 +239,7 @@ class EVMService {
if (await MethodsUtils.requestApproval(
data,
transportType: pRequest.transportType.name,
verifyContext: pRequest.verifyContext,
)) {
try {
final keys = GetIt.I<IKeyService>().getKeysForChain(
Expand Down Expand Up @@ -281,6 +285,7 @@ class EVMService {
method: pRequest.method,
chainId: pRequest.chainId,
transportType: pRequest.transportType.name,
verifyContext: pRequest.verifyContext,
);
if (transaction is Transaction) {
try {
Expand Down Expand Up @@ -337,6 +342,7 @@ class EVMService {
method: pRequest.method,
chainId: pRequest.chainId,
transportType: pRequest.transportType.name,
verifyContext: pRequest.verifyContext,
);
if (transaction is Transaction) {
try {
Expand Down Expand Up @@ -446,6 +452,7 @@ class EVMService {
String? title,
String? method,
String? chainId,
VerifyContext? verifyContext,
required String transportType,
}) async {
Transaction transaction = tJson.toTransaction();
Expand Down Expand Up @@ -480,6 +487,7 @@ class EVMService {
method: method,
chainId: chainId,
transportType: transportType,
verifyContext: verifyContext,
extraModels: [
WCConnectionModel(
title: 'Gas price',
Expand Down
7 changes: 2 additions & 5 deletions example/wallet/lib/dependencies/deep_link_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ class DeepLinkHandler {
);
}

static void checkInitialLink() {
static void checkInitialLink() async {
if (kIsWeb) return;
try {
_methodChannel.invokeMethod('initialLink').then(
_onLink,
onError: _onError,
);
_methodChannel.invokeMethod('initialLink');
} catch (e) {
debugPrint('[SampleWallet] [DeepLinkHandler] checkInitialLink $e');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:walletconnect_flutter_v2_wallet/dependencies/key_service/chain_key.dart';

abstract class IKeyService {
Future<void> clearAll();

/// Returns a list of all the keys.
Future<List<ChainKey>> loadKeys();

Expand All @@ -19,5 +21,7 @@ abstract class IKeyService {

Future<void> loadDefaultWallet();

Future<String> getMnemonic();

Future<void> restoreWalletFromSeed({required String mnemonic});
}
22 changes: 20 additions & 2 deletions example/wallet/lib/dependencies/key_service/key_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import 'package:walletconnect_flutter_v2_wallet/utils/dart_defines.dart';
class KeyService extends IKeyService {
List<ChainKey> _keys = [];

@override
Future<void> clearAll() async {
final prefs = await SharedPreferences.getInstance();
final keys = prefs.getKeys();
for (var key in keys) {
if (key.startsWith('w3w_')) {
await prefs.remove(key);
}
}
}

@override
Future<List<ChainKey>> loadKeys() async {
// ⚠️ WARNING: SharedPreferences is not the best way to store your keys! This is just for example purposes!
Expand Down Expand Up @@ -44,10 +55,11 @@ class KeyService extends IKeyService {

@override
List<ChainKey> getKeysForChain(String value) {
String namespace = value;
if (value.contains(':')) {
return _keys.where((e) => e.chains.contains(value)).toList();
namespace = NamespaceUtils.getNamespaceFromChain(value);
}
return _keys.where((e) => e.namespace == value).toList();
return _keys.where((e) => e.namespace == namespace).toList();
}

@override
Expand All @@ -61,6 +73,12 @@ class KeyService extends IKeyService {
return accounts;
}

@override
Future<String> getMnemonic() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString('w3w_mnemonic') ?? '';
}

// ** bip39/bip32 - EIP155 **

@override
Expand Down
7 changes: 4 additions & 3 deletions example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Web3WalletService extends IWeb3WalletService {
metadata: PairingMetadata(
name: 'Flutter Wallet Sample',
description: 'WalletConnect\'s sample wallet with Flutter',
url: 'https://walletconnect.com/',
url: _universalLink(),
icons: [
'https://docs.walletconnect.com/assets/images/web3walletLogo-54d3b546146931ceaf47a3500868a73a.png'
],
Expand Down Expand Up @@ -198,6 +198,7 @@ class Web3WalletService extends IWeb3WalletService {
final proposer = args.params.proposer;
final result = (await _bottomSheetHandler.queueBottomSheet(
widget: WCRequestWidget(
verifyContext: args.verifyContext,
child: WCConnectionRequestWidget(
proposalData: args.params,
verifyContext: args.verifyContext,
Expand Down Expand Up @@ -321,8 +322,8 @@ class Web3WalletService extends IWeb3WalletService {
WCBottomSheetResult.reject;

if (rs != WCBottomSheetResult.reject) {
final chainKeys = GetIt.I<IKeyService>().getKeysForChain('eip155:1');
final privateKey = '0x${chainKeys[0].privateKey}';
final chainKeys = GetIt.I<IKeyService>().getKeysForChain('eip155');
final privateKey = '0x${chainKeys.first.privateKey}';
final credentials = EthPrivateKey.fromHex(privateKey);
//
final messageToSign = formattedMessages.length;
Expand Down
2 changes: 1 addition & 1 deletion example/wallet/lib/pages/apps_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class AppsPageState extends State<AppsPage> with GetItStateMixin {
DeepLinkHandler.waiting.value = true;
await _web3Wallet.pair(uri: Uri.parse(uri!));
} on WalletConnectError catch (e) {
_showErrorDialog('${e.code}: ${e.message}');
_showErrorDialog('${e.code}: ${e.message}\n$uri');
} on TimeoutException catch (_) {
_showErrorDialog('Time out error. Check your connection.');
}
Expand Down
7 changes: 4 additions & 3 deletions example/wallet/lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class _SettingsPageState extends State<SettingsPage> {
}
},
onRestoreDefault: () async {
await keysService.clearAll();
await keysService.loadDefaultWallet();
await keysService.loadKeys();
await showDialog(
Expand Down Expand Up @@ -307,12 +308,12 @@ class _EVMAccountsState extends State<_EVMAccounts> {
const SizedBox(height: 20.0),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: FutureBuilder<SharedPreferences>(
future: SharedPreferences.getInstance(),
child: FutureBuilder<String>(
future: keysService.getMnemonic(),
builder: (context, snapshot) {
return _DataContainer(
title: 'Seed phrase',
data: snapshot.data?.getString('w3w_mnemonic') ?? '',
data: snapshot.data ?? '',
blurred: true,
);
},
Expand Down
8 changes: 7 additions & 1 deletion example/wallet/lib/utils/methods_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class MethodsUtils {
String? address,
required String transportType,
List<WCConnectionModel> extraModels = const [],
VerifyContext? verifyContext,
}) async {
final bottomSheetService = GetIt.I<IBottomSheetService>();
final WCBottomSheetResult rs = (await bottomSheetService.queueBottomSheet(
widget: WCRequestWidget(
verifyContext: verifyContext,
child: WCConnectionWidget(
title: title ?? 'Approve Request',
info: [
Expand Down Expand Up @@ -55,7 +57,11 @@ class MethodsUtils {
debugPrint(
'[SampleWallet] handleRedirect topic: $topic, redirect: $redirect, error: $error');
openApp(topic, redirect, onFail: (e) {
goBackModal(title: 'Error', message: e!.message, success: false);
goBackModal(
title: 'Error',
message: error,
success: false,
);
});
}

Expand Down
Loading

0 comments on commit b2f0f8c

Please sign in to comment.