# Web SDK

Ultiverse SDK allows you to easily integrate your app with the Ultiverse account system. We are committed to providing you with a pleasant access experience!

## Getting Started

### Step1: Include Ultiverse SDK Script

Import Ultiverse SDK to your project:

```bash
yarn add @ultiverse/auth
```

### Step2: Setup Ultiverse Account Auth Instance

```typescript
import { setup } from '@ultiverse/auth';

// 2. Setup Ultiverse Account Auth Instance
setup({
  client_id: 'xxxxxx',
  state: 'xxxxxx',
});

```

## API Reference

### setup(options: SetupOptions)

```typescript
import { setup } from '@ultiverse/auth';

interface SetupOptions {
  project_id: string;
  client_key: string;
  app_id: string;
  client_id: string;
  state: string;
  chain_id?: 1 | 5 | 56 | 97;
  env?: 'development' | 'production';
}

setup({
  project_id: 'xxxxxx',
  client_key: 'xxxxxx',
  app_id: ‘xxxxxx',
  client_id: 'xxxxxx',
  state: 'xxxxxx',
  chain_id: 56,
  env: 'development'
});
```

### redirectToAuth(redirect\_url?: string)

```typescript
import { redirectToAuth } from '@ultiverse/auth';

redirectToAuth();
```

The page will redirect to:

```
https://auth-test.ultiverse.io/?
clientId=client_id&state=you_state&redirect_url=redirect_url
```

### loginWallet(wallet\_access\_token: string)

```typescript
import { loginWallet } from '@ultiverse/auth';

const url = new URL(window.location.href);
const wallet_access_token = url.searchParams.get('wallet_access_token');

const { info, err } = await loginWallet(wallet_access_token);
```

### getWalletInfo()

```typescript
import { getWalletInfo } from '@ultiverse/auth';

const { info, err } = getWalletInfo();
```

### getWalletProvider(userInfo: UserInfo)

Please get user info from your server API: [document](https://docs.ultiverse.dev/ultiverse-account/getting-user-profile)

```typescript
import { getWalletProvider } from '@ultiverse/auth';

const userInfo = await fetch('/api/user/info').then(res => res.json());

const { provider, err } = getWalletProvider(userInfo);
```

Use wallet provider by web3.js or ethers:

```typescript
//if you use web3.js
import Web3 from 'web3';
window.web3 = new Web3(particleProvider);
window.web3.currentProvider.isParticleNetwork // => true

//if you use ethers.js
import { ethers } from 'ethers';
const ethersProvider = new ethers.providers.Web3Provider(particleProvider, 'any');
const ethersSigner = ethersProvider.getSigner();
```

### logoutWallet()

```typescript
import { logoutWallet } from '@ultiverse/auth';

await logoutWallet();
```
