UNPKG

@reown/appkit-siwe-react-native

Version:

#### 🔎 [Examples](https://github.com/reown-com/react-native-examples)

106 lines (103 loc) • 2.74 kB
import { proxy, ref, subscribe as sub } from 'valtio'; import { subscribeKey as subKey } from 'valtio/utils'; import { OptionsController } from '@reown/appkit-core-react-native'; // -- Types --------------------------------------------- // // -- State --------------------------------------------- // const state = proxy({ status: 'uninitialized' }); // -- Controller ---------------------------------------- // export const SIWEController = { state, subscribeKey(key, callback) { return subKey(state, key, callback); }, subscribe(callback) { return sub(state, () => callback(state)); }, _getClient() { if (!state._client) { throw new Error('SIWEController client not set'); } return state._client; }, async getNonce(address) { const client = this._getClient(); const nonce = await client.getNonce(address); this.setNonce(nonce); return nonce; }, async getSession() { try { const client = this._getClient(); const session = await client.getSession(); if (session?.address && session?.chainId) { this.setSession(session); this.setStatus('success'); } else { this.setSession(undefined); this.setStatus('ready'); } return session; } catch { return undefined; } }, createMessage(args) { const client = this._getClient(); const message = client.createMessage(args); this.setMessage(message); return message; }, async verifyMessage(args) { const client = this._getClient(); const isValid = await client.verifyMessage(args); return isValid; }, async signIn() { try { this.setStatus('loading'); const client = this._getClient(); const session = await client.signIn(); this.setStatus('success'); return session; } catch (error) { this.setStatus('error'); throw error; } }, async signOut() { const client = this._getClient(); await client.signOut(); this.setStatus('ready'); this.setSession(undefined); client.onSignOut?.(); }, onSignIn(args) { const client = this._getClient(); client.onSignIn?.(args); }, onSignOut() { const client = this._getClient(); client.onSignOut?.(); }, setSIWEClient(client) { state._client = ref(client); state.status = 'ready'; OptionsController.setIsSiweEnabled(client.options.enabled); }, setNonce(nonce) { state.nonce = nonce; }, setStatus(status) { state.status = status; }, setMessage(message) { state.message = message; }, setSession(session) { state.session = session; state.status = session ? 'success' : 'ready'; } }; //# sourceMappingURL=SIWEController.js.map