feat: check if node is electrs, and supports sp

This commit is contained in:
Rafael Saes 2024-05-24 15:36:53 -03:00
parent da59860241
commit cfa4515a82
5 changed files with 43 additions and 16 deletions

View file

@ -150,7 +150,8 @@ class ElectrumClient {
}
}
Future<List<String>> version() => call(method: 'server.version').then((dynamic result) {
Future<List<String>> version() =>
call(method: 'server.version', params: ["", "1.4"]).then((dynamic result) {
if (result is List) {
return result.map((dynamic val) => val.toString()).toList();
}
@ -287,9 +288,8 @@ class ElectrumClient {
);
}
Future<Map<String, dynamic>> getTweaks({required int height}) async =>
await callWithTimeout(method: 'blockchain.tweaks.get', params: [height], timeout: 10000)
as Map<String, dynamic>;
Future<dynamic> getTweaks({required int height}) async =>
await callWithTimeout(method: 'blockchain.tweaks.subscribe', params: [height, 1, false]);
Future<double> estimatefee({required int p}) =>
call(method: 'blockchain.estimatefee', params: [p]).then((dynamic result) {

View file

@ -509,8 +509,7 @@ class CWBitcoin extends Bitcoin {
Future<void> setScanningActive(Object wallet, bool active) async {
final bitcoinWallet = wallet as ElectrumWallet;
// TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore
if (!getNodeIsCakeElectrs(wallet)) {
if (!(await getNodeIsElectrsSPEnabled(wallet))) {
final node = Node(
useSSL: false,
uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}',
@ -535,8 +534,7 @@ class CWBitcoin extends Bitcoin {
@override
Future<void> rescan(Object wallet, {required int height, bool? doSingleScan}) async {
final bitcoinWallet = wallet as ElectrumWallet;
// TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore
if (!getNodeIsCakeElectrs(wallet)) {
if (!(await getNodeIsElectrsSPEnabled(wallet))) {
final node = Node(
useSSL: false,
uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}',
@ -547,13 +545,40 @@ class CWBitcoin extends Bitcoin {
bitcoinWallet.rescan(height: height, doSingleScan: doSingleScan);
}
@override
bool getNodeIsCakeElectrs(Object wallet) {
Future<bool> getNodeIsElectrs(Object wallet) async {
final bitcoinWallet = wallet as ElectrumWallet;
final node = bitcoinWallet.node;
return node?.uri.host == 'electrs.cakewallet.com' &&
node?.uri.port == (wallet.network == BitcoinNetwork.testnet ? 50002 : 50001);
final version = await bitcoinWallet.electrumClient.version();
if (version.isEmpty) {
return false;
}
final server = version[0];
if (server.toLowerCase().contains('electrs')) {
return true;
}
return false;
}
@override
Future<bool> getNodeIsElectrsSPEnabled(Object wallet) async {
if (!(await getNodeIsElectrs(wallet))) {
return false;
}
final bitcoinWallet = wallet as ElectrumWallet;
final tweaksResponse = await bitcoinWallet.electrumClient.getTweaks(height: 0);
print('tweaksResponse: $tweaksResponse');
if (tweaksResponse != null) {
return true;
}
return false;
}
@override

View file

@ -329,7 +329,8 @@ class CryptoBalanceWidget extends StatelessWidget {
final isSilentPaymentsScanningActive = dashboardViewModel.silentPaymentsScanningActive;
final newValue = !isSilentPaymentsScanningActive;
final needsToSwitch = bitcoin!.getNodeIsCakeElectrs(dashboardViewModel.wallet) == false;
final needsToSwitch = !isSilentPaymentsScanningActive &&
await bitcoin!.getNodeIsElectrsSPEnabled(dashboardViewModel.wallet) == false;
if (needsToSwitch) {
return showPopUp<void>(

View file

@ -55,7 +55,8 @@ class RescanPage extends BasePage {
}
Future<void> _toggleSilentPaymentsScanning(BuildContext context) async {
final needsToSwitch = bitcoin!.getNodeIsCakeElectrs(_rescanViewModel.wallet) == false;
final needsToSwitch =
await bitcoin!.getNodeIsElectrsSPEnabled(_rescanViewModel.wallet) == false;
if (needsToSwitch) {
return showPopUp<void>(

View file

@ -213,7 +213,7 @@ abstract class Bitcoin {
{int? outputsCount, int? size});
int getHeightByDate({required DateTime date});
Future<void> rescan(Object wallet, {required int height, bool? doSingleScan});
bool getNodeIsCakeElectrs(Object wallet);
bool getNodeIsElectrsSPEnabled(Object wallet);
void deleteSilentPaymentAddress(Object wallet, String address);
Future<void> updateFeeRates(Object wallet);
int getMaxCustomFeeRate(Object wallet);