1717#include " WalletManager.h"
1818#include " Wallets/SyncWalletsManager.h"
1919#include " Wallets/SyncHDWallet.h"
20+ #include " ProtobufHeadlessUtils.h"
2021
2122HwDeviceManager::HwDeviceManager (const std::shared_ptr<ConnectionManager>& connectionManager, std::shared_ptr<bs::sync::WalletsManager> walletManager,
2223 bool testNet, QObject* parent /* = nullptr*/ )
2324 : QObject(parent)
25+ , logger_(connectionManager->GetLogger ())
2426 , testNet_(testNet)
2527{
2628 walletManager_ = walletManager;
2729 trezorClient_ = std::make_unique<TrezorClient>(connectionManager, walletManager, testNet, this );
28- ledgerClient_ = std::make_unique<LedgerClient>(connectionManager-> GetLogger () , walletManager, testNet);
30+ ledgerClient_ = std::make_unique<LedgerClient>(logger_ , walletManager, testNet);
2931
3032 model_ = new HwDeviceModel (this );
3133}
3234
33- HwDeviceManager::~HwDeviceManager () = default ;
35+ HwDeviceManager::~HwDeviceManager ()
36+ {
37+ releaseConnection (nullptr );
38+ };
3439
3540void HwDeviceManager::scanDevices ()
3641{
@@ -51,7 +56,7 @@ void HwDeviceManager::scanDevices()
5156
5257 ledgerClient_->scanDevices (doneScanning);
5358 releaseConnection ([this , doneScanning] {
54- trezorClient_->initConnection ([this , doneScanning]() {
59+ trezorClient_->initConnection (true , [this , doneScanning]() {
5560 doneScanning ();
5661 });
5762 });
@@ -185,11 +190,41 @@ void HwDeviceManager::signTX(QVariant reqTX)
185190 return ;
186191 }
187192
188- device->signTX (reqTX, [this ](QVariant&& data) {
193+ Blocksettle::Communication::headless::SignTxRequest pbSignReq;
194+ bool rc = pbSignReq.ParseFromString (reqTX.toByteArray ().toStdString ());
195+ if (!rc) {
196+ SPDLOG_LOGGER_ERROR (logger_, " parse TX failed" );
197+ emit operationFailed (tr (" Invalid sign request" ));
198+ return ;
199+ }
200+
201+ auto signReq = bs::signer::pbTxRequestToCore (pbSignReq);
202+
203+ device->signTX (signReq, [this , signReq](QVariant&& data) {
189204 assert (data.canConvert <HWSignedTx>());
190205 auto tx = data.value <HWSignedTx>();
206+
207+ try {
208+ std::map<BinaryData, std::map<unsigned , UTXO>> utxoMap;
209+ for (const auto &utxo : signReq.inputs ) {
210+ auto & idMap = utxoMap[utxo.getTxHash ()];
211+ idMap.emplace (utxo.getTxOutIndex (), utxo);
212+ }
213+ unsigned flags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_SEGWIT | SCRIPT_VERIFY_P2SH_SHA256;
214+ bool validSign = Signer::verify (SecureBinaryData::fromString (tx.signedTx )
215+ , utxoMap, flags, true ).isValid ();
216+ if (!validSign) {
217+ SPDLOG_LOGGER_ERROR (logger_, " sign verification failed" );
218+ emit operationFailed (tr (" Sign verification failed" ));
219+ return ;
220+ }
221+ } catch (const std::exception &e) {
222+ SPDLOG_LOGGER_ERROR (logger_, " sign verification failed: {}" , e.what ());
223+ emit operationFailed (tr (" Sign verification failed" ));
224+ return ;
225+ }
226+
191227 txSigned ({ BinaryData::fromString (tx.signedTx ) });
192- releaseDevices ();
193228 });
194229
195230 connect (device, &HwDeviceInterface::requestPinMatrix,
@@ -235,7 +270,7 @@ void HwDeviceManager::releaseConnection(AsyncCallBack&& cb/*= nullptr*/)
235270 for (int i = 0 ; i < model_->rowCount (); ++i) {
236271 auto device = getDevice (model_->getDevice (i));
237272 if (device) {
238- trezorClient_->initConnection ([this , cbCopy = std::move (cb)] {
273+ trezorClient_->initConnection (true , [this , cbCopy = std::move (cb)] {
239274 trezorClient_->releaseConnection ([this , cb = std::move (cbCopy)]() {
240275 if (cb) {
241276 cb ();
0 commit comments