2023-09-14 22:28:11 +00:00
|
|
|
import 'dart:async';
|
2023-08-30 16:40:39 +00:00
|
|
|
import 'dart:convert';
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-09-20 03:14:34 +00:00
|
|
|
import 'package:decimal/decimal.dart';
|
2023-07-27 23:13:03 +00:00
|
|
|
import 'package:fusiondart/fusiondart.dart';
|
2023-08-30 16:40:39 +00:00
|
|
|
import 'package:fusiondart/src/models/address.dart' as fusion_address;
|
|
|
|
import 'package:fusiondart/src/models/input.dart' as fusion_input;
|
2023-09-20 03:14:34 +00:00
|
|
|
import 'package:fusiondart/src/models/output.dart' as fusion_output;
|
2023-08-30 16:40:39 +00:00
|
|
|
import 'package:fusiondart/src/models/transaction.dart' as fusion_tx;
|
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-08-07 18:54:44 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
2023-09-15 19:21:36 +00:00
|
|
|
import 'package:stackwallet/services/tor_service.dart';
|
2023-09-20 03:14:34 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-07-26 22:06:02 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-08-07 18:54:44 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/derive_path_type_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-19 22:58:55 +00:00
|
|
|
late final CachedElectrumX _cachedElectrumX;
|
2023-09-14 22:28:11 +00:00
|
|
|
late final TorService _torService;
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Passed in wallet functions.
|
2023-08-07 18:54:44 +00:00
|
|
|
late final Future<Address> Function(
|
|
|
|
int chain,
|
|
|
|
int index,
|
|
|
|
DerivePathType derivePathType,
|
|
|
|
) _generateAddressForChain;
|
2023-08-07 02:15:05 +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-08-07 18:54:44 +00:00
|
|
|
required Future<Address> Function(
|
|
|
|
int,
|
|
|
|
int,
|
|
|
|
DerivePathType,
|
|
|
|
) generateAddressForChain,
|
2023-09-19 22:58:55 +00:00
|
|
|
required CachedElectrumX cachedElectrumX,
|
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-08-07 18:54:44 +00:00
|
|
|
_generateAddressForChain = generateAddressForChain;
|
2023-09-14 22:28:11 +00:00
|
|
|
_torService = TorService.sharedInstance;
|
2023-09-19 22:58:55 +00:00
|
|
|
_cachedElectrumX = cachedElectrumX;
|
2023-09-14 22:28:11 +00:00
|
|
|
|
2023-09-15 21:02:25 +00:00
|
|
|
// Try getting the proxy info.
|
|
|
|
//
|
|
|
|
// Start the Tor service if it's not already running. Returns if Tor is already
|
|
|
|
// connected or else after Tor returns from start().
|
|
|
|
try {
|
|
|
|
_torService.getProxyInfo();
|
|
|
|
// Proxy info successfully retrieved, Tor is connected.
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
// Init the Tor service if it hasn't already been.
|
|
|
|
final torDir = await StackFileSystem.applicationTorDirectory();
|
|
|
|
_torService.init(torDataDirPath: torDir.path);
|
2023-09-14 22:28:11 +00:00
|
|
|
|
|
|
|
// Start the Tor service.
|
2023-09-15 21:02:25 +00:00
|
|
|
return await _torService.start();
|
2023-09-14 22:28:11 +00:00
|
|
|
}
|
2023-07-26 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all addresses in the wallet.
|
2023-08-30 16:40:39 +00:00
|
|
|
Future<List<fusion_address.Address>> getFusionAddresses() async {
|
|
|
|
List<Address> _addresses = await _db.getAddresses(_walletId).findAll();
|
|
|
|
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-09-22 16:48:14 +00:00
|
|
|
Future<List<fusion_tx.Transaction>> getTransactionsByAddress(
|
2023-08-30 16:40:39 +00:00
|
|
|
String address) async {
|
2023-09-22 16:48:14 +00:00
|
|
|
final _txs = await _db.getTransactions(_walletId).findAll();
|
2023-08-30 16:40:39 +00:00
|
|
|
|
2023-09-19 22:58:55 +00:00
|
|
|
// Use Future.wait to await all the futures in the set and then convert it to a set.
|
2023-09-22 16:48:14 +00:00
|
|
|
final resultSet = await Future.wait(
|
2023-09-22 18:50:59 +00:00
|
|
|
_txs.map(
|
|
|
|
(tx) => tx.toFusionTransaction(
|
|
|
|
dbInstance: _db,
|
|
|
|
cachedElectrumX: _cachedElectrumX,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2023-09-19 22:58:55 +00:00
|
|
|
|
2023-09-22 16:48:14 +00:00
|
|
|
return resultSet;
|
2023-08-30 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns a list of all UTXOs in the wallet for the given address.
|
2023-08-30 16:40:39 +00:00
|
|
|
Future<List<fusion_input.Input>> 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-09-22 16:48:14 +00:00
|
|
|
List<Future<fusion_input.Input>> futureInputs = _utxos
|
|
|
|
.map(
|
|
|
|
(utxo) => utxo.toFusionInput(
|
|
|
|
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-08-30 16:40:39 +00:00
|
|
|
Future<fusion_address.Address> createNewReservedChangeAddress() async {
|
2023-08-07 18:54:44 +00:00
|
|
|
int? highestChangeIndex = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.typeEqualTo(AddressType.p2pkh)
|
|
|
|
.subTypeEqualTo(AddressSubType.change)
|
|
|
|
.derivationPath((q) => q.not().valueStartsWith("m/44'/0'"))
|
|
|
|
.sortByDerivationIndexDesc()
|
|
|
|
.derivationIndexProperty()
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
Address address = await _generateAddressForChain(
|
|
|
|
1, // change chain
|
|
|
|
highestChangeIndex ?? 0,
|
|
|
|
DerivePathTypeExt.primaryFor(_coin),
|
|
|
|
);
|
|
|
|
address = address.copyWith(otherData: kReservedFusionAddress);
|
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// TODO if we really want to be sure it's not used, call electrumx and check it
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
Address? _address = await _db.getAddress(_walletId, address.value);
|
|
|
|
if (_address != null) {
|
|
|
|
// throw Exception("Address already exists");
|
|
|
|
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-08-30 16:40:39 +00:00
|
|
|
Future<List<fusion_address.Address>> getUnusedReservedChangeAddresses(
|
2023-08-07 18:54:44 +00:00
|
|
|
int numberOfAddresses,
|
|
|
|
) async {
|
2023-09-14 22:28:11 +00:00
|
|
|
// Fetch all transactions that have been sent to a reserved change address.
|
2023-08-07 18:54:44 +00:00
|
|
|
final txns = await _db
|
|
|
|
.getTransactions(_walletId)
|
|
|
|
.filter()
|
|
|
|
.address((q) => q.otherDataEqualTo(kReservedFusionAddress))
|
|
|
|
.findAll();
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Fetch all addresses that have been used in a transaction.
|
2023-08-07 18:54:44 +00:00
|
|
|
final List<String> usedAddresses = txns
|
|
|
|
.where((e) => e.address.value != null)
|
|
|
|
.map((e) => e.address.value!.value)
|
|
|
|
.toList(growable: false);
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Fetch all reserved change addresses.
|
2023-08-07 18:54:44 +00:00
|
|
|
final List<Address> addresses = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
|
|
|
.otherDataEqualTo(kReservedFusionAddress)
|
|
|
|
.findAll();
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Initialize a list of unused reserved change addresses.
|
2023-08-30 16:40:39 +00:00
|
|
|
final List<fusion_address.Address> unusedAddresses = [];
|
2023-08-07 18:54:44 +00:00
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Add any unused reserved change addresses to the list.
|
2023-08-07 18:54:44 +00:00
|
|
|
for (final address in addresses) {
|
|
|
|
if (!usedAddresses.contains(address.value)) {
|
2023-08-07 19:29:56 +00:00
|
|
|
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-08-07 18:54:44 +00:00
|
|
|
if (unusedAddresses.length < numberOfAddresses) {
|
|
|
|
for (int i = unusedAddresses.length; i < numberOfAddresses; i++) {
|
|
|
|
unusedAddresses.add(await createNewReservedChangeAddress());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// Return the list of unused reserved change addresses.
|
2023-08-07 18:54:44 +00:00
|
|
|
return unusedAddresses;
|
2023-08-07 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
/// Returns the current Tor proxy address.
|
|
|
|
Future<({InternetAddress host, int port})> getSocksProxyAddress() async {
|
|
|
|
/*
|
|
|
|
// Start the Tor service if it's not already running.
|
|
|
|
if (_torService.proxyInfo.port == -1) { // -1 indicates that the proxy is not running.
|
|
|
|
await _torService.start(); // We already unawaited this in initFusionInterface...
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TODO make sure we've properly awaited the Tor service starting before
|
|
|
|
// returning the proxy address.
|
|
|
|
|
|
|
|
// Return the proxy address.
|
2023-09-15 21:02:25 +00:00
|
|
|
return _torService.getProxyInfo();
|
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-08-07 19:15:08 +00:00
|
|
|
Fusion mainFusionObject = Fusion(
|
2023-08-30 16:40:39 +00:00
|
|
|
getAddresses: () => getFusionAddresses(),
|
|
|
|
getTransactionsByAddress: (String address) =>
|
|
|
|
getTransactionsByAddress(address),
|
|
|
|
getInputsByAddress: (String address) => getInputsByAddress(address),
|
|
|
|
// createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
|
2023-08-08 14:32:11 +00:00
|
|
|
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
|
|
|
|
getUnusedReservedChangeAddresses(numberOfAddresses),
|
2023-09-14 22:28:11 +00:00
|
|
|
getSocksProxyAddress: () => getSocksProxyAddress(),
|
2023-08-08 14:32:11 +00:00
|
|
|
);
|
2023-07-26 22:06:02 +00:00
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// Pass wallet functions to the Fusion object
|
|
|
|
mainFusionObject.initFusion(
|
|
|
|
getAddresses: getFusionAddresses,
|
|
|
|
getTransactionsByAddress: getTransactionsByAddress,
|
|
|
|
getInputsByAddress: getInputsByAddress,
|
|
|
|
/*createNewReservedChangeAddress: createNewReservedChangeAddress,*/
|
2023-09-14 22:28:11 +00:00
|
|
|
getUnusedReservedChangeAddresses: getUnusedReservedChangeAddresses,
|
|
|
|
getSocksProxyAddress: getSocksProxyAddress);
|
2023-08-30 16:40:39 +00:00
|
|
|
|
|
|
|
// Add stack UTXOs.
|
2023-07-26 22:06:02 +00:00
|
|
|
List<UTXO> utxos = await _db.getUTXOs(_walletId).findAll();
|
2023-09-19 22:58:55 +00:00
|
|
|
List<(String, int, int, List<int>)> coinList = [];
|
|
|
|
|
|
|
|
// Loop through UTXOs, checking and adding valid ones.
|
|
|
|
for (var e in utxos) {
|
|
|
|
// Check if address is available.
|
|
|
|
if (e.address == null) {
|
|
|
|
// TODO we could continue here (and below during scriptPubKey validation) instead of throwing.
|
|
|
|
throw Exception("UTXO ${e.txid}:${e.vout} address is null");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find public key.
|
2023-09-20 03:14:34 +00:00
|
|
|
Map<String, dynamic> tx = await _cachedElectrumX.getTransaction(
|
|
|
|
coin: _coin,
|
|
|
|
txHash: e.txid,
|
|
|
|
verbose: true); // TODO is verbose needed?
|
2023-09-19 22:58:55 +00:00
|
|
|
|
|
|
|
// Check if scriptPubKey is available.
|
|
|
|
if (tx["vout"] == null) {
|
|
|
|
throw Exception("Vout in transaction ${e.txid} is null");
|
|
|
|
}
|
|
|
|
if (tx["vout"][e.vout] == null) {
|
|
|
|
throw Exception("Vout index ${e.vout} in transaction is null");
|
|
|
|
}
|
|
|
|
if (tx["vout"][e.vout]["scriptPubKey"] == null) {
|
|
|
|
throw Exception("scriptPubKey at vout index ${e.vout} is null");
|
|
|
|
}
|
|
|
|
if (tx["vout"][e.vout]["scriptPubKey"]["hex"] == null) {
|
|
|
|
throw Exception("hex in scriptPubKey at vout index ${e.vout} is null");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign scriptPubKey to pubKey. TODO verify this is correct.
|
|
|
|
List<int> pubKey =
|
|
|
|
utf8.encode("${tx["vout"][e.vout]["scriptPubKey"]["hex"]}");
|
|
|
|
|
|
|
|
// Add UTXO to coinList.
|
|
|
|
coinList.add((e.txid, e.vout, e.value, pubKey));
|
|
|
|
}
|
2023-08-30 16:40:39 +00:00
|
|
|
|
2023-09-19 22:58:55 +00:00
|
|
|
// Add Stack UTXOs.
|
|
|
|
await mainFusionObject.addCoinsFromWallet(coinList);
|
2023-08-07 02:15:05 +00:00
|
|
|
|
2023-08-30 16:40:39 +00:00
|
|
|
// Fuse UTXOs.
|
2023-09-14 22:28:11 +00:00
|
|
|
return await mainFusionObject.fuse();
|
2023-07-26 22:06:02 +00:00
|
|
|
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");
|
|
|
|
|
2023-09-14 22:28:11 +00:00
|
|
|
// TODO remove or fix code below.
|
|
|
|
|
2023-08-07 02:15:05 +00:00
|
|
|
/*
|
2023-07-26 22:06:02 +00:00
|
|
|
print("DEBUG: Waiting for any potential incoming data...");
|
|
|
|
try {
|
|
|
|
await Future.delayed(Duration(seconds: 5)); // wait for 5 seconds
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
print (e);
|
|
|
|
}
|
|
|
|
print("DEBUG: Done waiting.");
|
|
|
|
|
|
|
|
bool mydebug1 = false;
|
|
|
|
if (mydebug1 == true) {
|
|
|
|
var serverIp = '167.114.119.46';
|
|
|
|
var serverPort = 8787;
|
|
|
|
|
|
|
|
List<int> frame = [
|
|
|
|
118,
|
|
|
|
91,
|
|
|
|
232,
|
|
|
|
180,
|
|
|
|
228,
|
|
|
|
57,
|
|
|
|
109,
|
|
|
|
207,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
45,
|
|
|
|
10,
|
|
|
|
43,
|
|
|
|
10,
|
|
|
|
7,
|
|
|
|
97,
|
|
|
|
108,
|
|
|
|
112,
|
|
|
|
104,
|
|
|
|
97,
|
|
|
|
49,
|
|
|
|
51,
|
|
|
|
18,
|
|
|
|
32,
|
|
|
|
111,
|
|
|
|
226,
|
|
|
|
140,
|
|
|
|
10,
|
|
|
|
182,
|
|
|
|
241,
|
|
|
|
179,
|
|
|
|
114,
|
|
|
|
193,
|
|
|
|
166,
|
|
|
|
162,
|
|
|
|
70,
|
|
|
|
174,
|
|
|
|
99,
|
|
|
|
247,
|
|
|
|
79,
|
|
|
|
147,
|
|
|
|
30,
|
|
|
|
131,
|
|
|
|
101,
|
|
|
|
225,
|
|
|
|
90,
|
|
|
|
8,
|
|
|
|
156,
|
|
|
|
104,
|
|
|
|
214,
|
|
|
|
25,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
];
|
|
|
|
print("lets try to connect to a socket again");
|
|
|
|
var socket = await Socket.connect(serverIp, serverPort);
|
|
|
|
|
|
|
|
print('Connected to the server.');
|
|
|
|
socket.add(frame);
|
|
|
|
print('Sent frame: $frame');
|
|
|
|
|
|
|
|
socket.listen((data) {
|
|
|
|
print('Received from server: $data');
|
|
|
|
}, onDone: () {
|
|
|
|
print('Server closed connection.');
|
|
|
|
socket.destroy();
|
|
|
|
}, onError: (error) {
|
|
|
|
print('Error: $error');
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// await _checkCurrentChangeAddressesForTransactions();
|
|
|
|
// await _checkCurrentReceivingAddressesForTransactions();
|
2023-09-14 22:28:11 +00:00
|
|
|
*/
|
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 {
|
|
|
|
fusion_address.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-09-20 15:17:52 +00:00
|
|
|
return fusion_address.Address(
|
2023-09-22 16:48:14 +00:00
|
|
|
addr: value,
|
|
|
|
publicKey: publicKey,
|
|
|
|
derivationPath: fusion_address.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-09-22 16:48:14 +00:00
|
|
|
Future<fusion_input.Input> toFusionInput({
|
|
|
|
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-09-22 16:48:14 +00:00
|
|
|
return fusion_input.Input(
|
|
|
|
prevTxid: utf8.encode(txid),
|
|
|
|
prevIndex: vout,
|
|
|
|
pubKey: addr.publicKey,
|
|
|
|
amount: value,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
rethrow;
|
|
|
|
}
|
2023-09-20 15:17:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 16:45:16 +00:00
|
|
|
/// Converts a Stack Wallet UTXO to a FusionDart Output.
|
2023-09-22 16:48:14 +00:00
|
|
|
Future<fusion_output.Output> toFusionOutput({
|
|
|
|
required String walletId,
|
|
|
|
required MainDB dbInstance,
|
|
|
|
}) async {
|
2023-09-22 05:38:32 +00:00
|
|
|
if (address == null) {
|
|
|
|
throw Exception("toFutionOutput Address is null");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search isar for address to get pubKey.
|
2023-09-22 16:48:14 +00:00
|
|
|
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");
|
|
|
|
}
|
2023-09-22 05:38:32 +00:00
|
|
|
|
2023-09-20 15:17:52 +00:00
|
|
|
return fusion_output.Output(
|
|
|
|
addr: fusion_address.Address(
|
2023-09-22 05:38:32 +00:00
|
|
|
addr: address!,
|
2023-09-22 16:48:14 +00:00
|
|
|
publicKey: addr.publicKey,
|
|
|
|
derivationPath: fusion_address.DerivationPath(
|
|
|
|
addr.derivationPath!.value,
|
|
|
|
),
|
2023-09-20 15:17:52 +00:00
|
|
|
),
|
|
|
|
value: value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:14:34 +00:00
|
|
|
/// An extension of Stack Wallet's Transaction class that adds CashFusion functionality.
|
|
|
|
extension FusionTransaction on Transaction {
|
2023-09-22 18:50:59 +00:00
|
|
|
/// Fetch the public key of an address stored in the database.
|
|
|
|
Future<String?> _getAddressDerivationPathString({
|
|
|
|
required String address,
|
|
|
|
required MainDB dbInstance,
|
|
|
|
}) async {
|
|
|
|
final Address? addr = await dbInstance.getAddress(walletId, address);
|
|
|
|
|
|
|
|
return addr?.derivationPath?.value;
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:14:34 +00:00
|
|
|
// WIP.
|
2023-09-22 18:50:59 +00:00
|
|
|
Future<fusion_tx.Transaction> toFusionTransaction({
|
|
|
|
required CachedElectrumX cachedElectrumX,
|
|
|
|
required MainDB dbInstance,
|
|
|
|
}) async {
|
2023-09-20 03:14:34 +00:00
|
|
|
// Initialize Fusion Dart's Transaction object.
|
|
|
|
fusion_tx.Transaction fusionTransaction = fusion_tx.Transaction();
|
|
|
|
|
|
|
|
// WIP.
|
2023-09-22 18:50:59 +00:00
|
|
|
fusionTransaction.Inputs = await Future.wait(inputs.map((input) async {
|
2023-09-20 03:14:34 +00:00
|
|
|
// Find input amount.
|
|
|
|
Map<String, dynamic> _tx = await cachedElectrumX.getTransaction(
|
2023-09-22 19:33:36 +00:00
|
|
|
coin: Coin.bitcoincash,
|
|
|
|
txHash: input.txid,
|
|
|
|
verbose: true,
|
|
|
|
);
|
2023-09-20 03:14:34 +00:00
|
|
|
|
|
|
|
if (_tx.isEmpty) {
|
2023-09-22 18:50:59 +00:00
|
|
|
throw Exception("Transaction not found for input: ${input.txid}");
|
2023-09-20 03:14:34 +00:00
|
|
|
}
|
2023-09-22 18:50:59 +00:00
|
|
|
|
|
|
|
// Check if output amount is available.
|
|
|
|
final txVoutAmount = Decimal.tryParse(
|
|
|
|
_tx["vout"]?[input.vout]?["value"].toString() ?? "",
|
|
|
|
);
|
|
|
|
if (txVoutAmount == null) {
|
|
|
|
throw Exception(
|
|
|
|
"Output value at index ${input.vout} in transaction ${input.txid} not found",
|
|
|
|
);
|
2023-09-20 03:14:34 +00:00
|
|
|
}
|
2023-09-22 19:33:36 +00:00
|
|
|
|
|
|
|
final scriptPubKeyHex =
|
2023-09-22 19:49:08 +00:00
|
|
|
_tx["vout"]?[input.vout]?["scriptPubKey"]?["hex"] as String?;
|
2023-09-22 19:33:36 +00:00
|
|
|
if (scriptPubKeyHex == null) {
|
2023-09-22 17:18:43 +00:00
|
|
|
throw Exception(
|
2023-09-22 19:33:36 +00:00
|
|
|
"scriptPubKey of vout index ${input.vout} in transaction is null",
|
|
|
|
);
|
2023-09-22 17:18:43 +00:00
|
|
|
}
|
2023-09-20 03:14:34 +00:00
|
|
|
|
|
|
|
// Assign vout value to amount.
|
|
|
|
final value = Amount.fromDecimal(
|
2023-09-22 18:50:59 +00:00
|
|
|
txVoutAmount,
|
2023-09-20 03:14:34 +00:00
|
|
|
fractionDigits: Coin.bitcoincash.decimals,
|
|
|
|
);
|
|
|
|
|
|
|
|
return fusion_input.Input(
|
2023-09-22 18:50:59 +00:00
|
|
|
prevTxid: utf8.encode(input.txid),
|
|
|
|
prevIndex: input.vout,
|
2023-09-22 19:33:36 +00:00
|
|
|
pubKey: scriptPubKeyHex.toUint8ListFromHex,
|
2023-09-20 03:14:34 +00:00
|
|
|
amount: value.raw.toInt(),
|
|
|
|
);
|
|
|
|
}).toList());
|
|
|
|
|
2023-09-22 18:50:59 +00:00
|
|
|
fusionTransaction.Outputs = await Future.wait(outputs.map((output) async {
|
|
|
|
// TODO: maybe only need one of these but IIRC scriptPubKeyAddress is required for bitcoincash transactions?
|
|
|
|
if (output.scriptPubKeyAddress.isEmpty) {
|
|
|
|
throw Exception("isar model output.scriptPubKeyAddress is empty!");
|
|
|
|
}
|
|
|
|
if (output.scriptPubKey == null || output.scriptPubKey!.isEmpty) {
|
|
|
|
throw Exception("isar model output.scriptPubKey is null or empty!");
|
2023-09-20 03:14:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 18:50:59 +00:00
|
|
|
final outputAddress = output.scriptPubKeyAddress;
|
|
|
|
final outputAddressScriptPubKey = output.scriptPubKey!.toUint8ListFromHex;
|
2023-09-20 03:14:34 +00:00
|
|
|
|
2023-09-22 18:50:59 +00:00
|
|
|
// fetch address derivation path
|
|
|
|
final derivationPathString = await _getAddressDerivationPathString(
|
|
|
|
address: outputAddress,
|
|
|
|
dbInstance: dbInstance,
|
|
|
|
);
|
2023-09-22 19:01:02 +00:00
|
|
|
|
|
|
|
final fusion_address.DerivationPath? derivationPath;
|
2023-09-22 18:50:59 +00:00
|
|
|
if (derivationPathString == null) {
|
|
|
|
// TODO: check on this:
|
2023-09-22 19:01:02 +00:00
|
|
|
// The address is not an address of this wallet and in that case we
|
|
|
|
// cannot know the derivation path
|
|
|
|
derivationPath = null;
|
2023-09-20 03:14:34 +00:00
|
|
|
} else {
|
2023-09-22 18:50:59 +00:00
|
|
|
derivationPath = fusion_address.DerivationPath(
|
|
|
|
derivationPathString,
|
|
|
|
);
|
2023-09-20 03:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fusion_output.Output(
|
|
|
|
addr: fusion_address.Address(
|
2023-09-22 18:50:59 +00:00
|
|
|
addr: output.scriptPubKeyAddress,
|
|
|
|
publicKey: outputAddressScriptPubKey,
|
2023-09-20 03:14:34 +00:00
|
|
|
derivationPath: derivationPath,
|
|
|
|
),
|
2023-09-22 18:50:59 +00:00
|
|
|
value: output.value,
|
2023-09-20 03:14:34 +00:00
|
|
|
);
|
2023-09-22 18:50:59 +00:00
|
|
|
}).toList());
|
2023-09-20 03:14:34 +00:00
|
|
|
|
|
|
|
return fusionTransaction;
|
|
|
|
}
|
|
|
|
}
|