# Methods

## getStatus

Returns the lifecycle status of the WepinSDK.

### Parameters

* None

### Return Value

* CompletableFutuer\<WepinLifeCycle>
  * NOT\_INITIALIZED: WepinSDK has not been initialized.
  * INITIALIZING: WepinSDK is in the process of initializing.
  * INITIALIZED: WepinSDK has been successfully initialized.료
  * BEFORE\_LOGIN: WepinSDK is initialized, but the user is not logged in.
  * LOGIN: The user is logged in and registered with Wepin.
  * LOGIN\_BEFORE\_REGISTER: The user is logged in but not registered with Wepin.

### Example

{% tabs %}
{% tab title="Java" %}

<pre class="language-java"><code class="lang-java"><strong>CompletableFuture&#x3C;WepinLifeCycle> res = wepinWidget.getStatus()
</strong>res.whenComplete((state, error) -> {
    if (error ==  null) {
        Log.d(TAG, "Wepin SDK Lifecycle is $state");
    } else {
        Log.d(TAG, "loginWithUI error: $error");
    }
});
</code></pre>

{% endtab %}

{% tab title="Kotlin" %}

<pre class="language-kotlin"><code class="lang-kotlin"><strong>val res = wepinWidget.getStatus()
</strong>res?.whenComplete { state, error ->
    if (error == null) {
        Log.d(TAG, "Wepin SDK Lifecycle is $state")
    } else {
        Log.d(TAG, "getStatus error: $error")
    }
</code></pre>

{% endtab %}
{% endtabs %}

***

## loginWithUI

The `loginWithUI()` method provides the functionality to log in using a widget and returns the information of the logged-in user. If a user is already logged in, the widget will not be displayed, and the method will directly return the logged-in user's information. To perform a login without the widget, use the `loginWepin()` method from the `login` variable instead.

{% hint style="warning" %}
This method can only be used after the authentication key has been deleted from the [Wepin Workspace](https://workspace.wepin.io/). (Wepin Workspace > Development Tools menu > Login tab > Auth Key > Delete)

> The Auth Key menu is visible only if an authentication key was previously generated.
> {% endhint %}

### Parameters

* `context` \<Context>\
  The `Context` parameter is required in Android as it provides access to application-specific resources and classes, as well as information about the application environment. It is used to  start new activities, access application assets, retrieve system service, and more.
* `loginProviders` \<List\<LoginProvider>>\
  An array of login providers to configure the widget. If an empty array is provided, only the email login function is available.
  * `provider`\<String>\
    The OAuth login provider (e.g., 'google', 'naver', 'discord', 'apple').
  * `clientId`\<String>\
    The client ID of the OAuth login provider.
* `email`\<String> *optional*\
  The `email` parameter allows users to log in using the specified email address when logging in through the widget.

{% hint style="info" %}
To use OAuth login functionality (e.g., `loginWithUI`), you need to configure OAuth login providers. You must first register your OAuth login provider information in the [Wepin Workspace](https://workspace.wepin.io/). For more details on setting up OAuth providers, refer to the [Social Login OAuth Providers documentation](https://docs.wepin.io/en/login/social-login-auth-providers).
{% endhint %}

### Return Value

* CompletableFuture\<WepinUser>
  * `status` \<String>\
    Indicates success or failure ('success'|'fail').
  * `userInfo` \<WepinUserInfo> *optional -* User Information
    * `userId` \<String>\
      Wepin user ID
    * `email` \<String>\
      The email address of the user logged in to Wepin
    * `provider` \<String>\
      Login provider <'google'|'apple'|'naver'|'discord'|'email'|'external\_toekn'>
    * `use2FA` \<Boolean>\
      inticates whether 2FA is enabled on the user's wallet.
  * `userStatus` \<WepinUserStatus> - User status.
    * `loginStatus` \<String>\
      Login status <'complete'|'pinRequired'|'registerRequired'>
    * `pinRequired` \<Boolean> *optional*\
      indicates if a PIN is required.
  * `walletId` \<String> *optional* - Wepin user's wallet ID
  * `token` \<WepinToken> - Wepin token information
    * `accessToken` \<String>\
      Wepin Access Token
    * `refreshToken` \<String>\
      Wepin Refresh Token&#x20;

### Example

{% tabs %}
{% tab title="Java" %}

```java
Context context = this;
List<LoginProviderInfo> providers = List.of(new LoginProviderInfo("google", "GOOGLE_CLIENT_ID"));
CompletableFuture<WepinUser> res = wepinWidget.loginWithUI(context, providers, null);
res.whenComplete((user, error) -> {
    if (error ==  null) {
        Log.d(TAG, "Wepin User is $user");
    } else {
        Log.d(TAG, "loginWithUI error: $error");
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.loginWithUI(context, providers, email)
res?.whenComplete { wepinUser, error ->
    if (error == null) {
        Log.d(TAG, "Wepin User is $wepinUser")
    } else {
        Log.d(TAG, "loginWithUI error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## openWidget

Opens the widget window. If the user is not logged in, the widget window will not open. Therefore, the user must be logged in to Wepin before calling `openWidget`. To log in to Wepin, use the `loginWithUI` or `loginWepin` method from the `login` variable.

### Parameters

* context \<Context>\
  The `Context` parameter is required in Android as it provides access to application-specific resources and classes, as well as information about the application environment. It is used to  start new activities, access application assets, retrieve system service, and more.

### Return Value

* CompletableFuture\<Boolean>

### Example

{% tabs %}
{% tab title="Java" %}

```java
Context context = this;
CompletableFuture<Boolean> res = wepinWidget.openWidget(context);
res.whenComplete((result, error) -> {
    if (error ==  null) {
        Log.d(TAG, "openWidget Result is $user");
    } else {
        Log.d(TAG, "openWidget error: $error");
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.openWidget(context)
res?.whenComplete { result, error ->
    if (error == null) {
        Log.d(TAG, "openWidget result is $result")
    } else {
        Log.d(TAG, "openWidget error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## closeWidget

Closes the widget window. Closing the window does not log out the user.

### Parameters

* None

### Return Value

* None

### Example

{% tabs %}
{% tab title="Java" %}

<pre class="language-java"><code class="lang-java"><strong>wepinWidget.closeWidget();
</strong></code></pre>

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
wepinWidget.closeWidget()
```

{% endtab %}
{% endtabs %}

***

## register

Registers the user with Wepin. After signing up and logging in, the Wepin widget’s registration page opens, allowing the user to register (create a wallet and account) with Wepin services. This function can only be used when `WepinLifeCycle` is `loginBeforeRegister`. After calling the `loginWepin()` method from the `login` variable, if the `loginStatus` value in `userStatus` is not 'complete', this method should be called.

### Parameters

* context \<Context>\
  Represents the location of a widget in the widget tree. It is used to locate the widget's position in the tree and to provide various functions like navigation and accessing theme data. Pass the current context when calling `register` to ensure the widget is displayed in the correct part of the UI hierarchy.

### Return Value

* CompletableFuture\<WepinUser>
  * `status` \<String>\
    Indicates success or failure ('success'|'fail').
  * `userInfo` \<WepinUserInfo> *optional -* User Information
    * `userId` \<String>\
      Wepin user ID
    * `email` \<String>\
      The email address of the user logged in to Wepin
    * `provider` \<String>\
      Login provider <'google'|'apple'|'naver'|'discord'|'email'|'external\_toekn'>
    * `use2FA` \<Boolean>\
      inticates whether 2FA is enabled on the user's wallet.
  * `userStatus` \<WepinUserStatus> - User status.
    * `loginStatus` \<String>\
      Login status <'complete'|'pinRequired'|'registerRequired'>
    * `pinRequired` \<Boolean> *optional*\
      indicates if a PIN is required.
  * `walletId` \<String> *optional* - Wepin user's wallet ID
  * `token` \<WepinToken> - Wepin token information
    * `accessToken` \<String>\
      Wepin Access Token
    * `refreshToken` \<String>\
      Wepin Refresh Token&#x20;

### Example

{% tabs %}
{% tab title="Java" %}

```java
Context context = this;
CompletableFuture<WepinUser> res = wepinWidget.register(context);
res.whenComplete((wepinUser, error) -> {
    if (error ==  null) {
        Log.d(TAG, "Wepin User is $wepinUser");
    } else {
        Log.d(TAG, "register error: $error");
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.register(context)
res?.whenComplete { wepinUser, error ->
    if (error == null) {
        Log.d(TAG, "Wepin User is $wepinUser")
    } else {
        Log.d(TAG, "register error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## getAccounts

Returns the user's account information (networks and addresses) available in the app. This function can only be used after logging in to Wepin. If no parameters are provided, it returns information for all the user's accounts.

### Parameters

* `networks` \<List\<String>> optional\
  The networks for which you want to return account information. The supported blockchain networks can be found on the supported blockchain page.
* `withEoa` \<Boolean> optional\
  If AA accounts are present, this determines whether to include EOA accounts in the return.

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

### Return Value

* CompletableFuture\<List\<WepinAccount>>
  * `address` \<String>\
    User account address.
  * `network` \<String>\
    The network type of the user's account.
  * `contract` \<String> *optional*\
    The contract address of the token.
  * `isAA` \<Boolean> *optional*\
    Indicates if it is an AA account.

### Example

{% tabs %}
{% tab title="Java" %}

<pre class="language-java"><code class="lang-java">List&#x3C;String> networks = List.of("Ethereum", "Kaia");
<strong>CompletableFuture&#x3C;List&#x3C;WepinAccount>> res = wepinWidget.getAccounts(networks, null);
</strong>res.whenComplete((result, error) -> {
    if (error ==  null) {
        Log.d(TAG, "WepinAccount List is $result");
    } else {
        Log.d(TAG, "getAccounts error: $error");
    }
});
</code></pre>

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.getAccounts(networks)
res?.whenComplete { accounts, error ->
    if (error == null) {
        Log.d(TAG, "Account List is $accounts")
    } else {
        Log.d(TAG, "getAccounts error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## getBalance

Returns the balance information (amount) of accounts. This function can only be used after logging in to Wepin. If no `accounts` parameter is provided, it returns the balance of all user accounts.

### Parameters

* `accounts` \<List\<WepinAccount>> *optional*
  * `network` \<String>\
    The network type of the account for which to check the balance.
  * `address` \<String>\
    The address of the account for which to check the balance.
  * `isAA` \<Boolean> *optional*\
    Indicates if it is an AA account.

### Return Value

* CompletableFuture\<List\<WepinAccountBalanceInfo>>
  * `network` \<String>\
    The network type of the user's account.
  * `address` \<String>\
    The address of the user's account.
  * `symbol` \<String>\
    Network symbol.
  * `balance` \<String>\
    The amount of the network coin held.
  * `token` \<List\<WepinTokenBalanceInfo>>
    * `symbol` \<String>\
      Token symbol.
    * `balance` \<String>\
      The amount of the token held.
    * `contract` \<String>\
      Token contract address.

### Example

{% tabs %}
{% tab title="Java" %}

```java
CompletableFuture<List<WepinAccountBalanceInfo>> res = wepinWidget.getBalance(accounts);
res.whenComplete((result, error) -> {
    if (error ==  null) {
        Log.d(TAG, "Balances is $result");
    } else {
        Log.d(TAG, "getBalance error: $error");
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.getBalance()
res?.whenComplete { balance, error ->
    if (error == null) {
        Log.d(TAG, "Balance is $balance")
    } else {
        Log.d(TAG, "getBalance error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## getNFTs

Returns the user's NFTs. This function can only be used after logging in to Wepin. If no `networks` parameter is provided, it returns all of the user's NFT information.

### Parameters

* `refresh` \<Boolean>\
  Required parameter to indicate whether to refresh the NFT data.
* `networks` \<List\<String>> optional\
  A list of network names to filter the NFTs.

### Return Value

* CompletableFuture\<List\<WepinNFT>>
  * `account` \<WepinAccount>
    * `address` \<String>\
      User account address.
    * `network` \<String>\
      The network type of the user's account.
    * `contract` \<String> *optional*\
      Token contract address.
    * `isAA` \<Boolean> *optional*\
      Indicates if it is an AA account.
  * contract \<WepinNFTContract>
    * `name` \<String>\
      NFT contract name.
    * `address` \<String>\
      NFT contract address.
    * `scheme` \<String>\
      Scheme of the NFT.
    * `description` \<String> *optional*\
      Description of the NFT contract.
    * `network` \<String>\
      Network associated with the NFT contract.
    * `externalLink` \<String> *optional*\
      External link associated with the NFT contract.
    * `imageUrl` \<String> *optional*\
      Image URL associated with the NFT contract.
  * `name` \<String>\
    Name of the NFT.
  * `description` \<String>\
    Description of the NFT.
  * `externalLink` \<String>\
    External link associated with the NFT.
  * `imageUrl` \<String>\
    Image URL associated with the NFT.
  * `contentUrl` \<String> *optional*\
    URL pointing to the content associated with the NFT.
  * `quantity` \<Int> *optional*\
    Quantity of the NFT.
  * `contentType` \<String>\
    Content type of the NFT ('image' | 'video').
  * `state` \<Int>\
    State of the NFT.

### Example

{% tabs %}
{% tab title="Java" %}

```java
CompletableFuture<List<WepinNFT>> res = wepinWidget.getNFTs(true, null);
res.whenComplete((result, error) -> {
    if (error ==  null) {
        Log.d(TAG, "NFT List is $result");
    } else {
        Log.d(TAG, "getNFTs error: $error");
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.getNFTs(true)
res?.whenComplete { nfts, error ->
    if (error == null) {
        Log.d(TAG, "Wepin NFT List is $nfts")
    } else {
        Log.d(TAG, "getNFTs error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## send

Performs the send function using the widget and returns the ID information of the send transaction. This function can only be used after logging in to Wepin.

### Parameters

* context \<Context>\
  Represents the location of a widget in the widget tree. It is used to locate the widget's position in the tree and to provide various functions like navigation and accessing theme data. Pass the current context when calling `register` to ensure the widget is displayed in the correct part of the UI hierarchy.
* `account` \<WepinAccount>
  * `address` \<String>\
    The type of network to send to.
  * `network` \<String>\
    The address of the account to send to.
  * `contract` \<String> *optional*\
    The contract address of the token.
  * `isAA` \<Boolean> *optional*\
    Indicates if it is an AA account.
* txData \<WepinTxData> *optional*
  * `toAddress` \<String>\
    The address to send to.
  * `amount` \<String>\
    The amount to send.

### Return Value

* CompletableFuture\<WepinSendResponse>
  * `txId` \<String>\
    The transaction ID of the send transaction.

Example

{% tabs %}
{% tab title="Java" %}

```java
Context context = this;
CompletableFuture<WepinSendResponse> res = wepinWidget.send(context, account);
res.whenComplete((sendResult, error) -> {
    if (error ==  null) {
        Log.d(TAG, "Send Result is $sendResult");
    } else {
        Log.d(TAG, "send error: $error");
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.send(context, account, txData)
res?.whenComplete { result, error ->
    if (error == null) {
        Log.d(TAG, "Send Result is $result")
    } else {
        Log.d(TAG, "send error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## receive

The `receive()` method opens the account information page associated with the specified account. This method can only be used after logging into Wepin.

### Parameters

* context \<Context>\
  Represents the location of a widget in the widget tree. It is used to locate the widget's position in the tree and to provide various functions like navigation and accessing theme data. Pass the current context when calling `register` to ensure the widget is displayed in the correct part of the UI hierarchy.
* `account` \<WepinAccount>
  * `address` \<String>\
    The type of network to send to.
  * `network` \<String>\
    The address of the account to send to.
  * `contract` \<String> *optional*\
    The contract address of the token.
  * `isAA` \<Boolean> *optional*\
    Indicates if it is an AA account.

### Return Value

* CompletableFuture\<WepinReceiveResponse>
  * `account` \<WepinAccount>
    * `address` \<String>\
      The type of network to send to.
    * `network` \<String>\
      The address of the account to send to.
    * `contract` \<String> *optional*\
      The contract address of the token.
    * `isAA` \<Boolean> *optional*\
      Indicates if it is an AA account.

### Example

{% tabs %}
{% tab title="Java" %}

```java
Context context = this;
CompletableFuture<WepinReceiveResponse> res = wepinWidget.receive(context, account);
res.whenComplete((result, error) -> {
    if (error ==  null) {
        Log.d(TAG, "Receive Result is $result");
    } else {
        Log.d(TAG, "receive error: $error");
    }
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val res = wepinWidget.receive(context, account)
res?.whenComplete { result, error ->
    if (error == null) {
        Log.d(TAG, "Receive Result is $result")
    } else {
        Log.d(TAG, "receive error: $error")
    }
```

{% endtab %}
{% endtabs %}

***

## finalize

```kotlin
wepinWidget.finalize()
```

Terminates the use of WepinSDK. The `WepinLifeCycle` changes to `notInitialized`.

### **Parameters**

* None

### **Return Value**

* None

### **Example**

{% tabs %}
{% tab title="Java" %}

```java
wepinWidget.finalize();
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
wepinWidget.finalize()
```

{% endtab %}
{% endtabs %}

## login

If you choose **not to use the integrated `WepinLogin` within `WepinWidget`** and instead use a separate `WepinLogin` SDK, **make sure both SDKs are using the same version**.\
Using different versions of the two SDKs may result in errors.

The `login` variable is a Wepin login library that includes various authentication methods, allowing users to log in using different approaches. It supports email and password login, OAuth provider login, login using ID tokens or access tokens, and more. For detailed information on each method, refer to the official [Login Library guide](https://docs.wepin.io/en/widget-integration/flutter-sdk/login-library/methods).

### **Available Methods**

* [`loginWithOauthProvider`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#loginwithoauthprovider)
* [`signUpWithEmailAndPassword`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#signupwithemailandpassword)
* [`loginWithEmailAndPassword`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#loginwithemailandpassword)
* [`loginWithIdToken`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#loginwithidtoken)
* [`loginWithAccessToken`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#loginwithaccesstoken)
* [`getRefreshFirebaseToken`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#getrefreshfirebasetoken)
* [`loginWepin`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#loginwepin)
* [`getCurrentWepinUser`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#getcurrentwepinuser)
* [`logout`](https://docs.wepin.io/widget-integration/ios-swift-sdk/login-library/methods#logoutwepin)

These methods support various login scenarios, allowing you to select the appropriate method based on your needs.

**Exception**

* [WepinError](https://docs.wepin.io/widget-integration/flutter-sdk/widget/methods#wepinerror)

**Example**

```kotlin
// Login with OAuth Provider
wepinSDK.login.loginWithOauthProvider("google", "your-client-id")
    .thenCompose { authResult ->
        // Login using ID Token
        wepinSDK.login.loginWithIdToken(authResult.idToken)
    }.thenCompose { idTokenResult ->
        // Login to Wepin
        wepinSDK.login.loginWepin(idTokenResult)
    }.thenAccept { user ->
        Log.d("WepinSDK", "Login success! User Information: $user")
    }.exceptionally { e ->
        Log.e("WepinSDK", "Login fail: ${e.message}", e)
        null
    }

// Sign up with email and password
wepinSDK.login.signUpWithEmailAndPassword(
    email = 'example@example.com', 
    password = 'password123'
).thenAccept { signUpResult ->
    Log.d("WepinSDK", "Sign up success! User Information: $user")
}.exceptionally { error ->
    if (error is WepinError) {
    }
}

// Get current logged in user information
var currentUser = wepinSDK.login.getCurrentWepinUser()
    .thenAccept { user ->
        Log.d("WepinSDK", "Current user: $user")
    }.exceptionally { e ->
        Log.e("WepinSDK", "getCurrentWepinUser fail", e)
        null
    }

// logout
wepinSDK.login.logout()
    .thenAccept { result ->
        Log.d("WepinSDK", "Logout result: $result")
    }.exceptionally { e ->
        Log.e("WepinSDK", "Logout fail", e)
        null
    }    
```
