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

Commit

Permalink
feat: Add sign personal message
Browse files Browse the repository at this point in the history
  • Loading branch information
reasje committed Aug 8, 2024
1 parent d67d463 commit d77a9f0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/features/common/contract/token_contract_use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ class TokenContractUseCase extends ReactiveUseCase {
.getTokenTransferData(tokenHash, toAddress, amount);
}

String signPersonalMessage({required String privateKey, required String message}) {
return _repository.tokenContract
.signPersonalMessage(privateKey: privateKey, message: message);
}


String signMessage({required String privateKey, required String message}) {
return _repository.tokenContract
.signMessage(privateKey: privateKey, message: message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ class BridgeFunctionsHelper {
}
}

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

String? signTypedMessage(
String hexData,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class BridgeHelper {
}
}

/// This function is used for both sign message and sign personal message

void signMessage({
required Map<String, dynamic> object,
required VoidCallback cancel,
Expand Down Expand Up @@ -220,6 +220,39 @@ class BridgeHelper {
}
}


void signPersonalMessage({
required Map<String, dynamic> object,
required VoidCallback cancel,
required Function(String hash) success,
}) async {
final hexData = object['data'] as String;
String message = MXCType.hexToString(hexData);
int chainId = state.network!.chainId;
String name = state.network!.symbol;

try {
final result = await showSignMessageDialog(
context!,
title: translate('signature_request')!,
message: message,
networkName: '$name ($chainId)',
);

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

void signTypedMessage({
required Map<String, dynamic> object,
required VoidCallback cancel,
Expand Down
7 changes: 3 additions & 4 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ class OpenAppPage extends HookConsumerWidget {
presenter.changeProgress(progress);
presenter.setChain(null);
},
onUpdateVisitedHistory:
(controller, url, androidIsReload) {
onUpdateVisitedHistory: (controller, url, androidIsReload) {
presenter.updateCurrentUrl(url);
},
shouldOverrideUrlLoading: presenter.checkDeepLink,
Expand Down Expand Up @@ -137,8 +136,8 @@ class OpenAppPage extends HookConsumerWidget {
);
break;
case EIP1193.signPersonalMessage:
Map<String, dynamic> object = params["object"];
presenter.signMessage(
Map<String, dynamic> object = params["object"];
presenter.signPersonalMessage(
object: object,
cancel: () {
controller?.cancel(id);
Expand Down
11 changes: 11 additions & 0 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
success: success,
);

void signPersonalMessage({
required Map<String, dynamic> object,
required VoidCallback cancel,
required Function(String hash) success,
}) async =>
bridgeHelper.signPersonalMessage(
object: object,
cancel: cancel,
success: success,
);

void signTypedMessage({
required Map<String, dynamic> object,
required VoidCallback cancel,
Expand Down
2 changes: 1 addition & 1 deletion packages/shared

0 comments on commit d77a9f0

Please sign in to comment.