2023-09-14 22:28:11 +00:00
|
|
|
import 'dart:async';
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'dart:io';
|
2023-10-09 21:42:42 +00:00
|
|
|
import 'dart:typed_data';
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-10-09 21:42:42 +00:00
|
|
|
import 'package:bitcoindart/bitcoindart.dart' as btcdart;
|
2023-09-28 16:41:39 +00:00
|
|
|
import 'package:fusiondart/fusiondart.dart' as fusion;
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:stackwallet/db/isar/main_db.dart';
|
2023-09-19 22:58:55 +00:00
|
|
|
import 'package:stackwallet/electrumx_rpc/cached_electrumx.dart';
|
2023-09-28 20:16:48 +00:00
|
|
|
import 'package:stackwallet/models/fusion_progress_ui_state.dart';
|
2023-08-07 18:54:44 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
2023-09-22 21:44:40 +00:00
|
|
|
import 'package:stackwallet/services/fusion_tor_service.dart';
|
2023-10-09 21:42:42 +00:00
|
|
|
import 'package:stackwallet/utilities/bip32_utils.dart';
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-09-22 18:50:59 +00:00
|
|
|
import 'package:stackwallet/utilities/extensions/impl/string.dart';
|
2023-09-15 21:02:25 +00:00
|
|
|
import 'package:stackwallet/utilities/stack_file_system.dart';
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-08-07 18:54:44 +00:00
|
|
|
const String kReservedFusionAddress = "reserved_fusion_address";
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// A mixin for the BitcoinCashWallet class that adds CashFusion functionality.
|
2023-08-24 16:22:13 +00:00
|
|
|
mixin FusionWalletInterface {
|
2023-09-14 22:28:11 +00:00
|
|
|
// Passed in wallet data.
|
2023-09-22 16:48:14 +00:00
|
|
|
late final String _walletId;
|
2023-07-26 22:06:02 +00:00
|
|
|
late final Coin _coin;
|
2023-09-22 16:48:14 +00:00
|
|
|
late final MainDB _db;
|
2023-09-22 21:44:40 +00:00
|
|
|
late final FusionTorService _torService;
|
2023-10-09 21:42:42 +00:00
|
|
|
late final Future<String?> _mnemonic;
|
|
|
|
late final Future<String?> _mnemonicPassphrase;
|
|
|
|
late final btcdart.NetworkType _network;
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-09-28 20:16:48 +00:00
|
|
|
// setting values on this should notify any listeners (the GUI)
|
|
|
|
FusionProgressUIState? _uiState;
|
|
|
|
FusionProgressUIState get uiState {
|
|
|
|
if (_uiState == null) {
|
|
|
|
throw Exception("FusionProgressUIState has not been set for $_walletId");
|
|
|
|
}
|
|
|
|
return _uiState!;
|
|
|
|
}
|
|
|
|
|
|
|
|
set uiState(FusionProgressUIState state) {
|
|
|
|
if (_uiState != null) {
|
|
|
|
throw Exception("FusionProgressUIState was already set for $_walletId");
|
|
|
|
}
|
|
|
|
_uiState = state;
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Passed in wallet functions.
|
2023-09-22 22:39:59 +00:00
|
|
|
late final Future<Address> Function() _getNextUnusedChangeAddress;
|
2023-09-22 21:59:07 +00:00
|
|
|
late final CachedElectrumX Function() _getWalletCachedElectrumX;
|
2023-09-22 23:32:38 +00:00
|
|
|
late final Future<int> Function({
|
|
|
|
required String address,
|
|
|
|
}) _getTxCountForAddress;
|
2023-09-28 16:05:06 +00:00
|
|
|
late final Future<int> Function() _getChainHeight;
|
2023-09-22 21:59:07 +00:00
|
|
|
|
2023-09-15 21:02:25 +00:00
|
|
|
/// Initializes the FusionWalletInterface mixin.
|
|
|
|
///
|
|
|
|
/// This function must be called before any other functions in this mixin.
|
|
|
|
///
|
|
|
|
/// Returns a `Future<void>` that resolves when Tor has been started.
|
|
|
|
Future<void> initFusionInterface({
|
2023-07-26 22:06:02 +00:00
|
|
|
required String walletId,
|
|
|
|
required Coin coin,
|
|
|
|
required MainDB db,
|
2023-09-22 22:39:59 +00:00
|
|
|
required Future<Address> Function() getNextUnusedChangeAddress,
|
2023-09-22 21:59:07 +00:00
|
|
|
required CachedElectrumX Function() getWalletCachedElectrumX,
|
2023-09-22 23:32:38 +00:00
|
|
|
required Future<int> Function({
|
|
|
|
required String address,
|
|
|
|
}) getTxCountForAddress,
|
2023-09-28 16:05:06 +00:00
|
|
|
required Future<int> Function() getChainHeight,
|
2023-10-09 21:42:42 +00:00
|
|
|
required Future<String?> mnemonic,
|
|
|
|
required Future<String?> mnemonicPassphrase,
|
|
|
|
required btcdart.NetworkType network,
|
2023-09-15 21:02:25 +00:00
|
|
|
}) async {
|
|
|
|
// Set passed in wallet data.
|
2023-07-26 22:06:02 +00:00
|
|
|
_walletId = walletId;
|
|
|
|
_coin = coin;
|
|
|
|
_db = db;
|
2023-09-22 22:39:59 +00:00
|
|
|
_getNextUnusedChangeAddress = getNextUnusedChangeAddress;
|
2023-09-22 21:44:40 +00:00
|
|
|
_torService = FusionTorService.sharedInstance;
|
2023-09-22 21:59:07 +00:00
|
|
|
_getWalletCachedElectrumX = getWalletCachedElectrumX;
|
2023-09-22 23:32:38 +00:00
|
|
|
_getTxCountForAddress = getTxCountForAddress;
|
2023-09-28 16:05:06 +00:00
|
|
|
_getChainHeight = getChainHeight;
|
2023-10-09 21:42:42 +00:00
|
|
|
_mnemonic = mnemonic;
|
|
|
|
_mnemonicPassphrase = mnemonicPassphrase;
|
|
|
|
_network = network;
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 20:16:48 +00:00
|
|
|
// callback to update the ui state object
|
|
|
|
void updateStatus(fusion.FusionStatus fusionStatus) {
|
|
|
|
// TODO: this
|
|
|
|
// set _uiState states
|
|
|
|
}
|
|
|
|
|
2023-10-09 17:44:17 +00:00
|
|
|
/// Returns a list of all owned p2pkh addresses in the wallet.
|
2023-09-28 16:41:39 +00:00
|
|
|
Future<List<fusion.Address>> getFusionAddresses() async {
|
2023-10-09 17:44:17 +00:00
|
|
|
List<Address> _addresses = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.typeEqualTo(AddressType.p2pkh)
|
|
|
|
.and()
|
|
|
|
.group((q) => q
|
|
|
|
.subTypeEqualTo(AddressSubType.receiving)
|
|
|
|
.or()
|
|
|
|
.subTypeEqualTo(AddressSubType.change))
|
|
|
|
.findAll();
|
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
return _addresses.map((address) => address.toFusionAddress()).toList();
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all transactions in the wallet for the given address.
|
2023-10-09 16:55:06 +00:00
|
|
|
Future<List<Map<String, dynamic>>> getTransactionsByAddress(
|
|
|
|
String address,
|
|
|
|
) async {
|
|
|
|
final txidList =
|
|
|
|
await _db.getTransactions(_walletId).txidProperty().findAll();
|
|
|
|
|
|
|
|
final futures = txidList.map(
|
|
|
|
(e) => _getWalletCachedElectrumX().getTransaction(
|
|
|
|
txHash: e,
|
|
|
|
coin: _coin,
|
2023-09-22 18:50:59 +00:00
|
|
|
),
|
|
|
|
);
|
2023-09-19 22:58:55 +00:00
|
|
|
|
2023-10-09 16:55:06 +00:00
|
|
|
return await Future.wait(futures);
|
2023-08-30 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 21:42:42 +00:00
|
|
|
Future<Uint8List> getPrivateKeyForPubKey(List<int> pubKey) async {
|
|
|
|
// can't directly query for equal lists in isar so we need to fetch
|
|
|
|
// all addresses then search in dart
|
|
|
|
try {
|
|
|
|
final derivationPath = (await getFusionAddresses())
|
|
|
|
.firstWhere((e) => e.publicKey.toString() == pubKey.toString())
|
|
|
|
.derivationPath!
|
|
|
|
.value;
|
|
|
|
|
|
|
|
final node = await Bip32Utils.getBip32Node(
|
|
|
|
(await _mnemonic)!,
|
|
|
|
(await _mnemonicPassphrase)!,
|
|
|
|
_network,
|
|
|
|
derivationPath,
|
|
|
|
);
|
|
|
|
|
|
|
|
return node.privateKey!;
|
|
|
|
} catch (_) {
|
|
|
|
throw Exception("Derivation path for pubkey=$pubKey could not be found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all UTXOs in the wallet for the given address.
|
2023-10-09 21:05:04 +00:00
|
|
|
Future<List<fusion.UtxoDTO>> getInputsByAddress(String address) async {
|
2023-09-22 16:48:14 +00:00
|
|
|
final _utxos = await _db.getUTXOsByAddress(_walletId, address).findAll();
|
2023-08-30 16:40:39 +00:00
|
|
|
|
2023-10-09 21:05:04 +00:00
|
|
|
List<Future<fusion.UtxoDTO>> futureInputs = _utxos
|
2023-09-22 16:48:14 +00:00
|
|
|
.map(
|
2023-10-09 21:05:04 +00:00
|
|
|
(utxo) => utxo.toFusionUtxoDTO(
|
2023-09-22 16:48:14 +00:00
|
|
|
walletId: _walletId,
|
|
|
|
dbInstance: _db,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList();
|
2023-09-22 05:38:32 +00:00
|
|
|
|
|
|
|
return await Future.wait(futureInputs);
|
2023-08-30 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Creates a new reserved change address.
|
2023-09-28 16:41:39 +00:00
|
|
|
Future<fusion.Address> createNewReservedChangeAddress() async {
|
2023-09-22 22:39:59 +00:00
|
|
|
// _getNextUnusedChangeAddress() grabs the latest unused change address
|
|
|
|
// from the wallet.
|
|
|
|
// CopyWith to mark it as a fusion reserved change address
|
|
|
|
final address = (await _getNextUnusedChangeAddress())
|
|
|
|
.copyWith(otherData: kReservedFusionAddress);
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-09-22 22:39:59 +00:00
|
|
|
final _address = await _db.getAddress(_walletId, address.value);
|
2023-08-30 16:40:39 +00:00
|
|
|
if (_address != null) {
|
|
|
|
await _db.updateAddress(_address, address);
|
|
|
|
} else {
|
|
|
|
await _db.putAddress(address);
|
|
|
|
}
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-08-07 19:15:08 +00:00
|
|
|
return address.toFusionAddress();
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of unused reserved change addresses.
|
|
|
|
///
|
|
|
|
/// If there are not enough unused reserved change addresses, new ones are created.
|
2023-09-28 16:41:39 +00:00
|
|
|
Future<List<fusion.Address>> getUnusedReservedChangeAddresses(
|
2023-08-07 18:54:44 +00:00
|
|
|
int numberOfAddresses,
|
|
|
|
) async {
|
2023-09-14 22:28:11 +00:00
|
|
|
// Fetch all reserved change addresses.
|
2023-09-22 23:32:38 +00:00
|
|
|
final List<Address> reservedChangeAddresses = await _db
|
2023-08-07 18:54:44 +00:00
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.otherDataEqualTo(kReservedFusionAddress)
|
2023-09-22 22:39:59 +00:00
|
|
|
.and()
|
|
|
|
.subTypeEqualTo(AddressSubType.change)
|
2023-08-07 18:54:44 +00:00
|
|
|
.findAll();
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Initialize a list of unused reserved change addresses.
|
2023-09-28 16:41:39 +00:00
|
|
|
final List<fusion.Address> unusedAddresses = [];
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-09-22 23:32:38 +00:00
|
|
|
// check addresses for tx history
|
|
|
|
for (final address in reservedChangeAddresses) {
|
|
|
|
// first check in db to avoid unnecessary network calls
|
|
|
|
final txCountInDB = await _db
|
|
|
|
.getTransactions(_walletId)
|
|
|
|
.filter()
|
|
|
|
.address((q) => q.valueEqualTo(address.value))
|
|
|
|
.count();
|
|
|
|
if (txCountInDB == 0) {
|
|
|
|
// double check via electrumx
|
|
|
|
// _getTxCountForAddress can throw!
|
|
|
|
final count = await _getTxCountForAddress(address: address.value);
|
|
|
|
if (count == 0) {
|
|
|
|
unusedAddresses.add(address.toFusionAddress());
|
|
|
|
}
|
2023-08-07 18:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// If there are not enough unused reserved change addresses, create new ones.
|
2023-09-22 23:32:38 +00:00
|
|
|
while (unusedAddresses.length < numberOfAddresses) {
|
|
|
|
unusedAddresses.add(await createNewReservedChangeAddress());
|
2023-08-07 18:54:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Return the list of unused reserved change addresses.
|
2023-09-28 16:34:43 +00:00
|
|
|
return unusedAddresses.sublist(0, numberOfAddresses);
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 21:44:40 +00:00
|
|
|
int _torStartCount = 0;
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns the current Tor proxy address.
|
|
|
|
Future<({InternetAddress host, int port})> getSocksProxyAddress() async {
|
2023-09-22 21:44:40 +00:00
|
|
|
if (_torStartCount > 5) {
|
|
|
|
// something is quite broken so stop trying to recursively fetch
|
|
|
|
// start up tor and fetch proxy info
|
|
|
|
throw Exception(
|
|
|
|
"Fusion interface attempted to start tor $_torStartCount times and failed!",
|
|
|
|
);
|
2023-09-14 22:28:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 21:44:40 +00:00
|
|
|
try {
|
|
|
|
final info = _torService.getProxyInfo();
|
2023-09-14 22:28:11 +00:00
|
|
|
|
2023-09-22 21:44:40 +00:00
|
|
|
// reset counter before return info;
|
|
|
|
_torStartCount = 0;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
} catch (_) {
|
|
|
|
// tor is probably not running so lets fix that
|
|
|
|
final torDir = await StackFileSystem.applicationTorDirectory();
|
|
|
|
_torService.init(torDataDirPath: torDir.path);
|
|
|
|
|
|
|
|
// increment start attempt count
|
|
|
|
_torStartCount++;
|
|
|
|
|
|
|
|
await _torService.start();
|
|
|
|
|
|
|
|
// try again to fetch proxy info
|
|
|
|
return await getSocksProxyAddress();
|
|
|
|
}
|
2023-09-14 22:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initial attempt for CashFusion integration goes here.
|
|
|
|
|
|
|
|
/// Fuse the wallet's UTXOs.
|
|
|
|
///
|
|
|
|
/// This function is called when the user taps the "Fuse" button in the UI.
|
|
|
|
///
|
|
|
|
/// Returns:
|
|
|
|
/// A `Future<void>` that resolves when the fusion operation is finished.
|
|
|
|
Future<void> fuse() async {
|
2023-07-26 22:06:02 +00:00
|
|
|
// Initial attempt for CashFusion integration goes here.
|
2023-09-28 16:41:39 +00:00
|
|
|
final mainFusionObject = fusion.Fusion(fusion.FusionParams());
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// Pass wallet functions to the Fusion object
|
2023-09-27 20:04:24 +00:00
|
|
|
await mainFusionObject.initFusion(
|
2023-09-22 23:00:32 +00:00
|
|
|
getAddresses: getFusionAddresses,
|
|
|
|
getTransactionsByAddress: getTransactionsByAddress,
|
|
|
|
getInputsByAddress: getInputsByAddress,
|
|
|
|
getUnusedReservedChangeAddresses: getUnusedReservedChangeAddresses,
|
|
|
|
getSocksProxyAddress: getSocksProxyAddress,
|
2023-09-28 16:20:09 +00:00
|
|
|
getChainHeight: _getChainHeight,
|
2023-09-28 20:16:48 +00:00
|
|
|
updateStatusCallback: updateStatus,
|
2023-10-09 20:22:45 +00:00
|
|
|
getTransactionJson: (String txid) async =>
|
|
|
|
await _getWalletCachedElectrumX().getTransaction(
|
|
|
|
coin: _coin,
|
|
|
|
txHash: txid,
|
|
|
|
),
|
2023-10-09 21:42:42 +00:00
|
|
|
getPrivateKeyForPubKey: getPrivateKeyForPubKey,
|
2023-09-22 23:00:32 +00:00
|
|
|
);
|
2023-08-30 16:40:39 +00:00
|
|
|
|
|
|
|
// Add stack UTXOs.
|
2023-09-22 23:00:32 +00:00
|
|
|
final List<UTXO> walletUtxos = await _db.getUTXOs(_walletId).findAll();
|
2023-09-28 17:44:17 +00:00
|
|
|
final List<fusion.UtxoDTO> coinList = [];
|
2023-09-19 22:58:55 +00:00
|
|
|
|
|
|
|
// Loop through UTXOs, checking and adding valid ones.
|
2023-09-22 23:00:32 +00:00
|
|
|
for (final utxo in walletUtxos) {
|
2023-09-19 22:58:55 +00:00
|
|
|
// Check if address is available.
|
2023-09-22 23:00:32 +00:00
|
|
|
if (utxo.address == null) {
|
2023-09-19 22:58:55 +00:00
|
|
|
// TODO we could continue here (and below during scriptPubKey validation) instead of throwing.
|
2023-09-22 23:00:32 +00:00
|
|
|
throw Exception("UTXO ${utxo.txid}:${utxo.vout} address is null");
|
2023-09-19 22:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find public key.
|
2023-09-22 21:59:07 +00:00
|
|
|
Map<String, dynamic> tx =
|
|
|
|
await _getWalletCachedElectrumX().getTransaction(
|
2023-09-22 20:25:38 +00:00
|
|
|
coin: _coin,
|
2023-09-22 23:00:32 +00:00
|
|
|
txHash: utxo.txid,
|
2023-09-22 20:25:38 +00:00
|
|
|
verbose: true,
|
|
|
|
);
|
2023-09-19 22:58:55 +00:00
|
|
|
|
|
|
|
// Check if scriptPubKey is available.
|
2023-09-22 20:25:38 +00:00
|
|
|
final scriptPubKeyHex =
|
2023-09-22 23:00:32 +00:00
|
|
|
tx["vout"]?[utxo.vout]?["scriptPubKey"]?["hex"] as String?;
|
2023-09-22 20:25:38 +00:00
|
|
|
if (scriptPubKeyHex == null) {
|
|
|
|
throw Exception(
|
2023-09-22 23:00:32 +00:00
|
|
|
"hex in scriptPubKey of vout index ${utxo.vout} in transaction is null",
|
2023-09-22 20:25:38 +00:00
|
|
|
);
|
2023-09-19 22:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign scriptPubKey to pubKey. TODO verify this is correct.
|
2023-09-22 20:25:38 +00:00
|
|
|
List<int> pubKey = scriptPubKeyHex.toUint8ListFromHex;
|
2023-09-19 22:58:55 +00:00
|
|
|
|
|
|
|
// Add UTXO to coinList.
|
2023-09-28 17:44:17 +00:00
|
|
|
coinList.add(
|
|
|
|
fusion.UtxoDTO(
|
|
|
|
txid: utxo.txid,
|
|
|
|
vout: utxo.vout,
|
|
|
|
value: utxo.value,
|
|
|
|
pubKey: pubKey,
|
|
|
|
),
|
|
|
|
);
|
2023-09-19 22:58:55 +00:00
|
|
|
}
|
2023-08-30 16:40:39 +00:00
|
|
|
|
2023-09-19 22:58:55 +00:00
|
|
|
// Add Stack UTXOs.
|
2023-09-29 16:47:53 +00:00
|
|
|
final inputs = await mainFusionObject.addCoinsFromWallet(coinList);
|
2023-08-07 02:15:05 +00:00
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// Fuse UTXOs.
|
2023-10-09 16:06:27 +00:00
|
|
|
return await mainFusionObject.fuse(
|
|
|
|
inputsFromWallet: inputs,
|
|
|
|
network:
|
|
|
|
_coin.isTestNet ? fusion.Utilities.testNet : fusion.Utilities.mainNet,
|
|
|
|
);
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refreshFusion() {
|
|
|
|
// TODO
|
2023-07-27 19:39:36 +00:00
|
|
|
throw UnimplementedError(
|
|
|
|
"TODO refreshFusion eg look up number of fusion participants connected/coordinating");
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-20 03:14:34 +00:00
|
|
|
|
2023-09-20 15:17:52 +00:00
|
|
|
/// An extension of Stack Wallet's Address class that adds CashFusion functionality.
|
|
|
|
extension FusionAddress on Address {
|
2023-09-28 16:41:39 +00:00
|
|
|
fusion.Address toFusionAddress() {
|
2023-09-22 16:48:14 +00:00
|
|
|
if (derivationPath == null) {
|
2023-09-22 17:18:43 +00:00
|
|
|
// throw Exception("Fusion Addresses require a derivation path");
|
|
|
|
// TODO calculate a derivation path if it is null.
|
2023-09-22 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 17:44:36 +00:00
|
|
|
final bool fusionReserved = otherData == kReservedFusionAddress;
|
|
|
|
|
2023-09-28 16:41:39 +00:00
|
|
|
return fusion.Address(
|
2023-10-09 16:06:27 +00:00
|
|
|
address: value,
|
2023-09-22 16:48:14 +00:00
|
|
|
publicKey: publicKey,
|
2023-10-09 17:44:36 +00:00
|
|
|
fusionReserved: fusionReserved,
|
2023-09-28 16:41:39 +00:00
|
|
|
derivationPath: fusion.DerivationPath(
|
2023-09-22 17:18:43 +00:00
|
|
|
derivationPath?.value ?? "", // TODO fix null derivation path.
|
2023-09-22 16:48:14 +00:00
|
|
|
),
|
|
|
|
);
|
2023-09-20 15:17:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An extension of Stack Wallet's UTXO class that adds CashFusion functionality.
|
|
|
|
///
|
|
|
|
/// This class is used to convert Stack Wallet's UTXO class to FusionDart's
|
|
|
|
/// Input and Output classes.
|
|
|
|
extension FusionUTXO on UTXO {
|
2023-09-22 16:48:14 +00:00
|
|
|
/// Fetch the public key of an address stored in the database.
|
|
|
|
Future<Address> _getAddressPubkey({
|
|
|
|
required String address,
|
|
|
|
required String walletId,
|
|
|
|
required MainDB dbInstance,
|
|
|
|
}) async {
|
|
|
|
final Address? addr = await dbInstance.getAddress(walletId, address);
|
|
|
|
|
|
|
|
if (addr == null) {
|
|
|
|
throw Exception("Address not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2023-09-20 15:17:52 +00:00
|
|
|
/// Converts a Stack Wallet UTXO to a FusionDart Input.
|
2023-10-09 21:05:04 +00:00
|
|
|
Future<fusion.UtxoDTO> toFusionUtxoDTO({
|
2023-09-22 16:48:14 +00:00
|
|
|
required String walletId,
|
|
|
|
required MainDB dbInstance,
|
|
|
|
}) async {
|
2023-09-22 05:38:32 +00:00
|
|
|
if (address == null) {
|
2023-09-22 16:48:14 +00:00
|
|
|
throw Exception("toFusionInput Address is null");
|
2023-09-21 21:57:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 16:48:14 +00:00
|
|
|
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");
|
|
|
|
}
|
2023-09-22 05:38:32 +00:00
|
|
|
|
2023-10-09 21:05:04 +00:00
|
|
|
return fusion.UtxoDTO(
|
|
|
|
txid: txid,
|
|
|
|
vout: vout,
|
2023-09-22 16:48:14 +00:00
|
|
|
pubKey: addr.publicKey,
|
2023-10-09 21:05:04 +00:00
|
|
|
value: value,
|
2023-09-22 16:48:14 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
rethrow;
|
|
|
|
}
|
2023-09-20 15:17:52 +00:00
|
|
|
}
|
|
|
|
}
|