Platforms
Chrome extensions
Run the wallet SDKs in a Manifest V3 service worker with the right CSP, bundle, and storage.
Overview
In a Manifest V3 extension, run @gray3/client and @gray3/keystore in the service worker (or an extension page), and keep keys out of content scripts. Content scripts run inside web pages and should only send requests to the service worker with chrome.runtime.sendMessage.
1. Install
npm install @gray3/client @gray3/keystore sql.js2. Manifest and CSP
Allow WebAssembly (used by sql.js and by Polkadot and Bittensor crypto) and ship sql-wasm.wasm inside the extension.
{
"manifest_version": 3,
"name": "My Wallet",
"version": "1.0.0",
"background": { "service_worker": "background.js", "type": "module" },
"permissions": ["storage"],
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
},
"web_accessible_resources": []
}Copy node_modules/sql.js/dist/sql-wasm.wasm into the extension's output folder as part of your build.
3. Bundle the service worker as one file
Service workers cannot load code with dynamic import(), and the SDK lazy-loads some chain libraries. Bundle the worker into a single file so those imports are inlined:
# esbuild: no code splitting, browser conditions
npx esbuild src/background.ts --bundle --format=esm --platform=browser --outfile=dist/background.js// Vite / Rollup: inline dynamic imports for the worker entry
export default {
build: {
rollupOptions: {
input: "src/background.ts",
output: { entryFileNames: "background.js", inlineDynamicImports: true },
},
},
};4. Storage in the service worker
Service workers have no localStorage. Use the SQLite web backend (IndexedDB), or asyncKvAdapter over chrome.storage.local.
// Option A: SQLite in IndexedDB, wasm shipped with the extension
import { GrayKeystore } from "@gray3/keystore";
const storage = await GrayKeystore.create({
sqlite: { web: { name: "gray-wallets", wasmUrl: chrome.runtime.getURL("sql-wasm.wasm") } },
password: pin,
});// Option B: chrome.storage.local through asyncKvAdapter (small wallet counts)
import { GrayKeystore, asyncKvAdapter } from "@gray3/keystore";
const storage = await GrayKeystore.create({
adapter: asyncKvAdapter({
getItem: async (key) => ((await chrome.storage.local.get(key))[key] as string | undefined) ?? null,
setItem: (key, value) => chrome.storage.local.set({ [key]: value }),
removeItem: (key) => chrome.storage.local.remove(key),
getAllKeys: async () => Object.keys(await chrome.storage.local.get(null)),
}),
password: pin,
});5. Keep keys in the service worker
// background.ts
import { GrayClient } from "@gray3/client";
let client: GrayClient | null = null;
chrome.runtime.onMessage.addListener((msg, sender, reply) => {
(async () => {
if (msg.type === "unlock") {
client = await GrayClient.create({ storage: await openStorage(msg.pin) });
reply({ ok: true });
} else if (msg.type === "address" && client) {
const [meta] = await client.wallets.list();
const wallet = await client.wallets.open(meta!.id, { password: msg.pin });
reply({ address: wallet.account(0).address("ethereum") });
}
})();
return true; // keep the channel open for the async reply
});Return addresses and signatures to pages, never mnemonics or private keys. Ask the user to confirm every signature in an extension page (popup) rather than in a content script. The service worker can be stopped at any time, so treat the unlocked client as short-lived and ask for the PIN again after a restart.