Skip to content

Commit

Permalink
decoupling zome call signer logic from js-client
Browse files Browse the repository at this point in the history
  • Loading branch information
matthme committed Jan 26, 2024
1 parent a435f9e commit d915eac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/api/app/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { decode, encode } from "@msgpack/msgpack";
import Emittery from "emittery";
import nacl from "tweetnacl";
import {
getHostZomeCallSigner,
getLauncherEnvironment,
isLauncher,
signZomeCallTauri,
Expand Down Expand Up @@ -193,10 +194,13 @@ const callZomeTransform: Transformer<
if ("signature" in request) {
return request;
}
const signedZomeCall = isLauncher
? await signZomeCallTauri(request)
: await signZomeCall(request);
return signedZomeCall;
const hostSigner = getHostZomeCallSigner();
if (hostSigner) {
return hostSigner.signZomeCall(request);
} else if (isLauncher) {
return signZomeCallTauri(request);
}
return signZomeCall(request);
},
output: (response) => decode(response),
};
Expand Down
9 changes: 9 additions & 0 deletions src/environments/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ export interface LauncherEnvironment {
INSTALLED_APP_ID?: InstalledAppId;
}

export interface HostZomeCallSigner {
signZomeCall: (request: CallZomeRequest) => Promise<CallZomeRequestSigned>;
}

const __HC_LAUNCHER_ENV__ = "__HC_LAUNCHER_ENV__";
const __HC_ZOME_CALL_SIGNER__ = "__HC_ZOME_CALL_SIGNER__";

export const isLauncher =
typeof window === "object" && __HC_LAUNCHER_ENV__ in window;

export const getLauncherEnvironment = (): LauncherEnvironment | undefined =>
isLauncher ? window[__HC_LAUNCHER_ENV__] : undefined;

export const getHostZomeCallSigner = (): HostZomeCallSigner | undefined =>
globalThis.window && globalThis.window[__HC_ZOME_CALL_SIGNER__];

declare global {
interface Window {
[__HC_LAUNCHER_ENV__]: LauncherEnvironment | undefined;
[__HC_ZOME_CALL_SIGNER__]?: HostZomeCallSigner;
}
}

Expand Down

0 comments on commit d915eac

Please sign in to comment.