mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-18 16:55:58 +00:00
unspent coins / coin control fixes
This commit is contained in:
parent
dfdf16a6c2
commit
4ec9d7b2e1
2 changed files with 45 additions and 29 deletions
|
@ -79,6 +79,9 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
autorun((_) {
|
autorun((_) {
|
||||||
this.walletAddresses.isEnabledAutoGenerateSubaddress = this.isEnabledAutoGenerateSubaddress;
|
this.walletAddresses.isEnabledAutoGenerateSubaddress = this.isEnabledAutoGenerateSubaddress;
|
||||||
});
|
});
|
||||||
|
CwMweb.stub().then((value) {
|
||||||
|
_stub = value;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final bitcoin.HDWallet mwebHd;
|
final bitcoin.HDWallet mwebHd;
|
||||||
|
@ -86,6 +89,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
Timer? _syncTimer;
|
Timer? _syncTimer;
|
||||||
// late int lastMwebUtxosHeight;
|
// late int lastMwebUtxosHeight;
|
||||||
int mwebUtxosHeight = 0;
|
int mwebUtxosHeight = 0;
|
||||||
|
late RpcClient _stub;
|
||||||
|
|
||||||
static Future<LitecoinWallet> create(
|
static Future<LitecoinWallet> create(
|
||||||
{required String mnemonic,
|
{required String mnemonic,
|
||||||
|
@ -152,12 +156,11 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
@override
|
@override
|
||||||
Future<void> startSync() async {
|
Future<void> startSync() async {
|
||||||
await super.startSync();
|
await super.startSync();
|
||||||
final stub = await CwMweb.stub();
|
|
||||||
_syncTimer?.cancel();
|
_syncTimer?.cancel();
|
||||||
_syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async {
|
_syncTimer = Timer.periodic(const Duration(milliseconds: 1500), (timer) async {
|
||||||
if (syncStatus is FailedSyncStatus) return;
|
if (syncStatus is FailedSyncStatus) return;
|
||||||
final height = await electrumClient.getCurrentBlockChainTip() ?? 0;
|
final height = await electrumClient.getCurrentBlockChainTip() ?? 0;
|
||||||
final resp = await stub.status(StatusRequest());
|
final resp = await _stub.status(StatusRequest());
|
||||||
// print("stats:");
|
// print("stats:");
|
||||||
// print("???????????????????");
|
// print("???????????????????");
|
||||||
// print(resp.blockHeaderHeight);
|
// print(resp.blockHeaderHeight);
|
||||||
|
@ -279,7 +282,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> processMwebUtxos() async {
|
Future<void> processMwebUtxos() async {
|
||||||
final stub = await CwMweb.stub();
|
|
||||||
final scanSecret = mwebHd.derive(0x80000000).privKey!;
|
final scanSecret = mwebHd.derive(0x80000000).privKey!;
|
||||||
int restoreHeight = walletInfo.restoreHeight;
|
int restoreHeight = walletInfo.restoreHeight;
|
||||||
print("SCANNING FROM HEIGHT: $restoreHeight");
|
print("SCANNING FROM HEIGHT: $restoreHeight");
|
||||||
|
@ -296,7 +298,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
await handleIncoming(utxo, stub);
|
await handleIncoming(utxo, _stub);
|
||||||
|
|
||||||
if (initDone) {
|
if (initDone) {
|
||||||
await updateUnspent();
|
await updateUnspent();
|
||||||
|
@ -308,7 +310,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await for (Utxo sUtxo in stub.utxos(req)) {
|
await for (Utxo sUtxo in _stub.utxos(req)) {
|
||||||
final utxo = MwebUtxo(
|
final utxo = MwebUtxo(
|
||||||
address: sUtxo.address,
|
address: sUtxo.address,
|
||||||
blockTime: sUtxo.blockTime,
|
blockTime: sUtxo.blockTime,
|
||||||
|
@ -331,7 +333,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
|
|
||||||
await mwebUtxosBox.put(utxo.outputId, utxo);
|
await mwebUtxosBox.put(utxo.outputId, utxo);
|
||||||
|
|
||||||
await handleIncoming(utxo, stub);
|
await handleIncoming(utxo, _stub);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,11 +345,10 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
final outputIds =
|
final outputIds =
|
||||||
mwebUtxosBox.values.where((utxo) => utxo.height > 0).map((utxo) => utxo.outputId).toList();
|
mwebUtxosBox.values.where((utxo) => utxo.height > 0).map((utxo) => utxo.outputId).toList();
|
||||||
|
|
||||||
final stub = await CwMweb.stub();
|
final resp = await _stub.spent(SpentRequest(outputId: outputIds));
|
||||||
final resp = await stub.spent(SpentRequest(outputId: outputIds));
|
|
||||||
final spent = resp.outputId;
|
final spent = resp.outputId;
|
||||||
if (spent.isEmpty) return;
|
if (spent.isEmpty) return;
|
||||||
final status = await stub.status(StatusRequest());
|
final status = await _stub.status(StatusRequest());
|
||||||
final height = await electrumClient.getCurrentBlockChainTip();
|
final height = await electrumClient.getCurrentBlockChainTip();
|
||||||
if (height == null || status.blockHeaderHeight != height) return;
|
if (height == null || status.blockHeaderHeight != height) return;
|
||||||
if (status.mwebUtxosHeight != height) return;
|
if (status.mwebUtxosHeight != height) return;
|
||||||
|
@ -405,11 +406,12 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
target.add(outputId);
|
target.add(outputId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (outputId.isEmpty) return false;
|
if (outputId.isEmpty) {
|
||||||
final stub = await CwMweb.stub();
|
return false;
|
||||||
final resp = await stub.spent(SpentRequest(outputId: outputId));
|
}
|
||||||
|
final resp = await _stub.spent(SpentRequest(outputId: outputId));
|
||||||
if (!setEquals(resp.outputId.toSet(), target)) return false;
|
if (!setEquals(resp.outputId.toSet(), target)) return false;
|
||||||
final status = await stub.status(StatusRequest());
|
final status = await _stub.status(StatusRequest());
|
||||||
if (!tx.isPending) return false;
|
if (!tx.isPending) return false;
|
||||||
tx.height = status.mwebUtxosHeight;
|
tx.height = status.mwebUtxosHeight;
|
||||||
tx.confirmations = 1;
|
tx.confirmations = 1;
|
||||||
|
@ -422,6 +424,18 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
Future<void> updateUnspent() async {
|
Future<void> updateUnspent() async {
|
||||||
await super.updateUnspent();
|
await super.updateUnspent();
|
||||||
await checkMwebUtxosSpent();
|
await checkMwebUtxosSpent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
@action
|
||||||
|
Future<void> updateAllUnspents() async {
|
||||||
|
List<BitcoinUnspent> updatedUnspentCoins = [];
|
||||||
|
|
||||||
|
await Future.wait(walletAddresses.allAddresses.map((address) async {
|
||||||
|
updatedUnspentCoins.addAll(await fetchUnspent(address));
|
||||||
|
}));
|
||||||
|
|
||||||
|
// update mweb unspents:
|
||||||
final mwebAddrs = (walletAddresses as LitecoinWalletAddresses).mwebAddrs;
|
final mwebAddrs = (walletAddresses as LitecoinWalletAddresses).mwebAddrs;
|
||||||
mwebUtxosBox.keys.forEach((dynamic oId) {
|
mwebUtxosBox.keys.forEach((dynamic oId) {
|
||||||
final String outputId = oId as String;
|
final String outputId = oId as String;
|
||||||
|
@ -436,11 +450,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
final addressRecord = walletAddresses.allAddresses
|
final addressRecord = walletAddresses.allAddresses
|
||||||
.firstWhereOrNull((addressRecord) => addressRecord.address == utxo.address);
|
.firstWhereOrNull((addressRecord) => addressRecord.address == utxo.address);
|
||||||
|
|
||||||
// print("^^^^^^^^^^^^^^^^^^");
|
|
||||||
// print(utxo.address);
|
|
||||||
// for (var a in walletAddresses.allAddresses) {
|
|
||||||
// print(a.address);
|
|
||||||
// }
|
|
||||||
if (addressRecord == null) {
|
if (addressRecord == null) {
|
||||||
print("addressRecord is null! TODO: handle this case2");
|
print("addressRecord is null! TODO: handle this case2");
|
||||||
return;
|
return;
|
||||||
|
@ -454,8 +463,10 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
if (unspent.vout == 0) {
|
if (unspent.vout == 0) {
|
||||||
unspent.isChange = true;
|
unspent.isChange = true;
|
||||||
}
|
}
|
||||||
unspentCoins.add(unspent);
|
updatedUnspentCoins.add(unspent);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
unspentCoins = updatedUnspentCoins;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -524,8 +535,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
final fee = utxos.sumOfUtxosValue() - preOutputSum;
|
final fee = utxos.sumOfUtxosValue() - preOutputSum;
|
||||||
final txb =
|
final txb =
|
||||||
BitcoinTransactionBuilder(utxos: utxos, outputs: outputs, fee: fee, network: network);
|
BitcoinTransactionBuilder(utxos: utxos, outputs: outputs, fee: fee, network: network);
|
||||||
final stub = await CwMweb.stub();
|
final resp = await _stub.create(CreateRequest(
|
||||||
final resp = await stub.create(CreateRequest(
|
|
||||||
rawTx: txb.buildTransaction((a, b, c, d) => '').toBytes(),
|
rawTx: txb.buildTransaction((a, b, c, d) => '').toBytes(),
|
||||||
scanSecret: hex.decode(mwebHd.derive(0x80000000).privKey!),
|
scanSecret: hex.decode(mwebHd.derive(0x80000000).privKey!),
|
||||||
spendSecret: hex.decode(mwebHd.derive(0x80000001).privKey!),
|
spendSecret: hex.decode(mwebHd.derive(0x80000001).privKey!),
|
||||||
|
@ -560,8 +570,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
||||||
try {
|
try {
|
||||||
final tx = await super.createTransaction(credentials) as PendingBitcoinTransaction;
|
final tx = await super.createTransaction(credentials) as PendingBitcoinTransaction;
|
||||||
|
|
||||||
final stub = await CwMweb.stub();
|
final resp = await _stub.create(CreateRequest(
|
||||||
final resp = await stub.create(CreateRequest(
|
|
||||||
rawTx: hex.decode(tx.hex),
|
rawTx: hex.decode(tx.hex),
|
||||||
scanSecret: hex.decode(mwebHd.derive(0x80000000).privKey!),
|
scanSecret: hex.decode(mwebHd.derive(0x80000000).privKey!),
|
||||||
spendSecret: hex.decode(mwebHd.derive(0x80000001).privKey!),
|
spendSecret: hex.decode(mwebHd.derive(0x80000001).privKey!),
|
||||||
|
|
|
@ -85,19 +85,26 @@ abstract class UnspentCoinsListViewModelBase with Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Unspent> _getUnspents() {
|
List<Unspent> _getUnspents() {
|
||||||
if (wallet.type == WalletType.monero) return monero!.getUnspents(wallet);
|
switch (wallet.type) {
|
||||||
if (wallet.type == WalletType.wownero) return wownero!.getUnspents(wallet);
|
case WalletType.monero:
|
||||||
if ([WalletType.bitcoin, WalletType.litecoin, WalletType.bitcoinCash].contains(wallet.type))
|
return monero!.getUnspents(wallet);
|
||||||
|
case WalletType.wownero:
|
||||||
|
return wownero!.getUnspents(wallet);
|
||||||
|
case WalletType.bitcoin:
|
||||||
|
case WalletType.litecoin:
|
||||||
|
case WalletType.bitcoinCash:
|
||||||
return bitcoin!.getUnspents(wallet);
|
return bitcoin!.getUnspents(wallet);
|
||||||
|
default:
|
||||||
return List.empty();
|
return List.empty();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
void _updateUnspentCoinsInfo() {
|
void _updateUnspentCoinsInfo() {
|
||||||
_items.clear();
|
_items.clear();
|
||||||
|
|
||||||
List<UnspentCoinsItem> unspents = [];
|
List<UnspentCoinsItem> unspents = [];
|
||||||
_getUnspents().forEach((elem) {
|
_getUnspents().forEach((Unspent elem) {
|
||||||
try {
|
try {
|
||||||
final info =
|
final info =
|
||||||
getUnspentCoinInfo(elem.hash, elem.address, elem.value, elem.vout, elem.keyImage);
|
getUnspentCoinInfo(elem.hash, elem.address, elem.value, elem.vout, elem.keyImage);
|
||||||
|
|
Loading…
Reference in a new issue