Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rileystephens28 committed Nov 27, 2024
1 parent 792d568 commit e1a717e
Showing 1 changed file with 15 additions and 38 deletions.
53 changes: 15 additions & 38 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,47 +255,24 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
const derivationPath = isChange ? 'BIP44:change' : 'BIP44:external';
const addresses = this._addressesMap.get(derivationPath) || [];

// if (derivationPath === 'BIP44:external' && addresses.length === 0) {
// return {
// address: '0x00928694B9ecd4192C2BEc60f286e3AfaCCcf6A2',
// pubKey: '0x0300928694B9ecd4192C2BEc60f286e3AfaCCcf6A2',
// account,
// index: 0,
// change: isChange,
// zone,
// status: AddressStatus.UNKNOWN,
// derivationPath: 'BIP44:external',
// lastSyncedBlock: null,
// };
// }

const lastIndex = this._findLastUsedIndex(addresses, account, zone);
const addressNode = this.deriveNextAddressNode(account, lastIndex + 1, zone, isChange);
const newAddressInfo = this._createAndStoreQiAddressInfo(addressNode, account, zone, isChange);

const privateKeysArray = this._addressesMap.get(QiHDWallet.PRIVATE_KEYS_PATH) || [];
const existingPrivateKeyIndex = privateKeysArray.findIndex((info) => info.address === addressNode.address);
const existingPrivateKeyIndex = privateKeysArray.findIndex((info) => info.address === newAddressInfo.address);

if (existingPrivateKeyIndex !== -1) {
// Update the status and last synced block for the address being moved from private keys to bip44
const pkAddressToRemove = privateKeysArray[existingPrivateKeyIndex];
const updatedAddresses = addresses.map((addressInfo) => {
if (addressInfo.address === pkAddressToRemove.address) {
return {
...addressInfo,
status: pkAddressToRemove.status,
lastSyncedBlock: pkAddressToRemove.lastSyncedBlock,
};
}
return addressInfo;
});
// Update the newAddressInfo directly with the status and last synced block from the private key address
const pkAddressInfo = privateKeysArray[existingPrivateKeyIndex];
newAddressInfo.status = pkAddressInfo.status;
newAddressInfo.lastSyncedBlock = pkAddressInfo.lastSyncedBlock;

// update the private keys array
// Remove the address from the privateKeysArray
privateKeysArray.splice(existingPrivateKeyIndex, 1);
this._addressesMap.set(QiHDWallet.PRIVATE_KEYS_PATH, privateKeysArray);

// update the addresses array
this._addressesMap.set(derivationPath, updatedAddresses);
}

return newAddressInfo;
}

Expand Down Expand Up @@ -649,20 +626,20 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {

const getChangeAddressesForOutputs = async (count: number): Promise<string[]> => {
const currentChangeAddresses = this._addressesMap.get('BIP44:change') || [];
const outpusChangeAddresses: QiAddressInfo[] = [];
const outputChangeAddresses: QiAddressInfo[] = [];

for (let i = 0; i < currentChangeAddresses.length; i++) {
if (currentChangeAddresses[i].status === AddressStatus.UNUSED) {
outpusChangeAddresses.push(currentChangeAddresses[i]);
outputChangeAddresses.push(currentChangeAddresses[i]);
}

if (outpusChangeAddresses.length === count) break;
if (outputChangeAddresses.length === count) break;
}

// Generate the remaining number of change addresses if needed
const remainingAddressesNeeded = count - outpusChangeAddresses.length;
const remainingAddressesNeeded = count - outputChangeAddresses.length;
if (remainingAddressesNeeded > 0) {
outpusChangeAddresses.push(
outputChangeAddresses.push(
...Array(remainingAddressesNeeded)
.fill(0)
.map(() => this.getNextChangeAddressSync(0, originZone)),
Expand All @@ -673,7 +650,7 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
const mergedChangeAddresses = [
// Not updated last synced block because we are not certain of the success of the transaction
// so we will want to get deltas from last **checked** block
...outpusChangeAddresses.map((address) => ({
...outputChangeAddresses.map((address) => ({
...address,
status: AddressStatus.ATTEMPTED_USE,
})),
Expand All @@ -686,7 +663,7 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
// Update the _addressesMap with the modified change addresses and statuses
this._addressesMap.set('BIP44:change', sortedAndFilteredChangeAddresses);

return outpusChangeAddresses.map((address) => address.address);
return outputChangeAddresses.map((address) => address.address);
};

// 4. Get change addresses
Expand Down

0 comments on commit e1a717e

Please sign in to comment.