Skip to content

Commit

Permalink
feat: move SessionKVStore into extension, so we can reuse other stora…
Browse files Browse the repository at this point in the history
…ge types in Namadillo
  • Loading branch information
jurevans committed Sep 18, 2024
1 parent 3832b37 commit 5940fbf
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
ExtensionKVStore,
IndexedDBKVStore,
MemoryKVStore,
SessionKVStore,
} from "@namada/storage";
import { SessionKVStore } from "extension";
import browser from "webextension-polyfill";

// Needed to allow the background script to send messages containing bigints
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { KVStore } from "./types";
import { KVStore } from "@namada/storage";
import browser from "webextension-polyfill";

export class SessionKVStore<T> implements KVStore<T[]> {
constructor(private readonly _prefix: string) {}
constructor(private readonly _prefix: string) { }

public async get<T = unknown>(key: string): Promise<T | undefined> {
const obj = await browser.storage.session.get(this.prefix());
Expand Down
3 changes: 2 additions & 1 deletion apps/extension/src/extension/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from "./ExtensionBroadcaster";
export * from "./ExtensionRequester";
export * from "./ExtensionMessenger";
export * from "./ExtensionMessengerMock";
export * from "./ExtensionRequester";
export * from "./ExtensionRouter";
export * from "./guards";
export * from "./SessionKVStore";
export * from "./utils";
13 changes: 11 additions & 2 deletions apps/namadillo/src/atoms/masp/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// TODO
import { atomWithAsyncStorage } from "atoms/utils";
import { MaspParam, MaspParamStorage } from "types";

// Masp Params Atom
export const MASP_PARAMS_KEY = "masp-params";
export const maspParamAtom = atomWithAsyncStorage<MaspParamStorage>(
MASP_PARAMS_KEY,
{
[MaspParam.Output]: null,
[MaspParam.Spend]: null,
[MaspParam.Convert]: null,
}
);
6 changes: 5 additions & 1 deletion apps/namadillo/src/atoms/masp/functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { toBase64 } from "@cosmjs/encoding";
import { IndexedDBKVStore } from "@namada/storage";
import { MaspParam } from "types";
import { sha256Hash } from "utils";
import { MASP_PARAMS_URL, MaspParam, MaspParamConfigs } from "./types";
import { MASP_PARAMS_URL, MaspParamConfigs } from "./types";

export type MaspParamBytes = {
param: MaspParam;
Expand All @@ -20,6 +21,9 @@ export type MaspParamBytesComplete = {
progress: number;
};

/**
* Fetch a MASP Param, and stream the bytes, resolving to Uint8Array if successful
*/
export const fetchMaspParam = async (
param: MaspParam,
// Optional callback to track download progress
Expand Down
10 changes: 2 additions & 8 deletions apps/namadillo/src/atoms/masp/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MaspParam } from "types";

const { NAMADA_INTERFACE_PROXY: isProxy } = process.env;

const MASP_MPC_URL =
Expand All @@ -6,14 +8,6 @@ const MASP_MPC_URL =
export const MASP_PARAMS_URL =
isProxy === "true" ? "http://localhost:8010/proxy" : MASP_MPC_URL;

export const STORAGE_PREFIX = "namadillo";

export enum MaspParam {
Output = "masp-output.params",
Convert = "masp-convert.params",
Spend = "masp-spend.params",
}

export const MaspParamConfigs: Record<
MaspParam,
{ length: number; sha256sum: string }
Expand Down
23 changes: 23 additions & 0 deletions apps/namadillo/src/atoms/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { IndexedDBKVStore } from "@namada/storage";
import {
dismissToastNotificationAtom,
dispatchToastNotificationAtom,
} from "atoms/notifications";
import { useSetAtom } from "jotai";
import { AtomWithQueryResult } from "jotai-tanstack-query";
import { atomWithStorage } from "jotai/utils";
import { useEffect } from "react";
import { ToastNotification } from "types";

Expand Down Expand Up @@ -92,3 +94,24 @@ export const useNotifyOnAtomError = (
}
}, deps);
};

// Atom using IndexedDBKVStore
export function atomWithAsyncStorage<T>(key: string, initial: T): unknown {
const DB_PREFIX = "namadillo";
const store = new IndexedDBKVStore(DB_PREFIX);

return atomWithStorage<T>(key, initial, {
setItem: (key, newValue) => store.set(key, newValue),
getItem: (key) =>
store.get<T>(key).then((value) => {
if (value !== undefined) {
return value;
}
if (initial !== undefined) {
store.set(key, initial);
}
return initial;
}),
removeItem: (key) => store.set(key, null),
});
}
8 changes: 8 additions & 0 deletions apps/namadillo/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,11 @@ export type ToastNotification = {
export type ToastNotificationEntryFilter = (
notification: ToastNotification
) => boolean;

export enum MaspParam {
Output = "masp-output.params",
Convert = "masp-convert.params",
Spend = "masp-spend.params",
}

export type MaspParamStorage = Record<MaspParam, string | null>;
4 changes: 1 addition & 3 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"lint:ci": "yarn lint --max-warnings 0"
},
"devDependencies": {
"@types/firefox-webext-browser": "^94.0.1",
"@types/jest": "^29.5.12",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -27,8 +26,7 @@
"eslint-plugin-react": "^7.35.2",
"eslint-plugin-react-hooks": "^4.6.0",
"jest": "^29.7.0",
"typescript": "^5.5.4",
"webextension-polyfill": "^0.10.0"
"typescript": "^5.5.4"
},
"files": [
"dist"
Expand Down
1 change: 0 additions & 1 deletion packages/storage/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from "./ExtensionKVStore";
export * from "./IndexedDBKVStore";
export * from "./LocalKVStore";
export * from "./SessionKVStore";
export * from "./MemoryKVStore";
export * from "./Store";
export * from "./types";
2 changes: 0 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3745,7 +3745,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@namada/storage@workspace:packages/storage"
dependencies:
"@types/firefox-webext-browser": "npm:^94.0.1"
"@types/jest": "npm:^29.5.12"
eslint: "npm:^8.57.0"
eslint-config-prettier: "npm:^9.1.0"
Expand All @@ -3755,7 +3754,6 @@ __metadata:
eslint-plugin-react-hooks: "npm:^4.6.0"
jest: "npm:^29.7.0"
typescript: "npm:^5.5.4"
webextension-polyfill: "npm:^0.10.0"
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 5940fbf

Please sign in to comment.