check anon set when creating list of mints

This commit is contained in:
julian 2023-07-25 08:55:50 -06:00
parent 462b845bd4
commit 18f3a2056c

View file

@ -2559,8 +2559,7 @@ class FiroWallet extends CoinServiceAPI
Future<List<Map<String, dynamic>>> createMintsFromAmount(int total) async { Future<List<Map<String, dynamic>>> createMintsFromAmount(int total) async {
int tmpTotal = total; int tmpTotal = total;
int index = 0; int counter = 0;
final mints = <Map<String, dynamic>>[];
final lastUsedIndex = await db.isar.lelantusCoins final lastUsedIndex = await db.isar.lelantusCoins
.where() .where()
.walletIdEqualTo(walletId) .walletIdEqualTo(walletId)
@ -2568,20 +2567,82 @@ class FiroWallet extends CoinServiceAPI
.mintIndexProperty() .mintIndexProperty()
.findFirst(); .findFirst();
final nextFreeMintIndex = (lastUsedIndex ?? 0) + 1; final nextFreeMintIndex = (lastUsedIndex ?? 0) + 1;
final root = await Bip32Utils.getBip32Root(
(await mnemonic).join(" "),
(await mnemonicPassphrase)!,
_network,
);
final mints = <Map<String, dynamic>>[];
while (tmpTotal > 0) { while (tmpTotal > 0) {
final index = nextFreeMintIndex + counter;
final bip32.BIP32 mintKeyPair = await Bip32Utils.getBip32NodeFromRoot(
root,
constructDerivePath(
networkWIF: _network.wif,
chain: MINT_INDEX,
index: index,
),
);
final String mintTag = CreateTag(
Format.uint8listToString(mintKeyPair.privateKey!),
index,
Format.uint8listToString(mintKeyPair.identifier),
isTestnet: coin == Coin.firoTestNet,
);
final List<Map<String, dynamic>> anonymitySets;
try {
anonymitySets = await fetchAnonymitySets();
} catch (e, s) {
Logging.instance.log(
"Firo needs better internet to create mints: $e\n$s",
level: LogLevel.Fatal,
);
rethrow;
}
bool isUsedMintTag = false;
// stupid dynamic maps
for (final set in anonymitySets) {
final setCoins = set["coins"] as List;
for (final coin in setCoins) {
if (coin[1] == mintTag) {
isUsedMintTag = true;
break;
}
}
if (isUsedMintTag) {
break;
}
}
if (isUsedMintTag) {
Logging.instance.log(
"Found used index when minting",
level: LogLevel.Warning,
);
}
if (!isUsedMintTag) {
final mintValue = min(tmpTotal, MINT_LIMIT); final mintValue = min(tmpTotal, MINT_LIMIT);
final mint = await _getMintHex( final mint = await _getMintHex(
mintValue, mintValue,
nextFreeMintIndex + index, index,
); );
mints.add({ mints.add({
"value": mintValue, "value": mintValue,
"script": mint, "script": mint,
"index": nextFreeMintIndex + index, "index": index,
"publicCoin": "", "publicCoin": "",
}); });
tmpTotal = tmpTotal - MINT_LIMIT; tmpTotal = tmpTotal - MINT_LIMIT;
index++; }
counter++;
} }
return mints; return mints;
} }