From c7446f1946d3e056dcb101870dc2586bc3059594 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 27 Jan 2023 14:43:00 -0600 Subject: [PATCH] receiving address fix --- .../mixins/paynym_wallet_interface.dart | 64 ++++++++++++------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/services/mixins/paynym_wallet_interface.dart b/lib/services/mixins/paynym_wallet_interface.dart index a3b44951f..16d65c6cf 100644 --- a/lib/services/mixins/paynym_wallet_interface.dart +++ b/lib/services/mixins/paynym_wallet_interface.dart @@ -140,10 +140,9 @@ mixin PaynymWalletInterface { final address = await _db .getAddresses(_walletId) .filter() - .group((q) => q - .subTypeEqualTo(AddressSubType.paynymReceive) - .and() - .otherDataEqualTo(sender.toString())) + .subTypeEqualTo(AddressSubType.paynymReceive) + .and() + .otherDataEqualTo(sender.toString()) .sortByDerivationIndexDesc() .findFirst(); @@ -160,12 +159,15 @@ mixin PaynymWalletInterface { PaymentCode sender, int index, ) async { - final myPrivateKey = - await deriveNotificationPrivateKey(mnemonic: await _getMnemonic()); + final myPrivateKey = await deriveReceivingPrivateKey( + mnemonic: await _getMnemonic(), + index: index, + ); + final paymentAddress = PaymentAddress.initWithPrivateKey( myPrivateKey, sender, - index, + 0, ); final pair = paymentAddress.getReceiveAddressKeyPair(); final address = await generatePaynymReceivingAddressFromKeyPair( @@ -216,6 +218,15 @@ mixin PaynymWalletInterface { return node.privateKey!; } + Future deriveReceivingPrivateKey({ + required List mnemonic, + required int index, + }) async { + final root = await getRootNode(mnemonic: mnemonic); + final node = root.derivePath(kPaynymDerivePath).derive(index); + return node.privateKey!; + } + /// fetch or generate this wallet's bip47 payment code Future getPaymentCode( DerivePathType derivePathType, @@ -722,8 +733,12 @@ mixin PaynymWalletInterface { const maxCount = 2147483647; assert(maxNumberOfIndexesToCheck < maxCount); - final myPrivateKey = - await deriveNotificationPrivateKey(mnemonic: await _getMnemonic()); + final mnemonic = await _getMnemonic(); + + final mySendPrivateKey = + await deriveNotificationPrivateKey(mnemonic: mnemonic); + final receivingNode = + (await getRootNode(mnemonic: mnemonic)).derivePath(kPaynymDerivePath); List
addresses = []; int receivingGapCounter = 0; @@ -734,14 +749,13 @@ mixin PaynymWalletInterface { (receivingGapCounter < maxUnusedAddressGap || outgoingGapCounter < maxUnusedAddressGap); i++) { - final paymentAddress = PaymentAddress.initWithPrivateKey( - myPrivateKey, - other, - i, // index to use - ); - if (outgoingGapCounter < maxUnusedAddressGap) { - final pair = paymentAddress.getSendAddressKeyPair(); + final paymentAddressSending = PaymentAddress.initWithPrivateKey( + mySendPrivateKey, + other, + i, // index to use + ); + final pair = paymentAddressSending.getSendAddressKeyPair(); final address = generatePaynymSendAddressFromKeyPair( pair: pair, derivationIndex: i, @@ -753,14 +767,20 @@ mixin PaynymWalletInterface { final count = await _getTxCount(address: address.value); if (count > 0) { - outgoingGapCounter++; - } else { outgoingGapCounter = 0; + } else { + outgoingGapCounter++; } } if (receivingGapCounter < maxUnusedAddressGap) { - final pair = paymentAddress.getReceiveAddressKeyPair(); + final myReceivingPrivateKey = receivingNode.derive(i).privateKey!; + final paymentAddressReceiving = PaymentAddress.initWithPrivateKey( + myReceivingPrivateKey, + other, + 0, + ); + final pair = paymentAddressReceiving.getReceiveAddressKeyPair(); final address = await generatePaynymReceivingAddressFromKeyPair( pair: pair, derivationIndex: i, @@ -772,13 +792,13 @@ mixin PaynymWalletInterface { final count = await _getTxCount(address: address.value); if (count > 0) { - receivingGapCounter++; - } else { receivingGapCounter = 0; + } else { + receivingGapCounter++; } } } - await _db.putAddresses(addresses); + await _db.updateOrPutAddresses(addresses); } Address generatePaynymSendAddressFromKeyPair({