# API

{% code overflow="wrap" %}

```javascript
declare global {
 interface Window {
  xidar: {
   v1: {
    hasWallet: () => Promise<boolean>
    isConnected: () => Promise<boolean>
    connect: () => Promise<string>
    disconnect: () => Promise<{code: number}>
    sign: (challenge: string) => Promise<string>
    submitTransaction: (payload: {}) => Promise<any>
    encrypt: (message: string, fromAddress: string, toAddress: string) => Promise<string>
    decrypt: (message: string, fromAddress: string) => Promise<string>
    accounts: () => Promise<string[]>
    balances: () => Promise<{}>
    stakes: () => Promise<{}>
    unstakes: () => Promise<{}>
   }
  }
 }
}
```

{% endcode %}

## hasWallet()

This function allows you to know if the user has the wallet installed and initialized. Returns true or false.

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

```javascript
hasWallet: () => Promise<boolean>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    //Do code...
}
```

{% endtab %}
{% endtabs %}

## isConnected()

This function allows you to know if the user's wallet is synchronized with the current domain name. Returns true or false.

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

```javascript
isConnected: () => Promise<boolean>
```

{% endtab %}

{% tab title="Example" %}

<pre class="language-javascript"><code class="lang-javascript"><strong>const hasWallet = await window.xidar.v1.hasWallet();
</strong>if (hasWallet) {
<strong>    const isConnected = await window.xidar.v1.isConnected();
</strong>    if (isConnected) {
        //Do code...
    }
}
</code></pre>

{% endtab %}
{% endtabs %}

## connect()

This function allows the website to be synchronized with the user's wallet. Ask for permissions if the user has not already accepted. Returns the active address of the user or null.

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

```javascript
connect: () => Promise<string>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const activeAddress = await window.xidar.v1.connect();
    if (activeAddress) {
        // Do code
    }
}
```

{% endtab %}
{% endtabs %}

## disconnect()

This function allows to disconnect the website from the user's wallet.

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

```javascript
disconnect: () => Promise<{code: number}>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const disconnectResult = await window.xidar.v1.disconnect();
    }
}
```

{% endtab %}
{% endtabs %}

## sign()

This function allows you to sign a message with the private key of the active address of the user's wallet. Returns the signed message.

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

```javascript
sign: (challenge: string) => Promise<string>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const signedMessage = await window.xidar.v1.sign('Here is my message');
    }
}
```

{% endtab %}
{% endtabs %}

## submitTransaction()

This function allows you to send a transaction with the user's active wallet address. Return the transaction ID.

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

```javascript
submitTransaction: (payload: {}) => Promise<any>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const activeAddress = await window.xidar.v1.connect();
        const transaction = await window.xidar.v1.submitTransaction({
             "actions": [
                {
                    "type": "TransferTokens",
                    "from_account": {
                        "address": activeAddress
                    },
                    "to_account": {
                        "address": "rdx1qsp3q5gpep5x2la7q87paqf8yla43krz0lageax8svsjnq9fmfswgfcyhd8pm"
                    },
                    "amount": {
                        "token_identifier": {
                            "rri": "xrd_rr1qy5wfsfh"
                        },
                        "value": "100000000000000000000"
                    }
                }
            ],
            "message": "Message example",
            "encryptMessage": false
        });
        const transactionID = transaction.txID
    }
}
```

{% endtab %}
{% endtabs %}

## encrypt()

This function allows you to encrypt a message with the the user's active wallet address. Returns the encrypted message.

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

```javascript
encrypt: (message: string, fromAddress: string, toAddress: string) => Promise<string>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const activeAddress = await window.xidar.v1.connect();
        const encryptedMessage = await window.xidar.v1.encrypt(
            'Here is my message',
            activeAddress,
            "rdx1qsp3q5gpep5x2la7q87paqf8yla43krz0lageax8svsjnq9fmfswgfcyhd8pm"
        );
    }
}
```

{% endtab %}
{% endtabs %}

## decrypt()

This function allows you to decrypt a message with the the user's active wallet address. Returns the decrypted message.

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

```javascript
decrypt: (message: string, fromAddress: string) => Promise<string>
```

{% endtab %}

{% tab title="Example" %}

```javascript

const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const decryptedMessage = await window.xidar.v1.decrypt(
            '01ff022c6b5987523d48d3ac9e329114cac862fa8b9c442806b0b4431dcce8055d75c183f2d1e3298325f5089f1b2a9bbfa7ca7efeb860049941db4f5dca332b35a7a6de5664a1b131ffc5af80a6915eb7',
            "rdx1qsp3q5gpep5x2la7q87paqf8yla43krz0lageax8svsjnq9fmfswgfcyhd8pm"
        );
    }
}
```

{% endtab %}
{% endtabs %}

## accounts()

This function allows you to retrieve the list of all the addresses of the user's wallet. Return an array of addresses.

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

```javascript
accounts: () => Promise<string[]>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const addresses = await window.xidar.v1.accounts();
    }
}
```

{% endtab %}
{% endtabs %}

## balances()

This function allows you to retrieve balances of the user's active wallet address. Returns an array of tokens informations.

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

```javascript
balances: () => Promise<{}>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const balances = await window.xidar.v1.balances();
    }
}
```

{% endtab %}
{% endtabs %}

## stakes()

This function allows you to retrieve staking positions of the user's active wallet address. Returns an array of staking positions.

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

```javascript
stakes: () => Promise<{}>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const stakes = await window.xidar.v1.stakes();
    }
}
```

{% endtab %}
{% endtabs %}

## unstakes()

This function allows you to retrieve unstaking positions of the user's active wallet address. Returns an array of unstaking positions.

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

```javascript
unstakes: () => Promise<{}>
```

{% endtab %}

{% tab title="Example" %}

```javascript
const hasWallet = await window.xidar.v1.hasWallet();
if (hasWallet) {
    const isConnected = await window.xidar.v1.isConnected();
    if (isConnected) {
        const unstakes = await window.xidar.v1.unstakes();
    }
}
```

{% endtab %}
{% endtabs %}


---

# 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://xidar.gitbook.io/wallet/integration/api.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.
