WIP fixing _refreshLelantusData

This commit is contained in:
julian 2023-07-25 20:29:44 -06:00
parent c9da22601e
commit 57839c2d18

View file

@ -2797,96 +2797,68 @@ class FiroWallet extends CoinServiceAPI
.walletIdEqualTo(walletId) .walletIdEqualTo(walletId)
.filter() .filter()
.isUsedEqualTo(false) .isUsedEqualTo(false)
.and()
.isJMintEqualTo(true)
.not() .not()
.valueEqualTo(0.toString()) .valueEqualTo(0.toString())
.findAll(); .findAll();
// Get all joinsplit transaction ids final List<isar_models.LelantusCoin> updatedCoins = [];
final lelantusJoinSplitTxns = await db final usedSerialNumbersSet = (await getUsedCoinSerials()).toSet();
.getTransactions(walletId)
.filter()
.isLelantusEqualTo(true)
.and()
.subTypeEqualTo(isar_models.TransactionSubType.join)
.findAll();
Set<String> joinSplitTXIDs = {}; final root = await Bip32Utils.getBip32Root(
(await mnemonic).join(" "),
// for (final tx in lelantusJoinSplitTxns) { (await mnemonicPassphrase)!,
// joinSplitTXIDs.add(tx.txid); _network,
// }
for (final coin in lelantusCoins) {
joinSplitTXIDs.add(coin.txid);
}
Map<String, Tuple2<isar_models.Address?, isar_models.Transaction>>
updatedData = {};
// Grab the most recent information on all the joinsplits
final updatedJSplit = await getJMintTransactions(
cachedElectrumXClient,
joinSplitTXIDs.toList(),
coin,
); );
final currentChainHeight = await chainHeight; for (final coin in lelantusCoins) {
final _derivePath = constructDerivePath(
networkWIF: _network.wif,
chain: MINT_INDEX,
index: coin.mintIndex,
);
final bip32.BIP32 mintKeyPair = await Bip32Utils.getBip32NodeFromRoot(
root,
_derivePath,
);
// update all of joinsplits that are now confirmed. final String serialNumber = GetSerialNumber(
for (final tx in updatedJSplit.entries) { int.parse(coin.value),
isar_models.Transaction? currentTx; Format.uint8listToString(mintKeyPair.privateKey!),
coin.mintIndex,
isTestnet: this.coin == Coin.firoTestNet,
);
final bool isUsed = usedSerialNumbersSet.contains(serialNumber);
if (isUsed) {
updatedCoins.add(coin.copyWith(isUsed: isUsed));
}
final tx = await db.getTransaction(walletId, coin.txid);
if (tx == null) {
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
}
}
if (updatedCoins.isNotEmpty) {
try { try {
currentTx = await db.isar.writeTxn(() async {
lelantusJoinSplitTxns.firstWhere((e) => e.txid == tx.value.txid); for (final c in updatedCoins) {
} catch (_) { await db.isar.lelantusCoins.deleteByMintIndexWalletId(
currentTx = null; c.mintIndex,
c.walletId,
);
} }
await db.isar.lelantusCoins.putAll(updatedCoins);
if (currentTx == null) { });
// this send was accidentally not included in the list } catch (e, s) {
tx.value.isLelantus = true; Logging.instance.log(
updatedData[tx.value.txid] = "$e\n$s",
Tuple2(tx.value.address.value ?? tx.key, tx.value); level: LogLevel.Fatal,
} else if (currentTx.isConfirmed( );
currentChainHeight, MINIMUM_CONFIRMATIONS) != rethrow;
tx.value.isConfirmed(currentChainHeight, MINIMUM_CONFIRMATIONS)) {
tx.value.isLelantus = true;
updatedData[tx.value.txid] =
Tuple2(tx.value.address.value ?? tx.key, tx.value);
} }
} }
final List<Tuple2<isar_models.Transaction, isar_models.Address?>> txnsData =
[];
for (final value in updatedData.values) {
// allow possible null address on mints as we don't display address
// this should normally never be null anyways but old (dbVersion up to 4)
// migrated transactions may not have had an address (full rescan should
// fix this)
isar_models.Address? transactionAddress;
try {
transactionAddress =
value.item2.subType == isar_models.TransactionSubType.mint
? value.item1
: value.item1!;
} catch (_) {
Logging.instance
.log("_refreshLelantusData value: $value", level: LogLevel.Fatal);
}
final outs =
value.item2.outputs.where((_) => true).toList(growable: false);
final ins = value.item2.inputs.where((_) => true).toList(growable: false);
txnsData.add(Tuple2(
value.item2.copyWith(inputs: ins, outputs: outs).item1,
transactionAddress));
}
await db.addNewTransactionData(txnsData, walletId);
} }
Future<String> _getMintHex(int amount, int index) async { Future<String> _getMintHex(int amount, int index) async {