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

fix: create a range for get address for UTXO networks #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
59 changes: 49 additions & 10 deletions packages/sysweb3-keyring/src/keyring-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ export interface IkeyringManagerOpts {
activeChain: INetworkType;
password?: string;
}

export interface ISysAccountWithId extends ISysAccount {
id: number;
}

const ethHdPath: Readonly<string> = "m/44'/60'/0'";

export class KeyringManager implements IKeyringManager {
private storage: any; //todo type
private wallet: IWalletState; //todo change this name, we will use wallets for another const -> Maybe for defaultInitialState / defaultStartState;
Expand All @@ -84,6 +87,7 @@ export class KeyringManager implements IKeyringManager {
//transactions objects
public ethereumTransaction: IEthereumTransactions;
public syscoinTransaction: ISyscoinTransactions;

constructor(opts?: IkeyringManagerOpts | null) {
this.storage = sysweb3.sysweb3Di.getStateStorageDb();
this.trezorAccounts = [];
Expand Down Expand Up @@ -128,6 +132,7 @@ export class KeyringManager implements IKeyringManager {
this.ledgerSigner
);
}

// ===================================== AUXILIARY METHOD - FOR TRANSACTIONS CLASSES ===================================== //
private getDecryptedPrivateKey = (): {
address: string;
Expand Down Expand Up @@ -795,6 +800,7 @@ export class KeyringManager implements IKeyringManager {
},
};
};

public async importTrezorAccount(
coin: string,
slip44: string,
Expand Down Expand Up @@ -838,6 +844,7 @@ export class KeyringManager implements IKeyringManager {
throw error;
}
}

public getActiveUTXOAccountState = () => {
return {
...this.wallet.accounts.HDAccount[this.wallet.activeAccountId],
Expand Down Expand Up @@ -1260,28 +1267,59 @@ export class KeyringManager implements IKeyringManager {
};

private setLatestIndexesFromXPubTokens = (tokens: any) => {
let changeIndex = 0;
let receivingIndex = 0;
if (tokens) {
tokens.forEach((token: any) => {
let maxChangeIndex = 0;
let maxReceivingIndex = 0;

if (tokens && tokens.length > 0) {
for (let i = tokens.length - 1; i >= 0; i--) {
const token = tokens[i];
if (!token.transfers || !token.path) {
return;
continue;
}

const transfers = parseInt(token.transfers, 10);
if (token.path && transfers > 0) {
const splitPath = token.path.split('/');
if (splitPath.length >= 6) {
const change = parseInt(splitPath[4], 10);
const index = parseInt(splitPath[5], 10);
if (change === 1) {
changeIndex = index + 1;

// set the max index for change and receiving
if (change === 1 && maxChangeIndex === 0) {
maxChangeIndex = index;
} else if (change === 0 && maxReceivingIndex === 0) {
maxReceivingIndex = index;
}

if (maxChangeIndex !== 0 && maxReceivingIndex !== 0) {
// we found both indexes
break;
}
receivingIndex = index + 1;
}
}
});
}
}
return { changeIndex, receivingIndex };

// create a range
const changeRange = Array.from({ length: maxChangeIndex + 1 }, (_, i) => i);
const receivingRange = Array.from(
{ length: maxReceivingIndex + 1 },
(_, i) => i
);

const getRandomIndex = (type: 'change' | 'receiving') => {
const range = type === 'change' ? changeRange : receivingRange;
return range[Math.floor(Math.random() * range.length)];
};

// select random indexes (using the max index as the range)
const randomChangeIndex = changeRange[getRandomIndex('change')];
const randomReceivingIndex = receivingRange[getRandomIndex('receiving')];

return {
changeIndex: randomChangeIndex,
receivingIndex: randomReceivingIndex,
};
};

//todo network type
Expand Down Expand Up @@ -1820,6 +1858,7 @@ export class KeyringManager implements IKeyringManager {

return importedAccount;
}

//TODO: validate updateAllPrivateKeyAccounts updating 2 accounts or more works properly
public async updateAllPrivateKeyAccounts() {
try {
Expand Down
Loading