From 2c38641d5c6220c85bbb70899b81982e71b93a19 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Tue, 10 Sep 2024 14:58:03 -0700 Subject: [PATCH] more address generation reliability fixes --- cw_bitcoin/lib/electrum_wallet.dart | 2 -- cw_bitcoin/lib/electrum_wallet_addresses.dart | 4 +-- cw_bitcoin/lib/litecoin_wallet.dart | 6 ++++ cw_bitcoin/lib/litecoin_wallet_addresses.dart | 34 +++++++++++++++++-- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 7733562fb..b812907ad 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -2395,8 +2395,6 @@ class PublicKeyWithDerivationPath { } BitcoinBaseAddress addressTypeFromStr(String address, BasedUtxoNetwork network) { - // print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); - // print(network); if (network is BitcoinCashNetwork) { if (!address.startsWith("bitcoincash:") && (address.startsWith("q") || address.startsWith("p"))) { diff --git a/cw_bitcoin/lib/electrum_wallet_addresses.dart b/cw_bitcoin/lib/electrum_wallet_addresses.dart index 0653200ee..314b8768a 100644 --- a/cw_bitcoin/lib/electrum_wallet_addresses.dart +++ b/cw_bitcoin/lib/electrum_wallet_addresses.dart @@ -223,8 +223,8 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { if (walletInfo.type == WalletType.bitcoinCash) { await _generateInitialAddresses(type: P2pkhAddressType.p2pkh); } else if (walletInfo.type == WalletType.litecoin) { - // await _generateInitialAddresses(); - // await _generateInitialAddresses(type: SegwitAddresType.mweb); + await _generateInitialAddresses(type: SegwitAddresType.p2wpkh); + await _generateInitialAddresses(type: SegwitAddresType.mweb); } else if (walletInfo.type == WalletType.bitcoin) { await _generateInitialAddresses(); await _generateInitialAddresses(type: P2pkhAddressType.p2pkh); diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index e2010c520..c5ddad44a 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -250,6 +250,12 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { return; } + final mwebAddrs = (walletAddresses as LitecoinWalletAddresses).mwebAddrs; + while (mwebAddrs.length < 1000) { + print("waiting for mweb addresses to finish generating..."); + await Future.delayed(const Duration(milliseconds: 1000)); + } + await getStub(); await updateUnspent(); await updateBalance(); diff --git a/cw_bitcoin/lib/litecoin_wallet_addresses.dart b/cw_bitcoin/lib/litecoin_wallet_addresses.dart index 0e7434029..b657ce60d 100644 --- a/cw_bitcoin/lib/litecoin_wallet_addresses.dart +++ b/cw_bitcoin/lib/litecoin_wallet_addresses.dart @@ -4,6 +4,7 @@ import 'dart:typed_data'; import 'package:bech32/bech32.dart'; import 'package:bitcoin_base/bitcoin_base.dart'; import 'package:blockchain_utils/blockchain_utils.dart'; +import 'package:cw_bitcoin/bitcoin_address_record.dart'; import 'package:cw_bitcoin/electrum_wallet.dart'; import 'package:cw_bitcoin/utils.dart'; import 'package:cw_bitcoin/electrum_wallet_addresses.dart'; @@ -33,7 +34,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with super.initialChangeAddressIndex, }) : super(walletInfo) { // start generating mweb addresses in the background: - initMwebAddresses(); + // initMwebAddresses(); } final Bip32Slip10Secp256k1 mwebHd; @@ -46,6 +47,12 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with List get spendPubkey => mwebHd.childKey(Bip32KeyIndex(0x80000001)).publicKey.pubKey.compressed; + @override + Future init() async { + await super.init(); + initMwebAddresses(); + } + Future ensureMwebAddressUpToIndexExists(int index) async { Uint8List scan = Uint8List.fromList(scanSecret); Uint8List spend = Uint8List.fromList(spendPubkey); @@ -66,10 +73,30 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with } Future initMwebAddresses() async { - for (int i = 0; i < 4; i++) { + print("Initializing MWEB address timer!"); + Timer.periodic(const Duration(seconds: 2), (timer) async { + print("Generating MWEB addresses..."); await generateNumAddresses(250); await Future.delayed(const Duration(milliseconds: 1500)); - } + + if (mwebAddrs.length > 1000) { + // convert mwebAddrs to BitcoinAddressRecords: + List mwebAddresses = mwebAddrs + .asMap() + .entries + .map((e) => BitcoinAddressRecord( + e.value, + index: e.key, + type: SegwitAddresType.mweb, + network: network, + )) + .toList(); + // add them to the list of all addresses: + addAddresses(mwebAddresses); + print("MWEB addresses initialized ${mwebAddrs.length}"); + timer.cancel(); + } + }); } @override @@ -126,6 +153,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with } if (mwebEnabled) { + await ensureMwebAddressUpToIndexExists(1); return mwebAddrs[0]; }