Skip to content

Commit

Permalink
Merge pull request #8 from wakumo/feature/support-redirect
Browse files Browse the repository at this point in the history
feat(metadata): add redirect field, support to launch redirect
  • Loading branch information
ludowkm authored Apr 12, 2023
2 parents 71ef639 + 2acd0b1 commit 4c20d0b
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
## 1.0.1

* Upgrade to the latest SDKs Android Bom 1.8.0

## 1.0.2

* Add `redirect` field into AppMetadata
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ class WalletConnectV2Plugin : FlutterPlugin, MethodCallHandler, ActivityAware,

val walletDelegate = object : SignClient.WalletDelegate {
override fun onSessionProposal(sessionProposal: Sign.Model.SessionProposal) {
// TODO: it should be have redirect here!
// https://github.com/WalletConnect/WalletConnectKotlinV2/issues/792
onEvent(
name = "proposal", data = sessionProposal.toFlutterValue()
name = "proposal", data = sessionProposal.toFlutterValue(null)
)
}

Expand Down Expand Up @@ -401,6 +403,7 @@ fun Sign.Model.SessionProposal.toFlutterValue(): Map<String, Any> {
"description" to this.description,
"url" to this.url,
"icons" to this.icons.map { it.toString() },
// TODO: should have redirect field here
),
"namespaces" to this.requiredNamespaces.map { (key, value) ->
key to mapOf(
Expand All @@ -420,6 +423,7 @@ fun Sign.Model.Session.toFlutterValue(): Map<String, Any> {
"description" to this.metaData?.description,
"url" to this.metaData?.url,
"icons" to this.metaData?.icons,
"redirect" to this.metaData?.redirect
),
"expiration" to Date(this.expiry).toUtcIsoDateString(),
"namespaces" to this.namespaces.map { (key, value) ->
Expand Down
6 changes: 6 additions & 0 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ PODS:
- shared_preferences_ios (0.0.1):
- Flutter
- Starscream (3.1.1)
- url_launcher_ios (0.0.1):
- Flutter
- wallet_connect_v2 (0.0.1):
- Flutter
- Starscream (= 3.1.1)
Expand All @@ -28,6 +30,7 @@ PODS:
DEPENDENCIES:
- Flutter (from `Flutter`)
- shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
- wallet_connect_v2 (from `.symlinks/plugins/wallet_connect_v2/ios`)

SPEC REPOS:
Expand All @@ -40,13 +43,16 @@ EXTERNAL SOURCES:
:path: Flutter
shared_preferences_ios:
:path: ".symlinks/plugins/shared_preferences_ios/ios"
url_launcher_ios:
:path: ".symlinks/plugins/url_launcher_ios/ios"
wallet_connect_v2:
:path: ".symlinks/plugins/wallet_connect_v2/ios"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad
Starscream: 4bb2f9942274833f7b4d296a55504dcfc7edb7b0
url_launcher_ios: 08a3dfac5fb39e8759aeb0abbd5d9480f30fc8b4
wallet_connect_v2: 6dbafd13b16a32d40a68d223001d527441992baf
WalletConnectSwiftV2: 4b90e4115ad47e8b8ce749be6de0d8b13a33225a

Expand Down
11 changes: 11 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>wcexample</string>
</array>
</dict>
</array>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
Expand Down
77 changes: 48 additions & 29 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';

import 'package:bip32/bip32.dart' as bip32;
import 'package:bip39/bip39.dart' as bip39;
Expand All @@ -9,6 +10,7 @@ import 'package:flutter/services.dart';
import 'package:hex/hex.dart';
import 'package:http/http.dart' hide Request;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wallet_connect_v2/wallet_connect_v2.dart';
import 'package:web3dart/crypto.dart';
import 'package:web3dart/web3dart.dart';
Expand Down Expand Up @@ -46,7 +48,8 @@ class _HomeViewState extends State<HomeView> with WidgetsBindingObserver {
name: 'Flutter Wallet',
url: 'https://avacus.cc',
description: 'Flutter Wallet by Avacus',
icons: ['https://avacus.cc/apple-icon-180x180.png']);
icons: ['https://avacus.cc/apple-icon-180x180.png'],
redirect: 'wcexample');
final _uriController = TextEditingController();
final List<Session> _sessions = [];

Expand Down Expand Up @@ -344,12 +347,13 @@ class _HomeViewState extends State<HomeView> with WidgetsBindingObserver {
children: [
_dappTopic == null
? TextButton(
child: const Text('Create Pair'),
child: const Text(
'Create Pair for Ethereum Mainnet'),
onPressed: () async {
final uri = await _walletConnectV2Plugin
.createPair(namespaces: {
'eip155': ProposalNamespace(chains: [
'eip155:5'
'eip155:1'
], methods: [
"eth_sendTransaction",
"personal_sign",
Expand All @@ -375,32 +379,8 @@ class _HomeViewState extends State<HomeView> with WidgetsBindingObserver {
TextButton(
child: const Text(
'Send personal_sign Request'),
onPressed: () async {
_walletConnectV2Plugin
.sendRequest(
request: Request(
method:
'personal_sign',
chainId:
'eip155:5',
topic:
_dappTopic!,
params: [
_exampleMessage,
_sessions
.firstWhere(
(element) =>
element
.topic ==
_dappTopic!)
.namespaces[
'eip155']!
.accounts
.first
.split(':')
.last
]));
}),
onPressed: () =>
onSendPersonalMessageTest()),
TextButton(
child: const Text('Disconnect'),
onPressed: () async {
Expand Down Expand Up @@ -568,6 +548,22 @@ class _HomeViewState extends State<HomeView> with WidgetsBindingObserver {
);
}

void onSendPersonalMessageTest() async {
final session =
_sessions.firstWhere((element) => element.topic == _dappTopic!);
await _walletConnectV2Plugin.sendRequest(
request: Request(
method: 'personal_sign',
chainId: 'eip155:1',
topic: _dappTopic!,
params: [
_exampleMessage,
session.namespaces['eip155']!.accounts.first.split(':').last
]));
// TODO: don't forget to check if where is the source come from to determine launch or not
session.peer.redirect?.launch();
}

Future _refreshSessions() async {
try {
final sessions = await _walletConnectV2Plugin.getActivatedSessions();
Expand Down Expand Up @@ -663,3 +659,26 @@ class _HomeViewState extends State<HomeView> with WidgetsBindingObserver {
super.dispose();
}
}

extension StringExt on String {
Future launch({int delayInMillis = 500}) async {
try {
await Future.delayed(Duration(milliseconds: delayInMillis));
final uri = Uri.parse(this);
if (Platform.isAndroid) {
final isAbleToLaunch = await canLaunchUrl(uri);
if (!isAbleToLaunch) return;
}
if (startsWith('http')) {
await launchUrl(
uri,
mode: LaunchMode.externalApplication,
);
} else if (contains('://')) {
await launchUrl(uri);
} else {
await launchUrl(Uri.parse('$this://'));
}
} catch (_) {}
}
}
68 changes: 66 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,70 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
url_launcher:
dependency: "direct main"
description:
name: url_launcher
sha256: "75f2846facd11168d007529d6cd8fcb2b750186bea046af9711f10b907e1587e"
url: "https://pub.dev"
source: hosted
version: "6.1.10"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
sha256: dd729390aa936bf1bdf5cd1bc7468ff340263f80a2c4f569416507667de8e3c8
url: "https://pub.dev"
source: hosted
version: "6.0.26"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
sha256: "9af7ea73259886b92199f9e42c116072f05ff9bea2dcb339ab935dfc957392c2"
url: "https://pub.dev"
source: hosted
version: "6.1.4"
url_launcher_linux:
dependency: transitive
description:
name: url_launcher_linux
sha256: "206fb8334a700ef7754d6a9ed119e7349bc830448098f21a69bf1b4ed038cabc"
url: "https://pub.dev"
source: hosted
version: "3.0.4"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
sha256: "0ef2b4f97942a16523e51256b799e9aa1843da6c60c55eefbfa9dbc2dcb8331a"
url: "https://pub.dev"
source: hosted
version: "3.0.4"
url_launcher_platform_interface:
dependency: transitive
description:
name: url_launcher_platform_interface
sha256: "6c9ca697a5ae218ce56cece69d46128169a58aa8653c1b01d26fcd4aad8c4370"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
url_launcher_web:
dependency: transitive
description:
name: url_launcher_web
sha256: "81fe91b6c4f84f222d186a9d23c73157dc4c8e1c71489c4d08be1ad3b228f1aa"
url: "https://pub.dev"
source: hosted
version: "2.0.16"
url_launcher_windows:
dependency: transitive
description:
name: url_launcher_windows
sha256: a83ba3607a507758669cfafb03f9de09bf6e6280c14d9b9cb18f013e406dcacd
url: "https://pub.dev"
source: hosted
version: "3.0.5"
uuid:
dependency: transitive
description:
Expand All @@ -587,7 +651,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.0.2"
watcher:
dependency: transitive
description:
Expand Down Expand Up @@ -630,4 +694,4 @@ packages:
version: "3.1.1"
sdks:
dart: ">=2.18.5 <3.0.0"
flutter: ">=3.0.0"
flutter: ">=3.3.0"
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies:
bip32: ^2.0.0
eth_sig_util: ^0.0.9
shared_preferences: ^2.0.15
url_launcher: ^6.1.10

dev_dependencies:
flutter_test:
Expand Down
9 changes: 8 additions & 1 deletion ios/Classes/SwiftWalletConnectV2Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public class SwiftWalletConnectV2Plugin: NSObject, FlutterPlugin, FlutterStreamH
case "init": do {
let arguments = call.arguments as! [String: Any]
let projectId = arguments["projectId"] as! String
let appMetadata = arguments["appMetadata"] as! [String: Any]
var appMetadata = arguments["appMetadata"] as! [String: Any]
if (appMetadata["redirect"] != nil) {
appMetadata["redirect"] = [
"native": (appMetadata["redirect"] as! String).starts(with: "http") ? nil : (appMetadata["redirect"] as? String),
"universal": (appMetadata["redirect"] as! String).starts(with: "http") ? (appMetadata["redirect"] as? String) : nil,
];
}
let metadata: AppMetadata = try! JSONDecoder().decode(AppMetadata.self, from: JSONSerialization.data(withJSONObject: appMetadata))

Networking.configure(projectId: projectId, socketFactory: SocketFactory(), socketConnectionType: .manual)
Expand Down Expand Up @@ -310,6 +316,7 @@ extension AppMetadata {
"description": self.description,
"url": self.url,
"icons": self.icons,
"redirect": self.redirect?.universal != nil ? self.redirect?.universal as Any : self.redirect?.native as Any
];
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/model/app_metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class AppMetadata {
final String url;
final String description;
final List<String> icons;
final String? redirect;

AppMetadata(
{required this.name,
required this.url,
required this.description,
required this.icons});
required this.icons,
this.redirect});

factory AppMetadata.fromJson(Map<String, dynamic> json) =>
_$AppMetadataFromJson(json);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/model/app_metadata.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/model/session_delete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class SessionDelete {
_$SessionDeleteFromJson(json);

Map<String, dynamic> toJson() => _$SessionDeleteToJson(this);
}
}
2 changes: 1 addition & 1 deletion lib/src/model/session_rejection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class SessionRejection {
_$SessionRejectionFromJson(json);

Map<String, dynamic> toJson() => _$SessionRejectionToJson(this);
}
}
Loading

0 comments on commit 4c20d0b

Please sign in to comment.