Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional namespace #1424

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ class Wc2ConnectPlugin(private val application: Application) : FlutterPlugin,
override fun onSessionProposal(sessionProposal: Sign.Model.SessionProposal) {
Timber.d("[WalletDelegate] onSessionProposal $sessionProposal")
pendingProposals.add(sessionProposal)
val namespaces = sessionProposal.requiredNamespaces.mapValues { e ->
val requiredNamespaces = sessionProposal.requiredNamespaces.mapValues { e ->
e.value.toProposalNamespace()
}
val optionalNamespaces = sessionProposal.optionalNamespaces.mapValues { e ->
e.value.toProposalNamespace()
}
val proposer = mapOf(
Expand All @@ -138,7 +141,8 @@ class Wc2ConnectPlugin(private val application: Application) : FlutterPlugin,
val params = mapOf(
"id" to sessionProposal.proposerPublicKey,
"proposer" to Gson().toJson(proposer),
"requiredNamespaces" to Json.encodeToString(namespaces)
"requiredNamespaces" to Json.encodeToString(requiredNamespaces),
"optionalNamespaces" to Json.encodeToString(optionalNamespaces)
)
mainScope?.launch {
eventPublisher.emit(
Expand Down Expand Up @@ -247,7 +251,7 @@ class Wc2ConnectPlugin(private val application: Application) : FlutterPlugin,
result.error("-1", "Proposal not found", null)
return
}
val namespaces = proposal.requiredNamespaces.mapValues {
val namespaces = (proposal.requiredNamespaces + proposal.optionalNamespaces).mapValues {
Sign.Model.Namespace.Session(
chains = it.value.chains,
methods = it.value.methods,
Expand Down
16 changes: 13 additions & 3 deletions ios/Runner/Wallet Connect 2.0/WC2ChannelHanler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,16 @@ class WC2ChannelHandler: NSObject {
return
}

var allNamespaces = proposal.requiredNamespaces

if let optionalNamespaces = proposal.optionalNamespaces {
allNamespaces.merge(optionalNamespaces) { _, optionalValue in
return optionalValue
}
}

var sessionNamespaces = [String: SessionNamespace]()
proposal.requiredNamespaces.forEach {
allNamespaces.forEach {
let caip2Namespace = $0.key
let proposalNamespace = $0.value
let accounts = Set(proposalNamespace.chains!.compactMap { Account($0.absoluteString + ":\(account)") })
Expand Down Expand Up @@ -165,10 +173,12 @@ extension WC2ChannelHandler: FlutterStreamHandler {

var params: [String: Any] = [:]
let proposer = try? JSONEncoder().encode(sessionProposal.proposer)
let namespaces = try? JSONEncoder().encode(sessionProposal.requiredNamespaces)
let requiredNamespaces = try? JSONEncoder().encode(sessionProposal.requiredNamespaces)
let optionalNamespaces = try? JSONEncoder().encode(sessionProposal.optionalNamespaces)
params["id"] = sessionProposal.id
params["proposer"] = proposer != nil ? String(data: proposer!, encoding: .utf8) : nil
params["requiredNamespaces"] = namespaces != nil ? String(data: namespaces!, encoding: .utf8) : nil
params["requiredNamespaces"] = requiredNamespaces != nil ? String(data: requiredNamespaces!, encoding: .utf8) : nil
params["optionalNamespaces"] = optionalNamespaces != nil ? String(data: optionalNamespaces!, encoding: .utf8) : nil

events([
"eventName": "onSessionProposal",
Expand Down
50 changes: 26 additions & 24 deletions lib/model/connection_request_args.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// that can be found in the LICENSE file.
//

import 'package:autonomy_flutter/service/wc2_service.dart';
import 'package:collection/collection.dart';
import 'package:autonomy_flutter/model/wc2_request.dart';
import 'package:tezart/tezart.dart';

abstract class ConnectionRequest {
Expand All @@ -16,7 +15,7 @@ abstract class ConnectionRequest {

bool get isBeaconConnect => false;

get id;
String get id;

String? get name;

Expand All @@ -42,7 +41,7 @@ class BeaconRequest extends ConnectionRequest {
bool get isBeaconConnect => true;

@override
get id => _id;
String get id => _id;

@override
String? get name => appName;
Expand Down Expand Up @@ -70,6 +69,7 @@ class Wc2Proposal extends ConnectionRequest {
this._id, {
required this.proposer,
required this.requiredNamespaces,
this.optionalNamespaces = const {},
});

@override
Expand All @@ -79,19 +79,21 @@ class Wc2Proposal extends ConnectionRequest {
bool get isAutonomyConnect => _isAutonomyConnect();

bool _isAutonomyConnect() {
final proposalMethods =
requiredNamespaces.values.map((e) => e.methods).flattened.toSet();
final unsupportedMethods =
proposalMethods.difference(Wc2Service.autonomyMethods);
return unsupportedMethods.isEmpty;
final proposalChains =
allNamespaces.keys.toSet();
phuocbitmark marked this conversation as resolved.
Show resolved Hide resolved
return proposalChains.contains(Wc2Chain.autonomy);
}

AppMetadata proposer;
Map<String, Wc2Namespace> requiredNamespaces;
Map<String, Wc2Namespace> optionalNamespaces;

Map<String, Wc2Namespace> get allNamespaces =>
{...requiredNamespaces, ...optionalNamespaces};
final String _id;

@override
get id => _id;
String get id => _id;

@override
String? get name => proposer.name;
Expand All @@ -114,17 +116,17 @@ class AppMetadata {
String description;

factory AppMetadata.fromJson(Map<String, dynamic> json) => AppMetadata(
icons: List<String>.from(json["icons"].map((x) => x)),
name: json["name"],
url: json["url"],
description: json["description"],
icons: List<String>.from(json['icons'].map((x) => x)),
name: json['name'],
url: json['url'],
description: json['description'],
);

Map<String, dynamic> toJson() => {
"icons": List<dynamic>.from(icons.map((x) => x)),
"name": name,
"url": url,
"description": description,
'icons': List<dynamic>.from(icons.map((x) => x)),
'name': name,
'url': url,
'description': description,
};
}

Expand All @@ -140,14 +142,14 @@ class Wc2Namespace {
List<dynamic> events;

factory Wc2Namespace.fromJson(Map<String, dynamic> json) => Wc2Namespace(
chains: List<String>.from(json["chains"].map((x) => x)),
methods: List<String>.from(json["methods"].map((x) => x)),
events: List<dynamic>.from(json["events"].map((x) => x)),
chains: List<String>.from(json['chains'].map((x) => x)),
methods: List<String>.from(json['methods'].map((x) => x)),
events: List<dynamic>.from(json['events'].map((x) => x)),
);

Map<String, dynamic> toJson() => {
"chains": List<dynamic>.from(chains.map((x) => x)),
"methods": List<dynamic>.from(methods.map((x) => x)),
"events": List<dynamic>.from(events.map((x) => x)),
'chains': List<dynamic>.from(chains.map((x) => x)),
'methods': List<dynamic>.from(methods.map((x) => x)),
'events': List<dynamic>.from(events.map((x) => x)),
};
}
Loading
Loading