# Methods

Here are the methods provided by the Wepin Widget JavaScript SDK.

## getStatus

```javascript
await wepinSdk.getStatus()
```

It returns the lifecycle state value of WepinSDK.

### **Parameters**

* \<void>

### **Return value**

* `WepinLifeCycle` \<string>
  * `not_initialized`: Wepin is not initialized.
  * `initializing`: Wepin is being initialized.
  * `initialized`: Wepin initialization is complete.
  * `before_login`: Wepin is initialized, but the user is not logged in.
  * `login`: The user is logged in.
  * `login_before_register` : The user is logged in with email but not registered with Wepin.

### **Example**

```javascript
const status = await wepinSdk.getStatus()
```

## openWidget

```javascript
await wepinSdk.openWidget()
```

It shows the widget window. If a user is not logged in, the widget will not open. Therefore, you must log in to Wepin before using this method. To log in to Wepin, use the `loginWithUI` method or the `loginWepin` method from the `@wepin/login-js`.

### **Parameters**

* \<void>

### **Return Value**

* `<Promise>` \<void>

### **Example**

```javascript
await wepinSdk.openWidget()
```

## closeWidget

```javascript
wepinSdk.closeWidget() 
```

It closes the widget window. Logging out is not performed even if the window is closed.

### **Parameters**

* \<void>

### **Return Value**

* \<void>

### **Example**

```javascript
wepinSdk.closeWidget()
```

## loginWithUI

```javascript
await wepinSdk.loginWithUI({email}?)
```

It returns the information of the logged-in user. If there is no logged-in user, the Wepin widget will display the login page. To perform a login without a widget, use the loginWepin() method from @wepin/login-js.

### **Parameters**

* `email` \<string> **optional**\
  The email address of the user logging into Wepin.

### **Return Value**

* Promise \<IWepinUser>
  * `status` \<string> &#x20;

    Success status <'success'|'fail'>
  * `userInfo` \<object> **optional**
    * `userId` \<string>

      The user ID in Wepin
    * `email` \<string>\
      The email address of the logged-in user
    * `provider` \<string>

      &#x20;<'google'|'apple'|'naver'|'discord'|'email'|'external\_token'>
    * `use2FA` \<boolean>

      Whether 2FA is enabled for the user's wallet
  * `userStatus`: \<object>&#x20;
    * `loginStatus` \<string>

      <'complete' | 'pinRequired' | 'registerRequired'> \
      If the user's loginStatus is not 'complete', the user needs to register with Wepin.
    * `pinRequired` \<boolean> **optional**

      Whether the user's PIN number is required
  * `walletId` \<string>

    The wallet ID of the Wepin user
  * `token` \<object>\
    User's wepin token
    * `accessToken` \<string>\
      wepin accessToken
    * `refreshToken` \<string>\
      wepin refreshToken

### **Example**

```javascript
//without email
const userInfo = await wepinSdk.loginWithUI()
//with email
const userInfo = await wepinSdk.loginWithUI({email})
```

* response

```json
{
    "status": "success",
      "userInfo": {
        "userId": "120349034824234234",
        "email": "abc@gmail.com",
        "provider": "google",
        "use2FA": true,
      },
}
```

## getLoginSession

```javascript
await wepinSdk.getLoginSession(provToken?)
```

The `getLoginSession` method retrieves and refreshes the Firebase authentication token. If a previous token is provided, it updates the stored token before returning the latest authentication details.

### **Supported Version**

* Supported in version <mark style="color:orange;">**`0.0.33`**</mark> and later

### **Parameters**

* `provToken` \<object> **optional** \
  Previous Token
  * `firebaseToken` \<IFirebaseWepin>\
    Firebase login information
    * `provider` \<string>\
      Provider for Firebase Login
    * `idToken` \<string>\
      Wepin Firebase idToken
    * `refreshToken` \<string>\
      Wepin Firebase refreshToken
  * `wepinToken` \<IWepinToken>\
    User's Wepin Token
    * `accessToken` \<string>\
      Wepin  Access Token
    * `refreshToken` \<string>\
      Wepin Refresh Token

### **Return Value**

* Promise \<object>
  * `firebaseToken` \<IFirebaseWepin>\
    Firebase login information
    * `provider` \<string>\
      Provider for Firebase Login
    * `idToken` \<string>\
      Wepin Firebase idToken
    * `refreshToken` \<string>\
      Wepin Firebase refreshToken
  * `wepinToken` \<IWepinToken>\
    User's Wepin Token
    * `accessToken` \<string>\
      Wepin  Access Token
    * `refreshToken` \<string>\
      Wepin Refresh Token

