Open Jogiter opened 1 year ago
Many thanks @Jogiter for opening an issue and a PR as well.
We will discuss in the team whether we will keep this and update the documentation. Or to revert back to the 1.x behavior.
Just a hint for your PR: do not call .get(0)
as it could be a previously added wallet. But maybe use [account.address]
Thanks for the hint! i am new to web3.js, As the documentation is not the latest one, i will close this and waiting for the new document
Hi @Jogiter ,
Thanks for your contribution to web3.js.
Related to this issue and your good MR. I would like to discuss regarding the wallet.add(...)
method. Currently, it is not easy for the user to get the index or the public key of an account after it has been added using the private key. This is in particular difficult to manage for the user if he tried to add the same account twice. Like in the following scenario:
So, I would like to take the input from the team. And then someone will implement it. And it would be great if it was you to add and test... My suggestion is to go with either of those:
AddAndGet
. The alternative is to modify the current add
method to have something like an output parameter, as follow:
public add(account: T | string, output_addedAccount?: { index: number; account: T }): this {
if (typeof account === 'string') {
return this.add(
this._accountProvider.privateKeyToAccount(account),
output_addedAccount,
);
}
let index = this.length;
const existAccount = this.get(account.address);
if (existAccount) {
console.warn(`Account ${account.address.toLowerCase()} already exists.`);
index = this._addressMap.get(account.address.toLowerCase()) ?? index;
}
this._addressMap.set(account.address.toLowerCase(), index);
this[index] = account;
if (output_addedAccount) {
if (!isNullish(output_addedAccount.index) || !isNullish(output_addedAccount.account)) {
throw new Error(
'The parameter `output_addedAccount` should be empty! It is an output parameter that its properties filled inside the function.',
);
}
output_addedAccount.index = index;
output_addedAccount.account = account;
}
return this;
}
Or what do you think?
After a team discussion we decided that we will add function for backward compatibility for this without breaking 4.x.
Expected behavior
the above code works well
Actual behavior
from: account.address,
got a typescript lint error: Property 'address' does not exist on type 'Walletaccording to the docs
Wallet
should be en array object. there is no propertyaddress
exist. we can use get method to get the account of the walletSteps to reproduce the behavior
Logs
Environment