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';
|
2024-04-08 23:46:34 +00:00
|
|
|
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-08 23:46:34 +00:00
|
|
|
|
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,
|
2024-04-17 15:45:18 +00:00
|
|
|
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 {
|
2024-04-10 16:36:47 +00:00
|
|
|
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);
|
2024-04-10 16:36:47 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
state = FailureState(e.toString());
|
|
|
|
}
|
2024-04-09 18:52:52 +00:00
|
|
|
}
|
2024-04-08 23:46:34 +00:00
|
|
|
}
|