### **Example**

```javascript
//without parameter. get prevToken
const prevToken = await wepinSdk.getLoginSession()
//with parameter. refresh Firebase Token.
const loginToken = await wepinSdk.getLoginSession(prevToken)
```

* respnse

```json
{
  "firebaseToken": {
    "provider": "google",
    "idToken": "eyJhbGci...",
    "refreshToken": "AMf-vBwvHUYdt5..."
  },
  "wepinToken": {
    "accessToken": "eyJhbGciOiJS...",
    "refreshToken": "eyJhbGciOiJ..."
  }
}
```

## register

```javascript
await wepinSdk.register()
```

It registers the user with Wepin. After registration and login, the registration page of the Wepin widget opens, and registration (wallet creation and account creation) proceeds in the Wepin service. This feature is only available when the WepinSDK's `WepinLifeCycle` is login\_before\_register. After calling the `loginWepin()` method from `@wepin/login-js`, if the loginStatus value in userStatus is not 'complete', this method should be called.

### **Parameters**

* `<void>`

### **Return Value**

* Promise \<IWepinUser>
  * `status` \<string> &#x20;

    Success status <'success'|'fail'>
  * `userInfo` \<object> **optional**
    * `userId` \<string>

      The user ID in Wepin
    * `email` \<string>

      The email address of the logged-in user
    * `provider` \<string>

      <'google'|'apple'|'naver'|'discord'|'email'|'external\_token'>
    * `use2FA` \<boolean>

      Whether 2FA is enabled for the user's wallet
  * `userStatus`: \<object>&#x20;
    * `loginStatus` \<string>

      <'complete' | 'pinRequired' | 'registerRequired'>&#x20;

      If the user's loginStatus is not 'complete', the user needs to register with Wepin.
    * `pinRequired` \<boolean> **optional**

      Whether the user's PIN number is required.
  * `walletId` \<string>

    The wallet ID of the Wepin user
  * `token` \<object>\
    User's wepin token
    * `accessToken` \<string>\
      wepin accessToken
    * `refreshToken` \<string>\
      wepin refreshToken

### **Example**

```javascript
const userInfo = await wepinSdk.register()
```

## registerUserEmail

```javascript
await wepinSdk.registerUserEmail(param)
```

The `registerUserEmail` function registers an email for accounts from OAuth providers that do not already have an email associated with them.

### **Supported Version**

* Supported in version <mark style="color:orange;">**`0.0.18`**</mark> and later

### **Parameters**

