Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude imported address from derivation and fix path deserialization for Qi wallet #352

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
* @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,
);
}

/**
Expand Down Expand Up @@ -1268,7 +1270,6 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
*/
public serialize(): SerializedQiHDWallet {
const hdwalletSerialized = super.serialize();

return {
...hdwalletSerialized,
outpoints: this._availableOutpoints,
Expand Down Expand Up @@ -1314,16 +1315,25 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
);
}
};
// validate and import all the wallet addresses

// First, group addresses by derivation path
const addressesByPath = new Map<string, QiAddressInfo[]>();
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
Expand Down
Loading