@reown/appkit-siwe-react-native
Version:
#### 🔎 [Examples](https://github.com/reown-com/react-native-examples)
111 lines (108 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SIWEController = void 0;
var _valtio = require("valtio");
var _utils = require("valtio/utils");
var _appkitCoreReactNative = require("@reown/appkit-core-react-native");
// -- Types --------------------------------------------- //
// -- State --------------------------------------------- //
const state = (0, _valtio.proxy)({
status: 'uninitialized'
});
// -- Controller ---------------------------------------- //
const SIWEController = exports.SIWEController = {
state,
subscribeKey(key, callback) {
return (0, _utils.subscribeKey)(state, key, callback);
},
subscribe(callback) {
return (0, _valtio.subscribe)(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 = (0, _valtio.ref)(client);
state.status = 'ready';
_appkitCoreReactNative.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