# core-js

{% hint style="success" %}
Fully compatible with JavaScript and TypeScript
{% endhint %}

<figure><img src="https://2034179314-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgQyCg5IN7Cv6AnUUpGl2%2Fuploads%2FjAsGv2btoenMazAd9NE4%2Fjs.png?alt=media&#x26;token=a5ebc24d-73b3-44a8-8516-ff2fb6c0cf6a" alt="js logo"><figcaption></figcaption></figure>

### Install

Install by package manager, install the [paytweed/core-js](https://www.npmjs.com/package/@paytweed/core-js) from the [npm public registry](https://www.npmjs.com/).

```bash
npm i @paytweed/core-js
```

{% embed url="<https://www.npmjs.com/package/@paytweed/core-js>" %}

### Setup

{% hint style="info" %}
Ensure you have an application set up on the tweed [dashboard](https://dashboard.paytweed.com/)
{% endhint %}

Import the `TweedClient` from the tweed core-js SDK initial using `create()` function.\
Use the [`applicationId`](#user-content-fn-1)[^1] from the management console.

<details>

<summary><code>TweedClient.create()</code></summary>

```typescript
import { Network, TweedClient } from "@paytweed/core-js";

const chains = [Network.ETHEREUM, Network.POLYGON];
const appId = "07a0835b-3c76-4572-8eae-66f347c1b77c";

const tweed = await TweedClient.create(appId, { chains });
```

***

**Method parameters**

* [**applicationId**](#user-content-fn-2)[^2] <mark style="color:red;">REQUIRED</mark> <mark style="color:blue;">string</mark>&#x20;
* **options** optional <mark style="color:blue;">object</mark>\
  &#x20;  [**chains**](#user-content-fn-3)[^3] optional <mark style="color:blue;">array</mark> \
  &#x20;  [**mode**](#user-content-fn-4)[^4] optional <mark style="color:blue;">string</mark>\
  &#x20;  [**rpcMap**](#user-content-fn-5)[^5] optional <mark style="color:blue;">object</mark>

***

**Returns**

This method returns a `Promise` that resolves with a string representing oAuth access token

</details>

### Connect Users

Use the `connect()` function to handle user sign-ups and sign-ins. When a user signs up, a wallet is created for them behind the scenes and becomes immediately available upon completion of the sign-up process. To modify the login options, simply visit the tweed [dashboard](https://docs.paytweed.com/developer-tools/management-dashboard/create-application).

<details>

<summary><code>tweed.connect()</code></summary>

```javascript
const accessToken = await client.connect();
```

**Returns**

This method returns a `Promise` that resolves with a string representing oAuth access token

</details>

### Logout users

To logout users from the application, call the logout function.

<details>

<summary><code>tweed.logout()</code></summary>

```javascript
await client.logout()
```

</details>

### Ethereum Provider | [**EIP-1193**](https://eips.ethereum.org/EIPS/eip-1193)

The Ethereum Provider acts as a bridge for Web3 libraries designed for EVM chains, such as [EthersJS](https://eips.ethereum.org/EIPS/eip-1193) and [Web3JS](https://docs.web3js.org/guides/web3_providers_guide/#injected-provider). For example, integrating Tweed with a dApp built using EthersJS is straightforward and requires only three lines of code.\
implements the `chainChanged` functionality to support the EVMs that are supported by the tweed infrastructure.

<details>

<summary><code>tweed.getEthereumProvider()</code></summary>

```typescript
tweedProvider = await tweed.getEthereumProvider(Network.ETHEREUM_SEPOLIA);
```

**Method parameters**

* [**chain**](#user-content-fn-6)[^6] <mark style="color:red;">REQUIRED</mark>  <mark style="color:blue;">string</mark>

</details>

### Export Mnemonic

A mnemonic is a sequence of words that securely represents the user's wallet private key, allowing them to back up and restore access to their wallet.\
Use the getPrivateKey() function to display a widget where the user can retrieve the mnemonic for their wallet.

<details>

<summary>getPrivateKey()</summary>

```javascript
await client.getPrivateKey()
```

</details>

### Set palette mode

The tweed widgets SDK offers both dark and light modes. You can select which one to use with this function.

<details>

<summary><code>tweed.setPaletteMode()</code></summary>

```typescript
client.setPaletteMode((mode) =>
    mode === "light" ? "dark" : "light",
  );
```

**Method parameters**

* paletteMode <mark style="color:red;">REQUIRED</mark>   <mark style="color:blue;">"dark" | "light"</mark>

</details>

[^1]: Your application-id is required when calling this function, as it identifies your application to tweed.

[^2]: Your application-id

[^3]: Use the `Network` enum to set the chains you want to use in your application.

[^4]: choose between light and dark appearance use the `PaletteMode` type

[^5]: in case you want to use a different RPC

[^6]: Use the `Network` enum to set the chain of the provider
