WIP join session, return early from covert

TODO fix `pubKey`s, `amount`s, etc passed in as 0000 or String? (whereas parameter is String for example, ie null awareness/safety)

Can instantiate a cached electrum client and call eg txid for info missing from the transaction blockchain model
This commit is contained in:
sneurlax 2023-08-30 11:40:39 -05:00
parent 5f1e936435
commit aba37faacb
5 changed files with 111 additions and 11 deletions

@ -1 +1 @@
Subproject commit e260d01513dc8bce1ba3e3fc67a21fa94b4e5854
Subproject commit 527a80ff1ff65f6e0e7450867dbda2cf642c12b6

View file

@ -241,6 +241,14 @@ class MainDB {
QueryBuilder<UTXO, UTXO, QAfterWhereClause> getUTXOs(String walletId) =>
isar.utxos.where().walletIdEqualTo(walletId);
QueryBuilder<UTXO, UTXO, QAfterFilterCondition> getUTXOsByAddress(
String walletId, String address) =>
isar.utxos
.where()
.walletIdEqualTo(walletId)
.filter()
.addressEqualTo(address);
Future<void> putUTXO(UTXO utxo) => isar.writeTxn(() async {
await isar.utxos.put(utxo);
});

View file

@ -11,6 +11,10 @@
import 'dart:convert';
import 'dart:math';
import 'package:fusiondart/src/models/address.dart' as fusion_address;
import 'package:fusiondart/src/models/input.dart' as fusion_input;
import 'package:fusiondart/src/models/output.dart' as fusion_output;
import 'package:fusiondart/src/models/transaction.dart' as fusion_tx;
import 'package:isar/isar.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/input.dart';
@ -22,7 +26,6 @@ part 'transaction.g.dart';
@Collection()
class Transaction {
Transaction({
required this.walletId,
required this.txid,
@ -232,6 +235,37 @@ class Transaction {
return Tuple2(transaction, address);
}
}
// Convert to fusiondart's Transaction type.
fusion_tx.Transaction toFusionTransaction() {
// Initialize Fusion Dart's Transaction object.
fusion_tx.Transaction fusionTransaction = fusion_tx.Transaction();
// Map the Inputs and Outputs to Fusion Dart's format
fusionTransaction.Inputs = inputs
.map((e) => fusion_input.Input(
prevTxid: utf8.encode(e.txid),
prevIndex: e.vout,
pubKey: utf8.encode(e.witness), // TODO fix
amount: 0, // TODO fix
))
.toList();
fusionTransaction.Outputs = outputs
.map((e) => fusion_output.Output(
addr: fusion_address.Address(
addr: e.scriptPubKeyAddress,
publicKey: utf8.encode(e.scriptPubKey),
derivationPath: fusion_address.DerivationPath(), // TODO fix
),
value: e.value,
))
.toList();
// If any other information needs to be altered/added, do so here.
return fusionTransaction;
}
}
// Used in Isar db and stored there as int indexes so adding/removing values

View file

@ -8,8 +8,10 @@
*
*/
import 'dart:convert';
import 'dart:math';
import 'package:fusiondart/src/models/input.dart' as FusionInput;
import 'package:isar/isar.dart';
part 'utxo.g.dart';
@ -145,3 +147,14 @@ class UTXO {
@ignore
int get hashCode => Object.hashAll([walletId, txid, vout]);
}
extension ToFusionInput on UTXO {
FusionInput.Input toFusionInput({required List<int> pubKey}) {
return FusionInput.Input(
prevTxid: utf8.encode(txid),
prevIndex: vout,
pubKey: pubKey,
amount: value,
);
}
}

View file

@ -1,7 +1,10 @@
import 'dart:convert';
import 'dart:io';
import 'package:fusiondart/fusiondart.dart';
import 'package:fusiondart/src/models/address.dart' as cash_fusion;
import 'package:fusiondart/src/models/address.dart' as fusion_address;
import 'package:fusiondart/src/models/input.dart' as fusion_input;
import 'package:fusiondart/src/models/transaction.dart' as fusion_tx;
import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
@ -39,7 +42,30 @@ mixin FusionWalletInterface {
_generateAddressForChain = generateAddressForChain;
}
Future<cash_fusion.Address> createNewReservedChangeAddress() async {
Future<List<fusion_address.Address>> getFusionAddresses() async {
List<Address> _addresses = await _db.getAddresses(_walletId).findAll();
return _addresses.map((address) => address.toFusionAddress()).toList();
}
Future<Set<fusion_tx.Transaction>> getTransactionsByAddress(
String address) async {
var _txs = await _db.getTransactions(_walletId).findAll();
return _txs
.map((tx) => tx.toFusionTransaction())
.toSet(); // TODO feed in proper public key
}
Future<List<fusion_input.Input>> getInputsByAddress(String address) async {
var _utxos = await _db.getUTXOsByAddress(_walletId, address).findAll();
return _utxos
.map((utxo) => utxo.toFusionInput(
pubKey: utf8.encode('0000'))) // TODO feed in proper public key
.toList();
}
Future<fusion_address.Address> createNewReservedChangeAddress() async {
int? highestChangeIndex = await _db
.getAddresses(_walletId)
.filter()
@ -57,14 +83,20 @@ mixin FusionWalletInterface {
);
address = address.copyWith(otherData: kReservedFusionAddress);
// TODO if we really want to be sure its not used, call electrumx and check it
// TODO if we really want to be sure it's not used, call electrumx and check it
await _db.putAddress(address);
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);
}
return address.toFusionAddress();
}
Future<List<cash_fusion.Address>> getUnusedReservedChangeAddresses(
Future<List<fusion_address.Address>> getUnusedReservedChangeAddresses(
int numberOfAddresses,
) async {
final txns = await _db
@ -84,7 +116,7 @@ mixin FusionWalletInterface {
.otherDataEqualTo(kReservedFusionAddress)
.findAll();
final List<cash_fusion.Address> unusedAddresses = [];
final List<fusion_address.Address> unusedAddresses = [];
for (final address in addresses) {
if (!usedAddresses.contains(address.value)) {
@ -104,17 +136,30 @@ mixin FusionWalletInterface {
void fuse() async {
// Initial attempt for CashFusion integration goes here.
Fusion mainFusionObject = Fusion(
createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
getAddresses: () => getFusionAddresses(),
getTransactionsByAddress: (String address) =>
getTransactionsByAddress(address),
getInputsByAddress: (String address) => getInputsByAddress(address),
// createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
getUnusedReservedChangeAddresses(numberOfAddresses),
);
// add stack utxos
// Pass wallet functions to the Fusion object
mainFusionObject.initFusion(
getAddresses: getFusionAddresses,
getTransactionsByAddress: getTransactionsByAddress,
getInputsByAddress: getInputsByAddress,
/*createNewReservedChangeAddress: createNewReservedChangeAddress,*/
getUnusedReservedChangeAddresses: getUnusedReservedChangeAddresses);
// Add stack UTXOs.
List<UTXO> utxos = await _db.getUTXOs(_walletId).findAll();
await mainFusionObject.addCoinsFromWallet(
utxos.map((e) => (e.txid, e.vout, e.value)).toList());
// fuse utxos
// Fuse UTXOs.
await mainFusionObject.fuse();
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");