npm package: @raydeck/useplug
Description: Plug Provider and Hook for Reactjs apps
Latest version: 1.0.3


<a name="readmemd"></a>

# usePlug - React Wrapper for Plug Wallet

[Plug Wallet](https://plugwallet.ooo) makes it easy for browser users to interact with ICP on an economic basis. usePlug makes it easy for React app developers to integrate usePlug for authentication and working with actors. 

**Typescript Native** Implementation - get all your sweet autocomplete. 

We are very grateful for the folks at the [DFINITY foundation](https://dfinity.org) for putting on the Supernova Hackathon when we created this tool. And our thanks to [Psychedelic](https://psychedelic.ooo) for making Plug Wallet and open-sourcing their work so we could make it easy for others. 

## License
[MIT License - use, share and enjoy!](./LICENSE.md)

## Installation
**Prequisite: A React app (often built with `npx create-react-app`);
```bash
yarn add @dfinity/agent @dfinity/candid @dfinity/principal # Dependencies
yarn add @raydeck/useplug # This package
```

## Usage
### App.tsx
Add The plug provider to the top level of your application that you want authenticated. 

**Remember to set the whitelist** Plug will only let you interact with canisters on the whitelist you provided to the plugprovider

## PlugProvider 
PlugProvider wraps your app to manage communication with the plug wallet. To instantiate, provide props with the canisters and network with which you want to communicate

### Props
  * **whitelist?: string[];** Which canisterids - if any - are allowed to interact using the plug wallet ID
  * **host?: string;** Which server to use for authentication. By default uses mainnet. (setting explicitly doesn't seem to work as it should for local replicas)
  * **timeout?: number;** How long (in ms) to wait for communications from the IC network
 
  *Note:* While whitelist is optional because of use cases where you are just trading ICP using plug, it's a code smell to leave it undefined in most cases. The others are fine to leave alone.
### Example
```tsx
import {PlugProvider, Authenticated, Unauthenticated, LoggedOut } from '@raydeck/useplug'
export const App = ()=>{
    return (
        <PlugProvider whitelist={whitelist}>
            {/* Providers want a single node underneath, which we wrangle with a fragment */}
            <Fragment> 
                {/* Wrapper  for tree in authenticated state */}
                <Authenticated> 
                    {/* Your app, which can use usePlug */}
                    <Main />
                </Authenticated>
                {/* Wrapper for for unauthenticated state */}
                <Unauthenticated> 
                    {/* App in logged out state - we have a pre-built login screen for you */}
                    <LoggedOut /> 
                </Unauthenticated>
            </Fragment>
        </PlugProvider>
    );
}
```
## usePlug() => PlugContext
Access the context of your instantiated Plug connection from any component beneath your PlugProvider. 

The shape of the plug Provider context is:
  * `authenticated`: Whether the plug wallet is in an authenticated state (boolean)
  * `principal`: Principal authenticated with the plug wallet (Principal or undefined)
  * `agent`: Plug-mediated agent for talking with IC canisters (HttpAgent)
  * `login`: Trigger plug wallet interactive authentication (()=>void)
  * `logout` Unauthenticate from plug wallet (presently just reloads the window, since plug doesn't maintain auth across reloads) (()=>void)
  * `createActor`: Pass-through to the plug wallet createActor function
  * `requestBalance`:Pass-through to the plug wallet requestBalance function
  * `requestTransfer`:Pass-through to the plug wallet requestTransfer function
  * `batchTransactions`:Pass-through to the plug wallet batchTransactions function

### Example - Authenticated Path
```ts
import { usePlug, useActor } from '@raydeck/useplug'
import { _SERVICE} from '../../src/declarations/mycanister/mycanister.did';
import {idlFactory} from '../../src/declarations/mycanister'

const canisterid = process.env.MYCANISTER_CANISTER_ID;
export const Main = ()=>{
    const {
        principal, 
        logout, /* attach to a logout button*/
        createActor, /* could use, but useActor is easier (below) */
        requestBalance,
        requestTransfer,
        batchTransactions,
    } = usePlug();
}
```
## useActor<ServiceType>(id: String, factory: InterfaceFactory) => ActorSubclass<ServiceType> | undefined
This one is cool. useActor wraps the concerns of instantiating an actor to communicate with your canister mediated by Plug. Plus, it keeps your Typescript types auto-generated by `dfx generate` so all the auto-complete stays with you. 

### Example
```tsx
import { useActor } from "@raydeck/useplug"
import { _SERVICE} from '../../src/declarations/mycanister/mycanister.did';
import {idlFactory} from '../../src/declarations/mycanister'

const useMyCanister = ()=>{
    return useActor<_SERVICE>(canisterId, idlFactory);
}

const MyComponent = ()=>{
    const myCanister = useMyCanister()
}
```

<a name="_librarymd"></a>

@raydeck/useplug - v1.0.3

# @raydeck/useplug - v1.0.3

## Table of contents

### Type aliases

- [Balance](#balance)
- [BatchTransactionResponse](#batchtransactionresponse)
- [CanisterId](#canisterid)
- [CreateActorParams](#createactorparams)
- [Cycles](#cycles)
- [Plug](#plug)
- [PublicKey](#publickey)
- [RequestBurnXTCParams](#requestburnxtcparams)
- [RequestConnectParams](#requestconnectparams)
- [RequestTransferParams](#requesttransferparams)
- [RequestTransferResponse](#requesttransferresponse)

### Variables

- [Authenticated](#authenticated)
- [PlugProvider](#plugprovider)
- [Unauthenticated](#unauthenticated)
- [plug](#plug)

### Functions

- [PlugButton](#plugbutton)
- [useActor](#useactor)
- [usePlug](#useplug)

## Type aliases

### Balance

Ƭ **Balance**: `Object`

#### Type declaration

| Name | Type |
| :------ | :------ |
| `amount` | `number` |
| `currency` | `string` |
| `image` | `string` |
| `name` | `string` |
| `value` | `number` |

#### Defined in

[PlugProvider.tsx:26](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L26)

___

### BatchTransactionResponse

Ƭ **BatchTransactionResponse**: `Object`

#### Defined in

[PlugProvider.tsx:58](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L58)

___

### CanisterId

Ƭ **CanisterId**: `string`

#### Defined in

[PlugProvider.tsx:47](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L47)

___

### CreateActorParams

Ƭ **CreateActorParams**: `Object`

#### Type declaration

| Name | Type |
| :------ | :------ |
| `canisterId` | `string` |
| `interfaceFactory` | `InterfaceFactory` |

#### Defined in

[PlugProvider.tsx:22](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L22)

___

### Cycles

Ƭ **Cycles**: `number`

#### Defined in

[PlugProvider.tsx:46](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L46)

___

### Plug

Ƭ **Plug**: `Object`

#### Type declaration

| Name | Type |
| :------ | :------ |
| `agent` | `HttpAgent` |
| `batchTransactions` | (`transactions`: `Transaction`[]) => `Promise`<[`BatchTransactionResponse`](#batchtransactionresponse)\> |
| `createActor` | <T\>(`params`: [`CreateActorParams`](#createactorparams)) => `Promise`<`ActorSubclass`<`T`\>\> |
| `isConnected` | () => `boolean` |
| `requestBalance` | () => `Promise`<[`Balance`](#balance)[]\> |
| `requestBurnXTC` | (`params`: [`RequestBurnXTCParams`](#requestburnxtcparams)) => `Promise`<[`RequestTransferResponse`](#requesttransferresponse)\> |
| `requestConnect` | (`o?`: [`RequestConnectParams`](#requestconnectparams)) => `Promise`<`any`\> |
| `requestTransfer` | (`params`: [`RequestTransferParams`](#requesttransferparams)) => `Promise`<[`RequestTransferResponse`](#requesttransferresponse)\> |

#### Defined in

[PlugProvider.tsx:59](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L59)

___

### PublicKey

Ƭ **PublicKey**: `any`

#### Defined in

[PlugProvider.tsx:21](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L21)

___

### RequestBurnXTCParams

Ƭ **RequestBurnXTCParams**: `Object`

#### Type declaration

| Name | Type |
| :------ | :------ |
| `amount` | [`Cycles`](#cycles) |
| `to` | [`CanisterId`](#canisterid) |

#### Defined in

[PlugProvider.tsx:48](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L48)

___

### RequestConnectParams

Ƭ **RequestConnectParams**: `Object`

#### Type declaration

| Name | Type |
| :------ | :------ |
| `host?` | `string` |
| `timeout?` | `number` |
| `whitelist?` | `string`[] |

#### Defined in

[PlugProvider.tsx:16](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L16)

___

### RequestTransferParams

Ƭ **RequestTransferParams**: `Object`

#### Type declaration

| Name | Type |
| :------ | :------ |
| `amount` | `number` |
| `opts?` | `Object` |
| `opts.created_at_time?` | `Object` |
| `opts.created_at_time.timestamp_nanos` | `number` |
| `opts.fee?` | `number` |
| `opts.from_subaccount?` | `Number` |
| `opts.memo?` | `string` |
| `to` | `String` |

#### Defined in

[PlugProvider.tsx:33](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L33)

___

### RequestTransferResponse

Ƭ **RequestTransferResponse**: `Object`

#### Type declaration

| Name | Type |
| :------ | :------ |
| `height` | `number` |

#### Defined in

[PlugProvider.tsx:45](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L45)

## Variables

### Authenticated

• **Authenticated**: `FC`<`Object`\>

#### Defined in

[PlugProvider.tsx:172](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L172)

___

### PlugProvider

• **PlugProvider**: `FC`<`Object`\>

#### Defined in

[PlugProvider.tsx:106](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L106)

___

### Unauthenticated

• **Unauthenticated**: `FC`<`Object`\>

#### Defined in

[PlugProvider.tsx:178](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L178)

___

### plug

• **plug**: `undefined` \| [`Plug`](#plug)

#### Defined in

[PlugProvider.tsx:87](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L87)

## Functions

### PlugButton

▸ `Const` **PlugButton**(`__namedParameters`): `Element`

#### Parameters

| Name | Type |
| :------ | :------ |
| `__namedParameters` | `Object` |
| `__namedParameters.dark?` | `boolean` |
| `__namedParameters.title?` | `string` |

#### Returns

`Element`

#### Defined in

[PlugButton.tsx:53](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugButton.tsx#L53)

___

### useActor

▸ **useActor**<`T`\>(`canisterId`, `interfaceFactory`): `undefined` \| `ActorSubclass`<`T`\>

#### Type parameters

| Name |
| :------ |
| `T` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `canisterId` | `string` |
| `interfaceFactory` | `InterfaceFactory` |

#### Returns

`undefined` \| `ActorSubclass`<`T`\>

#### Defined in

[useActor.ts:6](https://github.com/rhdeck/useplug/blob/434b7f8/src/useActor.ts#L6)

___

### usePlug

▸ `Const` **usePlug**(): `Object`

#### Returns

`Object`

| Name | Type |
| :------ | :------ |
| `agent` | `undefined` \| `HttpAgent` |
| `authenticated` | `boolean` |
| `batchTransactions` | `undefined` \| (`transactions`: `Transaction`[]) => `Promise`<[`BatchTransactionResponse`](#batchtransactionresponse)\> |
| `createActor` | `undefined` \| <T\>(`params`: [`CreateActorParams`](#createactorparams)) => `Promise`<`ActorSubclass`<`T`\>\> |
| `login` | () => `void` |
| `logout` | () => `void` |
| `plug` | `undefined` \| [`Plug`](#plug) |
| `principal` | ``null`` \| `Principal` |
| `requestBalance` | `undefined` \| () => `Promise`<[`Balance`](#balance)[]\> |
| `requestTransfer` | `undefined` \| (`params`: [`RequestTransferParams`](#requesttransferparams)) => `Promise`<[`RequestTransferResponse`](#requesttransferresponse)\> |

#### Defined in

[PlugProvider.tsx:166](https://github.com/rhdeck/useplug/blob/434b7f8/src/PlugProvider.tsx#L166)