@aptos-labs/wallet-adapter-react 0.1.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/.eslintrc.js ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ["adapter"],
4
+ };
package/README.md ADDED
@@ -0,0 +1,192 @@
1
+ # Wallet Adapter React Provider
2
+
3
+ A react provider wrapper for the Aptos Wallet Adapter
4
+
5
+ Dapps that want to use the adapter should install this package and other supported wallet packages.
6
+
7
+ #### Supported wallet packages
8
+
9
+ - Petra
10
+ - Martian
11
+ - Rise
12
+
13
+ ### Usage
14
+
15
+ #### Install Dependencies
16
+
17
+ Install wallet dependencies you want to include in your app. To do that, you can had to each of the wallet in the supported wallets list (each name is linked to the npm package page) above and follow the instructions.
18
+
19
+ Next, install the `@aptos-labs/wallet-adapter-react`
20
+
21
+ ```
22
+ pnpm i @aptos-labs/wallet-adapter-react
23
+ ```
24
+
25
+ using npm
26
+
27
+ ```
28
+ npm i @aptos-labs/wallet-adapter-react
29
+ ```
30
+
31
+ #### Import dependencies
32
+
33
+ On the `App.jsx` file,
34
+
35
+ Import the installed wallets.
36
+
37
+ ```js
38
+ import { AptosWallet } from "some-aptos-wallet-package";
39
+ ```
40
+
41
+ Import the `AptosWalletAdapterProvider`.
42
+
43
+ ```js
44
+ import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";
45
+ ```
46
+
47
+ Wrap your app with the Provider, pass it the `plugins (wallets)` you want to have on your app as an array and a `autoConnect` option (set to false by default)
48
+
49
+ ```js
50
+ const wallets = [new AptosWallet()];
51
+
52
+ <AptosWalletAdapterProvider plugins={wallets} autoConnect={true}>
53
+ <App />
54
+ </AptosWalletAdapterProvider>;
55
+ ```
56
+
57
+ #### Use Wallet
58
+
59
+ On any page you want to use the wallet props, import `useWallet` from `@aptos-labs/wallet-adapter-react`
60
+
61
+ ```js
62
+ import { useWallet } from "@aptos-labs/wallet-adapter-react";
63
+ ```
64
+
65
+ Then you can use the exported properties
66
+
67
+ ```js
68
+ const {
69
+ connect,
70
+ account,
71
+ network,
72
+ connected,
73
+ disconnect,
74
+ wallet,
75
+ wallets,
76
+ signAndSubmitTransaction,
77
+ signTransaction,
78
+ signMessage,
79
+ } = useWallet();
80
+ ```
81
+
82
+ #### Examples
83
+
84
+ ##### connect(walletName)
85
+
86
+ ```js
87
+ <button onClick={() => connect(wallet.name)}>{wallet.name}</button>
88
+ ```
89
+
90
+ ##### disconnect()
91
+
92
+ ```js
93
+ <button onClick={disconnect}>Disconnect</button>
94
+ ```
95
+
96
+ ##### signAndSubmitTransaction(payload)
97
+
98
+ ```js
99
+ const onSignAndSubmitTransaction = async () => {
100
+ const payload: Types.TransactionPayload = {
101
+ type: "entry_function_payload",
102
+ function: "0x1::coin::transfer",
103
+ type_arguments: ["0x1::aptos_coin::AptosCoin"],
104
+ arguments: [account?.address, 1], // 1 is in Octas
105
+ };
106
+ try {
107
+ const response = await signAndSubmitTransaction(payload);
108
+ // if you want to wait for transaction
109
+ await aptosClient.waitForTransaction(response?.hash || "");
110
+ console.log(response?.hash)
111
+ } catch (error: any) {
112
+ console.log("error", error);
113
+ }
114
+ };
115
+
116
+ <button onClick={onSignAndSubmitTransaction}>
117
+ Sign and submit transaction
118
+ </button>
119
+ ```
120
+
121
+ ##### signTransaction(payload)
122
+
123
+ ```js
124
+ const onSignTransaction = async () => {
125
+ const payload: Types.TransactionPayload = {
126
+ type: "entry_function_payload",
127
+ function: "0x1::coin::transfer",
128
+ type_arguments: ["0x1::aptos_coin::AptosCoin"],
129
+ arguments: [account?.address, 1], // 1 is in Octas
130
+ };
131
+ try {
132
+ const response = await signTransaction(payload);
133
+ console.log("response", response);
134
+ } catch (error: any) {
135
+ console.log("error", error);
136
+ }
137
+ };
138
+
139
+ <button onClick={onSignTransaction}>
140
+ Sign transaction
141
+ </button>
142
+ ```
143
+
144
+ ##### signMessage(payload)
145
+
146
+ ```js
147
+ const onSignMessage = async () => {
148
+ const payload = {
149
+ message: "Hello from Aptos Wallet Adapter",
150
+ nonce: "random_string",
151
+ };
152
+ try {
153
+ const response = await signMessage(payload);
154
+ console.log("response", response);
155
+ } catch (error: any) {
156
+ console.log("error", error);
157
+ }
158
+ };
159
+
160
+ <button onClick={onSignMessage}>
161
+ Sign message
162
+ </button>
163
+ ```
164
+
165
+ ##### Account
166
+
167
+ ```js
168
+ <div>{account?.address}</div>
169
+ <div>{account?.publicKey}</div>
170
+ ```
171
+
172
+ ##### Network
173
+
174
+ ```js
175
+ <div>{network?.name}</div>
176
+ ```
177
+
178
+ ##### Wallet
179
+
180
+ ```js
181
+ <div>{wallet?.name}</div>
182
+ <div>{wallet?.icon}</div>
183
+ <div>{wallet?.url}</div>
184
+ ```
185
+
186
+ ##### Wallets
187
+
188
+ ```js
189
+ {
190
+ wallets.map((wallet) => <p>{wallet.name}</p>);
191
+ }
192
+ ```
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@aptos-labs/wallet-adapter-react",
3
+ "version": "0.1.3",
4
+ "description": "Aptos Wallet Adapter React Provider",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "license": "Apache-2.0",
9
+ "exports": {
10
+ "require": "./dist/index.js",
11
+ "import": "./dist/index.mjs",
12
+ "types": "./dist/index.d.ts"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/aptos-labs/aptos-wallet-adapter.git"
17
+ },
18
+ "homepage": "https://github.com/aptos-labs/aptos-wallet-adapter",
19
+ "bugs": {
20
+ "url": "https://github.com/aptos-labs/aptos-wallet-adapter/issues"
21
+ },
22
+ "author": "aptoslabs.com",
23
+ "keywords": [
24
+ "Aptos",
25
+ "Aptos Labs",
26
+ "Wallet",
27
+ "Wallet Adapter",
28
+ "Wallet Adapter Provider",
29
+ "React"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsup src/index.tsx --format esm,cjs --dts --external react",
33
+ "dev": "tsup src/index.tsx --format esm,cjs --watch --dts --external react",
34
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
35
+ "lint": "TIMING=1 eslint \"src/**/*.ts*\""
36
+ },
37
+ "peerDependencies": {
38
+ "react": "*"
39
+ },
40
+ "devDependencies": {
41
+ "@aptos-labs/wallet-adapter-tsconfig": "workspace:*",
42
+ "@types/react": "^18.0.17",
43
+ "@types/react-dom": "^18.0.6",
44
+ "eslint": "^8.15.0",
45
+ "typescript": "^4.5.3",
46
+ "tsup": "^5.10.1"
47
+ },
48
+ "dependencies": {
49
+ "@aptos-labs/wallet-adapter-core": "workspace:*",
50
+ "aptos": "^1.3.17"
51
+ }
52
+ }
@@ -0,0 +1,209 @@
1
+ import * as React from "react";
2
+ import {
3
+ FC,
4
+ ReactNode,
5
+ useCallback,
6
+ useEffect,
7
+ useMemo,
8
+ useState,
9
+ } from "react";
10
+ import { WalletContext } from "./useWallet";
11
+ import type {
12
+ AccountInfo,
13
+ NetworkInfo,
14
+ SignMessagePayload,
15
+ Wallet,
16
+ WalletInfo,
17
+ WalletName,
18
+ } from "@aptos-labs/wallet-adapter-core";
19
+ import { WalletCore } from "@aptos-labs/wallet-adapter-core";
20
+
21
+ import { Types } from "aptos";
22
+
23
+ export interface AptosWalletProviderProps {
24
+ children: ReactNode;
25
+ plugins: Wallet[];
26
+ autoConnect?: boolean;
27
+ }
28
+
29
+ const initialState: {
30
+ account: AccountInfo | null;
31
+ network: NetworkInfo | null;
32
+ connected: boolean;
33
+ wallet: WalletInfo | null;
34
+ } = {
35
+ connected: false,
36
+ account: null,
37
+ network: null,
38
+ wallet: null,
39
+ };
40
+
41
+ export const AptosWalletAdapterProvider: FC<AptosWalletProviderProps> = ({
42
+ children,
43
+ plugins,
44
+ autoConnect = false,
45
+ }: AptosWalletProviderProps) => {
46
+ const [{ connected, account, network, wallet }, setState] =
47
+ useState(initialState);
48
+
49
+ const walletCore = useMemo(() => new WalletCore(plugins), []);
50
+ const [wallets, setWallets] = useState<Wallet[]>(walletCore.wallets);
51
+
52
+ const connect = (walletName: WalletName) => {
53
+ try {
54
+ walletCore.connect(walletName);
55
+ } catch (e) {
56
+ console.log("connect error", e);
57
+ }
58
+ };
59
+
60
+ const disconnect = () => {
61
+ try {
62
+ walletCore.disconnect();
63
+ } catch (e) {
64
+ console.log("disconnect error", e);
65
+ }
66
+ };
67
+
68
+ const signAndSubmitTransaction = async (
69
+ transaction: Types.TransactionPayload
70
+ ) => {
71
+ try {
72
+ return await walletCore.signAndSubmitTransaction(transaction);
73
+ } catch (error: any) {
74
+ throw error;
75
+ }
76
+ };
77
+
78
+ const signTransaction = async (transaction: Types.TransactionPayload) => {
79
+ try {
80
+ return await walletCore.signTransaction(transaction);
81
+ } catch (error: any) {
82
+ throw error;
83
+ }
84
+ };
85
+
86
+ const signMessage = async (message: SignMessagePayload) => {
87
+ try {
88
+ return await walletCore.signMessage(message);
89
+ } catch (error: any) {
90
+ throw error;
91
+ }
92
+ };
93
+
94
+ useEffect(() => {
95
+ if (autoConnect) {
96
+ if (localStorage.getItem("AptosWalletName")) {
97
+ connect(localStorage.getItem("AptosWalletName") as WalletName);
98
+ }
99
+ }
100
+ }, [wallets]);
101
+
102
+ useEffect(() => {
103
+ if (connected) {
104
+ walletCore.onAccountChange();
105
+ walletCore.onNetworkChange();
106
+ }
107
+ }, [wallets, connected]);
108
+
109
+ // Handle the adapter's connect event
110
+ const handleConnect = () => {
111
+ setState((state) => {
112
+ return {
113
+ ...state,
114
+ connected: true,
115
+ account: walletCore.account,
116
+ network: walletCore.network,
117
+ wallet: walletCore.wallet,
118
+ };
119
+ });
120
+ };
121
+
122
+ // Handle the adapter's disconnect event
123
+ const handleDisconnect = () => {
124
+ if (!connected) return;
125
+ setState((state) => {
126
+ return {
127
+ ...state,
128
+ connected: false,
129
+ account: walletCore.account,
130
+ network: walletCore.network,
131
+ wallet: null,
132
+ };
133
+ });
134
+ };
135
+
136
+ // Handle the adapter's account change event
137
+ const handleAccountChange = useCallback(() => {
138
+ if (!connected) return;
139
+ if (!walletCore.wallet) return;
140
+ setState((state) => {
141
+ return {
142
+ ...state,
143
+ account: walletCore.account,
144
+ };
145
+ });
146
+ }, [connected]);
147
+
148
+ // Handle the adapter's network event
149
+ const handleNetworkChange = useCallback(() => {
150
+ if (!connected) return;
151
+ if (!walletCore.wallet) return;
152
+ setState((state) => {
153
+ return {
154
+ ...state,
155
+ network: walletCore.network,
156
+ };
157
+ });
158
+ }, [connected]);
159
+
160
+ // Handle the adapter's ready state change event
161
+ const handleReadyStateChange = (wallet: Wallet) => {
162
+ setWallets((prevWallets) => {
163
+ const index = prevWallets.findIndex(
164
+ (currWallet) => currWallet === wallet
165
+ );
166
+ if (index === -1) return prevWallets;
167
+
168
+ return [
169
+ ...prevWallets.slice(0, index),
170
+ wallet,
171
+ ...prevWallets.slice(index + 1),
172
+ ];
173
+ });
174
+ };
175
+
176
+ useEffect(() => {
177
+ walletCore.on("connect", handleConnect);
178
+ walletCore.on("disconnect", handleDisconnect);
179
+ walletCore.on("accountChange", handleAccountChange);
180
+ walletCore.on("networkChange", handleNetworkChange);
181
+ walletCore.on("readyStateChange", handleReadyStateChange);
182
+ return () => {
183
+ walletCore.off("connect", handleConnect);
184
+ walletCore.off("disconnect", handleDisconnect);
185
+ walletCore.off("accountChange", handleAccountChange);
186
+ walletCore.off("networkChange", handleNetworkChange);
187
+ walletCore.off("readyStateChange", handleReadyStateChange);
188
+ };
189
+ }, [wallets, connected]);
190
+
191
+ return (
192
+ <WalletContext.Provider
193
+ value={{
194
+ connect,
195
+ account,
196
+ network,
197
+ connected,
198
+ disconnect,
199
+ wallet,
200
+ wallets,
201
+ signAndSubmitTransaction,
202
+ signTransaction,
203
+ signMessage,
204
+ }}
205
+ >
206
+ {children}
207
+ </WalletContext.Provider>
208
+ );
209
+ };
package/src/index.tsx ADDED
@@ -0,0 +1,3 @@
1
+ import * as React from "react";
2
+ export { useWallet } from "./useWallet";
3
+ export * from "./WalletProvider";
@@ -0,0 +1,48 @@
1
+ import {
2
+ AccountInfo,
3
+ NetworkInfo,
4
+ WalletInfo,
5
+ WalletName,
6
+ SignMessagePayload,
7
+ SignMessageResponse,
8
+ Wallet,
9
+ } from "@aptos-labs/wallet-adapter-core";
10
+ import { createContext, useContext } from "react";
11
+ import { Types } from "aptos";
12
+
13
+ export type { WalletName };
14
+
15
+ export interface WalletContextState {
16
+ connected: boolean;
17
+ account: AccountInfo | null;
18
+ network: NetworkInfo | null;
19
+ connect(walletName: WalletName): void;
20
+ disconnect(): void;
21
+ wallet: WalletInfo | null;
22
+ wallets: Wallet[];
23
+ signAndSubmitTransaction<T extends Types.TransactionPayload, V>(
24
+ transaction: T,
25
+ options?: V
26
+ ): Promise<any>;
27
+ signTransaction<T extends Types.TransactionPayload, V>(
28
+ transaction: T,
29
+ options?: V
30
+ ): Promise<any>;
31
+ signMessage(message: SignMessagePayload): Promise<SignMessageResponse | null>;
32
+ }
33
+
34
+ const DEFAULT_COUNTEXT = {
35
+ connected: false,
36
+ };
37
+
38
+ export const WalletContext = createContext<WalletContextState>(
39
+ DEFAULT_COUNTEXT as WalletContextState
40
+ );
41
+
42
+ export function useWallet(): WalletContextState {
43
+ const context = useContext(WalletContext);
44
+ if (!context) {
45
+ throw new Error("useWallet must be used within a WalletContextState");
46
+ }
47
+ return context;
48
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "@aptos-labs/wallet-adapter-tsconfig/react-library.json",
3
+ "include": ["."],
4
+ "exclude": ["dist", "build", "node_modules"]
5
+ }