@evolustr/core 0.0.0-stage → 0.1.1
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/README.md +15 -2
- package/dist/aad.d.ts +9 -0
- package/dist/aad.d.ts.map +1 -0
- package/dist/aad.js +5 -0
- package/dist/aad.js.map +1 -0
- package/dist/appId.d.ts +12 -0
- package/dist/appId.d.ts.map +1 -0
- package/dist/appId.js +45 -0
- package/dist/appId.js.map +1 -0
- package/dist/bytes.d.ts +12 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +51 -0
- package/dist/bytes.js.map +1 -0
- package/dist/check.d.ts +63 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +107 -0
- package/dist/check.js.map +1 -0
- package/dist/crypto.d.ts +18 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +29 -0
- package/dist/crypto.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/keys.d.ts +48 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +50 -0
- package/dist/keys.js.map +1 -0
- package/dist/nostr/index.d.ts +7 -0
- package/dist/nostr/index.d.ts.map +1 -0
- package/dist/nostr/index.js +7 -0
- package/dist/nostr/index.js.map +1 -0
- package/dist/nostr/relays.d.ts +24 -0
- package/dist/nostr/relays.d.ts.map +1 -0
- package/dist/nostr/relays.js +178 -0
- package/dist/nostr/relays.js.map +1 -0
- package/dist/nostr/replaceable.d.ts +47 -0
- package/dist/nostr/replaceable.d.ts.map +1 -0
- package/dist/nostr/replaceable.js +49 -0
- package/dist/nostr/replaceable.js.map +1 -0
- package/dist/nostr/signers.d.ts +29 -0
- package/dist/nostr/signers.d.ts.map +1 -0
- package/dist/nostr/signers.js +35 -0
- package/dist/nostr/signers.js.map +1 -0
- package/dist/nostr/types.d.ts +67 -0
- package/dist/nostr/types.d.ts.map +1 -0
- package/dist/nostr/types.js +2 -0
- package/dist/nostr/types.js.map +1 -0
- package/dist/nostr/vaultEvent.d.ts +13 -0
- package/dist/nostr/vaultEvent.d.ts.map +1 -0
- package/dist/nostr/vaultEvent.js +37 -0
- package/dist/nostr/vaultEvent.js.map +1 -0
- package/dist/nostr/verify.d.ts +7 -0
- package/dist/nostr/verify.d.ts.map +1 -0
- package/dist/nostr/verify.js +10 -0
- package/dist/nostr/verify.js.map +1 -0
- package/dist/passkeys.d.ts +47 -0
- package/dist/passkeys.d.ts.map +1 -0
- package/dist/passkeys.js +112 -0
- package/dist/passkeys.js.map +1 -0
- package/dist/phrase.d.ts +12 -0
- package/dist/phrase.d.ts.map +1 -0
- package/dist/phrase.js +23 -0
- package/dist/phrase.js.map +1 -0
- package/dist/result.d.ts +27 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js +41 -0
- package/dist/result.js.map +1 -0
- package/dist/settings.d.ts +37 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/settings.js +73 -0
- package/dist/settings.js.map +1 -0
- package/dist/tags.d.ts +12 -0
- package/dist/tags.d.ts.map +1 -0
- package/dist/tags.js +13 -0
- package/dist/tags.js.map +1 -0
- package/dist/vault.d.ts +229 -0
- package/dist/vault.d.ts.map +1 -0
- package/dist/vault.js +398 -0
- package/dist/vault.js.map +1 -0
- package/package.json +43 -4
- package/src/aad.ts +21 -0
- package/src/appId.ts +46 -0
- package/src/bytes.ts +55 -0
- package/src/check.ts +160 -0
- package/src/crypto.ts +49 -0
- package/src/index.ts +13 -0
- package/src/keys.ts +78 -0
- package/src/nostr/index.ts +6 -0
- package/src/nostr/relays.ts +178 -0
- package/src/nostr/replaceable.ts +93 -0
- package/src/nostr/signers.ts +63 -0
- package/src/nostr/types.ts +73 -0
- package/src/nostr/vaultEvent.ts +46 -0
- package/src/nostr/verify.ts +11 -0
- package/src/passkeys.ts +151 -0
- package/src/phrase.ts +26 -0
- package/src/result.ts +63 -0
- package/src/settings.ts +109 -0
- package/src/tags.ts +20 -0
- package/src/vault.ts +612 -0
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @evolustr/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> ⚠️ **Early alpha: don't use it for data you can't afford to lose.**
|
|
4
|
+
>
|
|
5
|
+
> Evolustr has not had a security audit, and its vault format and APIs may still change without a migration path. It has been tested mainly with simulated signers, relays and passkeys; testing on real devices has only started.
|
|
6
|
+
>
|
|
7
|
+
> Keep your own backup of anything important, save your recovery phrase, and don't use it to protect high-value secrets (keys to funds, bank credentials). See the [project README](https://github.com/saintego/evolustr#readme) and the [open issues](https://github.com/saintego/evolustr/issues).
|
|
8
|
+
|
|
9
|
+
Shared building blocks of [Evolustr](https://github.com/saintego/evolustr): crypto (key derivation, sealing, tags), the vault format and its checks, the recovery phrase, app ids, and Nostr I/O (signers, relays, vault events). Pure functions and plain data; side effects come in through small interfaces (`Signer`, `Relays`, `Passkeys`).
|
|
10
|
+
|
|
11
|
+
Apps usually don't use it directly: start with [`@evolustr/client`](https://www.npmjs.com/package/@evolustr/client). Signers start with [`@evolustr/signer`](https://www.npmjs.com/package/@evolustr/signer).
|
|
12
|
+
|
|
13
|
+
- How it works, without the crypto background: [docs/how-it-works.md](https://github.com/saintego/evolustr/blob/main/docs/how-it-works.md)
|
|
14
|
+
- The exact rules: [SPEC.md](https://github.com/saintego/evolustr/blob/main/SPEC.md)
|
|
15
|
+
|
|
16
|
+
MIT licence.
|
package/dist/aad.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Wrap types inside app vaults (SPEC §3.1). */
|
|
2
|
+
export type AppWrapType = "signer" | "passkey" | "root";
|
|
3
|
+
/** AAD for app-vault wraps: binds the wrap to user, app, type and its key id. */
|
|
4
|
+
export declare const appWrapAad: (pubkey: string, appId: string, wrapType: AppWrapType, keyId: string) => string;
|
|
5
|
+
/** Wrap types inside a signer's own vault (SPEC §5.3). */
|
|
6
|
+
export type SignerWrapType = "sk" | "passkey" | "previous";
|
|
7
|
+
/** AAD for signer-vault wraps; `keyId` is the credId, or "sk". */
|
|
8
|
+
export declare const signerWrapAad: (pubkey: string, signerKey: string, wrapType: SignerWrapType, keyId: string) => string;
|
|
9
|
+
//# sourceMappingURL=aad.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aad.d.ts","sourceRoot":"","sources":["../src/aad.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;AAExD,iFAAiF;AACjF,eAAO,MAAM,UAAU,WACb,MAAM,SACP,MAAM,YACH,WAAW,SACd,MAAM,KACZ,MAA+D,CAAC;AAEnE,0DAA0D;AAC1D,MAAM,MAAM,cAAc,GAAG,IAAI,GAAG,SAAS,GAAG,UAAU,CAAC;AAE3D,kEAAkE;AAClE,eAAO,MAAM,aAAa,WAChB,MAAM,aACH,MAAM,YACP,cAAc,SACjB,MAAM,KACZ,MAA0E,CAAC"}
|
package/dist/aad.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** AAD for app-vault wraps: binds the wrap to user, app, type and its key id. */
|
|
2
|
+
export const appWrapAad = (pubkey, appId, wrapType, keyId) => `evolustr/v1|${pubkey}|${appId}|${wrapType}|${keyId}`;
|
|
3
|
+
/** AAD for signer-vault wraps; `keyId` is the credId, or "sk". */
|
|
4
|
+
export const signerWrapAad = (pubkey, signerKey, wrapType, keyId) => `evolustr/v1|signer|${pubkey}|${signerKey}|${wrapType}|${keyId}`;
|
|
5
|
+
//# sourceMappingURL=aad.js.map
|
package/dist/aad.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aad.js","sourceRoot":"","sources":["../src/aad.ts"],"names":[],"mappings":"AAGA,iFAAiF;AACjF,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,MAAc,EACd,KAAa,EACb,QAAqB,EACrB,KAAa,EACL,EAAE,CAAC,eAAe,MAAM,IAAI,KAAK,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC;AAKnE,kEAAkE;AAClE,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,MAAc,EACd,SAAiB,EACjB,QAAwB,EACxB,KAAa,EACL,EAAE,CAAC,sBAAsB,MAAM,IAAI,SAAS,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC"}
|
package/dist/appId.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type EvolustrError, type Result } from "./result.ts";
|
|
2
|
+
/**
|
|
3
|
+
* App identity from a page origin (SPEC §3.3). On a known nsite gateway,
|
|
4
|
+
* `<npub>.<gateway>` maps to `nsite:<npub>` so every known gateway shares one vault.
|
|
5
|
+
*
|
|
6
|
+
* Only `https:` origins, and `http:` on localhost for development, are accepted:
|
|
7
|
+
* a network attacker could otherwise serve a page on `http://app.example` and get
|
|
8
|
+
* that app's keys (#30). Anything that isn't a valid origin is an error, never
|
|
9
|
+
* passed through, because app ids end up in AADs and d-tags.
|
|
10
|
+
*/
|
|
11
|
+
export declare const appIdFromOrigin: (origin: string, knownNsiteGateways?: ReadonlyArray<string>) => Result<string, EvolustrError>;
|
|
12
|
+
//# sourceMappingURL=appId.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appId.d.ts","sourceRoot":"","sources":["../src/appId.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,MAAM,EAAa,MAAM,aAAa,CAAC;AAKzE;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,WAClB,MAAM,uBACM,aAAa,CAAC,MAAM,CAAC,KACxC,MAAM,CAAC,MAAM,EAAE,aAAa,CAmB9B,CAAC"}
|
package/dist/appId.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { decode } from "nostr-tools/nip19";
|
|
2
|
+
import { error, ok } from "./result.js";
|
|
3
|
+
const isLocalhost = (host) => host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host.endsWith(".localhost");
|
|
4
|
+
/**
|
|
5
|
+
* App identity from a page origin (SPEC §3.3). On a known nsite gateway,
|
|
6
|
+
* `<npub>.<gateway>` maps to `nsite:<npub>` so every known gateway shares one vault.
|
|
7
|
+
*
|
|
8
|
+
* Only `https:` origins, and `http:` on localhost for development, are accepted:
|
|
9
|
+
* a network attacker could otherwise serve a page on `http://app.example` and get
|
|
10
|
+
* that app's keys (#30). Anything that isn't a valid origin is an error, never
|
|
11
|
+
* passed through, because app ids end up in AADs and d-tags.
|
|
12
|
+
*/
|
|
13
|
+
export const appIdFromOrigin = (origin, knownNsiteGateways = []) => {
|
|
14
|
+
let url;
|
|
15
|
+
try {
|
|
16
|
+
url = new URL(origin);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return error("invalidOrigin", "Not a valid origin");
|
|
20
|
+
}
|
|
21
|
+
const host = url.hostname.toLowerCase();
|
|
22
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && isLocalhost(host))) {
|
|
23
|
+
return error("insecureOrigin", "Only https: origins (or http: on localhost) can have app data");
|
|
24
|
+
}
|
|
25
|
+
for (const gateway of knownNsiteGateways) {
|
|
26
|
+
const g = gateway.toLowerCase().replace(/^\.+/, "");
|
|
27
|
+
if (!host.endsWith(`.${g}`))
|
|
28
|
+
continue;
|
|
29
|
+
const label = host.slice(0, -(g.length + 1));
|
|
30
|
+
if (label.includes(".") || !label.startsWith("npub1"))
|
|
31
|
+
continue;
|
|
32
|
+
if (isValidNpub(label))
|
|
33
|
+
return ok(`nsite:${label}`);
|
|
34
|
+
}
|
|
35
|
+
return ok(url.origin);
|
|
36
|
+
};
|
|
37
|
+
const isValidNpub = (value) => {
|
|
38
|
+
try {
|
|
39
|
+
return decode(value).type === "npub";
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|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tC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;QAC7C,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC;YAAE,SAAS;QAChE,IAAI,WAAW,CAAC,KAAK,CAAC;YAAE,OAAO,EAAE,CAAC,SAAS,KAAK,EAAE,CAAC,CAAC;IACtD,CAAC;IACD,OAAO,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAC,KAAa,EAAW,EAAE;IAC7C,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC;IACvC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC"}
|
package/dist/bytes.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type Bytes = Uint8Array;
|
|
2
|
+
export declare const utf8: (text: string) => Bytes;
|
|
3
|
+
export declare const fromUtf8: (bytes: Bytes) => string;
|
|
4
|
+
export declare const randomBytes: (length: number) => Bytes;
|
|
5
|
+
export declare const toHex: (bytes: Bytes) => string;
|
|
6
|
+
export declare const fromHex: (hex: string) => Bytes | null;
|
|
7
|
+
export declare const toBase64Url: (bytes: Bytes) => string;
|
|
8
|
+
export declare const fromBase64Url: (text: string) => Bytes | null;
|
|
9
|
+
export declare const concat: (...parts: ReadonlyArray<Bytes>) => Bytes;
|
|
10
|
+
/** Constant-time comparison for equal-length secrets. */
|
|
11
|
+
export declare const bytesEqual: (a: Bytes, b: Bytes) => boolean;
|
|
12
|
+
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC;AAK/B,eAAO,MAAM,IAAI,SAAU,MAAM,KAAG,KAA6B,CAAC;AAClE,eAAO,MAAM,QAAQ,UAAW,KAAK,KAAG,MAA+B,CAAC;AAExE,eAAO,MAAM,WAAW,WAAY,MAAM,KAAG,KAAiC,CAAC;AAE/E,eAAO,MAAM,KAAK,UAAW,KAAK,KAAG,MAC+B,CAAC;AAErE,eAAO,MAAM,OAAO,QAAS,MAAM,KAAG,KAAK,GAAG,IAK7C,CAAC;AAEF,eAAO,MAAM,WAAW,UAAW,KAAK,KAAG,MAI1C,CAAC;AAEF,eAAO,MAAM,aAAa,SAAU,MAAM,KAAG,KAAK,GAAG,IAQpD,CAAC;AAEF,eAAO,MAAM,MAAM,aAAc,aAAa,CAAC,KAAK,CAAC,KAAG,KAQvD,CAAC;AAEF,yDAAyD;AACzD,eAAO,MAAM,UAAU,MAAO,KAAK,KAAK,KAAK,KAAG,OAK/C,CAAC"}
|
package/dist/bytes.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
const decoder = new TextDecoder();
|
|
4
|
+
export const utf8 = (text) => encoder.encode(text);
|
|
5
|
+
export const fromUtf8 = (bytes) => decoder.decode(bytes);
|
|
6
|
+
export const randomBytes = (length) => nobleRandomBytes(length);
|
|
7
|
+
export const toHex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
8
|
+
export const fromHex = (hex) => {
|
|
9
|
+
if (hex.length % 2 !== 0 || !/^[0-9a-f]*$/i.test(hex))
|
|
10
|
+
return null;
|
|
11
|
+
const out = new Uint8Array(hex.length / 2);
|
|
12
|
+
for (let i = 0; i < out.length; i++)
|
|
13
|
+
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
14
|
+
return out;
|
|
15
|
+
};
|
|
16
|
+
export const toBase64Url = (bytes) => {
|
|
17
|
+
let binary = "";
|
|
18
|
+
for (const b of bytes)
|
|
19
|
+
binary += String.fromCharCode(b);
|
|
20
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
21
|
+
};
|
|
22
|
+
export const fromBase64Url = (text) => {
|
|
23
|
+
if (!/^[A-Za-z0-9_-]*$/.test(text))
|
|
24
|
+
return null;
|
|
25
|
+
try {
|
|
26
|
+
const binary = atob(text.replace(/-/g, "+").replace(/_/g, "/"));
|
|
27
|
+
return Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
export const concat = (...parts) => {
|
|
34
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
35
|
+
let offset = 0;
|
|
36
|
+
for (const p of parts) {
|
|
37
|
+
out.set(p, offset);
|
|
38
|
+
offset += p.length;
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
};
|
|
42
|
+
/** Constant-time comparison for equal-length secrets. */
|
|
43
|
+
export const bytesEqual = (a, b) => {
|
|
44
|
+
if (a.length !== b.length)
|
|
45
|
+
return false;
|
|
46
|
+
let diff = 0;
|
|
47
|
+
for (let i = 0; i < a.length; i++)
|
|
48
|
+
diff |= (a[i] ?? 0) ^ (b[i] ?? 0);
|
|
49
|
+
return diff === 0;
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.js","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAIzE,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,IAAI,W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,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,yDAAyD;AACzD,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAQ,EAAE,CAAQ,EAAW,EAAE;IACxD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IACrE,OAAO,IAAI,KAAK,CAAC,CAAC;AACpB,CAAC,CAAC"}
|
package/dist/check.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decides what a device does with the vault it fetched (SPEC §6.3).
|
|
3
|
+
* Pure: the caller supplies what it fetched and what it already knows.
|
|
4
|
+
*/
|
|
5
|
+
import type { Bytes } from "./bytes.ts";
|
|
6
|
+
import { type Vault, type VaultContext } from "./vault.ts";
|
|
7
|
+
export type Fetched = {
|
|
8
|
+
readonly kind: "found";
|
|
9
|
+
readonly vault: Vault;
|
|
10
|
+
}
|
|
11
|
+
/** Every relay answered and none has a vault. */
|
|
12
|
+
| {
|
|
13
|
+
readonly kind: "none";
|
|
14
|
+
}
|
|
15
|
+
/** A vault event exists but can't be parsed, or relays didn't all answer. */
|
|
16
|
+
| {
|
|
17
|
+
readonly kind: "unreadable";
|
|
18
|
+
};
|
|
19
|
+
export interface LocalState {
|
|
20
|
+
/** `sHash` of the local database for this (pubkey, appId). */
|
|
21
|
+
readonly sHash: string;
|
|
22
|
+
/** R, if this device knows it (cached or just unlocked). */
|
|
23
|
+
readonly r?: Bytes;
|
|
24
|
+
/** The last good vault, as kept in the app database (SPEC §6.2 rule 5). */
|
|
25
|
+
readonly copy?: Vault;
|
|
26
|
+
/** Data secrets this device has moved past, kept locally beyond the vault's history limit (#44). */
|
|
27
|
+
readonly seen?: ReadonlyArray<string>;
|
|
28
|
+
}
|
|
29
|
+
export type VaultChoice = "republishCopy" | "followRelay" | "restorePhrase" | "startNew";
|
|
30
|
+
export type VaultDecision =
|
|
31
|
+
/** No local database: unlock normally; warn about the weak level before first use. */
|
|
32
|
+
{
|
|
33
|
+
readonly type: "firstUse";
|
|
34
|
+
readonly vault: Vault;
|
|
35
|
+
readonly warnWeak: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** No vault anywhere and no local database: create one. */
|
|
38
|
+
| {
|
|
39
|
+
readonly type: "create";
|
|
40
|
+
} | {
|
|
41
|
+
readonly type: "same";
|
|
42
|
+
}
|
|
43
|
+
/** A rotation this device can verify (its secret is in the vault's history): follow and migrate. */
|
|
44
|
+
| {
|
|
45
|
+
readonly type: "follow";
|
|
46
|
+
readonly vault: Vault;
|
|
47
|
+
readonly s: Bytes;
|
|
48
|
+
readonly cutoff: number | null;
|
|
49
|
+
}
|
|
50
|
+
/** The device can't verify a strict vault: needs a strong method or the phrase. */
|
|
51
|
+
| {
|
|
52
|
+
readonly type: "needsStrongUnlock";
|
|
53
|
+
readonly vault: Vault;
|
|
54
|
+
}
|
|
55
|
+
/** The user must choose; `options` lists what is safe to offer. */
|
|
56
|
+
| {
|
|
57
|
+
readonly type: "choose";
|
|
58
|
+
readonly reason: "replaced" | "ambiguous" | "missing";
|
|
59
|
+
readonly vault?: Vault;
|
|
60
|
+
readonly options: ReadonlyArray<VaultChoice>;
|
|
61
|
+
};
|
|
62
|
+
export declare const decideVault: (ctx: VaultContext, fetched: Fetched, local: LocalState | null) => VaultDecision;
|
|
63
|
+
//# sourceMappingURL=check.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../src/check.ts"],"names":[],"mappings":"AAAA;;;GA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,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE;AACvG,mFAAmF;GACjF;IAAE,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAA;CAAE;AAC/D,mEAAmE;GACjE;IACE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,WAAW,GAAG,SAAS,CAAC;IACtD,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;CAC9C,CAAC;AAkCN,eAAO,MAAM,WAAW,QACjB,YAAY,WACR,OAAO,SACT,UAAU,GAAG,IAAI,KACvB,aA0EF,CAAC"}
|
package/dist/check.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { hasRootKey, headerBinding, migrationCutoff, nip44Wrap, openRootData, sameVaultCore, verifyRootRotation } from "./vault.js";
|
|
2
|
+
/** True when this device has already moved past the vault's secret: a rollback. */
|
|
3
|
+
const movedPast = (local, sHash) => local.copy?.history.some((h) => h.sHash === sHash) === true || local.seen?.includes(sHash) === true;
|
|
4
|
+
/**
|
|
5
|
+
* Same secret as the local database (#38). The local copy is the trust anchor,
|
|
6
|
+
* so a fetched vault is only accepted when R verifies it or it matches the copy.
|
|
7
|
+
*/
|
|
8
|
+
const decideSameSecret = (ctx, vault, local, replaced, canRepublish) => {
|
|
9
|
+
const copy = local.copy;
|
|
10
|
+
if (local.r !== undefined) {
|
|
11
|
+
if (hasRootKey(vault))
|
|
12
|
+
return openRootData(ctx, vault, local.r).ok ? { type: "same" } : replaced;
|
|
13
|
+
// The relay version lost the root key the copy has: forged.
|
|
14
|
+
return copy !== undefined && hasRootKey(copy) ? replaced : { type: "same" };
|
|
15
|
+
}
|
|
16
|
+
// No copy and no R: nothing to check a root key or strict header against (#54).
|
|
17
|
+
if (copy === undefined)
|
|
18
|
+
return vault.mode === "strict" || hasRootKey(vault) ? { type: "needsStrongUnlock", vault } : { type: "same" };
|
|
19
|
+
if (sameVaultCore(copy, vault))
|
|
20
|
+
return { type: "same" };
|
|
21
|
+
if (!hasRootKey(copy)) {
|
|
22
|
+
// Another device added R: the header and the nip44 wrap are unchanged.
|
|
23
|
+
const sameHeader = headerBinding(copy) === headerBinding(vault);
|
|
24
|
+
const sameNip44 = nip44Wrap(copy)?.ct === nip44Wrap(vault)?.ct;
|
|
25
|
+
if (hasRootKey(vault) && sameHeader && sameNip44)
|
|
26
|
+
return { type: "same" };
|
|
27
|
+
return { type: "choose", reason: "ambiguous", vault, options: canRepublish ? ["followRelay", "republishCopy"] : ["followRelay"] };
|
|
28
|
+
}
|
|
29
|
+
// The copy has a root key and the relay version differs: a legitimate
|
|
30
|
+
// downgrade elsewhere looks like this, and only R can tell.
|
|
31
|
+
return { type: "needsStrongUnlock", vault };
|
|
32
|
+
};
|
|
33
|
+
export const decideVault = (ctx, fetched, local) => {
|
|
34
|
+
const canRepublish = local?.copy !== undefined;
|
|
35
|
+
if (fetched.kind !== "found") {
|
|
36
|
+
if (local === null) {
|
|
37
|
+
return fetched.kind === "none"
|
|
38
|
+
? { type: "create" }
|
|
39
|
+
: { type: "choose", reason: "missing", options: ["restorePhrase", "startNew"] };
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
type: "choose",
|
|
43
|
+
reason: "missing",
|
|
44
|
+
options: canRepublish ? ["republishCopy", "restorePhrase", "startNew"] : ["restorePhrase", "startNew"],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const vault = fetched.vault;
|
|
48
|
+
if (local === null)
|
|
49
|
+
return { type: "firstUse", vault, warnWeak: vault.mode === "compatible" };
|
|
50
|
+
const replaced = {
|
|
51
|
+
type: "choose",
|
|
52
|
+
reason: "replaced",
|
|
53
|
+
vault,
|
|
54
|
+
options: canRepublish ? ["republishCopy", "restorePhrase", "startNew"] : ["restorePhrase", "startNew"],
|
|
55
|
+
};
|
|
56
|
+
if (vault.sHash === local.sHash)
|
|
57
|
+
return decideSameSecret(ctx, vault, local, replaced, canRepublish);
|
|
58
|
+
// A root rotation moving forward from this device's secret (#52 phase C).
|
|
59
|
+
const forward = vault.history.some((h) => h.sHash === local.sHash);
|
|
60
|
+
if (local.r !== undefined) {
|
|
61
|
+
const s = openRootData(ctx, vault, local.r);
|
|
62
|
+
// Signed by this device's R: the user replaced the root key elsewhere. The
|
|
63
|
+
// new R comes from a strong method; never offer to republish the old copy.
|
|
64
|
+
if (!s.ok)
|
|
65
|
+
return forward && verifyRootRotation(ctx, vault, local.r) ? { type: "needsStrongUnlock", vault } : replaced;
|
|
66
|
+
// Follow on its own only when moving forward: the vault's authenticated
|
|
67
|
+
// history names the local secret (#25). An older vault replayed by someone
|
|
68
|
+
// who can sign for the user opens with R too, so it must never be followed.
|
|
69
|
+
if (vault.history.some((h) => h.sHash === local.sHash)) {
|
|
70
|
+
return { type: "follow", vault, s: s.value, cutoff: migrationCutoff(vault, local.sHash) };
|
|
71
|
+
}
|
|
72
|
+
if (movedPast(local, vault.sHash))
|
|
73
|
+
return replaced;
|
|
74
|
+
return {
|
|
75
|
+
type: "choose",
|
|
76
|
+
reason: "ambiguous",
|
|
77
|
+
vault,
|
|
78
|
+
options: canRepublish ? ["republishCopy", "followRelay", "restorePhrase"] : ["followRelay", "restorePhrase"],
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
// Without R the header can't be verified, but the local copy still tells a
|
|
82
|
+
// rollback (an older secret this device has moved past) or a swap from strict
|
|
83
|
+
// to a different, weak vault: both are replaced, never followed (#25, #27).
|
|
84
|
+
if (movedPast(local, vault.sHash))
|
|
85
|
+
return replaced;
|
|
86
|
+
if (local.copy?.mode === "strict" && vault.mode === "compatible")
|
|
87
|
+
return replaced;
|
|
88
|
+
if (vault.mode === "strict") {
|
|
89
|
+
// Never republish here: it could undo a legitimate makeStrict().
|
|
90
|
+
return { type: "needsStrongUnlock", vault };
|
|
91
|
+
}
|
|
92
|
+
// Claims a root rotation from this device's secret: only R can check it, so
|
|
93
|
+
// ask for a strong unlock rather than offering to undo it (#52 phase C).
|
|
94
|
+
if (forward && vault.rootRotation !== undefined)
|
|
95
|
+
return { type: "needsStrongUnlock", vault };
|
|
96
|
+
// This device's database is protected by a root key: never offer to follow a
|
|
97
|
+
// vault that only NIP-44 can open (#38).
|
|
98
|
+
if (local.copy !== undefined && hasRootKey(local.copy))
|
|
99
|
+
return replaced;
|
|
100
|
+
return {
|
|
101
|
+
type: "choose",
|
|
102
|
+
reason: "ambiguous",
|
|
103
|
+
vault,
|
|
104
|
+
options: canRepublish ? ["followRelay", "republishCopy"] : ["followRelay"],
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
//# sourceMappingURL=check.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check.js","sourceRoot":"","sources":["../src/check.ts"],"names":[],"mappings":"AAKA,OAAO,EAAiC,UAAU,EAAE,aAAa,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAoBnK,mFAAmF;AACnF,MAAM,SAAS,GAAG,CAAC,KAAiB,EAAE,KAAa,EAAW,EAAE,CAC9D,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC;AAsBtG;;;GAGG;AACH,MAAM,gBAAgB,GAAG,CACvB,GAAiB,EACjB,KAAY,EACZ,KAAiB,EACjB,QAAuB,EACvB,YAAqB,EACN,EAAE;IACjB,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IACxB,IAAI,KAAK,CAAC,CAA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|
+
export declare const sha256: (data: Bytes) => Bytes;
|
|
4
|
+
export declare const sha256Hex: (data: Bytes | string) => string;
|
|
5
|
+
/** HMAC-SHA256. */
|
|
6
|
+
export declare const hmacSha256: (key: Bytes, message: Bytes) => Bytes;
|
|
7
|
+
/** HKDF-SHA256 with an empty salt unless one is given. */
|
|
8
|
+
export declare const hkdf: (ikm: Bytes, info: string, length?: number, salt?: Bytes) => Bytes;
|
|
9
|
+
/** AES-256-GCM ciphertext as stored in vault JSON. */
|
|
10
|
+
export interface Sealed {
|
|
11
|
+
readonly iv: string;
|
|
12
|
+
readonly ct: string;
|
|
13
|
+
}
|
|
14
|
+
/** Encrypts with AES-256-GCM, a random 12-byte IV and the given AAD. */
|
|
15
|
+
export declare const seal: (key: Bytes, plaintext: Bytes, aad: string) => Sealed;
|
|
16
|
+
/** Decrypts {@link seal} output; fails on a wrong key, wrong AAD or tampering. */
|
|
17
|
+
export declare const open: (key: Bytes, sealed: Sealed, aad: string) => Result<Bytes, EvolustrError>;
|
|
18
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,KAAK,KAAK,EAMX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,MAAM,EAAsB,MAAM,aAAa,CAAC;AAElF,eAAO,MAAM,MAAM,SAAU,KAAK,KAAG,KAA0B,CAAC;AAEhE,eAAO,MAAM,SAAS,SAAU,KAAK,GAAG,MAAM,KAAG,MACY,CAAC;AAE9D,mBAAmB;AACnB,eAAO,MAAM,UAAU,QAAS,KAAK,WAAW,KAAK,KAAG,KAA6C,CAAC;AAEtG,0DAA0D;AAC1D,eAAO,MAAM,IAAI,QAAS,KAAK,QAAQ,MAAM,0BAAsB,KAAK,KAAG,KACpB,CAAC;AAExD,sDAAsD;AACtD,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACrB;AAED,wEAAwE;AACxE,eAAO,MAAM,IAAI,QAAS,KAAK,aAAa,KAAK,OAAO,MAAM,KAAG,MAIhE,CAAC;AAEF,kFAAkF;AAClF,eAAO,MAAM,IAAI,QAAS,KAAK,UAAU,MAAM,OAAO,MAAM,KAAG,MAAM,CAAC,KAAK,EAAE,aAAa,CAQzF,CAAC"}
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { gcm } from "@noble/ciphers/aes.js";
|
|
2
|
+
import { hkdf as nobleHkdf } from "@noble/hashes/hkdf.js";
|
|
3
|
+
import { hmac as nobleHmac } from "@noble/hashes/hmac.js";
|
|
4
|
+
import { sha256 as nobleSha256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { fromBase64Url, randomBytes, toBase64Url, toHex, utf8, } from "./bytes.js";
|
|
6
|
+
import { attempt, error, ok } from "./result.js";
|
|
7
|
+
export const sha256 = (data) => nobleSha256(data);
|
|
8
|
+
export const sha256Hex = (data) => toHex(sha256(typeof data === "string" ? utf8(data) : data));
|
|
9
|
+
/** HMAC-SHA256. */
|
|
10
|
+
export const hmacSha256 = (key, message) => nobleHmac(nobleSha256, key, message);
|
|
11
|
+
/** HKDF-SHA256 with an empty salt unless one is given. */
|
|
12
|
+
export const hkdf = (ikm, info, length = 32, salt) => nobleHkdf(nobleSha256, ikm, salt, utf8(info), length);
|
|
13
|
+
/** Encrypts with AES-256-GCM, a random 12-byte IV and the given AAD. */
|
|
14
|
+
export const seal = (key, plaintext, aad) => {
|
|
15
|
+
const iv = randomBytes(12);
|
|
16
|
+
const ct = gcm(key, iv, utf8(aad)).encrypt(plaintext);
|
|
17
|
+
return { iv: toBase64Url(iv), ct: toBase64Url(ct) };
|
|
18
|
+
};
|
|
19
|
+
/** Decrypts {@link seal} output; fails on a wrong key, wrong AAD or tampering. */
|
|
20
|
+
export const open = (key, sealed, aad) => {
|
|
21
|
+
const iv = fromBase64Url(sealed.iv);
|
|
22
|
+
const ct = fromBase64Url(sealed.ct);
|
|
23
|
+
if (iv === null || iv.length !== 12 || ct === null) {
|
|
24
|
+
return error("malformedCiphertext", "IV or ciphertext is not valid base64url");
|
|
25
|
+
}
|
|
26
|
+
const r = attempt("decryptFailed", () => gcm(key, iv, utf8(aad)).decrypt(ct));
|
|
27
|
+
return r.ok ? ok(r.value) : error("decryptFailed", "Wrong key, wrong AAD, or tampered data");
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAC5C,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,MAAM,IAAI,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAEL,aAAa,EACb,WAAW,EACX,WAAW,EACX,KAAK,EACL,IAAI,GACL,MAAM,YAAY,CAAC;AACpB,OAAO,EAAmC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAElF,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,IAAW,EAAS,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;AAEhE,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAoB,EAAU,EAAE,CACxD,KAAK,CAAC,MAAM,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AAE9D,mBAAmB;AACnB,MAAM,CAAC,MAAM,U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,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9E,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,eAAe,EAAE,wCAAwC,CAAC,CAAC;AAC/F,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./result.ts";
|
|
2
|
+
export * from "./bytes.ts";
|
|
3
|
+
export * from "./crypto.ts";
|
|
4
|
+
export * from "./keys.ts";
|
|
5
|
+
export * from "./aad.ts";
|
|
6
|
+
export * from "./phrase.ts";
|
|
7
|
+
export * from "./tags.ts";
|
|
8
|
+
export * from "./appId.ts";
|
|
9
|
+
export * from "./vault.ts";
|
|
10
|
+
export * from "./check.ts";
|
|
11
|
+
export * from "./nostr/index.ts";
|
|
12
|
+
export * from "./settings.ts";
|
|
13
|
+
export * from "./passkeys.ts";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./result.js";
|
|
2
|
+
export * from "./bytes.js";
|
|
3
|
+
export * from "./crypto.js";
|
|
4
|
+
export * from "./keys.js";
|
|
5
|
+
export * from "./aad.js";
|
|
6
|
+
export * from "./phrase.js";
|
|
7
|
+
export * from "./tags.js";
|
|
8
|
+
export * from "./appId.js";
|
|
9
|
+
export * from "./vault.js";
|
|
10
|
+
export * from "./check.js";
|
|
11
|
+
export * from "./nostr/index.js";
|
|
12
|
+
export * from "./settings.js";
|
|
13
|
+
export * from "./passkeys.js";
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type Bytes } from "./bytes.ts";
|
|
2
|
+
/** Root key R and data secret S are 16 bytes (12 BIP-39 words). Signer master M is 32 bytes. */
|
|
3
|
+
export declare const ROOT_KEY_LENGTH = 16;
|
|
4
|
+
export declare const DATA_SECRET_LENGTH = 16;
|
|
5
|
+
export declare const MASTER_SECRET_LENGTH = 32;
|
|
6
|
+
/** First 8 bytes of SHA-256(S), hex (SPEC §6.1). */
|
|
7
|
+
export declare const sHash: (s: Bytes) => string;
|
|
8
|
+
/** Key for the `root` data wrap: HKDF(R, "evolustr/v1/data|" + appId) (SPEC §3.1). */
|
|
9
|
+
export declare const rootDataKey: (r: Bytes, appId: string) => Bytes;
|
|
10
|
+
/** Key for root-wrap tags (SPEC §3.1, #37): only holders of R can make a valid tag. */
|
|
11
|
+
export declare const wrapAuthKey: (r: Bytes, appId: string) => Bytes;
|
|
12
|
+
/** HMAC key the old R uses to authorise a root rotation (#52 phase C). */
|
|
13
|
+
export declare const rootRotateKey: (r: Bytes, appId: string) => Bytes;
|
|
14
|
+
/** PRF input shared by all client passkeys of one vault (SPEC §4.1). */
|
|
15
|
+
export declare const clientPrfInput: (pubkey: string, appId: string) => Bytes;
|
|
16
|
+
/** Wrap key from a client passkey's PRF output (SPEC §4.1). */
|
|
17
|
+
export declare const passkeyWrapKey: (prfOutput: Bytes, appId: string) => Bytes;
|
|
18
|
+
/** PRF input for a signer passkey (SPEC §5.3). */
|
|
19
|
+
export declare const signerPrfInput: (pubkey: string) => Bytes;
|
|
20
|
+
/** Wrap key for M from a signer passkey's PRF output. */
|
|
21
|
+
export declare const signerPasskeyWrapKey: (prfOutput: Bytes) => Bytes;
|
|
22
|
+
/** Wrap key for M from the nsec (`sk` wrap, SPEC §5.3). */
|
|
23
|
+
export declare const signerSkWrapKey: (secretKey: Bytes) => Bytes;
|
|
24
|
+
/** HMAC key for signer-vault wrap tags (#54): only a holder of M can tag a wrap. */
|
|
25
|
+
export declare const signerWrapAuthKey: (m: Bytes) => Bytes;
|
|
26
|
+
/** Public id of a signer master secret, kept in the signer vault (#52 phase B). */
|
|
27
|
+
export declare const signerMId: (m: Bytes) => string;
|
|
28
|
+
/** HMAC key the old M uses to authorise its successor (#52 phase B). */
|
|
29
|
+
export declare const signerRotateKey: (m: Bytes) => Bytes;
|
|
30
|
+
/** Key the new M seals the previous M with, so old app keys stay available (#52 phase B). */
|
|
31
|
+
export declare const signerPreviousKey: (m: Bytes) => Bytes;
|
|
32
|
+
/** Per-app key a capable signer gives the caller (SPEC §5.3). */
|
|
33
|
+
export declare const signerAppKey: (m: Bytes, appId: string) => Bytes;
|
|
34
|
+
/**
|
|
35
|
+
* A secret the host signer derives from M for its own use (§5.6), e.g. the key
|
|
36
|
+
* of its synced settings. Namespaced apart from app keys (`evolustr/v1|app|…`),
|
|
37
|
+
* so a label can never yield an app's key. Labels are non-empty and contain no
|
|
38
|
+
* `|`; the host checks that with `isHostSecretLabel`.
|
|
39
|
+
*/
|
|
40
|
+
export declare const hostSecret: (m: Bytes, label: string) => Bytes;
|
|
41
|
+
/** A valid label for `hostSecret`: 1–200 characters, no `|`. */
|
|
42
|
+
export declare const isHostSecretLabel: (label: string) => boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Label of a signer's wrap inside one app's vault (SPEC §5.3). Derived per app,
|
|
45
|
+
* so apps can't use it to link the same user across apps (#29).
|
|
46
|
+
*/
|
|
47
|
+
export declare const signerId: (m: Bytes, appId: string) => string;
|
|
48
|
+
//# sourceMappingURL=keys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAe,MAAM,YAAY,CAAC;AAGrD,gGAAgG;AAChG,eAAO,MAAM,eAAe,KAAK,CAAC;AAClC,eAAO,MAAM,kBAAkB,KAAK,CAAC;AACrC,eAAO,MAAM,oBAAoB,KAAK,CAAC;AAIvC,oDAAoD;AACpD,eAAO,MAAM,KAAK,MAAO,KAAK,KAAG,MAA0B,CAAC;AAE5D,sFAAsF;AACtF,eAAO,MAAM,WAAW,MAAO,KAAK,SAAS,MAAM,KAAG,KAChB,CAAC;AAEvC,uFAAuF;AACvF,eAAO,MAAM,WAAW,MAAO,KAAK,SAAS,MAAM,KAAG,KAAkD,CAAC;AAEzG,0EAA0E;AAC1E,eAAO,MAAM,aAAa,MAAO,KAAK,SAAS,MAAM,KAAG,KAAoD,CAAC;AAE7G,wEAAwE;AACxE,eAAO,MAAM,cAAc,WAAY,MAAM,SAAS,MAAM,KAAG,KACX,CAAC;AAErD,+DAA+D;AAC/D,eAAO,MAAM,cAAc,cAAe,KAAK,SAAS,MAAM,KAAG,KACnB,CAAC;AAE/C,kDAAkD;AAClD,eAAO,MAAM,cAAc,WAAY,MAAM,KAAG,KACE,CAAC;AAEnD,yDAAyD;AACzD,eAAO,MAAM,oBAAoB,cAAe,KAAK,KAAG,KACJ,CAAC;AAErD,2DAA2D;AAC3D,eAAO,MAAM,eAAe,cAAe,KAAK,KAAG,KACP,CAAC;AAE7C,oFAAoF;AACpF,eAAO,MAAM,iBAAiB,MAAO,KAAK,KAAG,KAAgD,CAAC;AAE9F,mFAAmF;AACnF,eAAO,MAAM,SAAS,MAAO,KAAK,KAAG,MAAqD,CAAC;AAE3F,wEAAwE;AACxE,eAAO,MAAM,eAAe,MAAO,KAAK,KAAG,KAA6C,CAAC;AAEzF,6FAA6F;AAC7F,eAAO,MAAM,iBAAiB,MAAO,KAAK,KAAG,KAA+C,CAAC;AAE7F,iEAAiE;AACjE,eAAO,MAAM,YAAY,MAAO,KAAK,SAAS,MAAM,KAAG,KAClB,CAAC;AAEtC;;;;;GAKG;AACH,eAAO,MAAM,UAAU,MAAO,KAAK,SAAS,MAAM,KAAG,KACf,CAAC;AAEvC,gEAAgE;AAChE,eAAO,MAAM,iBAAiB,UAAW,MAAM,KAAG,OACe,CAAC;AAElE;;;GAGG;AACH,eAAO,MAAM,QAAQ,MAAO,KAAK,SAAS,MAAM,KAAG,MACE,CAAC"}
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { toHex, utf8 } from "./bytes.js";
|
|
2
|
+
import { hkdf, sha256 } from "./crypto.js";
|
|
3
|
+
/** Root key R and data secret S are 16 bytes (12 BIP-39 words). Signer master M is 32 bytes. */
|
|
4
|
+
export const ROOT_KEY_LENGTH = 16;
|
|
5
|
+
export const DATA_SECRET_LENGTH = 16;
|
|
6
|
+
export const MASTER_SECRET_LENGTH = 32;
|
|
7
|
+
const short = (bytes) => toHex(bytes.slice(0, 8));
|
|
8
|
+
/** First 8 bytes of SHA-256(S), hex (SPEC §6.1). */
|
|
9
|
+
export const sHash = (s) => short(sha256(s));
|
|
10
|
+
/** Key for the `root` data wrap: HKDF(R, "evolustr/v1/data|" + appId) (SPEC §3.1). */
|
|
11
|
+
export const rootDataKey = (r, appId) => hkdf(r, `evolustr/v1/data|${appId}`);
|
|
12
|
+
/** Key for root-wrap tags (SPEC §3.1, #37): only holders of R can make a valid tag. */
|
|
13
|
+
export const wrapAuthKey = (r, appId) => hkdf(r, `evolustr/v1/wrap-auth|${appId}`);
|
|
14
|
+
/** HMAC key the old R uses to authorise a root rotation (#52 phase C). */
|
|
15
|
+
export const rootRotateKey = (r, appId) => hkdf(r, `evolustr/v1/root-rotate|${appId}`);
|
|
16
|
+
/** PRF input shared by all client passkeys of one vault (SPEC §4.1). */
|
|
17
|
+
export const clientPrfInput = (pubkey, appId) => sha256(utf8(`evolustr/v1/prf|${pubkey}|${appId}`));
|
|
18
|
+
/** Wrap key from a client passkey's PRF output (SPEC §4.1). */
|
|
19
|
+
export const passkeyWrapKey = (prfOutput, appId) => hkdf(prfOutput, `evolustr/v1/wrap|${appId}`);
|
|
20
|
+
/** PRF input for a signer passkey (SPEC §5.3). */
|
|
21
|
+
export const signerPrfInput = (pubkey) => sha256(utf8(`evolustr/v1/signer-prf|${pubkey}`));
|
|
22
|
+
/** Wrap key for M from a signer passkey's PRF output. */
|
|
23
|
+
export const signerPasskeyWrapKey = (prfOutput) => hkdf(prfOutput, "evolustr/v1/signer-passkey-wrap");
|
|
24
|
+
/** Wrap key for M from the nsec (`sk` wrap, SPEC §5.3). */
|
|
25
|
+
export const signerSkWrapKey = (secretKey) => hkdf(secretKey, "evolustr/v1/signer-wrap");
|
|
26
|
+
/** HMAC key for signer-vault wrap tags (#54): only a holder of M can tag a wrap. */
|
|
27
|
+
export const signerWrapAuthKey = (m) => hkdf(m, "evolustr/v1/signer-wrap-auth");
|
|
28
|
+
/** Public id of a signer master secret, kept in the signer vault (#52 phase B). */
|
|
29
|
+
export const signerMId = (m) => toHex(hkdf(m, "evolustr/v1/signer-mid", 8));
|
|
30
|
+
/** HMAC key the old M uses to authorise its successor (#52 phase B). */
|
|
31
|
+
export const signerRotateKey = (m) => hkdf(m, "evolustr/v1/signer-rotate");
|
|
32
|
+
/** Key the new M seals the previous M with, so old app keys stay available (#52 phase B). */
|
|
33
|
+
export const signerPreviousKey = (m) => hkdf(m, "evolustr/v1/signer-previous");
|
|
34
|
+
/** Per-app key a capable signer gives the caller (SPEC §5.3). */
|
|
35
|
+
export const signerAppKey = (m, appId) => hkdf(m, `evolustr/v1|app|${appId}`);
|
|
36
|
+
/**
|
|
37
|
+
* A secret the host signer derives from M for its own use (§5.6), e.g. the key
|
|
38
|
+
* of its synced settings. Namespaced apart from app keys (`evolustr/v1|app|…`),
|
|
39
|
+
* so a label can never yield an app's key. Labels are non-empty and contain no
|
|
40
|
+
* `|`; the host checks that with `isHostSecretLabel`.
|
|
41
|
+
*/
|
|
42
|
+
export const hostSecret = (m, label) => hkdf(m, `evolustr/v1|host|${label}`);
|
|
43
|
+
/** A valid label for `hostSecret`: 1–200 characters, no `|`. */
|
|
44
|
+
export const isHostSecretLabel = (label) => label.length > 0 && label.length <= 200 && !label.includes("|");
|
|
45
|
+
/**
|
|
46
|
+
* Label of a signer's wrap inside one app's vault (SPEC §5.3). Derived per app,
|
|
47
|
+
* so apps can't use it to link the same user across apps (#29).
|
|
48
|
+
*/
|
|
49
|
+
export const signerId = (m, appId) => toHex(hkdf(m, `evolustr/v1/signer-id|${appId}`, 8));
|
|
50
|
+
//# sourceMappingURL=keys.js.map
|
package/dist/keys.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE3C,gGAAgG;AAChG,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;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,EAAE,CAC1D,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AAElE;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,CAAQ,EAAE,KAAa,EAAU,EAAE,CAC1D,KAAK,CAAC,IAAI,CAAC,CAAC,EAAE,yBAAyB,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nostr/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nostr/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC"}
|