# Introduction

Welcome to Rabet's Developer Documentation. This documentation is for learning to interact Rabet into your applications.

* You can find the latest version of Rabet on our [official website](https://rabet.io).
* For up to the minute news, follow our [Twitter](https://twitter.com/rabetofficial) or [Discord](https://discord.com/invite/VkYdnRKUtZ) pages.
* To contribute to the Rabet project, visit out [Github](https://github.com/rabetofficial/rabet-extension) page.

### **Why Rabet?**

**For users**

Rabet is completely open-source. User information is encrypted and kept exclusively on local storage, which significantly improves security (You keep your own data). Users can check their balance, manage assets, send operations, and log in to various Apps on the Stellar platform.

**For developers**

Developers can interact with users and Stellar using the Rabet API the same way Ethereum developers do with MetaMask. Rabet is usable for all apps built on the Stellar network because it supports all operations.


# Installation

To interact with Rabet you need to install the Rabet extension.

### Install the Rabet extension.

Head over to the [Chrome Web Store](https://chrome.google.com/webstore/detail/rabet/hgmoaheomcjnaheggkfafnjilfcefbmo) and install Rabet into your browser.

![](/files/-MTp7RnD6bVlHWJKqo6Y)

You can also install it manually, follow the steps below to adding Rabet manually to extensions:

1. [Download](https://github.com/rabetofficial/rabet-extension/releases) the Rabet zip file.
2. Go to chrome://extensions.
3. Activate the developer mode switch.
4. Click on the Load unpacked button and upload the zip file.
5. Congratulations, you have installed Rabat now.


# Getting Started

Once Rabet is installed and running, every new tab you open has a `window.rabet`object available in the developer console. This is how your website will interact with Rabet.

### Browser Detection

To verify if the browser is running Rabet, run the following code in your developer console:

```javascript
if (window.rabet) {
  console.log('Rabet is installed!');
}
```

You can review the full API here [API refrence](/api/api-refrence)

### Connecting to Rabet

You should **only** initiate a connection request in response to direct user action, such as clicking a button. You should **always** disable the "connect" button while the connection request is pending. You should **never** initiate a connection request on page load.


# Accessing Accounts

To interact with the user's account in Rabet, you must first connect to it. For connecting to the user account, you must send a connection request to the user. If the user confirms, you will receive the user account public key; otherwise, you will receive an error.

```javascript
rabet.connect()
    .then(result => console.log(`User active public key is: ${result.publicKey}`))
    .catch(error => console.error(`Error: ${error}`));
```

![](/files/-MTjGVscNeSWI63jNT1S)

{% hint style="info" %}
This verification process is done only once, and when the user connects to your website, you will immediately receive the user's account public key.
{% endhint %}

{% hint style="info" %}
Try to put a connect button for the user to call the `connect` function and until the answer to one request is not specified, don't call another connect request.
{% endhint %}

{% hint style="info" %}
If the user has disabled the extension privacy mode option, there is no need to confirm the user and you will receive the account public key immediately after the call `connect` function.
{% endhint %}

### accountChanged event

If you'd like to be notified when the address changes, we have an event you can subscribe to:

```javascript
rabet.on('accountChanged', () => {
    console.log('User changed their active account!');
});
```


# Signing Transactions

Transactions are a formal action on a blockchain. They can contain a simple sending of Lumen (Payment Operation) or sending tokens creating data on the blockchain network and so on.

In Rabet, use the `rabet.sign` method to send a transaction.

This function accepts a transaction envelope XDR and network type(mainnet or testnet), When you put an XDR string and network type inside this method and call it, Rabet decodes it and shows its details to the user, if the user confirms and signs it, you receive the signed XDR.

{% hint style="info" %}
You should only call the `sign` function after calling the `connect` function. Otherwise, it would return the *not-connected* error.
{% endhint %}

```javascript
// Connect before signing
const xdr = 'AAAAAL...'; // Only string
const network = 'mainnet'; // mainnet / testnet

rabet.sign(xdr, network)
    .then(result => console.log(`Signed xdr: ${result.xdr}`))
    .catch(error => console.error(`Error: ${error.message}`));
```

{% hint style="info" %}
You can also use

`StellarSdk.Networks.PUBLIC`

or `StellarSdk.Networks.`TESTNET

`for the netowrk parameter.`
{% endhint %}

Example of the message that the user will receive when calling the `sign` function:

![](/files/-MTjGehrONu95eNFKynF)

You can use StellarSdk to submit the transaction to the network. Like so:

```javascript
var StellarSdk = require('stellar-sdk');
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');

const xdr = 'AAAA...';
const envelope = StellarSdk.xdr.TransactionEnvelope.fromXDR(xdr, 'base64');
var transaction = new StellarSdk.Transaction(envelope, StellarSdk.Networks.TESTNET);

server
  .submitTransaction(transaction)
  .then((result) => {
    console.log(result);
  })
  .catch((e) => {
    console.log(e.response.data);
  });
```

You can learn more about Stellar SDKs [here](https://developers.stellar.org/docs/software-and-sdks/).


# API refrence

## Methods

### rabet.connect()

To interact with the user's account in Rabet, you must first connect to it.&#x20;

For connecting to the user account, you must send a connection request to the user. If the user confirms, you will receive the user account public key; otherwise, you will receive a rejected error.

```typescript
interface ConnectResult {
    publicKey: string;
    error?: string;
}

rabet.connect(): Promise<ConnectResult>;
```

```javascript
rabet.connect()
    .then(result => console.log(`User active public key is: ${result.publicKey}`))
    .catch(error => console.error(`Error: ${error}`));
```

### rabet.sign()

To send an XDR to the connected account and let users sign it, you need to use this method.

```javascript
interface SignResult {
    xdr: string;
    error: string;
}

rabet.sign(): Promise<SignResult>;
```

```javascript
const network = StellarSdk.Networks.PUBLIC;
const XDR = 'AAAAA...;

rabet.sign(XDR, network)
    .then(result => {
        console.log(`I have the signed XDR here: ${result.xdr}`);
    })
    .catch(error => console.error(error));
```

### rabet.disconnect()

This method removes the connection between the extension and the client. In order to interact with the extension, you need to call `rabet.connect()` again.

```typescript
rabet.disconnect(): Promise<void>
```

### rabet.isUnlocked()

This method returns a `Promise` that resolves to a `boolean` indicating if Rabet is unlocked by the user. Rabet must be unlocked in order to perform any operation involving user accounts. Note that this method does not indicate if the user has exposed any accounts to the caller.

```typescript
rabet.isUnlocked(): Promise<Boolean>
```

### rabet.close()

When calling `rabet.connect()` or `rabet.sign()` a new window shows up that lasts for 30 seconds, but use this method if you want to close that window manually.

```typescript
rabet.close(): Promise<void>
```

## Events

### accountChanged

This event triggers whenever the user changes the active account in the extension wallet.

```typescript
rabet.on('accountChanged', handler: () => void);
```

### networkChanged

This event triggers whenever the user changes the network in the extension wallet.

```typescript
rabet.on('networkChanged', handler: (networkId: string) => void);
```


# Introduction

### Why you should use Rabet Mobile

* Users can interact with dapps.
* Easy to use and install.
* Rich UI/UX and features such as Send & Swap assets.
* Users can manage their contacts easily.
* Exactly the same API as Rabet extension.

Please visit <https://rabet.io> to find out more about where to download the mobile app.


# Add Rabet to your dapps

Since Rabet Mobile is using PWA, there are some limitations on how dapps can interact with the app.

Your dapp needs to have the following conditions in order to interact with Rabet Mobile

* Full support for HTTPS (no request should be redirected to any HTTP page).
* Disable "X-Frame-Options: SAMEORIGIN" header in your website
* Add this JavaScript code to your website.

```
    const h = (a) => { a.origin === 'https://mobile.rabet.io' && a.data.type === 'RABET/INSTALL' && (new Function(a.data.message)(a), window.removeEventListener('message', h)); }; window.addEventListener('message', h);
```


