@txnlab/use-wallet 0.1.23 → 1.0.1
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 +255 -95
- package/dist/cjs/algod/index.d.ts +3 -2
- package/dist/cjs/clients/algosigner/client.d.ts +35 -0
- package/dist/cjs/clients/algosigner/constants.d.ts +2 -0
- package/dist/cjs/clients/algosigner/index.d.ts +2 -0
- package/dist/cjs/clients/algosigner/types.d.ts +40 -0
- package/dist/{esm/clients → cjs/clients/base}/base.d.ts +12 -25
- package/dist/cjs/clients/base/index.d.ts +2 -0
- package/dist/cjs/clients/defly/client.d.ts +37 -0
- package/dist/cjs/clients/defly/constants.d.ts +1 -0
- package/dist/cjs/clients/defly/index.d.ts +2 -0
- package/dist/cjs/clients/defly/types.d.ts +36 -0
- package/dist/cjs/clients/exodus/client.d.ts +33 -0
- package/dist/cjs/clients/exodus/constants.d.ts +1 -0
- package/dist/cjs/clients/exodus/index.d.ts +2 -0
- package/dist/cjs/clients/exodus/types.d.ts +40 -0
- package/dist/cjs/clients/index.d.ts +11 -15
- package/dist/cjs/clients/kmd/client.d.ts +29 -0
- package/dist/cjs/clients/kmd/constants.d.ts +1 -0
- package/dist/cjs/clients/kmd/index.d.ts +2 -0
- package/dist/cjs/clients/kmd/types.d.ts +38 -0
- package/dist/cjs/clients/{myalgowallet.d.ts → myalgo/client.d.ts} +14 -17
- package/dist/cjs/clients/myalgo/constants.d.ts +1 -0
- package/dist/cjs/clients/myalgo/index.d.ts +2 -0
- package/dist/cjs/clients/myalgo/types.d.ts +23 -0
- package/dist/cjs/clients/pera/client.d.ts +35 -0
- package/dist/cjs/clients/pera/constants.d.ts +1 -0
- package/dist/cjs/clients/pera/index.d.ts +2 -0
- package/dist/cjs/clients/pera/types.d.ts +36 -0
- package/dist/cjs/clients/walletconnect/client.d.ts +37 -0
- package/dist/cjs/clients/walletconnect/constants.d.ts +2 -0
- package/dist/cjs/clients/walletconnect/index.d.ts +2 -0
- package/dist/cjs/clients/walletconnect/types.d.ts +54 -0
- package/dist/cjs/constants/constants.d.ts +12 -16
- package/dist/cjs/hooks/index.d.ts +0 -1
- package/dist/cjs/hooks/useWallet.d.ts +140 -8
- package/dist/cjs/index.d.ts +4 -2
- package/dist/cjs/index.js +736 -739
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/store/index.d.ts +1 -0
- package/dist/cjs/store/state/clientStore.d.ts +22 -0
- package/dist/cjs/types/node.d.ts +3 -0
- package/dist/cjs/types/wallet.d.ts +2 -2
- package/dist/cjs/utils/index.d.ts +2 -2
- package/dist/cjs/utils/initializeProviders.d.ts +13 -0
- package/dist/cjs/utils/reconnectProviders.d.ts +6 -0
- package/dist/esm/algod/index.d.ts +3 -2
- package/dist/esm/clients/algosigner/client.d.ts +35 -0
- package/dist/esm/clients/algosigner/constants.d.ts +2 -0
- package/dist/esm/clients/algosigner/index.d.ts +2 -0
- package/dist/esm/clients/algosigner/types.d.ts +40 -0
- package/dist/{cjs/clients → esm/clients/base}/base.d.ts +12 -25
- package/dist/esm/clients/base/index.d.ts +2 -0
- package/dist/esm/clients/defly/client.d.ts +37 -0
- package/dist/esm/clients/defly/constants.d.ts +1 -0
- package/dist/esm/clients/defly/index.d.ts +2 -0
- package/dist/esm/clients/defly/types.d.ts +36 -0
- package/dist/esm/clients/exodus/client.d.ts +33 -0
- package/dist/esm/clients/exodus/constants.d.ts +1 -0
- package/dist/esm/clients/exodus/index.d.ts +2 -0
- package/dist/esm/clients/exodus/types.d.ts +40 -0
- package/dist/esm/clients/index.d.ts +11 -15
- package/dist/esm/clients/kmd/client.d.ts +29 -0
- package/dist/esm/clients/kmd/constants.d.ts +1 -0
- package/dist/esm/clients/kmd/index.d.ts +2 -0
- package/dist/esm/clients/kmd/types.d.ts +38 -0
- package/dist/esm/clients/{myalgowallet.d.ts → myalgo/client.d.ts} +14 -17
- package/dist/esm/clients/myalgo/constants.d.ts +1 -0
- package/dist/esm/clients/myalgo/index.d.ts +2 -0
- package/dist/esm/clients/myalgo/types.d.ts +23 -0
- package/dist/esm/clients/pera/client.d.ts +35 -0
- package/dist/esm/clients/pera/constants.d.ts +1 -0
- package/dist/esm/clients/pera/index.d.ts +2 -0
- package/dist/esm/clients/pera/types.d.ts +36 -0
- package/dist/esm/clients/walletconnect/client.d.ts +37 -0
- package/dist/esm/clients/walletconnect/constants.d.ts +2 -0
- package/dist/esm/clients/walletconnect/index.d.ts +2 -0
- package/dist/esm/clients/walletconnect/types.d.ts +54 -0
- package/dist/esm/constants/constants.d.ts +12 -16
- package/dist/esm/hooks/index.d.ts +0 -1
- package/dist/esm/hooks/useWallet.d.ts +140 -8
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.js +724 -730
- package/dist/esm/store/index.d.ts +1 -0
- package/dist/esm/store/state/clientStore.d.ts +22 -0
- package/dist/esm/types/node.d.ts +3 -0
- package/dist/esm/types/wallet.d.ts +2 -2
- package/dist/esm/utils/index.d.ts +2 -2
- package/dist/esm/utils/initializeProviders.d.ts +13 -0
- package/dist/esm/utils/reconnectProviders.d.ts +6 -0
- package/dist/index.d.ts +669 -66
- package/package.json +6 -6
- package/dist/cjs/clients/algosigner.d.ts +0 -62
- package/dist/cjs/clients/defly.d.ts +0 -51
- package/dist/cjs/clients/exodus.d.ts +0 -52
- package/dist/cjs/clients/kmd.d.ts +0 -31
- package/dist/cjs/clients/perawallet.d.ts +0 -47
- package/dist/cjs/clients/walletconnect.d.ts +0 -47
- package/dist/cjs/hooks/useConnectWallet.d.ts +0 -30
- package/dist/cjs/providers/algosigner.d.ts +0 -2
- package/dist/cjs/providers/defly.d.ts +0 -2
- package/dist/cjs/providers/exodus.d.ts +0 -2
- package/dist/cjs/providers/index.d.ts +0 -9
- package/dist/cjs/providers/kmd.d.ts +0 -2
- package/dist/cjs/providers/myalgowallet.d.ts +0 -2
- package/dist/cjs/providers/perawallet.d.ts +0 -2
- package/dist/cjs/providers/walletconnect.d.ts +0 -2
- package/dist/esm/clients/algosigner.d.ts +0 -62
- package/dist/esm/clients/defly.d.ts +0 -51
- package/dist/esm/clients/exodus.d.ts +0 -52
- package/dist/esm/clients/kmd.d.ts +0 -31
- package/dist/esm/clients/perawallet.d.ts +0 -47
- package/dist/esm/clients/walletconnect.d.ts +0 -47
- package/dist/esm/hooks/useConnectWallet.d.ts +0 -30
- package/dist/esm/providers/algosigner.d.ts +0 -2
- package/dist/esm/providers/defly.d.ts +0 -2
- package/dist/esm/providers/exodus.d.ts +0 -2
- package/dist/esm/providers/index.d.ts +0 -9
- package/dist/esm/providers/kmd.d.ts +0 -2
- package/dist/esm/providers/myalgowallet.d.ts +0 -2
- package/dist/esm/providers/perawallet.d.ts +0 -2
- package/dist/esm/providers/walletconnect.d.ts +0 -2
package/README.md
CHANGED
|
@@ -12,14 +12,13 @@ React hooks for using Algorand compatible wallets with web applications.
|
|
|
12
12
|
- [WalletConnect](https://walletconnect.com)
|
|
13
13
|
- [KMD](https://developer.algorand.org/docs/rest-apis/kmd)
|
|
14
14
|
|
|
15
|
-
|
|
16
15
|
## Demo
|
|
17
16
|
|
|
18
17
|
Preview a basic implementation in [Storybook](https://txnlab.github.io/use-wallet) or check out [this example](https://github.com/gabrielkuettel/use-wallet-example).
|
|
19
18
|
|
|
20
19
|
## Quick Start
|
|
21
20
|
|
|
22
|
-
⚠️ If you're using
|
|
21
|
+
⚠️ If you're using `create-react-app` and `webpack 5` (most newer React projects), you will need to install polyfills. Follow [these directions](#webpack-5).
|
|
23
22
|
|
|
24
23
|
### Yarn
|
|
25
24
|
|
|
@@ -45,64 +44,106 @@ Install peer dependencies (if needed)
|
|
|
45
44
|
npm install algosdk @blockshake/defly-connect @perawallet/connect @randlabs/myalgo-connect @walletconnect/client algorand-walletconnect-qrcode-modal @json-rpc-tools/utils
|
|
46
45
|
```
|
|
47
46
|
|
|
48
|
-
### Set up the
|
|
47
|
+
### Set up the Wallet Provider
|
|
48
|
+
|
|
49
|
+
In `app.js`, initialize the Wallet Provider so that the `useWallet` hook can be used in the child components, and use the `reconnectProviders` function to restore sessions for users returning to the app.
|
|
50
|
+
|
|
49
51
|
|
|
50
52
|
```jsx
|
|
51
53
|
import React from "react";
|
|
52
|
-
import {
|
|
54
|
+
import {
|
|
55
|
+
reconnectProviders,
|
|
56
|
+
initializeProviders,
|
|
57
|
+
WalletProvider,
|
|
58
|
+
} from "@txnlab/use-wallet";
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
const { providers, reconnectProviders, accounts, activeAccount } = useConnectWallet();
|
|
60
|
+
const walletProviders = initializeProviders();
|
|
56
61
|
|
|
62
|
+
export default function App() {
|
|
57
63
|
// Reconnect the session when the user returns to the dApp
|
|
58
64
|
React.useEffect(() => {
|
|
59
|
-
reconnectProviders();
|
|
65
|
+
reconnectProviders(walletProviders);
|
|
60
66
|
}, []);
|
|
61
67
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
return (
|
|
69
|
+
<WalletProvider value={walletProviders}>
|
|
70
|
+
...
|
|
71
|
+
</WalletProvider>);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The `reconnectProviders` function is used to restore session states of wallets that rely on the `WalletConnect` protocol.
|
|
76
|
+
|
|
77
|
+
By default, all of the supported providers except for `KMD` are returned by `useConnectWallet`. An array can be passed to `initializeProviders` to determine which providers your dApp supports, as shown below.
|
|
78
|
+
|
|
79
|
+
```jsx
|
|
80
|
+
import { initializeProviders, PROVIDER_ID } from "@txnlab/use-wallet";
|
|
81
|
+
|
|
82
|
+
const walletProviders = initializeProviders([
|
|
83
|
+
PROVIDER_ID.KMD_WALLET,
|
|
84
|
+
PROVIDER_ID.WALLET_CONNECT,
|
|
85
|
+
]);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
For more configuration options, see [Provider Configuration](#provider-configuration).
|
|
89
|
+
|
|
90
|
+
### Connect
|
|
91
|
+
|
|
92
|
+
Map through the `providers` object to list the providers and enable users to connect.
|
|
93
|
+
|
|
94
|
+
```jsx
|
|
95
|
+
import React from "react";
|
|
96
|
+
import { useWallet } from "@txnlab/use-wallet";
|
|
97
|
+
|
|
98
|
+
export default function Connect() {
|
|
99
|
+
const { providers, activeAccount } = useWallet();
|
|
68
100
|
|
|
69
101
|
// Map through the providers.
|
|
70
102
|
// Render account information and "connect", "set active", and "disconnect" buttons.
|
|
71
103
|
// Finally, map through the `accounts` property to render a dropdown for each connected account.
|
|
72
104
|
return (
|
|
73
105
|
<div>
|
|
74
|
-
{providers
|
|
75
|
-
<div key={"provider-" + provider.id}>
|
|
106
|
+
{providers?.map((provider) => (
|
|
107
|
+
<div key={"provider-" + provider.metadata.id}>
|
|
76
108
|
<h4>
|
|
77
|
-
<img width={30} height={30} src={provider.icon} />
|
|
78
|
-
{provider.name} {provider.isActive && "[active]"}
|
|
109
|
+
<img width={30} height={30} alt="" src={provider.metadata.icon} />
|
|
110
|
+
{provider.metadata.name} {provider.isActive && "[active]"}
|
|
79
111
|
</h4>
|
|
80
112
|
<div>
|
|
81
113
|
<button onClick={provider.connect} disabled={provider.isConnected}>
|
|
82
114
|
Connect
|
|
83
115
|
</button>
|
|
84
|
-
<button
|
|
85
|
-
|
|
116
|
+
<button
|
|
117
|
+
onClick={provider.disconnect}
|
|
118
|
+
disabled={!provider.isConnected}
|
|
119
|
+
>
|
|
120
|
+
Disconnect
|
|
86
121
|
</button>
|
|
87
|
-
<button
|
|
122
|
+
<button
|
|
123
|
+
onClick={provider.setActiveProvider}
|
|
124
|
+
disabled={!provider.isConnected || provider.isActive}
|
|
125
|
+
>
|
|
88
126
|
Set Active
|
|
89
127
|
</button>
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
128
|
+
<div>
|
|
129
|
+
{provider.isActive && provider.accounts.length && (
|
|
130
|
+
<select
|
|
131
|
+
value={activeAccount?.address}
|
|
132
|
+
onChange={(e) => provider.setActiveAccount(e.target.value)}
|
|
133
|
+
>
|
|
134
|
+
{provider.accounts.map((account) => (
|
|
135
|
+
<option key={account.address} value={account.address}>{account.address}</option>
|
|
136
|
+
))}
|
|
137
|
+
</select>
|
|
138
|
+
)}
|
|
139
|
+
</div>
|
|
100
140
|
</div>
|
|
101
141
|
</div>
|
|
102
142
|
))}
|
|
103
143
|
</div>
|
|
104
144
|
);
|
|
105
145
|
}
|
|
146
|
+
|
|
106
147
|
```
|
|
107
148
|
|
|
108
149
|
Each provider has two connection states: `isConnected` and `isActive`.
|
|
@@ -111,30 +152,30 @@ Each provider has two connection states: `isConnected` and `isActive`.
|
|
|
111
152
|
|
|
112
153
|
`isActive` indicates that the provider is currently active and will be used to sign and send transactions when using the `useWallet` hook.
|
|
113
154
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
```jsx
|
|
117
|
-
import { useConnectWallet, PROVIDER_ID } from "@txnlab/use-wallet";
|
|
118
|
-
|
|
119
|
-
...
|
|
120
|
-
|
|
121
|
-
const { providers } = useConnectWallet({
|
|
122
|
-
providers: [
|
|
123
|
-
PROVIDER_ID.MYALGO_WALLET,
|
|
124
|
-
PROVIDER_ID.PERA_WALLET,
|
|
125
|
-
PROVIDER_ID.KMD_WALLET,
|
|
126
|
-
],
|
|
127
|
-
});
|
|
128
|
-
```
|
|
155
|
+
The `activeAccount` is the primary account that is currently active and will be used to sign and send transactions.
|
|
129
156
|
|
|
130
157
|
### Sign and send transactions
|
|
131
158
|
|
|
159
|
+
Construct a transaction using `algosdk`, and sign and send the transaction using the `signTransactions` and `sendTransactions` functions provided by the `useWallet` hook.
|
|
160
|
+
|
|
132
161
|
```jsx
|
|
133
162
|
import React from "react";
|
|
134
|
-
import {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
163
|
+
import {
|
|
164
|
+
useWallet,
|
|
165
|
+
DEFAULT_NODE_BASEURL,
|
|
166
|
+
DEFAULT_NODE_TOKEN,
|
|
167
|
+
DEFAULT_NODE_PORT,
|
|
168
|
+
} from "@txnlab/use-wallet";
|
|
169
|
+
import algosdk from "algosdk";
|
|
170
|
+
|
|
171
|
+
const algodClient = new algosdk.Algodv2(
|
|
172
|
+
DEFAULT_NODE_TOKEN,
|
|
173
|
+
DEFAULT_NODE_BASEURL,
|
|
174
|
+
DEFAULT_NODE_PORT
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
export default function Transact() {
|
|
178
|
+
const { activeAddress, signTransactions, sendTransactions } = useWallet();
|
|
138
179
|
|
|
139
180
|
const sendTransaction = async (
|
|
140
181
|
from?: string,
|
|
@@ -145,93 +186,184 @@ function Wallet() {
|
|
|
145
186
|
throw new Error("Missing transaction params.");
|
|
146
187
|
}
|
|
147
188
|
|
|
148
|
-
const
|
|
189
|
+
const suggestedParams = await algodClient.getTransactionParams().do();
|
|
149
190
|
|
|
150
|
-
// Construct a transaction to be signed and sent.
|
|
151
191
|
const transaction = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
|
|
152
192
|
from,
|
|
153
193
|
to,
|
|
154
194
|
amount,
|
|
155
|
-
suggestedParams
|
|
195
|
+
suggestedParams,
|
|
156
196
|
});
|
|
157
197
|
|
|
158
|
-
// Encode the transactions into a byte array.
|
|
159
198
|
const encodedTransaction = algosdk.encodeUnsignedTransaction(transaction);
|
|
160
199
|
|
|
161
|
-
// Sign the transactions.
|
|
162
200
|
const signedTransactions = await signTransactions([encodedTransaction]);
|
|
163
201
|
|
|
164
|
-
|
|
165
|
-
|
|
202
|
+
const waitRoundsToConfirm = 4;
|
|
203
|
+
|
|
204
|
+
const { id } = await sendTransactions(
|
|
205
|
+
signedTransactions,
|
|
206
|
+
waitRoundsToConfirm
|
|
207
|
+
);
|
|
166
208
|
|
|
167
209
|
console.log("Successfully sent transaction. Transaction ID: ", id);
|
|
168
210
|
};
|
|
169
211
|
|
|
170
|
-
if (!
|
|
212
|
+
if (!activeAddress) {
|
|
171
213
|
return <p>Connect an account first.</p>;
|
|
172
214
|
}
|
|
173
215
|
|
|
174
216
|
return (
|
|
175
217
|
<div>
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
1000
|
|
183
|
-
)
|
|
184
|
-
}
|
|
185
|
-
className="button"
|
|
186
|
-
>
|
|
187
|
-
Sign and send transactions
|
|
188
|
-
</button>
|
|
189
|
-
}
|
|
218
|
+
<button
|
|
219
|
+
onClick={() => sendTransaction(activeAddress, activeAddress, 1000)}
|
|
220
|
+
className="button"
|
|
221
|
+
>
|
|
222
|
+
Sign and send transactions
|
|
223
|
+
</button>
|
|
190
224
|
</div>
|
|
191
225
|
);
|
|
192
|
-
}
|
|
226
|
+
}
|
|
193
227
|
```
|
|
194
228
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
By default, wallets will connect to Algorand [MainNet](https://developer.algorand.org/docs/get-details/algorand-networks/mainnet). You can change this behavior by setting the following environment variables:
|
|
229
|
+
### Display account details
|
|
198
230
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
* `NODE_SERVER`
|
|
231
|
+
The `activeAccount` object can be used to display details for the currently active account. For convenience, the `activeAddress` property shows the currently active address.
|
|
202
232
|
|
|
203
|
-
|
|
233
|
+
```jsx
|
|
234
|
+
import React from "react";
|
|
235
|
+
import { useWallet } from "@txnlab/use-wallet";
|
|
204
236
|
|
|
205
|
-
|
|
237
|
+
export default function Account() {
|
|
238
|
+
const { activeAccount } = useWallet();
|
|
206
239
|
|
|
207
|
-
|
|
240
|
+
if (!activeAccount) {
|
|
241
|
+
return <p>No account active.</p>;
|
|
242
|
+
}
|
|
208
243
|
|
|
244
|
+
return (
|
|
245
|
+
<div>
|
|
246
|
+
<h4>Active Account</h4>
|
|
247
|
+
<p>
|
|
248
|
+
Name: <span>{activeAccount.name}</span>
|
|
249
|
+
</p>
|
|
250
|
+
<p>
|
|
251
|
+
Address: <span>{activeAccount.address}</span>
|
|
252
|
+
</p>
|
|
253
|
+
<p>
|
|
254
|
+
Provider: <span>{activeAccount.providerId}</span>
|
|
255
|
+
</p>
|
|
256
|
+
</div>
|
|
257
|
+
);
|
|
258
|
+
}
|
|
209
259
|
```
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
260
|
+
|
|
261
|
+
## Provider Configuration
|
|
262
|
+
|
|
263
|
+
The `initializeProviders` functon accepts a configuration object that can be used to configure the nodes that the providers use to send transactions, as shown below.
|
|
264
|
+
|
|
265
|
+
```jsx
|
|
266
|
+
const walletProviders = initializeProviders([], {
|
|
267
|
+
network: "devmodenet",
|
|
268
|
+
nodeServer: "http://algod",
|
|
269
|
+
nodeToken: "xxxxxxxxx",
|
|
270
|
+
nodePort: "8080",
|
|
271
|
+
});
|
|
214
272
|
```
|
|
215
273
|
|
|
216
|
-
|
|
274
|
+
Passing an empty array as the first argument enables all of the default providers. The `network` property should be specified as `betanet`, `testnet`, `mainnet` or the name of your local development network.
|
|
217
275
|
|
|
218
|
-
|
|
276
|
+
For more custom configuration options, the providers can be configured individually by creating an object and passing it to the `WalletProvider` where the key contains the provider ID, and the value calls the `init` function of the provider client. See below for an example:
|
|
219
277
|
|
|
220
|
-
|
|
278
|
+
```jsx
|
|
279
|
+
...
|
|
221
280
|
|
|
222
|
-
|
|
281
|
+
import {
|
|
282
|
+
PROVIDER_ID,
|
|
283
|
+
pera,
|
|
284
|
+
myalgo,
|
|
285
|
+
} from "@txnlab/use-wallet";
|
|
286
|
+
|
|
287
|
+
const walletProviders = {
|
|
288
|
+
[PROVIDER_ID.PERA]: pera.init({
|
|
289
|
+
clientOptions: {
|
|
290
|
+
shouldShowSignTxnToast: true,
|
|
291
|
+
},
|
|
292
|
+
}),
|
|
293
|
+
[PROVIDER_ID.MYALGO]: myalgo.init({
|
|
294
|
+
network: "devmodenet",
|
|
295
|
+
algodOptions: ["xxxxxxxxx", "http://algod", "8080"],
|
|
296
|
+
clientOptions: { disableLedgerNano: true },
|
|
297
|
+
}),
|
|
298
|
+
};
|
|
223
299
|
|
|
224
|
-
|
|
300
|
+
...
|
|
225
301
|
|
|
302
|
+
<WalletProvider value={walletProviders}>
|
|
303
|
+
...
|
|
304
|
+
</WalletProvider>
|
|
226
305
|
```
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
306
|
+
|
|
307
|
+
## Static Imports
|
|
308
|
+
|
|
309
|
+
By default, `use-wallet` dynamically imports all of the dependencies for the providiers, as well as `algosdk`, to reduce bundle size.
|
|
310
|
+
|
|
311
|
+
Some React frameworks, like [Remix](https://remix.run/), do not support dynamic imports. To get around this, those dependencies can be imported in your application and passed to the `useWallet` provider. See below for an example.
|
|
312
|
+
|
|
313
|
+
```jsx
|
|
314
|
+
...
|
|
315
|
+
|
|
316
|
+
import algosdk from "algosdk";
|
|
317
|
+
import MyAlgoConnect from "@randlabs/myalgo-connect";
|
|
318
|
+
import { PeraWalletConnect } from "@perawallet/connect";
|
|
319
|
+
import { DeflyWalletConnect } from "@blockshake/defly-connect";
|
|
320
|
+
import WalletConnect from "@walletconnect/client";
|
|
321
|
+
import QRCodeModal from "algorand-walletconnect-qrcode-modal";
|
|
322
|
+
|
|
323
|
+
const walletProviders = {
|
|
324
|
+
[PROVIDER_ID.PERA]: pera.init({
|
|
325
|
+
algosdkStatic: algosdk,
|
|
326
|
+
clientStatic: PeraWalletConnect,
|
|
327
|
+
}),
|
|
328
|
+
[PROVIDER_ID.MYALGO]: myalgo.init({
|
|
329
|
+
algosdkStatic: algosdk,
|
|
330
|
+
clientStatic: MyAlgoConnect,
|
|
331
|
+
}),
|
|
332
|
+
[PROVIDER_ID.DEFLY]: defly.init({
|
|
333
|
+
algosdkStatic: algosdk,
|
|
334
|
+
clientStatic: DeflyWalletConnect,
|
|
335
|
+
}),
|
|
336
|
+
[PROVIDER_ID.EXODUS]: exodus.init({
|
|
337
|
+
algosdkStatic: algosdk,
|
|
338
|
+
}),
|
|
339
|
+
[PROVIDER_ID.ALGOSIGNER]: algosigner.init({
|
|
340
|
+
algosdkStatic: algosdk,
|
|
341
|
+
}),
|
|
342
|
+
[PROVIDER_ID.WALLETCONNECT]: walletconnect.init({
|
|
343
|
+
algosdkStatic: algosdk,
|
|
344
|
+
clientStatic: WalletConnect,
|
|
345
|
+
modalStatic: QRCodeModal,
|
|
346
|
+
}),
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export default function App() {
|
|
350
|
+
...
|
|
351
|
+
|
|
352
|
+
return (
|
|
353
|
+
<WalletProvider value={walletProviders}>
|
|
354
|
+
...
|
|
355
|
+
</WalletProvider>
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
|
|
231
359
|
```
|
|
232
360
|
|
|
361
|
+
Note that some of the providers do not require static imports to be provided. This is usually the case of providers that are browser extensions.
|
|
362
|
+
|
|
233
363
|
## Webpack 5
|
|
234
364
|
|
|
365
|
+
Note: Frameworks like `next.js` already include polyfills. This step is required if you're using `create-react-app` with `webpack 5`.
|
|
366
|
+
|
|
235
367
|
1. Install `react-app-rewired` and the missing polyfills.
|
|
236
368
|
|
|
237
369
|
```bash
|
|
@@ -287,16 +419,44 @@ yarn install
|
|
|
287
419
|
### Demo in Storybook
|
|
288
420
|
|
|
289
421
|
```bash
|
|
290
|
-
yarn
|
|
422
|
+
yarn dev
|
|
291
423
|
|
|
292
424
|
```
|
|
293
425
|
|
|
426
|
+
To develop against a local version of `use-wallet` in your application, do the following:
|
|
427
|
+
|
|
294
428
|
### Build the library
|
|
295
429
|
|
|
296
430
|
```bash
|
|
297
431
|
yarn build
|
|
298
432
|
```
|
|
299
433
|
|
|
434
|
+
### Symlink the library
|
|
435
|
+
|
|
436
|
+
In the root of `use-wallet` directory, run:
|
|
437
|
+
|
|
438
|
+
```bash
|
|
439
|
+
yarn link
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
In the root of your application, run:
|
|
443
|
+
```bash
|
|
444
|
+
yarn link @txnlab/use-wallet
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### Symlink React
|
|
448
|
+
|
|
449
|
+
In the root of your application, run:
|
|
450
|
+
```bash
|
|
451
|
+
cd node_modules/react
|
|
452
|
+
yarn link
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
In the root of `use-wallet` directory, run:
|
|
456
|
+
```bash
|
|
457
|
+
yarn link react
|
|
458
|
+
```
|
|
459
|
+
|
|
300
460
|
## License
|
|
301
461
|
|
|
302
462
|
See the [LICENSE](https://github.com/TxnLab/use-wallet/blob/main/LICENSE.md) file for license rights and limitations (MIT)
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type _algosdk from "algosdk";
|
|
2
|
+
import type { AlgodClientOptions } from "../types";
|
|
2
3
|
export declare const getAlgosdk: () => Promise<typeof _algosdk>;
|
|
3
|
-
export declare const getAlgodClient: (algosdk: typeof _algosdk) => Promise<_algosdk.Algodv2>;
|
|
4
|
+
export declare const getAlgodClient: (algosdk: typeof _algosdk, algodClientOptions?: AlgodClientOptions) => Promise<_algosdk.Algodv2>;
|
|
4
5
|
export default class Algod {
|
|
5
6
|
algosdk: typeof _algosdk;
|
|
6
7
|
algodClient: _algosdk.Algodv2;
|
|
7
8
|
constructor(algosdk: typeof _algosdk, algodClient: _algosdk.Algodv2);
|
|
8
|
-
static init(): Promise<Algod>;
|
|
9
|
+
static init(algodOptions?: AlgodClientOptions): Promise<Algod>;
|
|
9
10
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import BaseWallet from "../base";
|
|
2
|
+
import { PROVIDER_ID } from "../../constants";
|
|
3
|
+
import type { TransactionsArray, Network } from "../../types";
|
|
4
|
+
import type { AlgoSignerTransaction, AlgoSignerClientConstructor, InitParams } from "./types";
|
|
5
|
+
declare class AlgoSignerClient extends BaseWallet {
|
|
6
|
+
#private;
|
|
7
|
+
network: Network;
|
|
8
|
+
constructor({ client, algosdk, algodClient, network, }: AlgoSignerClientConstructor);
|
|
9
|
+
static metadata: {
|
|
10
|
+
id: PROVIDER_ID;
|
|
11
|
+
name: string;
|
|
12
|
+
icon: string;
|
|
13
|
+
isWalletConnect: boolean;
|
|
14
|
+
};
|
|
15
|
+
static init({ algodOptions, algosdkStatic, network, }: InitParams): Promise<AlgoSignerClient | null>;
|
|
16
|
+
connect(): Promise<{
|
|
17
|
+
accounts: {
|
|
18
|
+
name: string;
|
|
19
|
+
address: string;
|
|
20
|
+
providerId: PROVIDER_ID;
|
|
21
|
+
}[];
|
|
22
|
+
id: PROVIDER_ID;
|
|
23
|
+
name: string;
|
|
24
|
+
icon: string;
|
|
25
|
+
isWalletConnect: boolean;
|
|
26
|
+
}>;
|
|
27
|
+
reconnect(onDisconnect: () => void): Promise<null>;
|
|
28
|
+
disconnect(): Promise<void>;
|
|
29
|
+
signTransactions(connectedAccounts: string[], transactions: Uint8Array[]): Promise<Uint8Array[]>;
|
|
30
|
+
/** @deprecated */
|
|
31
|
+
formatTransactionsArray(transactions: TransactionsArray): AlgoSignerTransaction[];
|
|
32
|
+
/** @deprecated */
|
|
33
|
+
signEncodedTransactions(transactions: TransactionsArray): Promise<Uint8Array[]>;
|
|
34
|
+
}
|
|
35
|
+
export default AlgoSignerClient;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type _algosdk from "algosdk";
|
|
2
|
+
import { PROVIDER_ID } from "../../constants";
|
|
3
|
+
import type { AlgodClientOptions, Network } from "../../types";
|
|
4
|
+
export declare type WindowExtended = {
|
|
5
|
+
AlgoSigner: AlgoSigner;
|
|
6
|
+
} & Window & typeof globalThis;
|
|
7
|
+
export declare type AlgoSignerTransaction = {
|
|
8
|
+
txn: string;
|
|
9
|
+
signers?: [];
|
|
10
|
+
multisig?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare type SupportedLedgers = "MainNet" | "TestNet" | "BetaNet" | string;
|
|
13
|
+
export declare type AlgoSigner = {
|
|
14
|
+
connect: () => Promise<Record<string, never>>;
|
|
15
|
+
accounts: (ledger: {
|
|
16
|
+
ledger: SupportedLedgers;
|
|
17
|
+
}) => Promise<{
|
|
18
|
+
address: string;
|
|
19
|
+
}[]>;
|
|
20
|
+
signTxn: (transactions: AlgoSignerTransaction[]) => Promise<{
|
|
21
|
+
txID: string;
|
|
22
|
+
blob: string;
|
|
23
|
+
}[]>;
|
|
24
|
+
encoding: {
|
|
25
|
+
msgpackToBase64(transaction: Uint8Array): string;
|
|
26
|
+
byteArrayToString(transaction: Uint8Array): string;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export declare type AlgoSignerClientConstructor = {
|
|
30
|
+
client: AlgoSigner;
|
|
31
|
+
id: PROVIDER_ID;
|
|
32
|
+
algosdk: typeof _algosdk;
|
|
33
|
+
algodClient: _algosdk.Algodv2;
|
|
34
|
+
network: SupportedLedgers;
|
|
35
|
+
};
|
|
36
|
+
export declare type InitParams = {
|
|
37
|
+
algodOptions?: AlgodClientOptions;
|
|
38
|
+
algosdkStatic?: typeof _algosdk;
|
|
39
|
+
network?: Network;
|
|
40
|
+
};
|
|
@@ -1,29 +1,16 @@
|
|
|
1
1
|
import type _algosdk from "algosdk";
|
|
2
|
-
import { PROVIDER_ID } from "
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
connect(onDisconnect: () => void): Promise<Wallet>;
|
|
6
|
-
healthCheck(): Promise<Record<string, never>>;
|
|
7
|
-
disconnect(): Promise<void>;
|
|
8
|
-
reconnect(onDisconnect: () => void): Promise<Wallet | null>;
|
|
9
|
-
decodeTransaction(txn: string, isSigned: boolean): _algosdk.Transaction;
|
|
10
|
-
logEncodedTransaction(txn: string, isSigned: boolean): void;
|
|
11
|
-
groupTransactionsBySender(transactions: TransactionsArray): Record<string, TxnInfo[]>;
|
|
12
|
-
signTransactions(connectedAccounts: string[], transactions: Array<Uint8Array>): Promise<Uint8Array[]>;
|
|
13
|
-
signEncodedTransactions(transactions: TransactionsArray): Promise<Uint8Array[]>;
|
|
14
|
-
sendRawTransactions(transactions: Uint8Array[], waitRoundsToConfirm?: number): Promise<ConfirmedTxn & {
|
|
15
|
-
id: string;
|
|
16
|
-
}>;
|
|
17
|
-
getAccountInfo(address: string): Promise<AccountInfo>;
|
|
18
|
-
getAssets(address: string): Promise<Asset[]>;
|
|
19
|
-
waitForConfirmation(txId: string, timeout?: number): Promise<ConfirmedTxn>;
|
|
20
|
-
}
|
|
21
|
-
declare abstract class BaseWallet implements BaseWalletInterface {
|
|
2
|
+
import { PROVIDER_ID } from "../../constants";
|
|
3
|
+
import type { Asset, Wallet, AccountInfo, TransactionsArray } from "../../types";
|
|
4
|
+
declare abstract class BaseClient {
|
|
22
5
|
algosdk: typeof _algosdk;
|
|
23
6
|
algodClient: _algosdk.Algodv2;
|
|
24
7
|
keepWCAlive: HTMLAudioElement;
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
static metadata: {
|
|
9
|
+
id: PROVIDER_ID;
|
|
10
|
+
name: string;
|
|
11
|
+
icon: string;
|
|
12
|
+
isWalletConnect: boolean;
|
|
13
|
+
};
|
|
27
14
|
abstract connect(onDisconnect: () => void): Promise<Wallet>;
|
|
28
15
|
abstract disconnect(): Promise<void>;
|
|
29
16
|
abstract reconnect(onDisconnect: () => void): Promise<Wallet | null>;
|
|
@@ -39,7 +26,7 @@ declare abstract class BaseWallet implements BaseWalletInterface {
|
|
|
39
26
|
"pool-error": string;
|
|
40
27
|
txn: {
|
|
41
28
|
sig: Uint8Array;
|
|
42
|
-
txn: import("
|
|
29
|
+
txn: import("../../types").Txn;
|
|
43
30
|
};
|
|
44
31
|
txId: string;
|
|
45
32
|
}>;
|
|
@@ -52,7 +39,7 @@ declare abstract class BaseWallet implements BaseWalletInterface {
|
|
|
52
39
|
"pool-error": string;
|
|
53
40
|
txn: {
|
|
54
41
|
sig: Uint8Array;
|
|
55
|
-
txn: import("
|
|
42
|
+
txn: import("../../types").Txn;
|
|
56
43
|
};
|
|
57
44
|
txId: string;
|
|
58
45
|
id: any;
|
|
@@ -60,4 +47,4 @@ declare abstract class BaseWallet implements BaseWalletInterface {
|
|
|
60
47
|
keepWCAliveStart(): void;
|
|
61
48
|
keepWCAliveStop(): void;
|
|
62
49
|
}
|
|
63
|
-
export default
|
|
50
|
+
export default BaseClient;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Wallet } from "../../types";
|
|
2
|
+
import { PROVIDER_ID } from "../../constants";
|
|
3
|
+
import BaseWallet from "../base";
|
|
4
|
+
import { TransactionsArray } from "../../types";
|
|
5
|
+
import type { Network } from "../../types";
|
|
6
|
+
import { DeflyTransaction, InitParams, DeflyWalletClientConstructor } from "./types";
|
|
7
|
+
declare class DeflyWalletClient extends BaseWallet {
|
|
8
|
+
#private;
|
|
9
|
+
network: Network;
|
|
10
|
+
constructor({ client, algosdk, algodClient, network, }: DeflyWalletClientConstructor);
|
|
11
|
+
static metadata: {
|
|
12
|
+
id: PROVIDER_ID;
|
|
13
|
+
name: string;
|
|
14
|
+
icon: string;
|
|
15
|
+
isWalletConnect: boolean;
|
|
16
|
+
};
|
|
17
|
+
static init({ clientOptions, algodOptions, clientStatic, algosdkStatic, network, }: InitParams): Promise<DeflyWalletClient | null>;
|
|
18
|
+
connect(onDisconnect: () => void): Promise<Wallet>;
|
|
19
|
+
reconnect(onDisconnect: () => void): Promise<{
|
|
20
|
+
accounts: {
|
|
21
|
+
name: string;
|
|
22
|
+
address: string;
|
|
23
|
+
providerId: PROVIDER_ID;
|
|
24
|
+
}[];
|
|
25
|
+
id: PROVIDER_ID;
|
|
26
|
+
name: string;
|
|
27
|
+
icon: string;
|
|
28
|
+
isWalletConnect: boolean;
|
|
29
|
+
} | null>;
|
|
30
|
+
disconnect(): Promise<void>;
|
|
31
|
+
signTransactions(connectedAccounts: string[], transactions: Uint8Array[]): Promise<Uint8Array[]>;
|
|
32
|
+
/** @deprecated */
|
|
33
|
+
signEncodedTransactions(transactions: TransactionsArray): Promise<Uint8Array[]>;
|
|
34
|
+
/** @deprecated */
|
|
35
|
+
formatTransactionsArray(transactions: TransactionsArray): DeflyTransaction[];
|
|
36
|
+
}
|
|
37
|
+
export default DeflyWalletClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ICON: string;
|