# Integrating Leap Wallet

#### Detecting Leap Wallet

To seamlessly integrate Leap Wallet into your Solana application, use the [@wallet-standard/core](https://www.npmjs.com/package/@wallet-standard/core) package. This standardized approach ensures consistent wallet detection and interaction across different providers.

#### Prerequisites

Before you begin, install the required package using your preferred package manager:

```bash
# Using NPM
npm i @wallet-standard/core

# Using Yarn
yarn add @wallet-standard/core
```

#### **Implementing Wallet Detection**

After installing the package, use the following code snippet to detect Solana-compatible wallets, including Leap:

```tsx
import { getWallets } from '@wallet-standard/core'

const { get } = getWallets()

const allWallets = get()
```

Next, filter out any wallets that are not compatible with the solana standard.

```tsx
import { isWalletAdapterCompatibleStandardWallet }from '@solana/wallet-adapter-base'

const solanaWallets = allWallets.filter(isWalletAdapterCompatibleStandardWallet)
```

Once you've identified the Leap wallet within the `solanaWallets` array, you'll gain access to various functions like `connect`, which we’ll cover in the next section.

### **Alternative Detection Method**

You can also access the Leap wallet directly through the window object:

```tsx
const leapWallet = window.leap?.solana
```

This method provides direct access to the Leap wallet interface when available in the browser environment.

Note: Make sure to implement proper error handling and wallet availability checks in your application to ensure a smooth user experience.

#### Enable Connection

The wallet connection can be established using the connect method. If the wallet is locked, it will redirect to the extension login page. If the user hasn't permitted the webpage, it will ask for permission.

```tsx
import { StandardConnectOutput } from '@wallet-standard/base';

const { accounts }: StandardConnectOutput = await window.leap.solana.connect({
    silent?: boolean // Optional: If true, won't prompt if already connected
});
```

#### Disconnect

Disconnect the wallet from the dApp.

```tsx
await window.leap.solana.disconnect() // void function
```

#### Sign In

Authenticate with a dApp using Sign In with Solana.

```tsx
import { SolanaSignInInput, SolanaSignInOutput } from '@solana/wallet-standard-features';

const input: SolanaSignInInput[] = {
    domain: string, // The domain requesting sign in
    statement?: string, // Optional statement to display
    uri?: string, // Optional URI
    version?: string, // Optional version
    chainId?: string, // Optional chain ID
    nonce?: string, // Optional nonce
    issuedAt?: string, // Optional issuance timestamp
    expirationTime?: string, // Optional expiration time
    notBefore?: string, // Optional not-before time
    requestId?: string, // Optional request ID
    resources?: string[] // Optional array of resources
};

const result: SolanaSignInOutput[] = await window.leap.solana.signIn(input);
// Returns { account: WalletAccount, signedMessage: Uint8Array, signature: Uint8Array }
```

#### Sign Message

Sign an arbitrary message.

```tsx
import { SolanaSignMessageInput, SolanaSignMessageOutput } from '@solana/wallet-standard-features';
import type { WalletAccount } from '@wallet-standard/base';

const input: SolanaSignMessageInput[] = {
    message: Uint8Array, // The message to sign
    account: WalletAccount // The account to sign with
};

const signed: SolanaSignMessageOutput[] = await window.leap.solana.signMessage(input);
// Returns { signedMessage: Uint8Array, signature: Uint8Array }
```

#### Sign Transaction

Sign a transaction without broadcasting it.

```tsx
import { SolanaSignTransactionInput, SolanaSignTransactionOutput, SolanaSignTransactionOptions } from '@solana/wallet-standard-features';
import type { WalletAccount, IdentifierString } from '@wallet-standard/base';

const input: SolanaSignTransactionInput[] = {
    transaction: Uint8Array, // The transaction to sign
    account: WalletAccount, // The account to sign with
    chain?: IdentifierString // Optional: defaults to 'solana:mainnet'
    options?: SolanaSignTransactionOptions {
        // Optional transaction options
    }
};

const signed: SolanaSignTransactionOutput[] = await window.leap.solana.signTransaction(input);
// Returns { signedTransaction: Uint8Array }
```

#### Sign and Send Transaction

Sign and broadcast a transaction to the network.

```tsx
import { SolanaSignAndSendTransactionInput, SolanaSignAndSendTransactionOutput } from '@solana/wallet-standard-features';
import type { WalletAccount } from '@wallet-standard/base';

const input: SolanaSignAndSendTransactionInput = {
    transaction: Uint8Array, // The transaction to sign and send
    account: WalletAccount, // The account to sign with
    chain: IdentifierString, // The chain to broadcast transaction
    options?: {
        // Optional transaction options
    }
};

const result: SolanaSignAndSendTransactionOutput = await window.leap.solana.signAndSendTransaction(input);
// Returns { signature: Uint8Array }
```

#### Change Network

Switch between different Solana networks.

```tsx
interface ChangeNetworkInput {
  genesisHash: string;
  url?: string;
}

await window.leap.solana.changeNetwork({
  genesisHash,
  url
})

// Returns
interface ChangeNetworkResponse {
  status: UserResponseStatus;
  args?: {
    success: boolean;
    chainId?: string;
  }
}
```

#### Example Usage with Web3.js

```tsx
import { Connection, PublicKey, Transaction } from '@solana/web3.js';

// Connect to wallet
const response = await window.leap.solana.connect();
const account = response.accounts[0];

// Create a connection to Solana network
const connection = new Connection('<https://api.mainnet-beta.solana.com>');

// Create a transaction
const transaction = new Transaction();
// Add your instructions to the transaction

// Sign and send the transaction
const signedTx = await window.leap.solana.signAndSendTransaction({
  transaction: transaction.serialize(),
  account: account
  chain: 'solana:mainnet'
});

// Get the signature
const signature = signedTx.signature;
```

#### Events

The wallet provider emits events that you can listen to:

```tsx
window.leap.solana.on('change', ({ accounts }) => {
  // Handle account changes
});
```

Note: All methods may throw errors if the wallet is locked, user rejects the request, or if there are network issues. Make sure to implement proper error handling in your dApp.
