cake_wallet/cw_bitcoin/lib/litecoin_hardware_wallet_service.dart

46 lines
1.5 KiB
Dart
Raw Normal View History

2024-07-15 16:01:22 +00:00
import 'dart:async';
import 'package:bitcoin_base/bitcoin_base.dart';
2024-08-12 15:33:09 +00:00
import 'package:blockchain_utils/blockchain_utils.dart';
2024-07-15 16:01:22 +00:00
import 'package:cw_bitcoin/utils.dart';
import 'package:cw_core/hardware/hardware_account_data.dart';
import 'package:ledger_flutter/ledger_flutter.dart';
import 'package:ledger_litecoin/ledger_litecoin.dart';
class LitecoinHardwareWalletService {
LitecoinHardwareWalletService(this.ledger, this.device);
final Ledger ledger;
final LedgerDevice device;
Future<List<HardwareAccountData>> getAvailableAccounts({int index = 0, int limit = 5}) async {
final litecoinLedgerApp = LitecoinLedgerApp(ledger);
final version = await litecoinLedgerApp.getVersion(device);
print(version);
final accounts = <HardwareAccountData>[];
final indexRange = List.generate(limit, (i) => i + index);
2024-08-12 15:33:09 +00:00
final xpubVersion = Bip44Conf.litecoinMainNet.altKeyNetVer;
2024-07-15 16:01:22 +00:00
for (final i in indexRange) {
final derivationPath = "m/84'/2'/$i'";
final xpub = await litecoinLedgerApp.getXPubKey(device,
2024-08-12 15:33:09 +00:00
accountsDerivationPath: derivationPath,
xPubVersion: int.parse(hex.encode(xpubVersion.public), radix: 16));
2024-08-12 15:44:15 +00:00
final hd = Bip32Slip10Secp256k1.fromExtendedKey(xpub, xpubVersion).childKey(Bip32KeyIndex(0));
2024-07-15 16:01:22 +00:00
final address = generateP2WPKHAddress(hd: hd, index: 0, network: LitecoinNetwork.mainnet);
accounts.add(HardwareAccountData(
address: address,
accountIndex: i,
derivationPath: derivationPath,
xpub: xpub,
));
}
return accounts;
}
}