Skip to content

Commit

Permalink
add optional namespace (#1424)
Browse files Browse the repository at this point in the history
* add optional namespace

Signed-off-by: phuoc <[email protected]>

* rm unused import

Signed-off-by: phuoc <[email protected]>

* check autonomy connect

Signed-off-by: phuoc <[email protected]>

* Update lib/model/connection_request_args.dart

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* priority require name space

Signed-off-by: phuoc <[email protected]>

* priority require name space

Signed-off-by: phuoc <[email protected]>

---------

Signed-off-by: phuoc <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
phuocbitmark and github-actions[bot] authored Dec 15, 2023
1 parent 13888b9 commit 1f2aec9
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 142 deletions.
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,14 +251,15 @@ class Wc2ConnectPlugin(private val application: Application) : FlutterPlugin,
result.error("-1", "Proposal not found", null)
return
}
val namespaces = proposal.requiredNamespaces.mapValues {
val namespaces = (proposal.optionalNamespaces + proposal.requiredNamespaces).mapValues {
Sign.Model.Namespace.Session(
chains = it.value.chains,
methods = it.value.methods,
events = it.value.events,
accounts = it.value.chains?.map { chain -> "$chain:$account" } ?: emptyList(),
)
}
Timber.d("Approve namespace: $namespaces")
try {
SignClient.approveSession(
Sign.Params.Approve(
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) { requiredNamespaces, _ in
return requiredNamespaces
}
}

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
49 changes: 25 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,20 @@ 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();
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 +115,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 +141,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

0 comments on commit 1f2aec9

Please sign in to comment.