(feat): crypto migrations
This commit is contained in:
parent
930663d495
commit
cd2c2f8167
17 changed files with 839 additions and 825 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue