From b7ff9ab32b3bb631d60a65e7c4c4ba8d8d336fcf Mon Sep 17 00:00:00 2001 From: Rafael Saes Date: Sat, 23 Nov 2024 11:35:50 -0300 Subject: [PATCH] fix: api & create --- cw_bitcoin/lib/bitcoin_wallet.dart | 14 ++++-- cw_bitcoin/lib/electrum_wallet.dart | 5 +++ cw_bitcoin/lib/electrum_wallet_addresses.dart | 24 +++++++++- .../lib/electrum_worker/electrum_worker.dart | 44 ++++++++++--------- cw_core/lib/get_height_by_date.dart | 2 +- 5 files changed, 64 insertions(+), 25 deletions(-) diff --git a/cw_bitcoin/lib/bitcoin_wallet.dart b/cw_bitcoin/lib/bitcoin_wallet.dart index f6d390389b..653faddf45 100644 --- a/cw_bitcoin/lib/bitcoin_wallet.dart +++ b/cw_bitcoin/lib/bitcoin_wallet.dart @@ -150,9 +150,17 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store { hdWallets[CWBitcoinDerivationType.electrum]!; } } else { - seedBytes = walletInfo.derivationInfo?.derivationType == DerivationType.electrum - ? ElectrumV2SeedGenerator.generateFromString(mnemonic, passphrase) - : Bip39SeedGenerator.generateFromString(mnemonic, passphrase); + switch (walletInfo.derivationInfo?.derivationType) { + case DerivationType.bip39: + seedBytes = await Bip39SeedGenerator.generateFromString(mnemonic, passphrase); + hdWallets[CWBitcoinDerivationType.bip39] = Bip32Slip10Secp256k1.fromSeed(seedBytes); + break; + case DerivationType.electrum: + default: + seedBytes = await ElectrumV2SeedGenerator.generateFromString(mnemonic, passphrase); + hdWallets[CWBitcoinDerivationType.electrum] = Bip32Slip10Secp256k1.fromSeed(seedBytes); + break; + } } return BitcoinWallet( diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index de14efadd5..fb6fc92b10 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1308,6 +1308,11 @@ abstract class ElectrumWalletBase Future onHistoriesResponse(List histories) async { if (histories.isEmpty || _updatingHistories) { _updatingHistories = false; + _syncedTimes++; + if (_syncedTimes == 3) { + syncStatus = SyncedSyncStatus(); + } + return; } diff --git a/cw_bitcoin/lib/electrum_wallet_addresses.dart b/cw_bitcoin/lib/electrum_wallet_addresses.dart index 15056fb671..64b49ba5b0 100644 --- a/cw_bitcoin/lib/electrum_wallet_addresses.dart +++ b/cw_bitcoin/lib/electrum_wallet_addresses.dart @@ -467,7 +467,29 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { (element) => element.scriptType == addressType.toString(), ); - for (final derivationInfo in derivationInfos ?? []) { + if (derivationInfos == null || derivationInfos.isEmpty) { + final bitcoinDerivationInfo = BitcoinDerivationInfo( + derivationType: isElectrum ? BitcoinDerivationType.electrum : BitcoinDerivationType.bip39, + derivationPath: walletInfo.derivationInfo!.derivationPath!, + scriptType: addressType, + ); + + await discoverNewAddresses( + derivationType: derivationType, + isChange: false, + addressType: addressType, + derivationInfo: bitcoinDerivationInfo, + ); + await discoverNewAddresses( + derivationType: derivationType, + isChange: true, + addressType: addressType, + derivationInfo: bitcoinDerivationInfo, + ); + continue; + } + + for (final derivationInfo in derivationInfos) { final bitcoinDerivationInfo = BitcoinDerivationInfo( derivationType: isElectrum ? BitcoinDerivationType.electrum : BitcoinDerivationType.bip39, derivationPath: derivationInfo.derivationPath!, diff --git a/cw_bitcoin/lib/electrum_worker/electrum_worker.dart b/cw_bitcoin/lib/electrum_worker/electrum_worker.dart index 3a0cdf4cb8..14bb0c4dbb 100644 --- a/cw_bitcoin/lib/electrum_worker/electrum_worker.dart +++ b/cw_bitcoin/lib/electrum_worker/electrum_worker.dart @@ -47,6 +47,8 @@ class ElectrumWorker { } void handleMessage(dynamic message) async { + print("Worker received message: $message"); + try { Map messageJson; if (message is String) { @@ -107,7 +109,6 @@ class ElectrumWorker { ElectrumWorkerStopScanningRequest.fromJson(messageJson), ); break; - case ElectrumRequestMethods.estimateFeeMethod: case ElectrumRequestMethods.tweaksSubscribeMethod: if (_isScanning) { _stopScanRequested = false; @@ -279,12 +280,8 @@ class ElectrumWorker { walletType: result.walletType, ); } - - return Future.value(null); })); } - - return histories; })); _sendResponse(ElectrumWorkerGetHistoryResponse( @@ -411,29 +408,36 @@ class ElectrumWorker { if (getTime) { if (mempoolAPIEnabled) { try { - final txVerbose = await http.get( - Uri.parse( - "http://mempool.cakewallet.com:8999/api/v1/tx/$hash/status", - ), - ); + // TODO: mempool api class + final txVerbose = await http + .get( + Uri.parse( + "https://mempool.cakewallet.com/api/v1/tx/$hash/status", + ), + ) + .timeout(const Duration(seconds: 5)); if (txVerbose.statusCode == 200 && txVerbose.body.isNotEmpty && jsonDecode(txVerbose.body) != null) { height = jsonDecode(txVerbose.body)['block_height'] as int; - final blockHash = await http.get( - Uri.parse( - "http://mempool.cakewallet.com:8999/api/v1/block-height/$height", - ), - ); + final blockHash = await http + .get( + Uri.parse( + "https://mempool.cakewallet.com/api/v1/block-height/$height", + ), + ) + .timeout(const Duration(seconds: 5)); if (blockHash.statusCode == 200 && blockHash.body.isNotEmpty) { - final blockResponse = await http.get( - Uri.parse( - "http://mempool.cakewallet.com:8999/api/v1/block/${blockHash.body}", - ), - ); + final blockResponse = await http + .get( + Uri.parse( + "https://mempool.cakewallet.com/api/v1/block/${blockHash.body}", + ), + ) + .timeout(const Duration(seconds: 5)); if (blockResponse.statusCode == 200 && blockResponse.body.isNotEmpty && diff --git a/cw_core/lib/get_height_by_date.dart b/cw_core/lib/get_height_by_date.dart index 15451f31f0..dcf9c66759 100644 --- a/cw_core/lib/get_height_by_date.dart +++ b/cw_core/lib/get_height_by_date.dart @@ -273,7 +273,7 @@ const bitcoinDates = { Future getBitcoinHeightByDateAPI({required DateTime date}) async { final response = await http.get( Uri.parse( - "http://mempool.cakewallet.com:8999/api/v1/mining/blocks/timestamp/${(date.millisecondsSinceEpoch / 1000).round()}", + "https://mempool.cakewallet.com/api/v1/mining/blocks/timestamp/${(date.millisecondsSinceEpoch / 1000).round()}", ), );