(feat): crypto migrations
Some checks failed
/ build-web (push) Failing after 6m47s
/ build-desktop (linux) (push) Failing after 7m0s
/ build-mobile (push) Failing after 9m3s
/ release (push) Has been skipped

This commit is contained in:
Alois 2026-07-05 21:45:44 +02:00
commit cd2c2f8167
17 changed files with 839 additions and 825 deletions

View file

@ -21,20 +21,47 @@ type CryptoContextType = {
export const context = createContext<CryptoContextType | undefined>(undefined);
export function createCryptoActions(
getApi: () => CryptoContextType | null | undefined,
): CryptoContextType {
const requireApi = () => {
const api = getApi();
if (!api) {
throw new Error("Crypto API not initialized");
}
return api;
};
return {
decrypt: (secret, input) => requireApi().decrypt(secret, input),
decryptText: (secret, ciphertext) =>
requireApi().decryptText(secret, ciphertext),
encrypt: (secret, input) => requireApi().encrypt(secret, input),
encryptText: (secret, plaintext) =>
requireApi().encryptText(secret, plaintext),
getSharedSecret: (ownPrivateKey, ownPublicKey, otherPublicKey) =>
requireApi().getSharedSecret(ownPrivateKey, ownPublicKey, otherPublicKey),
};
}
function ownedBytes(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
const out = new Uint8Array(bytes.byteLength);
out.set(bytes);
return out;
}
export default function Provider(props: { children: React.ReactNode }) {
return (
<context.Provider
value={{
decrypt: crypto.decrypt,
encrypt: crypto.encrypt,
decryptText: crypto.decryptText,
encryptText: crypto.encryptText,
getSharedSecret: crypto.getSharedSecret,
}}
>
{props.children}
</context.Provider>
);
const actions = createCryptoActions(() => ({
decrypt: async (secret, input) =>
ownedBytes(await crypto.decrypt(secret, input)),
decryptText: crypto.decryptText,
encrypt: async (secret, input) =>
ownedBytes(await crypto.encrypt(secret, input)),
encryptText: crypto.encryptText,
getSharedSecret: crypto.getSharedSecret,
}));
return <context.Provider value={actions}>{props.children}</context.Provider>;
}
export function useCrypto(): CryptoContextType {