From 9e149190fa49d0205ff0b6142b3cdfe35a0f384b Mon Sep 17 00:00:00 2001 From: Karandeep Singh Date: Fri, 20 Sep 2024 21:53:57 -0400 Subject: [PATCH] chores:fix lint errors --- .../smart-session/exports/index.ts | 16 ++--- .../experimental/smart-session/src/client.ts | 65 ++++++++++--------- .../core/controller/SmartSessionController.ts | 4 +- .../smart-session/src/core/helper/index.ts | 2 +- .../smart-session/src/core/utils/TypeUtils.ts | 29 +++++---- .../src/core/utils/WalletConnectCosigner.ts | 42 ++++++------ 6 files changed, 79 insertions(+), 79 deletions(-) diff --git a/packages/experimental/smart-session/exports/index.ts b/packages/experimental/smart-session/exports/index.ts index ad8bc169f1..39c562ebc0 100644 --- a/packages/experimental/smart-session/exports/index.ts +++ b/packages/experimental/smart-session/exports/index.ts @@ -16,7 +16,7 @@ export type { export type { AppKitSmartSessionControllerClient } // -- Hooks ------------------------------------------------------------------- -export const useSmartSession = () => { +export function useSmartSession() { // Local state to store the latest smart session state const [permissions, setPermissions] = useState(SmartSessionController.state.permissions) const [permissionsContext, setPermissionsContext] = useState( @@ -28,15 +28,11 @@ export const useSmartSession = () => { async ( smartSessionGrantPermissionsRequest: SmartSessionGrantPermissionsRequest ): Promise => { - try { - const response = await SmartSessionController.grantPermissions( - smartSessionGrantPermissionsRequest - ) - return response - } catch (error) { - console.error('Error granting permissions:', error) - throw error - } + const response = await SmartSessionController.grantPermissions( + smartSessionGrantPermissionsRequest + ) + + return response }, [] ) diff --git a/packages/experimental/smart-session/src/client.ts b/packages/experimental/smart-session/src/client.ts index 5028e0442c..e9262ce1e6 100644 --- a/packages/experimental/smart-session/src/client.ts +++ b/packages/experimental/smart-session/src/client.ts @@ -5,52 +5,52 @@ import type { SmartSessionGrantPermissionsResponse, WalletGrantPermissionsResponse } from './core/utils/TypeUtils.js' -import { WalletConnectCosigner } from './core/utils/WalletConnectCosigner' +import { WalletConnectCosigner } from './core/utils/WalletConnectCosigner.js' import { ERROR_MESSAGES, validateRequest, validateSigner } from './core/helper/index.js' -// -- Client -------------------------------------------------------------------- // -// Constants for error messages +/* + * -- Client -------------------------------------------------------------------- // + * Constants for error messages + */ export class AppKitSmartSessionControllerClient { async grantPermissions( request: SmartSessionGrantPermissionsRequest ): Promise { - try { - validateRequest(request) - const projectId = OptionsController.state.projectId - const { activeCaipAddress } = ChainController.state + validateRequest(request) + const projectId = OptionsController.state.projectId + const { activeCaipAddress } = ChainController.state - const address = - activeCaipAddress && activeCaipAddress.startsWith('eip155:') ? activeCaipAddress : '' - if (!address) throw new Error(ERROR_MESSAGES.UNSUPPORTED_NAMESPACE) + const address = + activeCaipAddress && activeCaipAddress.startsWith('eip155:') ? activeCaipAddress : '' + if (!address) { + throw new Error(ERROR_MESSAGES.UNSUPPORTED_NAMESPACE) + } - validateSigner(request.signer) + validateSigner(request.signer) - const walletConnectCosigner = new WalletConnectCosigner(projectId) - const addPermissionResponse = await walletConnectCosigner.addPermission(address, request) + const walletConnectCosigner = new WalletConnectCosigner(projectId) + const addPermissionResponse = await walletConnectCosigner.addPermission(address, request) - const cosignerKey = this.getCosignerKey(addPermissionResponse.key) - this.updateRequestSigner(request, cosignerKey) + const cosignerKey = this.getCosignerKey(addPermissionResponse.key) + this.updateRequestSigner(request, cosignerKey) - const connectionControllerClient = ConnectionController._getClient('eip155') - const response = (await connectionControllerClient.grantPermissions( - request - )) as WalletGrantPermissionsResponse + const connectionControllerClient = ConnectionController._getClient('eip155') + const response = (await connectionControllerClient.grantPermissions( + request + )) as WalletGrantPermissionsResponse - if (!response) { - throw new Error(ERROR_MESSAGES.NO_RESPONSE_RECEIVED) - } + if (!response) { + throw new Error(ERROR_MESSAGES.NO_RESPONSE_RECEIVED) + } - await walletConnectCosigner.activatePermissions(address, { - pci: addPermissionResponse.pci, - ...response - }) + await walletConnectCosigner.activatePermissions(address, { + pci: addPermissionResponse.pci, + ...response + }) - return { - permissions: response.permissions, - context: response.context - } - } catch (error) { - throw error + return { + permissions: response.permissions, + context: response.context } } @@ -70,6 +70,7 @@ export class AppKitSmartSessionControllerClient { data: { keys: [cosignerKey, dAppKey] } } } + return request } } diff --git a/packages/experimental/smart-session/src/core/controller/SmartSessionController.ts b/packages/experimental/smart-session/src/core/controller/SmartSessionController.ts index 118890b94d..eecf898bd1 100644 --- a/packages/experimental/smart-session/src/core/controller/SmartSessionController.ts +++ b/packages/experimental/smart-session/src/core/controller/SmartSessionController.ts @@ -2,8 +2,8 @@ import { proxy, subscribe as sub } from 'valtio/vanilla' import type { SmartSessionGrantPermissionsRequest, SmartSessionGrantPermissionsResponse -} from '../utils/TypeUtils' -import { AppKitSmartSessionControllerClient } from '../../client' +} from '../utils/TypeUtils.js' +import { AppKitSmartSessionControllerClient } from '../../client.js' // -- Types --------------------------------------------- // export interface SmartSessionControllerState { diff --git a/packages/experimental/smart-session/src/core/helper/index.ts b/packages/experimental/smart-session/src/core/helper/index.ts index ffa06a295a..4e0e5cea24 100644 --- a/packages/experimental/smart-session/src/core/helper/index.ts +++ b/packages/experimental/smart-session/src/core/helper/index.ts @@ -1,4 +1,4 @@ -import type { Signer, SmartSessionGrantPermissionsRequest } from '../utils/TypeUtils' +import type { Signer, SmartSessionGrantPermissionsRequest } from '../utils/TypeUtils.js' export const ERROR_MESSAGES = { UNSUPPORTED_NAMESPACE: 'Unsupported namespace', diff --git a/packages/experimental/smart-session/src/core/utils/TypeUtils.ts b/packages/experimental/smart-session/src/core/utils/TypeUtils.ts index 2ff5c8e079..b69cfaa2e6 100644 --- a/packages/experimental/smart-session/src/core/utils/TypeUtils.ts +++ b/packages/experimental/smart-session/src/core/utils/TypeUtils.ts @@ -1,15 +1,19 @@ -// A wallet is the signer for these permissions -// `data` is not necessary for this signer type as the wallet is both the signer and grantor of these permissions +/* + * A wallet is the signer for these permissions + * `data` is not necessary for this signer type as the wallet is both the signer and grantor of these permissions + */ export type WalletSigner = { type: 'wallet' - data: {} + data: Record } // The types of keys that are supported for the following `key` and `keys` signer types. export type KeyType = 'secp256r1' | 'secp256k1' | 'ed25519' | 'schnorr' -// A signer representing a single key. -// "Key" types are explicitly secp256r1 (p256) or secp256k1, and the public keys are hex-encoded. +/* + * A signer representing a single key. + * "Key" types are explicitly secp256r1 (p256) or secp256k1, and the public keys are hex-encoded. + */ export type KeySigner = { type: 'key' data: { @@ -18,8 +22,10 @@ export type KeySigner = { } } -// A signer representing a multisig signer. -// Each element of `publicKeys` are all explicitly the same `KeyType`, and the public keys are hex-encoded. +/* + * A signer representing a multisig signer. + * Each element of `publicKeys` are all explicitly the same `KeyType`, and the public keys are hex-encoded. + */ export type MultiKeySigner = { type: 'keys' data: { @@ -47,11 +53,11 @@ export type SmartSessionGrantPermissionsRequest = { signer: Signer permissions: { type: string - data: Record + data: Record }[] policies: { type: string - data: Record + data: Record }[] } @@ -72,9 +78,10 @@ export type WalletGrantPermissionsResponse = SmartSessionGrantPermissionsRequest export type SmartSessionGrantPermissionsResponse = { permissions: { type: string - data: Record + data: Record }[] - context: string // context is set to `pci` + // Context is set to `pci` + context: string } //--Cosigner Types----------------------------------------------------------------------- // export type AddPermissionRequest = SmartSessionGrantPermissionsRequest diff --git a/packages/experimental/smart-session/src/core/utils/WalletConnectCosigner.ts b/packages/experimental/smart-session/src/core/utils/WalletConnectCosigner.ts index 946626981d..a1eb23c2df 100644 --- a/packages/experimental/smart-session/src/core/utils/WalletConnectCosigner.ts +++ b/packages/experimental/smart-session/src/core/utils/WalletConnectCosigner.ts @@ -1,11 +1,11 @@ /* eslint-disable max-classes-per-file */ import axios, { AxiosError } from 'axios' -import { ConstantsUtil } from './ConstantUtils' +import { ConstantsUtil } from './ConstantUtils.js' import type { ActivatePermissionsRequest, AddPermissionRequest, AddPermissionResponse -} from './TypeUtils' +} from './TypeUtils.js' // -- Custom Error Class --------------------------------------------------- // export class CoSignerApiError extends Error { @@ -25,43 +25,39 @@ export async function sendCoSignerRequest< TQueryParams extends Record = Record >({ url, - data, + request, queryParams = {} as TQueryParams, headers, transformRequest }: { url: string - data: TRequest + request: TRequest queryParams?: TQueryParams headers: Record transformRequest?: (data: TRequest) => unknown }): Promise { try { - const transformedData = transformRequest ? transformRequest(data) : data + const transformedData = transformRequest ? transformRequest(request) : request const response = await axios.post(url, transformedData, { params: queryParams, headers }) + return response.data } catch (error) { - handleAxiosError(error) - } -} - -// -- Helper for Axios Error Handling -------------------------------------- // -function handleAxiosError(error: unknown): never { - if (axios.isAxiosError(error)) { - const axiosError = error as AxiosError - if (axiosError.response) { - throw new CoSignerApiError( - axiosError.response.status, - JSON.stringify(axiosError.response.data) - ) - } else { - throw new CoSignerApiError(500, 'Network error') + if (axios.isAxiosError(error)) { + const axiosError = error as AxiosError + if (axiosError.response) { + throw new CoSignerApiError( + axiosError.response.status, + JSON.stringify(axiosError.response.data) + ) + } else { + throw new CoSignerApiError(500, 'Network error') + } } + throw error } - throw error } // -- WalletConnectCosigner Class ------------------------------------------ // @@ -86,7 +82,7 @@ export class WalletConnectCosigner { { projectId: string } >({ url, - data: data, + request: data, queryParams: { projectId: this.projectId }, headers: { 'Content-Type': 'application/json' } }) @@ -99,7 +95,7 @@ export class WalletConnectCosigner { const url = `${this.baseUrl}/${encodeURIComponent(address)}/context` await sendCoSignerRequest({ url, - data: updateData, + request: updateData, queryParams: { projectId: this.projectId }, headers: { 'Content-Type': 'application/json' } })