@txnlab/use-wallet 2.8.0 → 3.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1009
- package/dist/index.cjs +2060 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +511 -0
- package/dist/index.d.ts +436 -902
- package/dist/index.js +2012 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -119
- package/dist/cjs/index.js +0 -3934
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/jest.config.d.ts +0 -11
- package/dist/cjs/jest.setup.d.ts +0 -0
- package/dist/cjs/src/algod/index.d.ts +0 -10
- package/dist/cjs/src/clients/algosigner/client.d.ts +0 -37
- package/dist/cjs/src/clients/algosigner/constants.d.ts +0 -2
- package/dist/cjs/src/clients/algosigner/index.d.ts +0 -2
- package/dist/cjs/src/clients/algosigner/types.d.ts +0 -39
- package/dist/cjs/src/clients/base/base.d.ts +0 -46
- package/dist/cjs/src/clients/base/index.d.ts +0 -2
- package/dist/cjs/src/clients/custom/client.d.ts +0 -18
- package/dist/cjs/src/clients/custom/constants.d.ts +0 -1
- package/dist/cjs/src/clients/custom/index.d.ts +0 -2
- package/dist/cjs/src/clients/custom/types.d.ts +0 -25
- package/dist/cjs/src/clients/daffi/client.d.ts +0 -34
- package/dist/cjs/src/clients/daffi/constants.d.ts +0 -1
- package/dist/cjs/src/clients/daffi/index.d.ts +0 -2
- package/dist/cjs/src/clients/daffi/types.d.ts +0 -27
- package/dist/cjs/src/clients/defly/client.d.ts +0 -34
- package/dist/cjs/src/clients/defly/constants.d.ts +0 -1
- package/dist/cjs/src/clients/defly/index.d.ts +0 -2
- package/dist/cjs/src/clients/defly/types.d.ts +0 -32
- package/dist/cjs/src/clients/exodus/client.d.ts +0 -33
- package/dist/cjs/src/clients/exodus/constants.d.ts +0 -1
- package/dist/cjs/src/clients/exodus/index.d.ts +0 -2
- package/dist/cjs/src/clients/exodus/types.d.ts +0 -34
- package/dist/cjs/src/clients/index.d.ts +0 -19
- package/dist/cjs/src/clients/kibisis/client.d.ts +0 -73
- package/dist/cjs/src/clients/kibisis/constants.d.ts +0 -37
- package/dist/cjs/src/clients/kibisis/index.d.ts +0 -2
- package/dist/cjs/src/clients/kibisis/types.d.ts +0 -86
- package/dist/cjs/src/clients/kibisis/utils.d.ts +0 -9
- package/dist/cjs/src/clients/kibisis/utils.test.d.ts +0 -1
- package/dist/cjs/src/clients/kmd/client.d.ts +0 -30
- package/dist/cjs/src/clients/kmd/constants.d.ts +0 -1
- package/dist/cjs/src/clients/kmd/index.d.ts +0 -2
- package/dist/cjs/src/clients/kmd/types.d.ts +0 -32
- package/dist/cjs/src/clients/lute/client.d.ts +0 -34
- package/dist/cjs/src/clients/lute/constants.d.ts +0 -1
- package/dist/cjs/src/clients/lute/index.d.ts +0 -2
- package/dist/cjs/src/clients/lute/types.d.ts +0 -15
- package/dist/cjs/src/clients/magic/client.d.ts +0 -35
- package/dist/cjs/src/clients/magic/constants.d.ts +0 -1
- package/dist/cjs/src/clients/magic/index.d.ts +0 -2
- package/dist/cjs/src/clients/magic/types.d.ts +0 -27
- package/dist/cjs/src/clients/mnemonic/client.d.ts +0 -35
- package/dist/cjs/src/clients/mnemonic/constants.d.ts +0 -1
- package/dist/cjs/src/clients/mnemonic/index.d.ts +0 -2
- package/dist/cjs/src/clients/mnemonic/types.d.ts +0 -21
- package/dist/cjs/src/clients/myalgo/client.d.ts +0 -33
- package/dist/cjs/src/clients/myalgo/constants.d.ts +0 -1
- package/dist/cjs/src/clients/myalgo/index.d.ts +0 -2
- package/dist/cjs/src/clients/myalgo/types.d.ts +0 -19
- package/dist/cjs/src/clients/pera/client.d.ts +0 -34
- package/dist/cjs/src/clients/pera/constants.d.ts +0 -1
- package/dist/cjs/src/clients/pera/index.d.ts +0 -2
- package/dist/cjs/src/clients/pera/types.d.ts +0 -27
- package/dist/cjs/src/clients/walletconnect2/client.d.ts +0 -36
- package/dist/cjs/src/clients/walletconnect2/constants.d.ts +0 -5
- package/dist/cjs/src/clients/walletconnect2/index.d.ts +0 -2
- package/dist/cjs/src/clients/walletconnect2/types.d.ts +0 -28
- package/dist/cjs/src/clients/walletconnect2/utils.d.ts +0 -2
- package/dist/cjs/src/constants/constants.d.ts +0 -20
- package/dist/cjs/src/constants/index.d.ts +0 -1
- package/dist/cjs/src/context/WalletContext.d.ts +0 -8
- package/dist/cjs/src/hooks/index.d.ts +0 -2
- package/dist/cjs/src/hooks/useInitializeProviders.d.ts +0 -10
- package/dist/cjs/src/hooks/useInitializeProviders.test.d.ts +0 -4
- package/dist/cjs/src/hooks/useWallet.d.ts +0 -31
- package/dist/cjs/src/hooks/useWallet.test.d.ts +0 -4
- package/dist/cjs/src/index.d.ts +0 -6
- package/dist/cjs/src/media/audio.d.ts +0 -1
- package/dist/cjs/src/store/index.d.ts +0 -2
- package/dist/cjs/src/store/state/debugStore.d.ts +0 -6
- package/dist/cjs/src/store/state/walletStore.d.ts +0 -52
- package/dist/cjs/src/testUtils/mockAccounts.d.ts +0 -3
- package/dist/cjs/src/testUtils/mockClients.d.ts +0 -45
- package/dist/cjs/src/types/api.d.ts +0 -14
- package/dist/cjs/src/types/index.d.ts +0 -4
- package/dist/cjs/src/types/node.d.ts +0 -44
- package/dist/cjs/src/types/providers.d.ts +0 -148
- package/dist/cjs/src/types/utilities.d.ts +0 -1
- package/dist/cjs/src/types/wallet.d.ts +0 -54
- package/dist/cjs/src/utils/clearAccounts.d.ts +0 -2
- package/dist/cjs/src/utils/clearAccounts.test.d.ts +0 -1
- package/dist/cjs/src/utils/debugLog.d.ts +0 -3
- package/dist/cjs/src/utils/encodeNFDTransactionArray.spec.d.ts +0 -1
- package/dist/cjs/src/utils/encodeNFDTransactionsArray.d.ts +0 -2
- package/dist/cjs/src/utils/index.d.ts +0 -3
- package/dist/cjs/src/utils/initializeProviders.d.ts +0 -3
- package/dist/cjs/src/utils/initializeProviders.test.d.ts +0 -4
- package/dist/cjs/src/utils/providers.d.ts +0 -3
- package/dist/cjs/src/utils/providers.spec.d.ts +0 -1
- package/dist/cjs/src/utils/reconnectProviders.d.ts +0 -2
- package/dist/cjs/src/utils/reconnectProviders.test.d.ts +0 -1
- package/dist/cjs/src/utils/svgToBase64.d.ts +0 -1
- package/dist/cjs/src/utils/types.d.ts +0 -2
- package/dist/esm/index.js +0 -3911
- package/dist/esm/jest.config.d.ts +0 -11
- package/dist/esm/jest.setup.d.ts +0 -0
- package/dist/esm/src/algod/index.d.ts +0 -10
- package/dist/esm/src/clients/algosigner/client.d.ts +0 -37
- package/dist/esm/src/clients/algosigner/constants.d.ts +0 -2
- package/dist/esm/src/clients/algosigner/index.d.ts +0 -2
- package/dist/esm/src/clients/algosigner/types.d.ts +0 -39
- package/dist/esm/src/clients/base/base.d.ts +0 -46
- package/dist/esm/src/clients/base/index.d.ts +0 -2
- package/dist/esm/src/clients/custom/client.d.ts +0 -18
- package/dist/esm/src/clients/custom/constants.d.ts +0 -1
- package/dist/esm/src/clients/custom/index.d.ts +0 -2
- package/dist/esm/src/clients/custom/types.d.ts +0 -25
- package/dist/esm/src/clients/daffi/client.d.ts +0 -34
- package/dist/esm/src/clients/daffi/constants.d.ts +0 -1
- package/dist/esm/src/clients/daffi/index.d.ts +0 -2
- package/dist/esm/src/clients/daffi/types.d.ts +0 -27
- package/dist/esm/src/clients/defly/client.d.ts +0 -34
- package/dist/esm/src/clients/defly/constants.d.ts +0 -1
- package/dist/esm/src/clients/defly/index.d.ts +0 -2
- package/dist/esm/src/clients/defly/types.d.ts +0 -32
- package/dist/esm/src/clients/exodus/client.d.ts +0 -33
- package/dist/esm/src/clients/exodus/constants.d.ts +0 -1
- package/dist/esm/src/clients/exodus/index.d.ts +0 -2
- package/dist/esm/src/clients/exodus/types.d.ts +0 -34
- package/dist/esm/src/clients/index.d.ts +0 -19
- package/dist/esm/src/clients/kibisis/client.d.ts +0 -73
- package/dist/esm/src/clients/kibisis/constants.d.ts +0 -37
- package/dist/esm/src/clients/kibisis/index.d.ts +0 -2
- package/dist/esm/src/clients/kibisis/types.d.ts +0 -86
- package/dist/esm/src/clients/kibisis/utils.d.ts +0 -9
- package/dist/esm/src/clients/kibisis/utils.test.d.ts +0 -1
- package/dist/esm/src/clients/kmd/client.d.ts +0 -30
- package/dist/esm/src/clients/kmd/constants.d.ts +0 -1
- package/dist/esm/src/clients/kmd/index.d.ts +0 -2
- package/dist/esm/src/clients/kmd/types.d.ts +0 -32
- package/dist/esm/src/clients/lute/client.d.ts +0 -34
- package/dist/esm/src/clients/lute/constants.d.ts +0 -1
- package/dist/esm/src/clients/lute/index.d.ts +0 -2
- package/dist/esm/src/clients/lute/types.d.ts +0 -15
- package/dist/esm/src/clients/magic/client.d.ts +0 -35
- package/dist/esm/src/clients/magic/constants.d.ts +0 -1
- package/dist/esm/src/clients/magic/index.d.ts +0 -2
- package/dist/esm/src/clients/magic/types.d.ts +0 -27
- package/dist/esm/src/clients/mnemonic/client.d.ts +0 -35
- package/dist/esm/src/clients/mnemonic/constants.d.ts +0 -1
- package/dist/esm/src/clients/mnemonic/index.d.ts +0 -2
- package/dist/esm/src/clients/mnemonic/types.d.ts +0 -21
- package/dist/esm/src/clients/myalgo/client.d.ts +0 -33
- package/dist/esm/src/clients/myalgo/constants.d.ts +0 -1
- package/dist/esm/src/clients/myalgo/index.d.ts +0 -2
- package/dist/esm/src/clients/myalgo/types.d.ts +0 -19
- package/dist/esm/src/clients/pera/client.d.ts +0 -34
- package/dist/esm/src/clients/pera/constants.d.ts +0 -1
- package/dist/esm/src/clients/pera/index.d.ts +0 -2
- package/dist/esm/src/clients/pera/types.d.ts +0 -27
- package/dist/esm/src/clients/walletconnect2/client.d.ts +0 -36
- package/dist/esm/src/clients/walletconnect2/constants.d.ts +0 -5
- package/dist/esm/src/clients/walletconnect2/index.d.ts +0 -2
- package/dist/esm/src/clients/walletconnect2/types.d.ts +0 -28
- package/dist/esm/src/clients/walletconnect2/utils.d.ts +0 -2
- package/dist/esm/src/constants/constants.d.ts +0 -20
- package/dist/esm/src/constants/index.d.ts +0 -1
- package/dist/esm/src/context/WalletContext.d.ts +0 -8
- package/dist/esm/src/hooks/index.d.ts +0 -2
- package/dist/esm/src/hooks/useInitializeProviders.d.ts +0 -10
- package/dist/esm/src/hooks/useInitializeProviders.test.d.ts +0 -4
- package/dist/esm/src/hooks/useWallet.d.ts +0 -31
- package/dist/esm/src/hooks/useWallet.test.d.ts +0 -4
- package/dist/esm/src/index.d.ts +0 -6
- package/dist/esm/src/media/audio.d.ts +0 -1
- package/dist/esm/src/store/index.d.ts +0 -2
- package/dist/esm/src/store/state/debugStore.d.ts +0 -6
- package/dist/esm/src/store/state/walletStore.d.ts +0 -52
- package/dist/esm/src/testUtils/mockAccounts.d.ts +0 -3
- package/dist/esm/src/testUtils/mockClients.d.ts +0 -45
- package/dist/esm/src/types/api.d.ts +0 -14
- package/dist/esm/src/types/index.d.ts +0 -4
- package/dist/esm/src/types/node.d.ts +0 -44
- package/dist/esm/src/types/providers.d.ts +0 -148
- package/dist/esm/src/types/utilities.d.ts +0 -1
- package/dist/esm/src/types/wallet.d.ts +0 -54
- package/dist/esm/src/utils/clearAccounts.d.ts +0 -2
- package/dist/esm/src/utils/clearAccounts.test.d.ts +0 -1
- package/dist/esm/src/utils/debugLog.d.ts +0 -3
- package/dist/esm/src/utils/encodeNFDTransactionArray.spec.d.ts +0 -1
- package/dist/esm/src/utils/encodeNFDTransactionsArray.d.ts +0 -2
- package/dist/esm/src/utils/index.d.ts +0 -3
- package/dist/esm/src/utils/initializeProviders.d.ts +0 -3
- package/dist/esm/src/utils/initializeProviders.test.d.ts +0 -4
- package/dist/esm/src/utils/providers.d.ts +0 -3
- package/dist/esm/src/utils/providers.spec.d.ts +0 -1
- package/dist/esm/src/utils/reconnectProviders.d.ts +0 -2
- package/dist/esm/src/utils/reconnectProviders.test.d.ts +0 -1
- package/dist/esm/src/utils/svgToBase64.d.ts +0 -1
- package/dist/esm/src/utils/types.d.ts +0 -2
package/README.md
CHANGED
|
@@ -1,1013 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
# @txnlab/use-wallet
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
`@txnlab/use-wallet` is a React library that provides a simplified, consistent interface for integrating multiple Algorand wallets into your decentralized applications (dApps).
|
|
6
|
-
|
|
7
|
-
[](https://badge.fury.io/js/@txnlab%2Fuse-wallet)
|
|
8
|
-

|
|
9
|
-
|
|
10
|
-
## Overview
|
|
11
|
-
|
|
12
|
-
With UseWallet's hooks and utility functions, you can:
|
|
13
|
-
|
|
14
|
-
- Easily add or remove wallet support with a few lines of code
|
|
15
|
-
- Configure each wallet provider as needed for your application
|
|
16
|
-
- Allow users to easily switch between active accounts and wallet providers
|
|
17
|
-
- Sign and send transactions
|
|
18
|
-
- Restore sessions for returning users
|
|
19
|
-
|
|
20
|
-
It provides an abstraction layer that unifies the initialization, connection, and transaction signing logic, eliminating the need to interact with each wallet's individual API.
|
|
21
|
-
|
|
22
|
-
UseWallet supports most Algorand wallet providers, including Defly, Pera, Daffi, and Exodus (see [Supported Wallet Providers](#supported-wallet-providers) for the full list).
|
|
23
|
-
|
|
24
|
-
Version 2.x introduces [WalletConnect 2.0 support](#walletconnect-20-support).
|
|
25
|
-
|
|
26
|
-
## Disclaimer
|
|
27
|
-
|
|
28
|
-
TxnLab provides `@txnlab/use-wallet` as a wallet integration tool and does not endorse or assume responsibility for any external wallet providers' functionality or security.
|
|
29
|
-
|
|
30
|
-
## Table of Contents
|
|
31
|
-
|
|
32
|
-
- [Live Examples](#live-examples)
|
|
33
|
-
- [Requirements](#requirements)
|
|
34
|
-
- [Installation](#installation)
|
|
35
|
-
- [Initializing Providers](#initializing-providers)
|
|
36
|
-
- [The `useWallet` Hook](#the-usewallet-hook)
|
|
37
|
-
- [Type Definitions](#type-definitions)
|
|
38
|
-
- [Connect Menu](#connect-menu)
|
|
39
|
-
- [Displaying Account Details](#displaying-account-details)
|
|
40
|
-
- [Signing and Sending Transactions](#signing-and-sending-transactions)
|
|
41
|
-
- [Checking Connection Status](#checking-connection-status)
|
|
42
|
-
- [Supported Wallet Providers](#supported-wallet-providers)
|
|
43
|
-
- [Legacy Wallet Support](#legacy-wallet-support)
|
|
44
|
-
- [Provider Configuration](#provider-configuration)
|
|
45
|
-
- [Provider Definitions](#provider-definitions)
|
|
46
|
-
- [Node Configuration](#node-configuration)
|
|
47
|
-
- [Algosdk Static Import](#algosdk-static-import)
|
|
48
|
-
- [Full Configuration Example](#full-configuration-example)
|
|
49
|
-
- [WalletConnect 2.0 Support](#walletconnect-20-support)
|
|
50
|
-
- [Defly Wallet (iOS) beta](#defly-wallet-ios-beta)
|
|
51
|
-
- [Pera Wallet (Android) beta](#pera-wallet-android-beta)
|
|
52
|
-
- ["Module not found" errors in Next.js 13](#module-not-found-errors-in-nextjs-13)
|
|
53
|
-
- [Custom Provider](#custom-provider)
|
|
54
|
-
- [Custom Provider example](#custom-provider-example)
|
|
55
|
-
- [Initializing Custom Provider](#initializing-custom-provider)
|
|
56
|
-
- [Manual Provider (example implementation)](#manual-provider-example-implementation)
|
|
57
|
-
- [Migration Guide](#migration-guide)
|
|
58
|
-
- [Local Development](#local-development)
|
|
59
|
-
- [Support](#support)
|
|
60
|
-
- [Used By](#used-by)
|
|
61
|
-
- [License](#license)
|
|
62
|
-
|
|
63
|
-
## Live Examples
|
|
64
|
-
|
|
65
|
-
<!-- **Storybook demo** - https://txnlab.github.io/use-wallet -->
|
|
66
|
-
|
|
67
|
-
**Next.js example**
|
|
68
|
-
|
|
69
|
-
- Demo - https://next-use-wallet.vercel.app/
|
|
70
|
-
- Code - https://github.com/TxnLab/next-use-wallet
|
|
71
|
-
|
|
72
|
-
**NFDomains** - https://app.nf.domains/
|
|
73
|
-
|
|
74
|
-
## Requirements
|
|
75
|
-
|
|
76
|
-
Since this library uses [React Hooks](https://react.dev/reference/react), your app will need to be using React 16.8 or higher.
|
|
77
|
-
|
|
78
|
-
## Installation
|
|
79
|
-
|
|
80
|
-
Commands shown below use `npm install` but you can use `yarn add` or `pnpm add` instead.
|
|
81
|
-
|
|
82
|
-
First, install the library
|
|
83
|
-
|
|
84
|
-
```bash
|
|
85
|
-
npm install @txnlab/use-wallet
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
If you haven't already, install the Algorand JS SDK
|
|
89
|
-
|
|
90
|
-
```bash
|
|
91
|
-
npm install algosdk
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
Finally, install any peer dependencies for the wallets you wish to support. For example, to support Defly, Pera, and Daffi wallets:
|
|
95
|
-
|
|
96
|
-
```bash
|
|
97
|
-
npm install @blockshake/defly-connect @perawallet/connect @daffiwallet/connect
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
## Initializing Providers
|
|
101
|
-
|
|
102
|
-
In the root of your app, initialize the `WalletProvider` with the `useInitializeProviders` hook.
|
|
103
|
-
|
|
104
|
-
This example initializes Defly, Pera, Daffi, Exodus, Lute and Kibisis wallet providers. The default node configuration (mainnet via [AlgoNode](https://algonode.io/api/)) is used. See [Provider Configuration](#provider-configuration) for more options.
|
|
105
|
-
|
|
106
|
-
You can initialize your providers in two ways:
|
|
107
|
-
|
|
108
|
-
1. **Static Import** - This is the standard way of importing modules in JavaScript. In this method, the import statement is at the top of the file and the modules are imported when the file loads. This is done by passing the clientStatic property.
|
|
109
|
-
|
|
110
|
-
2. **Dynamic Import** - With the dynamic import() syntax, you can load modules on demand by calling a function. This can greatly reduce the initial load time of your app by only loading modules when they are needed. This is done by passing the getDynamicClient property which must be a function that returns a promise that resolves to the client.
|
|
111
|
-
|
|
112
|
-
Note: For each provider, either clientStatic or getDynamicClient must be passed, not both.
|
|
113
|
-
|
|
114
|
-
Here is an example of both:
|
|
115
|
-
|
|
116
|
-
### Static Import Example
|
|
117
|
-
|
|
118
|
-
```jsx
|
|
119
|
-
import React from 'react'
|
|
120
|
-
import { WalletProvider, useInitializeProviders, PROVIDER_ID } from '@txnlab/use-wallet'
|
|
121
|
-
import { DeflyWalletConnect } from '@blockshake/defly-connect'
|
|
122
|
-
import { PeraWalletConnect } from '@perawallet/connect'
|
|
123
|
-
import { DaffiWalletConnect } from '@daffiwallet/connect'
|
|
124
|
-
import LuteConnect from 'lute-connect'
|
|
125
|
-
|
|
126
|
-
export default function App() {
|
|
127
|
-
const providers = useInitializeProviders({
|
|
128
|
-
providers: [
|
|
129
|
-
{ id: PROVIDER_ID.DEFLY, clientStatic: DeflyWalletConnect },
|
|
130
|
-
{ id: PROVIDER_ID.PERA, clientStatic: PeraWalletConnect },
|
|
131
|
-
{ id: PROVIDER_ID.DAFFI, clientStatic: DaffiWalletConnect },
|
|
132
|
-
{ id: PROVIDER_ID.EXODUS },
|
|
133
|
-
{
|
|
134
|
-
id: PROVIDER_ID.LUTE,
|
|
135
|
-
clientStatic: LuteConnect,
|
|
136
|
-
clientOptions: { siteName: 'YourSiteName' }
|
|
137
|
-
},
|
|
138
|
-
{ id: PROVIDER_ID.KIBISIS }
|
|
139
|
-
]
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
return (
|
|
143
|
-
<WalletProvider value={providers}>
|
|
144
|
-
<div className="App">{/* ... */}</div>
|
|
145
|
-
</WalletProvider>
|
|
146
|
-
)
|
|
147
|
-
}
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
### Dynamic Import Example
|
|
151
|
-
|
|
152
|
-
```jsx
|
|
153
|
-
import React from 'react'
|
|
154
|
-
import { WalletProvider, useInitializeProviders, PROVIDER_ID } from '@txnlab/use-wallet'
|
|
155
|
-
|
|
156
|
-
const getDynamicDeflyWalletConnect = async () => {
|
|
157
|
-
const DeflyWalletConnect = (await import('@blockshake/defly-connect')).DeflyWalletConnect
|
|
158
|
-
return DeflyWalletConnect
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const getDynamicPeraWalletConnect = async () => {
|
|
162
|
-
const PeraWalletConnect = (await import('@perawallet/connect')).PeraWalletConnect
|
|
163
|
-
return PeraWalletConnect
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const getDynamicDaffiWalletConnect = async () => {
|
|
167
|
-
const DaffiWalletConnect = (await import('@daffiwallet/connect')).DaffiWalletConnect
|
|
168
|
-
return DaffiWalletConnect
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const getDynamicLuteConnect = async () => {
|
|
172
|
-
const LuteConnect = (await import('lute-connect')).default
|
|
173
|
-
return LuteConnect
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export default function App() {
|
|
177
|
-
const providers = useInitializeProviders({
|
|
178
|
-
providers: [
|
|
179
|
-
{ id: PROVIDER_ID.DEFLY, getDynamicClient: getDynamicDeflyWalletConnect },
|
|
180
|
-
{ id: PROVIDER_ID.PERA, getDynamicClient: getDynamicPeraWalletConnect },
|
|
181
|
-
{ id: PROVIDER_ID.DAFFI, getDynamicClient: getDynamicDaffiWalletConnect },
|
|
182
|
-
{ id: PROVIDER_ID.EXODUS },
|
|
183
|
-
{
|
|
184
|
-
id: PROVIDER_ID.LUTE,
|
|
185
|
-
getDynamicClient: getDynamicLuteConnect,
|
|
186
|
-
clientOptions: { siteName: 'YourSiteName' }
|
|
187
|
-
},
|
|
188
|
-
{ id: PROVIDER_ID.KIBISIS }
|
|
189
|
-
]
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
return (
|
|
193
|
-
<WalletProvider value={providers}>
|
|
194
|
-
<div className="App">{/* ... */}</div>
|
|
195
|
-
</WalletProvider>
|
|
196
|
-
)
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
## The `useWallet` Hook
|
|
201
|
-
|
|
202
|
-
The `useWallet` hook is used to access wallet provider and account state, send unsigned transactions to be signed, and send signed transactions to the node from anywhere in your app. It returns an object with the following properties:
|
|
203
|
-
|
|
204
|
-
- `providers` - Array of wallet providers that have been initialized (see `Provider` in [Type Definitions](#type-definitions))
|
|
205
|
-
- `activeAccount` - The currently active account in the active provider (see `Account` in [Type Definitions](#type-definitions))
|
|
206
|
-
- `connectedAccounts` - Array of accounts from all connected wallet providers
|
|
207
|
-
- `connectedActiveAccounts` - Array of accounts from the active wallet provider
|
|
208
|
-
- `activeAddress` - The address of `activeAccount`
|
|
209
|
-
- `status`, `isReady`, `isActive` - The current connection status, see [Check connection status](#check-connection-status)
|
|
210
|
-
- `signTransactions` - Function that sends unsigned transactions to active wallet provider for signature
|
|
211
|
-
- `sendTransactions` - Function that sends signed transactions to the node
|
|
212
|
-
- `groupTransactionsBySender` - Utility function that groups transactions by sender address
|
|
213
|
-
- `getAddress` - Utility function that returns the address of the `activeAccount`
|
|
214
|
-
- `getAccountInfo` - Utility function that fetches `activeAccount` account information from the node
|
|
215
|
-
- `getAssets` - Utility function that fetches `activeAccount` asset info/balances from the node
|
|
216
|
-
- `signer` - Function used by the [KMD](#kmd-algorand-key-management-daemon) provider to sign transactions
|
|
217
|
-
|
|
218
|
-
## Type Definitions
|
|
219
|
-
|
|
220
|
-
### `Provider`
|
|
221
|
-
|
|
222
|
-
```ts
|
|
223
|
-
type Provider = {
|
|
224
|
-
accounts: Account[]
|
|
225
|
-
isActive: boolean
|
|
226
|
-
isConnected: boolean
|
|
227
|
-
connect: () => Promise<void>
|
|
228
|
-
disconnect: () => Promise<void>
|
|
229
|
-
reconnect: () => Promise<void>
|
|
230
|
-
setActiveProvider: () => void
|
|
231
|
-
setActiveAccount: (account: string) => void
|
|
232
|
-
metadata: Metadata
|
|
233
|
-
}
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
Each provider has two connection states: `isConnected` and `isActive`.
|
|
237
|
-
|
|
238
|
-
`isConnected` means that the user has authorized the provider in the app. Multiple providers can be connected at the same time.
|
|
239
|
-
|
|
240
|
-
`isActive` means that the provider is currently active and will be used to sign and send transactions.
|
|
241
|
-
|
|
242
|
-
### `Account`
|
|
243
|
-
|
|
244
|
-
```ts
|
|
245
|
-
interface Account {
|
|
246
|
-
providerId: PROVIDER_ID
|
|
247
|
-
name: string
|
|
248
|
-
address: string
|
|
249
|
-
authAddr?: string
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
The `activeAccount` is the account that will be used to sign and send transactions.
|
|
254
|
-
|
|
255
|
-
To get the currently active wallet provider, read the `providerId` property of `activeAccount`.
|
|
256
|
-
|
|
257
|
-
## Connect Menu
|
|
258
|
-
|
|
259
|
-
In your app's UI you will need a menu for the user to `connect` or `disconnect` wallet providers, `setActiveProvider`, and `setActiveAccount`.
|
|
260
|
-
|
|
261
|
-
This is a bare-bones example for demonstration purposes. For a styled example, see https://app.nf.domains/
|
|
262
|
-
|
|
263
|
-
```jsx
|
|
264
|
-
import React from 'react'
|
|
265
|
-
import { useWallet } from '@txnlab/use-wallet'
|
|
266
|
-
|
|
267
|
-
export default function ConnectMenu() {
|
|
268
|
-
const { providers, activeAccount } = useWallet()
|
|
269
|
-
|
|
270
|
-
// 1. Map over `providers` array
|
|
271
|
-
// 2. Show the provider name/icon and "Connect", "Set Active", and "Disconnect" buttons
|
|
272
|
-
// 3. If active, map `provider.accounts` to render a select menu of connected accounts
|
|
273
|
-
|
|
274
|
-
return (
|
|
275
|
-
<div>
|
|
276
|
-
{providers?.map((provider) => (
|
|
277
|
-
<div key={provider.metadata.id}>
|
|
278
|
-
<h4>
|
|
279
|
-
<img
|
|
280
|
-
width={30}
|
|
281
|
-
height={30}
|
|
282
|
-
alt={`${provider.metadata.name} icon`}
|
|
283
|
-
src={provider.metadata.icon}
|
|
284
|
-
/>
|
|
285
|
-
{provider.metadata.name} {provider.isActive && '[active]'}
|
|
286
|
-
</h4>
|
|
287
|
-
|
|
288
|
-
<div>
|
|
289
|
-
<button type="button" onClick={provider.connect} disabled={provider.isConnected}>
|
|
290
|
-
Connect
|
|
291
|
-
</button>
|
|
292
|
-
<button type="button" onClick={provider.disconnect} disabled={!provider.isConnected}>
|
|
293
|
-
Disconnect
|
|
294
|
-
</button>
|
|
295
|
-
<button
|
|
296
|
-
type="button"
|
|
297
|
-
onClick={provider.setActiveProvider}
|
|
298
|
-
disabled={!provider.isConnected || provider.isActive}
|
|
299
|
-
>
|
|
300
|
-
Set Active
|
|
301
|
-
</button>
|
|
302
|
-
|
|
303
|
-
<div>
|
|
304
|
-
{provider.isActive && provider.accounts.length && (
|
|
305
|
-
<select
|
|
306
|
-
value={activeAccount?.address}
|
|
307
|
-
onChange={(e) => provider.setActiveAccount(e.target.value)}
|
|
308
|
-
>
|
|
309
|
-
{provider.accounts.map((account) => (
|
|
310
|
-
<option key={account.address} value={account.address}>
|
|
311
|
-
{account.address}
|
|
312
|
-
</option>
|
|
313
|
-
))}
|
|
314
|
-
</select>
|
|
315
|
-
)}
|
|
316
|
-
</div>
|
|
317
|
-
</div>
|
|
318
|
-
</div>
|
|
319
|
-
))}
|
|
320
|
-
</div>
|
|
321
|
-
)
|
|
322
|
-
}
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
## Displaying Account Details
|
|
326
|
-
|
|
327
|
-
The `activeAccount` object can be used to display details for the currently active account.
|
|
328
|
-
|
|
329
|
-
```jsx
|
|
330
|
-
import React from 'react'
|
|
331
|
-
import { useWallet } from '@txnlab/use-wallet'
|
|
332
|
-
|
|
333
|
-
export default function Account() {
|
|
334
|
-
const { activeAccount } = useWallet()
|
|
335
|
-
|
|
336
|
-
if (!activeAccount) {
|
|
337
|
-
return <p>No account active.</p>
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
return (
|
|
341
|
-
<div>
|
|
342
|
-
<h4>Active Account</h4>
|
|
343
|
-
<p>
|
|
344
|
-
Name: <span>{activeAccount.name}</span>
|
|
345
|
-
</p>
|
|
346
|
-
<p>
|
|
347
|
-
Address: <span>{activeAccount.address}</span>
|
|
348
|
-
</p>
|
|
349
|
-
<p>
|
|
350
|
-
Provider: <span>{activeAccount.providerId}</span>
|
|
351
|
-
</p>
|
|
352
|
-
</div>
|
|
353
|
-
)
|
|
354
|
-
}
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
## Signing and Sending Transactions
|
|
358
|
-
|
|
359
|
-
Here is an example of a signing and sending simple pay transaction using `signTransactions` and `sendTransactions`.
|
|
360
|
-
|
|
361
|
-
```jsx
|
|
362
|
-
import React from 'react'
|
|
363
|
-
import algosdk from 'algosdk'
|
|
364
|
-
import { useWallet } from '@txnlab/use-wallet'
|
|
365
|
-
import { NODE_BASEURL, NODE_TOKEN, NODE_PORT } from '@/constants'
|
|
366
|
-
|
|
367
|
-
const algodClient = new algosdk.Algodv2(NODE_TOKEN, NODE_BASEURL, NODE_PORT)
|
|
368
|
-
|
|
369
|
-
export default function Transact() {
|
|
370
|
-
const { activeAddress, signTransactions, sendTransactions } = useWallet()
|
|
371
|
-
|
|
372
|
-
const sendTransaction = async (from?: string, to?: string, amount?: number) => {
|
|
373
|
-
try {
|
|
374
|
-
if (!from || !to || !amount) {
|
|
375
|
-
throw new Error('Missing transaction params.')
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const suggestedParams = await algodClient.getTransactionParams().do()
|
|
379
|
-
|
|
380
|
-
const transaction = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
|
|
381
|
-
from,
|
|
382
|
-
to,
|
|
383
|
-
amount,
|
|
384
|
-
suggestedParams
|
|
385
|
-
})
|
|
386
|
-
|
|
387
|
-
const encodedTransaction = algosdk.encodeUnsignedTransaction(transaction)
|
|
388
|
-
const signedTransactions = await signTransactions([encodedTransaction])
|
|
389
|
-
const waitRoundsToConfirm = 4
|
|
390
|
-
const { id } = await sendTransactions(signedTransactions, waitRoundsToConfirm)
|
|
391
|
-
|
|
392
|
-
console.log('Successfully sent transaction. Transaction ID: ', id)
|
|
393
|
-
} catch (error) {
|
|
394
|
-
console.error(error)
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
if (!activeAddress) {
|
|
399
|
-
return <p>Connect an account first.</p>
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
return (
|
|
403
|
-
<div>
|
|
404
|
-
<button type="button" onClick={() => sendTransaction(activeAddress, activeAddress, 1000)}>
|
|
405
|
-
Sign and send transactions
|
|
406
|
-
</button>
|
|
407
|
-
</div>
|
|
408
|
-
)
|
|
409
|
-
}
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
### signTransactions
|
|
413
|
-
|
|
414
|
-
```jsx
|
|
415
|
-
const signTransactions: (
|
|
416
|
-
transactions: Uint8Array[] | Uint8Array[][],
|
|
417
|
-
indexesToSign?: number[],
|
|
418
|
-
returnGroup?: boolean // defaults to true
|
|
419
|
-
) => Promise<Uint8Array[]>
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
The `signTransactions` function will accept an array of transactions (encoded) or an array of transaction groups.
|
|
423
|
-
|
|
424
|
-
#### Advanced Usage
|
|
425
|
-
|
|
426
|
-
You can optionally specify which transactions should be signed by providing an array of indexes as the second argument, `indexesToSign`.
|
|
427
|
-
|
|
428
|
-
By setting `returnGroup` to `false`, the returned promise will resolve to an array of signed transactions only. Otherwise it will return a flat array of all transactions by default.
|
|
429
|
-
|
|
430
|
-
### sendTransactions
|
|
431
|
-
|
|
432
|
-
```jsx
|
|
433
|
-
const sendTransactions: (
|
|
434
|
-
signedTransactions: Uint8Array[],
|
|
435
|
-
waitRoundsToConfirm?: number
|
|
436
|
-
) => Promise<PendingTransactionResponse & { id: string }>
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
If `signTransactions` is successful, the returned array of transactions can be passed to `sendTransactions` to be sent to the network.
|
|
440
|
-
|
|
441
|
-
It will wait for confirmation before resolving the promise. Use the optional argument `waitRoundsToConfirm` to indicate how many rounds to wait for confirmation.
|
|
442
|
-
|
|
443
|
-
The promise will resolve to an object containing the transaction `id` and the [`PendingTransactionResponse`](https://developer.algorand.org/docs/rest-apis/algod/#pendingtransactionresponse) from the Algorand REST API.
|
|
444
|
-
|
|
445
|
-
## Checking Connection Status
|
|
446
|
-
|
|
447
|
-
The `isActive` and `isReady` properties can be used to check the status of the wallet providers. The `isActive` property determines whether or not an account is currently active. The `isReady` property indicates whether client has mounted and successfully read the connection status from the wallet providers.
|
|
448
|
-
|
|
449
|
-
These properties are useful when setting up client side access restrictions, for example, by redirecting a user if no wallet provider `isActive`, as shown below.
|
|
450
|
-
|
|
451
|
-
```jsx
|
|
452
|
-
const { isActive, isReady } = useWallet()
|
|
453
|
-
|
|
454
|
-
useEffect(() => {
|
|
455
|
-
if (isReady && isActive) {
|
|
456
|
-
allowAccess()
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
if (isReady && !isActive) {
|
|
460
|
-
denyAccess()
|
|
461
|
-
}
|
|
462
|
-
})
|
|
463
|
-
```
|
|
464
|
-
|
|
465
|
-
## Supported Wallet Providers
|
|
466
|
-
|
|
467
|
-
### Defly Wallet
|
|
468
|
-
|
|
469
|
-
- Website - https://defly.app/
|
|
470
|
-
- Download Defly Wallet - [iOS](https://apps.apple.com/us/app/defly/id1602672723) / [Android](https://play.google.com/store/apps/details?id=io.blockshake.defly.app)
|
|
471
|
-
- Defly Connect - https://github.com/blockshake-io/defly-connect
|
|
472
|
-
- Install dependency - `npm install @blockshake/defly-connect`
|
|
473
|
-
|
|
474
|
-
### Pera Wallet
|
|
475
|
-
|
|
476
|
-
- Website - https://perawallet.app/
|
|
477
|
-
- Download Pera Mobile - [iOS](https://apps.apple.com/us/app/algorand-wallet/id1459898525) / [Android](https://play.google.com/store/apps/details?id=com.algorand.android)
|
|
478
|
-
- Pera Web Wallet - https://web.perawallet.app/
|
|
479
|
-
- Pera Connect - https://github.com/perawallet/connect
|
|
480
|
-
- Install dependency - `npm install @perawallet/connect`
|
|
481
|
-
|
|
482
|
-
### Daffi Wallet
|
|
483
|
-
|
|
484
|
-
- Website - https://www.daffi.me/
|
|
485
|
-
- Download Daffi Wallet - [iOS](https://apps.apple.com/kn/app/daffiwallet/id1659597876) / [Android](https://play.google.com/store/apps/details?id=me.daffi.daffi_wallet)
|
|
486
|
-
- Daffi Connect - https://github.com/RDinitiativ/daffiwallet_connect
|
|
487
|
-
- Install dependency - `npm install @daffiwallet/connect`
|
|
488
|
-
|
|
489
|
-
### WalletConnect
|
|
490
|
-
|
|
491
|
-
- Website - https://walletconnect.com/
|
|
492
|
-
- Documentation - https://docs.walletconnect.com/
|
|
493
|
-
- WalletConnect Cloud - https://cloud.walletconnect.com/
|
|
494
|
-
- Web3Modal - https://web3modal.com/
|
|
495
|
-
- Install dependency - `npm install @walletconnect/modal-sign-html`
|
|
496
|
-
|
|
497
|
-
### Exodus Wallet
|
|
498
|
-
|
|
499
|
-
- Website - https://www.exodus.com/
|
|
500
|
-
- Download - https://www.exodus.com/download/
|
|
501
|
-
|
|
502
|
-
### Lute Wallet
|
|
503
|
-
|
|
504
|
-
- Website - https://lute.app/
|
|
505
|
-
- Install dependency - `npm install lute-connect`
|
|
506
|
-
|
|
507
|
-
### Kibisis Wallet
|
|
508
|
-
|
|
509
|
-
**\*NOTE:** Kibisis is in active development and currently only supports betanet and testnet.\*
|
|
510
|
-
|
|
511
|
-
- Website - https://kibis.is
|
|
512
|
-
- Download - https://kibis.is/#download
|
|
513
|
-
- Support/Issues - https://discord.com/channels/1055863853633785857/1181252381816655952
|
|
514
|
-
|
|
515
|
-
### Magic Auth
|
|
516
|
-
|
|
517
|
-
- Website - https://magic.link/
|
|
518
|
-
- Install dependency - `npm install magic-sdk @magic-ext/algorand`
|
|
519
|
-
|
|
520
|
-
### KMD (Algorand Key Management Daemon)
|
|
521
|
-
|
|
522
|
-
- Documentation - https://developer.algorand.org/docs/rest-apis/kmd
|
|
523
|
-
|
|
524
|
-
### Custom
|
|
525
|
-
|
|
526
|
-
- Documentation - [Custom Provider](#custom-provider)
|
|
527
|
-
|
|
528
|
-
## Legacy Wallet Support
|
|
529
|
-
|
|
530
|
-
Support for these wallets will be removed in a future release.
|
|
531
|
-
|
|
532
|
-
### AlgoSigner (deprecated)
|
|
533
|
-
|
|
534
|
-
- GitHub - https://github.com/PureStake/algosigner
|
|
535
|
-
- EOL Press Release - https://www.algorand.foundation/news/algosigner-support-ending
|
|
536
|
-
|
|
537
|
-
### MyAlgo (deprecation pending: January 30, 2024)
|
|
538
|
-
|
|
539
|
-
- **DEPRECATION NOTICE** - https://wallet.myalgo.com/access
|
|
540
|
-
- Website - https://wallet.myalgo.com/home
|
|
541
|
-
- FAQ - https://wallet.myalgo.com/home#faq
|
|
542
|
-
- Install dependency - `npm install @randlabs/myalgo-connect`
|
|
543
|
-
|
|
544
|
-
## Provider Configuration
|
|
545
|
-
|
|
546
|
-
The `useInitializeProviders` hook accepts a configuration object with the following properties:
|
|
547
|
-
|
|
548
|
-
| Key | Type | Default Value |
|
|
549
|
-
| ---------------------- | -------------------------------------------------------------------------------------------------------- | -------------------------------------- |
|
|
550
|
-
| providers | `Array<ProviderDef>` | _required_ |
|
|
551
|
-
| nodeConfig.network | `string \| undefined` | `'mainnet'` |
|
|
552
|
-
| nodeConfig.nodeServer | `string \| undefined` | `'https://mainnet-api.algonode.cloud'` |
|
|
553
|
-
| nodeConfig.nodeToken | `string \| algosdk.AlgodTokenHeader \| algosdk.CustomTokenHeader \| algosdk.BaseHTTPClient \| undefined` | `''` |
|
|
554
|
-
| nodeConfig.nodePort | `string \| number \| undefined` | `443` |
|
|
555
|
-
| nodeConfig.nodeHeaders | `Record<string, string> \| undefined` | |
|
|
556
|
-
| algosdkStatic | `typeof algosdk \| undefined` | |
|
|
557
|
-
|
|
558
|
-
### Provider Definitions
|
|
559
|
-
|
|
560
|
-
The `providers` property is required, and must include at least one provider definition object. This is how you specify which wallet providers you wish to support in your app.
|
|
561
|
-
|
|
562
|
-
If a wallet provider has an SDK library dependency, make sure you have installed the package. Then set the provider definition's `clientStatic` property to the dependency's exported module. For example, to use the Defly Wallet provider, you must install the `@blockshake/defly-connect` package and set its `clientStatic` property to `DeflyWalletConnect`.
|
|
563
|
-
|
|
564
|
-
```jsx
|
|
565
|
-
import React from 'react'
|
|
566
|
-
import { WalletProvider, useInitializeProviders, PROVIDER_ID } from '@txnlab/use-wallet'
|
|
567
|
-
import { DeflyWalletConnect } from '@blockshake/defly-connect'
|
|
568
|
-
|
|
569
|
-
export default function App() {
|
|
570
|
-
const providers = useInitializeProviders({
|
|
571
|
-
providers: [{ id: PROVIDER_ID.DEFLY, clientStatic: DeflyWalletConnect }]
|
|
572
|
-
})
|
|
573
|
-
|
|
574
|
-
return (
|
|
575
|
-
<WalletProvider value={providers}>
|
|
576
|
-
<div className="App">{/* ... */}</div>
|
|
577
|
-
</WalletProvider>
|
|
578
|
-
)
|
|
579
|
-
}
|
|
580
|
-
```
|
|
581
|
-
|
|
582
|
-
If a provider has options that can be configured, you can set them in the `clientOptions` property. In most cases these would be the options that are passed to the provider's SDK library when it is initialized. See each [supported provider](#supported-wallet-providers)'s documentation for more details.
|
|
583
|
-
|
|
584
|
-
While most client options are optional, the WalletConnect provider has required `clientOptions` that must be set. See the [WalletConnect 2.0](#walletconnect-20-support) section below for more details.
|
|
585
|
-
|
|
586
|
-
**TypeScript:** The provider definitions are type-safe, so your IDE should be able to provide autocomplete suggestions for the client's available options based on the `id` that is set.
|
|
587
|
-
|
|
588
|
-
### Node configuration
|
|
589
|
-
|
|
590
|
-
To configure the Algorand node that providers will use to send transactions, you can set the `nodeConfig` property. The `network` property should be specified as `mainnet`, `testnet`, `betanet` or the name of your local development network\*.
|
|
591
|
-
|
|
592
|
-
\* _Refer to each wallet providers' documentation to see which networks they support._
|
|
593
|
-
|
|
594
|
-
```jsx
|
|
595
|
-
const providers = useInitializeProviders({
|
|
596
|
-
providers: [...],
|
|
597
|
-
nodeConfig: {
|
|
598
|
-
network: 'testnet',
|
|
599
|
-
nodeServer: 'https://testnet-api.algonode.cloud',
|
|
600
|
-
nodeToken: '',
|
|
601
|
-
nodePort: '443'
|
|
602
|
-
}
|
|
603
|
-
})
|
|
604
|
-
```
|
|
605
|
-
|
|
606
|
-
The node configuration must match the network your app's algod client is connected to. The recommended approach is to define the node configuration variables as constants and pass them to both the `useInitializeProviders` hook and the `algosdk.Algodv2` constructor.
|
|
607
|
-
|
|
608
|
-
```jsx
|
|
609
|
-
import algosdk from 'algosdk'
|
|
610
|
-
import { NODE_BASEURL, NODE_TOKEN, NODE_PORT } from '@/constants'
|
|
611
|
-
|
|
612
|
-
const algodClient = new algosdk.Algodv2(NODE_TOKEN, NODE_BASEURL, NODE_PORT)
|
|
613
|
-
|
|
614
|
-
const providers = useInitializeProviders({
|
|
615
|
-
providers: [...],
|
|
616
|
-
nodeConfig: {
|
|
617
|
-
network: 'testnet',
|
|
618
|
-
nodeServer: NODE_BASEURL,
|
|
619
|
-
nodeToken: NODE_TOKEN,
|
|
620
|
-
nodePort: NODE_PORT
|
|
621
|
-
}
|
|
622
|
-
})
|
|
623
|
-
```
|
|
624
|
-
|
|
625
|
-
### Algosdk Static Import
|
|
626
|
-
|
|
627
|
-
By default, the providers dynamically import the `algosdk` peer dependency installed in your app, to reduce bundle size.
|
|
628
|
-
|
|
629
|
-
Some React frameworks, like [Remix](https://remix.run/), do not support dynamic imports. To get around this, you can set the optional `algosdkStatic` root property to the imported `algosdk` module.
|
|
630
|
-
|
|
631
|
-
```jsx
|
|
632
|
-
import React from 'react'
|
|
633
|
-
import algosdk from 'algosdk'
|
|
634
|
-
import { PROVIDER_ID, WalletProvider, useInitializeProviders } from '@txnlab/use-wallet'
|
|
635
|
-
import { DeflyWalletConnect } from '@blockshake/defly-connect'
|
|
636
|
-
|
|
637
|
-
export default function App() {
|
|
638
|
-
const providers = useInitializeProviders({
|
|
639
|
-
providers: [{ id: PROVIDER_ID.DEFLY, clientStatic: DeflyWalletConnect }],
|
|
640
|
-
algosdkStatic: algosdk
|
|
641
|
-
})
|
|
642
|
-
|
|
643
|
-
return (
|
|
644
|
-
<WalletProvider value={providers}>
|
|
645
|
-
<div className="App">{/* ... */}</div>
|
|
646
|
-
</WalletProvider>
|
|
647
|
-
)
|
|
648
|
-
}
|
|
649
|
-
```
|
|
650
|
-
|
|
651
|
-
### Full Configuration Example
|
|
652
|
-
|
|
653
|
-
```jsx
|
|
654
|
-
import React from 'react'
|
|
655
|
-
import algosdk from 'algosdk'
|
|
656
|
-
import { PROVIDER_ID, WalletProvider, useInitializeProviders } from '@txnlab/use-wallet'
|
|
657
|
-
import { DeflyWalletConnect } from '@blockshake/defly-connect'
|
|
658
|
-
import { PeraWalletConnect } from '@perawallet/connect'
|
|
659
|
-
import { DaffiWalletConnect } from '@daffiwallet/connect'
|
|
660
|
-
import { WalletConnectModalSign } from '@walletconnect/modal-sign-html'
|
|
661
|
-
import LuteConnect from 'lute-connect'
|
|
662
|
-
import { Magic } from 'magic-sdk'
|
|
663
|
-
import { AlgorandExtension } from '@magic-ext/algorand'
|
|
664
|
-
|
|
665
|
-
export default function App() {
|
|
666
|
-
const providers = useInitializeProviders({
|
|
667
|
-
providers: [
|
|
668
|
-
{ id: PROVIDER_ID.DEFLY, clientStatic: DeflyWalletConnect },
|
|
669
|
-
{ id: PROVIDER_ID.PERA, clientStatic: PeraWalletConnect },
|
|
670
|
-
{ id: PROVIDER_ID.DAFFI, clientStatic: DaffiWalletConnect },
|
|
671
|
-
{
|
|
672
|
-
id: PROVIDER_ID.WALLETCONNECT,
|
|
673
|
-
clientStatic: WalletConnectModalSign,
|
|
674
|
-
clientOptions: {
|
|
675
|
-
projectId: '<YOUR_PROJECT_ID>',
|
|
676
|
-
metadata: {
|
|
677
|
-
name: 'Example Dapp',
|
|
678
|
-
description: 'Example Dapp',
|
|
679
|
-
url: '#',
|
|
680
|
-
icons: ['https://walletconnect.com/walletconnect-logo.png']
|
|
681
|
-
},
|
|
682
|
-
modalOptions: {
|
|
683
|
-
themeMode: 'dark'
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
},
|
|
687
|
-
{ id: PROVIDER_ID.EXODUS },
|
|
688
|
-
{
|
|
689
|
-
id: PROVIDER_ID.LUTE,
|
|
690
|
-
clientStatic: LuteConnect,
|
|
691
|
-
clientOptions: { siteName: 'YourSiteName' }
|
|
692
|
-
},
|
|
693
|
-
{ id: PROVIDER_ID.KIBISIS }
|
|
694
|
-
},
|
|
695
|
-
{
|
|
696
|
-
id: PROVIDER_ID.MAGIC,
|
|
697
|
-
clientStatic: Magic,
|
|
698
|
-
extensionStatic: AlgorandExtension,
|
|
699
|
-
clientOptions: { apiKey: '<API_KEY>' }
|
|
700
|
-
}
|
|
701
|
-
],
|
|
702
|
-
nodeConfig: {
|
|
703
|
-
network: 'mainnet',
|
|
704
|
-
nodeServer: 'https://mainnet-api.algonode.cloud',
|
|
705
|
-
nodeToken: '',
|
|
706
|
-
nodePort: '443'
|
|
707
|
-
}
|
|
708
|
-
algosdkStatic: algosdk
|
|
709
|
-
})
|
|
710
|
-
|
|
711
|
-
return (
|
|
712
|
-
<WalletProvider value={providers}>
|
|
713
|
-
<div className="App">{/* ... */}</div>
|
|
714
|
-
</WalletProvider>
|
|
715
|
-
)
|
|
716
|
-
}
|
|
717
|
-
```
|
|
718
|
-
|
|
719
|
-
## WalletConnect 2.0 Support
|
|
720
|
-
|
|
721
|
-
UseWallet v2 introduces support for WalletConnect 2.0. This is a major upgrade to the WalletConnect protocol, and introduces a number of breaking changes for app developers.
|
|
722
|
-
|
|
723
|
-
However, Algorand apps with UseWallet will be able to support the new protocol with minimal effort:
|
|
724
|
-
|
|
725
|
-
1. **Obtain a project ID** - You will need to obtain a project ID from [WalletConnect Cloud](https://cloud.walletconnect.com/). This is a simple process, and there is no waiting period. Every app will need its own unique project ID.
|
|
726
|
-
|
|
727
|
-
2. **Install client library** - Install `@walletconnect/modal-sign-html` and set `clientStatic` to the imported `WalletConnectModalSign` module.
|
|
728
|
-
|
|
729
|
-
3. **Required options** - Set the required `clientOptions` as shown below
|
|
730
|
-
|
|
731
|
-
```jsx
|
|
732
|
-
const providers = useInitializeProviders({
|
|
733
|
-
providers: [
|
|
734
|
-
{
|
|
735
|
-
id: PROVIDER_ID.WALLETCONNECT,
|
|
736
|
-
clientStatic: WalletConnectModalSign,
|
|
737
|
-
clientOptions: {
|
|
738
|
-
projectId: '<YOUR_PROJECT_ID>',
|
|
739
|
-
metadata: {
|
|
740
|
-
name: 'Example Dapp',
|
|
741
|
-
description: 'Example Dapp',
|
|
742
|
-
url: '#',
|
|
743
|
-
icons: ['https://walletconnect.com/walletconnect-logo.png']
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
// other providers...
|
|
748
|
-
]
|
|
749
|
-
})
|
|
750
|
-
```
|
|
751
|
-
|
|
752
|
-
While `projectId` and `metadata` are required, you can also set optional `modalOptions` to customize the appearance of the WalletConnect modal. See the [WalletConnect docs](https://docs.walletconnect.com/2.0/web/walletConnectModal/sign/html/options) for more details.
|
|
753
|
-
|
|
754
|
-
**IMPORTANT:** Wallets must migrate to WalletConnect v2 or they will NOT work with this provider. **The latest versions of Pera Wallet, Defly Wallet, and Daffi Wallet still support WalletConnect v1 ONLY.** Beta versions of these wallets with WalletConnect v2 support are available now:
|
|
755
|
-
|
|
756
|
-
### Defly Wallet (iOS) beta
|
|
757
|
-
|
|
758
|
-
iOS users can join the [Defly Wallet (iOS) beta program](https://testflight.apple.com/join/S66wwJpq) for testing WalletConnect v2 support. Android beta coming soon.
|
|
759
|
-
|
|
760
|
-
### Pera Wallet (Android) beta
|
|
761
|
-
|
|
762
|
-
Android users can join the [Pera Wallet (Android) beta program](https://forms.gle/m5TJueyJD9f74ieL8) for testing WalletConnect v2 support. Fill out the form and wait for an email with instructions. Form submissions will be checked periodically and added in bulk.
|
|
763
|
-
|
|
764
|
-
### "Module not found" errors in Next.js 13
|
|
765
|
-
|
|
766
|
-
With the WalletConnect provider initialized in your Next.js 13 app, you may see the error `Module not found: Can't resolve 'lokijs' in...` or similar in local development. To resolve this, add the following to your `next.config.js` file:
|
|
767
|
-
|
|
768
|
-
```js
|
|
769
|
-
/** @type {import('next').NextConfig} */
|
|
770
|
-
const nextConfig = {
|
|
771
|
-
webpack: (config) => {
|
|
772
|
-
config.externals.push('pino-pretty', 'lokijs', 'encoding') // list modules in error messages
|
|
773
|
-
return config
|
|
774
|
-
}
|
|
775
|
-
// ...other config
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
module.exports = nextConfig
|
|
779
|
-
```
|
|
780
|
-
|
|
781
|
-
See https://github.com/WalletConnect/walletconnect-monorepo/issues/1908#issuecomment-1487801131
|
|
782
|
-
|
|
783
|
-
## Custom Provider
|
|
784
|
-
|
|
785
|
-
As of v2.2.0, you can now add support for a **custom provider** that delegates wallet actions to your application. This is useful if you want to support a wallet that is not yet integrated with UseWallet, or if your application requires any additional, custom interactions.
|
|
786
|
-
|
|
787
|
-
### Custom Provider example
|
|
788
|
-
|
|
789
|
-
```jsx
|
|
790
|
-
import { PROVIDER_ID, Metadata, CustomProvider } from '@txnlab/use-wallet'
|
|
791
|
-
import type _algosdk from 'algosdk'
|
|
792
|
-
|
|
793
|
-
export class MyCustomProvider implements CustomProvider {
|
|
794
|
-
algosdk: typeof _algosdk
|
|
795
|
-
|
|
796
|
-
constructor(algosdk: typeof _algosdk, ...) {
|
|
797
|
-
this.algosdk = algosdk
|
|
798
|
-
// ...
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
async connect(metadata: Metadata): Promise<Wallet> {
|
|
802
|
-
// connect to wallet
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
async disconnect(): Promise<void> {
|
|
806
|
-
// disconnect from wallet
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
async reconnect(metadata: Metadata): Promise<Wallet | null> {
|
|
810
|
-
// reconnect to wallet
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
async signTransactions(
|
|
814
|
-
connectedAccounts: string[],
|
|
815
|
-
txnGroups: Uint8Array[] | Uint8Array[][],
|
|
816
|
-
indexesToSign?: number[] | undefined,
|
|
817
|
-
returnGroup?: boolean | undefined
|
|
818
|
-
): Promise<Uint8Array[]> {
|
|
819
|
-
// sign transactions
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
```
|
|
823
|
-
|
|
824
|
-
### Initializing Custom Provider
|
|
825
|
-
|
|
826
|
-
```jsx
|
|
827
|
-
const providers = useInitializeProviders({
|
|
828
|
-
providers: [
|
|
829
|
-
{
|
|
830
|
-
id: PROVIDER_ID.CUSTOM,
|
|
831
|
-
clientOptions: {
|
|
832
|
-
name: 'My Custom Provider',
|
|
833
|
-
icon: 'data:image/png;base64...',
|
|
834
|
-
getProvider: (params: {
|
|
835
|
-
network?: Network
|
|
836
|
-
algod?: algosdk.Algodv2
|
|
837
|
-
algosdkStatic?: typeof algosdk
|
|
838
|
-
}) => {
|
|
839
|
-
return new MyCustomProvider(params.algosdkStatic ?? algosdk)
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
// other providers...
|
|
844
|
-
]
|
|
845
|
-
})
|
|
846
|
-
```
|
|
847
|
-
|
|
848
|
-
### Manual Provider (example implementation)
|
|
849
|
-
|
|
850
|
-
See [TestManualProvider.ts](https://github.com/TxnLab/use-wallet/blob/main/src/components/Example/TestManualProvider.tsx) for a full example implementation of a custom provider. You can try it by running
|
|
851
|
-
|
|
852
|
-
```bash
|
|
853
|
-
yarn storybook
|
|
854
|
-
```
|
|
855
|
-
|
|
856
|
-
## Migration Guide
|
|
857
|
-
|
|
858
|
-
Version 2.x is a major version bump, and includes some breaking changes from 1.x.
|
|
859
|
-
|
|
860
|
-
### useInitializeProviders
|
|
861
|
-
|
|
862
|
-
The most significant change is how wallet providers are initialized.
|
|
863
|
-
|
|
864
|
-
`initializeProviders` has been replaced by the `useInitializeProviders` hook. It accepts a single configuration object as an argument. Its properties correspond to the three arguments passed to its predecessor.
|
|
865
|
-
|
|
866
|
-
The `providers` property is an array of provider definition objects, with an `id` set to a `PROVIDER_ID` constant. Wallet provider SDKs are no longer dynamically imported by their client classes. They must be statically imported and set as the `clientStatic` property.
|
|
867
|
-
|
|
868
|
-
```diff
|
|
869
|
-
- const providers = initializeProviders([PROVIDER_ID.PERA, PROVIDER_ID.DEFLY, PROVIDER_ID.EXODUS])
|
|
870
|
-
+ const providers = useInitializeProviders({
|
|
871
|
-
+ providers: [
|
|
872
|
-
+ { id: PROVIDER_ID.PERA, clientStatic: PeraWalletConnect },
|
|
873
|
-
+ { id: PROVIDER_ID.DEFLY, clientStatic: DeflyWalletConnect },
|
|
874
|
-
+ { id: PROVIDER_ID.EXODUS }
|
|
875
|
-
+ ]
|
|
876
|
-
+ })
|
|
877
|
-
```
|
|
878
|
-
|
|
879
|
-
We've also done away with the concept of "default" providers. Each provider you wish to support in your app must be explicitly defined in the `providers` array.
|
|
880
|
-
|
|
881
|
-
Node configuration should be set as the `nodeConfig` property.
|
|
882
|
-
|
|
883
|
-
```diff
|
|
884
|
-
- const providers = initializeProviders([...], {
|
|
885
|
-
- network: 'testnet',
|
|
886
|
-
- nodeServer: 'https://testnet-api.algonode.cloud',
|
|
887
|
-
- nodeToken: '',
|
|
888
|
-
- nodePort: '443'
|
|
889
|
-
- })
|
|
890
|
-
+ const providers = useInitializeProviders({
|
|
891
|
-
+ providers: [...],
|
|
892
|
-
+ nodeConfig: {
|
|
893
|
-
+ network: 'testnet',
|
|
894
|
-
+ nodeServer: 'https://testnet-api.algonode.cloud',
|
|
895
|
-
+ nodeToken: '',
|
|
896
|
-
+ nodePort: '443'
|
|
897
|
-
+ }
|
|
898
|
-
+ })
|
|
899
|
-
```
|
|
900
|
-
|
|
901
|
-
See [Provider Configuration](#provider-configuration) for more details.
|
|
902
|
-
|
|
903
|
-
### shouldShowSignTxnToast
|
|
904
|
-
|
|
905
|
-
Pera Connect, Defly Connect and Daffi Connect share a `shouldShowSignTxnToast` option that is set to `true` by default. UseWallet v1 set these to `false` by default, and required setting the option back to `true` to achieve the libraries' default behavior.
|
|
906
|
-
|
|
907
|
-
In v2 this option is set to `true` by default. If your app has this option set to `true` you can remove it from your configuration. If you wish to disable the toast(s), you must now explicitly set the option to `false`.
|
|
908
|
-
|
|
909
|
-
```jsx
|
|
910
|
-
const providers = useInitializeProviders({
|
|
911
|
-
providers: [
|
|
912
|
-
{ id: PROVIDER_ID.DEFLY, clientOptions: { shouldShowSignTxnToast: false } },
|
|
913
|
-
{ id: PROVIDER_ID.PERA, clientOptions: { shouldShowSignTxnToast: false } },
|
|
914
|
-
{ id: PROVIDER_ID.DAFFI, clientOptions: { shouldShowSignTxnToast: false } }
|
|
915
|
-
// other providers...
|
|
916
|
-
]
|
|
917
|
-
})
|
|
918
|
-
```
|
|
919
|
-
|
|
920
|
-
### WalletConnect peer dependencies
|
|
921
|
-
|
|
922
|
-
The WalletConnect provider now supports WalletConnect 2.0. To continue supporting this provider, or to add support to your application, you must install the `@walletconnect/modal-sign-html` package.
|
|
923
|
-
|
|
924
|
-
```bash
|
|
925
|
-
npm install @walletconnect/modal-sign-html
|
|
926
|
-
```
|
|
927
|
-
|
|
928
|
-
The peer dependencies for WalletConnect 1.x should be uninstalled.
|
|
929
|
-
|
|
930
|
-
```bash
|
|
931
|
-
npm uninstall @walletconnect/client @json-rpc-tools/utils algorand-walletconnect-qrcode-modal
|
|
932
|
-
```
|
|
933
|
-
|
|
934
|
-
See [WalletConnect 2.0 Support](#walletconnect-20-support) for more details.
|
|
935
|
-
|
|
936
|
-
## Local Development
|
|
937
|
-
|
|
938
|
-
### Install dependencies
|
|
939
|
-
|
|
940
|
-
```bash
|
|
941
|
-
yarn install
|
|
942
|
-
```
|
|
943
|
-
|
|
944
|
-
### Demo in Storybook
|
|
945
|
-
|
|
946
|
-
```bash
|
|
947
|
-
yarn dev
|
|
948
|
-
```
|
|
949
|
-
|
|
950
|
-
To develop against a local version of `@txnlab/use-wallet` in your application, do the following:
|
|
951
|
-
|
|
952
|
-
### Build the library
|
|
953
|
-
|
|
954
|
-
```bash
|
|
955
|
-
yarn build
|
|
956
|
-
```
|
|
957
|
-
|
|
958
|
-
### Symlink the library
|
|
959
|
-
|
|
960
|
-
In the root of `use-wallet` directory, run:
|
|
961
|
-
|
|
962
|
-
```bash
|
|
963
|
-
yarn link
|
|
964
|
-
```
|
|
965
|
-
|
|
966
|
-
In the root of your application, run:
|
|
967
|
-
|
|
968
|
-
```bash
|
|
969
|
-
yarn link @txnlab/use-wallet
|
|
970
|
-
```
|
|
971
|
-
|
|
972
|
-
### Symlink React
|
|
973
|
-
|
|
974
|
-
In the root of your application, run:
|
|
975
|
-
|
|
976
|
-
```bash
|
|
977
|
-
cd node_modules/react
|
|
978
|
-
yarn link
|
|
979
|
-
cd ../react-dom
|
|
980
|
-
yarn link
|
|
981
|
-
```
|
|
982
|
-
|
|
983
|
-
In the root of the `use-wallet` directory, run:
|
|
984
|
-
|
|
985
|
-
```bash
|
|
986
|
-
yarn link react react-dom
|
|
987
|
-
```
|
|
988
|
-
|
|
989
|
-
## Support
|
|
990
|
-
|
|
991
|
-
For questions or technical support, you can reach us in the #use-wallet channel on NFD's Discord: https://discord.gg/w6vSwG5bFK
|
|
992
|
-
|
|
993
|
-
To report bugs/issues or make a feature request here on GitHub:
|
|
994
|
-
|
|
995
|
-
- [Create an issue](https://github.com/TxnLab/use-wallet/issues/new)
|
|
996
|
-
- [Create a discussion](https://github.com/TxnLab/use-wallet/discussions/new/choose)
|
|
997
|
-
|
|
998
|
-
## Used By
|
|
999
|
-
|
|
1000
|
-
Are you using `@txnlab/use-wallet`? We'd love to include your project here. Let us know! [Twitter](https://twitter.com/NFDomains) | [Discord](https://discord.gg/w6vSwG5bFK) | [Email](mailto:admin@txnlab.dev)
|
|
1001
|
-
|
|
1002
|
-
- [@algoscan/use-wallet-ui](https://github.com/algoscan/use-wallet-ui)
|
|
1003
|
-
- [@algoworldnft/algoworld-swapper](https://github.com/algoworldnft/algoworld-swapper)
|
|
1004
|
-
- [Algogator](https://algogator.finance/)
|
|
1005
|
-
- [Notiboy](https://notiboy.com/)
|
|
1006
|
-
- [Rand Drops](https://drops.randgallery.com/drops)
|
|
1007
|
-
- [Vestige](https://vestige.fi/)
|
|
1008
|
-
|
|
1009
|
-
Full list of [Dependents](https://github.com/TxnLab/use-wallet/network/dependents)
|
|
3
|
+
[use-wallet](https://github.com/TxnLab/use-wallet/tree/v3/) core library
|
|
1010
4
|
|
|
1011
5
|
## License
|
|
1012
6
|
|
|
1013
|
-
|
|
7
|
+
MIT ©2024 [TxnLab, Inc.](https://txnlab.dev)
|