Skip to content

Commit

Permalink
chores:fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KannuSingh committed Sep 21, 2024
1 parent f21fa30 commit 9e14919
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 79 deletions.
16 changes: 6 additions & 10 deletions packages/experimental/smart-session/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -28,15 +28,11 @@ export const useSmartSession = () => {
async (
smartSessionGrantPermissionsRequest: SmartSessionGrantPermissionsRequest
): Promise<SmartSessionGrantPermissionsResponse> => {
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
},
[]
)
Expand Down
65 changes: 33 additions & 32 deletions packages/experimental/smart-session/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SmartSessionGrantPermissionsResponse> {
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
}
}

Expand All @@ -70,6 +70,7 @@ export class AppKitSmartSessionControllerClient {
data: { keys: [cosignerKey, dAppKey] }
}
}

return request
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
29 changes: 18 additions & 11 deletions packages/experimental/smart-session/src/core/utils/TypeUtils.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>
}

// 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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -47,11 +53,11 @@ export type SmartSessionGrantPermissionsRequest = {
signer: Signer
permissions: {
type: string
data: Record<string, any>
data: Record<string, unknown>
}[]
policies: {
type: string
data: Record<string, any>
data: Record<string, unknown>
}[]
}

Expand All @@ -72,9 +78,10 @@ export type WalletGrantPermissionsResponse = SmartSessionGrantPermissionsRequest
export type SmartSessionGrantPermissionsResponse = {
permissions: {
type: string
data: Record<string, any>
data: Record<string, unknown>
}[]
context: string // context is set to `pci`
// Context is set to `pci`
context: string
}
//--Cosigner Types----------------------------------------------------------------------- //
export type AddPermissionRequest = SmartSessionGrantPermissionsRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -25,43 +25,39 @@ export async function sendCoSignerRequest<
TQueryParams extends Record<string, string> = Record<string, never>
>({
url,
data,
request,
queryParams = {} as TQueryParams,
headers,
transformRequest
}: {
url: string
data: TRequest
request: TRequest
queryParams?: TQueryParams
headers: Record<string, string>
transformRequest?: (data: TRequest) => unknown
}): Promise<TResponse> {
try {
const transformedData = transformRequest ? transformRequest(data) : data
const transformedData = transformRequest ? transformRequest(request) : request
const response = await axios.post<TResponse>(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 ------------------------------------------ //
Expand All @@ -86,7 +82,7 @@ export class WalletConnectCosigner {
{ projectId: string }
>({
url,
data: data,
request: data,
queryParams: { projectId: this.projectId },
headers: { 'Content-Type': 'application/json' }
})
Expand All @@ -99,7 +95,7 @@ export class WalletConnectCosigner {
const url = `${this.baseUrl}/${encodeURIComponent(address)}/context`
await sendCoSignerRequest<ActivatePermissionsRequest, never, { projectId: string }>({
url,
data: updateData,
request: updateData,
queryParams: { projectId: this.projectId },
headers: { 'Content-Type': 'application/json' }
})
Expand Down

0 comments on commit 9e14919

Please sign in to comment.