feat: sync improvements, make it more similar to cw_monero/lib/api/wallet.dart from monero sync code

This commit is contained in:
Rafael Saes 2023-11-28 18:20:19 -03:00
parent 2f0e82aa09
commit be02458b15
11 changed files with 520 additions and 558 deletions

View file

@ -1,5 +1,4 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data';
class BitcoinAddressRecord { class BitcoinAddressRecord {
BitcoinAddressRecord(this.address, BitcoinAddressRecord(this.address,
@ -25,7 +24,7 @@ class BitcoinAddressRecord {
final String address; final String address;
final bool isHidden; final bool isHidden;
final String? silentAddressLabel; final String? silentAddressLabel;
final Uint8List? silentPaymentTweak; final String? silentPaymentTweak;
final int index; final int index;
bool get isUsed => _isUsed; bool get isUsed => _isUsed;

View file

@ -1,18 +1,20 @@
import 'dart:typed_data';
import 'package:cw_bitcoin/bitcoin_address_record.dart'; import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_core/unspent_transaction_output.dart'; import 'package:cw_core/unspent_transaction_output.dart';
class BitcoinUnspent extends Unspent { class BitcoinUnspent extends Unspent {
BitcoinUnspent(BitcoinAddressRecord addressRecord, String hash, int value, int vout, BitcoinUnspent(BitcoinAddressRecord addressRecord, String hash, int value, int vout,
{this.silentPaymentTweak}) {this.silentPaymentTweak, bool? isTaproot})
: bitcoinAddressRecord = addressRecord, : bitcoinAddressRecord = addressRecord,
isTaproot = isTaproot ?? false,
super(addressRecord.address, hash, value, vout, null); super(addressRecord.address, hash, value, vout, null);
factory BitcoinUnspent.fromJSON(BitcoinAddressRecord address, Map<String, dynamic> json) => factory BitcoinUnspent.fromJSON(BitcoinAddressRecord address, Map<String, dynamic> json) =>
BitcoinUnspent( BitcoinUnspent(
address, json['tx_hash'] as String, json['value'] as int, json['tx_pos'] as int); address, json['tx_hash'] as String, json['value'] as int, json['tx_pos'] as int,
silentPaymentTweak: json['silent_payment_tweak'] as String?,
isTaproot: json['is_taproot'] as bool?);
final BitcoinAddressRecord bitcoinAddressRecord; final BitcoinAddressRecord bitcoinAddressRecord;
Uint8List? silentPaymentTweak; String? silentPaymentTweak;
bool isTaproot = false;
} }

View file

@ -4,6 +4,7 @@ import 'dart:io';
import 'dart:isolate'; import 'dart:isolate';
import 'dart:math'; import 'dart:math';
import 'package:http/http.dart' as http;
import 'package:cw_core/encryption_file_utils.dart'; import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/unspent_coins_info.dart'; import 'package:cw_core/unspent_coins_info.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
@ -25,26 +26,18 @@ import 'package:cw_bitcoin/electrum.dart';
import 'package:cw_bitcoin/electrum_balance.dart'; import 'package:cw_bitcoin/electrum_balance.dart';
import 'package:cw_bitcoin/electrum_transaction_history.dart'; import 'package:cw_bitcoin/electrum_transaction_history.dart';
import 'package:cw_bitcoin/electrum_transaction_info.dart'; import 'package:cw_bitcoin/electrum_transaction_info.dart';
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
import 'package:cw_bitcoin/pending_bitcoin_transaction.dart'; import 'package:cw_bitcoin/pending_bitcoin_transaction.dart';
import 'package:cw_bitcoin/script_hash.dart'; import 'package:cw_bitcoin/script_hash.dart';
import 'package:cw_bitcoin/utils.dart'; import 'package:cw_bitcoin/utils.dart';
import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/crypto_currency.dart';
import 'package:collection/collection.dart';
import 'package:cw_core/node.dart'; import 'package:cw_core/node.dart';
import 'package:cw_core/pathForWallet.dart'; import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/pending_transaction.dart'; import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart'; import 'package:cw_core/sync_status.dart';
import 'package:cw_core/transaction_direction.dart';
import 'package:cw_core/transaction_priority.dart'; import 'package:cw_core/transaction_priority.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
import 'package:flutter/foundation.dart';
import 'package:hex/hex.dart'; import 'package:hex/hex.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:rxdart/subjects.dart';
part 'electrum_wallet.g.dart'; part 'electrum_wallet.g.dart';
@ -141,15 +134,66 @@ abstract class ElectrumWalletBase
Map<String, BehaviorSubject<Object>?> _scripthashesUpdateSubject; Map<String, BehaviorSubject<Object>?> _scripthashesUpdateSubject;
BehaviorSubject<Object>? _chainTipUpdateSubject; BehaviorSubject<Object>? _chainTipUpdateSubject;
bool _isTransactionUpdating; bool _isTransactionUpdating;
int _initialSyncHeight = 0;
Future<Isolate>? _isolate; Future<Isolate>? _isolate;
void Function(FlutterErrorDetails)? _onError; void Function(FlutterErrorDetails)? _onError;
Timer? _autoSaveTimer;
static const int _autoSaveInterval = 30;
Future<void> init() async { Future<void> init() async {
await walletAddresses.init(); await walletAddresses.init();
await transactionHistory.init(); await transactionHistory.init();
await save();
_autoSaveTimer =
Timer.periodic(Duration(seconds: _autoSaveInterval), (_) async => await save());
}
void _setListeners(int height, {int? chainTip}) async {
final currentChainTip = chainTip ?? await electrumClient.getCurrentBlockChainTip() ?? 0;
syncStatus = AttemptingSyncStatus();
if (_isolate != null) {
final runningIsolate = await _isolate!;
runningIsolate.kill(priority: Isolate.immediate);
}
final receivePort = ReceivePort();
_isolate = Isolate.spawn(
startRefresh,
ScanData(
sendPort: receivePort.sendPort,
silentAddress: walletAddresses.silentAddress!.toString(),
scanPrivkeyCompressed:
walletAddresses.silentAddress!.scanPrivkey.toCompressedHex().fromHex,
spendPubkeyCompressed:
walletAddresses.silentAddress!.spendPubkey.toCompressedHex().fromHex,
networkType: networkType,
height: height,
chainTip: currentChainTip,
electrumClient: ElectrumClient(),
transactionHistoryIds: transactionHistory.transactions.keys.toList(),
node: electrumClient.uri.toString(),
));
await for (var message in receivePort) {
if (message is BitcoinUnspent) {
unspentCoins.add(message);
walletAddresses.addresses.add(message.bitcoinAddressRecord);
await save();
await updateUnspent();
await updateBalance();
await updateTransactions();
_subscribeForUpdates();
}
// check if is a SyncStatus type since "is SyncStatus" doesn't work here
if (message is SyncResponse) {
syncStatus = message.syncStatus;
walletInfo.restoreHeight = message.height;
await walletInfo.save();
}
}
} }
@action @action
@ -190,6 +234,12 @@ abstract class ElectrumWalletBase
} }
}; };
syncStatus = ConnectedSyncStatus(); syncStatus = ConnectedSyncStatus();
final currentChainTip = await electrumClient.getCurrentBlockChainTip();
if ((currentChainTip ?? 0) > walletInfo.restoreHeight) {
_setListeners(walletInfo.restoreHeight, chainTip: currentChainTip);
}
} catch (e) { } catch (e) {
print(e.toString()); print(e.toString());
syncStatus = FailedSyncStatus(); syncStatus = FailedSyncStatus();
@ -299,14 +349,13 @@ abstract class ElectrumWalletBase
outpoints.add(bitcoin.Outpoint(txid: utx.hash, index: utx.vout)); outpoints.add(bitcoin.Outpoint(txid: utx.hash, index: utx.vout));
print(utx.bitcoinAddressRecord.silentPaymentTweak);
if (utx.bitcoinAddressRecord.silentPaymentTweak != null) { if (utx.bitcoinAddressRecord.silentPaymentTweak != null) {
// https://github.com/bitcoin/bips/blob/c55f80c53c98642357712c1839cfdc0551d531c4/bip-0352.mediawiki#user-content-Spending // https://github.com/bitcoin/bips/blob/c55f80c53c98642357712c1839cfdc0551d531c4/bip-0352.mediawiki#user-content-Spending
final d = bitcoin.PrivateKey.fromHex(bitcoin.getSecp256k1(), final d = bitcoin.PrivateKey.fromHex(bitcoin.getSecp256k1(),
walletAddresses.silentAddress!.spendPrivkey.toCompressedHex()) walletAddresses.silentAddress!.spendPrivkey.toCompressedHex())
.tweakAdd(utx.bitcoinAddressRecord.silentPaymentTweak!.bigint)!; .tweakAdd(utx.bitcoinAddressRecord.silentPaymentTweak!.fromHex.bigint)!;
inputPrivKeys.add(bitcoin.PrivateKeyInfo(d, true)); inputPrivKeys.add(bitcoin.PrivateKeyInfo(d, utx.isTaproot));
final point = bitcoin.ECPublic.fromHex(d.publicKey.toHex()).toTapPoint(); final point = bitcoin.ECPublic.fromHex(d.publicKey.toHex()).toTapPoint();
final p2tr = bitcoin.P2trAddress(program: point); final p2tr = bitcoin.P2trAddress(program: point);
@ -330,7 +379,7 @@ abstract class ElectrumWalletBase
network: networkType) network: networkType)
.privateKey! .privateKey!
.hex), .hex),
false)); utx.isTaproot));
bitcoin.ECPair keyPair = generateKeyPair( bitcoin.ECPair keyPair = generateKeyPair(
hd: utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd, hd: utx.bitcoinAddressRecord.isHidden ? walletAddresses.sideHd : walletAddresses.mainHd,
@ -372,7 +421,6 @@ abstract class ElectrumWalletBase
final outputAmount = hasMultiDestination ? item.formattedCryptoAmount : amount; final outputAmount = hasMultiDestination ? item.formattedCryptoAmount : amount;
final outputAddress = item.isParsedAddress ? item.extractedAddress! : item.address; final outputAddress = item.isParsedAddress ? item.extractedAddress! : item.address;
if (outputAddress.startsWith('tsp1')) { if (outputAddress.startsWith('tsp1')) {
print([outputAddress, outputAmount!]);
silentPaymentDestinations silentPaymentDestinations
.add(bitcoin.SilentPaymentDestination.fromAddress(outputAddress, outputAmount!)); .add(bitcoin.SilentPaymentDestination.fromAddress(outputAddress, outputAmount!));
} else { } else {
@ -382,9 +430,8 @@ abstract class ElectrumWalletBase
if (silentPaymentDestinations.isNotEmpty) { if (silentPaymentDestinations.isNotEmpty) {
final outpointsHash = bitcoin.SilentPayment.hashOutpoints(outpoints); final outpointsHash = bitcoin.SilentPayment.hashOutpoints(outpoints);
final aSum = bitcoin.SilentPayment.getSumInputPrivKeys(inputPrivKeys);
final generatedOutputs = bitcoin.SilentPayment.generateMultipleRecipientPubkeys( final generatedOutputs = bitcoin.SilentPayment.generateMultipleRecipientPubkeys(
aSum, outpointsHash, silentPaymentDestinations); inputPrivKeys, outpointsHash, silentPaymentDestinations);
generatedOutputs.forEach((recipientSilentAddress, generatedOutput) { generatedOutputs.forEach((recipientSilentAddress, generatedOutput) {
generatedOutput.forEach((output) { generatedOutput.forEach((output) {
@ -544,60 +591,12 @@ abstract class ElectrumWalletBase
@action @action
@override @override
Future<void> rescan({required int height, int? chainTip, ScanData? scanData}) async { Future<void> rescan({required int height, int? chainTip, ScanData? scanData}) async {
syncStatus = AttemptingSyncStatus(); if (height >= walletInfo.restoreHeight) {
syncStatus = SyncedSyncStatus();
walletInfo.restoreHeight = height; return;
await walletInfo.save();
final currentChainTip = chainTip ?? await electrumClient.getCurrentBlockChainTip() ?? 0;
if (_isolate != null) {
final runningIsolate = await _isolate!;
runningIsolate.kill(priority: Isolate.immediate);
} }
// if (currentChainTip <= height) { _setListeners(height);
// syncStatus = SyncedSyncStatus();
// return;
// }
final receivePort = ReceivePort();
_isolate = Isolate.spawn(
startRefresh,
ScanData(
sendPort: receivePort.sendPort,
silentAddress: walletAddresses.silentAddress!.toString(),
scanPrivkeyCompressed:
walletAddresses.silentAddress!.scanPrivkey.toCompressedHex().fromHex,
spendPubkeyCompressed:
walletAddresses.silentAddress!.spendPubkey.toCompressedHex().fromHex,
networkType: networkType,
height: walletInfo.restoreHeight,
chainTip: currentChainTip,
initialSyncHeight: _initialSyncHeight,
electrumClient: ElectrumClient(),
transactionHistoryIds: transactionHistory.transactions.keys.toList(),
node: electrumClient.uri.toString()));
await for (var message in receivePort) {
if (message is BitcoinUnspent) {
unspentCoins.add(message);
walletAddresses.addresses.add(message.bitcoinAddressRecord);
await save();
await updateUnspent();
await updateBalance();
await updateTransactions();
_subscribeForUpdates();
}
// check if is a SyncStatus type since "is SyncStatus" doesn't work here
if (message is SyncResponse) {
syncStatus = message.syncStatus;
walletInfo.restoreHeight = message.height;
await walletInfo.save();
}
}
} }
@override @override
@ -605,6 +604,7 @@ abstract class ElectrumWalletBase
try { try {
await electrumClient.close(); await electrumClient.close();
} catch (_) {} } catch (_) {}
_autoSaveTimer?.cancel();
} }
Future<String> makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type); Future<String> makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type);
@ -780,6 +780,20 @@ abstract class ElectrumWalletBase
} }
}); });
}); });
await _chainTipUpdateSubject?.close();
_chainTipUpdateSubject = electrumClient.chainTipUpdate();
_chainTipUpdateSubject!.listen((event) async {
try {
rescan(height: walletInfo.restoreHeight);
} catch (e, s) {
print(e.toString());
_onError?.call(FlutterErrorDetails(
exception: e,
stack: s,
library: this.runtimeType.toString(),
));
}
});
} }
Future<ElectrumBalance> _fetchBalances() async { Future<ElectrumBalance> _fetchBalances() async {
@ -864,8 +878,6 @@ abstract class ElectrumWalletBase
final currentHeight = await electrumClient.getCurrentBlockChainTip(); final currentHeight = await electrumClient.getCurrentBlockChainTip();
if (currentHeight != null) walletInfo.restoreHeight = currentHeight; if (currentHeight != null) walletInfo.restoreHeight = currentHeight;
} }
_initialSyncHeight = walletInfo.restoreHeight;
} }
} }
@ -929,40 +941,35 @@ class ScanData {
final String node; final String node;
final bitcoin.NetworkType networkType; final bitcoin.NetworkType networkType;
final int chainTip; final int chainTip;
final int initialSyncHeight;
final ElectrumClient electrumClient; final ElectrumClient electrumClient;
final List<String> transactionHistoryIds; final List<String> transactionHistoryIds;
final int? checkpointTxPos; ScanData({
required this.sendPort,
required this.scanPrivkeyCompressed,
required this.spendPubkeyCompressed,
required this.silentAddress,
required this.height,
required this.node,
required this.networkType,
required this.chainTip,
required this.electrumClient,
required this.transactionHistoryIds,
});
ScanData( factory ScanData.fromHeight(ScanData scanData, int newHeight) {
{required this.sendPort,
required this.scanPrivkeyCompressed,
required this.spendPubkeyCompressed,
required this.silentAddress,
required this.height,
required this.node,
required this.networkType,
required this.chainTip,
required this.initialSyncHeight,
required this.electrumClient,
required this.transactionHistoryIds,
this.checkpointTxPos});
factory ScanData.withCheckpoint(ScanData scanData, int newHeight, int? checkpointTx) {
return ScanData( return ScanData(
sendPort: scanData.sendPort, sendPort: scanData.sendPort,
scanPrivkeyCompressed: scanData.scanPrivkeyCompressed, scanPrivkeyCompressed: scanData.scanPrivkeyCompressed,
spendPubkeyCompressed: scanData.spendPubkeyCompressed, spendPubkeyCompressed: scanData.spendPubkeyCompressed,
silentAddress: scanData.silentAddress, silentAddress: scanData.silentAddress,
height: newHeight, height: newHeight,
node: scanData.node, node: scanData.node,
networkType: scanData.networkType, networkType: scanData.networkType,
chainTip: scanData.chainTip, chainTip: scanData.chainTip,
initialSyncHeight: scanData.initialSyncHeight, transactionHistoryIds: scanData.transactionHistoryIds,
electrumClient: scanData.electrumClient, electrumClient: scanData.electrumClient,
transactionHistoryIds: scanData.transactionHistoryIds, );
checkpointTxPos: checkpointTx);
} }
} }
@ -974,181 +981,235 @@ class SyncResponse {
} }
Future<void> startRefresh(ScanData scanData) async { Future<void> startRefresh(ScanData scanData) async {
final currentChainTip = scanData.chainTip; var cachedBlockchainHeight = scanData.chainTip;
// if (scanData.height >= currentChainTip) {
// scanData.sendPort.send(SyncResponse(scanData.height, SyncedSyncStatus()));
// return;
// }
var checkpointTxPos = scanData.checkpointTxPos; Future<int> getNodeHeightOrUpdate(int baseHeight) async {
final height = scanData.height; if (cachedBlockchainHeight < baseHeight || cachedBlockchainHeight == 0) {
final electrumClient = scanData.electrumClient;
if (!electrumClient.isConnected) {
final node = scanData.node;
await electrumClient.connectToUri(Uri.parse(node));
}
print(["HEIGHT:", height]); cachedBlockchainHeight =
await electrumClient.getCurrentBlockChainTip() ?? cachedBlockchainHeight;
try {
final track = currentChainTip - height;
final diff = (currentChainTip - scanData.initialSyncHeight) - track;
final ptc = diff <= 0 ? 0.0 : diff / track;
scanData.sendPort.send(SyncResponse(height, SyncingSyncStatus(track, ptc)));
final electrumClient = scanData.electrumClient;
if (!electrumClient.isConnected) {
final node = scanData.node;
await electrumClient.connectToUri(Uri.parse(node));
} }
// tx pos always begin from 1 --> we know pos 0 is coinbase tx return cachedBlockchainHeight;
int pos = checkpointTxPos ?? 1; }
while (true) {
try {
final txid = await electrumClient.getTxidFromPos(height: height, pos: pos);
print(["scanning tx:", txid]);
// TODO: if already tx already scanned & stored skip var lastKnownBlockHeight = 0;
// if (scanData.transactionHistoryIds.contains(txid)) { var initialSyncHeight = 0;
// // already scanned tx, continue to next tx
// checkpointTxPos = pos;
// pos++;
// continue;
// }
List<String> pubkeys = []; var syncHeight = scanData.height;
List<bitcoin.Outpoint> outpoints = []; var currentChainTip = scanData.chainTip;
try { if (syncHeight <= 0) {
final txBundle = await getTransactionExpanded( syncHeight = currentChainTip;
hash: txid, }
height: height,
electrumClient: electrumClient,
networkType: scanData.networkType);
bool skip = false; if (initialSyncHeight <= 0) {
txBundle.originalTransaction.ins.forEach((input) { initialSyncHeight = syncHeight;
if (input.witness == null) { }
skip = true;
return;
}
if (input.witness!.length != 2) { if (lastKnownBlockHeight == syncHeight) {
skip = true; scanData.sendPort.send(SyncResponse(currentChainTip, SyncedSyncStatus()));
return; return;
} }
final pubkey = input.witness![1].hex; // Run this until no more blocks left to scan txs. At first this was recursive
pubkeys.add(pubkey); // i.e. re-calling the startRefresh function but this was easier for the above values to retain
outpoints.add(bitcoin.Outpoint( // their initial values
txid: HEX.encode(input.hash!.reversed.toList()), index: input.index!)); while (true) {
}); lastKnownBlockHeight = syncHeight;
if (skip) { final syncingStatus =
// skipped tx, save checkpoint in case of issues and continue to next tx SyncingSyncStatus.fromHeightValues(currentChainTip, initialSyncHeight, syncHeight);
checkpointTxPos = pos; scanData.sendPort.send(SyncResponse(syncHeight, syncingStatus));
pos++;
continue; if (syncingStatus.blocksLeft <= 0) {
scanData.sendPort.send(SyncResponse(currentChainTip, SyncedSyncStatus()));
return;
}
print(["Scanning from height:", syncHeight]);
try {
final networkPath =
scanData.networkType.network == bitcoin.BtcNetwork.mainnet ? "" : "/testnet";
// This endpoint gets up to 10 latest blocks from the given height
final tenNewestBlocks =
(await http.get(Uri.parse("https://blockstream.info$networkPath/api/blocks/$syncHeight")))
.body;
var decodedBlocks = json.decode(tenNewestBlocks) as List<dynamic>;
decodedBlocks.sort((a, b) => (a["height"] as int).compareTo(b["height"] as int));
decodedBlocks =
decodedBlocks.where((element) => (element["height"] as int) >= syncHeight).toList();
// for each block, get up to 25 txs
for (var i = 0; i < decodedBlocks.length; i++) {
final blockJson = decodedBlocks[i];
final blockHash = blockJson["id"];
final txCount = blockJson["tx_count"] as int;
// print(["Scanning block index:", i, "with tx count:", txCount]);
int startIndex = 0;
// go through each tx in block until no more txs are left
while (startIndex < txCount) {
// This endpoint gets up to 25 txs from the given block hash and start index
final twentyFiveTxs = json.decode((await http.get(Uri.parse(
"https://blockstream.info$networkPath/api/block/$blockHash/txs/$startIndex")))
.body) as List<dynamic>;
// print(["Scanning txs index:", startIndex]);
// For each tx, apply silent payment filtering and do shared secret calculation when applied
for (var i = 0; i < twentyFiveTxs.length; i++) {
try {
final tx = twentyFiveTxs[i];
final txid = tx["txid"] as String;
// print(["Scanning tx:", txid]);
// TODO: if tx already scanned & stored skip
// if (scanData.transactionHistoryIds.contains(txid)) {
// // already scanned tx, continue to next tx
// pos++;
// continue;
// }
List<String> pubkeys = [];
List<bitcoin.Outpoint> outpoints = [];
bool skip = false;
for (var i = 0; i < (tx["vin"] as List<dynamic>).length; i++) {
final input = tx["vin"][i];
if (input["witness"] == null) {
skip = true;
// print("Skipping, no witness");
break;
}
if (input["witness"].length != 2) {
skip = true;
// print("Skipping, invalid witness");
break;
}
final pubkey = input["witness"][1] as String;
pubkeys.add(pubkey);
outpoints.add(bitcoin.Outpoint(
txid: HEX.encode((input["txid"] as String).fromHex.reversed.toList()),
index: input["vout"] as int));
}
if (skip) {
// skipped tx, continue to next tx
continue;
}
Map<Uint8List, bitcoin.Outpoint> outpointsByP2TRpubkey = {};
for (var i = 0; i < (tx["vout"] as List<dynamic>).length; i++) {
final output = tx["vout"][i];
if (output["scriptpubkey_type"] != "v1_p2tr") {
// print("Skipping, not a v1_p2tr output");
break;
}
final script = (output["scriptpubkey"] as String).fromHex;
// final alreadySpentOutput = (await electrumClient.getHistory(
// scriptHashFromScript(script, networkType: scanData.networkType)))
// .length >
// 1;
// if (alreadySpentOutput) {
// print("Skipping, invalid witness");
// break;
// }
final p2tr = bitcoin.P2trAddress(program: script.sublist(2).hex);
final address = p2tr.toAddress(scanData.networkType);
// print(["Verifying taproot address:", address]);
outpointsByP2TRpubkey[p2tr.toScriptPubKey().toBytes()] =
bitcoin.Outpoint(txid: txid, index: i, value: output["value"] as int);
}
if (pubkeys.isEmpty || outpoints.isEmpty || outpointsByP2TRpubkey.isEmpty) {
// skipped tx, continue to next tx
continue;
}
final outpointHash = bitcoin.SilentPayment.hashOutpoints(outpoints);
final result = bitcoin.scanOutputs(
bitcoin.PrivateKey.fromHex(
bitcoin.getSecp256k1(), scanData.scanPrivkeyCompressed.hex),
bitcoin.PublicKey.fromHex(
bitcoin.getSecp256k1(), scanData.spendPubkeyCompressed.hex),
bitcoin.getSumInputPubKeys(pubkeys),
outpointHash,
outpointsByP2TRpubkey.keys.toList());
if (result.isEmpty) {
// no results tx, continue to next tx
continue;
}
if (result.length > 1) {
print("MULTIPLE UNSPENT COINS FOUND!");
} else {
print("UNSPENT COIN FOUND!");
}
print(result);
result.forEach((key, value) {
final outpoint = outpointsByP2TRpubkey[key.fromHex];
if (outpoint == null) {
return;
}
// found utxo for tx
scanData.sendPort.send(BitcoinUnspent(
BitcoinAddressRecord(
key,
index: 0,
isHidden: false,
isUsed: true,
silentAddressLabel: null,
silentPaymentTweak: value,
),
outpoint.txid,
outpoint.value!,
outpoint.index,
silentPaymentTweak: value,
));
});
} catch (_) {}
} }
Map<String, bitcoin.Outpoint> outpointsByP2TRpubkey = {}; // Finished scanning batch of txs in block, add 25 to start index and continue to next block in loop
int i = 0; startIndex += 25;
txBundle.originalTransaction.outs.forEach((output) {
if (bitcoin.classifyOutput(output.script!) != "taproot") {
return;
}
final address = bitcoin.P2trAddress(program: output.script!.sublist(2).hex).toAddress(
scanData.networkType.bech32 == bitcoin.testnet.bech32
? bitcoin.NetworkInfo.TESTNET
: bitcoin.NetworkInfo.BITCOIN);
print(["verifying taproot address:", address]);
outpointsByP2TRpubkey[address] =
bitcoin.Outpoint(txid: txid, index: i, value: output.value!);
i++;
});
if (pubkeys.isEmpty || outpoints.isEmpty || outpointsByP2TRpubkey.isEmpty) {
// skipped tx, save checkpoint in case of issues and continue to next tx
checkpointTxPos = pos;
pos++;
continue;
}
Uint8List sumOfInputPublicKeys =
bitcoin.getSumInputPubKeys(pubkeys).toCompressedHex().fromHex;
final outpointHash = bitcoin.SilentPayment.hashOutpoints(outpoints);
final result = bitcoin.scanOutputs(
scanData.scanPrivkeyCompressed,
scanData.spendPubkeyCompressed,
sumOfInputPublicKeys,
outpointHash,
outpointsByP2TRpubkey.keys.toList());
if (result.isEmpty) {
// no results tx, save checkpoint in case of issues and continue to next tx
checkpointTxPos = pos;
pos++;
continue;
}
if (result.length > 1) {
print("MULTIPLE UNSPENT COINS FOUND!");
} else {
print("UNSPENT COIN FOUND!");
}
print(result);
result.forEach((key, value) {
final outpoint = outpointsByP2TRpubkey[key];
if (outpoint == null) {
return;
}
// found utxo for tx
scanData.sendPort.send(BitcoinUnspent(
BitcoinAddressRecord(
key,
index: 0,
isHidden: false,
isUsed: true,
silentAddressLabel: null,
silentPaymentTweak: value,
),
outpoint.txid,
outpoint.value!,
outpoint.index,
silentPaymentTweak: value,
));
});
} catch (e, stacktrace) {
print(stacktrace);
print(e.toString());
} }
pos++; // Finished scanning block, add 1 to height and continue to next block in loop
} catch (e, stacktrace) { syncHeight += 1;
print(stacktrace); currentChainTip = await getNodeHeightOrUpdate(syncHeight);
print(e.toString()); scanData.sendPort.send(SyncResponse(syncHeight,
SyncingSyncStatus.fromHeightValues(currentChainTip, initialSyncHeight, syncHeight)));
// last position, no more txs for the given block height
break;
} }
} catch (e, stacktrace) {
print(stacktrace);
print(e.toString());
break;
} }
final newHeight = height + 1;
if (newHeight < currentChainTip) {
// recursive, scan next block again until at the current tip
return startRefresh(ScanData.withCheckpoint(scanData, newHeight, 1));
}
// otherwise, finished scanning
scanData.sendPort.send(SyncResponse(currentChainTip, SyncedSyncStatus()));
} catch (e, stacktrace) {
print(stacktrace);
print(e.toString());
startRefresh(ScanData.withCheckpoint(scanData, height, checkpointTxPos));
} }
} }

