Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add siwx cloud auth storage #3221

Draft
wants to merge 4 commits into
base: chore/siwe-to-siwx-migration
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/laboratory/src/pages/library/multichain-siwx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AppKitButtons } from '../../components/AppKitButtons'
import { HuobiWalletAdapter, SolflareWalletAdapter } from '@solana/wallet-adapter-wallets'
import { MultiChainTestsEthersSolana } from '../../components/MultiChainTestsEthersSolana'
import { mainnet } from '@reown/appkit/networks'
import { DefaultSIWX } from '@reown/appkit-siwx'
import { CloudAuth, DefaultSIWX } from '@reown/appkit-siwx'

const networks = ConstantsUtil.AllNetworks

Expand All @@ -28,7 +28,9 @@ const modal = createAppKit({
},
termsConditionsUrl: 'https://reown.com/terms-of-service',
privacyPolicyUrl: 'https://reown.com/privacy-policy',
siwx: new DefaultSIWX()
siwx: new DefaultSIWX({
storage: new CloudAuth()
})
})

ThemeStore.setModal(modal)
Expand Down
101 changes: 101 additions & 0 deletions packages/siwx/src/storages/CloudAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
ApiController,
BlockchainApiController,
type SIWXMessage,
type SIWXSession
} from '@reown/appkit-core'
import type { SIWXStorage } from '../core/SIWXStorage.js'
import { ConstantsUtil, type CaipNetworkId } from '@reown/appkit-common'

export class CloudAuth implements SIWXStorage {
add(session: SIWXSession): Promise<void> {
return this.request('authenticate', {
message: session.message,
signature: session.signature,
clientId: BlockchainApiController.state.clientId
})
}

delete(_chainId: string, _address: string): Promise<void> {
return this.request('sign-out', undefined)
}

async get(chainId: CaipNetworkId, address: string): Promise<SIWXSession[]> {
try {
const siweSession = await this.request('me', undefined)
const siweCaipNetworkId = `eip155:${siweSession?.chainId}`

if (!siweSession || siweCaipNetworkId !== chainId || siweSession.address !== address) {
return []
}

const session: SIWXSession = {
data: {
accountAddress: siweSession.address,
chainId: siweCaipNetworkId
} as SIWXMessage.Data,
message: '',
signature: ''
}

return [session]
} catch {
return []
}
}

set(_sessions: SIWXSession[]): Promise<void> {
throw new Error('Set is not available for CloudAuth')
}

private async request<Key extends CloudAuth.RequestKey>(
key: Key,
params: CloudAuth.Requests[Key]['body']
): Promise<CloudAuth.Requests[Key]['response']> {
const response = await fetch(`${ConstantsUtil.W3M_API_URL}/auth/v1/${key}`, {
method: RequestMethod[key],
body: JSON.stringify(params),
headers: ApiController._getApiHeaders() satisfies { 'x-project-id': string }
})

if (response.headers.get('content-type')?.includes('application/json')) {
return response.json()
}

throw new Error(await response.text())
}
}

const RequestMethod = {
nonce: 'GET',
me: 'GET',
authenticate: 'POST',
'update-user-metadata': 'PATCH',
'sign-out': 'POST'
} satisfies { [key in CloudAuth.RequestKey]: CloudAuth.Requests[key]['method'] }

export namespace CloudAuth {
export type Request<Method extends 'GET' | 'POST' | 'PATCH', Params, Response> = {
method: Method
body: Params
response: Response
}

export type Requests = {
nonce: Request<'GET', undefined, { nonce: string }>
me: Request<'GET', undefined, { address: string; chainId: string }>
authenticate: Request<
'POST',
{
message: string
signature: string
clientId?: string | null
},
never
>
'update-user-metadata': Request<'PATCH', Record<string, unknown>, unknown>
'sign-out': Request<'POST', undefined, never>
}

export type RequestKey = keyof Requests
}
1 change: 1 addition & 0 deletions packages/siwx/src/storages/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './LocalStorage.js'
export * from './CloudAuth.js'