From 9cc0d74b16b182ddac0775e3f5c8d0945803b311 Mon Sep 17 00:00:00 2001 From: julian <julian@cypherstack.com> Date: Thu, 26 Jan 2023 15:25:53 -0600 Subject: [PATCH] check paynym receiving addresses based on payment code notification tx history --- .../coins/dogecoin/dogecoin_wallet.dart | 2 + .../mixins/paynym_wallet_interface.dart | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/lib/services/coins/dogecoin/dogecoin_wallet.dart b/lib/services/coins/dogecoin/dogecoin_wallet.dart index f5a057500..f6b13f586 100644 --- a/lib/services/coins/dogecoin/dogecoin_wallet.dart +++ b/lib/services/coins/dogecoin/dogecoin_wallet.dart @@ -762,6 +762,8 @@ class DogecoinWallet extends CoinServiceAPI GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.3, walletId)); await _checkCurrentReceivingAddressesForTransactions(); + await checkAllCurrentReceivingPaynymAddressesForTransactions(); + final fetchFuture = _refreshTransactions(); final utxosRefreshFuture = _updateUTXOs(); GlobalEventBus.instance diff --git a/lib/services/mixins/paynym_wallet_interface.dart b/lib/services/mixins/paynym_wallet_interface.dart index 45f33d3be..1d194feb9 100644 --- a/lib/services/mixins/paynym_wallet_interface.dart +++ b/lib/services/mixins/paynym_wallet_interface.dart @@ -108,6 +108,70 @@ mixin PaynymWalletInterface { // convenience getter btc_dart.NetworkType get networkType => _network; + Future<Address> currentReceivingPaynymAddress(PaymentCode sender) async { + final address = await _db + .getAddresses(_walletId) + .filter() + .group((q) => q + .subTypeEqualTo(AddressSubType.paynymReceive) + .and() + .otherDataEqualTo(sender.toString())) + .sortByDerivationIndexDesc() + .findFirst(); + + if (address == null) { + final generatedAddress = await _generatePaynymReceivingAddress(sender, 0); + await _db.putAddress(generatedAddress); + return currentReceivingPaynymAddress(sender); + } else { + return address; + } + } + + Future<Address> _generatePaynymReceivingAddress( + PaymentCode sender, + int index, + ) async { + final myPrivateKey = + await deriveNotificationPrivateKey(mnemonic: await _getMnemonic()); + final paymentAddress = PaymentAddress.initWithPrivateKey( + myPrivateKey, + sender, + index, + ); + final pair = paymentAddress.getReceiveAddressKeyPair(); + final address = generatePaynymReceivingAddressFromKeyPair( + pair: pair, + derivationIndex: index, + derivePathType: DerivePathType.bip44, + fromPaymentCode: sender, + ); + return address; + } + + Future<void> checkCurrentPaynymReceivingAddressForTransactions( + PaymentCode sender) async { + final address = await currentReceivingPaynymAddress(sender); + final txCount = await _getTxCount(address: address.value); + if (txCount > 0) { + // generate next address and add to db + final nextAddress = await _generatePaynymReceivingAddress( + sender, + address.derivationIndex + 1, + ); + await _db.putAddress(nextAddress); + } + } + + Future<void> checkAllCurrentReceivingPaynymAddressesForTransactions() async { + final codes = await getAllPaymentCodesFromNotificationTransactions(); + final List<Future<void>> futures = []; + for (final code in codes) { + futures.add(checkCurrentPaynymReceivingAddressForTransactions(code)); + } + await Future.wait(futures); + } + // generate bip32 payment code root Future<bip32.BIP32> getRootNode({ required List<String> mnemonic,