mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-09 12:19:24 +00:00
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:
parent
5f1e936435
commit
aba37faacb
5 changed files with 111 additions and 11 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit e260d01513dc8bce1ba3e3fc67a21fa94b4e5854
|
Subproject commit 527a80ff1ff65f6e0e7450867dbda2cf642c12b6
|
|
@ -241,6 +241,14 @@ class MainDB {
|
||||||
QueryBuilder<UTXO, UTXO, QAfterWhereClause> getUTXOs(String walletId) =>
|
QueryBuilder<UTXO, UTXO, QAfterWhereClause> getUTXOs(String walletId) =>
|
||||||
isar.utxos.where().walletIdEqualTo(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 {
|
Future<void> putUTXO(UTXO utxo) => isar.writeTxn(() async {
|
||||||
await isar.utxos.put(utxo);
|
await isar.utxos.put(utxo);
|
||||||
});
|
});
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:math';
|
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:isar/isar.dart';
|
||||||
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
||||||
import 'package:stackwallet/models/isar/models/blockchain_data/input.dart';
|
import 'package:stackwallet/models/isar/models/blockchain_data/input.dart';
|
||||||
|
@ -22,7 +26,6 @@ part 'transaction.g.dart';
|
||||||
|
|
||||||
@Collection()
|
@Collection()
|
||||||
class Transaction {
|
class Transaction {
|
||||||
|
|
||||||
Transaction({
|
Transaction({
|
||||||
required this.walletId,
|
required this.walletId,
|
||||||
required this.txid,
|
required this.txid,
|
||||||
|
@ -232,6 +235,37 @@ class Transaction {
|
||||||
return Tuple2(transaction, address);
|
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
|
// Used in Isar db and stored there as int indexes so adding/removing values
|
||||||
|
|
|
@ -8,8 +8,10 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
|
import 'package:fusiondart/src/models/input.dart' as FusionInput;
|
||||||
import 'package:isar/isar.dart';
|
import 'package:isar/isar.dart';
|
||||||
|
|
||||||
part 'utxo.g.dart';
|
part 'utxo.g.dart';
|
||||||
|
@ -145,3 +147,14 @@ class UTXO {
|
||||||
@ignore
|
@ignore
|
||||||
int get hashCode => Object.hashAll([walletId, txid, vout]);
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:fusiondart/fusiondart.dart';
|
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:isar/isar.dart';
|
||||||
import 'package:stackwallet/db/isar/main_db.dart';
|
import 'package:stackwallet/db/isar/main_db.dart';
|
||||||
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
||||||
|
@ -39,7 +42,30 @@ mixin FusionWalletInterface {
|
||||||
_generateAddressForChain = generateAddressForChain;
|
_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
|
int? highestChangeIndex = await _db
|
||||||
.getAddresses(_walletId)
|
.getAddresses(_walletId)
|
||||||
.filter()
|
.filter()
|
||||||
|
@ -57,14 +83,20 @@ mixin FusionWalletInterface {
|
||||||
);
|
);
|
||||||
address = address.copyWith(otherData: kReservedFusionAddress);
|
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();
|
return address.toFusionAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<cash_fusion.Address>> getUnusedReservedChangeAddresses(
|
Future<List<fusion_address.Address>> getUnusedReservedChangeAddresses(
|
||||||
int numberOfAddresses,
|
int numberOfAddresses,
|
||||||
) async {
|
) async {
|
||||||
final txns = await _db
|
final txns = await _db
|
||||||
|
@ -84,7 +116,7 @@ mixin FusionWalletInterface {
|
||||||
.otherDataEqualTo(kReservedFusionAddress)
|
.otherDataEqualTo(kReservedFusionAddress)
|
||||||
.findAll();
|
.findAll();
|
||||||
|
|
||||||
final List<cash_fusion.Address> unusedAddresses = [];
|
final List<fusion_address.Address> unusedAddresses = [];
|
||||||
|
|
||||||
for (final address in addresses) {
|
for (final address in addresses) {
|
||||||
if (!usedAddresses.contains(address.value)) {
|
if (!usedAddresses.contains(address.value)) {
|
||||||
|
@ -104,17 +136,30 @@ mixin FusionWalletInterface {
|
||||||
void fuse() async {
|
void fuse() async {
|
||||||
// Initial attempt for CashFusion integration goes here.
|
// Initial attempt for CashFusion integration goes here.
|
||||||
Fusion mainFusionObject = Fusion(
|
Fusion mainFusionObject = Fusion(
|
||||||
createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
|
getAddresses: () => getFusionAddresses(),
|
||||||
|
getTransactionsByAddress: (String address) =>
|
||||||
|
getTransactionsByAddress(address),
|
||||||
|
getInputsByAddress: (String address) => getInputsByAddress(address),
|
||||||
|
// createNewReservedChangeAddress: () => createNewReservedChangeAddress(),
|
||||||
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
|
getUnusedReservedChangeAddresses: (int numberOfAddresses) =>
|
||||||
getUnusedReservedChangeAddresses(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();
|
List<UTXO> utxos = await _db.getUTXOs(_walletId).findAll();
|
||||||
|
|
||||||
await mainFusionObject.addCoinsFromWallet(
|
await mainFusionObject.addCoinsFromWallet(
|
||||||
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();
|
await mainFusionObject.fuse();
|
||||||
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");
|
//print ("DEBUG FUSION bitcoincash_wallet.dart 1202");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue