Skip to content

Commit

Permalink
Support for Stellar and web3
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Oct 1, 2024
1 parent 81dd840 commit 033d74a
Show file tree
Hide file tree
Showing 205 changed files with 51,966 additions and 31,065 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion mrt_native_support/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.1.8
blockchain_utils: ^3.3.0
blockchain_utils: ^3.4.0
# blockchain_utils:
# path: ../../../bitcoin/blockchain_utils

Expand Down
Binary file added mrt_wallet/assets/image/xlm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mrt_wallet/assets/wasm/crypto.unopt.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions mrt_wallet/assets/wasm/crypto.unopt.wasm.map

Large diffs are not rendered by default.

Binary file modified mrt_wallet/assets/wasm/crypto.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions mrt_wallet/assets/wasm/crypto.wasm.map

Large diffs are not rendered by default.

71,461 changes: 40,694 additions & 30,767 deletions mrt_wallet/assets/webview/script.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions mrt_wallet/js/js_wallet/models/models/networks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export 'networks/ethereum.dart';
export 'networks/solana.dart';
export 'networks/tron.dart';
export 'networks/ton.dart';
export 'networks/stellar.dart';
82 changes: 82 additions & 0 deletions mrt_wallet/js/js_wallet/models/models/networks/stellar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'dart:js_interop';
import 'package:mrt_wallet/app/core.dart';
import '../../models.dart';

@JS("stellar")
external set stellar(Proxy? stellar);
extension type StellarWalletAdapter(JSObject _) implements MRTNetworkAdapter {
external set publicKey(JSObject? publicKey);

external bool get isConnected;
external set isConnected(bool isConnected);
external set connect(JSFunction f);

external set signMessage(JSFunction f);
@JS("signTransaction")
external set signTransaction(JSFunction f);
@JS("signAndSendTransaction")
external set signAndSendTransaction(JSFunction f);
@JS("signAllTransactions")
external set signAllTransactions(JSFunction f);
@JS("signAndSendAllTransactions")
external set signAndSendAllTransactions(JSFunction f);

external set on(JSFunction f);
external set removeListener(JSFunction f);

factory StellarWalletAdapter.setup() {
return StellarWalletAdapter(JSObject());
}
}

class StellarAccountsChanged {
final List<String> accounts;
final String? defaultAddress;
final StellarProviderConnectInfo connectInfo;
StellarAccountsChanged({
required List<String> accounts,
required this.defaultAddress,
required this.connectInfo,
}) : accounts = accounts.imutable;
factory StellarAccountsChanged.fromJson(Map<String, dynamic> json) {
return StellarAccountsChanged(
accounts: (json["accounts"] as List).cast(),
defaultAddress: json["defaultAddress"],
connectInfo: StellarProviderConnectInfo.fromJson(json["connectInfo"]));
}
Map<String, dynamic> toJson() {
return {
"accounts": accounts,
"defaultAddress": defaultAddress,
"connectInfo": connectInfo.toJson()
};
}

JSAny? get accountJS => accounts.map((e) => e.toJS).toList().toJS;

@JSExport("toString")
@override
String toString() {
return "StellarAccountsChanged${toJson()}";
}
}

