use address type implied by derivation path in view only wallets

This commit is contained in:
julian 2024-11-14 14:41:31 -06:00 committed by julian-CStack
parent 3177fadea6
commit 44f9886ffd

View file

@ -85,10 +85,17 @@ abstract class Bip39HDWallet<T extends Bip39HDCurrency> extends Bip39Wallet<T>
final index = current == null ? 0 : current.derivationIndex + 1;
const chain = 0; // receiving address
final DerivePathType type;
if (isViewOnly) {
type = await _viewOnlyPathHelper();
} else {
type = _fromAddressType(info.mainAddressType);
}
final address = await _generateAddress(
chain: chain,
index: index,
derivePathType: _fromAddressType(info.mainAddressType),
derivePathType: type,
);
await mainDB.updateOrPutAddresses([address]);
@ -107,10 +114,17 @@ abstract class Bip39HDWallet<T extends Bip39HDCurrency> extends Bip39Wallet<T>
final index = current == null ? 0 : current.derivationIndex + 1;
const chain = 1; // change address
final DerivePathType type;
if (isViewOnly) {
type = await _viewOnlyPathHelper();
} else {
type = _fromAddressType(info.mainAddressType);
}
final address = await _generateAddress(
chain: chain,
index: index,
derivePathType: _fromAddressType(info.mainAddressType),
derivePathType: type,
);
await mainDB.updateOrPutAddresses([address]);
@ -122,10 +136,17 @@ abstract class Bip39HDWallet<T extends Bip39HDCurrency> extends Bip39Wallet<T>
final current = await getCurrentReceivingAddress();
if (current == null) {
final DerivePathType type;
if (isViewOnly) {
type = await _viewOnlyPathHelper();
} else {
type = _fromAddressType(info.mainAddressType);
}
final address = await _generateAddress(
chain: 0, // receiving
index: 0, // initial index
derivePathType: _fromAddressType(info.mainAddressType),
derivePathType: type,
);
await mainDB.updateOrPutAddresses([address]);
@ -142,6 +163,25 @@ abstract class Bip39HDWallet<T extends Bip39HDCurrency> extends Bip39Wallet<T>
// ========== Private ========================================================
Future<DerivePathType> _viewOnlyPathHelper() async {
final voData =
await getViewOnlyWalletData() as ExtendedKeysViewOnlyWalletData;
for (final type in cryptoCurrency.supportedDerivationPathTypes) {
final testPath = cryptoCurrency.constructDerivePath(
derivePathType: type,
chain: 0,
index: 0,
);
if (testPath.startsWith(voData.xPubs.first.path)) {
return type;
}
}
throw Exception(
"_viewOnlyPathHelper viewOnly failed to match paths",
);
}
DerivePathType _fromAddressType(AddressType addressType) {
switch (addressType) {
case AddressType.p2pkh: