fix static variable usage

This commit is contained in:
julian 2023-09-22 10:48:14 -06:00
parent b0f0ea48e1
commit 4b964f611c

View file

@ -23,9 +23,9 @@ const String kReservedFusionAddress = "reserved_fusion_address";
/// A mixin for the BitcoinCashWallet class that adds CashFusion functionality. /// A mixin for the BitcoinCashWallet class that adds CashFusion functionality.
mixin FusionWalletInterface { mixin FusionWalletInterface {
// Passed in wallet data. // Passed in wallet data.
static late final String _walletId; late final String _walletId;
late final Coin _coin; late final Coin _coin;
static late final MainDB _db; late final MainDB _db;
late final CachedElectrumX _cachedElectrumX; late final CachedElectrumX _cachedElectrumX;
late final TorService _torService; late final TorService _torService;
@ -85,23 +85,29 @@ mixin FusionWalletInterface {
} }
/// Returns a list of all transactions in the wallet for the given address. /// Returns a list of all transactions in the wallet for the given address.
Future<Set<fusion_tx.Transaction>> getTransactionsByAddress( Future<List<fusion_tx.Transaction>> getTransactionsByAddress(
String address) async { String address) async {
var _txs = await _db.getTransactions(_walletId).findAll(); final _txs = await _db.getTransactions(_walletId).findAll();
// Use Future.wait to await all the futures in the set and then convert it to a set. // Use Future.wait to await all the futures in the set and then convert it to a set.
var resultSet = await Future.wait( final resultSet = await Future.wait(
_txs.map((tx) => tx.toFusionTransaction(_cachedElectrumX))); _txs.map((tx) => tx.toFusionTransaction(_cachedElectrumX)));
return resultSet.toSet(); return resultSet;
} }
/// Returns a list of all UTXOs in the wallet for the given address. /// Returns a list of all UTXOs in the wallet for the given address.
Future<List<fusion_input.Input>> getInputsByAddress(String address) async { Future<List<fusion_input.Input>> getInputsByAddress(String address) async {
var _utxos = await _db.getUTXOsByAddress(_walletId, address).findAll(); final _utxos = await _db.getUTXOsByAddress(_walletId, address).findAll();
List<Future<fusion_input.Input>> futureInputs = List<Future<fusion_input.Input>> futureInputs = _utxos
_utxos.map((utxo) => utxo.toFusionInput()).toList(); .map(
(utxo) => utxo.toFusionInput(
walletId: _walletId,
dbInstance: _db,
),
)
.toList();
return await Future.wait(futureInputs); return await Future.wait(futureInputs);
} }
@ -185,17 +191,6 @@ mixin FusionWalletInterface {
return unusedAddresses; return unusedAddresses;
} }
/// Get an address.
static Future<Address> getAddress(String addr) async {
Address? address = await _db.getAddress(_walletId, addr);
if (address == null) {
throw Exception("Address not found");
}
return address;
}
/// Returns the current Tor proxy address. /// Returns the current Tor proxy address.
Future<({InternetAddress host, int port})> getSocksProxyAddress() async { Future<({InternetAddress host, int port})> getSocksProxyAddress() async {
/* /*
@ -399,12 +394,17 @@ mixin FusionWalletInterface {
/// An extension of Stack Wallet's Address class that adds CashFusion functionality. /// An extension of Stack Wallet's Address class that adds CashFusion functionality.
extension FusionAddress on Address { extension FusionAddress on Address {
fusion_address.Address toFusionAddress() { fusion_address.Address toFusionAddress() {
if (derivationPath == null) {
throw Exception("Fusion Addresses require a derivation path");
}
return fusion_address.Address( return fusion_address.Address(
addr: value, addr: value,
publicKey: publicKey, publicKey: publicKey,
derivationPath: derivationPath: fusion_address.DerivationPath(
fusion_address.DerivationPath(derivationPath?.value ?? "")); derivationPath!.value,
// TODO fix default derivation path. ),
);
} }
} }
@ -413,14 +413,40 @@ extension FusionAddress on Address {
/// This class is used to convert Stack Wallet's UTXO class to FusionDart's /// This class is used to convert Stack Wallet's UTXO class to FusionDart's
/// Input and Output classes. /// Input and Output classes.
extension FusionUTXO on UTXO { extension FusionUTXO on UTXO {
/// Converts a Stack Wallet UTXO to a FusionDart Input. /// Fetch the public key of an address stored in the database.
Future<fusion_input.Input> toFusionInput() async { Future<Address> _getAddressPubkey({
if (address == null) { required String address,
throw Exception("toFutionInput Address is null"); required String walletId,
required MainDB dbInstance,
}) async {
final Address? addr = await dbInstance.getAddress(walletId, address);
if (addr == null) {
throw Exception("Address not found");
} }
// Search isar for address to get pubKey. return addr;
Address addr = await FusionWalletInterface.getAddress(address!); }
/// Converts a Stack Wallet UTXO to a FusionDart Input.
Future<fusion_input.Input> toFusionInput({
required String walletId,
required MainDB dbInstance,
}) async {
if (address == null) {
throw Exception("toFusionInput Address is null");
}
try {
final Address addr = await _getAddressPubkey(
address: address!,
walletId: walletId,
dbInstance: dbInstance,
);
if (addr.publicKey.isEmpty) {
throw Exception("Public key for fetched address is empty");
}
return fusion_input.Input( return fusion_input.Input(
prevTxid: utf8.encode(txid), prevTxid: utf8.encode(txid),
@ -428,22 +454,42 @@ extension FusionUTXO on UTXO {
pubKey: addr.publicKey, pubKey: addr.publicKey,
amount: value, amount: value,
); );
} catch (e) {
rethrow;
}
} }
/// Converts a Stack Wallet UTXO to a FusionDart Output... eventually. /// Converts a Stack Wallet UTXO to a FusionDart Output... eventually.
Future<fusion_output.Output> toFusionOutput() async { Future<fusion_output.Output> toFusionOutput({
required String walletId,
required MainDB dbInstance,
}) async {
if (address == null) { if (address == null) {
throw Exception("toFutionOutput Address is null"); throw Exception("toFutionOutput Address is null");
} }
// Search isar for address to get pubKey. // Search isar for address to get pubKey.
Address addr = await FusionWalletInterface.getAddress(address!); final Address addr = await _getAddressPubkey(
address: address!,
walletId: walletId,
dbInstance: dbInstance,
);
if (addr.publicKey.isEmpty) {
throw Exception("Public key for fetched address is empty");
}
if (addr.derivationPath == null) {
throw Exception("Derivation path for fetched address is empty");
}
return fusion_output.Output( return fusion_output.Output(
addr: fusion_address.Address( addr: fusion_address.Address(
addr: address!, addr: address!,
publicKey: addr.publicKey, // TODO fix public key. publicKey: addr.publicKey,
derivationPath: null, // TODO fix derivation path. derivationPath: fusion_address.DerivationPath(
addr.derivationPath!.value,
),
), ),
value: value, value: value,
); );