# guardee-js

[![NPM Version][npm-version-image]][npm-url]

[![NPM Install Size][npm-install-size-image]][npm-install-size-url]

[![NPM Downloads][npm-downloads-image]][npm-downloads-url]


## Installation

```bash
  npm install guardee
```

## Basic Usage

The constructor of the guardee client takes an object with parameters such as `signalserver_host`, `signalserver_key` and `connect_option`. Syntax:

```javascript
const Guardee = require('guardee');
const guardee = new Guardee({
    signalserver_host: 'http://signalserver.guardee.io',
    signalserver_key: '906c7939ed0007b48b9c779302d788299183cb4a',
    connect_option: {
        initiator: true,
    },
});
```

### `signalserver_host` and `signalserver_key`

You need to acquire `signalserver_host` and `signalserver_key` which contemporary Guardee wallet provides. These arguments are used to communicate with the signal server for peer-to-peer connection between your dApp and Guardee wallet.

### `connect_option`

`connetion_option` is used by `guardee.connector` to provide options for its peer-to-peer connection. In case of NodeJS, you need to provide wrtc as a parameter.

```javascript
const Guardee = require('guardee');
const wrtc = require('wrtc');
const guardee = new Guardee({
    signalserver_host: 'http://signalserver.guardee.io',
    signalserver_key: '906c7939ed0007b48b9c779302d788299183cb4a',
    connect_option: {
        initiator: true,
        wrtc: wrtc
    },
});
```


## API

### guardee.connector

#### RegisterGenesisOffer

Register genesis connection offer to the signal server.
 
- `connectionOffer` : <strong>Object</strong> { `connectionId`, `offer` } (optional) connection offer object
- `waitSeconds` : <strong>Number</strong> How long should the code wait measured in seconds (default = 60)

#### CancelGenesisOffer

Cancel genesis connection offer and notice to the signal server

#### Request

Send request to connected peer

- `type` : <strong>String</strong> request type (`account_connection`, `sign_tx`, `send_tx`)
- `body` : <strong>Object</strong> request body
- `encode` : <strong>Boolean</strong> (optional) whether to stringify request body
- `encrypt` : <strong>Boolean</strong> (optional) whether to encode and encrypt request body with password
- `password` : <strong>String</strong> (optional) password to encrypt request body


#### Response

Send response to connected peer

- `type` : <strong>String</strong> response type (`account_connection`, `sign_tx`, `send_tx`)
- `body` : <strong>Object</strong> response body
- `encode` : <strong>Boolean</strong> (optional) whether to stringify response body
- `encrypt` : <strong>Boolean</strong> (optional) whether to encode and encrypt response body with password
- `password` : <strong>String</strong> (optional) password to encrypt response body

#### SendDataSequence

Send data sequence to the connected peer

- `type` : <strong>String</strong> data sequence type ('sign_tx', 'send_tx')
- `dataSequence` : <strong>Array</strong> array of sequential transactions


#### SendDataChunkSequence

Send already chunked data sequence to the connected peer

- `type` : <strong>String</strong> data sequence type ('sign_tx', 'send_tx')
- `dataChunkSequence` : <strong>Array</strong> chunked array of sequential transactions

#### Disconnect

Destroy all `guardee.connector.connections`

#### Reset

Reset connections

#### On

Listen on given event and run callback

- `event`: <strong>String</strong>
- `cb`: <strong>Function</strong>
- `options`: <strong>Object</strong>

#### RemoveEventListener

Remove EventListener on given event callback function

- `event`: <strong>String</strong>
- `fn`: <strong>Function</strong>
- `options`: <strong>Object</strong>


### guardee.encoder

#### Stringify

Encode given object into safe uri component

- `obj`: <strong>Object</strong>

#### Parse

Decode given string into original object

- `stringified`: <strong>String</strong>

## Events

There are 15 different events that `guardee.connector` emits when connection issues occur.

- `genesis_connection`: Emitted when the genesis connection is initiated
- `genesis_offer`: Emitted when the genesis connection offer is generated
- `genesis_offer_register`: Emitted when the genesis connection offer is registered to the signal server
- `genesis_offer_cancel`: Emitted when the genesis connection offer is cancelled
- `genesis_answer_received`: Emitted when the answer for the genesis connection offer is received
- `genesis_answer_set`: Emitted when the answer for the genesis connection is set to the instance
- `error`: Emitted when error occurs
- `ready`: Emitted when stable amount of connections are alive
- `disconnect`: Emitted when every peer connection is destroyed
- `request`: Emitted when request is received
- `response`: Emitted when response is received
- `chunk`: Emitted when chunk is received
- `chunk_start`: Emitted when the first chunk is sent
- `chunk_data`: Emitted when chunk data is sent
- `chunk_end`: Emitted when the last chunk is sent








[npm-downloads-image]: https://badgen.net/npm/dm/guardee
[npm-downloads-url]: https://npmcharts.com/compare/guardee?minimal=true
[npm-install-size-image]: https://badgen.net/packagephobia/install/guardee
[npm-install-size-url]: https://packagephobia.com/result?p=guardee
[npm-url]: https://npmjs.org/package/guardee
[npm-version-image]: https://badgen.net/npm/v/guardee