mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-18 02:07:43 +00:00
add getSocksProxyAddress passin, documentation, and comments
This commit is contained in:
parent
1f1ceaae5a
commit
fafdb45e48
3 changed files with 71 additions and 7 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit 04c8bd01b1982d1543f3f0a2554361056eb7ef38
|
Subproject commit fb563fb37b0867cd1ada29e0b956778836049ebe
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
@ -11,15 +12,20 @@ import 'package:stackwallet/models/isar/models/isar_models.dart';
|
||||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||||
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
||||||
|
|
||||||
|
import 'package:stackwallet/services/tor_service.dart';
|
||||||
|
|
||||||
const String kReservedFusionAddress = "reserved_fusion_address";
|
const String kReservedFusionAddress = "reserved_fusion_address";
|
||||||
|
|
||||||
|
|
||||||
|
/// A mixin for the BitcoinCashWallet class that adds CashFusion functionality.
|
||||||
mixin FusionWalletInterface {
|
mixin FusionWalletInterface {
|
||||||
// passed in wallet data
|
// Passed in wallet data.
|
||||||
late final String _walletId;
|
late final String _walletId;
|
||||||
late final Coin _coin;
|
late final Coin _coin;
|
||||||
late final MainDB _db;
|
late final MainDB _db;
|
||||||
|
late final TorService _torService;
|
||||||
|
|
||||||
// passed in wallet functions
|
// Passed in wallet functions.
|
||||||
late final Future<Address> Function(
|
late final Future<Address> Function(
|
||||||
int chain,
|
int chain,
|
||||||
int index,
|
int index,
|
||||||
|
@ -40,13 +46,29 @@ mixin FusionWalletInterface {
|
||||||
_coin = coin;
|
_coin = coin;
|
||||||
_db = db;
|
_db = db;
|
||||||
_generateAddressForChain = generateAddressForChain;
|
_generateAddressForChain = generateAddressForChain;
|
||||||
|
_torService = TorService.sharedInstance;
|
||||||
|
|
||||||
|
// Start the Tor service if it's not already running.
|
||||||
|
if (_torService.proxyInfo.port == -1) { // -1 indicates that the proxy is not running.
|
||||||
|
// Initialize the ffi lib instance if it hasn't already been set.
|
||||||
|
_torService.init();
|
||||||
|
|
||||||
|
// Start the Tor service.
|
||||||
|
//
|
||||||
|
// TODO should we await this? At this point I don't want to make this init function async.
|
||||||
|
// The risk would be that the Tor service is not started before the Fusion library tries to
|
||||||
|
// connect to it.
|
||||||
|
unawaited(_torService.start());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a list of all addresses in the wallet.
|
||||||
Future<List<fusion_address.Address>> getFusionAddresses() async {
|
Future<List<fusion_address.Address>> getFusionAddresses() async {
|
||||||
List<Address> _addresses = await _db.getAddresses(_walletId).findAll();
|
List<Address> _addresses = await _db.getAddresses(_walletId).findAll();
|
||||||
return _addresses.map((address) => address.toFusionAddress()).toList();
|
return _addresses.map((address) => address.toFusionAddress()).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a list of all transactions in the wallet for the given address.
|
||||||
Future<Set<fusion_tx.Transaction>> getTransactionsByAddress(
|
Future<Set<fusion_tx.Transaction>> getTransactionsByAddress(
|
||||||
String address) async {
|
String address) async {
|
||||||
var _txs = await _db.getTransactions(_walletId).findAll();
|
var _txs = await _db.getTransactions(_walletId).findAll();
|
||||||
|
@ -56,6 +78,7 @@ mixin FusionWalletInterface {
|
||||||
.toSet(); // TODO feed in proper public key
|
.toSet(); // TODO feed in proper public key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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();
|
var _utxos = await _db.getUTXOsByAddress(_walletId, address).findAll();
|
||||||
|
|
||||||
|
@ -65,6 +88,7 @@ mixin FusionWalletInterface {
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new reserved change address.
|
||||||
Future<fusion_address.Address> createNewReservedChangeAddress() async {
|
Future<fusion_address.Address> createNewReservedChangeAddress() async {
|
||||||
int? highestChangeIndex = await _db
|
int? highestChangeIndex = await _db
|
||||||
.getAddresses(_walletId)
|
.getAddresses(_walletId)
|
||||||
|
@ -96,44 +120,78 @@ mixin FusionWalletInterface {
|
||||||
return address.toFusionAddress();
|
return address.toFusionAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a list of unused reserved change addresses.
|
||||||
|
///
|
||||||
|
/// If there are not enough unused reserved change addresses, new ones are created.
|
||||||
Future<List<fusion_address.Address>> getUnusedReservedChangeAddresses(
|
Future<List<fusion_address.Address>> getUnusedReservedChangeAddresses(
|
||||||
int numberOfAddresses,
|
int numberOfAddresses,
|
||||||
) async {
|
) async {
|
||||||
|
// Fetch all transactions that have been sent to a reserved change address.
|
||||||
final txns = await _db
|
final txns = await _db
|
||||||
.getTransactions(_walletId)
|
.getTransactions(_walletId)
|
||||||
.filter()
|
.filter()
|
||||||
.address((q) => q.otherDataEqualTo(kReservedFusionAddress))
|
.address((q) => q.otherDataEqualTo(kReservedFusionAddress))
|
||||||
.findAll();
|
.findAll();
|
||||||
|
|
||||||
|
// Fetch all addresses that have been used in a transaction.
|
||||||
final List<String> usedAddresses = txns
|
final List<String> usedAddresses = txns
|
||||||
.where((e) => e.address.value != null)
|
.where((e) => e.address.value != null)
|
||||||
.map((e) => e.address.value!.value)
|
.map((e) => e.address.value!.value)
|
||||||
.toList(growable: false);
|
.toList(growable: false);
|
||||||
|
|
||||||
|
// Fetch all reserved change addresses.
|
||||||
final List<Address> addresses = await _db
|
final List<Address> addresses = await _db
|
||||||
.getAddresses(_walletId)
|
.getAddresses(_walletId)
|
||||||
.filter()
|
.filter()
|
||||||
.otherDataEqualTo(kReservedFusionAddress)
|
.otherDataEqualTo(kReservedFusionAddress)
|
||||||
.findAll();
|
.findAll();
|
||||||
|
|
||||||
|
// Initialize a list of unused reserved change addresses.
|
||||||
final List<fusion_address.Address> unusedAddresses = [];
|
final List<fusion_address.Address> unusedAddresses = [];
|
||||||
|
|
||||||
|
// Add any unused reserved change addresses to the list.
|
||||||
for (final address in addresses) {
|
for (final address in addresses) {
|
||||||
if (!usedAddresses.contains(address.value)) {
|
if (!usedAddresses.contains(address.value)) {
|
||||||
unusedAddresses.add(address.toFusionAddress());
|
unusedAddresses.add(address.toFusionAddress());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there are not enough unused reserved change addresses, create new ones.
|
||||||
if (unusedAddresses.length < numberOfAddresses) {
|
if (unusedAddresses.length < numberOfAddresses) {
|
||||||
for (int i = unusedAddresses.length; i < numberOfAddresses; i++) {
|
for (int i = unusedAddresses.length; i < numberOfAddresses; i++) {
|
||||||
unusedAddresses.add(await createNewReservedChangeAddress());
|
unusedAddresses.add(await createNewReservedChangeAddress());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the list of unused reserved change addresses.
|
||||||
return unusedAddresses;
|
return unusedAddresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fuse() async {
|
/// 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.
|
||||||
|
return _torService.proxyInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
// Initial attempt for CashFusion integration goes here.
|
// Initial attempt for CashFusion integration goes here.
|
||||||
Fusion mainFusionObject = Fusion(
|
Fusion mainFusionObject = Fusion(
|
||||||
getAddresses: () => getFusionAddresses(),
|
getAddresses: () => getFusionAddresses(),
|
||||||
|
@ -143,6 +201,7 @@ mixin FusionWalletInterface {
|
||||||
// createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
|
// createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
|
||||||
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
|
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
|
||||||
getUnusedReservedChangeAddresses(numberOfAddresses),
|
getUnusedReservedChangeAddresses(numberOfAddresses),
|
||||||
|
getSocksProxyAddress: () => getSocksProxyAddress(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Pass wallet functions to the Fusion object
|
// Pass wallet functions to the Fusion object
|
||||||
|
@ -151,7 +210,8 @@ mixin FusionWalletInterface {
|
||||||
getTransactionsByAddress: getTransactionsByAddress,
|
getTransactionsByAddress: getTransactionsByAddress,
|
||||||
getInputsByAddress: getInputsByAddress,
|
getInputsByAddress: getInputsByAddress,
|
||||||
/*createNewReservedChangeAddress: createNewReservedChangeAddress,*/
|
/*createNewReservedChangeAddress: createNewReservedChangeAddress,*/
|
||||||
getUnusedReservedChangeAddresses: getUnusedReservedChangeAddresses);
|
getUnusedReservedChangeAddresses: getUnusedReservedChangeAddresses,
|
||||||
|
getSocksProxyAddress: getSocksProxyAddress);
|
||||||
|
|
||||||
// Add stack UTXOs.
|
// Add stack UTXOs.
|
||||||
List<UTXO> utxos = await _db.getUTXOs(_walletId).findAll();
|
List<UTXO> utxos = await _db.getUTXOs(_walletId).findAll();
|
||||||
|
@ -160,9 +220,11 @@ mixin FusionWalletInterface {
|
||||||
utxos.map((e) => (e.txid, e.vout, e.value)).toList());
|
utxos.map((e) => (e.txid, e.vout, e.value)).toList());
|
||||||
|
|
||||||
// Fuse UTXOs.
|
// Fuse UTXOs.
|
||||||
await mainFusionObject.fuse();
|
return await mainFusionObject.fuse();
|
||||||
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");
|
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");
|
||||||
|
|
||||||
|
// TODO remove or fix code below.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
print("DEBUG: Waiting for any potential incoming data...");
|
print("DEBUG: Waiting for any potential incoming data...");
|
||||||
try {
|
try {
|
||||||
|
@ -172,7 +234,6 @@ mixin FusionWalletInterface {
|
||||||
print (e);
|
print (e);
|
||||||
}
|
}
|
||||||
print("DEBUG: Done waiting.");
|
print("DEBUG: Done waiting.");
|
||||||
*/
|
|
||||||
|
|
||||||
bool mydebug1 = false;
|
bool mydebug1 = false;
|
||||||
if (mydebug1 == true) {
|
if (mydebug1 == true) {
|
||||||
|
@ -258,6 +319,7 @@ mixin FusionWalletInterface {
|
||||||
|
|
||||||
// await _checkCurrentChangeAddressesForTransactions();
|
// await _checkCurrentChangeAddressesForTransactions();
|
||||||
// await _checkCurrentReceivingAddressesForTransactions();
|
// await _checkCurrentReceivingAddressesForTransactions();
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> refreshFusion() {
|
Future<void> refreshFusion() {
|
||||||
|
|
|
@ -25,6 +25,8 @@ class TorService {
|
||||||
static final sharedInstance = TorService._();
|
static final sharedInstance = TorService._();
|
||||||
|
|
||||||
/// Getter for the proxyInfo.
|
/// Getter for the proxyInfo.
|
||||||
|
///
|
||||||
|
/// Returns a Map with the host and port of the Tor proxy.
|
||||||
({
|
({
|
||||||
InternetAddress host,
|
InternetAddress host,
|
||||||
int port,
|
int port,
|
||||||
|
|
Loading…
Reference in a new issue