@aptos-labs/wallet-adapter-react 0.1.4 → 0.1.5
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/dist/index.d.ts +26 -0
- package/dist/index.js +206 -0
- package/dist/index.mjs +183 -0
- package/package.json +10 -10
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AccountInfo, NetworkInfo, WalletName, WalletInfo, Wallet, SignMessagePayload, SignMessageResponse } from '@aptos-labs/wallet-adapter-core';
|
|
2
|
+
import { Types } from 'aptos';
|
|
3
|
+
import { ReactNode, FC } from 'react';
|
|
4
|
+
|
|
5
|
+
interface WalletContextState {
|
|
6
|
+
connected: boolean;
|
|
7
|
+
account: AccountInfo | null;
|
|
8
|
+
network: NetworkInfo | null;
|
|
9
|
+
connect(walletName: WalletName): void;
|
|
10
|
+
disconnect(): void;
|
|
11
|
+
wallet: WalletInfo | null;
|
|
12
|
+
wallets: Wallet[];
|
|
13
|
+
signAndSubmitTransaction<T extends Types.TransactionPayload, V>(transaction: T, options?: V): Promise<any>;
|
|
14
|
+
signTransaction<T extends Types.TransactionPayload, V>(transaction: T, options?: V): Promise<any>;
|
|
15
|
+
signMessage(message: SignMessagePayload): Promise<SignMessageResponse | null>;
|
|
16
|
+
}
|
|
17
|
+
declare function useWallet(): WalletContextState;
|
|
18
|
+
|
|
19
|
+
interface AptosWalletProviderProps {
|
|
20
|
+
children: ReactNode;
|
|
21
|
+
plugins: Wallet[];
|
|
22
|
+
autoConnect?: boolean;
|
|
23
|
+
}
|
|
24
|
+
declare const AptosWalletAdapterProvider: FC<AptosWalletProviderProps>;
|
|
25
|
+
|
|
26
|
+
export { AptosWalletAdapterProvider, AptosWalletProviderProps, useWallet };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.tsx
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
AptosWalletAdapterProvider: () => AptosWalletAdapterProvider,
|
|
24
|
+
useWallet: () => useWallet
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/useWallet.tsx
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
var DEFAULT_COUNTEXT = {
|
|
31
|
+
connected: false
|
|
32
|
+
};
|
|
33
|
+
var WalletContext = (0, import_react.createContext)(
|
|
34
|
+
DEFAULT_COUNTEXT
|
|
35
|
+
);
|
|
36
|
+
function useWallet() {
|
|
37
|
+
const context = (0, import_react.useContext)(WalletContext);
|
|
38
|
+
if (!context) {
|
|
39
|
+
throw new Error("useWallet must be used within a WalletContextState");
|
|
40
|
+
}
|
|
41
|
+
return context;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/WalletProvider.tsx
|
|
45
|
+
var import_react2 = require("react");
|
|
46
|
+
var import_wallet_adapter_core = require("@aptos-labs/wallet-adapter-core");
|
|
47
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
48
|
+
var initialState = {
|
|
49
|
+
connected: false,
|
|
50
|
+
account: null,
|
|
51
|
+
network: null,
|
|
52
|
+
wallet: null
|
|
53
|
+
};
|
|
54
|
+
var AptosWalletAdapterProvider = ({
|
|
55
|
+
children,
|
|
56
|
+
plugins,
|
|
57
|
+
autoConnect = false
|
|
58
|
+
}) => {
|
|
59
|
+
const [{ connected, account, network, wallet }, setState] = (0, import_react2.useState)(initialState);
|
|
60
|
+
const walletCore = (0, import_react2.useMemo)(() => new import_wallet_adapter_core.WalletCore(plugins), []);
|
|
61
|
+
const [wallets, setWallets] = (0, import_react2.useState)(walletCore.wallets);
|
|
62
|
+
const connect = (walletName) => {
|
|
63
|
+
try {
|
|
64
|
+
walletCore.connect(walletName);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
console.log("connect error", e);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const disconnect = () => {
|
|
70
|
+
try {
|
|
71
|
+
walletCore.disconnect();
|
|
72
|
+
} catch (e) {
|
|
73
|
+
console.log("disconnect error", e);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const signAndSubmitTransaction = async (transaction) => {
|
|
77
|
+
try {
|
|
78
|
+
return await walletCore.signAndSubmitTransaction(transaction);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const signTransaction = async (transaction) => {
|
|
84
|
+
try {
|
|
85
|
+
return await walletCore.signTransaction(transaction);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const signMessage = async (message) => {
|
|
91
|
+
try {
|
|
92
|
+
return await walletCore.signMessage(message);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
(0, import_react2.useEffect)(() => {
|
|
98
|
+
if (autoConnect) {
|
|
99
|
+
if (localStorage.getItem("AptosWalletName")) {
|
|
100
|
+
connect(localStorage.getItem("AptosWalletName"));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}, [wallets]);
|
|
104
|
+
(0, import_react2.useEffect)(() => {
|
|
105
|
+
if (connected) {
|
|
106
|
+
walletCore.onAccountChange();
|
|
107
|
+
walletCore.onNetworkChange();
|
|
108
|
+
}
|
|
109
|
+
}, [wallets, connected]);
|
|
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
|
+
const handleDisconnect = () => {
|
|
122
|
+
if (!connected)
|
|
123
|
+
return;
|
|
124
|
+
setState((state) => {
|
|
125
|
+
return {
|
|
126
|
+
...state,
|
|
127
|
+
connected: false,
|
|
128
|
+
account: walletCore.account,
|
|
129
|
+
network: walletCore.network,
|
|
130
|
+
wallet: null
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
const handleAccountChange = (0, import_react2.useCallback)(() => {
|
|
135
|
+
if (!connected)
|
|
136
|
+
return;
|
|
137
|
+
if (!walletCore.wallet)
|
|
138
|
+
return;
|
|
139
|
+
setState((state) => {
|
|
140
|
+
return {
|
|
141
|
+
...state,
|
|
142
|
+
account: walletCore.account
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
}, [connected]);
|
|
146
|
+
const handleNetworkChange = (0, import_react2.useCallback)(() => {
|
|
147
|
+
if (!connected)
|
|
148
|
+
return;
|
|
149
|
+
if (!walletCore.wallet)
|
|
150
|
+
return;
|
|
151
|
+
setState((state) => {
|
|
152
|
+
return {
|
|
153
|
+
...state,
|
|
154
|
+
network: walletCore.network
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
}, [connected]);
|
|
158
|
+
const handleReadyStateChange = (wallet2) => {
|
|
159
|
+
setWallets((prevWallets) => {
|
|
160
|
+
const index = prevWallets.findIndex(
|
|
161
|
+
(currWallet) => currWallet === wallet2
|
|
162
|
+
);
|
|
163
|
+
if (index === -1)
|
|
164
|
+
return prevWallets;
|
|
165
|
+
return [
|
|
166
|
+
...prevWallets.slice(0, index),
|
|
167
|
+
wallet2,
|
|
168
|
+
...prevWallets.slice(index + 1)
|
|
169
|
+
];
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
(0, import_react2.useEffect)(() => {
|
|
173
|
+
walletCore.on("connect", handleConnect);
|
|
174
|
+
walletCore.on("disconnect", handleDisconnect);
|
|
175
|
+
walletCore.on("accountChange", handleAccountChange);
|
|
176
|
+
walletCore.on("networkChange", handleNetworkChange);
|
|
177
|
+
walletCore.on("readyStateChange", handleReadyStateChange);
|
|
178
|
+
return () => {
|
|
179
|
+
walletCore.off("connect", handleConnect);
|
|
180
|
+
walletCore.off("disconnect", handleDisconnect);
|
|
181
|
+
walletCore.off("accountChange", handleAccountChange);
|
|
182
|
+
walletCore.off("networkChange", handleNetworkChange);
|
|
183
|
+
walletCore.off("readyStateChange", handleReadyStateChange);
|
|
184
|
+
};
|
|
185
|
+
}, [wallets, connected]);
|
|
186
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(WalletContext.Provider, {
|
|
187
|
+
value: {
|
|
188
|
+
connect,
|
|
189
|
+
account,
|
|
190
|
+
network,
|
|
191
|
+
connected,
|
|
192
|
+
disconnect,
|
|
193
|
+
wallet,
|
|
194
|
+
wallets,
|
|
195
|
+
signAndSubmitTransaction,
|
|
196
|
+
signTransaction,
|
|
197
|
+
signMessage
|
|
198
|
+
},
|
|
199
|
+
children
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
203
|
+
0 && (module.exports = {
|
|
204
|
+
AptosWalletAdapterProvider,
|
|
205
|
+
useWallet
|
|
206
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// src/useWallet.tsx
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
var DEFAULT_COUNTEXT = {
|
|
4
|
+
connected: false
|
|
5
|
+
};
|
|
6
|
+
var WalletContext = createContext(
|
|
7
|
+
DEFAULT_COUNTEXT
|
|
8
|
+
);
|
|
9
|
+
function useWallet() {
|
|
10
|
+
const context = useContext(WalletContext);
|
|
11
|
+
if (!context) {
|
|
12
|
+
throw new Error("useWallet must be used within a WalletContextState");
|
|
13
|
+
}
|
|
14
|
+
return context;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/WalletProvider.tsx
|
|
18
|
+
import {
|
|
19
|
+
useCallback,
|
|
20
|
+
useEffect,
|
|
21
|
+
useMemo,
|
|
22
|
+
useState
|
|
23
|
+
} from "react";
|
|
24
|
+
import { WalletCore } from "@aptos-labs/wallet-adapter-core";
|
|
25
|
+
import { jsx } from "react/jsx-runtime";
|
|
26
|
+
var initialState = {
|
|
27
|
+
connected: false,
|
|
28
|
+
account: null,
|
|
29
|
+
network: null,
|
|
30
|
+
wallet: null
|
|
31
|
+
};
|
|
32
|
+
var AptosWalletAdapterProvider = ({
|
|
33
|
+
children,
|
|
34
|
+
plugins,
|
|
35
|
+
autoConnect = false
|
|
36
|
+
}) => {
|
|
37
|
+
const [{ connected, account, network, wallet }, setState] = useState(initialState);
|
|
38
|
+
const walletCore = useMemo(() => new WalletCore(plugins), []);
|
|
39
|
+
const [wallets, setWallets] = useState(walletCore.wallets);
|
|
40
|
+
const connect = (walletName) => {
|
|
41
|
+
try {
|
|
42
|
+
walletCore.connect(walletName);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.log("connect error", e);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const disconnect = () => {
|
|
48
|
+
try {
|
|
49
|
+
walletCore.disconnect();
|
|
50
|
+
} catch (e) {
|
|
51
|
+
console.log("disconnect error", e);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const signAndSubmitTransaction = async (transaction) => {
|
|
55
|
+
try {
|
|
56
|
+
return await walletCore.signAndSubmitTransaction(transaction);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const signTransaction = async (transaction) => {
|
|
62
|
+
try {
|
|
63
|
+
return await walletCore.signTransaction(transaction);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const signMessage = async (message) => {
|
|
69
|
+
try {
|
|
70
|
+
return await walletCore.signMessage(message);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (autoConnect) {
|
|
77
|
+
if (localStorage.getItem("AptosWalletName")) {
|
|
78
|
+
connect(localStorage.getItem("AptosWalletName"));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}, [wallets]);
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
if (connected) {
|
|
84
|
+
walletCore.onAccountChange();
|
|
85
|
+
walletCore.onNetworkChange();
|
|
86
|
+
}
|
|
87
|
+
}, [wallets, connected]);
|
|
88
|
+
const handleConnect = () => {
|
|
89
|
+
setState((state) => {
|
|
90
|
+
return {
|
|
91
|
+
...state,
|
|
92
|
+
connected: true,
|
|
93
|
+
account: walletCore.account,
|
|
94
|
+
network: walletCore.network,
|
|
95
|
+
wallet: walletCore.wallet
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
const handleDisconnect = () => {
|
|
100
|
+
if (!connected)
|
|
101
|
+
return;
|
|
102
|
+
setState((state) => {
|
|
103
|
+
return {
|
|
104
|
+
...state,
|
|
105
|
+
connected: false,
|
|
106
|
+
account: walletCore.account,
|
|
107
|
+
network: walletCore.network,
|
|
108
|
+
wallet: null
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
const handleAccountChange = useCallback(() => {
|
|
113
|
+
if (!connected)
|
|
114
|
+
return;
|
|
115
|
+
if (!walletCore.wallet)
|
|
116
|
+
return;
|
|
117
|
+
setState((state) => {
|
|
118
|
+
return {
|
|
119
|
+
...state,
|
|
120
|
+
account: walletCore.account
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}, [connected]);
|
|
124
|
+
const handleNetworkChange = useCallback(() => {
|
|
125
|
+
if (!connected)
|
|
126
|
+
return;
|
|
127
|
+
if (!walletCore.wallet)
|
|
128
|
+
return;
|
|
129
|
+
setState((state) => {
|
|
130
|
+
return {
|
|
131
|
+
...state,
|
|
132
|
+
network: walletCore.network
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
}, [connected]);
|
|
136
|
+
const handleReadyStateChange = (wallet2) => {
|
|
137
|
+
setWallets((prevWallets) => {
|
|
138
|
+
const index = prevWallets.findIndex(
|
|
139
|
+
(currWallet) => currWallet === wallet2
|
|
140
|
+
);
|
|
141
|
+
if (index === -1)
|
|
142
|
+
return prevWallets;
|
|
143
|
+
return [
|
|
144
|
+
...prevWallets.slice(0, index),
|
|
145
|
+
wallet2,
|
|
146
|
+
...prevWallets.slice(index + 1)
|
|
147
|
+
];
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
walletCore.on("connect", handleConnect);
|
|
152
|
+
walletCore.on("disconnect", handleDisconnect);
|
|
153
|
+
walletCore.on("accountChange", handleAccountChange);
|
|
154
|
+
walletCore.on("networkChange", handleNetworkChange);
|
|
155
|
+
walletCore.on("readyStateChange", handleReadyStateChange);
|
|
156
|
+
return () => {
|
|
157
|
+
walletCore.off("connect", handleConnect);
|
|
158
|
+
walletCore.off("disconnect", handleDisconnect);
|
|
159
|
+
walletCore.off("accountChange", handleAccountChange);
|
|
160
|
+
walletCore.off("networkChange", handleNetworkChange);
|
|
161
|
+
walletCore.off("readyStateChange", handleReadyStateChange);
|
|
162
|
+
};
|
|
163
|
+
}, [wallets, connected]);
|
|
164
|
+
return /* @__PURE__ */ jsx(WalletContext.Provider, {
|
|
165
|
+
value: {
|
|
166
|
+
connect,
|
|
167
|
+
account,
|
|
168
|
+
network,
|
|
169
|
+
connected,
|
|
170
|
+
disconnect,
|
|
171
|
+
wallet,
|
|
172
|
+
wallets,
|
|
173
|
+
signAndSubmitTransaction,
|
|
174
|
+
signTransaction,
|
|
175
|
+
signMessage
|
|
176
|
+
},
|
|
177
|
+
children
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
export {
|
|
181
|
+
AptosWalletAdapterProvider,
|
|
182
|
+
useWallet
|
|
183
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aptos-labs/wallet-adapter-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Aptos Wallet Adapter React Provider",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -28,25 +28,25 @@
|
|
|
28
28
|
"Wallet Adapter Provider",
|
|
29
29
|
"React"
|
|
30
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
31
|
"peerDependencies": {
|
|
38
32
|
"react": "*"
|
|
39
33
|
},
|
|
40
34
|
"devDependencies": {
|
|
41
|
-
"@aptos-labs/wallet-adapter-tsconfig": "workspace:*",
|
|
42
35
|
"@types/react": "^18.0.17",
|
|
43
36
|
"@types/react-dom": "^18.0.6",
|
|
44
37
|
"eslint": "^8.15.0",
|
|
45
38
|
"typescript": "^4.5.3",
|
|
46
|
-
"tsup": "^5.10.1"
|
|
39
|
+
"tsup": "^5.10.1",
|
|
40
|
+
"@aptos-labs/wallet-adapter-tsconfig": "0.0.0"
|
|
47
41
|
},
|
|
48
42
|
"dependencies": {
|
|
49
43
|
"@aptos-labs/wallet-adapter-core": "*",
|
|
50
44
|
"aptos": "^1.3.17"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup src/index.tsx --format esm,cjs --dts --external react",
|
|
48
|
+
"dev": "tsup src/index.tsx --format esm,cjs --watch --dts --external react",
|
|
49
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
|
|
50
|
+
"lint": "TIMING=1 eslint \"src/**/*.ts*\""
|
|
51
51
|
}
|
|
52
|
-
}
|
|
52
|
+
}
|