Skip to content

Commit

Permalink
fix: snap and extension are out of sync (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston authored Oct 4, 2023
1 parent 44b43c6 commit 612b813
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git+https://github.com/MetaMask/snap-simple-keyring.git"
},
"source": {
"shasum": "MOUsqF3AS//7rm61xuhfO0saoBr/TvTEPLABpN7r7FI=",
"shasum": "Ch+6Kdk6BTTf7oMjvwf+DDfuRstXMqRUZqK2uJODiLk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
53 changes: 29 additions & 24 deletions packages/snap/src/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,35 @@ export class SimpleKeyring implements Keyring {
if (!isUniqueAddress(address, Object.values(this.#state.wallets))) {
throw new Error(`Account address already in use: ${address}`);
}

// The private key should not be stored in the account options since the
// account object is exposed to external components, such as MetaMask and
// the snap UI.
if (options?.privateKey) {
delete options.privateKey;
}

const account: KeyringAccount = {
id: uuid(),
options,
address,
methods: [
EthMethod.PersonalSign,
EthMethod.Sign,
EthMethod.SignTransaction,
EthMethod.SignTypedDataV1,
EthMethod.SignTypedDataV3,
EthMethod.SignTypedDataV4,
],
type: EthAccountType.Eoa,
};

this.#state.wallets[account.id] = { account, privateKey };
await this.#saveState();
await this.#emitEvent(KeyringEvent.AccountCreated, { account });

return account;
try {
const account: KeyringAccount = {
id: uuid(),
options,
address,
methods: [
EthMethod.PersonalSign,
EthMethod.Sign,
EthMethod.SignTransaction,
EthMethod.SignTypedDataV1,
EthMethod.SignTypedDataV3,
EthMethod.SignTypedDataV4,
],
type: EthAccountType.Eoa,
};
await this.#emitEvent(KeyringEvent.AccountCreated, { account });
this.#state.wallets[account.id] = { account, privateKey };
await this.#saveState();
return account;
} catch (error) {
throw new Error((error as Error).message);
}
}

async filterAccountChains(_id: string, chains: string[]): Promise<string[]> {
Expand Down Expand Up @@ -138,9 +139,13 @@ export class SimpleKeyring implements Keyring {
}

async deleteAccount(id: string): Promise<void> {
delete this.#state.wallets[id];
await this.#saveState();
await this.#emitEvent(KeyringEvent.AccountDeleted, { id });
try {
await this.#emitEvent(KeyringEvent.AccountDeleted, { id });
delete this.#state.wallets[id];
await this.#saveState();
} catch (error) {
throwError((error as Error).message);
}
}

async listRequests(): Promise<KeyringRequest[]> {
Expand Down

0 comments on commit 612b813

Please sign in to comment.