2023-06-12 19:03:32 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-06-21 11:29:28 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/db/isar/main_db.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/models/balance.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/models/node_model.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/models/paymint/fee_object_model.dart';
|
|
|
|
import 'package:stackwallet/services/coins/coin_service.dart';
|
2023-12-04 21:45:33 +00:00
|
|
|
import 'package:stackwallet/services/coins/tezos/api/tezos_api.dart';
|
|
|
|
import 'package:stackwallet/services/coins/tezos/api/tezos_rpc_api.dart';
|
2023-08-14 22:53:44 +00:00
|
|
|
import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart';
|
2023-08-23 17:58:16 +00:00
|
|
|
import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart';
|
|
|
|
import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart';
|
2023-08-14 22:53:44 +00:00
|
|
|
import 'package:stackwallet/services/event_bus/global_event_bus.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/services/mixins/wallet_cache.dart';
|
|
|
|
import 'package:stackwallet/services/mixins/wallet_db.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/services/node_service.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/services/transaction_notification_tracker.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-08-14 22:53:44 +00:00
|
|
|
import 'package:stackwallet/utilities/constants.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/utilities/default_nodes.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
import 'package:stackwallet/utilities/prefs.dart';
|
2023-12-04 21:45:33 +00:00
|
|
|
import 'package:tezart/tezart.dart' as tezart;
|
2023-06-21 11:29:28 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
const int MINIMUM_CONFIRMATIONS = 1;
|
2023-08-16 12:46:21 +00:00
|
|
|
const int _gasLimit = 10200;
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|
|
|
TezosWallet({
|
|
|
|
required String walletId,
|
|
|
|
required String walletName,
|
|
|
|
required Coin coin,
|
|
|
|
required SecureStorageInterface secureStore,
|
|
|
|
required TransactionNotificationTracker tracker,
|
|
|
|
MainDB? mockableOverride,
|
|
|
|
}) {
|
|
|
|
txTracker = tracker;
|
|
|
|
_walletId = walletId;
|
|
|
|
_walletName = walletName;
|
|
|
|
_coin = coin;
|
|
|
|
_secureStore = secureStore;
|
|
|
|
initCache(walletId, coin);
|
|
|
|
initWalletDB(mockableOverride: mockableOverride);
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeModel? _xtzNode;
|
|
|
|
|
|
|
|
NodeModel getCurrentNode() {
|
2023-07-28 16:32:50 +00:00
|
|
|
return _xtzNode ??
|
|
|
|
NodeService(secureStorageInterface: _secureStore)
|
|
|
|
.getPrimaryNodeFor(coin: Coin.tezos) ??
|
|
|
|
DefaultNodes.getNodeFor(Coin.tezos);
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 21:45:33 +00:00
|
|
|
Future<tezart.Keystore> getKeystore() async {
|
|
|
|
return tezart.Keystore.fromMnemonic((await mnemonicString).toString());
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
@override
|
|
|
|
String get walletId => _walletId;
|
|
|
|
late String _walletId;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get walletName => _walletName;
|
|
|
|
late String _walletName;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set walletName(String name) => _walletName = name;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set isFavorite(bool markFavorite) {
|
|
|
|
_isFavorite = markFavorite;
|
|
|
|
updateCachedIsFavorite(markFavorite);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isFavorite => _isFavorite ??= getCachedIsFavorite();
|
|
|
|
bool? _isFavorite;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Coin get coin => _coin;
|
|
|
|
late Coin _coin;
|
|
|
|
|
|
|
|
late SecureStorageInterface _secureStore;
|
|
|
|
late final TransactionNotificationTracker txTracker;
|
|
|
|
final _prefs = Prefs.instance;
|
|
|
|
|
|
|
|
Timer? timer;
|
|
|
|
bool _shouldAutoSync = false;
|
2023-08-14 22:53:44 +00:00
|
|
|
Timer? _networkAliveTimer;
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool get shouldAutoSync => _shouldAutoSync;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set shouldAutoSync(bool shouldAutoSync) {
|
|
|
|
if (_shouldAutoSync != shouldAutoSync) {
|
|
|
|
_shouldAutoSync = shouldAutoSync;
|
|
|
|
if (!shouldAutoSync) {
|
|
|
|
timer?.cancel();
|
|
|
|
timer = null;
|
2023-08-14 22:53:44 +00:00
|
|
|
stopNetworkAlivePinging();
|
2023-06-12 19:03:32 +00:00
|
|
|
} else {
|
2023-08-14 22:53:44 +00:00
|
|
|
startNetworkAlivePinging();
|
2023-06-12 19:03:32 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:53:44 +00:00
|
|
|
void startNetworkAlivePinging() {
|
|
|
|
// call once on start right away
|
|
|
|
_periodicPingCheck();
|
|
|
|
|
|
|
|
// then periodically check
|
|
|
|
_networkAliveTimer = Timer.periodic(
|
|
|
|
Constants.networkAliveTimerDuration,
|
|
|
|
(_) async {
|
|
|
|
_periodicPingCheck();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopNetworkAlivePinging() {
|
|
|
|
_networkAliveTimer?.cancel();
|
|
|
|
_networkAliveTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _periodicPingCheck() async {
|
|
|
|
bool hasNetwork = await testNetworkConnection();
|
|
|
|
|
|
|
|
if (_isConnected != hasNetwork) {
|
|
|
|
NodeConnectionStatus status = hasNetwork
|
|
|
|
? NodeConnectionStatus.connected
|
|
|
|
: NodeConnectionStatus.disconnected;
|
|
|
|
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
NodeConnectionStatusChangedEvent(
|
|
|
|
status,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
_isConnected = hasNetwork;
|
|
|
|
if (hasNetwork) {
|
|
|
|
unawaited(refresh());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
@override
|
|
|
|
Balance get balance => _balance ??= getCachedBalance();
|
|
|
|
Balance? _balance;
|
|
|
|
|
2023-12-04 21:45:33 +00:00
|
|
|
Future<tezart.OperationsList> _buildSendTransaction({
|
|
|
|
required Amount amount,
|
|
|
|
required String address,
|
|
|
|
required int counter,
|
|
|
|
}) async {
|
|
|
|
try {
|
|
|
|
final sourceKeyStore = await getKeystore();
|
|
|
|
final server = (_xtzNode ?? getCurrentNode()).host;
|
|
|
|
final tezartClient = tezart.TezartClient(
|
|
|
|
server,
|
|
|
|
);
|
|
|
|
|
|
|
|
final opList = await tezartClient.transferOperation(
|
|
|
|
source: sourceKeyStore,
|
|
|
|
destination: address,
|
|
|
|
amount: amount.raw.toInt(),
|
|
|
|
);
|
|
|
|
|
|
|
|
for (final op in opList.operations) {
|
|
|
|
op.counter = counter;
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return opList;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Error in _buildSendTransaction() in tezos_wallet.dart: $e\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
@override
|
2023-12-04 21:45:33 +00:00
|
|
|
Future<Map<String, dynamic>> prepareSend({
|
|
|
|
required String address,
|
|
|
|
required Amount amount,
|
|
|
|
Map<String, dynamic>? args,
|
|
|
|
}) async {
|
2023-06-21 11:29:28 +00:00
|
|
|
try {
|
|
|
|
if (amount.decimals != coin.decimals) {
|
|
|
|
throw Exception("Amount decimals do not match coin decimals!");
|
|
|
|
}
|
2023-12-04 21:45:33 +00:00
|
|
|
|
|
|
|
if (amount > balance.spendable) {
|
|
|
|
throw Exception("Insufficient available balance");
|
|
|
|
}
|
|
|
|
|
|
|
|
final myAddress = await currentReceivingAddress;
|
|
|
|
final account = await TezosAPI.getAccount(
|
|
|
|
myAddress,
|
|
|
|
);
|
|
|
|
|
|
|
|
final opList = await _buildSendTransaction(
|
|
|
|
amount: amount,
|
|
|
|
address: address,
|
|
|
|
counter: account.counter + 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
await opList.computeLimits();
|
|
|
|
await opList.computeFees();
|
|
|
|
await opList.simulate();
|
|
|
|
|
2023-06-21 11:29:28 +00:00
|
|
|
Map<String, dynamic> txData = {
|
2023-12-04 21:45:33 +00:00
|
|
|
"fee": Amount(
|
|
|
|
rawValue: opList.operations
|
|
|
|
.map(
|
|
|
|
(e) => BigInt.from(e.fee),
|
|
|
|
)
|
|
|
|
.fold(
|
|
|
|
BigInt.zero,
|
|
|
|
(p, e) => p + e,
|
|
|
|
),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
).raw.toInt(),
|
2023-06-21 11:29:28 +00:00
|
|
|
"address": address,
|
|
|
|
"recipientAmt": amount,
|
2023-12-04 21:45:33 +00:00
|
|
|
"tezosOperationsList": opList,
|
2023-06-21 11:29:28 +00:00
|
|
|
};
|
2023-12-04 21:45:33 +00:00
|
|
|
return txData;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Error in prepareSend() in tezos_wallet.dart: $e\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (e
|
|
|
|
.toString()
|
|
|
|
.contains("(_operationResult['errors']): Must not be null")) {
|
|
|
|
throw Exception("Probably insufficient balance");
|
|
|
|
} else if (e.toString().contains(
|
|
|
|
"The simulation of the operation: \"transaction\" failed with error(s) :"
|
|
|
|
" contract.balance_too_low, tez.subtraction_underflow.",
|
|
|
|
)) {
|
|
|
|
throw Exception("Insufficient balance to pay fees");
|
|
|
|
}
|
|
|
|
|
|
|
|
rethrow;
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> confirmSend({required Map<String, dynamic> txData}) async {
|
|
|
|
try {
|
2023-12-04 21:45:33 +00:00
|
|
|
final opList = txData["tezosOperationsList"] as tezart.OperationsList;
|
|
|
|
await opList.inject();
|
|
|
|
await opList.monitor();
|
|
|
|
return opList.result.id!;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("ConfirmSend: $e\n$s", level: LogLevel.Error);
|
|
|
|
rethrow;
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> get currentReceivingAddress async {
|
|
|
|
var mneString = await mnemonicString;
|
|
|
|
if (mneString == null) {
|
|
|
|
throw Exception("No mnemonic found!");
|
|
|
|
}
|
2023-12-04 21:45:33 +00:00
|
|
|
return Future.value((tezart.Keystore.fromMnemonic(mneString)).address);
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-05 21:16:19 +00:00
|
|
|
Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
|
2023-08-14 22:53:44 +00:00
|
|
|
return Amount(
|
2023-12-04 21:45:33 +00:00
|
|
|
rawValue: BigInt.from(0),
|
2023-08-16 13:17:58 +00:00
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
);
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> exit() {
|
|
|
|
_hasCalledExit = true;
|
|
|
|
return Future.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-06-21 11:29:28 +00:00
|
|
|
Future<FeeObject> get fees async {
|
2023-12-04 21:45:33 +00:00
|
|
|
int feePerTx = 0;
|
2023-06-21 11:29:28 +00:00
|
|
|
return FeeObject(
|
2023-08-16 13:17:58 +00:00
|
|
|
numberOfBlocksFast: 10,
|
2023-08-05 21:16:19 +00:00
|
|
|
numberOfBlocksAverage: 10,
|
2023-08-16 13:17:58 +00:00
|
|
|
numberOfBlocksSlow: 10,
|
|
|
|
fast: feePerTx,
|
2023-08-05 21:16:19 +00:00
|
|
|
medium: feePerTx,
|
2023-08-16 13:17:58 +00:00
|
|
|
slow: feePerTx,
|
2023-06-21 11:29:28 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> generateNewAddress() {
|
|
|
|
// TODO: implement generateNewAddress
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get hasCalledExit => _hasCalledExit;
|
|
|
|
bool _hasCalledExit = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> initializeExisting() async {
|
|
|
|
await _prefs.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-23 17:08:32 +00:00
|
|
|
Future<void> initializeNew(
|
|
|
|
({String mnemonicPassphrase, int wordCount})? data,
|
|
|
|
) async {
|
2023-08-05 21:16:19 +00:00
|
|
|
if ((await mnemonicString) != null || (await mnemonicPassphrase) != null) {
|
|
|
|
throw Exception(
|
|
|
|
"Attempted to overwrite mnemonic on generate new wallet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
await _prefs.init();
|
|
|
|
|
2023-12-04 21:45:33 +00:00
|
|
|
var newKeystore = tezart.Keystore.random();
|
2023-06-12 19:03:32 +00:00
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonic',
|
|
|
|
value: newKeystore.mnemonic,
|
|
|
|
);
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonicPassphrase',
|
|
|
|
value: "",
|
|
|
|
);
|
|
|
|
|
|
|
|
final address = Address(
|
2023-07-28 16:32:50 +00:00
|
|
|
walletId: walletId,
|
|
|
|
value: newKeystore.address,
|
2023-08-23 20:58:22 +00:00
|
|
|
publicKey: [],
|
2023-07-28 16:32:50 +00:00
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: null,
|
|
|
|
type: AddressType.unknown,
|
2023-08-23 20:58:22 +00:00
|
|
|
subType: AddressSubType.receiving,
|
2023-06-12 19:03:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await db.putAddress(address);
|
|
|
|
|
|
|
|
await Future.wait([
|
|
|
|
updateCachedId(walletId),
|
|
|
|
updateCachedIsFavorite(false),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isConnected => _isConnected;
|
|
|
|
bool _isConnected = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isRefreshing => refreshMutex;
|
|
|
|
bool refreshMutex = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
// TODO: implement maxFee
|
|
|
|
Future<int> get maxFee => throw UnimplementedError();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<String>> get mnemonic async {
|
|
|
|
final mnemonic = await mnemonicString;
|
|
|
|
final mnemonicPassphrase = await this.mnemonicPassphrase;
|
|
|
|
if (mnemonic == null) {
|
|
|
|
throw Exception("No mnemonic found!");
|
|
|
|
}
|
|
|
|
if (mnemonicPassphrase == null) {
|
|
|
|
throw Exception("No mnemonic passphrase found!");
|
|
|
|
}
|
|
|
|
return mnemonic.split(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<String?> get mnemonicPassphrase =>
|
|
|
|
_secureStore.read(key: '${_walletId}_mnemonicPassphrase');
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<String?> get mnemonicString =>
|
|
|
|
_secureStore.read(key: '${_walletId}_mnemonic');
|
2023-06-12 19:03:32 +00:00
|
|
|
|
2023-08-23 20:58:22 +00:00
|
|
|
Future<void> _recoverWalletFromSeedPhrase({
|
|
|
|
required String mnemonic,
|
|
|
|
required String mnemonicPassphrase,
|
|
|
|
bool isRescan = false,
|
|
|
|
}) async {
|
2023-12-04 21:45:33 +00:00
|
|
|
final keystore = tezart.Keystore.fromMnemonic(
|
2023-08-23 20:58:22 +00:00
|
|
|
mnemonic,
|
|
|
|
password: mnemonicPassphrase,
|
2023-06-12 19:03:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
final address = Address(
|
2023-07-28 16:32:50 +00:00
|
|
|
walletId: walletId,
|
2023-08-23 20:58:22 +00:00
|
|
|
value: keystore.address,
|
|
|
|
publicKey: [],
|
2023-07-28 16:32:50 +00:00
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: null,
|
|
|
|
type: AddressType.unknown,
|
2023-08-23 20:58:22 +00:00
|
|
|
subType: AddressSubType.receiving,
|
2023-06-12 19:03:32 +00:00
|
|
|
);
|
|
|
|
|
2023-08-23 20:58:22 +00:00
|
|
|
if (isRescan) {
|
|
|
|
await db.updateOrPutAddresses([address]);
|
|
|
|
} else {
|
|
|
|
await db.putAddress(address);
|
|
|
|
}
|
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
|
2023-08-23 20:58:22 +00:00
|
|
|
bool longMutex = false;
|
|
|
|
@override
|
|
|
|
Future<void> fullRescan(
|
|
|
|
int maxUnusedAddressGap,
|
|
|
|
int maxNumberOfIndexesToCheck,
|
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
Logging.instance.log("Starting full rescan!", level: LogLevel.Info);
|
|
|
|
longMutex = true;
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
WalletSyncStatusChangedEvent(
|
|
|
|
WalletSyncStatus.syncing,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final _mnemonic = await mnemonicString;
|
|
|
|
final _mnemonicPassphrase = await mnemonicPassphrase;
|
|
|
|
|
|
|
|
await db.deleteWalletBlockchainData(walletId);
|
|
|
|
|
|
|
|
await _recoverWalletFromSeedPhrase(
|
|
|
|
mnemonic: _mnemonic!,
|
|
|
|
mnemonicPassphrase: _mnemonicPassphrase!,
|
|
|
|
isRescan: true,
|
|
|
|
);
|
|
|
|
|
|
|
|
await refresh();
|
|
|
|
Logging.instance.log("Full rescan complete!", level: LogLevel.Info);
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
WalletSyncStatusChangedEvent(
|
|
|
|
WalletSyncStatus.synced,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} catch (e, s) {
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
WalletSyncStatusChangedEvent(
|
|
|
|
WalletSyncStatus.unableToSync,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
Logging.instance.log(
|
|
|
|
"Exception rethrown from fullRescan(): $e\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
} finally {
|
|
|
|
longMutex = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> recoverFromMnemonic({
|
|
|
|
required String mnemonic,
|
|
|
|
String? mnemonicPassphrase,
|
|
|
|
required int maxUnusedAddressGap,
|
|
|
|
required int maxNumberOfIndexesToCheck,
|
|
|
|
required int height,
|
|
|
|
}) async {
|
|
|
|
longMutex = true;
|
|
|
|
try {
|
|
|
|
if ((await mnemonicString) != null ||
|
|
|
|
(await this.mnemonicPassphrase) != null) {
|
|
|
|
throw Exception("Attempted to overwrite mnemonic on restore!");
|
|
|
|
}
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonic', value: mnemonic.trim());
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonicPassphrase',
|
|
|
|
value: mnemonicPassphrase ?? "",
|
|
|
|
);
|
|
|
|
|
|
|
|
await _recoverWalletFromSeedPhrase(
|
|
|
|
mnemonic: mnemonic,
|
|
|
|
mnemonicPassphrase: mnemonicPassphrase ?? "",
|
|
|
|
isRescan: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
await Future.wait([
|
|
|
|
updateCachedId(walletId),
|
|
|
|
updateCachedIsFavorite(false),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await refresh();
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Exception rethrown from recoverFromMnemonic(): $e\n$s",
|
|
|
|
level: LogLevel.Error);
|
|
|
|
|
|
|
|
rethrow;
|
|
|
|
} finally {
|
|
|
|
longMutex = false;
|
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateBalance() async {
|
2023-08-14 14:36:27 +00:00
|
|
|
try {
|
2023-12-04 21:45:33 +00:00
|
|
|
final node = getCurrentNode();
|
|
|
|
final bal = await TezosRpcAPI.getBalance(
|
|
|
|
address: await currentReceivingAddress,
|
|
|
|
nodeInfo: (
|
|
|
|
host: node.host,
|
|
|
|
port: node.port,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
Amount balanceInAmount =
|
|
|
|
Amount(rawValue: bal ?? BigInt.zero, fractionDigits: coin.decimals);
|
2023-08-14 14:36:27 +00:00
|
|
|
_balance = Balance(
|
|
|
|
total: balanceInAmount,
|
|
|
|
spendable: balanceInAmount,
|
2023-08-14 22:53:44 +00:00
|
|
|
blockedTotal:
|
|
|
|
Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
|
|
|
|
pendingSpendable:
|
|
|
|
Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
|
2023-08-14 14:36:27 +00:00
|
|
|
);
|
|
|
|
await updateCachedBalance(_balance!);
|
|
|
|
} catch (e, s) {
|
2023-08-14 22:53:44 +00:00
|
|
|
Logging.instance
|
|
|
|
.log("ERROR GETTING BALANCE ${e.toString()}", level: LogLevel.Error);
|
2023-08-14 14:36:27 +00:00
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 11:29:28 +00:00
|
|
|
Future<void> updateTransactions() async {
|
2023-12-04 21:45:33 +00:00
|
|
|
final txns = await TezosAPI.getTransactions(await currentReceivingAddress);
|
2023-06-21 11:29:28 +00:00
|
|
|
List<Tuple2<Transaction, Address>> txs = [];
|
2023-12-04 21:45:33 +00:00
|
|
|
for (var tx in txns) {
|
|
|
|
if (tx.type == "transaction") {
|
2023-08-15 14:49:28 +00:00
|
|
|
TransactionType txType;
|
2023-08-23 20:58:22 +00:00
|
|
|
final String myAddress = await currentReceivingAddress;
|
2023-12-04 21:45:33 +00:00
|
|
|
final String senderAddress = tx.senderAddress;
|
|
|
|
final String targetAddress = tx.receiverAddress;
|
2023-08-23 20:58:22 +00:00
|
|
|
if (senderAddress == myAddress && targetAddress == myAddress) {
|
|
|
|
txType = TransactionType.sentToSelf;
|
|
|
|
} else if (senderAddress == myAddress) {
|
2023-08-15 14:49:28 +00:00
|
|
|
txType = TransactionType.outgoing;
|
2023-08-23 20:58:22 +00:00
|
|
|
} else if (targetAddress == myAddress) {
|
2023-08-15 14:49:28 +00:00
|
|
|
txType = TransactionType.incoming;
|
2023-08-23 20:58:22 +00:00
|
|
|
} else {
|
|
|
|
txType = TransactionType.unknown;
|
2023-07-28 19:16:04 +00:00
|
|
|
}
|
2023-08-15 14:49:28 +00:00
|
|
|
|
|
|
|
var theTx = Transaction(
|
|
|
|
walletId: walletId,
|
2023-12-04 21:45:33 +00:00
|
|
|
txid: tx.hash,
|
|
|
|
timestamp: tx.timestamp,
|
2023-08-15 14:49:28 +00:00
|
|
|
type: txType,
|
|
|
|
subType: TransactionSubType.none,
|
2023-12-04 21:45:33 +00:00
|
|
|
amount: tx.amountInMicroTez,
|
2023-08-15 14:49:28 +00:00
|
|
|
amountString: Amount(
|
2023-12-04 21:45:33 +00:00
|
|
|
rawValue: BigInt.from(tx.amountInMicroTez),
|
2023-08-23 17:08:32 +00:00
|
|
|
fractionDigits: coin.decimals)
|
2023-08-15 14:49:28 +00:00
|
|
|
.toJsonString(),
|
2023-12-04 21:45:33 +00:00
|
|
|
fee: tx.feeInMicroTez,
|
|
|
|
height: tx.height,
|
2023-08-15 14:49:28 +00:00
|
|
|
isCancelled: false,
|
|
|
|
isLelantus: false,
|
|
|
|
slateId: "",
|
|
|
|
otherData: "",
|
|
|
|
inputs: [],
|
|
|
|
outputs: [],
|
|
|
|
nonce: 0,
|
|
|
|
numberOfMessages: null,
|
|
|
|
);
|
2023-08-23 20:58:22 +00:00
|
|
|
final AddressSubType subType;
|
|
|
|
switch (txType) {
|
|
|
|
case TransactionType.incoming:
|
|
|
|
case TransactionType.sentToSelf:
|
|
|
|
subType = AddressSubType.receiving;
|
|
|
|
break;
|
|
|
|
case TransactionType.outgoing:
|
|
|
|
case TransactionType.unknown:
|
|
|
|
subType = AddressSubType.unknown;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
final theAddress = Address(
|
2023-08-15 14:49:28 +00:00
|
|
|
walletId: walletId,
|
2023-08-23 20:58:22 +00:00
|
|
|
value: targetAddress,
|
|
|
|
publicKey: [],
|
2023-08-15 14:49:28 +00:00
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: null,
|
|
|
|
type: AddressType.unknown,
|
2023-08-23 20:58:22 +00:00
|
|
|
subType: subType,
|
2023-08-15 14:49:28 +00:00
|
|
|
);
|
|
|
|
txs.add(Tuple2(theTx, theAddress));
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-28 19:16:04 +00:00
|
|
|
Logging.instance.log("Transactions: $txs", level: LogLevel.Info);
|
2023-07-28 10:39:07 +00:00
|
|
|
await db.addNewTransactionData(txs, walletId);
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateChainHeight() async {
|
2023-08-14 14:36:27 +00:00
|
|
|
try {
|
2023-12-04 21:45:33 +00:00
|
|
|
final node = getCurrentNode();
|
|
|
|
final int intHeight = (await TezosRpcAPI.getChainHeight(
|
|
|
|
nodeInfo: (
|
|
|
|
host: node.host,
|
|
|
|
port: node.port,
|
|
|
|
),
|
|
|
|
))!;
|
2023-08-14 14:36:27 +00:00
|
|
|
Logging.instance.log("Chain height: $intHeight", level: LogLevel.Info);
|
|
|
|
await updateCachedChainHeight(intHeight);
|
|
|
|
} catch (e, s) {
|
2023-08-14 22:53:44 +00:00
|
|
|
Logging.instance
|
|
|
|
.log("GET CHAIN HEIGHT ERROR ${e.toString()}", level: LogLevel.Error);
|
2023-08-14 14:36:27 +00:00
|
|
|
}
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
@override
|
2023-08-23 17:58:16 +00:00
|
|
|
Future<void> refresh() async {
|
|
|
|
if (refreshMutex) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"$walletId $walletName refreshMutex denied",
|
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
refreshMutex = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
WalletSyncStatusChangedEvent(
|
|
|
|
WalletSyncStatus.syncing,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await updateChainHeight();
|
|
|
|
await updateBalance();
|
|
|
|
await updateTransactions();
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
WalletSyncStatusChangedEvent(
|
|
|
|
WalletSyncStatus.synced,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (shouldAutoSync) {
|
|
|
|
timer ??= Timer.periodic(const Duration(seconds: 30), (timer) async {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Periodic refresh check for $walletId $walletName in object instance: $hashCode",
|
|
|
|
level: LogLevel.Info);
|
|
|
|
|
|
|
|
await refresh();
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
UpdatedInBackgroundEvent(
|
|
|
|
"New data found in $walletId $walletName in background!",
|
|
|
|
walletId,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Failed to refresh stellar wallet $walletId: '$walletName': $e\n$s",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
WalletSyncStatusChangedEvent(
|
|
|
|
WalletSyncStatus.unableToSync,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshMutex = false;
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get storedChainHeight => getCachedChainHeight();
|
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<bool> testNetworkConnection() async {
|
2023-06-21 11:29:28 +00:00
|
|
|
try {
|
2023-12-04 21:45:33 +00:00
|
|
|
final node = getCurrentNode();
|
|
|
|
return await TezosRpcAPI.testNetworkConnection(
|
|
|
|
nodeInfo: (
|
|
|
|
host: node.host,
|
|
|
|
port: node.port,
|
|
|
|
),
|
2023-09-08 22:53:09 +00:00
|
|
|
);
|
2023-06-21 11:29:28 +00:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<List<Transaction>> get transactions =>
|
|
|
|
db.getTransactions(walletId).findAll();
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> updateNode(bool shouldRefresh) async {
|
2023-07-28 16:32:50 +00:00
|
|
|
_xtzNode = NodeService(secureStorageInterface: _secureStore)
|
|
|
|
.getPrimaryNodeFor(coin: coin) ??
|
|
|
|
DefaultNodes.getNodeFor(coin);
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
if (shouldRefresh) {
|
|
|
|
await refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-16 12:46:21 +00:00
|
|
|
Future<void> updateSentCachedTxData(Map<String, dynamic> txData) async {
|
2023-12-04 21:45:33 +00:00
|
|
|
// do nothing
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
// TODO: implement utxos
|
|
|
|
Future<List<UTXO>> get utxos => throw UnimplementedError();
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool validateAddress(String address) {
|
|
|
|
return RegExp(r"^tz[1-9A-HJ-NP-Za-km-z]{34}$").hasMatch(address);
|
|
|
|
}
|
|
|
|
}
|