Skip to content

Commit

Permalink
feat: add Custom provider (#181)
Browse files Browse the repository at this point in the history
* feat: add Custom provider

* test: handle missing provider option in constructor

* feat: optional methods in `CustomProvider`

* docs: update README with Custom Provider

* fix: add CustomWallet to createWalletMap()
  • Loading branch information
drichar authored Jun 5, 2024
1 parent 9be225a commit bdaf3ee
Show file tree
Hide file tree
Showing 6 changed files with 611 additions and 0 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,77 @@ The following examples demonstrate how to use the `use-wallet` v3 library in var

- [Vanilla TypeScript (Vite)](https://github.com/TxnLab/use-wallet/tree/v3/examples/vanilla-ts)

## Custom Provider

If you want to integrate a wallet provider that is not included in the library, or if your application requires any additional custom interactions, you can create a custom provider.

1. Create a new class that implements the `CustomProvider` type

```ts
import { CustomProvider, WalletAccount } from '@txnlab/use-wallet' // Or any framework adapter
class ExampleProvider implements CustomProvider {
/* Required */
async connect(args?: Record<string, any>): Promise<WalletAccount[]> {
// Must return an array of connected accounts
// Optional `args` parameter can be used to pass any additional configuration
}

/* Optional */
async disconnect(): Promise<void> {
// Disconnect from the wallet provider, if necessary
}

/* Optional */
async resumeSession(): Promise<WalletAccount[] | void> {
// Reconnect to the wallet provider when the app mounts, if necessary
// If an array of accounts is returned, they are checked against the stored accounts
// The stored accounts are updated if they differ
}

/* The two signing methods are optional, but you'll want to define at least one! */

async signTransactions(
txnGroup: algosdk.Transaction[] | algosdk.Transaction[][] | Uint8Array[] | Uint8Array[][],
indexesToSign?: number[],
returnGroup?: boolean
): Promise<Uint8Array[]> {
// Sign transactions with the wallet
// Return signed transactions only or the original group if `returnGroup` is true
}

async transactionSigner(
txnGroup: algosdk.Transaction[],
indexesToSign: number[]
): Promise<Uint8Array[]> {
// Sign an array of transaction objects with the wallet
// Return signed transactions only
// Compatible with algosdk's Atomic Transaction Composer
}
}
```

2. Add the provider to the `WalletManager` configuration

```ts
const walletManager = new WalletManager({
wallets: [
// Include the custom provider in the wallets array
{
id: WalletId.CUSTOM,
options: {
provider: new ExampleProvider()
},
metadata: {
name: 'Example Wallet'
icon: 'data:image/svg+xml;base64,...'
}
}
],
network: NetworkId.TESTNET
})
```

## WalletManager API

The following API is exposed via the `WalletManager` class instance. The framework adapters abstract the `WalletManager` class and expose a similar API via the `useWallet` function.
Expand Down
Loading

0 comments on commit bdaf3ee

Please sign in to comment.