@txnlab/use-wallet 0.0.1 → 0.0.3

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 CHANGED
@@ -4,6 +4,8 @@ React hooks for using Algorand compatible wallets with web applications.
4
4
 
5
5
  ### Quick Start
6
6
 
7
+ If you're using Webpack 5 (most newer React projects), you need to install polyfills. Follow [these directions.](#webpack-5-issues)
8
+
7
9
  Install with Yarn.
8
10
 
9
11
  ```bash
@@ -34,13 +36,9 @@ Setup the wallet providers
34
36
 
35
37
  ```jsx
36
38
  import React from "react";
37
- import { useConnectWallet } from "../../index";
38
-
39
- type ConnectWalletProps = {
40
- foo?: string;
41
- };
39
+ import { useConnectWallet } from "@txnlab/use-wallet";
42
40
 
43
- export default function ConnectWallet(props: ConnectWalletProps) {
41
+ function App() {
44
42
  const { providers, reconnectProviders, accounts, activeAccount } =
45
43
  useConnectWallet();
46
44
 
@@ -50,12 +48,13 @@ export default function ConnectWallet(props: ConnectWalletProps) {
50
48
  }, []);
51
49
 
52
50
  // Use these properties to display connected accounts to users.
51
+ // They are reactive and presisted to local storage.
53
52
  React.useEffect(() => {
54
53
  console.log("connected accounts", accounts);
55
54
  console.log("active account", activeAccount);
56
55
  });
57
56
 
58
- // Map through the providers, and render "connect", "set active", and "disconnect" buttons
57
+ // Map through the providers, and render account information and "connect", "set active", and "disconnect" buttons
59
58
  return (
60
59
  <div>
61
60
  {providers.map((provider) => (
@@ -65,18 +64,15 @@ export default function ConnectWallet(props: ConnectWalletProps) {
65
64
  {provider.name} {provider.isActive && "[active]"}
66
65
  </h4>
67
66
  <div>
68
- {/* If the wallet provider isn't connected, render a "connect" button */}
69
- {!provider.isConnected && (
70
- <button onClick={provider.connect}>Connect</button>
71
- )}
72
- {/* If the wallet provider is connected and active, render a "disconnect" button */}
73
- {provider.isConnected && (
74
- <button onClick={provider.disconnect}>Disonnect</button>
75
- )}
76
- {/* If the wallet provider is connected but not active, render a "set active" button */}
77
- {provider.isConnected && !provider.isActive && (
78
- <button onClick={provider.setActive}>Set Active</button>
79
- )}
67
+ <button onClick={provider.connect} disabled={provider.isConnected}>
68
+ Connect
69
+ </button>
70
+ <button onClick={provider.disconnect} disabled={!provider.isConnected}>
71
+ Disonnect
72
+ </button>
73
+ <button onClick={provider.setActive} disabled={!provider.isConnected || provider.isActive}>
74
+ Set Active
75
+ </button>
80
76
  </div>
81
77
  </div>
82
78
  ))}
@@ -85,10 +81,16 @@ export default function ConnectWallet(props: ConnectWalletProps) {
85
81
  }
86
82
  ```
87
83
 
84
+ Each provider has two connection states: `isConnected` and `isActive`.
85
+
86
+ `isConnected` means that the user has authorized the provider to talk to the dApp. The connection flow does not need to be restarted when switching to this wallet from a different one.
87
+
88
+ `isActive` indicates that the provider is currently active and will be used to sign and send transactions when using the `useWallet` hook.
89
+
88
90
  Sign and send transactions
89
91
 
90
92
  ```jsx
91
- const Wallet = (props: WalletProps) => {
93
+ function Wallet() {
92
94
  const { activeAccount, signTransactions, sendTransactions } = useWallet();
93
95
 
94
96
  const sendTransaction = async (
@@ -102,6 +104,7 @@ const Wallet = (props: WalletProps) => {
102
104
 
103
105
  const params = await algodClient.getTransactionParams().do();
104
106
 
107
+ // Construct a transaction to be signed and sent.
105
108
  const transaction = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
106
109
  from,
107
110
  to,
@@ -109,9 +112,13 @@ const Wallet = (props: WalletProps) => {
109
112
  suggestedParams: params,
110
113
  });
111
114
 
115
+ // Encode the transactions into a byte array.
112
116
  const encodedTransaction = algosdk.encodeUnsignedTransaction(transaction);
117
+
118
+ // Sign the transactions.
113
119
  const signedTransactions = await signTransactions([encodedTransaction]);
114
120
 
121
+ // Send the transactions.
115
122
  const { id } = await sendTransactions(signedTransactions);
116
123
 
117
124
  console.log("Successfully sent transaction. Transaction ID: ", id);
@@ -141,3 +148,67 @@ const Wallet = (props: WalletProps) => {
141
148
  );
142
149
  };
143
150
  ```
151
+
152
+ ### Local Development
153
+
154
+ Install dependencies.
155
+
156
+ ```bash
157
+ yarn install
158
+ ```
159
+
160
+ Demo the components in StoryBook.
161
+
162
+ ```bash
163
+ yarn storybook
164
+
165
+ ```
166
+ Build the library.
167
+
168
+ ```bash
169
+ yarn build
170
+ ```
171
+ ### Webpack 5 issues
172
+
173
+ Install `react-app-rewired` and the missing polyfills.
174
+
175
+ ```bash
176
+ yarn add --dev react-app-rewired crypto-browserify stream-browserify assert stream-http https-browserify os-browserify url buffer process
177
+ ```
178
+
179
+ Create `config-overrides.js` in the root of your project and add the following:
180
+
181
+ ```js
182
+ const webpack = require("webpack");
183
+
184
+ module.exports = function override(config) {
185
+ const fallback = config.resolve.fallback || {};
186
+ Object.assign(fallback, {
187
+ crypto: require.resolve("crypto-browserify"),
188
+ stream: require.resolve("stream-browserify"),
189
+ assert: require.resolve("assert"),
190
+ http: require.resolve("stream-http"),
191
+ https: require.resolve("https-browserify"),
192
+ os: require.resolve("os-browserify"),
193
+ url: require.resolve("url"),
194
+ });
195
+ config.resolve.fallback = fallback;
196
+ config.plugins = (config.plugins || []).concat([
197
+ new webpack.ProvidePlugin({
198
+ process: "process/browser",
199
+ Buffer: ["buffer", "Buffer"],
200
+ }),
201
+ ]);
202
+ return config;
203
+ };
204
+ ```
205
+
206
+ Change your scripts in `package.json` to the following:
207
+ ```
208
+ "scripts": {
209
+ "start": "react-app-rewired start",
210
+ "build": "react-app-rewired build",
211
+ "test": "react-app-rewired test",
212
+ "eject": "react-scripts eject"
213
+ },
214
+ ```
@@ -5,6 +5,6 @@ export declare enum PROVIDER_ID {
5
5
  DEFLY = "Defly",
6
6
  EXODUS = "Exodus"
7
7
  }
8
- export declare const NODE_SERVER = "https://node.algoexplorerapi.io";
8
+ export declare const NODE_SERVER = "https://mainnet-api.algonode.cloud";
9
9
  export declare const NODE_TOKEN = "";
10
10
  export declare const NODE_PORT = "";
package/dist/cjs/index.js CHANGED
@@ -520,7 +520,7 @@ exports.PROVIDER_ID = void 0;
520
520
  PROVIDER_ID["DEFLY"] = "Defly";
521
521
  PROVIDER_ID["EXODUS"] = "Exodus";
522
522
  })(exports.PROVIDER_ID || (exports.PROVIDER_ID = {}));
523
- const NODE_SERVER = "https://node.algoexplorerapi.io";
523
+ const NODE_SERVER = "https://mainnet-api.algonode.cloud";
524
524
  const NODE_TOKEN = "";
525
525
  const NODE_PORT = "";
526
526