update address in case of bad index from migrate or any other reason

This commit is contained in:
julian 2023-01-18 16:55:59 -06:00
parent 6784e6aab4
commit 575cce1a7d
10 changed files with 283 additions and 32 deletions

View file

@ -42,6 +42,17 @@ class MainDB {
await isar.addresses.putAll(addresses);
});
Future<void> updateAddress(Address oldAddress, Address newAddress) =>
isar.writeTxn(() async {
newAddress.id = oldAddress.id;
await oldAddress.transaction.load();
final txns = oldAddress.transaction.toList();
await isar.addresses.delete(oldAddress.id);
await isar.addresses.put(newAddress);
newAddress.transaction.addAll(txns);
await newAddress.transaction.save();
});
// transactions
QueryBuilder<Transaction, Transaction, QAfterWhereClause> getTransactions(
String walletId) =>

View file

@ -1859,8 +1859,23 @@ class BitcoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress = await _generateAddressForChain(
0, newReceivingIndex, DerivePathType.bip84);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} catch (e, s) {
Logging.instance.log(
@ -1886,8 +1901,23 @@ class BitcoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newChangeAddress = await _generateAddressForChain(
1, newChangeIndex, DerivePathType.bip84);
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newChangeAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newChangeAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newChangeAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkChangeAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(

View file

@ -1746,8 +1746,23 @@ class BitcoinCashWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress = await _generateAddressForChain(
0, newReceivingIndex, DerivePathType.bip44);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(
@ -1778,8 +1793,23 @@ class BitcoinCashWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newChangeAddress = await _generateAddressForChain(
1, newChangeIndex, DerivePathType.bip44);
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newChangeAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newChangeAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newChangeAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkChangeAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(

View file

@ -1650,8 +1650,23 @@ class DogecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress = await _generateAddressForChain(
0, newReceivingIndex, DerivePathType.bip44);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(
@ -1682,8 +1697,23 @@ class DogecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newChangeAddress = await _generateAddressForChain(
1, newChangeIndex, DerivePathType.bip44);
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newChangeAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newChangeAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newChangeAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await checkChangeAddressForTransactions();
}
}
} catch (e, s) {
Logging.instance.log(

View file

@ -3147,8 +3147,23 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB, FiroHive {
newReceivingIndex,
);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await checkReceivingAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(
@ -3182,8 +3197,23 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB, FiroHive {
newChangeIndex,
);
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newChangeAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newChangeAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newChangeAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await checkChangeAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(

View file

@ -1885,8 +1885,23 @@ class LitecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress = await _generateAddressForChain(
0, newReceivingIndex, DerivePathType.bip84);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} catch (e, s) {
Logging.instance.log(
@ -1912,8 +1927,23 @@ class LitecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newChangeAddress = await _generateAddressForChain(
1, newChangeIndex, DerivePathType.bip84);
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newChangeAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newChangeAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newChangeAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkChangeAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(

View file

@ -1131,8 +1131,23 @@ class MoneroWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress =
await _generateAddressForChain(0, newReceivingIndex);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(

View file

@ -1867,8 +1867,23 @@ class NamecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress = await _generateAddressForChain(
0, newReceivingIndex, DerivePathType.bip84);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} catch (e, s) {
Logging.instance.log(
@ -1894,8 +1909,23 @@ class NamecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newChangeAddress = await _generateAddressForChain(
1, newChangeIndex, DerivePathType.bip84);
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newChangeAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newChangeAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newChangeAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkChangeAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(

View file

@ -1754,8 +1754,23 @@ class ParticlWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress = await _generateAddressForChain(
0, newReceivingIndex, DerivePathType.bip84);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} catch (e, s) {
Logging.instance.log(
@ -1781,8 +1796,23 @@ class ParticlWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newChangeAddress = await _generateAddressForChain(
1, newChangeIndex, DerivePathType.bip84);
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newChangeAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newChangeAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newChangeAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkChangeAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(

View file

@ -1200,8 +1200,23 @@ class WowneroWallet extends CoinServiceAPI with WalletCache, WalletDB {
final newReceivingAddress =
await _generateAddressForChain(0, newReceivingIndex);
// Add that new receiving address
final existing = await db
.getAddresses(walletId)
.filter()
.valueEqualTo(newReceivingAddress.value)
.findFirst();
if (existing == null) {
// Add that new change address
await db.putAddress(newReceivingAddress);
} else {
// we need to update the address
await db.updateAddress(existing, newReceivingAddress);
// since we updated an existing address there is a chance it has
// some tx history. To prevent address reuse we will call check again
// recursively
await _checkReceivingAddressForTransactions();
}
}
} on SocketException catch (se, s) {
Logging.instance.log(