diff --git a/package.json b/package.json index 202bcd03..91925feb 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "@bitgo/blake2b": "^3.2.4", "@holochain/serialization": "^0.1.0-beta-rc.3", "@msgpack/msgpack": "^2.8.0", - "@tauri-apps/api": "^1.4.0", "emittery": "^1.0.1", "isomorphic-ws": "^5.0.0", "js-base64": "^3.7.5", diff --git a/src/api/app/types.ts b/src/api/app/types.ts index 5b6fd393..bc796e7c 100644 --- a/src/api/app/types.ts +++ b/src/api/app/types.ts @@ -1,6 +1,7 @@ import { UnsubscribeFunction } from "emittery"; import { AgentPubKey, + AppAuthenticationToken, AppInfo, CapSecret, CellId, @@ -14,6 +15,7 @@ import { Nonce256Bit, RoleName, Timestamp, + WebsocketConnectionOptions, ZomeName, } from "../../index.js"; @@ -282,3 +284,8 @@ export interface AppClient { ): Promise; networkInfo(args: AppNetworkInfoRequest): Promise; } + +export interface AppWebsocketConnectionOptions + extends WebsocketConnectionOptions { + token?: AppAuthenticationToken; +} diff --git a/src/api/app/websocket.ts b/src/api/app/websocket.ts index 652c0da5..0bed6caa 100644 --- a/src/api/app/websocket.ts +++ b/src/api/app/websocket.ts @@ -2,7 +2,7 @@ import Emittery, { UnsubscribeFunction } from "emittery"; import { omit } from "lodash-es"; import { AgentPubKey, CellId, RoleName } from "../../types.js"; -import { AppAuthenticationToken, AppInfo, CellType } from "../admin"; +import { AppInfo, CellType } from "../admin"; import { catchError, DEFAULT_TIMEOUT, @@ -13,7 +13,6 @@ import { Requester, requesterTransformer, Transformer, - WebsocketConnectionOptions, } from "../common.js"; import { AppCallZomeRequest, @@ -39,12 +38,11 @@ import { EnableCloneCellResponse, NetworkInfoRequest, NetworkInfoResponse, + AppWebsocketConnectionOptions, } from "./types.js"; import { getHostZomeCallSigner, getLauncherEnvironment, - signZomeCallElectron, - signZomeCallTauri, } from "../../environments/launcher"; import { decode, encode } from "@msgpack/msgpack"; import { @@ -161,10 +159,7 @@ export class AppWebsocket implements AppClient { * @param options - {@link (WebsocketConnectionOptions:interface)} * @returns A new instance of an AppWebsocket. */ - static async connect( - token: AppAuthenticationToken, - options: WebsocketConnectionOptions = {} - ) { + static async connect(options: AppWebsocketConnectionOptions = {}) { // Check if we are in the launcher's environment, and if so, redirect the url to connect to const env = getLauncherEnvironment(); @@ -180,7 +175,17 @@ export class AppWebsocket implements AppClient { } const client = await WsClient.connect(options.url, options.wsClientOptions); - await client.authenticate({ token }); + if (env?.APP_INTERFACE_TOKEN) { + await client.authenticate({ token: env.APP_INTERFACE_TOKEN }); + } else { + if (!options.token) { + throw new HolochainError( + "AppAuthenticationTokenMissing", + `unable to connect to Conductor API - no app authentication token provided.` + ); + } + await client.authenticate({ token: options.token }); + } const appInfo = await ( this.requester(client, "app_info", DEFAULT_TIMEOUT) as Requester< @@ -426,14 +431,7 @@ const callZomeTransform: Transformer< if (hostSigner) { return hostSigner.signZomeCall(request); } else { - const env = getLauncherEnvironment(); - if (!env) { - return signZomeCall(request); - } - if (env.FRAMEWORK === "electron") { - return signZomeCallElectron(request); - } - return signZomeCallTauri(request); + return signZomeCall(request); } }, output: (response) => decode(response), diff --git a/src/environments/launcher.ts b/src/environments/launcher.ts index 99e45559..9dec5bd2 100644 --- a/src/environments/launcher.ts +++ b/src/environments/launcher.ts @@ -1,15 +1,12 @@ -import { encode } from "@msgpack/msgpack"; -import { invoke } from "@tauri-apps/api/tauri"; -import { CallZomeRequest } from "../api"; +import { AppAuthenticationToken, CallZomeRequest } from "../api"; import { CallZomeRequestSigned, CallZomeRequestUnsigned } from "../api"; -import { getNonceExpiration, randomNonce } from "../api"; import { InstalledAppId } from "../types.js"; export interface LauncherEnvironment { APP_INTERFACE_PORT?: number; ADMIN_INTERFACE_PORT?: number; INSTALLED_APP_ID?: InstalledAppId; - FRAMEWORK?: "tauri" | "electron"; + APP_INTERFACE_TOKEN?: AppAuthenticationToken; } export interface HostZomeCallSigner { @@ -40,30 +37,6 @@ declare global { } } -type TauriByteArray = number[]; // Tauri requires a number array instead of a Uint8Array - -interface CallZomeRequestSignedTauri - extends Omit< - CallZomeRequestSigned, - "cap_secret" | "cell_id" | "provenance" | "nonce" - > { - cell_id: [TauriByteArray, TauriByteArray]; - provenance: TauriByteArray; - nonce: TauriByteArray; - expires_at: number; -} - -interface CallZomeRequestUnsignedTauri - extends Omit< - CallZomeRequestUnsigned, - "cap_secret" | "cell_id" | "provenance" | "nonce" - > { - cell_id: [TauriByteArray, TauriByteArray]; - provenance: TauriByteArray; - nonce: TauriByteArray; - expires_at: number; -} - interface CallZomeRequestSignedElectron extends Omit< CallZomeRequestSigned, @@ -101,75 +74,3 @@ interface CallZomeRequestUnsignedElectron nonce: Array; expiresAt: number; } - -export const signZomeCallTauri = async (request: CallZomeRequest) => { - const zomeCallUnsigned: CallZomeRequestUnsignedTauri = { - provenance: Array.from(request.provenance), - cell_id: [Array.from(request.cell_id[0]), Array.from(request.cell_id[1])], - zome_name: request.zome_name, - fn_name: request.fn_name, - payload: Array.from(encode(request.payload)), - nonce: Array.from(await randomNonce()), - expires_at: getNonceExpiration(), - }; - - const signedZomeCallTauri: CallZomeRequestSignedTauri = await invoke( - "sign_zome_call", - { zomeCallUnsigned } - ); - - const signedZomeCall: CallZomeRequestSigned = { - provenance: Uint8Array.from(signedZomeCallTauri.provenance), - cap_secret: null, - cell_id: [ - Uint8Array.from(signedZomeCallTauri.cell_id[0]), - Uint8Array.from(signedZomeCallTauri.cell_id[1]), - ], - zome_name: signedZomeCallTauri.zome_name, - fn_name: signedZomeCallTauri.fn_name, - payload: Uint8Array.from(signedZomeCallTauri.payload), - signature: Uint8Array.from(signedZomeCallTauri.signature), - expires_at: signedZomeCallTauri.expires_at, - nonce: Uint8Array.from(signedZomeCallTauri.nonce), - }; - - return signedZomeCall; -}; - -export const signZomeCallElectron = async (request: CallZomeRequest) => { - if (!window.electronAPI) { - throw Error( - "Unable to signZomeCallElectron. window.electronAPI not defined" - ); - } - - const zomeCallUnsignedElectron: CallZomeRequestUnsignedElectron = { - provenance: Array.from(request.provenance), - cellId: [Array.from(request.cell_id[0]), Array.from(request.cell_id[1])], - zomeName: request.zome_name, - fnName: request.fn_name, - payload: Array.from(encode(request.payload)), - nonce: Array.from(await randomNonce()), - expiresAt: getNonceExpiration(), - }; - - const zomeCallSignedElectron: CallZomeRequestSignedElectron = - await window.electronAPI.signZomeCall(zomeCallUnsignedElectron); - - const zomeCallSigned: CallZomeRequestSigned = { - provenance: Uint8Array.from(zomeCallSignedElectron.provenance), - cap_secret: null, - cell_id: [ - Uint8Array.from(zomeCallSignedElectron.cellId[0]), - Uint8Array.from(zomeCallSignedElectron.cellId[1]), - ], - zome_name: zomeCallSignedElectron.zomeName, - fn_name: zomeCallSignedElectron.fnName, - payload: Uint8Array.from(zomeCallSignedElectron.payload), - signature: Uint8Array.from(zomeCallSignedElectron.signature), - expires_at: zomeCallSignedElectron.expiresAt, - nonce: Uint8Array.from(zomeCallSignedElectron.nonce), - }; - - return zomeCallSigned; -};