UNPKG

@reown/appkit-siwe-react-native

Version:

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

106 lines (105 loc) • 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppKitSIWEClient = void 0; var _appkitCoreReactNative = require("@reown/appkit-core-react-native"); var _appkitCommonReactNative = require("@reown/appkit-common-react-native"); var _ConstantsUtil = require("./utils/ConstantsUtil"); // -- Client -------------------------------------------------------------------- // class AppKitSIWEClient { constructor(siweConfig) { const { enabled = true, nonceRefetchIntervalMs = _ConstantsUtil.ConstantsUtil.FIVE_MINUTES_IN_MS, sessionRefetchIntervalMs = _ConstantsUtil.ConstantsUtil.FIVE_MINUTES_IN_MS, signOutOnAccountChange = true, signOutOnDisconnect = true, signOutOnNetworkChange = true, ...siweConfigMethods } = siweConfig; this.options = { // Default options enabled, nonceRefetchIntervalMs, sessionRefetchIntervalMs, signOutOnDisconnect, signOutOnAccountChange, signOutOnNetworkChange }; this.methods = siweConfigMethods; } async getNonce(address) { const nonce = await this.methods.getNonce(address); if (!nonce) { throw new Error('siweControllerClient:getNonce - nonce is undefined'); } return nonce; } async getMessageParams() { return (await this.methods.getMessageParams?.()) || {}; } createMessage(args) { const message = this.methods.createMessage(args); if (!message) { throw new Error('siweControllerClient:createMessage - message is undefined'); } return message; } async verifyMessage(args) { const isValid = await this.methods.verifyMessage(args); return isValid; } async getSession() { const session = await this.methods.getSession(); if (!session) { throw new Error('siweControllerClient:getSession - session is undefined'); } return session; } async signIn() { const { address } = _appkitCoreReactNative.AccountController.state; const nonce = await this.getNonce(address); if (!address) { throw new Error('An address is required to create a SIWE message.'); } const chainId = _appkitCommonReactNative.NetworkUtil.caipNetworkIdToNumber(_appkitCoreReactNative.NetworkController.state.caipNetwork?.id); if (!chainId) { throw new Error('A chainId is required to create a SIWE message.'); } const messageParams = await this.getMessageParams?.(); const message = this.createMessage({ address: `eip155:${chainId}:${address}`, chainId, nonce, version: '1', iat: messageParams?.iat || new Date().toISOString(), ...messageParams }); const signature = await _appkitCoreReactNative.ConnectionController.signMessage(message); const isValid = await this.verifyMessage({ message, signature }); if (!isValid) { throw new Error('Error verifying SIWE signature'); } const session = await this.getSession(); if (!session) { throw new Error('Error getting SIWE session'); } if (this.methods.onSignIn) { this.methods.onSignIn(session); } _appkitCoreReactNative.RouterUtil.navigateAfterNetworkSwitch(); return session; } async signOut() { this.methods.onSignOut?.(); return this.methods.signOut(); } } exports.AppKitSIWEClient = AppKitSIWEClient; //# sourceMappingURL=client.js.map