@abraca/nuxt 2.37.0 → 2.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/dist/runtime/components/aware/AMedia.d.vue.ts +1 -1
- package/dist/runtime/components/aware/AMedia.vue.d.ts +1 -1
- package/dist/runtime/composables/useAbracadabraAuth.d.ts +1 -0
- package/dist/runtime/composables/useAbracadabraAuth.js +1 -0
- package/dist/runtime/composables/useWorkspace.d.ts +57 -0
- package/dist/runtime/composables/useWorkspace.js +58 -0
- package/dist/runtime/middleware/abracadabra-auth.js +11 -1
- package/dist/runtime/plugin-abracadabra.client.js +121 -12
- package/dist/runtime/utils/softKeystore.d.ts +55 -0
- package/dist/runtime/utils/softKeystore.js +160 -0
- package/package.json +1 -1
package/dist/module.json
CHANGED
|
@@ -12,8 +12,8 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {
|
|
|
12
12
|
awareness: boolean;
|
|
13
13
|
tag: "video" | "audio";
|
|
14
14
|
live: boolean;
|
|
15
|
-
total: boolean;
|
|
16
15
|
controls: boolean;
|
|
16
|
+
total: boolean;
|
|
17
17
|
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
18
18
|
declare const _default: typeof __VLS_export;
|
|
19
19
|
export default _default;
|
|
@@ -12,8 +12,8 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {
|
|
|
12
12
|
awareness: boolean;
|
|
13
13
|
tag: "video" | "audio";
|
|
14
14
|
live: boolean;
|
|
15
|
-
total: boolean;
|
|
16
15
|
controls: boolean;
|
|
16
|
+
total: boolean;
|
|
17
17
|
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
18
18
|
declare const _default: typeof __VLS_export;
|
|
19
19
|
export default _default;
|
|
@@ -17,6 +17,7 @@ export function useAbracadabraAuth() {
|
|
|
17
17
|
loginWithHardware: abra.loginWithHardware,
|
|
18
18
|
loginWithPassword: abra.loginWithPassword,
|
|
19
19
|
registerWithPassword: abra.registerWithPassword,
|
|
20
|
+
loginWithMnemonic: abra.loginWithMnemonic,
|
|
20
21
|
logout: abra.logout,
|
|
21
22
|
logoutServer: abra.logoutServer,
|
|
22
23
|
logoutAll: abra.logoutAll,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useWorkspace — low-level glue for navigating ONE doc-tree as an app workspace.
|
|
3
|
+
*
|
|
4
|
+
* Every Abracadabra app reimplements the same handful of doc-tree operations:
|
|
5
|
+
* scope the visible tree to a container, walk a breadcrumb up to it, flip a
|
|
6
|
+
* container's page type in place (the polymorphic-tree lens), and create a new
|
|
7
|
+
* child doc. This composable is that shared core — apps compose domain helpers
|
|
8
|
+
* beside it instead of copy-pasting the plumbing.
|
|
9
|
+
*
|
|
10
|
+
* Three ideas, all over the root doc's "doc-tree" Y.Map:
|
|
11
|
+
* 1. ROOT SCOPING — pass the container id your app lives under; breadcrumb +
|
|
12
|
+
* newDocument scope to it. Omit it (or pass null/'') to operate on the
|
|
13
|
+
* whole space tree.
|
|
14
|
+
* 2. THE LENS FLIP — setType() rewrites a container's `type`; <ADocRenderer>
|
|
15
|
+
* re-renders the SAME children under the new page type, zero data movement.
|
|
16
|
+
* 3. PLUMBING — entry/docType/childCount/isContainer/breadcrumb that every
|
|
17
|
+
* route, navbar, and command palette needs.
|
|
18
|
+
*
|
|
19
|
+
* Rendering, node panels, and awareness are handled by <ADocRenderer> + the
|
|
20
|
+
* renderers; this is purely the tree-navigation layer.
|
|
21
|
+
*
|
|
22
|
+
* @param root - The workspace container id (string, ref, or getter). Empty/null
|
|
23
|
+
* → the whole tree (no scoping).
|
|
24
|
+
*/
|
|
25
|
+
import { type ComputedRef, type MaybeRefOrGetter } from 'vue';
|
|
26
|
+
export interface WorkspaceEntry {
|
|
27
|
+
label?: string;
|
|
28
|
+
parentId?: string | null;
|
|
29
|
+
order?: number;
|
|
30
|
+
type?: string;
|
|
31
|
+
meta?: Record<string, any>;
|
|
32
|
+
[k: string]: any;
|
|
33
|
+
}
|
|
34
|
+
export interface WorkspaceCrumb {
|
|
35
|
+
id: string;
|
|
36
|
+
label: string;
|
|
37
|
+
icon?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface UseWorkspace {
|
|
40
|
+
/** The scoped container id (empty string = unscoped / whole tree). */
|
|
41
|
+
rootDocId: ComputedRef<string>;
|
|
42
|
+
/** Raw doc-tree entry for an id. */
|
|
43
|
+
entry: (id: string) => WorkspaceEntry | undefined;
|
|
44
|
+
/** A doc's page type (defaults to "doc"). */
|
|
45
|
+
docType: (id: string) => string;
|
|
46
|
+
/** Number of direct children of a doc. */
|
|
47
|
+
childCount: (id: string) => number;
|
|
48
|
+
/** True when the doc owns children (a container — shows the lens switcher). */
|
|
49
|
+
isContainer: (id: string) => boolean;
|
|
50
|
+
/** Trail from just-below the root down to docId, EXCLUDING the root itself. */
|
|
51
|
+
breadcrumb: (docId: string) => WorkspaceCrumb[];
|
|
52
|
+
/** Flip a container's page type in place (the lens flip). */
|
|
53
|
+
setType: (id: string, type: string) => void;
|
|
54
|
+
/** Create a new child under the root (or top-level when unscoped). */
|
|
55
|
+
newDocument: (label?: string, type?: string, meta?: Record<string, any>) => string | null;
|
|
56
|
+
}
|
|
57
|
+
export declare function useWorkspace(root?: MaybeRefOrGetter<string | null | undefined>): UseWorkspace;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { computed, unref } from "vue";
|
|
2
|
+
import { useAbracadabra } from "./useAbracadabra.js";
|
|
3
|
+
import { useSyncedMap } from "./useYDoc.js";
|
|
4
|
+
export function useWorkspace(root) {
|
|
5
|
+
const abra = useAbracadabra();
|
|
6
|
+
const treeMap = useSyncedMap(abra.doc, "doc-tree");
|
|
7
|
+
const rootDocId = computed(() => {
|
|
8
|
+
const r = typeof root === "function" ? root() : unref(root);
|
|
9
|
+
return r || "";
|
|
10
|
+
});
|
|
11
|
+
function entry(id) {
|
|
12
|
+
return id ? treeMap.data[id] : void 0;
|
|
13
|
+
}
|
|
14
|
+
function docType(id) {
|
|
15
|
+
return entry(id)?.type ?? "doc";
|
|
16
|
+
}
|
|
17
|
+
function childCount(id) {
|
|
18
|
+
const data = treeMap.data;
|
|
19
|
+
let n = 0;
|
|
20
|
+
for (const k in data) if (data[k]?.parentId === id) n++;
|
|
21
|
+
return n;
|
|
22
|
+
}
|
|
23
|
+
function isContainer(id) {
|
|
24
|
+
return childCount(id) > 0;
|
|
25
|
+
}
|
|
26
|
+
function breadcrumb(docId) {
|
|
27
|
+
const out = [];
|
|
28
|
+
let id = docId;
|
|
29
|
+
let guard = 0;
|
|
30
|
+
while (id && id !== rootDocId.value && guard++ < 100) {
|
|
31
|
+
const e = entry(id);
|
|
32
|
+
if (!e) break;
|
|
33
|
+
out.unshift({ id, label: e.label || "Untitled", icon: e.meta?.icon });
|
|
34
|
+
id = e.parentId ?? "";
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
function setType(id, type) {
|
|
39
|
+
const e = entry(id);
|
|
40
|
+
if (!e || e.type === type) return;
|
|
41
|
+
treeMap.set(id, { ...e, type });
|
|
42
|
+
}
|
|
43
|
+
function newDocument(label = "Untitled", type = "doc", meta) {
|
|
44
|
+
const doc = abra.doc.value;
|
|
45
|
+
if (!doc || abra.isReady && !abra.isReady.value) return null;
|
|
46
|
+
const id = crypto.randomUUID();
|
|
47
|
+
const e = {
|
|
48
|
+
label,
|
|
49
|
+
parentId: rootDocId.value || null,
|
|
50
|
+
order: Date.now(),
|
|
51
|
+
type
|
|
52
|
+
};
|
|
53
|
+
if (meta) e.meta = meta;
|
|
54
|
+
doc.getMap("doc-tree").set(id, e);
|
|
55
|
+
return id;
|
|
56
|
+
}
|
|
57
|
+
return { rootDocId, entry, docType, childCount, isContainer, breadcrumb, setType, newDocument };
|
|
58
|
+
}
|
|
@@ -4,8 +4,18 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
|
|
4
4
|
if (import.meta.server) return;
|
|
5
5
|
const abra = useAbracadabra();
|
|
6
6
|
const config = useRuntimeConfig();
|
|
7
|
-
const
|
|
7
|
+
const authCfg = config.public?.abracadabra?.auth ?? {};
|
|
8
|
+
const loginPath = authCfg.loginPath ?? "/login";
|
|
9
|
+
const requireClaimed = authCfg.requireClaimed === true;
|
|
10
|
+
if (to.path === loginPath) return;
|
|
8
11
|
await abra.whenReady?.();
|
|
12
|
+
if (requireClaimed) {
|
|
13
|
+
const state = abra.identityState?.value;
|
|
14
|
+
if (state !== "claimed" && state !== "account") {
|
|
15
|
+
return navigateTo({ path: loginPath, query: { redirect: to.fullPath } });
|
|
16
|
+
}
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
9
19
|
if (!abra.publicKeyB64?.value) {
|
|
10
20
|
return navigateTo({ path: loginPath, query: { redirect: to.fullPath } });
|
|
11
21
|
}
|
|
@@ -48,7 +48,7 @@ import {
|
|
|
48
48
|
_initServerInfo,
|
|
49
49
|
_destroyServerInfo
|
|
50
50
|
} from "./composables/useServerInfo.js";
|
|
51
|
-
import { setSdkModule } from "./utils/sdkRef.js";
|
|
51
|
+
import { setSdkModule, getSdkModule } from "./utils/sdkRef.js";
|
|
52
52
|
const NUXT_UI_PRIMARY_COLORS = /* @__PURE__ */ new Set([
|
|
53
53
|
"red",
|
|
54
54
|
"orange",
|
|
@@ -375,6 +375,16 @@ export default defineNuxtPlugin({
|
|
|
375
375
|
let _wsp = null;
|
|
376
376
|
let _tokenManager = null;
|
|
377
377
|
let _signChallenge = null;
|
|
378
|
+
let _ephemeralSeed = null;
|
|
379
|
+
function _wipeEphemeralSeed() {
|
|
380
|
+
if (_ephemeralSeed) {
|
|
381
|
+
_ephemeralSeed.fill(0);
|
|
382
|
+
_ephemeralSeed = null;
|
|
383
|
+
}
|
|
384
|
+
if (typeof window !== "undefined") {
|
|
385
|
+
window.removeEventListener("beforeunload", _wipeEphemeralSeed);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
378
388
|
let authFailureCount = 0;
|
|
379
389
|
const AUTH_FAILURE_LIMIT = 3;
|
|
380
390
|
let lastClientIds = /* @__PURE__ */ new Set();
|
|
@@ -747,6 +757,12 @@ export default defineNuxtPlugin({
|
|
|
747
757
|
_tokenManager?.clearSession();
|
|
748
758
|
if (client.value) client.value.logout();
|
|
749
759
|
if (keystore.value) await keystore.value.clear();
|
|
760
|
+
_wipeEphemeralSeed();
|
|
761
|
+
try {
|
|
762
|
+
const { clearSoftKey } = await import("./utils/softKeystore.js");
|
|
763
|
+
await clearSoftKey();
|
|
764
|
+
} catch {
|
|
765
|
+
}
|
|
750
766
|
localStorage.removeItem("abracadabra_privkey");
|
|
751
767
|
localStorage.removeItem("abracadabra_usercolor");
|
|
752
768
|
localStorage.removeItem("abracadabra_neutralcolor");
|
|
@@ -824,6 +840,50 @@ export default defineNuxtPlugin({
|
|
|
824
840
|
setTimeout(() => _wsp?.connect(), 300);
|
|
825
841
|
}
|
|
826
842
|
}
|
|
843
|
+
async function loginWithMnemonic(opts) {
|
|
844
|
+
const sdk = getSdkModule();
|
|
845
|
+
if (!sdk) throw new Error("SDK not initialized");
|
|
846
|
+
const { mnemonicToKeyPair, validateMnemonic } = sdk;
|
|
847
|
+
if (!validateMnemonic(opts.mnemonic)) throw new Error("Invalid recovery phrase");
|
|
848
|
+
const kp = await mnemonicToKeyPair(opts.mnemonic, opts.passphrase);
|
|
849
|
+
const pubKey = kp.publicKeyB64;
|
|
850
|
+
if (opts.ephemeral) {
|
|
851
|
+
_wipeEphemeralSeed();
|
|
852
|
+
_ephemeralSeed = kp.seed;
|
|
853
|
+
localStorage.removeItem("abracadabra_privkey");
|
|
854
|
+
if (typeof window !== "undefined") {
|
|
855
|
+
window.addEventListener("beforeunload", _wipeEphemeralSeed);
|
|
856
|
+
}
|
|
857
|
+
publicKeyB64.value = pubKey;
|
|
858
|
+
identityState.value = "claimed";
|
|
859
|
+
addLog("Ephemeral recovery-phrase session", "auth");
|
|
860
|
+
} else {
|
|
861
|
+
_wipeEphemeralSeed();
|
|
862
|
+
try {
|
|
863
|
+
const { softKeystoreAvailable, importSoftKeyFromSeed } = await import("./utils/softKeystore.js");
|
|
864
|
+
if (softKeystoreAvailable()) {
|
|
865
|
+
await importSoftKeyFromSeed(kp.seed);
|
|
866
|
+
} else {
|
|
867
|
+
localStorage.setItem("abracadabra_privkey", toBase64Url(kp.seed));
|
|
868
|
+
}
|
|
869
|
+
} finally {
|
|
870
|
+
kp.seed.fill(0);
|
|
871
|
+
}
|
|
872
|
+
localStorage.setItem(CLAIMED_FLAG_KEY, "1");
|
|
873
|
+
localStorage.setItem(PUBKEY_KEY, pubKey);
|
|
874
|
+
localStorage.removeItem(REQUIRE_PASSKEY_KEY);
|
|
875
|
+
publicKeyB64.value = pubKey;
|
|
876
|
+
identityState.value = "claimed";
|
|
877
|
+
addLog("Authenticated via recovery phrase", "auth");
|
|
878
|
+
}
|
|
879
|
+
const serverUrl = currentServerUrl.value;
|
|
880
|
+
if (client.value || serverUrl) {
|
|
881
|
+
_teardown();
|
|
882
|
+
_initPromise = null;
|
|
883
|
+
_initPromise = init(serverUrl);
|
|
884
|
+
await _initPromise;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
827
887
|
async function confirmPasswordReset(opts) {
|
|
828
888
|
if (!client.value) throw new Error("Not connected");
|
|
829
889
|
await client.value.confirmPasswordReset(opts);
|
|
@@ -947,28 +1007,64 @@ export default defineNuxtPlugin({
|
|
|
947
1007
|
const wasPreviouslyClaimed = localStorage.getItem(CLAIMED_FLAG_KEY) === "1";
|
|
948
1008
|
const storedPubKey = localStorage.getItem(PUBKEY_KEY);
|
|
949
1009
|
requirePasskeyOnLogin.value = localStorage.getItem(REQUIRE_PASSKEY_KEY) === "1";
|
|
950
|
-
|
|
1010
|
+
const { softKeystoreAvailable, getOrCreateSoftKey, getSoftKeyPublicKey, importSoftKeyFromSeed, signWithSoftKey } = await import("./utils/softKeystore.js");
|
|
1011
|
+
if (_ephemeralSeed) {
|
|
1012
|
+
pubKey = toBase64Url(ed.getPublicKey(_ephemeralSeed));
|
|
1013
|
+
identityState.value = "claimed";
|
|
1014
|
+
addLog("Ephemeral recovery-phrase identity", "auth");
|
|
1015
|
+
} else if (wasPreviouslyClaimed && storedPubKey) {
|
|
951
1016
|
pubKey = storedPubKey;
|
|
952
1017
|
identityState.value = "claimed";
|
|
953
|
-
|
|
1018
|
+
if (softKeystoreAvailable()) {
|
|
1019
|
+
const legacySeed = localStorage.getItem("abracadabra_privkey");
|
|
1020
|
+
if (legacySeed) {
|
|
1021
|
+
try {
|
|
1022
|
+
const seedBytes = fromBase64Url(legacySeed);
|
|
1023
|
+
await importSoftKeyFromSeed(seedBytes);
|
|
1024
|
+
seedBytes.fill(0);
|
|
1025
|
+
localStorage.removeItem("abracadabra_privkey");
|
|
1026
|
+
addLog("Migrated recovered key identity to encrypted soft keystore", "auth");
|
|
1027
|
+
} catch {
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
if (await getSoftKeyPublicKey() === pubKey) {
|
|
1031
|
+
addLog("Restored recovered key identity (soft seed)", "auth");
|
|
1032
|
+
} else {
|
|
1033
|
+
addLog("Restored claimed identity from cache", "auth");
|
|
1034
|
+
}
|
|
1035
|
+
} else {
|
|
1036
|
+
const recoveredSeed = localStorage.getItem("abracadabra_privkey");
|
|
1037
|
+
if (recoveredSeed) {
|
|
1038
|
+
privKey = fromBase64Url(recoveredSeed);
|
|
1039
|
+
addLog("Restored recovered key identity (soft seed)", "auth");
|
|
1040
|
+
} else {
|
|
1041
|
+
addLog("Restored claimed identity from cache", "auth");
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
954
1044
|
} else if (wasPreviouslyClaimed && !storedPubKey) {
|
|
955
1045
|
identityState.value = "identityLost";
|
|
956
1046
|
addLog("Previously claimed account \u2014 cached key missing, waiting for recovery", "auth");
|
|
957
1047
|
publicKeyB64.value = "";
|
|
958
1048
|
return;
|
|
959
1049
|
} else {
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
1050
|
+
if (softKeystoreAvailable()) {
|
|
1051
|
+
const softKey = await getOrCreateSoftKey();
|
|
1052
|
+
pubKey = softKey.publicKey;
|
|
1053
|
+
softKey.privateKey.fill(0);
|
|
1054
|
+
addLog("Using guest identity (soft key, IDB)", "auth");
|
|
964
1055
|
} else {
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
1056
|
+
const storedPrivKey = localStorage.getItem("abracadabra_privkey");
|
|
1057
|
+
if (storedPrivKey) {
|
|
1058
|
+
privKey = fromBase64Url(storedPrivKey);
|
|
1059
|
+
addLog("Using guest identity (soft key)", "auth");
|
|
1060
|
+
} else {
|
|
1061
|
+
privKey = ed.utils.randomSecretKey();
|
|
1062
|
+
localStorage.setItem("abracadabra_privkey", toBase64Url(privKey));
|
|
1063
|
+
addLog("Created new guest identity (soft key)", "auth");
|
|
1064
|
+
}
|
|
1065
|
+
pubKey = toBase64Url(ed.getPublicKey(privKey));
|
|
968
1066
|
}
|
|
969
1067
|
identityState.value = "guest";
|
|
970
|
-
const pubKeyBytes = ed.getPublicKey(privKey);
|
|
971
|
-
pubKey = toBase64Url(pubKeyBytes);
|
|
972
1068
|
}
|
|
973
1069
|
publicKeyB64.value = pubKey;
|
|
974
1070
|
const storedColor = localStorage.getItem("abracadabra_usercolor");
|
|
@@ -1011,7 +1107,19 @@ export default defineNuxtPlugin({
|
|
|
1011
1107
|
}
|
|
1012
1108
|
addLog(`Public key: ${pubKey.slice(0, 16)}...`, "auth");
|
|
1013
1109
|
const signChallengeFn = async (challenge) => {
|
|
1110
|
+
if (_ephemeralSeed) {
|
|
1111
|
+
return await CryptoIdentityKeystore.signWithSeed(challenge, _ephemeralSeed);
|
|
1112
|
+
}
|
|
1113
|
+
if (softKeystoreAvailable()) {
|
|
1114
|
+
try {
|
|
1115
|
+
if (await getSoftKeyPublicKey() === pubKey) {
|
|
1116
|
+
return await signWithSoftKey(challenge);
|
|
1117
|
+
}
|
|
1118
|
+
} catch {
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1014
1121
|
if (isClaimed.value) return await ks.sign(challenge);
|
|
1122
|
+
if (softKeystoreAvailable()) return await signWithSoftKey(challenge);
|
|
1015
1123
|
if (privKey) {
|
|
1016
1124
|
const sig = await ed.sign(fromBase64Url(challenge), privKey);
|
|
1017
1125
|
return toBase64Url(sig);
|
|
@@ -1412,6 +1520,7 @@ export default defineNuxtPlugin({
|
|
|
1412
1520
|
loginWithHardware,
|
|
1413
1521
|
loginWithPassword,
|
|
1414
1522
|
registerWithPassword,
|
|
1523
|
+
loginWithMnemonic,
|
|
1415
1524
|
logout,
|
|
1416
1525
|
reconnect,
|
|
1417
1526
|
logoutServer,
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SoftKeystore (module port)
|
|
3
|
+
*
|
|
4
|
+
* Stores Ed25519 private keys for guest (unclaimed) and recovery-phrase
|
|
5
|
+
* identities in IndexedDB, encrypted with a non-extractable AES-256-GCM
|
|
6
|
+
* CryptoKey — instead of the legacy plaintext `abracadabra_privkey`
|
|
7
|
+
* localStorage entry.
|
|
8
|
+
*
|
|
9
|
+
* This prevents XSS from reading the raw key material out of localStorage.
|
|
10
|
+
* An attacker with JS execution can still _use_ the key (sign challenges)
|
|
11
|
+
* while the page is open, but cannot exfiltrate it for offline use.
|
|
12
|
+
*
|
|
13
|
+
* Mirrors `cou-sh/app/utils/softKeystore.ts` so the Nuxt module and the
|
|
14
|
+
* reference app persist soft identities identically. Encoding helpers are
|
|
15
|
+
* inlined here to keep the util self-contained (the module has no shared
|
|
16
|
+
* encoding util).
|
|
17
|
+
*/
|
|
18
|
+
/** Whether the browser supports the APIs required for secure soft key storage. */
|
|
19
|
+
export declare function softKeystoreAvailable(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Initialize or retrieve the soft key. Returns the Ed25519 private key bytes
|
|
22
|
+
* and the base64url-encoded public key.
|
|
23
|
+
*
|
|
24
|
+
* On first call, generates a new keypair, encrypts the private key, and stores
|
|
25
|
+
* everything in IndexedDB. On subsequent calls, decrypts and returns the
|
|
26
|
+
* existing key. Automatically migrates a plaintext key from localStorage.
|
|
27
|
+
*/
|
|
28
|
+
export declare function getOrCreateSoftKey(): Promise<{
|
|
29
|
+
privateKey: Uint8Array;
|
|
30
|
+
publicKey: string;
|
|
31
|
+
}>;
|
|
32
|
+
/**
|
|
33
|
+
* Persist an externally-supplied Ed25519 seed (e.g. derived from a recovery
|
|
34
|
+
* phrase) as the active soft key, encrypted at rest exactly like a generated
|
|
35
|
+
* guest key. This lets a mnemonic-recovered identity survive JWT expiry / page
|
|
36
|
+
* reloads in the browser, where there is no OS keychain to fall back on.
|
|
37
|
+
*
|
|
38
|
+
* The store holds a SINGLE record, so this overwrites any existing guest soft
|
|
39
|
+
* key — the intended semantics of "this recovered identity is now active".
|
|
40
|
+
*
|
|
41
|
+
* @param seed 32-byte Ed25519 secret. The caller still owns the passed buffer.
|
|
42
|
+
*/
|
|
43
|
+
export declare function importSoftKeyFromSeed(seed: Uint8Array): Promise<{
|
|
44
|
+
publicKey: string;
|
|
45
|
+
}>;
|
|
46
|
+
/** Retrieve the stored public key without decrypting the private key. */
|
|
47
|
+
export declare function getSoftKeyPublicKey(): Promise<string | null>;
|
|
48
|
+
/**
|
|
49
|
+
* Sign a base64url-encoded challenge using the stored soft key.
|
|
50
|
+
* Returns a base64url-encoded Ed25519 signature. Decrypts on demand and wipes
|
|
51
|
+
* the private key from memory afterwards.
|
|
52
|
+
*/
|
|
53
|
+
export declare function signWithSoftKey(challengeB64: string): Promise<string>;
|
|
54
|
+
/** Delete the stored soft key identity (and any legacy plaintext entry). */
|
|
55
|
+
export declare function clearSoftKey(): Promise<void>;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import * as ed from "@noble/ed25519";
|
|
2
|
+
import { sha512 } from "@noble/hashes/sha2.js";
|
|
3
|
+
if (!ed.hashes.sha512) {
|
|
4
|
+
ed.hashes.sha512 = sha512;
|
|
5
|
+
ed.hashes.sha512Async = (m) => Promise.resolve(sha512(m));
|
|
6
|
+
}
|
|
7
|
+
function toBase64Url(bytes) {
|
|
8
|
+
let bin = "";
|
|
9
|
+
for (const b of bytes) bin += String.fromCharCode(b);
|
|
10
|
+
return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
11
|
+
}
|
|
12
|
+
function fromBase64Url(str) {
|
|
13
|
+
const b64 = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
14
|
+
const bin = atob(b64);
|
|
15
|
+
const bytes = new Uint8Array(bin.length);
|
|
16
|
+
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
|
|
17
|
+
return bytes;
|
|
18
|
+
}
|
|
19
|
+
const DB_NAME = "abracadabra:softkey";
|
|
20
|
+
const STORE_NAME = "identity";
|
|
21
|
+
const RECORD_KEY = "current";
|
|
22
|
+
const LEGACY_LS_KEY = "abracadabra_privkey";
|
|
23
|
+
function openDb() {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const req = indexedDB.open(DB_NAME, 1);
|
|
26
|
+
req.onupgradeneeded = () => {
|
|
27
|
+
req.result.createObjectStore(STORE_NAME);
|
|
28
|
+
};
|
|
29
|
+
req.onsuccess = () => resolve(req.result);
|
|
30
|
+
req.onerror = () => reject(req.error);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function dbGet(db) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const tx = db.transaction(STORE_NAME, "readonly");
|
|
36
|
+
const req = tx.objectStore(STORE_NAME).get(RECORD_KEY);
|
|
37
|
+
req.onsuccess = () => resolve(req.result);
|
|
38
|
+
req.onerror = () => reject(req.error);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function dbPut(db, value) {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const tx = db.transaction(STORE_NAME, "readwrite");
|
|
44
|
+
const req = tx.objectStore(STORE_NAME).put(value, RECORD_KEY);
|
|
45
|
+
req.onsuccess = () => resolve();
|
|
46
|
+
req.onerror = () => reject(req.error);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function dbDelete(db) {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const tx = db.transaction(STORE_NAME, "readwrite");
|
|
52
|
+
const req = tx.objectStore(STORE_NAME).delete(RECORD_KEY);
|
|
53
|
+
req.onsuccess = () => resolve();
|
|
54
|
+
req.onerror = () => reject(req.error);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async function generateWrappingKey() {
|
|
58
|
+
return crypto.subtle.generateKey(
|
|
59
|
+
{ name: "AES-GCM", length: 256 },
|
|
60
|
+
false,
|
|
61
|
+
// non-extractable
|
|
62
|
+
["encrypt", "decrypt"]
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
async function encryptPrivateKey(wrappingKey, privateKey) {
|
|
66
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
67
|
+
const ciphertext = await crypto.subtle.encrypt(
|
|
68
|
+
{ name: "AES-GCM", iv },
|
|
69
|
+
wrappingKey,
|
|
70
|
+
privateKey
|
|
71
|
+
);
|
|
72
|
+
return { ciphertext, iv };
|
|
73
|
+
}
|
|
74
|
+
async function decryptPrivateKey(wrappingKey, ciphertext, iv) {
|
|
75
|
+
const plain = await crypto.subtle.decrypt(
|
|
76
|
+
{ name: "AES-GCM", iv },
|
|
77
|
+
wrappingKey,
|
|
78
|
+
ciphertext
|
|
79
|
+
);
|
|
80
|
+
return new Uint8Array(plain);
|
|
81
|
+
}
|
|
82
|
+
export function softKeystoreAvailable() {
|
|
83
|
+
return typeof globalThis.crypto?.subtle !== "undefined" && typeof globalThis.indexedDB !== "undefined";
|
|
84
|
+
}
|
|
85
|
+
export async function getOrCreateSoftKey() {
|
|
86
|
+
const db = await openDb();
|
|
87
|
+
try {
|
|
88
|
+
const stored = await dbGet(db);
|
|
89
|
+
if (stored) {
|
|
90
|
+
const privateKey2 = await decryptPrivateKey(stored.wrappingKey, stored.encryptedPrivateKey, stored.iv);
|
|
91
|
+
return { privateKey: privateKey2, publicKey: stored.publicKey };
|
|
92
|
+
}
|
|
93
|
+
const legacyKey = localStorage.getItem(LEGACY_LS_KEY);
|
|
94
|
+
let privateKey;
|
|
95
|
+
if (legacyKey) {
|
|
96
|
+
privateKey = fromBase64Url(legacyKey);
|
|
97
|
+
localStorage.removeItem(LEGACY_LS_KEY);
|
|
98
|
+
} else {
|
|
99
|
+
privateKey = ed.utils.randomSecretKey();
|
|
100
|
+
}
|
|
101
|
+
const publicKey = toBase64Url(ed.getPublicKey(privateKey));
|
|
102
|
+
const wrappingKey = await generateWrappingKey();
|
|
103
|
+
const { ciphertext, iv } = await encryptPrivateKey(wrappingKey, privateKey);
|
|
104
|
+
await dbPut(db, { publicKey, encryptedPrivateKey: ciphertext, iv, wrappingKey });
|
|
105
|
+
return { privateKey, publicKey };
|
|
106
|
+
} finally {
|
|
107
|
+
db.close();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export async function importSoftKeyFromSeed(seed) {
|
|
111
|
+
const publicKey = toBase64Url(ed.getPublicKey(seed));
|
|
112
|
+
const wrappingKey = await generateWrappingKey();
|
|
113
|
+
const seedCopy = seed.slice();
|
|
114
|
+
try {
|
|
115
|
+
const { ciphertext, iv } = await encryptPrivateKey(wrappingKey, seedCopy);
|
|
116
|
+
const db = await openDb();
|
|
117
|
+
try {
|
|
118
|
+
await dbPut(db, { publicKey, encryptedPrivateKey: ciphertext, iv, wrappingKey });
|
|
119
|
+
} finally {
|
|
120
|
+
db.close();
|
|
121
|
+
}
|
|
122
|
+
} finally {
|
|
123
|
+
seedCopy.fill(0);
|
|
124
|
+
}
|
|
125
|
+
return { publicKey };
|
|
126
|
+
}
|
|
127
|
+
export async function getSoftKeyPublicKey() {
|
|
128
|
+
const db = await openDb();
|
|
129
|
+
try {
|
|
130
|
+
const stored = await dbGet(db);
|
|
131
|
+
return stored?.publicKey ?? null;
|
|
132
|
+
} finally {
|
|
133
|
+
db.close();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export async function signWithSoftKey(challengeB64) {
|
|
137
|
+
const db = await openDb();
|
|
138
|
+
try {
|
|
139
|
+
const stored = await dbGet(db);
|
|
140
|
+
if (!stored) throw new Error("No soft key stored");
|
|
141
|
+
const privateKey = await decryptPrivateKey(stored.wrappingKey, stored.encryptedPrivateKey, stored.iv);
|
|
142
|
+
try {
|
|
143
|
+
const sig = await ed.sign(fromBase64Url(challengeB64), privateKey);
|
|
144
|
+
return toBase64Url(sig);
|
|
145
|
+
} finally {
|
|
146
|
+
privateKey.fill(0);
|
|
147
|
+
}
|
|
148
|
+
} finally {
|
|
149
|
+
db.close();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export async function clearSoftKey() {
|
|
153
|
+
const db = await openDb();
|
|
154
|
+
try {
|
|
155
|
+
await dbDelete(db);
|
|
156
|
+
} finally {
|
|
157
|
+
db.close();
|
|
158
|
+
}
|
|
159
|
+
localStorage.removeItem(LEGACY_LS_KEY);
|
|
160
|
+
}
|