View file

@ -1,9 +1,15 @@
import 'package:bitcoin_flutter/bitcoin_flutter.dart'; import 'package:bitcoin_flutter/bitcoin_flutter.dart';
final litecoinNetwork = NetworkType( final litecoinNetwork = NetworkType(
messagePrefix: '\x19Litecoin Signed Message:\n', messagePrefix: '\x19Litecoin Signed Message:\n',
bech32: 'ltc', bech32: 'ltc',
bip32: Bip32Type(public: 0x0488b21e, private: 0x0488ade4), bip32: Bip32Type(public: 0x0488b21e, private: 0x0488ade4),
pubKeyHash: 0x30, pubKeyHash: 0x30,
scriptHash: 0x32, scriptHash: 0x32,
wif: 0xb0); wif: 0xb0,
p2pkhPrefix: bitcoin.p2pkhPrefix,
network: bitcoin.network,
p2shPrefix: bitcoin.p2shPrefix,
extendPublic: bitcoin.extendPublic,
extendPrivate: bitcoin.extendPrivate,
);

View file

@ -1,9 +1,25 @@
import 'dart:typed_data';
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin; import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
String scriptHash(String address, {required bitcoin.NetworkType networkType}) { String scriptHash(String address, {required bitcoin.NetworkType networkType}) {
final outputScript = final outputScript = bitcoin.Address.addressToOutputScript(address, networkType);
bitcoin.Address.addressToOutputScript(address, networkType); final parts = sha256.convert(outputScript).toString().split('');
var res = '';
for (var i = parts.length - 1; i >= 0; i--) {
final char = parts[i];
i--;
final nextChar = parts[i];
res += nextChar;
res += char;
}
return res;
}
String scriptHashFromScript(Uint8List outputScript, {required bitcoin.NetworkType networkType}) {
final parts = sha256.convert(outputScript).toString().split(''); final parts = sha256.convert(outputScript).toString().split('');
var res = ''; var res = '';

View file

@ -88,7 +88,7 @@ packages:
description: description:
path: "." path: "."
ref: silent-payments ref: silent-payments
resolved-ref: c96f9d5344e7c6a6164d0c184d77cbd6d8e21e65 resolved-ref: "18dc839f06b98a9319334e3fac316e8be9ca52dc"
url: "https://github.com/cake-tech/bitcoin_flutter" url: "https://github.com/cake-tech/bitcoin_flutter"
source: git source: git
version: "2.0.2" version: "2.0.2"
@ -168,10 +168,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: built_value name: built_value
sha256: "723b4021e903217dfc445ec4cf5b42e27975aece1fc4ebbc1ca6329c2d9fb54e" sha256: "69acb7007eb2a31dc901512bfe0f7b767168be34cb734835d54c070bfa74c1b2"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "8.7.0" version: "8.8.0"
characters: characters:
dependency: transitive dependency: transitive
description: description:
@ -200,10 +200,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: code_builder name: code_builder
sha256: "1be9be30396d7e4c0db42c35ea6ccd7cc6a1e19916b5dc64d6ac216b5544d677" sha256: b2151ce26a06171005b379ecff6e08d34c470180ffe16b8e14b6d52be292b55f
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.7.0" version: "4.8.0"
collection: collection:
dependency: transitive dependency: transitive
description: description:
@ -256,7 +256,7 @@ packages:
description: description:
path: "." path: "."
ref: silent-payments ref: silent-payments
resolved-ref: ad7b1cccb54b5feba8ead4a0f67cb98a5a01a88b resolved-ref: "09343cf1bc03e74715413972dbec62a2505f5011"
url: "https://github.com/cake-tech/dart-elliptic" url: "https://github.com/cake-tech/dart-elliptic"
source: git source: git
version: "0.3.10" version: "0.3.10"

View file

@ -14,6 +14,16 @@ class SyncingSyncStatus extends SyncStatus {
@override @override
String toString() => '$blocksLeft'; String toString() => '$blocksLeft';
factory SyncingSyncStatus.fromHeightValues(int chainTip, int initialSyncHeight, int syncHeight) {
final track = chainTip - initialSyncHeight;
final diff = track - (chainTip - syncHeight);
final ptc = diff <= 0 ? 0.0 : diff / track;
final left = chainTip - syncHeight;
// sum 1 because if at the chain tip, will say "0 blocks left"
return SyncingSyncStatus(left + 1, ptc);
}
} }
class SyncedSyncStatus extends SyncStatus { class SyncedSyncStatus extends SyncStatus {

View file

@ -1,6 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin; // import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
import 'package:cake_wallet/core/wallet_loading_service.dart'; import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/store/settings_store.dart';
@ -8,9 +8,9 @@ import 'package:cake_wallet/utils/device_info.dart';
import 'package:cake_wallet/view_model/settings/sync_mode.dart'; import 'package:cake_wallet/view_model/settings/sync_mode.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart';
import 'package:cw_bitcoin/bitcoin_address_record.dart'; // import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_bitcoin/bitcoin_unspent.dart'; // import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_bitcoin/bitcoin_wallet.dart'; // import 'package:cw_bitcoin/bitcoin_wallet.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@ -21,7 +21,7 @@ import 'package:cake_wallet/main.dart';
import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/di.dart';
const moneroSyncTaskKey = "com.fotolockr.cakewallet.monero_sync_task"; const moneroSyncTaskKey = "com.fotolockr.cakewallet.monero_sync_task";
const bitcoinSilentPaymentsSyncTaskKey = "com.fotolockr.cakewallet.bitcoin_sync_task"; // const bitcoinSilentPaymentsSyncTaskKey = "com.fotolockr.cakewallet.bitcoin_sync_task";
@pragma('vm:entry-point') @pragma('vm:entry-point')
void callbackDispatcher() { void callbackDispatcher() {
@ -85,129 +85,129 @@ void callbackDispatcher() {
} }
break; break;
case bitcoinSilentPaymentsSyncTaskKey: // case bitcoinSilentPaymentsSyncTaskKey:
/// The work manager runs on a separate isolate from the main flutter isolate. // /// The work manager runs on a separate isolate from the main flutter isolate.
/// thus we initialize app configs first; hive, getIt, etc... // /// thus we initialize app configs first; hive, getIt, etc...
await initializeAppConfigs(); // await initializeAppConfigs();
final walletLoadingService = getIt.get<WalletLoadingService>(); // final walletLoadingService = getIt.get<WalletLoadingService>();
final name = getIt.get<SharedPreferences>().getString(PreferencesKey.currentWalletName); // final name = getIt.get<SharedPreferences>().getString(PreferencesKey.currentWalletName);
final wallet = // final wallet =
await walletLoadingService.load(WalletType.bitcoin, name!) as BitcoinWallet; // await walletLoadingService.load(WalletType.bitcoin, name!) as BitcoinWallet;
String? checkpointTx = inputData!["checkpoint_tx"] as String?; // String? checkpointTx = inputData!["checkpoint_tx"] as String?;
try { // try {
final height = wallet.walletInfo.restoreHeight; // final height = wallet.walletInfo.restoreHeight;
print(["HEIGHT:", height]); // print(["HEIGHT:", height]);
List<String> txids = []; // List<String> txids = [];
int pos = 0; // int pos = 0;
while (pos < 150) { // while (pos < 150) {
var txid = await wallet.electrumClient.getTxidFromPos(height: height, pos: pos); // var txid = await wallet.electrumClient.getTxidFromPos(height: height, pos: pos);
print(["TXID", txid]); // print(["TXID", txid]);
txids.add(txid); // txids.add(txid);
pos++; // pos++;
} // }
if (checkpointTx != null && checkpointTx.isNotEmpty) { // if (checkpointTx != null && checkpointTx.isNotEmpty) {
txids = txids.sublist(txids.indexOf(checkpointTx) + 1); // txids = txids.sublist(txids.indexOf(checkpointTx) + 1);
} // }
List<BitcoinUnspent> unspentCoins = []; // List<BitcoinUnspent> unspentCoins = [];
for (var txid in txids) { // for (var txid in txids) {
checkpointTx = txid.toString(); // checkpointTx = txid.toString();
List<String> pubkeys = []; // List<String> pubkeys = [];
List<bitcoin.Outpoint> outpoints = []; // List<bitcoin.Outpoint> outpoints = [];
Map<String, bitcoin.Outpoint> outpointsByP2TRpubkey = {}; // Map<String, bitcoin.Outpoint> outpointsByP2TRpubkey = {};
// final txInfo = // // final txInfo =
// await wallet.fetchTransactionInfo(hash: txid.toString(), height: height); // // await wallet.fetchTransactionInfo(hash: txid.toString(), height: height);
// print(txInfo); // // print(txInfo);
bool skip = false; // bool skip = false;
// obj["vin"].forEach((input) { // // obj["vin"].forEach((input) {
// if (input["witness"] == null) { // // if (input["witness"] == null) {
// skip = true; // // skip = true;
// return; // // return;
// } // // }
// final witness = input["witness"] as List<dynamic>; // // final witness = input["witness"] as List<dynamic>;
// if (witness.length != 2) { // // if (witness.length != 2) {
// skip = true; // // skip = true;
// return; // // return;
// } // // }
// final pubkey = witness[1] as String; // // final pubkey = witness[1] as String;
// pubkeys.add(pubkey); // // pubkeys.add(pubkey);
// outpoints.add(bitcoin.Outpoint(txid: input["txid"] as String, index: input["vout"] as int)); // // outpoints.add(bitcoin.Outpoint(txid: input["txid"] as String, index: input["vout"] as int));
// }); // // });
// if (skip) continue; // // if (skip) continue;
// int i = 0; // // int i = 0;
// obj['vout'].forEach((out) { // // obj['vout'].forEach((out) {
// if (out["scriptpubkey_type"] != "v1_p2tr") { // // if (out["scriptpubkey_type"] != "v1_p2tr") {
// return; // // return;
// } // // }
// outpointsByP2TRpubkey[out['scriptpubkey_address'] as String] = // // outpointsByP2TRpubkey[out['scriptpubkey_address'] as String] =
// bitcoin.Outpoint(txid: txid.toString(), index: i, value: out["value"] as int); // // bitcoin.Outpoint(txid: txid.toString(), index: i, value: out["value"] as int);
// i++; // // i++;
// }); // // });
if (pubkeys.isEmpty && outpoints.isEmpty && outpointsByP2TRpubkey.isEmpty) { // if (pubkeys.isEmpty && outpoints.isEmpty && outpointsByP2TRpubkey.isEmpty) {
continue; // continue;
} // }
Uint8List sumOfInputPublicKeys = // Uint8List sumOfInputPublicKeys =
bitcoin.getSumInputPubKeys(pubkeys).toCompressedHex().fromHex; // bitcoin.getSumInputPubKeys(pubkeys).toCompressedHex().fromHex;
final outpointHash = bitcoin.SilentPayment.hashOutpoints(outpoints); // final outpointHash = bitcoin.SilentPayment.hashOutpoints(outpoints);
final result = bitcoin.scanOutputs( // final result = bitcoin.scanOutputs(
wallet.walletAddresses.silentAddress!.scanPrivkey.toCompressedHex().fromHex, // wallet.walletAddresses.silentAddress!.scanPrivkey.toCompressedHex().fromHex,
wallet.walletAddresses.silentAddress!.spendPubkey.toCompressedHex().fromHex, // wallet.walletAddresses.silentAddress!.spendPubkey.toCompressedHex().fromHex,
sumOfInputPublicKeys, // sumOfInputPublicKeys,
outpointHash, // outpointHash,
outpointsByP2TRpubkey.keys.toList()); // outpointsByP2TRpubkey.keys.toList());
if (result.isEmpty) { // if (result.isEmpty) {
continue; // continue;
} // }
result.forEach((key, value) { // result.forEach((key, value) {
final outpoint = outpointsByP2TRpubkey[key]; // final outpoint = outpointsByP2TRpubkey[key];
if (outpoint == null) { // if (outpoint == null) {
return; // return;
} // }
unspentCoins.add(BitcoinUnspent( // unspentCoins.add(BitcoinUnspent(
BitcoinAddressRecord(key, index: 0), // BitcoinAddressRecord(key, index: 0),
outpoint.txid, // outpoint.txid,
outpoint.value!, // outpoint.value!,
outpoint.index, // outpoint.index,
silentPaymentTweak: value, // silentPaymentTweak: value,
)); // ));
}); // });
} // }
break; // break;
} catch (e, stacktrace) { // } catch (e, stacktrace) {
print(stacktrace); // print(stacktrace);
print(e.toString()); // print(e.toString());
// timeout, wait 30sec // // timeout, wait 30sec
// return Future.delayed(const Duration(seconds: 30), // // return Future.delayed(const Duration(seconds: 30),
// () => startRefresh({"checkpoint_tx": checkpointTx ?? ""})); // // () => startRefresh({"checkpoint_tx": checkpointTx ?? ""}));
} // }
break; // break;
} }
return Future.value(true); return Future.value(true);
@ -276,59 +276,59 @@ class BackgroundTasks {
); );
} }
bool hasBitcoin = getIt // bool hasBitcoin = getIt
.get<WalletListViewModel>() // .get<WalletListViewModel>()
.wallets // .wallets
.any((element) => element.type == WalletType.bitcoin); // .any((element) => element.type == WalletType.bitcoin);
/// if its not android nor ios, or the user has no monero wallets; exit // /// if its not android nor ios, or the user has no monero wallets; exit
if (hasBitcoin) { // if (hasBitcoin) {
final settingsStore = getIt.get<SettingsStore>(); // final settingsStore = getIt.get<SettingsStore>();
final SyncMode syncMode = settingsStore.currentSyncMode; // final SyncMode syncMode = settingsStore.currentSyncMode;
final bool syncAll = settingsStore.currentSyncAll; // final bool syncAll = settingsStore.currentSyncAll;
if (syncMode.type == SyncType.disabled) { // if (syncMode.type == SyncType.disabled) {
cancelSyncTask(); // cancelSyncTask();
return; // return;
} // }
await Workmanager().initialize( // await Workmanager().initialize(
callbackDispatcher, // callbackDispatcher,
isInDebugMode: kDebugMode, // isInDebugMode: kDebugMode,
); // );
final inputData = <String, dynamic>{"sync_all": syncAll}; // final inputData = <String, dynamic>{"sync_all": syncAll};
final constraints = Constraints( // final constraints = Constraints(
networkType: // networkType:
syncMode.type == SyncType.unobtrusive ? NetworkType.unmetered : NetworkType.connected, // syncMode.type == SyncType.unobtrusive ? NetworkType.unmetered : NetworkType.connected,
requiresBatteryNotLow: syncMode.type == SyncType.unobtrusive, // requiresBatteryNotLow: syncMode.type == SyncType.unobtrusive,
requiresCharging: syncMode.type == SyncType.unobtrusive, // requiresCharging: syncMode.type == SyncType.unobtrusive,
requiresDeviceIdle: syncMode.type == SyncType.unobtrusive, // requiresDeviceIdle: syncMode.type == SyncType.unobtrusive,
); // );
if (Platform.isIOS) { // if (Platform.isIOS) {
await Workmanager().registerOneOffTask( // await Workmanager().registerOneOffTask(
bitcoinSilentPaymentsSyncTaskKey, // bitcoinSilentPaymentsSyncTaskKey,
bitcoinSilentPaymentsSyncTaskKey, // bitcoinSilentPaymentsSyncTaskKey,
initialDelay: syncMode.frequency, // initialDelay: syncMode.frequency,
existingWorkPolicy: ExistingWorkPolicy.replace, // existingWorkPolicy: ExistingWorkPolicy.replace,
inputData: inputData, // inputData: inputData,
constraints: constraints, // constraints: constraints,
); // );
return; // return;
} // }
await Workmanager().registerPeriodicTask( // await Workmanager().registerPeriodicTask(
bitcoinSilentPaymentsSyncTaskKey, // bitcoinSilentPaymentsSyncTaskKey,
bitcoinSilentPaymentsSyncTaskKey, // bitcoinSilentPaymentsSyncTaskKey,
initialDelay: syncMode.frequency, // initialDelay: syncMode.frequency,
frequency: syncMode.frequency, // frequency: syncMode.frequency,
existingWorkPolicy: changeExisting ? ExistingWorkPolicy.replace : ExistingWorkPolicy.keep, // existingWorkPolicy: changeExisting ? ExistingWorkPolicy.replace : ExistingWorkPolicy.keep,
inputData: inputData, // inputData: inputData,
constraints: constraints, // constraints: constraints,
); // );
} // }
} catch (error, stackTrace) { } catch (error, stackTrace) {
print(error); print(error);
print(stackTrace); print(stackTrace);
@ -344,128 +344,3 @@ class BackgroundTasks {
} }
} }
} }
Future<List<BitcoinUnspent>> startRefresh(Map<dynamic, dynamic> data) async {
// final rootIsolateToken = data["rootIsolateToken"] as RootIsolateToken;
// BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
/// The work manager runs on a separate isolate from the main flutter isolate.
/// thus we initialize app configs first; hive, getIt, etc...
await initializeAppConfigs();
final walletLoadingService = getIt.get<WalletLoadingService>();
final name = getIt.get<SharedPreferences>().getString(PreferencesKey.currentWalletName);
final wallet = await walletLoadingService.load(WalletType.bitcoin, name!) as BitcoinWallet;
String? checkpointTx = data["checkpoint_tx"] as String?;
try {
final height = wallet.walletInfo.restoreHeight;
print(["HEIGHT:", height]);
List<String> txids = [];
int pos = 0;
while (pos < 150) {
var txid = await wallet.electrumClient.getTxidFromPos(height: height, pos: pos);
print(["TXID", txid]);
txids.add(txid);
pos++;
}
if (checkpointTx != null && checkpointTx.isNotEmpty) {
txids = txids.sublist(txids.indexOf(checkpointTx) + 1);
}
List<BitcoinUnspent> unspentCoins = [];
for (var txid in txids) {
checkpointTx = txid.toString();
List<String> pubkeys = [];
List<bitcoin.Outpoint> outpoints = [];
Map<String, bitcoin.Outpoint> outpointsByP2TRpubkey = {};
// final txInfo = await wallet.fetchTransactionInfo(hash: txid.toString(), height: height);
// print(txInfo);
bool skip = false;
// obj["vin"].forEach((input) {
// if (input["witness"] == null) {
// skip = true;
// return;
// }
// final witness = input["witness"] as List<dynamic>;
// if (witness.length != 2) {
// skip = true;
// return;
// }
// final pubkey = witness[1] as String;
// pubkeys.add(pubkey);
// outpoints.add(bitcoin.Outpoint(txid: input["txid"] as String, index: input["vout"] as int));
// });
// if (skip) continue;
// int i = 0;
// obj['vout'].forEach((out) {
// if (out["scriptpubkey_type"] != "v1_p2tr") {
// return;
// }
// outpointsByP2TRpubkey[out['scriptpubkey_address'] as String] =
// bitcoin.Outpoint(txid: txid.toString(), index: i, value: out["value"] as int);
// i++;
// });
if (pubkeys.isEmpty && outpoints.isEmpty && outpointsByP2TRpubkey.isEmpty) {
continue;
}
Uint8List sumOfInputPublicKeys =
bitcoin.getSumInputPubKeys(pubkeys).toCompressedHex().fromHex;
final outpointHash = bitcoin.SilentPayment.hashOutpoints(outpoints);
final result = bitcoin.scanOutputs(
wallet.walletAddresses.silentAddress!.scanPrivkey.toCompressedHex().fromHex,
wallet.walletAddresses.silentAddress!.spendPubkey.toCompressedHex().fromHex,
sumOfInputPublicKeys,
outpointHash,
outpointsByP2TRpubkey.keys.toList());
if (result.isEmpty) {
continue;
}
result.forEach((key, value) {
final outpoint = outpointsByP2TRpubkey[key];
if (outpoint == null) {
return;
}
unspentCoins.add(BitcoinUnspent(
BitcoinAddressRecord(key, index: 0),
outpoint.txid,
outpoint.value!,
outpoint.index,
silentPaymentTweak: value,
));
});
}
return unspentCoins;
} catch (e, stacktrace) {
print(stacktrace);
print(e.toString());
// timeout, wait 30sec
return Future.delayed(
const Duration(seconds: 30), () => startRefresh({"checkpoint_tx": checkpointTx ?? ""}));
}
}

