Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8c1e9e5
feat(wallet): Add split persistence backed by better-sqlite3
rekmarks Apr 16, 2026
0b122f7
fix(wallet): Restore @ts-expect-error for protected destroy access
rekmarks Apr 16, 2026
331ef42
chore: Bump better-sqlite3
rekmarks Apr 16, 2026
283acca
chore: lint
rekmarks Apr 16, 2026
18b57d1
docs: Fix better-sqlite3 rebuild instructions
rekmarks Apr 16, 2026
b6b0596
refactor: Tweak sqlite imports
rekmarks Apr 16, 2026
428d427
refactor: Tweak types, simplify tests
rekmarks Apr 16, 2026
c7ca356
fix: Harden persistence layer error handling and lifecycle management
rekmarks Apr 16, 2026
c6a88f5
tests: Cover persistence error paths and lifecycle edge cases
rekmarks Apr 16, 2026
62ff2e3
chore: oxfmt & eslint
rekmarks Apr 16, 2026
6df4a99
refactor: Make change and persist sets readonly
rekmarks Apr 16, 2026
cfad387
refactor: Unsubscribe in try block
rekmarks Apr 16, 2026
b36d336
refactor: Decouple persistence from Wallet constructor
rekmarks Apr 17, 2026
366b265
chore: Re-export persistence-facing types from wallet entry
rekmarks Apr 17, 2026
f4e8dfa
fix: Prevent synchronous errors from interrupting destroy procedure
rekmarks Apr 17, 2026
86a69b0
test: Add tests for destruction robustuness
rekmarks Apr 17, 2026
ce80922
fix: Rebuild better-sqlite3 if bindings are missing at test time
rekmarks Apr 17, 2026
bd9c504
fix: Only rebuild better-sqlite3 addon when missing
rekmarks Apr 17, 2026
4c16ea9
refactor: Exclude instances without metadata from metadata getter
rekmarks Apr 20, 2026
58ab0c5
refactor: Remove extraneous registerInitialEventPayload
rekmarks Apr 20, 2026
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@
"babel-runtime>core-js": false,
"simple-git-hooks": false,
"tsx>esbuild": false,
"eslint-plugin-import-x>unrs-resolver": false
"eslint-plugin-import-x>unrs-resolver": false,
"better-sqlite3": true
}
}
}
10 changes: 10 additions & 0 deletions packages/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ or

`npm install @metamask/wallet`

## Troubleshooting

### Rebuilding `better-sqlite3`

This package depends on `better-sqlite3`, which includes a native C addon. The prebuilt binary is downloaded automatically during `yarn install`. If you switch Node versions or branches and the binding is missing, rebuild it with:

```sh
cd node_modules/better-sqlite3 && npx prebuild-install
```

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
16 changes: 14 additions & 2 deletions packages/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
"default": "./dist/index.cjs"
}
},
"./persistence": {
"import": {
"types": "./dist/persistence/index.d.mts",
"default": "./dist/persistence/index.mjs"
},
"require": {
"types": "./dist/persistence/index.d.cts",
"default": "./dist/persistence/index.cjs"
}
},
"./package.json": "./package.json"
},
"publishConfig": {
Expand All @@ -47,7 +57,7 @@
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --check",
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --generate",
"since-latest-release": "../../scripts/since-latest-release.sh",
"test:prepare": "./scripts/install-anvil.sh",
"test:prepare": "./scripts/install-binaries.sh",
"test": "yarn test:prepare && NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
Expand All @@ -65,12 +75,14 @@
"@metamask/remote-feature-flag-controller": "^4.2.0",
"@metamask/scure-bip39": "^2.1.1",
"@metamask/transaction-controller": "^64.3.0",
"@metamask/utils": "^11.9.0"
"@metamask/utils": "^11.9.0",
"better-sqlite3": "^12.9.0"
},
"devDependencies": {
"@metamask/auto-changelog": "^6.1.0",
"@metamask/foundryup": "^1.0.1",
"@ts-bridge/cli": "^0.6.4",
"@types/better-sqlite3": "^7.6.13",
"@types/jest": "^29.5.14",
"deepmerge": "^4.2.2",
"jest": "^29.7.0",
Expand Down
17 changes: 0 additions & 17 deletions packages/wallet/scripts/install-anvil.sh

This file was deleted.

31 changes: 31 additions & 0 deletions packages/wallet/scripts/install-binaries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -e
set -o pipefail

# Pin cwd to the package root so all paths are predictable regardless of how
# this script is invoked. Also derive the monorepo root (two levels up).
PACKAGE_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
MONOREPO_ROOT="$(cd "${PACKAGE_ROOT}/../.." && pwd)"
cd "${PACKAGE_ROOT}"

# Run foundryup's TypeScript entry point directly via tsx. This avoids having
# to build @metamask/foundryup first, which matters in CI where workspace deps
# aren't built before tests run.
if ! output=$(yarn tsx ../foundryup/src/cli.ts --binaries anvil 2>&1); then
echo "$output" >&2
exit 1
fi

# Install the better-sqlite3 native addon if missing. Yarn has
# `enableScripts: false` globally, so install scripts never run during
# `yarn install` and the addon may be absent from the filesystem. Invoke the
# prebuild-install binary directly to fetch a matching prebuild for the active
# Node version and platform.
BETTER_SQLITE3_DIR="${MONOREPO_ROOT}/node_modules/better-sqlite3"
if [ ! -f "${BETTER_SQLITE3_DIR}/build/Release/better_sqlite3.node" ]; then
(
cd "${BETTER_SQLITE3_DIR}"
"${MONOREPO_ROOT}/node_modules/.bin/prebuild-install"
)
fi
140 changes: 112 additions & 28 deletions packages/wallet/src/Wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
DistributionType,
EnvironmentType,
} from '@metamask/remote-feature-flag-controller';
import { TransactionController } from '@metamask/transaction-controller';
import { enableNetConnect } from 'nock';

