Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix: include impersonated accounts in eth_accounts #1990

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion src/chains/ethereum/ethereum/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,18 @@ export default class Wallet {
} else {
// this wasn't one of our initial accounts, so make a priv key.
privateKey = this.createFakePrivateKey(address);

// add the account to the list of addresses because we want
// `eth_accounts` to return this account.
this.addresses.push(address);
}
break;
} else {
// try to convert the arg string to a number.
// don't use parseInt because strings like `"123abc"` parse
// to `123`, and there is probably an error on the user's side we'd
// want to uncover.
const index = ((arg as any) as number) - 0;
const index = (arg as any as number) - 0;
// if we don't have a valid number, or the number isn't a valid JS
// integer (no bigints or decimals, please), throw an error.
if (!Number.isSafeInteger(index)) {
Expand Down Expand Up @@ -591,6 +595,7 @@ export default class Wallet {
const privateKey = this.createFakePrivateKey(lowerAddress);
await this.addToKeyFile(address, privateKey, passphrase, true);
this.knownAccounts.add(lowerAddress);
this.addresses.push(lowerAddress);
return true;
}

Expand All @@ -608,6 +613,7 @@ export default class Wallet {
if (privateKey != null) {
this.keyFiles.delete(lowerAddress);
this.knownAccounts.delete(lowerAddress);
this.addresses.splice(this.addresses.indexOf(lowerAddress), 1);
this.lockTimers.delete(lowerAddress);
this.unlockedAccounts.delete(lowerAddress);
return true;
Expand Down
17 changes: 17 additions & 0 deletions src/chains/ethereum/ethereum/tests/api/eth/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ describe("api", () => {
assert.strictEqual(opts.wallet.accounts.length, 0);
assert.strictEqual(initialAccounts.length, 0);
});

it("should unlock arbitrary accounts and include them in eth_accounts", async () => {
const arbitraryAccount = "0xab5801a7d398351b8be11c439e05c5b3259aec9b";
const totalAccounts = 50;
const provider = await getProvider({
wallet: {
deterministic: true,
totalAccounts: totalAccounts,
unlockedAccounts: [arbitraryAccount]
}
});
const accounts = await provider.send("eth_accounts");
assert.strictEqual(accounts.length, totalAccounts + 1);
assert(accounts.includes(arbitraryAccount));
const opts = provider.getOptions();
assert.strictEqual(totalAccounts, opts.wallet.totalAccounts);
});
});
});
});
8 changes: 8 additions & 0 deletions src/chains/ethereum/ethereum/tests/api/evm/evm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ describe("api", () => {
message: "authentication needed: passphrase or unlock"
});

// account is included in eth_accounts
assert((await provider.send("eth_accounts", [])).includes(address));

// account is included in personal_listAccounts
assert(
(await provider.send("personal_listAccounts", [])).includes(address)
);

// we're added to the personal namespace so we can unlock
const unlocked = await provider.send("personal_unlockAccount", [
address,
Expand Down