From 812169600ff2925281bd74a24d08c6cd949d2ac7 Mon Sep 17 00:00:00 2001 From: vjrj <vjrj@comunes.org> Date: Wed, 3 May 2023 00:25:16 +0200 Subject: [PATCH] Remove old from/to pubkeys in txs --- lib/data/models/transaction.dart | 30 +--------- lib/data/models/transaction.g.dart | 78 -------------------------- lib/data/models/transaction_cubit.dart | 5 +- lib/g1/transaction_parser.dart | 4 -- test/transactions_test.dart | 32 ++++++----- 5 files changed, 22 insertions(+), 127 deletions(-) diff --git a/lib/data/models/transaction.dart b/lib/data/models/transaction.dart index 73d8bf43..9299d25f 100644 --- a/lib/data/models/transaction.dart +++ b/lib/data/models/transaction.dart @@ -1,11 +1,8 @@ -import 'dart:typed_data'; - import 'package:copy_with_extension/copy_with_extension.dart'; import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; import 'contact.dart'; -import 'model_utils.dart'; import 'transaction_type.dart'; part 'transaction.g.dart'; @@ -15,36 +12,22 @@ part 'transaction.g.dart'; class Transaction extends Equatable { const Transaction({ required this.type, - required this.from, - required this.to, required this.amount, required this.comment, required this.time, required this.fromC, required this.toC, - this.toAvatar, - this.toNick, - this.fromAvatar, - this.fromNick, }); factory Transaction.fromJson(Map<String, dynamic> json) => _$TransactionFromJson(json); final TransactionType type; - final String from; - final String to; final Contact fromC; final Contact toC; final double amount; final String comment; final DateTime time; - @JsonKey(fromJson: uIntFromList, toJson: uIntToList) - final Uint8List? toAvatar; - final String? toNick; - @JsonKey(fromJson: uIntFromList, toJson: uIntToList) - final Uint8List? fromAvatar; - final String? fromNick; bool get isOutgoing => type == TransactionType.sending || type == TransactionType.sent; @@ -55,16 +38,5 @@ class Transaction extends Equatable { Map<String, dynamic> toJson() => _$TransactionToJson(this); @override - List<Object?> get props => <dynamic>[ - type, - from, - to, - amount, - comment, - time, - toAvatar, - toNick, - fromAvatar, - fromNick - ]; + List<Object?> get props => <dynamic>[type, fromC, toC, amount, comment, time]; } diff --git a/lib/data/models/transaction.g.dart b/lib/data/models/transaction.g.dart index 3f060cac..5b935b62 100644 --- a/lib/data/models/transaction.g.dart +++ b/lib/data/models/transaction.g.dart @@ -9,10 +9,6 @@ part of 'transaction.dart'; abstract class _$TransactionCWProxy { Transaction type(TransactionType type); - Transaction from(String from); - - Transaction to(String to); - Transaction amount(double amount); Transaction comment(String comment); @@ -23,14 +19,6 @@ abstract class _$TransactionCWProxy { Transaction toC(Contact toC); - Transaction toAvatar(Uint8List? toAvatar); - - Transaction toNick(String? toNick); - - Transaction fromAvatar(Uint8List? fromAvatar); - - Transaction fromNick(String? fromNick); - /// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `Transaction(...).copyWith.fieldName(...)` to override fields one at a time with nullification support. /// /// Usage @@ -39,17 +27,11 @@ abstract class _$TransactionCWProxy { /// ```` Transaction call({ TransactionType? type, - String? from, - String? to, double? amount, String? comment, DateTime? time, Contact? fromC, Contact? toC, - Uint8List? toAvatar, - String? toNick, - Uint8List? fromAvatar, - String? fromNick, }); } @@ -62,12 +44,6 @@ class _$TransactionCWProxyImpl implements _$TransactionCWProxy { @override Transaction type(TransactionType type) => this(type: type); - @override - Transaction from(String from) => this(from: from); - - @override - Transaction to(String to) => this(to: to); - @override Transaction amount(double amount) => this(amount: amount); @@ -83,18 +59,6 @@ class _$TransactionCWProxyImpl implements _$TransactionCWProxy { @override Transaction toC(Contact toC) => this(toC: toC); - @override - Transaction toAvatar(Uint8List? toAvatar) => this(toAvatar: toAvatar); - - @override - Transaction toNick(String? toNick) => this(toNick: toNick); - - @override - Transaction fromAvatar(Uint8List? fromAvatar) => this(fromAvatar: fromAvatar); - - @override - Transaction fromNick(String? fromNick) => this(fromNick: fromNick); - @override /// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `Transaction(...).copyWith.fieldName(...)` to override fields one at a time with nullification support. @@ -105,31 +69,17 @@ class _$TransactionCWProxyImpl implements _$TransactionCWProxy { /// ```` Transaction call({ Object? type = const $CopyWithPlaceholder(), - Object? from = const $CopyWithPlaceholder(), - Object? to = const $CopyWithPlaceholder(), Object? amount = const $CopyWithPlaceholder(), Object? comment = const $CopyWithPlaceholder(), Object? time = const $CopyWithPlaceholder(), Object? fromC = const $CopyWithPlaceholder(), Object? toC = const $CopyWithPlaceholder(), - Object? toAvatar = const $CopyWithPlaceholder(), - Object? toNick = const $CopyWithPlaceholder(), - Object? fromAvatar = const $CopyWithPlaceholder(), - Object? fromNick = const $CopyWithPlaceholder(), }) { return Transaction( type: type == const $CopyWithPlaceholder() || type == null ? _value.type // ignore: cast_nullable_to_non_nullable : type as TransactionType, - from: from == const $CopyWithPlaceholder() || from == null - ? _value.from - // ignore: cast_nullable_to_non_nullable - : from as String, - to: to == const $CopyWithPlaceholder() || to == null - ? _value.to - // ignore: cast_nullable_to_non_nullable - : to as String, amount: amount == const $CopyWithPlaceholder() || amount == null ? _value.amount // ignore: cast_nullable_to_non_nullable @@ -150,22 +100,6 @@ class _$TransactionCWProxyImpl implements _$TransactionCWProxy { ? _value.toC // ignore: cast_nullable_to_non_nullable : toC as Contact, - toAvatar: toAvatar == const $CopyWithPlaceholder() - ? _value.toAvatar - // ignore: cast_nullable_to_non_nullable - : toAvatar as Uint8List?, - toNick: toNick == const $CopyWithPlaceholder() - ? _value.toNick - // ignore: cast_nullable_to_non_nullable - : toNick as String?, - fromAvatar: fromAvatar == const $CopyWithPlaceholder() - ? _value.fromAvatar - // ignore: cast_nullable_to_non_nullable - : fromAvatar as Uint8List?, - fromNick: fromNick == const $CopyWithPlaceholder() - ? _value.fromNick - // ignore: cast_nullable_to_non_nullable - : fromNick as String?, ); } } @@ -182,33 +116,21 @@ extension $TransactionCopyWith on Transaction { Transaction _$TransactionFromJson(Map<String, dynamic> json) => Transaction( type: $enumDecode(_$TransactionTypeEnumMap, json['type']), - from: json['from'] as String, - to: json['to'] as String, amount: (json['amount'] as num).toDouble(), comment: json['comment'] as String, time: DateTime.parse(json['time'] as String), fromC: Contact.fromJson(json['fromC'] as Map<String, dynamic>), toC: Contact.fromJson(json['toC'] as Map<String, dynamic>), - toAvatar: uIntFromList(json['toAvatar']), - toNick: json['toNick'] as String?, - fromAvatar: uIntFromList(json['fromAvatar']), - fromNick: json['fromNick'] as String?, ); Map<String, dynamic> _$TransactionToJson(Transaction instance) => <String, dynamic>{ 'type': _$TransactionTypeEnumMap[instance.type]!, - 'from': instance.from, - 'to': instance.to, 'fromC': instance.fromC, 'toC': instance.toC, 'amount': instance.amount, 'comment': instance.comment, 'time': instance.time.toIso8601String(), - 'toAvatar': uIntToList(instance.toAvatar), - 'toNick': instance.toNick, - 'fromAvatar': uIntToList(instance.fromAvatar), - 'fromNick': instance.fromNick, }; const _$TransactionTypeEnumMap = { diff --git a/lib/data/models/transaction_cubit.dart b/lib/data/models/transaction_cubit.dart index 9c1e507b..384e5c2b 100644 --- a/lib/data/models/transaction_cubit.dart +++ b/lib/data/models/transaction_cubit.dart @@ -5,7 +5,6 @@ import 'package:tuple/tuple.dart'; import '../../../g1/api.dart'; import '../../../g1/transaction_parser.dart'; import '../../shared_prefs.dart'; -import '../../ui/contacts_cache.dart'; import '../../ui/logger.dart'; import '../../ui/notification_controller.dart'; import 'contact.dart'; @@ -83,7 +82,7 @@ class TransactionsCubit extends HydratedCubit<TransactionsAndBalanceState> { if (tx.type == TransactionType.received && newState.latestReceivedNotification.isBefore(tx.time)) { // Future - final Contact from = await ContactsCache().getContact(tx.from); + final Contact from = tx.fromC; NotificationController.createNewNotification( tx.time.millisecondsSinceEpoch.toString(), amount: tx.amount / 100, @@ -93,7 +92,7 @@ class TransactionsCubit extends HydratedCubit<TransactionsAndBalanceState> { if (tx.type == TransactionType.sent && newState.latestSentNotification.isBefore(tx.time)) { // Future - final Contact to = await ContactsCache().getContact(tx.to); + final Contact to = tx.toC; NotificationController.createNewNotification( tx.time.millisecondsSinceEpoch.toString(), amount: -tx.amount / 100, diff --git a/lib/g1/transaction_parser.dart b/lib/g1/transaction_parser.dart index 6399ee71..4ae66c9d 100644 --- a/lib/g1/transaction_parser.dart +++ b/lib/g1/transaction_parser.dart @@ -48,9 +48,7 @@ Future<TransactionsAndBalanceState> transactionParser(String txData) async { 0, Transaction( type: type, - from: address2, fromC: fromC, - to: address1, toC: toC, amount: pubKey == address2 ? -amount : amount, comment: comment, @@ -137,9 +135,7 @@ Future<Transaction> _txGvaParse( final Contact toC = await ContactsCache().getContact(to!); return Transaction( type: type, - from: from, fromC: fromC, - to: to, toC: toC, amount: amount, comment: comment, diff --git a/test/transactions_test.dart b/test/transactions_test.dart index 1d120653..4cf8e971 100644 --- a/test/transactions_test.dart +++ b/test/transactions_test.dart @@ -34,11 +34,14 @@ void main() { expect(result.balance, equals(6700)); final List<Transaction> txs = result.transactions; for (final Transaction tx in txs) { - expect(tx.from != tx.to, equals(true)); + expect(tx.fromC != tx.toC, equals(true)); } - expect(txs.first.to == '9Bcx5JV3swCQBEeH3PcuNcBVperLscWtN78hjFVx1yzG', + expect( + txs.first.toC.pubKey == '9Bcx5JV3swCQBEeH3PcuNcBVperLscWtN78hjFVx1yzG', equals(true)); - expect(txs.first.from != '9Bcx5JV3swCQBEeH3PcuNcBVperLscWtN78hjFVx1yzG', + expect( + txs.first.fromC.pubKey != + '9Bcx5JV3swCQBEeH3PcuNcBVperLscWtN78hjFVx1yzG', equals(true)); expect(txs[txs.length - 2].amount < 0, equals(true)); expect(txs.last.amount > 0, equals(true)); @@ -54,22 +57,25 @@ void main() { expect(result.balance, equals(3)); final List<Transaction> txs = result.transactions; for (final Transaction tx in txs) { - expect(tx.from != tx.to, equals(true)); + expect(tx.fromC != tx.toC, equals(true)); } - expect( - txs.first.from, equals('BrgsSYK3xUzDyztGBHmxq69gfNxBfe2UKpxG21oZUBr5')); - expect( - txs.first.to, equals('6DrGg8cftpkgffv4Y4Lse9HSjgc8coEQor3yvMPHAnVH')); + expect(txs.first.fromC.pubKey, + equals('BrgsSYK3xUzDyztGBHmxq69gfNxBfe2UKpxG21oZUBr5')); + expect(txs.first.toC.pubKey, + equals('6DrGg8cftpkgffv4Y4Lse9HSjgc8coEQor3yvMPHAnVH')); expect(txs.first.type, equals(TransactionType.receiving)); expect(txs.first.amount, equals(100)); - expect(txs[1].to, equals('EDB7chzCBdtUCnqFZquVeto4a65FjeRkPrqcV8NwVbTx')); - expect(txs[1].from, equals('6DrGg8cftpkgffv4Y4Lse9HSjgc8coEQor3yvMPHAnVH')); + expect(txs[1].toC.pubKey, + equals('EDB7chzCBdtUCnqFZquVeto4a65FjeRkPrqcV8NwVbTx')); + expect(txs[1].fromC.pubKey, + equals('6DrGg8cftpkgffv4Y4Lse9HSjgc8coEQor3yvMPHAnVH')); expect(txs[1].amount, equals(-1200)); expect(txs[1].type, equals(TransactionType.sent)); - expect( - txs.last.from, equals('A1Fc1VoCLKHyPYmXimYECSmjmsceqwRSZcTBXfgG9JaB')); - expect(txs.last.to, equals('6DrGg8cftpkgffv4Y4Lse9HSjgc8coEQor3yvMPHAnVH')); + expect(txs.last.fromC.pubKey, + equals('A1Fc1VoCLKHyPYmXimYECSmjmsceqwRSZcTBXfgG9JaB')); + expect(txs.last.toC.pubKey, + equals('6DrGg8cftpkgffv4Y4Lse9HSjgc8coEQor3yvMPHAnVH')); expect(txs.last.type, equals(TransactionType.received)); expect(txs.last.amount, equals(10000)); -- GitLab