# Methods

## login

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.

**Available Methods**

* `loginWithOauthProvider`
* `signUpWithEmailAndPassword`
* `loginWithEmailAndPassword`
* `loginWithIdToken`
* `loginWithAccessToken`
* `getRefreshFirebaseToken`
* `loginWepin`
* `getCurrentWepinUser`
* `logout`
* `getSignForLogin`

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

**Example**

```javascript
// Login using an OAuth provider
const oauthResult = await wepinPin.login.loginWithOauthProvider({
  provider: 'google',
  clientId: 'your-client-id',
});

// Sign up and log in using email and password
const signUpResult = await wepinPin.login.signUpWithEmailAndPassword(
  'example@example.com',
  'password123'
);

// Log in to Wepin
const wepinLoginResult = await wepinPin.login.loginWepin(signUpResult);

// Get the currently logged-in user
const currentUser = await wepinPin.login.getCurrentWepinUser();

// Logout
await wepinPin.login.logout();
```

## generateRegistrationPINBlock

```javascript
await wepinPin.generateRegistrationPINBlock();
```

Generates a pin block for registration. This method should only be used when the loginStatus is pinRequired.

#### **Parameters**

* None

#### **Return value**

* Promise\<RegistrationPinBlock>
  * `uvd` \<EncUVD> - Encrypted PIN
    * `b64Data` \<string> - Data encrypted with the original key in b64SKey
    * `b64SKey` \<string> - A key that encrypts data encrypted with the Wepin's public key.
    * `seqNum` \<number> **optional** - Values to check for when using PIN numbers to ensure they are used in order.
  * `hint` \<EncPinHint> - Hints in the encrypted PIN.
    * `data` \<string> - Encrypted hint data.
    * `length` \<string> - The length of the hint
    * `version` \<number> - The version of the hint

#### **Example**

```javascript
const pinBlock = await wepinPin.generateRegistrationPINBlock();

// You need to make a Wepin RESTful API request using the received data.
fetch('https://sdk.wepin.io/v1/app/register', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    // Add authentication headers
  },
  body: JSON.stringify({
    // Add other required fields
    UVD: pinBlock.uvd,
    hint: pinBlock.hint,
  }),
});
```

## generateAuthPINBlock

```javascript
await wepinPin.generateAuthPINBlock(count?);
```

Generates a pin block for authentication.

#### **Parameters**

* `count` \<number> **optional** - If multiple PIN blocks are needed, please enter the number to generate. If the count value is not provided, it will default to 1.

#### **Return value**

* Promise\<AuthPinBlock>
  * `uvdList` \<EncUVD\[]> - Encrypted pin list
    * `b64Data` \<string> - Data encrypted with the original key in b64SKey
    * `b64SKey` \<string> - A key that encrypts data encrypted with the wepin's public key.
    * `seqNum` \<number> **optional** - Values to check for when using PIN numbers to ensure they are used in order
  * `otp` \<string> **optional** - If OTP authentication is required, include the OTP.

#### **Example**

```javascript
const pinBlock = await wepinPin.generateAuthPINBlock(3);

// Sort seqNum of uvd in ascending order from 1 because you need to write it in order starting from 1
pinBlock.uvdList.sort((a, b) => (a.seqNum ?? 0) - (b.seqNum ?? 0));

const resArray = [];
for (const encUVD of pinBlock.uvdList) {
  const response = await fetch('https://sdk.wepin.io/v1/tx/sign', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      // Add authentication headers
    },
    body: JSON.stringify({
      userId: await getUserId(),
      walletId: await getWalletId(),
      accountId: (await getEthereumAccount()).accountId,
      type: 'msg_sign',
      txData: {
        data: '0x0',
      },
      pin: encUVD,
      otpCode: {
        code: pinBlock.otp,
      },
    }),
  });
  resArray.push(response);
}
```

## generateChangePINBlock

```javascript
await wepinPin.generateChangePINBlock();
```

Generate pin block for changing the PIN.

#### **Parameters**

* None

#### **Return value**

