1.0.3 • Published 2 years ago

@raydeck/useplug v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

usePlug - React Wrapper for Plug Wallet

Plug Wallet 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 for putting on the Supernova Hackathon when we created this tool. And our thanks to Psychedelic for making Plug Wallet and open-sourcing their work so we could make it easy for others.

License

MIT License - use, share and enjoy!

Installation

**Prequisite: A React app (often built with npx create-react-app);

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

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

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(id: String, factory: InterfaceFactory) => ActorSubclass | 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

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()
}

@raydeck/useplug - v1.0.3

@raydeck/useplug - v1.0.3

Table of contents

Type aliases

Variables

Functions

Type aliases

Balance

Ƭ Balance: Object

Type declaration

NameType
amountnumber
currencystring
imagestring
namestring
valuenumber

Defined in

PlugProvider.tsx:26


BatchTransactionResponse

Ƭ BatchTransactionResponse: Object

Defined in

PlugProvider.tsx:58


CanisterId

Ƭ CanisterId: string

Defined in

PlugProvider.tsx:47


CreateActorParams

Ƭ CreateActorParams: Object

Type declaration

NameType
canisterIdstring
interfaceFactoryInterfaceFactory

Defined in

PlugProvider.tsx:22


Cycles

Ƭ Cycles: number

Defined in

PlugProvider.tsx:46


Plug

Ƭ Plug: Object

Type declaration

NameType
agentHttpAgent
batchTransactions(transactions: Transaction[]) => Promise<BatchTransactionResponse>
createActor<T>(params: CreateActorParams) => Promise<ActorSubclass<T>>
isConnected() => boolean
requestBalance() => Promise<Balance[]>
requestBurnXTC(params: RequestBurnXTCParams) => Promise<RequestTransferResponse>
requestConnect(o?: RequestConnectParams) => Promise<any>
requestTransfer(params: RequestTransferParams) => Promise<RequestTransferResponse>

Defined in

PlugProvider.tsx:59


PublicKey

Ƭ PublicKey: any

Defined in

PlugProvider.tsx:21


RequestBurnXTCParams

Ƭ RequestBurnXTCParams: Object

Type declaration

NameType
amountCycles
toCanisterId

Defined in

PlugProvider.tsx:48


RequestConnectParams

Ƭ RequestConnectParams: Object

Type declaration

NameType
host?string
timeout?number
whitelist?string[]

Defined in

PlugProvider.tsx:16


RequestTransferParams

Ƭ RequestTransferParams: Object

Type declaration

NameType
amountnumber
opts?Object
opts.created_at_time?Object
opts.created_at_time.timestamp_nanosnumber
opts.fee?number
opts.from_subaccount?Number
opts.memo?string
toString

Defined in

PlugProvider.tsx:33


RequestTransferResponse

Ƭ RequestTransferResponse: Object

Type declaration

NameType
heightnumber

Defined in

PlugProvider.tsx:45

Variables

Authenticated

Authenticated: FC<Object>

Defined in

PlugProvider.tsx:172


PlugProvider

PlugProvider: FC<Object>

Defined in

PlugProvider.tsx:106


Unauthenticated

Unauthenticated: FC<Object>

Defined in

PlugProvider.tsx:178


plug

plug: undefined | Plug

Defined in

PlugProvider.tsx:87

Functions

PlugButton

Const PlugButton(__namedParameters): Element

Parameters

NameType
__namedParametersObject
__namedParameters.dark?boolean
__namedParameters.title?string

Returns

Element

Defined in

PlugButton.tsx:53


useActor

useActor<T>(canisterId, interfaceFactory): undefined | ActorSubclass<T>

Type parameters

Name
T

Parameters

NameType
canisterIdstring
interfaceFactoryInterfaceFactory

Returns

undefined | ActorSubclass<T>

Defined in

useActor.ts:6


usePlug

Const usePlug(): Object

Returns

Object

NameType
agentundefined | HttpAgent
authenticatedboolean
batchTransactionsundefined | (transactions: Transaction[]) => Promise<BatchTransactionResponse>
createActorundefined | <T>(params: CreateActorParams) => Promise<ActorSubclass<T>>
login() => void
logout() => void
plugundefined | Plug
principalnull | Principal
requestBalanceundefined | () => Promise<Balance[]>
requestTransferundefined | (params: RequestTransferParams) => Promise<RequestTransferResponse>

Defined in

PlugProvider.tsx:166