cake_wallet/cw_bitcoin/lib/litecoin_wallet_addresses.dart

87 lines
2.7 KiB
Dart

import 'package:convert/convert.dart';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:cw_bitcoin/utils.dart';
import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_mweb/cw_mweb.dart';
import 'package:cw_mweb/mwebd.pb.dart';
import 'package:mobx/mobx.dart';
part 'litecoin_wallet_addresses.g.dart';
class LitecoinWalletAddresses = LitecoinWalletAddressesBase with _$LitecoinWalletAddresses;
abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with Store {
LitecoinWalletAddressesBase(
WalletInfo walletInfo, {
required super.mainHd,
required super.sideHd,
required this.mwebHd,
required super.network,
super.initialAddresses,
super.initialRegularAddressIndex,
super.initialChangeAddressIndex,
}) : super(walletInfo) {
topUpMweb(0);
}
final Bip32Slip10Secp256k1 mwebHd;
List<int> get scanSecret => mwebHd.childKey(Bip32KeyIndex(0x80000000)).privateKey.privKey.raw;
// TODO: I'm not 100% sure if it's supposed to be the compressed or uncompressed public key!
List<int> get spendPubkey => mwebHd.childKey(Bip32KeyIndex(0x80000001)).publicKey.pubKey.compressed;
List<String> mwebAddrs = [];
Future<void> topUpMweb(int index) async {
while (mwebAddrs.length - index < 1000) {
final length = mwebAddrs.length;
final stub = await CwMweb.stub();
final resp = await stub.addresses(AddressRequest(
fromIndex: length,
toIndex: index + 1000,
scanSecret: scanSecret,
spendPubkey: spendPubkey,
));
if (mwebAddrs.length == length) {
mwebAddrs.addAll(resp.address);
}
}
}
@override
String getAddress({
required int index,
required Bip32Slip10Secp256k1 hd,
BitcoinAddressType? addressType,
}) {
if (addressType == SegwitAddresType.mweb) {
topUpMweb(index);
return hd == sideHd ? mwebAddrs[0] : mwebAddrs[index + 1];
}
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) {
await topUpMweb(index);
}
return getAddress(index: index, hd: hd, addressType: addressType);
}
@action
@override
Future<String> getChangeAddress() async {
// super.getChangeAddress();
// updateChangeAddresses();
// print("getChangeAddress @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
// this means all change addresses used will be mweb addresses!:
await topUpMweb(0);
return mwebAddrs[0];
}
}