Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import { ChildProcessSpawner } from "effect/process";
6
7import { decryptChromiumValue } from "../preview/BrowserImport/ChromiumCookies.ts";
8import { resolveChromiumKeys } from "../preview/BrowserImport/ChromiumKeys.ts";
9import * as DesktopEnvironment from "./DesktopEnvironment.ts";
10
11export class DesktopSafeStorageIdentity extends Context.Service<
12 DesktopSafeStorageIdentity,
13 {
14 readonly decryptWithPreviousName: (
15 encrypted: Uint8Array,
16 ) => Effect.Effect<Option.Option<string>>;
17 }
18>()("@t3tools/desktop/app/DesktopSafeStorageIdentity") {}
19
20const make = Effect.gen(function* () {
21 const environment = yield* DesktopEnvironment.DesktopEnvironment;
22 const services = yield* Effect.context<ChildProcessSpawner.ChildProcessSpawner>();
23
24 const decryptWithPreviousName = Effect.fn("desktop.safeStorageIdentity.decryptWithPreviousName")(
25 function* (encrypted: Uint8Array) {
26 if (environment.platform !== "darwin" && environment.platform !== "linux") {
27 return Option.none<string>();
28 }
29 if (
30 DesktopEnvironment.resolveDesktopSafeStorageName(environment.appVersion) !==
31 "T3 Code (Alpha)"
32 ) {
33 return Option.none<string>();
34 }
35 // Cookie imports also accept plaintext; safeStorage requires an OSCrypt prefix.
36 const prefix = Buffer.from(encrypted.subarray(0, 3)).toString("latin1");
37 if (prefix !== (environment.platform === "darwin" ? "v10" : "v11")) {
38 return Option.none<string>();
39 }
40 const keys = yield* resolveChromiumKeys({
41 platform: environment.platform,
42 keychainService: "T3 Code (Beta) Safe Storage",
43 keychainAccount: "T3 Code (Beta)",
44 linuxSecretApplication: "T3 Code (Beta)",
45 }).pipe(
46 Effect.tapError((error) =>
47 Effect.logWarning("Could not read the Beta safe storage key.", {
48 reason: error.reason,
49 }),
50 ),
51 Effect.option,
52 );
53 return Option.flatMapNullishOr(keys, (material) =>
54 decryptChromiumValue(encrypted, material, "", undefined, environment.platform),
55 );
56 },
57 Effect.provide(services),
58 );
59
60 return DesktopSafeStorageIdentity.of({ decryptWithPreviousName });
61});
62
63export const layer = Layer.effect(DesktopSafeStorageIdentity, make);