# Methods

### connect

```typescript
connect(provider: string, options: any): Promise<AccountType|undefined>
```

Call the `connect()` method using a provider name as the first parameter to request a connection using that wallet or login mechanism.

Some providers may need an options argument to indicate the connection behavior requested.

### decryptMessage

```typescript
decryptMessage(encryptedMessage: string, signingPublicKey: string): Promise<DecryptMessageResult | undefined>;
```

Triggers the mechanisms to request your user to decrypt a message with the active wallet.

`signingPublicKey` MUST be the public key for the active account. Otherwise, this method will return an error.

A [DecryptMessageResult ](https://docs.cspr.click/cspr.click-sdk/types#decryptmessageresult)object is returned with the decrypted message or an `error`.

### disconnect

```typescript
 disconnect(fromWallet: string, options?: any): void
```

Usually you will call `signOut()` method to close a user session. Use `disconnect()` when you want to clear the connection between the wallet and your app. Next time the user signs in with that wallet, he'll must gran connection permission again.

Send empty arguments or empty string to disconnect from currently active account. Or call disconnect with a wallet provider key to force the disconnection of a specific wallet.

### encryptMessage

```typescript
encryptMessage(message: string, signingPublicKey: string): Promise<EncryptMessageResult | undefined>;
```

Triggers the mechanisms to request the active wallet to encrypt a message with the user's public key.

`signingPublicKey` MUST be the public key for the active account. Otherwise, this method will return an error.

A [EncryptMessageResult ](https://docs.cspr.click/cspr.click-sdk/types#encryptmessageresult)object is returned with the encrypted message or an `error`.

### forgetAccount

```typescript
forgetAccount(account: AccountType): void
```

Removes an account from the list of known accounts in CSPR.click. It won’t be returned to the list of known accounts unless it’s connected again using the `connect()` method.

### getAccountIdenticon

`typescript` getAccountIdenticon(hex: string, size: number = 20): HTMLCanvasElement

````

This method generates a `<canvas>` HTML element to display the account identicon (aka avatar) of the public key.

It can be used with an account hash to display the hash ('#') symbol instead of a public key identicon.

On React, the `<AccountIdenticon>` component is available to wrap the call to this method.

### getActiveAccount

```typescript
getActiveAccount(): AccountType | null 
````

Gets the account for the current session (if any). Or `undefined` if there is no active session.

### getActiveAccountAsync

```typescript
getActiveAccountAsync(options?: GetActiveAccountOptions): Promise<AccountType | null> 
```

Gets the account for the current session (if any). Returns `undefined` if there is no active session.

Pass `options.withBalance = true` to include the balance of the account.

### getActivePublicKey

```typescript
getActivePublicKey(): Promise<string | undefined>
```

Gets the public key for the current session (if any). Or undefined if no active session.

### getCsprCloudProxy

```typescript
getCsprCloudProxy(): ICsprCloudProxy
```

Returns a CSPR.cloud proxy object that can be used to interact with the CSPR.cloud REST and Streaming APIs, as well to set up an Node RPC client with `casper-js-sdk`.

### getProviderInfo

```typescript
getProviderInfo(provider?: string): Promise<ProviderInfo|undefined>
```

Returns a [ProviderInfo](https://docs.cspr.click/cspr.click-sdk/types#providerinfo) object containing the information of the connected wallet, or the specified in the `provider` argument. Keep in mind that some information is only available if the wallet is connected (e.g. Version of Ledger can only be recovered if the hardware device is connected).

### getSignInOptions

```typescript
getSignInOptions(refresh: boolean = false): Promise<any>
```

Returns an object with a list of providers enabled to use in the application and a list of known accounts that can be used to sign in automatically with `signInWithAccount()`.

### init

```typescript
init(options: CsprClickInitOptions): void
```

Call `init` to initialize CSPR.click in your web application. This MUST be the first method you call after the downloading of the library.

See [CsprClickInitOptions ](https://docs.cspr.click/cspr.click-sdk/types#csprclickinitoptions)for reference on the `options` parameter.

### isConnected

```typescript
isConnected(provider: string): Promise<boolean | undefined>
```

Checks if the provider (not the account) indicated as the first argument is connected to the application. Note this check is independent of whether there's an active account on CSPR.click or not or even if that account belongs to the given provider.

### isProviderPresent

```typescript
isProviderPresent(provider: string): boolean
```

Checks if the provider indicated as the first argument is enabled in the application and installed (in case it’s a browser extension).

### isUnlocked

```typescript
isUnlocked(provider: string): Promise<boolean | undefined>
```

Returns `true` if the provider is unlocked. `false` if the provider is locked.

This method returns `undefined` when the provider does not offer this information.

### send

```typescript
send(transactionJSON: string | object, 
     signingPublicKey: string,
     onStatusUpdate: ((status: string, data: any) => void) | undefined = undefined,
     timeout: number = 120
  ): Promise<SendResult | undefined>
```

Triggers the mechanisms to request your user to sign a transaction with the active wallet.

When the user approves the signature, CSPR.click sends the transaction to the Casper network. A [SendResult ](https://docs.cspr.click/cspr.click-sdk/types#sendresult)object is returned with status information.

The `transactionJSON` is a json object (or a string) containing either a `Deploy` or a `TransactionV1`.

**Note:** If you're working with `Transaction` objects from `casper-js-sdk`, use the `transaction.toJSON()` method to get the object to pass as the first argument.

`signingPublicKey` MUST be the public key for the active account. Otherwise, this method will return an error.

Use `onStatusUpdate` to establish a websockets communication with CSPR.click backend and receive updates on the processing of the transaction sent.

There's a default waiting time of `120sec`. If the transaction is not processed in that time, the caller receives a `timeout` status update and the websocket connection is closed.

Example (see full example in CSPR.click project template):

```typescript
/* build a transaction object using Casper JS SDK */
const senderPk = activeAccount?.public_key?.toLowerCase() || '';
const transaction = new NativeTransferBuilder()
    .from(PublicKey.fromHex(senderPk))
    .target(PublicKey.fromHex(recipientPk))
    .amount('6' + '000000000')
    .id(Date.now())
    .chainName(clickRef.chainName!)
    .payment(100_000_000)
    .build();

/* define a status callback method to get updates during the transaction processing. 
   use this callback to update your UI on each step of the processing
*/
const onStatusUpdate = (status: string, data: any) => {
    console.log('STATUS UPDATE', status, data);
    if(status === TransactionStatus.SENT)
      setWaitingIndicator();
    if(status === TransactionStatus.PROCESSED)
      parseProcessedTransaction();
  };    

/* request a transaction signature and deploy the transaction via a node proxy */  
window.csprclick
  .send(transaction.toJSON() as object, sender, onStatusUpdate)
  .then((res: SendResult) => {
    // check result and update UI accordingly
  })
  .catch((err: any) => {
    alert('Error: ' + err);
    throw err;
  });
```

Check a list with possible processing status in the [SendResult](https://docs.cspr.click/cspr.click-sdk/types#sendresult) type reference.

### sign

```typescript
sign(transactionJSON: string | object, signingPublicKey: string): Promise<SignResult | undefined>
```

Triggers the mechanisms to request your user to sign a transaction with the active wallet.

A [SignResult ](https://docs.cspr.click/cspr.click-sdk/types#signresult)object is returned with the signature value or an `error`.

The `transactionJSON` is a json object (or a string) containing either a `Deploy` or a `TransactionV1`.

**Note:** If you're working with `Transaction` objects from `casper-js-sdk`, use the `transaction.toJSON()` method to get the object to pass as the first argument.

`signingPublicKey` MUST be the public key for the active account. Otherwise, this method will return an error.

### signIn

```typescript
signIn(): void
```

Triggers a request to a UI library to show a sign-in dialog.

### signInWithAccount

```typescript
signInWithAccount(account: AccountType): Promise<AccountType | undefined>
```

Starts a session with the indicated account. This account must be one of the accounts returned in `getKnownAccounts` or `getSignInOptions`.

Note that no interaction with the account provider is required to sign-in. CSPR.click will check and restore the connection if needed when there's a transaction or message to sign.

### signMessage

```typescript
signMessage(message: string, signingPublicKey: string): Promise<SignResult|undefined>
```

Triggers the mechanisms to request your user to sign a text message with the active wallet.

`signingPublicKey` MUST be the public key for the active account. Otherwise, this method will return an error.

### signOut

```typescript
signOut(): void
```

Closes an active session in your dApp.

Triggers the [`csprclick:signed_out`](https://docs.cspr.click/cspr.click-sdk/events#csprclick-signed_out) event.

### showBuyCsprUi

```typescript
showBuyCsprUi(): void
```

Displays the Buy CSPR widget. This widget allows the user to top-up his account with a credit card payment.

### switchAccount

```typescript
switchAccount(withProvider: string | undefined, options?: any): Promise<void>
```

Call this method to request to the specified wallet to offer the user the selection of a different account. This is valid for providers with its own UI (like browser extenstions).

Call this method without any provider to request CSPR.click UI to show the Switch Account modal window.
