feat: more info on privkey error (#1612)

Rafael committed Aug 15, 2024 at 22:34 UTC 102139b0611dc3dceffedea26085eaef89ac31f4
1 file changed +21 -3
cw_bitcoin/lib/electrum_wallet.dart
+21 -3
@@ -993,11 +993,29 @@ abstract class ElectrumWalletBase
993 bool hasTaprootInputs = false;
994
995 final transaction = txb.buildTransaction((txDigest, utxo, publicKey, sighash) {
996 - final key = estimatedTx.inputPrivKeyInfos
997 - .firstWhereOrNull((element) => element.privkey.getPublic().toHex() == publicKey);
996 + String error = "Cannot find private key.";
997 +
998 + ECPrivateInfo? key;
999 +
1000 + if (estimatedTx.inputPrivKeyInfos.isEmpty) {
1001 + error += "\nNo private keys generated.";
1002 + } else {
1003 + error += "\nAddress: ${utxo.ownerDetails.address.toAddress()}";
1004 +
1005 + key = estimatedTx.inputPrivKeyInfos.firstWhereOrNull((element) {
1006 + final elemPubkey = element.privkey.getPublic().toHex();
1007 + if (elemPubkey == publicKey) {
1008 + return true;
1009 + } else {
1010 + error += "\nExpected: $publicKey";
1011 + error += "\nPubkey: $elemPubkey";
1012 + return false;
1013 + }
1014 + });
1015 + }
1016
1017 if (key == null) {
1000 - throw Exception("Cannot find private key");
1018 + throw Exception(error);
1019 }
1020
1021 if (utxo.utxo.isP2tr()) {