From d73a90fb5177e3b24a12959d24485715bb659cc1 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 9 Oct 2023 15:42:42 -0600 Subject: [PATCH] privkey lookup for signing --- fusiondart | 2 +- .../coins/bitcoincash/bitcoincash_wallet.dart | 3 ++ .../mixins/fusion_wallet_interface.dart | 35 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fusiondart b/fusiondart index 56de6e9e8..dccd3a5c2 160000 --- a/fusiondart +++ b/fusiondart @@ -1 +1 @@ -Subproject commit 56de6e9e8cd3f1af9959be7dc4c14131058e7430 +Subproject commit dccd3a5c23aee021f49553c2da1ab8d59e6da5ea diff --git a/lib/services/coins/bitcoincash/bitcoincash_wallet.dart b/lib/services/coins/bitcoincash/bitcoincash_wallet.dart index 4d8d5a444..ab9aedf71 100644 --- a/lib/services/coins/bitcoincash/bitcoincash_wallet.dart +++ b/lib/services/coins/bitcoincash/bitcoincash_wallet.dart @@ -146,6 +146,9 @@ class BitcoinCashWallet extends CoinServiceAPI }, getTxCountForAddress: getTxCount, getChainHeight: () async => chainHeight, + mnemonic: mnemonicString, + mnemonicPassphrase: mnemonicPassphrase, + network: _network, ); initCoinControlInterface( walletId: walletId, diff --git a/lib/services/mixins/fusion_wallet_interface.dart b/lib/services/mixins/fusion_wallet_interface.dart index 02d093296..1c66271ac 100644 --- a/lib/services/mixins/fusion_wallet_interface.dart +++ b/lib/services/mixins/fusion_wallet_interface.dart @@ -1,6 +1,8 @@ import 'dart:async'; import 'dart:io'; +import 'dart:typed_data'; +import 'package:bitcoindart/bitcoindart.dart' as btcdart; import 'package:fusiondart/fusiondart.dart' as fusion; import 'package:isar/isar.dart'; import 'package:stackwallet/db/isar/main_db.dart'; @@ -8,6 +10,7 @@ import 'package:stackwallet/electrumx_rpc/cached_electrumx.dart'; import 'package:stackwallet/models/fusion_progress_ui_state.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/services/fusion_tor_service.dart'; +import 'package:stackwallet/utilities/bip32_utils.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/extensions/impl/string.dart'; import 'package:stackwallet/utilities/stack_file_system.dart'; @@ -21,6 +24,9 @@ mixin FusionWalletInterface { late final Coin _coin; late final MainDB _db; late final FusionTorService _torService; + late final Future _mnemonic; + late final Future _mnemonicPassphrase; + late final btcdart.NetworkType _network; // setting values on this should notify any listeners (the GUI) FusionProgressUIState? _uiState; @@ -61,6 +67,9 @@ mixin FusionWalletInterface { required String address, }) getTxCountForAddress, required Future Function() getChainHeight, + required Future mnemonic, + required Future mnemonicPassphrase, + required btcdart.NetworkType network, }) async { // Set passed in wallet data. _walletId = walletId; @@ -71,6 +80,9 @@ mixin FusionWalletInterface { _getWalletCachedElectrumX = getWalletCachedElectrumX; _getTxCountForAddress = getTxCountForAddress; _getChainHeight = getChainHeight; + _mnemonic = mnemonic; + _mnemonicPassphrase = mnemonicPassphrase; + _network = network; } // callback to update the ui state object @@ -112,6 +124,28 @@ mixin FusionWalletInterface { return await Future.wait(futures); } + Future getPrivateKeyForPubKey(List 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"); + } + } + /// Returns a list of all UTXOs in the wallet for the given address. Future> getInputsByAddress(String address) async { final _utxos = await _db.getUTXOsByAddress(_walletId, address).findAll(); @@ -251,6 +285,7 @@ mixin FusionWalletInterface { coin: _coin, txHash: txid, ), + getPrivateKeyForPubKey: getPrivateKeyForPubKey, ); // Add stack UTXOs.