receiving address fix

This commit is contained in:
julian 2023-01-27 14:43:00 -06:00
parent 6094a767d7
commit c7446f1946

View file

@ -140,10 +140,9 @@ mixin PaynymWalletInterface {
final address = await _db final address = await _db
.getAddresses(_walletId) .getAddresses(_walletId)
.filter() .filter()
.group((q) => q .subTypeEqualTo(AddressSubType.paynymReceive)
.subTypeEqualTo(AddressSubType.paynymReceive) .and()
.and() .otherDataEqualTo(sender.toString())
.otherDataEqualTo(sender.toString()))
.sortByDerivationIndexDesc() .sortByDerivationIndexDesc()
.findFirst(); .findFirst();
@ -160,12 +159,15 @@ mixin PaynymWalletInterface {
PaymentCode sender, PaymentCode sender,
int index, int index,
) async { ) async {
final myPrivateKey = final myPrivateKey = await deriveReceivingPrivateKey(
await deriveNotificationPrivateKey(mnemonic: await _getMnemonic()); mnemonic: await _getMnemonic(),
index: index,
);
final paymentAddress = PaymentAddress.initWithPrivateKey( final paymentAddress = PaymentAddress.initWithPrivateKey(
myPrivateKey, myPrivateKey,
sender, sender,
index, 0,
); );
final pair = paymentAddress.getReceiveAddressKeyPair(); final pair = paymentAddress.getReceiveAddressKeyPair();
final address = await generatePaynymReceivingAddressFromKeyPair( final address = await generatePaynymReceivingAddressFromKeyPair(
@ -216,6 +218,15 @@ mixin PaynymWalletInterface {
return node.privateKey!; return node.privateKey!;
} }
Future<Uint8List> deriveReceivingPrivateKey({
required List<String> 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 /// fetch or generate this wallet's bip47 payment code
Future<PaymentCode> getPaymentCode( Future<PaymentCode> getPaymentCode(
DerivePathType derivePathType, DerivePathType derivePathType,
@ -722,8 +733,12 @@ mixin PaynymWalletInterface {
const maxCount = 2147483647; const maxCount = 2147483647;
assert(maxNumberOfIndexesToCheck < maxCount); assert(maxNumberOfIndexesToCheck < maxCount);
final myPrivateKey = final mnemonic = await _getMnemonic();
await deriveNotificationPrivateKey(mnemonic: await _getMnemonic());
final mySendPrivateKey =
await deriveNotificationPrivateKey(mnemonic: mnemonic);
final receivingNode =
(await getRootNode(mnemonic: mnemonic)).derivePath(kPaynymDerivePath);
List<Address> addresses = []; List<Address> addresses = [];
int receivingGapCounter = 0; int receivingGapCounter = 0;
@ -734,14 +749,13 @@ mixin PaynymWalletInterface {
(receivingGapCounter < maxUnusedAddressGap || (receivingGapCounter < maxUnusedAddressGap ||
outgoingGapCounter < maxUnusedAddressGap); outgoingGapCounter < maxUnusedAddressGap);
i++) { i++) {
final paymentAddress = PaymentAddress.initWithPrivateKey(
myPrivateKey,
other,
i, // index to use
);
if (outgoingGapCounter < maxUnusedAddressGap) { if (outgoingGapCounter < maxUnusedAddressGap) {
final pair = paymentAddress.getSendAddressKeyPair(); final paymentAddressSending = PaymentAddress.initWithPrivateKey(
mySendPrivateKey,
other,
i, // index to use
);
final pair = paymentAddressSending.getSendAddressKeyPair();
final address = generatePaynymSendAddressFromKeyPair( final address = generatePaynymSendAddressFromKeyPair(
pair: pair, pair: pair,
derivationIndex: i, derivationIndex: i,
@ -753,14 +767,20 @@ mixin PaynymWalletInterface {
final count = await _getTxCount(address: address.value); final count = await _getTxCount(address: address.value);
if (count > 0) { if (count > 0) {
outgoingGapCounter++;
} else {
outgoingGapCounter = 0; outgoingGapCounter = 0;
} else {
outgoingGapCounter++;
} }
} }
if (receivingGapCounter < maxUnusedAddressGap) { 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( final address = await generatePaynymReceivingAddressFromKeyPair(
pair: pair, pair: pair,
derivationIndex: i, derivationIndex: i,
@ -772,13 +792,13 @@ mixin PaynymWalletInterface {
final count = await _getTxCount(address: address.value); final count = await _getTxCount(address: address.value);
if (count > 0) { if (count > 0) {
receivingGapCounter++;
} else {
receivingGapCounter = 0; receivingGapCounter = 0;
} else {
receivingGapCounter++;
} }
} }
} }
await _db.putAddresses(addresses); await _db.updateOrPutAddresses(addresses);
} }
Address generatePaynymSendAddressFromKeyPair({ Address generatePaynymSendAddressFromKeyPair({