Fixed updating of transactions history. Added support for part formatted electrum server response

This commit is contained in:
M 2020-12-16 21:16:47 +02:00
parent db7ef6f777
commit 02ebc54a38
7 changed files with 152 additions and 85 deletions

View file

@ -348,8 +348,8 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
} }
@override @override
void close() { void close() async{
await eclient.close();
} }
void _subscribeForUpdates() { void _subscribeForUpdates() {
@ -357,8 +357,8 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
await _scripthashesUpdateSubject[sh]?.close(); await _scripthashesUpdateSubject[sh]?.close();
_scripthashesUpdateSubject[sh] = eclient.scripthashUpdate(sh); _scripthashesUpdateSubject[sh] = eclient.scripthashUpdate(sh);
_scripthashesUpdateSubject[sh].listen((event) async { _scripthashesUpdateSubject[sh].listen((event) async {
transactionHistory.updateAsync();
await _updateBalance(); await _updateBalance();
transactionHistory.updateAsync();
}); });
}); });
} }

View file

@ -22,8 +22,9 @@ String jsonrpcparams(List<Object> params) {
} }
String jsonrpc( String jsonrpc(
{String method, List<Object> params, int id, double version = 2.0}) => {String method, List<Object> params, int id, double version = 2.0}) =>
'{"jsonrpc": "$version", "method": "$method", "id": "$id", "params": ${json.encode(params)}}\n'; '{"jsonrpc": "$version", "method": "$method", "id": "$id", "params": ${json
.encode(params)}}\n';
class SocketTask { class SocketTask {
SocketTask({this.completer, this.isSubscription, this.subject}); SocketTask({this.completer, this.isSubscription, this.subject});
@ -49,6 +50,7 @@ class ElectrumClient {
final Map<String, SocketTask> _tasks; final Map<String, SocketTask> _tasks;
bool _isConnected; bool _isConnected;
Timer _aliveTimer; Timer _aliveTimer;
String unterminatedString;
Future<void> connectToUri(String uri) async { Future<void> connectToUri(String uri) async {
final splittedUri = uri.split(':'); final splittedUri = uri.split(':');
@ -73,19 +75,22 @@ class ElectrumClient {
socket.listen((Uint8List event) { socket.listen((Uint8List event) {
try { try {
final jsoned = _handleResponse(utf8.decode(event.toList()));
json.decode(utf8.decode(event.toList())) as Map<String, Object>; } on FormatException catch (e) {
// print(jsoned); final msg = e.message.toLowerCase();
final method = jsoned['method'];
final id = jsoned['id'] as String;
final result = jsoned['result'];
if (method is String) { if (msg == 'Unterminated string'.toLowerCase()) {
_methodHandler(method: method, request: jsoned); unterminatedString = e.source as String;
return;
} }
_finish(id, result); if (msg == 'Unexpected character'.toLowerCase()) {
unterminatedString += e.source as String;
}
if (isJSONStringCorrect(unterminatedString)) {
_handleResponse(unterminatedString);
unterminatedString = null;
}
} catch (e) { } catch (e) {
print(e); print(e);
} }
@ -148,7 +153,7 @@ class ElectrumClient {
}); });
Future<List<Map<String, dynamic>>> getListUnspentWithAddress( Future<List<Map<String, dynamic>>> getListUnspentWithAddress(
String address) => String address) =>
call( call(
method: 'blockchain.scripthash.listunspent', method: 'blockchain.scripthash.listunspent',
params: [scriptHash(address)]).then((dynamic result) { params: [scriptHash(address)]).then((dynamic result) {
@ -199,7 +204,7 @@ class ElectrumClient {
}); });
Future<Map<String, Object>> getTransactionRaw( Future<Map<String, Object>> getTransactionRaw(
{@required String hash}) async => {@required String hash}) async =>
call(method: 'blockchain.transaction.get', params: [hash, true]) call(method: 'blockchain.transaction.get', params: [hash, true])
.then((dynamic result) { .then((dynamic result) {
if (result is Map<String, Object>) { if (result is Map<String, Object>) {
@ -228,7 +233,7 @@ class ElectrumClient {
} }
Future<String> broadcastTransaction( Future<String> broadcastTransaction(
{@required String transactionRaw}) async => {@required String transactionRaw}) async =>
call(method: 'blockchain.transaction.broadcast', params: [transactionRaw]) call(method: 'blockchain.transaction.broadcast', params: [transactionRaw])
.then((dynamic result) { .then((dynamic result) {
if (result is String) { if (result is String) {
@ -239,14 +244,14 @@ class ElectrumClient {
}); });
Future<Map<String, dynamic>> getMerkle( Future<Map<String, dynamic>> getMerkle(
{@required String hash, @required int height}) async => {@required String hash, @required int height}) async =>
await call( await call(
method: 'blockchain.transaction.get_merkle', method: 'blockchain.transaction.get_merkle',
params: [hash, height]) as Map<String, dynamic>; params: [hash, height]) as Map<String, dynamic>;
Future<Map<String, dynamic>> getHeader({@required int height}) async => Future<Map<String, dynamic>> getHeader({@required int height}) async =>
await call(method: 'blockchain.block.get_header', params: [height]) await call(method: 'blockchain.block.get_header', params: [height])
as Map<String, dynamic>; as Map<String, dynamic>;
Future<double> estimatefee({@required int p}) => Future<double> estimatefee({@required int p}) =>
call(method: 'blockchain.estimatefee', params: [p]) call(method: 'blockchain.estimatefee', params: [p])
@ -270,10 +275,9 @@ class ElectrumClient {
params: [scripthash]); params: [scripthash]);
} }
BehaviorSubject<T> subscribe<T>( BehaviorSubject<T> subscribe<T>({@required String id,
{@required String id, @required String method,
@required String method, List<Object> params = const []}) {
List<Object> params = const []}) {
final subscription = BehaviorSubject<T>(); final subscription = BehaviorSubject<T>();
_regisrySubscription(id, subscription); _regisrySubscription(id, subscription);
socket.write(jsonrpc(method: method, id: _id, params: params)); socket.write(jsonrpc(method: method, id: _id, params: params));
@ -292,10 +296,9 @@ class ElectrumClient {
return completer.future; return completer.future;
} }
Future<dynamic> callWithTimeout( Future<dynamic> callWithTimeout({String method,
{String method, List<Object> params = const [],
List<Object> params = const [], int timeout = 2000}) async {
int timeout = 2000}) async {
final completer = Completer<dynamic>(); final completer = Completer<dynamic>();
_id += 1; _id += 1;
final id = _id; final id = _id;
@ -316,8 +319,15 @@ class ElectrumClient {
socket.write(jsonrpc(method: method, id: _id, params: params)); socket.write(jsonrpc(method: method, id: _id, params: params));
} }
void _regisryTask(int id, Completer completer) => _tasks[id.toString()] = Future<void> close() async {
SocketTask(completer: completer, isSubscription: false); _aliveTimer.cancel();
await socket.close();
onConnectionStatusChange = null;
}
void _regisryTask(int id, Completer completer) =>
_tasks[id.toString()] =
SocketTask(completer: completer, isSubscription: false);
void _regisrySubscription(String id, BehaviorSubject subject) => void _regisrySubscription(String id, BehaviorSubject subject) =>
_tasks[id] = SocketTask(subject: subject, isSubscription: true); _tasks[id] = SocketTask(subject: subject, isSubscription: true);
@ -360,6 +370,31 @@ class ElectrumClient {
_isConnected = isConnected; _isConnected = isConnected;
} }
void _handleResponse(String response) {
print('Response: $response');
final jsoned = json.decode(response) as Map<String, Object>;
// print(jsoned);
final method = jsoned['method'];
final id = jsoned['id'] as String;
final result = jsoned['result'];
if (method is String) {
_methodHandler(method: method, request: jsoned);
return;
}
_finish(id, result);
}
}
// FIXME: move me
bool isJSONStringCorrect(String source) {
try {
json.decode(source);
return true;
} catch (_) {
return false;
}
} }
class RequestFailedTimeoutException implements Exception { class RequestFailedTimeoutException implements Exception {

View file

@ -16,6 +16,7 @@ class PendingBitcoinTransaction with PendingTransaction {
final int amount; final int amount;
final int fee; final int fee;
@override
String get id => _tx.getId(); String get id => _tx.getId();
@override @override

View file

@ -9,44 +9,53 @@ import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
// FIXME: terrible design // FIXME: terrible design
class WalletMenu { class WalletMenu {
WalletMenu(this.context, this.reconnect); WalletMenu(this.context, this.reconnect, this.hasRescan) : items = [] {
items.addAll([
final List<WalletMenuItem> items = [ WalletMenuItem(
WalletMenuItem( title: S.current.reconnect,
title: S.current.reconnect, image: Image.asset('assets/images/reconnect_menu.png',
image: Image.asset('assets/images/reconnect_menu.png', height: 16, width: 16)),
height: 16, width: 16)), if (hasRescan)
WalletMenuItem( WalletMenuItem(
title: S.current.rescan, title: S.current.rescan,
image: Image.asset('assets/images/filter_icon.png', image: Image.asset('assets/images/filter_icon.png',
height: 16, width: 16)), height: 16, width: 16)),
WalletMenuItem( WalletMenuItem(
title: S.current.wallets, title: S.current.wallets,
image: Image.asset('assets/images/wallet_menu.png', image: Image.asset('assets/images/wallet_menu.png',
height: 16, width: 16)), height: 16, width: 16)),
WalletMenuItem( WalletMenuItem(
title: S.current.nodes, title: S.current.nodes,
image: image: Image.asset('assets/images/nodes_menu.png',
Image.asset('assets/images/nodes_menu.png', height: 16, width: 16)), height: 16, width: 16)),
WalletMenuItem( WalletMenuItem(
title: S.current.show_keys, title: S.current.show_keys,
image: image:
Image.asset('assets/images/key_menu.png', height: 16, width: 16)), Image.asset('assets/images/key_menu.png', height: 16, width: 16)),
WalletMenuItem( WalletMenuItem(
title: S.current.address_book_menu, title: S.current.address_book_menu,
image: Image.asset('assets/images/open_book_menu.png', image: Image.asset('assets/images/open_book_menu.png',
height: 16, width: 16)), height: 16, width: 16)),
WalletMenuItem( WalletMenuItem(
title: S.current.settings_title, title: S.current.settings_title,
image: Image.asset('assets/images/settings_menu.png', image: Image.asset('assets/images/settings_menu.png',
height: 16, width: 16)), height: 16, width: 16)),
]; ]);
}
final List<WalletMenuItem> items;
final BuildContext context; final BuildContext context;
final Future<void> Function() reconnect; final Future<void> Function() reconnect;
final bool hasRescan;
void action(int index) { void action(int index) {
switch (index) { var indx = index;
if (index > 0 && !hasRescan) {
indx += 1;
}
switch (indx) {
case 0: case 0:
_presentReconnectAlert(context); _presentReconnectAlert(context);
break; break;

View file

@ -66,8 +66,10 @@ class MenuWidgetState extends State<MenuWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final walletMenu = final walletMenu = WalletMenu(
WalletMenu(context, () async => widget.dashboardViewModel.reconnect()); context,
() async => widget.dashboardViewModel.reconnect(),
widget.dashboardViewModel.hasRescan);
final itemCount = walletMenu.items.length; final itemCount = walletMenu.items.length;
moneroIcon = Image.asset('assets/images/monero_menu.png', moneroIcon = Image.asset('assets/images/monero_menu.png',
@ -148,16 +150,19 @@ class MenuWidgetState extends State<MenuWidget> {
), ),
if (widget.dashboardViewModel.subname != if (widget.dashboardViewModel.subname !=
null) null)
Observer(builder: (_) => Text( Observer(
widget.dashboardViewModel.subname, builder: (_) => Text(
style: TextStyle( widget.dashboardViewModel
color: Theme.of(context) .subname,
.accentTextTheme style: TextStyle(
.overline color: Theme.of(context)
.decorationColor, .accentTextTheme
fontWeight: FontWeight.w500, .overline
fontSize: 12), .decorationColor,
)) fontWeight:
FontWeight.w500,
fontSize: 12),
))
], ],
), ),
)) ))

View file

@ -35,16 +35,6 @@ class TransactionDetailsPage extends BasePage {
value: tx.feeFormatted()) value: tx.feeFormatted())
]; ];
if (showRecipientAddress) {
final recipientAddress = transactionDescriptionBox.values.firstWhere((val) => val.id == transactionInfo.id, orElse: () => null)?.recipientAddress;
if (recipientAddress?.isNotEmpty ?? false) {
items.add(StandartListItem(
title: S.current.transaction_details_recipient_address,
value: recipientAddress));
}
}
if (tx.key?.isNotEmpty ?? null) { if (tx.key?.isNotEmpty ?? null) {
// FIXME: add translation // FIXME: add translation
items.add(StandartListItem(title: 'Transaction Key', value: tx.key)); items.add(StandartListItem(title: 'Transaction Key', value: tx.key));
@ -71,6 +61,16 @@ class TransactionDetailsPage extends BasePage {
_items.addAll(items); _items.addAll(items);
} }
if (showRecipientAddress) {
final recipientAddress = transactionDescriptionBox.values.firstWhere((val) => val.id == transactionInfo.id, orElse: () => null)?.recipientAddress;
if (recipientAddress?.isNotEmpty ?? false) {
_items.add(StandartListItem(
title: S.current.transaction_details_recipient_address,
value: recipientAddress));
}
}
} }
@override @override

View file

@ -186,6 +186,8 @@ abstract class DashboardViewModelBase with Store {
@observable @observable
WalletBase wallet; WalletBase wallet;
bool get hasRescan => wallet.type == WalletType.monero;
BalanceViewModel balanceViewModel; BalanceViewModel balanceViewModel;
AppStore appStore; AppStore appStore;
@ -237,6 +239,21 @@ abstract class DashboardViewModelBase with Store {
balanceViewModel: balanceViewModel, balanceViewModel: balanceViewModel,
settingsStore: appStore.settingsStore))); settingsStore: appStore.settingsStore)));
} }
connectMapToListWithTransform(
appStore.wallet.transactionHistory.transactions,
transactions,
(TransactionInfo val) => TransactionListItem(
transaction: val,
balanceViewModel: balanceViewModel,
settingsStore: appStore.settingsStore),
filter: (TransactionInfo tx) {
if (tx is MoneroTransactionInfo && wallet is MoneroWallet) {
return tx.accountIndex == wallet.account.id;
}
return true;
});
} }
@action @action