import { startAnvil } from '../test/anvil';
import type { AnvilInstance } from '../test/anvil';
import * as initializationModule from './initialization';
import {
createSecretRecoveryPhrase,
importSecretRecoveryPhrase,
Expand All @@ -22,20 +24,18 @@ const TEST_PASSWORD = 'testpass';

async function setupWallet(): Promise<Wallet> {
const wallet = new Wallet({
options: {
infuraProjectId: 'fake-infura-project-id',
clientVersion: '1.0.0',
showApprovalRequest: (): undefined => undefined,
clientConfigApiService: new ClientConfigApiService({
fetch: globalThis.fetch,
config: {
client: ClientType.Extension,
distribution: DistributionType.Main,
environment: EnvironmentType.Production,
},
}),
getMetaMetricsId: (): string => 'fake-metrics-id',
},
infuraProjectId: 'fake-infura-project-id',
clientVersion: '1.0.0',
showApprovalRequest: (): undefined => undefined,
clientConfigApiService: new ClientConfigApiService({
fetch: globalThis.fetch,
config: {
client: ClientType.Extension,
distribution: DistributionType.Main,
environment: EnvironmentType.Production,
},
}),
getMetaMetricsId: (): string => 'fake-metrics-id',
});

await importSecretRecoveryPhrase(wallet, TEST_PASSWORD, TEST_PHRASE);
Expand Down Expand Up @@ -134,20 +134,18 @@ describe('Wallet', () => {

it('can create secret recovery phrase', async () => {
wallet = new Wallet({
options: {
infuraProjectId: 'fake-infura-project-id',
clientVersion: '1.0.0',
showApprovalRequest: (): undefined => undefined,
clientConfigApiService: new ClientConfigApiService({
fetch: globalThis.fetch,
config: {
client: ClientType.Extension,
distribution: DistributionType.Main,
environment: EnvironmentType.Production,
},
}),
getMetaMetricsId: (): string => 'fake-metrics-id',
},
infuraProjectId: 'fake-infura-project-id',
clientVersion: '1.0.0',
showApprovalRequest: (): undefined => undefined,
clientConfigApiService: new ClientConfigApiService({
fetch: globalThis.fetch,
config: {
client: ClientType.Extension,
distribution: DistributionType.Main,
environment: EnvironmentType.Production,
},
}),
getMetaMetricsId: (): string => 'fake-metrics-id',
});

await createSecretRecoveryPhrase(wallet, TEST_PASSWORD);
Expand All @@ -169,4 +167,90 @@ describe('Wallet', () => {
vault: expect.any(String),
});
});

describe('lifecycle', () => {
const options = {
infuraProjectId: 'fake-infura-project-id',
clientVersion: '1.0.0',
showApprovalRequest: (): undefined => undefined,
clientConfigApiService: new ClientConfigApiService({
fetch: globalThis.fetch,
config: {
client: ClientType.Extension,
distribution: DistributionType.Main,
environment: EnvironmentType.Production,
},
}),
getMetaMetricsId: (): string => 'fake-metrics-id',
};

it('exposes controllerMetadata for each initialized controller', () => {
wallet = new Wallet(options);

const names = Object.keys(wallet.controllerMetadata);
expect(names).toStrictEqual(Object.keys(wallet.state));
for (const name of names) {
expect(wallet.controllerMetadata[name]).toBeDefined();
}
});

it('omits instances without a metadata property from controllerMetadata', () => {
const fakeMetadata = { foo: { persist: true, anonymous: false } };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const fakeMetadata = { foo: { persist: true, anonymous: false } };
const fakeMetadata = { foo: { persist: true, includeInDebugSnapshot: false } };

jest.spyOn(initializationModule, 'initialize').mockReturnValueOnce({
WithMeta: { state: {}, metadata: fakeMetadata },
NoMeta: { state: {} },
} as never);

wallet = new Wallet(options);

expect(wallet.controllerMetadata).toStrictEqual({
WithMeta: fakeMetadata,
});
expect(Object.keys(wallet.state)).toStrictEqual(['WithMeta', 'NoMeta']);
});

it('publishes Root:walletDestroyed exactly once on destroy', async () => {
wallet = new Wallet(options);

const listener = jest.fn();
wallet.messenger.subscribe('Root:walletDestroyed', listener);

await wallet.destroy();
await wallet.destroy();

expect(listener).toHaveBeenCalledTimes(1);
});

it('publishes Root:walletDestroyed even if a controller destroy throws synchronously', async () => {
wallet = new Wallet(options);

jest
.spyOn(TransactionController.prototype, 'destroy')
.mockImplementation(() => {
throw new Error('sync destroy error');
});

const listener = jest.fn();
wallet.messenger.subscribe('Root:walletDestroyed', listener);

await wallet.destroy();

expect(listener).toHaveBeenCalledTimes(1);
});

it('publishes Root:walletDestroyed even if a controller destroy rejects', async () => {
wallet = new Wallet(options);

jest
.spyOn(TransactionController.prototype, 'destroy')
.mockRejectedValue(new Error('async destroy error') as never);

const listener = jest.fn();
wallet.messenger.subscribe('Root:walletDestroyed', listener);

await wallet.destroy();

expect(listener).toHaveBeenCalledTimes(1);
});
});
});
47 changes: 36 additions & 11 deletions packages/wallet/src/Wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { StateMetadataConstraint } from '@metamask/base-controller';
import { Messenger } from '@metamask/messenger';
import type { Json } from '@metamask/utils';
import { hasProperty } from '@metamask/utils';

import type {
DefaultActions,
Expand All @@ -11,23 +12,34 @@ import type {
import { initialize } from './initialization';
import type { WalletOptions } from './types';

export type WalletConstructorArgs = {
state?: Record<string, Json>;
options: WalletOptions;
};

export class Wallet {
// TODO: Expand types when passing additionalConfigurations.
public readonly messenger: RootMessenger<DefaultActions, DefaultEvents>;

readonly #instances: DefaultInstances;

constructor({ state = {}, options }: WalletConstructorArgs) {
readonly #controllerMetadata: Readonly<
Record<string, Readonly<StateMetadataConstraint>>
>;

#destroyed = false;

constructor({ state, ...options }: WalletOptions) {
this.messenger = new Messenger({
namespace: 'Root',
});

this.#instances = initialize({ state, messenger: this.messenger, options });
this.#instances = initialize({
state: state ?? {},
messenger: this.messenger,
options,
});

this.#controllerMetadata = Object.fromEntries(
Object.entries(this.#instances)
.filter(([_, instance]) => hasProperty(instance, 'metadata'))
.map(([name, instance]) => [name, instance.metadata]),
);
}

get state(): DefaultState {
Expand All @@ -40,17 +52,30 @@ export class Wallet {
) as DefaultState;
}

get controllerMetadata(): Readonly<
Record<string, Readonly<StateMetadataConstraint>>
> {
return this.#controllerMetadata;
}

async destroy(): Promise<void> {
await Promise.all(
if (this.#destroyed) {
return;
}
this.#destroyed = true;

await Promise.allSettled(
Object.values(this.#instances).map((instance) => {
// @ts-expect-error Accessing protected property.
if (typeof instance.destroy === 'function') {
// @ts-expect-error Accessing protected property.
return instance.destroy();
return (async (): Promise<void> => await instance.destroy())();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe simpler to make the map function async?

}
/* istanbul ignore next */
return undefined;
return Promise.resolve();
}),
);

this.messenger.publish('Root:walletDestroyed');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should reconsider the root messenger namespace, perhaps it should be Wallet: instead?

}
}
7 changes: 7 additions & 0 deletions packages/wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export { Wallet } from './Wallet';
export type { WalletOptions } from './types';
export type {
DefaultActions,
DefaultEvents,
RootMessenger,
WalletDestroyedEvent,
} from './initialization';
9 changes: 8 additions & 1 deletion packages/wallet/src/initialization/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ export type DefaultInstances = {

export type DefaultActions = MessengerActions<AllMessengers>;

export type DefaultEvents = MessengerEvents<AllMessengers>;
export type WalletDestroyedEvent = {
type: 'Root:walletDestroyed';
payload: [];
};

export type DefaultEvents =
| MessengerEvents<AllMessengers>
| WalletDestroyedEvent;

export type RootMessenger<
AllowedActions extends ActionConstraint = ActionConstraint,
Expand Down
Loading
Loading