From 812b808aced0793c0398ff11b22b00b0a149ea12 Mon Sep 17 00:00:00 2001 From: rileystephens28 Date: Thu, 14 Nov 2024 16:47:21 -0600 Subject: [PATCH] Exclude imported address from derivation and fix path deserialization --- src/wallet/qi-hdwallet.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/wallet/qi-hdwallet.ts b/src/wallet/qi-hdwallet.ts index f3ce4c1a..19e0b18c 100644 --- a/src/wallet/qi-hdwallet.ts +++ b/src/wallet/qi-hdwallet.ts @@ -212,7 +212,9 @@ export class QiHDWallet extends AbstractHDWallet { * @returns {string[]} The payment codes for all open channels. */ get openChannels(): string[] { - return Array.from(this._addressesMap.keys()).filter((key) => !key.startsWith('BIP44:')); + return Array.from(this._addressesMap.keys()).filter( + (key) => !key.startsWith('BIP44:') && key !== QiHDWallet.PRIVATE_KEYS_PATH, + ); } /** @@ -1268,7 +1270,6 @@ export class QiHDWallet extends AbstractHDWallet { */ public serialize(): SerializedQiHDWallet { const hdwalletSerialized = super.serialize(); - return { ...hdwalletSerialized, outpoints: this._availableOutpoints, @@ -1314,16 +1315,25 @@ export class QiHDWallet extends AbstractHDWallet { ); } }; - // validate and import all the wallet addresses + + // First, group addresses by derivation path + const addressesByPath = new Map(); for (const addressInfo of serialized.addresses) { validateQiAddressInfo(addressInfo); let key = addressInfo.derivationPath; if (isHexString(key, 32)) { key = QiHDWallet.PRIVATE_KEYS_PATH; - } else if (!key.startsWith('BIP44:')) { - wallet._addressesMap.set(key, []); } - wallet._addressesMap.get(key)!.push(addressInfo); + + if (!addressesByPath.has(key)) { + addressesByPath.set(key, []); + } + addressesByPath.get(key)!.push(addressInfo); + } + + // Then, set all paths in the wallet's address map + for (const [key, addresses] of addressesByPath) { + wallet._addressesMap.set(key, addresses); } // validate and import the counter party payment code info