CW-679 Add derivation Path of change address

This commit is contained in:
Konstantin Ullrich 2024-08-15 15:32:29 +02:00
parent 1c3db7f549
commit 71abf6a6b8
No known key found for this signature in database
GPG key ID: E9562A013280F5DB
3 changed files with 20 additions and 4 deletions

View file

@ -5,7 +5,6 @@ import 'dart:isolate';
import 'dart:math';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:cw_core/encryption_file_utils.dart';
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:collection/collection.dart';
import 'package:cw_bitcoin/address_from_output.dart';
@ -26,6 +25,7 @@ import 'package:cw_bitcoin/pending_bitcoin_transaction.dart';
import 'package:cw_bitcoin/script_hash.dart';
import 'package:cw_bitcoin/utils.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/get_height_by_date.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/pathForWallet.dart';
@ -769,12 +769,20 @@ abstract class ElectrumWalletBase
}
final changeAddress = await walletAddresses.getChangeAddress();
final address = addressTypeFromStr(changeAddress, network);
final address = addressTypeFromStr(changeAddress.address, network);
outputs.add(BitcoinOutput(
address: address,
value: BigInt.from(amountLeftForChangeAndFee),
));
// Get Derivation path for change Address since it is needed in Litecoin and BitcoinCash hardware Wallets
final changeDerivationPath =
"${_hardenedDerivationPath(walletInfo.derivationInfo?.derivationPath ?? "m/0'")}"
"/${changeAddress.isHidden ? "1" : "0"}"
"/${changeAddress.index}";
utxoDetails.publicKeys[address.pubKeyHash()] =
PublicKeyWithDerivationPath('', changeDerivationPath);
int estimatedSize;
if (network is BitcoinCashNetwork) {
estimatedSize = ForkedTransactionBuilder.estimateTransactionSize(

View file

@ -237,7 +237,7 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
}
@action
Future<String> getChangeAddress() async {
Future<BitcoinAddressRecord> getChangeAddress() async {
updateChangeAddresses();
if (changeAddresses.isEmpty) {
@ -252,7 +252,7 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
}
updateChangeAddresses();
final address = changeAddresses[currentChangeAddressIndex].address;
final address = changeAddresses[currentChangeAddressIndex];
currentChangeAddressIndex += 1;
return address;
}

View file

@ -214,6 +214,13 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
));
}
String? changePath;
for (final output in outputs) {
final maybeChangePath = publicKeys[(output as BitcoinOutput).address.pubKeyHash()];
if (maybeChangePath != null) changePath ??= maybeChangePath.derivationPath;
}
final rawHex = await _litecoinLedgerApp!.createTransaction(
_ledgerDevice!,
inputs: readyInputs,
@ -221,6 +228,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
.map((e) => TransactionOutput.fromBigInt(
(e as BitcoinOutput).value, Uint8List.fromList(e.address.toScriptPubKey().toBytes())))
.toList(),
changePath: changePath,
sigHashType: 0x01,
additionals: ["bech32"],
isSegWit: true,