* Promise\<ChangePinBlock>
  * `uvd` \<EncUVD> - The user's existing encrypted PIN
    * `b64Data` \<string> - Data encrypted with the original key in b64SKey
    * `b64SKey` \<string> - A key that encrypts data encrypted with the wepin's public key.
    * `seqNum` \<number> **optional** - Values to check for when using PIN numbers to ensure they are used in order
  * `newUVD` \<EncUVD> - The user's new encrypted PIN
    * `b64Data` \<string> - Data encrypted with the original key in b64SKey
    * `b64SKey` \<string> - A key that encrypts data encrypted with the wepin's public key.
    * `seqNum` \<number> **optional** - Values to check for when using PIN numbers to ensure they are used in order
  * `hint` \<EncPinHint> - Hints in the encrypted PIN
    * `data` \<string> - Encrypted hint data
    * `length` \<string> - The length of the hint
    * `version` \<number> - The version of the hint
  * `otp` \<string> **optional** - If OTP authentication is required, include the OTP.

#### **Example**

```javascript
const pinBlock = await wepinPin.generateChangePINBlock();

const response = await fetch('https://sdk.wepin.io/v1/wallet/pin/change', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    // Add authentication headers
  },
  body: JSON.stringify({
    userId: await getUserId(),
    walletId: await getWalletId(),
    UVD: pinBlock.uvd,
    newUVD: pinBlock.newUVD,
    hint: pinBlock.hint,
    otpCode: {
      code: pinBlock.otp,
    },
  }),
});
```

## generateAuthOTPCode

```javascript
await wepinPin.generateAuthOTPCode();
```

Generate OTP for failed verify OTP.

#### **Parameters**

* None

#### **Return value**

* Promise\<AuthOTP>
  * `code` \<string> - The OTP entered by the user.

#### **Example**

```javascript
let res = await getWepinSignMessage(pinBlocks.uvdList, pinBlock.otp);
if (res.body[0].message === 'OTP_MISMATCH_WRONG_CODE') {
  const otp = await wepinPin.generateAuthOTPCode();
  res = await getWepinSignMessage(pinBlocks.uvdList, otp.code);
}
```

## finalize

```javascript
await wepinWidget.getStatus();
```

The `finalize()` method finalizes the Wepin PIN Pad SDK.

#### **Parameters**

* None

#### **Return value**

* Promise\<void>

#### **Example**

```javascript
await wepinPin.finalize();
```

## WepinError

This section provides descriptions of various error codes that may be encountered while using the Wepin PIN Pad SDK functionalities. Each error code corresponds to a specific issue, and understanding these can help in debugging and handling errors effectively.

| Error Code                   | Description                                                                                       |
| ---------------------------- | ------------------------------------------------------------------------------------------------- |
| `ApiRequestError`            | There was an error while making the API request.                                                  |
| `InvalidParameters`          | One or more parameters provided are invalid or missing.                                           |
| `NotInitialized`             | The Wepin PIN Pad SDK has not been properly initialized.                                          |
| `InvalidAppKey`              | The Wepin app key is invalid.                                                                     |
| `InvalidLoginProvider`       | The login provider specified is not supported or is invalid.                                      |
| `InvalidToken`               | The token does not exist.                                                                         |
| `InvalidLoginSession`        | The login session information does not exist.                                                     |
| `UserCancelled`              | The user has cancelled the operation.                                                             |
| `UnknownError`               | An unknown error has occurred, and the cause is not identified.                                   |
| `NotConnectedInternet`       | The system is unable to detect an active internet connection.                                     |
| `FailedLogin`                | The login attempt has failed due to incorrect credentials or other issues.                        |
| `AlreadyLogout`              | The user is already logged out, so the logout operation cannot be performed again.                |
| `AlreadyInitialized`         | The Wepin PIN Pad SDK is already initialized.                                                     |
| `InvalidEmailDomain`         | The provided email address's domain is not allowed or recognized by the system.                   |
| `FailedSendEmail`            | The system encountered an error while sending an email.                                           |
| `RequiredEmailVerified`      | Email verification is required to proceed with the requested operation.                           |
| `IncorrectEmailForm`         | The provided email address does not match the expected format.                                    |
| `IncorrectPasswordForm`      | The provided password does not meet the required format or criteria.                              |
| `NotInitializedNetwork`      | The network or connection required for the operation has not been properly initialized.           |
| `RequiredSignupEmail`        | The user needs to sign up with an email address to proceed.                                       |
| `FailedEmailVerified`        | The Wepin PIN Pad SDK encountered an issue while attempting to verify the provided email address. |
| `FailedPasswordStateSetting` | Failed to set the password state.                                                                 |
| `FailedPasswordSetting`      | The Wepin PIN Pad SDK failed to set the password.                                                 |
| `ExistedEmail`               | The provided email address is already registered in Wepin.                                        |
| `NotActivity`                | The Context is not an activity.                                                                   |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wepin.io/en/widget-integration/react-native-sdk/pin-pad-library/method.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