* `provider` \<LoginProviders> \
  Provider for Firebase login. The value must be one of the supported login provider names in lowercase, such as 'google', 'naver', 'discord', 'apple', 'facebook', or 'line'. Please refer to [Wepin Social Login Auth Provider documentation](https://docs.wepin.io/en/login/social-login-auth-providers) to check the supported login providers.
* `idToken` \<string> \
  id token value to be used for login
* `accessToken` \<string> \
  access token value to be used for login

### **Return Value**

* `Promise` \<IWepinUser>
  * `status` \<string> &#x20;

    Success status <'success'|'fail'>
  * `userInfo` \<object> **optional**
    * `userId` \<string>

      The user ID in Wepin
    * `email` \<string>

      The email address of the logged-in user
    * `provider` \<string>

      <'google'|'apple'|'naver'|'discord'|'email'|'external\_token'>
    * `use2FA` \<boolean>

      Whether 2FA is enabled for the user's wallet
  * `userStatus`: \<object>&#x20;
    * `loginStatus` \<string>

      <'complete' | 'pinRequired' | 'registerRequired'>&#x20;

      If the user's loginStatus is not 'complete', the user needs to register with Wepin.
    * `pinRequired` \<boolean> **optional**

      Whether the user's PIN number is required.
  * `walletId` \<string>

    The wallet ID of the Wepin user
  * `token` \<object>\
    User's wepin token
    * `accessToken` \<string>\
      wepin accessToken
    * `refreshToken` \<string>\
      wepin refreshToken

### **Example**

```javascript
await wepinSdk.registerUserEmail({
    provider: 'google',
    idToken: 'google-idToken',
})
```

## logout

```javascript
await wepinSdk.logout()
```

It logs out the Wepin user.&#x20;

### **Parameters**

* \<void>

### **Return Value**

* `Promise` \<void>

### **Example**

```javascript
await wepinSdk.logout()
```

## getAccounts

```javascript
await wepinSdk.getAccounts()
or
await wepinSdk.getAccounts(options?)
```

It returns the account information (network and address) available in the app for the user.

This function can only be used after logging into Wepin.

If the `options` parameter is not provided, information on all user accounts is returned.

### **Parameters**

* `options` \<object> **optional**
  * `networks` \<string\[]> **optional**\
    It's the networks of the accounts to to be returned.\
    The blockchain networks that can be put in `networks` can be found on **the supported blockchain page**.
  * `withEoa` \<boolean> **optional**\
    Whether to include EOA accounts if there are AA accounts.

{% content-ref url="../../../wepin/supported-blockchains" %}
[supported-blockchains](https://docs.wepin.io/en/wepin/supported-blockchains)
{% endcontent-ref %}

### **Return Value**

If the user is logged in, account information `Account[]` of the network is returned.

* `Promise` \<Account\[]>
  * address \<string>\
    The address of the usre account
  * network \<string>\
    The network type of the user account
  * contract \<string> **optional** \
    The contract address of the token
  * isAA \<boolean> **optional** \
    Whether it is an AA account

### **Example**

```javascript
const result = await wepinSdk.getAccounts({
  networks: ['Ethereum'], 
  withEoa: true
})
```

* response

```javascript
[
  {
    "address": "0x0000001111112222223333334444445555556666",
    "network": "Ethereum",
  },
  {
    "address": "0x0000001111112222223333334444445555556666",
    "network": "Ethereum",
    "contract": "0x777777888888999999000000111111222222333333",
  },
  {
    "address": "0x4444445555556666000000111111222222333333",
    "network": "Ethereum",
    "isAA": true,
  },
]

```

## getBalance

```javascript
await wepinSdk.getBalance(accounts)
or
await wepinSdk.getBalance()
```

It returns the balance information of the account. This function can only be used after logging into Wepin.

If the `accounts` parameter is not provided, the balance of all user accounts is returned.

### **Parameters**

* `accounts` \<Account\[]> **optional**
  * `network` \<string>\
    The network type of the user account to query the balance
  * `address` \<string>

    The address of the user account to query the balance
  * `isAA` \<boolean> **optional**&#x20;

    Whether it is an AA account

### **Return Value**

* `Promise` \<AccountBalanceInfo\[]>
  * `network` \<string>

    The network type of the user account
  * `address` \<string>

    The address of the user account
  * `symbol` \<string>&#x20;

    The network symbol
  * `balance` \<string>&#x20;

    The amount of network coins held
  * `tokens` \<TokenBalanceInfo\[]>&#x20;
    * `symbol` \<string>&#x20;

      The token symbol
    * `balance` \<string>&#x20;

      The amount of tokens held
    * `contract` \<string>&#x20;

      The token contract address

### **Example**

```javascript
const result = await wepinSdk.getBalance([{
  address: '0x0000001111112222223333334444445555556666',
  network: 'Ethereum',
}])
```

* response

```
[
    {
        "symbol": "ETH",
            "balance": "1.1",
        "tokens":[
            {
                "contract": "0x123...213",
                "symbol": "TEST",
                "balance": "10"
            },
        ]
    }
]
```

## send

```javascript
await wepinSdk.send({account, txData?})
```

It performs the send function using the widget and returns the ID information of the send transaction. This function can only be used after logging into Wepin.

### **Parameters**

* `account` \<Account>&#x20;

  The sender's account information

  * `network` \<string>&#x20;

    The network type to send
  * `address` \<string>&#x20;

    The address of the account to send
* `txData` \<object> **optional**
  * `to` \<string>&#x20;

    The address to send to
  * `amount` \<string> &#x20;

    The amount to send

### **Return Value**

* `Promise` \<object>

  * `txId` \<string>

  &#x20;      The TXID of the send transaction

### **Example**

```javascript
const result = await wepinSdk.send({
    account: {
        address: '0x0000001111112222223333334444445555556666',
        network: 'Ethereum',
    },
    txData: {
        to: '0x9999991111112222223333334444445555556666',
        amount: '0.1',
    }
})
```

* response

```json
{
    "txId": "0x76bafd4b700ed959999d08ab76f95d7b6ab2249c0446921c62a6336a70b84f32"
}
```

## finalize

```javascript
wepinSdk.finalize() 
```

It terminates the use of WepinSDK. The `WepinLifeCycle` changes to `not_initialized`.

### **Return Value**

* `void`

### **Example**

```javascript
wepinSdk.finalize()
```