class StellarProviderConnectInfo {
@JSExport("passphrase")
final String passphrase;

StellarProviderConnectInfo(this.passphrase);
factory StellarProviderConnectInfo.fromJson(Map<String, dynamic> json) {
return StellarProviderConnectInfo(json["passphrase"]);
}
Map<String, dynamic> toJson() {
return {"passphrase": passphrase};
}

JSAny? get toJS => createJSInteropWrapper(this);
@JSExport("toString")
@override
String toString() {
return passphrase;
}
}
3 changes: 2 additions & 1 deletion mrt_wallet/js/js_wallet/models/models/requests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ enum JSClientType {
ethereum(tag: [151], networkName: "Ethereum"),
tron(tag: [152], networkName: "Tron"),
solana(tag: [153], networkName: "Solana"),
ton(tag: [154], networkName: "TON");
ton(tag: [154], networkName: "TON"),
stellar(tag: [155], networkName: "Stellar");

final List<int> tag;
final String networkName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class JSPageController {
final TronPageController tronPageController = TronPageController();
final SolanaPageController solanaPageController = SolanaPageController();
final TonPageController tonPageController = TonPageController();
final StellarPageController stellarPageController = StellarPageController();

void _onWalletEvent(CustomEvent response) {
try {
Expand All @@ -39,6 +40,9 @@ class JSPageController {
case JSClientType.ton:
tonPageController.onEvent(event);
break;
case JSClientType.stellar:
stellarPageController.onEvent(event);
break;
default:
break;
}
Expand Down
96 changes: 96 additions & 0 deletions mrt_wallet/js/js_wallet/page_script/networks/stellar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
part of '../scripts.dart';

class StellarPageController extends PageNetworkController {
StellarPageController();
ProxyMethodHandler<StellarWalletAdapter>? _stellar;
ProxyMethodHandler<StellarWalletAdapter> _createAdapter() {
final adapter = StellarWalletAdapter(JSObject());
adapter.enable = _requestAccount.toJS;
adapter.on = _addListener.toJS;
adapter.on = _addListener.toJS;

adapter.removeListener = _removeListener.toJS;
adapter.cancelListener = _removeListener.toJS;
adapter.sendWalletRequest = _onWalletRequest.toJS;
adapter.cancelAllListener = _cancelAllListeners.toJS;
return ProxyMethodHandler<StellarWalletAdapter>(adapter);
}

void _init() {
_stellar ??= _createAdapter();
final proxy = Proxy(_stellar!.object, createJSInteropWrapper(_stellar!));
stellar = proxy;
}

JSPromise<JSAny?> _requestAccount() {
final params = Web3JSRequestParams(method: "stellar_requestAccounts");
return _onWalletRequest(params);
}

void _disable(String? message) {
ton = null;
jsConsole.error(message);
}

void onEvent(WalletMessageEvent message) {
JSAny? eventData = message.data;
switch (message.eventType) {
case JSEventType.connect:
final chainChange =
StellarProviderConnectInfo.fromJson(message.asMap());
eventData = chainChange.passphrase.toJS;
break;
case JSEventType.chainChanged:
final chainChange =
StellarProviderConnectInfo.fromJson(message.asMap());
eventData = chainChange.toJS;
break;
case JSEventType.accountsChanged:
final chainChange = StellarAccountsChanged.fromJson(message.asMap());
eventData = chainChange.accountJS;
_stellar?.object.selectedAddress = chainChange.defaultAddress?.toJS;
break;
case JSEventType.disconnect:
_stellar?.object.selectedAddress = null;
break;
case JSEventType.disable:
_disable(message.asString());
return;
case JSEventType.active:
_init();
return;
default:
return;
}
_eventListeners(message.eventType, jsObject: eventData);
}

void _eventListeners(JSEventType type, {JSAny? jsObject}) {
if (!_listeners.containsKey(type)) return;
final listeners = <JSFunction>[..._listeners[type]!];
for (final i in listeners) {
i.callAsFunction(i, jsObject);
}
}

void _addListener(String type, JSFunction listener) {
final event = JSEventType.fromName(type);
if (event == null || !_listeners.containsKey(event)) return;
_listeners[event]?.add(listener);
_emitEvent(PageMessageEvent.build(event: event));
}

void _removeListener(String type, JSFunction listener) {
final event = JSEventType.fromName(type);
_listeners[event]?.remove(listener);
}

void _cancelAllListeners() {
for (final i in _listeners.keys.toList()) {
_listeners[i]!.clear();
}
}

@override
JSClientType get _client => JSClientType.stellar;
}
1 change: 1 addition & 0 deletions mrt_wallet/js/js_wallet/page_script/scripts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ part 'networks/tron.dart';
part 'controller/controller.dart';
part 'networks/solana.dart';
part 'networks/ton.dart';
part 'networks/stellar.dart';
5 changes: 4 additions & 1 deletion mrt_wallet/js/js_wallet/wallet/core/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import '../../utils/utils.dart';
import 'package:mrt_native_support/web/mrt_native_web.dart';
import '../networks/ethereum.dart';
import '../networks/solana.dart';
import '../networks/stellar.dart';
import '../networks/ton.dart';
import '../networks/tron.dart';
import 'network_handler.dart';
Expand All @@ -31,7 +32,9 @@ abstract class JSWalletHandler {
JSClientType.tron: JSTronHandler(sendMessageToClient: _sendEventToClient),
JSClientType.solana:
JSSolanaHandler(sendMessageToClient: _sendEventToClient),
JSClientType.ton: JSTonHandler(sendMessageToClient: _sendEventToClient)
JSClientType.ton: JSTonHandler(sendMessageToClient: _sendEventToClient),
JSClientType.stellar:
JSStellarHandler(sendMessageToClient: _sendEventToClient)
};

String get clientId;
Expand Down
Loading

0 comments on commit 033d74a

Please sign in to comment.