@evolustr/react 0.0.0-stage → 0.1.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/README.md +31 -2
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -4
- package/src/index.ts +89 -0
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @evolustr/react
|
|
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
|
+
React bindings for [`@evolustr/client`](https://www.npmjs.com/package/@evolustr/client).
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { EvolustrProvider, useEvolustr, useSetting } from "@evolustr/react";
|
|
13
|
+
|
|
14
|
+
// Create the client once, outside render (see @evolustr/client).
|
|
15
|
+
<EvolustrProvider client={evolustr}>
|
|
16
|
+
<App />
|
|
17
|
+
</EvolustrProvider>;
|
|
18
|
+
|
|
19
|
+
function App() {
|
|
20
|
+
const { state, status, client, refresh, settle } = useEvolustr();
|
|
21
|
+
const theme = useSetting("theme");
|
|
22
|
+
if (state.kind === "connecting") return <p>Connecting…</p>;
|
|
23
|
+
if (state.kind === "needsUserAction") return <button onClick={async () => settle(await client.unlock())}>Unlock</button>;
|
|
24
|
+
// state.kind: "ready", "vaultChanged" or "error"
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- `EvolustrProvider` runs `connect()` once on mount.
|
|
29
|
+
- `useEvolustr()` gives the state, the current status, the client, `refresh()` (call it after an action such as `makeStrict()`) and `settle()` (pass it the result of `unlock`, `resolveVaultChange` or `restoreFromPhrase`).
|
|
30
|
+
- `useSetting(key)` reads a setting: the app's value, else the shared one, else the default.
|
|
31
|
+
|
|
32
|
+
More in the [project README](https://github.com/saintego/evolustr#readme). MIT licence.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React bindings for @evolustr/client: a provider that runs `connect()` once,
|
|
3
|
+
* and hooks for the connection state, actions and settings.
|
|
4
|
+
*/
|
|
5
|
+
import type { ConnectResult, Evolustr, Ready, Status } from "@evolustr/client";
|
|
6
|
+
import type { SettingKey, SharedSettings } from "@evolustr/core";
|
|
7
|
+
import { type ReactNode } from "react";
|
|
8
|
+
export type EvolustrState = {
|
|
9
|
+
readonly kind: "connecting";
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: "ready";
|
|
12
|
+
readonly ready: Ready;
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: "needsUserAction";
|
|
15
|
+
readonly action: Extract<ConnectResult, {
|
|
16
|
+
state: "needsUserAction";
|
|
17
|
+
}>["action"];
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: "vaultChanged";
|
|
20
|
+
readonly result: Extract<ConnectResult, {
|
|
21
|
+
state: "vaultChanged";
|
|
22
|
+
}>;
|
|
23
|
+
} | {
|
|
24
|
+
readonly kind: "error";
|
|
25
|
+
readonly message: string;
|
|
26
|
+
};
|
|
27
|
+
interface ContextValue {
|
|
28
|
+
readonly client: Evolustr;
|
|
29
|
+
readonly state: EvolustrState;
|
|
30
|
+
/** Re-reads status and settings after an action. */
|
|
31
|
+
readonly refresh: () => void;
|
|
32
|
+
/** Applies a result that may complete a pending state. */
|
|
33
|
+
readonly settle: (r: {
|
|
34
|
+
readonly ok: true;
|
|
35
|
+
readonly value: Ready;
|
|
36
|
+
} | {
|
|
37
|
+
readonly ok: false;
|
|
38
|
+
readonly error: {
|
|
39
|
+
readonly message: string;
|
|
40
|
+
};
|
|
41
|
+
}) => void;
|
|
42
|
+
readonly version: number;
|
|
43
|
+
}
|
|
44
|
+
/** Connects once on mount. Create `client` outside render (or memoize it). */
|
|
45
|
+
export declare const EvolustrProvider: (props: {
|
|
46
|
+
readonly client: Evolustr;
|
|
47
|
+
readonly children?: ReactNode;
|
|
48
|
+
}) => import("react").FunctionComponentElement<import("react").ProviderProps<ContextValue | null>>;
|
|
49
|
+
/** Connection state, current status, and the client for actions. */
|
|
50
|
+
export declare const useEvolustr: () => {
|
|
51
|
+
readonly state: EvolustrState;
|
|
52
|
+
readonly status: Status | null;
|
|
53
|
+
readonly client: Evolustr;
|
|
54
|
+
/** Call after an action (enrollPasskey, makeStrict, …) to re-render with the new status. */
|
|
55
|
+
readonly refresh: () => void;
|
|
56
|
+
/** Use with unlock / resolveVaultChange / restoreFromPhrase results. */
|
|
57
|
+
readonly settle: ContextValue["settle"];
|
|
58
|
+
};
|
|
59
|
+
/** `app ?? shared ?? default` for one setting, re-read after `refresh()`. */
|
|
60
|
+
export declare const useSetting: <K extends SettingKey>(key: K) => SharedSettings[K];
|
|
61
|
+
export {};
|
|
62
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/E,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAuF,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAE5H,MAAM,MAAM,aAAa,GACrB;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GAC/B;IA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AwBnG,CAAC;AAQF,oEAAoE;AACpE,eAAO,MAAM,WAAW,QAAO;IAC7B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1B,4FAA4F;IAC5F,QAAQ,CAAC,OAAO,EAAE,MAAM,IAAI,CAAC;IAC7B,wEAAwE;IACxE,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;CAKzC,CAAC;AAEF,6EAA6E;AAC7E,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,UAAU,OAAO,CAAC,KAAG,cAAc,CAAC,CAAC,CAIzE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createContext, createElement, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
2
|
+
const EvolustrContext = createContext(null);
|
|
3
|
+
const fromConnect = (r) => {
|
|
4
|
+
if (!r.ok)
|
|
5
|
+
return { kind: "error", message: r.error.message };
|
|
6
|
+
const v = r.value;
|
|
7
|
+
if (v.state === "ready")
|
|
8
|
+
return { kind: "ready", ready: v };
|
|
9
|
+
if (v.state === "needsUserAction")
|
|
10
|
+
return { kind: "needsUserAction", action: v.action };
|
|
11
|
+
return { kind: "vaultChanged", result: v };
|
|
12
|
+
};
|
|
13
|
+
/** Connects once on mount. Create `client` outside render (or memoize it). */
|
|
14
|
+
export const EvolustrProvider = (props) => {
|
|
15
|
+
const { client } = props;
|
|
16
|
+
const [state, setState] = useState({ kind: "connecting" });
|
|
17
|
+
const [version, setVersion] = useState(0);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
let cancelled = false;
|
|
20
|
+
setState({ kind: "connecting" });
|
|
21
|
+
void client.connect().then((r) => {
|
|
22
|
+
if (!cancelled)
|
|
23
|
+
setState(fromConnect(r));
|
|
24
|
+
});
|
|
25
|
+
return () => {
|
|
26
|
+
cancelled = true;
|
|
27
|
+
};
|
|
28
|
+
}, [client]);
|
|
29
|
+
const refresh = useCallback(() => setVersion((v) => v + 1), []);
|
|
30
|
+
const settle = useCallback((r) => {
|
|
31
|
+
setState(r.ok ? { kind: "ready", ready: r.value } : { kind: "error", message: r.error.message });
|
|
32
|
+
setVersion((v) => v + 1);
|
|
33
|
+
}, []);
|
|
34
|
+
const value = useMemo(() => ({ client, state, refresh, settle, version }), [client, state, refresh, settle, version]);
|
|
35
|
+
return createElement(EvolustrContext.Provider, { value }, props.children);
|
|
36
|
+
};
|
|
37
|
+
const useCtx = () => {
|
|
38
|
+
const ctx = useContext(EvolustrContext);
|
|
39
|
+
if (ctx === null)
|
|
40
|
+
throw new Error("useEvolustr must be used inside <EvolustrProvider>");
|
|
41
|
+
return ctx;
|
|
42
|
+
};
|
|
43
|
+
/** Connection state, current status, and the client for actions. */
|
|
44
|
+
export const useEvolustr = () => {
|
|
45
|
+
const ctx = useCtx();
|
|
46
|
+
void ctx.version;
|
|
47
|
+
return { state: ctx.state, status: ctx.client.status(), client: ctx.client, refresh: ctx.refresh, settle: ctx.settle };
|
|
48
|
+
};
|
|
49
|
+
/** `app ?? shared ?? default` for one setting, re-read after `refresh()`. */
|
|
50
|
+
export const useSetting = (key) => {
|
|
51
|
+
const ctx = useCtx();
|
|
52
|
+
void ctx.version;
|
|
53
|
+
return ctx.client.settings.get(key);
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAkB,MAAM,OAAO,CAAC;AAmB5H,MAAM,eAAe,GAAG,aAAa,CAAsB,IAAI,CAAC,CAAC;AAEjE,MAAM,WAAW,GAAG,CAAC,C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AAG,GAAG,MAAM,EAAE,CAAC;IACrB,KAAK,GAAG,CAAC,OAAO,CAAC;IACjB,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC;AACzH,CAAC,CAAC;AAEF,6EAA6E;AAC7E,MAAM,CAAC,MAAM,UAAU,GAAG,CAAuB,GAAM,EAAqB,EAAE;IAC5E,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC;IACrB,KAAK,GAAG,CAAC,OAAO,CAAC;IACjB,OAAO,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACtC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evolustr/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"source": "./src/index.ts",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"src",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@evolustr/client": "0.1.0",
|
|
20
|
+
"@evolustr/core": "0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/react": "19.3.3",
|
|
27
|
+
"@types/react-dom": "^19.3.0",
|
|
28
|
+
"react": "19.3.1",
|
|
29
|
+
"react-dom": "^19.3.1"
|
|
30
|
+
},
|
|
31
|
+
"description": "React bindings for @evolustr/client. Early alpha: not audited, formats may change.",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/saintego/evolustr.git",
|
|
37
|
+
"directory": "packages/react"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.build.json"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"nostr",
|
|
47
|
+
"evolu",
|
|
48
|
+
"local-first",
|
|
49
|
+
"e2ee",
|
|
50
|
+
"passkeys"
|
|
51
|
+
]
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React bindings for @evolustr/client: a provider that runs `connect()` once,
|
|
3
|
+
* and hooks for the connection state, actions and settings.
|
|
4
|
+
*/
|
|
5
|
+
import type { ConnectResult, Evolustr, Ready, Status } from "@evolustr/client";
|
|
6
|
+
import type { SettingKey, SharedSettings } from "@evolustr/core";
|
|
7
|
+
import { createContext, createElement, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from "react";
|
|
8
|
+
|
|
9
|
+
export type EvolustrState =
|
|
10
|
+
| { readonly kind: "connecting" }
|
|
11
|
+
| { readonly kind: "ready"; readonly ready: Ready }
|
|
12
|
+
| { readonly kind: "needsUserAction"; readonly action: Extract<ConnectResult, { state: "needsUserAction" }>["action"] }
|
|
13
|
+
| { readonly kind: "vaultChanged"; readonly result: Extract<ConnectResult, { state: "vaultChanged" }> }
|
|
14
|
+
| { readonly kind: "error"; readonly message: string };
|
|
15
|
+
|
|
16
|
+
interface ContextValue {
|
|
17
|
+
readonly client: Evolustr;
|
|
18
|
+
readonly state: EvolustrState;
|
|
19
|
+
/** Re-reads status and settings after an action. */
|
|
20
|
+
readonly refresh: () => void;
|
|
21
|
+
/** Applies a result that may complete a pending state. */
|
|
22
|
+
readonly settle: (r: { readonly ok: true; readonly value: Ready } | { readonly ok: false; readonly error: { readonly message: string } }) => void;
|
|
23
|
+
readonly version: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const EvolustrContext = createContext<ContextValue | null>(null);
|
|
27
|
+
|
|
28
|
+
const fromConnect = (r: Awaited<ReturnType<Evolustr["connect"]>>): EvolustrState => {
|
|
29
|
+
if (!r.ok) return { kind: "error", message: r.error.message };
|
|
30
|
+
const v = r.value;
|
|
31
|
+
if (v.state === "ready") return { kind: "ready", ready: v };
|
|
32
|
+
if (v.state === "needsUserAction") return { kind: "needsUserAction", action: v.action };
|
|
33
|
+
return { kind: "vaultChanged", result: v };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Connects once on mount. Create `client` outside render (or memoize it). */
|
|
37
|
+
export const EvolustrProvider = (props: { readonly client: Evolustr; readonly children?: ReactNode }) => {
|
|
38
|
+
const { client } = props;
|
|
39
|
+
const [state, setState] = useState<EvolustrState>({ kind: "connecting" });
|
|
40
|
+
const [version, setVersion] = useState(0);
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
let cancelled = false;
|
|
44
|
+
setState({ kind: "connecting" });
|
|
45
|
+
void client.connect().then((r) => {
|
|
46
|
+
if (!cancelled) setState(fromConnect(r));
|
|
47
|
+
});
|
|
48
|
+
return () => {
|
|
49
|
+
cancelled = true;
|
|
50
|
+
};
|
|
51
|
+
}, [client]);
|
|
52
|
+
|
|
53
|
+
const refresh = useCallback(() => setVersion((v) => v + 1), []);
|
|
54
|
+
const settle = useCallback<ContextValue["settle"]>((r) => {
|
|
55
|
+
setState(r.ok ? { kind: "ready", ready: r.value } : { kind: "error", message: r.error.message });
|
|
56
|
+
setVersion((v) => v + 1);
|
|
57
|
+
}, []);
|
|
58
|
+
|
|
59
|
+
const value = useMemo(() => ({ client, state, refresh, settle, version }), [client, state, refresh, settle, version]);
|
|
60
|
+
return createElement(EvolustrContext.Provider, { value }, props.children);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const useCtx = (): ContextValue => {
|
|
64
|
+
const ctx = useContext(EvolustrContext);
|
|
65
|
+
if (ctx === null) throw new Error("useEvolustr must be used inside <EvolustrProvider>");
|
|
66
|
+
return ctx;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** Connection state, current status, and the client for actions. */
|
|
70
|
+
export const useEvolustr = (): {
|
|
71
|
+
readonly state: EvolustrState;
|
|
72
|
+
readonly status: Status | null;
|
|
73
|
+
readonly client: Evolustr;
|
|
74
|
+
/** Call after an action (enrollPasskey, makeStrict, …) to re-render with the new status. */
|
|
75
|
+
readonly refresh: () => void;
|
|
76
|
+
/** Use with unlock / resolveVaultChange / restoreFromPhrase results. */
|
|
77
|
+
readonly settle: ContextValue["settle"];
|
|
78
|
+
} => {
|
|
79
|
+
const ctx = useCtx();
|
|
80
|
+
void ctx.version;
|
|
81
|
+
return { state: ctx.state, status: ctx.client.status(), client: ctx.client, refresh: ctx.refresh, settle: ctx.settle };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** `app ?? shared ?? default` for one setting, re-read after `refresh()`. */
|
|
85
|
+
export const useSetting = <K extends SettingKey>(key: K): SharedSettings[K] => {
|
|
86
|
+
const ctx = useCtx();
|
|
87
|
+
void ctx.version;
|
|
88
|
+
return ctx.client.settings.get(key);
|
|
89
|
+
};
|