Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #69 from MXCzkEVM/sign_message_feat
Browse files Browse the repository at this point in the history
Sign message feat
  • Loading branch information
reasje authored Sep 26, 2023
2 parents feb65b2 + cd4dafa commit 7c778a0
Show file tree
Hide file tree
Showing 30 changed files with 345 additions and 54 deletions.
4 changes: 3 additions & 1 deletion assets/flutter_i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,7 @@
"network_not_found": "Network not found",
"checking_balance": "Checking balance...",
"no_balance": "You have no balance",
"no_balance_tip": "You currently have no MXC in your wallet.\nYou can either receive tokens or choose to claim a username later."
"no_balance_tip": "You currently have no MXC in your wallet.\nYou can either receive tokens or choose to claim a username later.",
"signature_request": "Signature request",
"sign": "Sign"
}
2 changes: 1 addition & 1 deletion lib/common/components/balance_panel/balance.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export 'presentation/balance_panel.dart';
export 'presentation/balance_panel.dart';
2 changes: 1 addition & 1 deletion lib/common/components/components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export 'recent_transactions/recent_transactions.dart';
export 'gradient_text.dart';
export 'grey_container.dart';
export 'balance_panel/balance.dart';
export 'snack_bar.dart';
export 'snack_bar.dart';
1 change: 0 additions & 1 deletion lib/common/components/recent_transactions/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,4 @@ class RecentTransactionsUtils {
);
}).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ class RecentTrxListItem extends HookConsumerWidget {
children: [
Text(
amount,
style: FontTheme.of(context).body1.primary().copyWith(
style: FontTheme.of(context)
.body1
.primary()
.copyWith(
fontWeight: FontWeight.w500,
foreground: state.hideBalance == true
? (Paint()
Expand All @@ -109,7 +112,8 @@ class RecentTrxListItem extends HookConsumerWidget {
style: FontTheme.of(context).h7().copyWith(
fontSize: 16,
fontWeight: FontWeight.w400,
color: ColorsTheme.of(context).textSecondary,
color:
ColorsTheme.of(context).textSecondary,
),
textAlign: TextAlign.start,
overflow: TextOverflow.ellipsis,
Expand Down
2 changes: 1 addition & 1 deletion lib/common/dialogs/dialogs.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export 'alert_dialog.dart';
export 'bottom_sheet.dart';
export 'warning_dialog.dart';
export 'warning_dialog.dart';
2 changes: 1 addition & 1 deletion lib/common/urls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Urls {
static const String mxcMainnetNftMarketPlace = 'https://nft.mxc.com/';
static const String mxcTestnetNftMarketPlace = 'https://wannsee-nft.mxc.com/';
static const String mxcStatus = 'https://mxc.instatus.com/';

static const String dappRoot =
'https://raw.githubusercontent.com/MXCzkEVM/MEP-1759-DApp-store/main';

Expand Down
7 changes: 5 additions & 2 deletions lib/common/utils/formatter.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dart:math';
import 'package:datadashwallet/common/config.dart';
import 'package:intl/intl.dart' as intl;
import 'package:flutter/material.dart';
import 'package:web3dart/web3dart.dart';

class Formatter {
static String formatBigNumber(double number) {
Expand Down Expand Up @@ -89,4 +87,9 @@ class Formatter {
throw e.toString();
}
}

static String capitalizeFirstLetter(String text) {
if (text.isEmpty) return text;
return text[0].toUpperCase() + text.substring(1);
}
}
5 changes: 5 additions & 0 deletions lib/features/common/contract/token_contract_use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ class TokenContractUseCase extends ReactiveUseCase {
data: data,
tokenAddress: tokenAddress);

String signTypedMessage({required String privateKey, required String data}) {
return _repository.tokenContract
.signTypedMessage(privateKey: privateKey, data: data);
}

Future<int> getChainId(String rpcUrl) async {
return await _repository.tokenContract.getChainId(rpcUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ class CardSizes {
child: child,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DAppIndicator extends StatelessWidget {
width: total * 10 < 100 ? null : 100,
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(
Radius.circular(50),
),
Expand Down
11 changes: 11 additions & 0 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,20 @@ class OpenAppPage extends HookConsumerWidget {
url: url);
break;
case EIP1193.signMessage:
break;
case EIP1193.signPersonalMessage:
break;
case EIP1193.signTypedMessage:
Map<String, dynamic> object = params["object"];
presenter.signTypedMessage(
object: object,
cancel: () {
controller?.cancel(id);
},
success: (idHash) {
controller?.sendResult(idHash, id);
},
);
break;
case EIP1193.addEthereumChain:
bool? result =
Expand Down
73 changes: 73 additions & 0 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:convert';
import 'package:datadashwallet/common/common.dart';
import 'package:datadashwallet/core/core.dart';
import 'package:datadashwallet/features/dapps/subfeatures/open_dapp/widgets/swtich_network_dialog.dart';
import 'package:datadashwallet/features/dapps/subfeatures/open_dapp/widgets/typed_message_dialog.dart';
import 'package:flutter/services.dart';
import 'package:mxc_logic/mxc_logic.dart';
import 'package:web3_provider/web3_provider.dart';
Expand Down Expand Up @@ -103,6 +105,21 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
}
}

String? _signTypedMessage(
String hexData,
) {
loading = true;
try {
final res = _tokenContractUseCase.signTypedMessage(
privateKey: state.account!.privateKey, data: hexData);
return res;
} catch (e, s) {
addError(e, s);
} finally {
loading = false;
}
}

void recordTransaction(String hash) {
final timeStamp = DateTime.now();
const txStatus = TransactionStatus.pending;
Expand Down Expand Up @@ -220,6 +237,42 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
}
}

void signPersonalMessage() {}

void signTypedMessage({
required Map<String, dynamic> object,
required VoidCallback cancel,
required Function(String hash) success,
}) async {
String hexData = object['raw'] as String;
Map<String, dynamic> data =
jsonDecode(object['raw'] as String) as Map<String, dynamic>;
Map<String, dynamic> domain = data['domain'] as Map<String, dynamic>;
String primaryType = data['primaryType'];
int chainId = (domain['chainId']) as int;
String name = domain['name'] as String;

try {
final result = await showTypedMessageDialog(context!,
title: translate('signature_request')!,
message: data['message'] as Map<String, dynamic>,
networkName: '$name ($chainId)',
primaryType: primaryType);

if (result != null && result) {
final hash = _signTypedMessage(
hexData,
);
if (hash != null) success.call(hash);
} else {
cancel.call();
}
} catch (e, s) {
cancel.call();
addError(e, s);
}
}

void changeProgress(int progress) => notify(() => state.progress = progress);

void setAddress(dynamic id) {
Expand Down Expand Up @@ -275,4 +328,24 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
},
);
}

void launchAddress(String address) {
final chainExplorerUrl = state.network!.explorerUrl!;
final explorerUrl = chainExplorerUrl.endsWith('/')
? chainExplorerUrl
: '$chainExplorerUrl/';

final addressUrl = '$explorerUrl${Config.addressExplorer(address)}';
state.webviewController!
.loadUrl(urlRequest: URLRequest(url: Uri.parse(addressUrl)));
}

bool isAddress(String address) {
try {
EthereumAddress.fromHex(address);
return true;
} catch (e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class AllowMultipleHorizontalDrag extends HorizontalDragGestureRecognizer {
void rejectGesture(int pointer) {
acceptGesture(pointer);
}

}

class AllowMultipleDoubleTap extends DoubleTapGestureRecognizer {
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:web3_provider/web3_provider.dart';

class BridgeParams {
Expand Down Expand Up @@ -26,8 +25,7 @@ class BridgeParams {
String? jsonPrice = json['gasPrice'];
gasPrice = jsonPrice == null
? null
: (int.parse(jsonPrice.replaceAll('0x', ''), radix: 16))
.toString();
: (int.parse(jsonPrice.replaceAll('0x', ''), radix: 16)).toString();

value = BigInt.tryParse((json['value'] ?? '0').replaceAll('0x', ''),
radix: 16) ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import 'package:mxc_ui/mxc_ui.dart';

import 'transaction_info.dart';

Future<bool?> showTransactionDialog(
BuildContext context, {
String? title,
required String amount,
required String from,
required String to,
String? estimatedFee,
VoidCallback? onTap,
required String symbol
}) {
Future<bool?> showTransactionDialog(BuildContext context,
{String? title,
required String amount,
required String from,
required String to,
String? estimatedFee,
VoidCallback? onTap,
required String symbol}) {
return showModalBottomSheet<bool>(
context: context,
useRootNavigator: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:datadashwallet/features/dapps/subfeatures/open_dapp/widgets/typed_message_info.dart';
import 'package:flutter/material.dart';
import 'package:mxc_ui/mxc_ui.dart';

Future<bool?> showTypedMessageDialog(
BuildContext context, {
String? title,
required String networkName,
required String primaryType,
required Map<String, dynamic> message,
VoidCallback? onTap,
}) {
return showModalBottomSheet<bool>(
context: context,
useRootNavigator: true,
isScrollControlled: true,
isDismissible: false,
backgroundColor: Colors.transparent,
builder: (BuildContext context) => Container(
padding: const EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 44),
decoration: BoxDecoration(
color: ColorsTheme.of(context).screenBackground,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
MxcAppBarEvenly.title(
titleText: title ?? '',
action: Container(
alignment: Alignment.centerRight,
child: InkWell(
child: const Icon(Icons.close),
onTap: () => Navigator.of(context).pop(false),
),
),
),
TypeMessageInfo(
message: message,
networkName: networkName,
primaryType: primaryType,
onTap: onTap,
),
const SizedBox(height: 10),
],
),
),
);
}
Loading

0 comments on commit 7c778a0

Please sign in to comment.