cake_wallet/lib/view_model/dashboard/sign_view_model.dart

56 lines
1.5 KiB
Dart
Raw Permalink Normal View History

2024-04-09 18:52:52 +00:00
import 'package:cake_wallet/core/execution_state.dart';
2024-04-10 21:39:35 +00:00
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:mobx/mobx.dart';
part 'sign_view_model.g.dart';
class SignViewModel = SignViewModelBase with _$SignViewModel;
abstract class SignViewModelBase with Store {
2024-04-09 18:52:52 +00:00
SignViewModelBase(this.wallet) : state = InitialExecutionState();
2024-04-09 18:52:52 +00:00
final WalletBase wallet;
@observable
ExecutionState state;
@observable
bool isSigning = true;
bool get signIncludesAddress => [
WalletType.monero,
2024-04-12 16:15:03 +00:00
WalletType.bitcoin,
WalletType.bitcoinCash,
WalletType.litecoin,
2024-04-09 18:52:52 +00:00
WalletType.haven,
].contains(wallet.type);
@action
Future<void> sign(String message, {String? address}) async {
state = IsExecutingState();
try {
final signature = await wallet.signMessage(message, address: address);
state = ExecutedSuccessfullyState(payload: signature);
} catch (e) {
state = FailureState(e.toString());
}
}
@action
Future<void> verify(String message, String signature, {String? address}) async {
state = IsExecutingState();
try {
final sig = await wallet.verifyMessage(message, signature, address: address);
if (sig) {
state = ExecutedSuccessfullyState();
} else {
2024-04-10 21:39:35 +00:00
state = FailureState(S.current.signature_invalid_error);
}
} catch (e) {
state = FailureState(e.toString());
}
2024-04-09 18:52:52 +00:00
}
}