[skip ci] wip

This commit is contained in:
Matthew Fosse 2024-09-05 11:02:16 -07:00
parent aa6cac897e
commit c8aa159f69
6 changed files with 22 additions and 44 deletions

View file

@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:bech32/bech32.dart';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:blockchain_utils/bech32/bech32_base.dart';
@ -50,32 +52,18 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
mwebHd.childKey(Bip32KeyIndex(0x80000001)).publicKey.pubKey.compressed;
List<String> mwebAddrs = [];
List<String> oldMwebAddrs = [];
Future<void> topUpMweb(int index) async {
final stub = await CwMweb.stub();
// generate up to index + 1000 addresses:
while (mwebAddrs.length - index < 1000) {
final length = mwebAddrs.length;
final resp = await stub.addresses(AddressRequest(
fromIndex: length,
toIndex: index + 1000,
scanSecret: scanSecret,
spendPubkey: spendPubkey,
));
if (mwebAddrs.length == length) {
mwebAddrs.addAll(resp.address);
}
final address = await CwMweb.address(
Uint8List.fromList(scanSecret),
Uint8List.fromList(spendPubkey),
length,
);
mwebAddrs.add(address!);
}
// for (int i = 0; i < 10; i++) {
// final address = await CwMweb.address(
// hex.encode(scanSecret),
// hex.encode(spendPubkey),
// index + 1000,
// );
// mwebAddrs.add(address!);
// }
// print("old function: ${oldMwebAddrs.first} new function!: ${mwebAddrs.first}");
}
@override
@ -84,7 +72,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
required Bip32Slip10Secp256k1 hd,
BitcoinAddressType? addressType,
}) {
if (addressType == SegwitAddresType.mweb && mwebEnabled) {
if (addressType == SegwitAddresType.mweb) {
topUpMweb(index);
return hd == sideHd ? mwebAddrs[0] : mwebAddrs[index + 1];
}
@ -97,11 +85,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
required Bip32Slip10Secp256k1 hd,
BitcoinAddressType? addressType,
}) async {
// if mweb isn't enabled we'll just return the regular address type which does effectively nothing
// sort of a hack but easier than trying to pull the mweb setting into the electrum_wallet_addresses initialization code
// (we want to avoid initializing the mweb.stub() if it's not enabled or we'd be starting the whole server for no reason and it's slow)
// TODO: find a way to do address generation without starting the whole mweb server
if (addressType == SegwitAddresType.mweb && mwebEnabled) {
if (addressType == SegwitAddresType.mweb) {
await topUpMweb(index);
}
return getAddress(index: index, hd: hd, addressType: addressType);

View file

@ -39,10 +39,10 @@ class CwMwebPlugin: FlutterPlugin, MethodCallHandler {
port = null
result.success(null)
} else if (call.method == "address") {
val scanSecret: String = call.argument<String>("scanSecret") ?: ""
val spendPub: String = call.argument<String>("spendPub") ?: ""
val scanSecret: ByteArray = call.argument<ByteArray>("scanSecret") ?: ByteArray(0)
val spendPub: ByteArray = call.argument<ByteArray>("spendPub") ?: ByteArray(0)
val index: Int = call.argument<Int>("index") ?: 0
val res = Mwebd.addressIndex(scanSecret, spendPub, index.toString())
val res = Mwebd.addressIndex(scanSecret, spendPub, index.toLong())
result.success(res)
} else {
result.notImplemented()

View file

@ -30,11 +30,11 @@ public static func register(with registrar: FlutterPluginRegistrar) {
result(nil)
break
case "address":
let args = call.arguments as? [String: String]
let args = call.arguments as? [String: Any]
let scanSecret = args?["scanSecret"]
let spendPub = args?["spendPub"]
let index = args?["index"]
result(address(scanSecret, spendPub, index))
result(MwebdAddressIndex(scanSecret, spendPub, index))
break
default:
result(FlutterMethodNotImplemented)
@ -77,15 +77,6 @@ public static func register(with registrar: FlutterPluginRegistrar) {
CwMwebPlugin.port = 0
}
private func address(_ scanSecret: String?, _ spendPub: String?, _ index: Int?) -> String? {
guard let scanSecret = scanSecret, let spendPub = spendPub, let index = index else {
print("Invalid arguments for address function")
return nil
}
return MwebdAddressIndex(scanSecret, spendPub, UInt32(index))
}
deinit {
stopServer()
}

View file

@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';
import 'package:grpc/grpc.dart';
import 'package:path_provider/path_provider.dart';
@ -53,7 +54,7 @@ class CwMweb {
await cleanup();
}
static Future<String?> address(String scanSecret, String spendPub, int index) async {
static Future<String?> address(Uint8List scanSecret, Uint8List spendPub, int index) async {
// try {
// return (await CwMwebPlatform.instance.address(scan, spendPub, index))!;
// } catch (e) {

View file

@ -21,7 +21,7 @@ class MethodChannelCwMweb extends CwMwebPlatform {
}
@override
Future<String?> address(String scanSecret, String spendPub, int index) async {
Future<String?> address(Uint8List scanSecret, Uint8List spendPub, int index) async {
final result = await methodChannel.invokeMethod<String>('address', {
'scanSecret': scanSecret,
'spendPub': spendPub,

View file

@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'cw_mweb_method_channel.dart';
@ -31,7 +33,7 @@ abstract class CwMwebPlatform extends PlatformInterface {
throw UnimplementedError('stop() has not been implemented.');
}
Future<String?> address(String scanSecret, String spendPub, int index) {
Future<String?> address(Uint8List scanSecret, Uint8List spendPub, int index) {
throw UnimplementedError('address(int) has not been implemented.');
}
}