[skip ci] wip

This commit is contained in:
Matthew Fosse 2024-09-09 20:59:52 -07:00
parent 3963233c0e
commit 1061b7de01
2 changed files with 68 additions and 12 deletions

View file

@ -335,6 +335,13 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
}) => }) =>
''; '';
Future<String> getAddressAsync({
required int index,
required Bip32Slip10Secp256k1 hd,
BitcoinAddressType? addressType,
}) async =>
getAddress(index: index, hd: hd, addressType: addressType);
void addBitcoinAddressTypes() { void addBitcoinAddressTypes() {
final lastP2wpkh = _addresses final lastP2wpkh = _addresses
.where((addressRecord) => .where((addressRecord) =>
@ -569,7 +576,7 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
for (var i = startIndex; i < count + startIndex; i++) { for (var i = startIndex; i < count + startIndex; i++) {
final address = BitcoinAddressRecord( final address = BitcoinAddressRecord(
getAddress(index: i, hd: _getHd(isHidden), addressType: type ?? addressPageType), await getAddressAsync(index: i, hd: _getHd(isHidden), addressType: type ?? addressPageType),
index: i, index: i,
isHidden: isHidden, isHidden: isHidden,
type: type ?? addressPageType, type: type ?? addressPageType,

View file

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:bech32/bech32.dart'; import 'package:bech32/bech32.dart';
@ -30,29 +31,63 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
super.initialRegularAddressIndex, super.initialRegularAddressIndex,
super.initialChangeAddressIndex, super.initialChangeAddressIndex,
}) : super(walletInfo) { }) : super(walletInfo) {
topUpMweb(0); initMwebAddresses();
// topUpMweb(0);
print("initialized LitecoinWalletAddressesBase");
} }
final Bip32Slip10Secp256k1 mwebHd; final Bip32Slip10Secp256k1 mwebHd;
bool mwebEnabled; bool mwebEnabled;
int mwebTopUpIndex = 1000;
List<String> mwebAddrs = [];
static Timer? mwebTopUpTimer;
List<int> get scanSecret => mwebHd.childKey(Bip32KeyIndex(0x80000000)).privateKey.privKey.raw; List<int> get scanSecret => mwebHd.childKey(Bip32KeyIndex(0x80000000)).privateKey.privKey.raw;
List<int> get spendPubkey => List<int> get spendPubkey =>
mwebHd.childKey(Bip32KeyIndex(0x80000001)).publicKey.pubKey.compressed; mwebHd.childKey(Bip32KeyIndex(0x80000001)).publicKey.pubKey.compressed;
List<String> mwebAddrs = []; // Future<void> topUpMweb(int index) async {
// // generate up to index + 1000 addresses:
// while (mwebAddrs.length - index < 1000) {
// final length = mwebAddrs.length;
// final address = await CwMweb.address(
// Uint8List.fromList(scanSecret),
// Uint8List.fromList(spendPubkey),
// length,
// );
// mwebAddrs.add(address!);
// }
// }
Future<void> topUpMweb(int index) async { Future<void> initMwebAddresses() async {
// generate up to index + 1000 addresses: print("mweb addresses: ${mwebAddrs.length}");
while (mwebAddrs.length - index < 1000) { const int INITIAL_MWEB_ADDRESSES = 25;
final length = mwebAddrs.length; // make sure we have at least 20 addresses:
while (mwebAddrs.length < INITIAL_MWEB_ADDRESSES) {
final address = await CwMweb.address( final address = await CwMweb.address(
Uint8List.fromList(scanSecret), Uint8List.fromList(scanSecret),
Uint8List.fromList(spendPubkey), Uint8List.fromList(spendPubkey),
length, mwebAddrs.length,
); );
mwebAddrs.add(address!); mwebAddrs.add(address!);
} }
// set up a periodic task to fill up the mweb addresses to 1000:
mwebTopUpTimer?.cancel();
mwebTopUpTimer = Timer.periodic(Duration(seconds: 5), (timer) async {
if (mwebAddrs.length >= mwebTopUpIndex) {
return;
}
for (int i = 0; i < 10; i++) {
final address = await CwMweb.address(
Uint8List.fromList(scanSecret),
Uint8List.fromList(spendPubkey),
mwebAddrs.length,
);
mwebAddrs.add(address!);
}
print("mweb addresses: ${mwebAddrs.length}");
});
} }
@override @override
@ -62,13 +97,28 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
BitcoinAddressType? addressType, BitcoinAddressType? addressType,
}) { }) {
if (addressType == SegwitAddresType.mweb) { if (addressType == SegwitAddresType.mweb) {
topUpMweb(index).then((value) { return hd == sideHd ? mwebAddrs[0] : mwebAddrs[index + 1];
return hd == sideHd ? mwebAddrs[0] : mwebAddrs[index + 1];
});
} }
return generateP2WPKHAddress(hd: hd, index: index, network: network); return generateP2WPKHAddress(hd: hd, index: index, network: network);
} }
@override
Future<String> getAddressAsync({
required int index,
required Bip32Slip10Secp256k1 hd,
BitcoinAddressType? addressType,
}) async {
if (addressType == SegwitAddresType.mweb) {
if (index + 1000 > mwebTopUpIndex) {
mwebTopUpIndex = index + 1000;
}
while (mwebAddrs.length <= index) {
await Future.delayed(const Duration(seconds: 1));
}
}
return getAddress(index: index, hd: hd, addressType: addressType);
}
@action @action
@override @override
Future<String> getChangeAddress({List<BitcoinOutput>? outputs, UtxoDetails? utxoDetails}) async { Future<String> getChangeAddress({List<BitcoinOutput>? outputs, UtxoDetails? utxoDetails}) async {
@ -99,7 +149,6 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
} }
if (mwebEnabled) { if (mwebEnabled) {
await topUpMweb(0);
return mwebAddrs[0]; return mwebAddrs[0];
} }