View file

@ -8,7 +8,6 @@
#include <cw_monero/cw_monero_plugin.h> #include <cw_monero/cw_monero_plugin.h>
#include <devicelocale/devicelocale_plugin.h> #include <devicelocale/devicelocale_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h> #include <url_launcher_linux/url_launcher_plugin.h>
void fl_register_plugins(FlPluginRegistry* registry) { void fl_register_plugins(FlPluginRegistry* registry) {
@ -18,9 +17,6 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) devicelocale_registrar = g_autoptr(FlPluginRegistrar) devicelocale_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "DevicelocalePlugin"); fl_plugin_registry_get_registrar_for_plugin(registry, "DevicelocalePlugin");
devicelocale_plugin_register_with_registrar(devicelocale_registrar); devicelocale_plugin_register_with_registrar(devicelocale_registrar);
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar = g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin"); fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar); url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);

View file

@ -5,7 +5,6 @@
list(APPEND FLUTTER_PLUGIN_LIST list(APPEND FLUTTER_PLUGIN_LIST
cw_monero cw_monero
devicelocale devicelocale
flutter_secure_storage_linux
url_launcher_linux url_launcher_linux
) )

View file

@ -8,7 +8,6 @@ import Foundation
import cw_monero import cw_monero
import device_info_plus import device_info_plus
import devicelocale import devicelocale
import flutter_secure_storage_macos
import in_app_review import in_app_review
import package_info_plus import package_info_plus
import path_provider_foundation import path_provider_foundation
@ -21,7 +20,6 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
CwMoneroPlugin.register(with: registry.registrar(forPlugin: "CwMoneroPlugin")) CwMoneroPlugin.register(with: registry.registrar(forPlugin: "CwMoneroPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin")) DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
DevicelocalePlugin.register(with: registry.registrar(forPlugin: "DevicelocalePlugin")) DevicelocalePlugin.register(with: registry.registrar(forPlugin: "DevicelocalePlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
InAppReviewPlugin.register(with: registry.registrar(forPlugin: "InAppReviewPlugin")) InAppReviewPlugin.register(with: registry.registrar(forPlugin: "InAppReviewPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin")) FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))