@manahippo/aptos-wallet-adapter 0.4.10 → 0.4.12

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.
@@ -0,0 +1,280 @@
1
+ import { MaybeHexString } from 'aptos';
2
+ import { TransactionPayload, HexEncodedBytes } from 'aptos/src/generated';
3
+ import {
4
+ WalletDisconnectionError,
5
+ WalletNotConnectedError,
6
+ WalletGetNetworkError,
7
+ WalletNotReadyError,
8
+ WalletSignAndSubmitMessageError,
9
+ WalletSignMessageError,
10
+ WalletSignTransactionError
11
+ } from '../WalletProviders/errors';
12
+ import {
13
+ AccountKeys,
14
+ BaseWalletAdapter,
15
+ NetworkInfo,
16
+ scopePollingDetectionStrategy,
17
+ SignMessagePayload,
18
+ SignMessageResponse,
19
+ WalletAdapterNetwork,
20
+ WalletName,
21
+ WalletReadyState
22
+ } from './BaseAdapter';
23
+
24
+ interface TokenPocketAccount {
25
+ address: MaybeHexString;
26
+ publicKey: MaybeHexString;
27
+ }
28
+
29
+ interface ITokenPocketWallet {
30
+ isTokenPocket: boolean;
31
+ connect: () => Promise<TokenPocketAccount>;
32
+ account: () => Promise<TokenPocketAccount>;
33
+ isConnected: () => Promise<boolean>;
34
+ signAndSubmitTransaction(
35
+ transaction: TransactionPayload,
36
+ options?: any
37
+ ): Promise<{ hash: HexEncodedBytes }>;
38
+ network: () => Promise<WalletAdapterNetwork>;
39
+ getChainId: () => Promise<string>;
40
+ getNodeUrl: () => Promise<string>;
41
+ signTransaction(transaction: TransactionPayload, options?: any): Promise<Uint8Array>;
42
+ signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
43
+ disconnect(): Promise<void>;
44
+ onAccountChange: (listener: (newAddress: TokenPocketAccount) => void) => void;
45
+ onNetworkChange: (listener: (network: { networkName: string }) => void) => void;
46
+ }
47
+
48
+ interface TokenPocketWindow extends Window {
49
+ aptos?: ITokenPocketWallet;
50
+ }
51
+
52
+ declare const window: TokenPocketWindow;
53
+
54
+ export const TokenPocketWalletName = 'TokenPocket' as WalletName<'TokenPocket'>;
55
+
56
+ export interface TokenPocketWalletAdapterConfig {
57
+ provider?: ITokenPocketWallet;
58
+ // network?: WalletAdapterNetwork;
59
+ timeout?: number;
60
+ }
61
+
62
+ export class TokenPocketWalletAdapter extends BaseWalletAdapter {
63
+ name = TokenPocketWalletName;
64
+
65
+ url = 'https://tokenpocket.pro';
66
+
67
+ icon = 'https://tp-statics.tokenpocket.pro/logo/tokenpocket.png';
68
+
69
+ protected _provider: ITokenPocketWallet | undefined;
70
+
71
+ protected _network: WalletAdapterNetwork;
72
+
73
+ protected _chainId: string;
74
+
75
+ protected _api: string;
76
+
77
+ // protected _network: WalletAdapterNetwork;
78
+ protected _timeout: number;
79
+
80
+ protected _readyState: WalletReadyState =
81
+ typeof window === 'undefined' || typeof document === 'undefined'
82
+ ? WalletReadyState.Unsupported
83
+ : WalletReadyState.NotDetected;
84
+
85
+ protected _connecting: boolean;
86
+
87
+ protected _wallet: any | null;
88
+
89
+ constructor({
90
+ // provider,
91
+ // network = WalletAdapterNetwork.Mainnet,
92
+ timeout = 10000
93
+ }: TokenPocketWalletAdapterConfig = {}) {
94
+ super();
95
+
96
+ this._provider = typeof window !== 'undefined' ? window.aptos : undefined;
97
+ this._network = undefined;
98
+ this._timeout = timeout;
99
+ this._connecting = false;
100
+ this._wallet = null;
101
+
102
+ if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
103
+ scopePollingDetectionStrategy(() => {
104
+ if (window.aptos && window.aptos?.isTokenPocket) {
105
+ this._readyState = WalletReadyState.Installed;
106
+ this.emit('readyStateChange', this._readyState);
107
+ return true;
108
+ }
109
+ return false;
110
+ });
111
+ }
112
+ }
113
+
114
+ get publicAccount(): AccountKeys {
115
+ return {
116
+ publicKey: this._wallet?.publicKey || null,
117
+ address: this._wallet?.address || null,
118
+ authKey: this._wallet?.authKey || null
119
+ };
120
+ }
121
+
122
+ get network(): NetworkInfo {
123
+ return {
124
+ name: this._network,
125
+ api: this._api,
126
+ chainId: this._chainId
127
+ }
128
+ }
129
+
130
+ get connecting(): boolean {
131
+ return this._connecting;
132
+ }
133
+
134
+ get connected(): boolean {
135
+ return !!this._wallet?.isConnected;
136
+ }
137
+
138
+ get readyState(): WalletReadyState {
139
+ return this._readyState;
140
+ }
141
+
142
+ async connect(): Promise<void> {
143
+ try {
144
+ if (this.connected || this.connecting) return;
145
+ if (
146
+ !(
147
+ this._readyState === WalletReadyState.Loadable ||
148
+ this._readyState === WalletReadyState.Installed
149
+ )
150
+ )
151
+ throw new WalletNotReadyError();
152
+ this._connecting = true;
153
+
154
+ const provider = this._provider || window.aptos;
155
+ const isConnected = await provider?.isConnected();
156
+ if (isConnected) {
157
+ await provider?.disconnect();
158
+ }
159
+ const response = await provider?.connect();
160
+
161
+ if (!response) {
162
+ throw new WalletNotConnectedError('No connect response');
163
+ }
164
+
165
+ this._wallet = {
166
+ address: response?.address,
167
+ publicKey: response?.publicKey,
168
+ isConnected: true
169
+ };
170
+
171
+ try {
172
+ const network = await provider?.network();
173
+ const chainId = await provider?.getChainId();
174
+ const api = await provider?.getNodeUrl();
175
+
176
+ this._network = network;
177
+ this._chainId = chainId;
178
+ this._api = api;
179
+ } catch (error: any) {
180
+ const errMsg = error.message;
181
+ this.emit('error', new WalletGetNetworkError(errMsg));
182
+ throw error;
183
+ }
184
+
185
+ this.emit('connect', this._wallet.address);
186
+ } catch (error: any) {
187
+ this.emit('error', new Error(error));
188
+ throw error;
189
+ } finally {
190
+ this._connecting = false;
191
+ }
192
+ }
193
+
194
+ async disconnect(): Promise<void> {
195
+ const wallet = this._wallet;
196
+ const provider = this._provider || window.aptos;
197
+ if (wallet) {
198
+ this._wallet = null;
199
+
200
+ try {
201
+ await provider?.disconnect();
202
+ } catch (error: any) {
203
+ this.emit('error', new WalletDisconnectionError(error?.message, error));
204
+ }
205
+ }
206
+
207
+ this.emit('disconnect');
208
+ }
209
+
210
+ async signTransaction(transaction: TransactionPayload, options?: any): Promise<Uint8Array> {
211
+ try {
212
+ const wallet = this._wallet;
213
+ const provider = this._provider || window.aptos;
214
+ if (!wallet || !provider) throw new WalletNotConnectedError();
215
+
216
+ const response = await provider.signTransaction(transaction, options);
217
+
218
+ if (!response) {
219
+ throw new Error('No response');
220
+ }
221
+ return response;
222
+ } catch (error: any) {
223
+ this.emit('error', new WalletSignTransactionError(error));
224
+ throw error;
225
+ }
226
+ }
227
+
228
+ async signAndSubmitTransaction(
229
+ transaction: TransactionPayload,
230
+ options?: any
231
+ ): Promise<{ hash: HexEncodedBytes }> {
232
+ try {
233
+ const wallet = this._wallet;
234
+ const provider = this._provider || window.aptos;
235
+ if (!wallet || !provider) throw new WalletNotConnectedError();
236
+
237
+ const response = await provider.signAndSubmitTransaction(transaction, options);
238
+
239
+ if (!response) {
240
+ throw new Error('No response');
241
+ }
242
+ return response as { hash: HexEncodedBytes };
243
+ } catch (error: any) {
244
+ this.emit('error', new WalletSignAndSubmitMessageError(error));
245
+ throw error;
246
+ }
247
+ }
248
+
249
+ async signMessage(msgPayload: SignMessagePayload): Promise<SignMessageResponse> {
250
+ try {
251
+ const wallet = this._wallet;
252
+ const provider = this._provider || window.aptos;
253
+ if (!wallet || !provider) throw new WalletNotConnectedError();
254
+ if (typeof msgPayload !== 'object' || !msgPayload.nonce) {
255
+ throw new WalletSignMessageError('Invalid signMessage Payload');
256
+ }
257
+ const response = await provider?.signMessage(msgPayload);
258
+ if (response) {
259
+ return response;
260
+ } else {
261
+ throw new Error('Sign Message failed');
262
+ }
263
+ } catch (error: any) {
264
+ const errMsg = error.message;
265
+ this.emit('error', new WalletSignMessageError(errMsg));
266
+ throw error;
267
+ }
268
+ }
269
+
270
+
271
+ async onNetworkChange(): Promise<void> {
272
+
273
+ }
274
+
275
+ async onAccountChange(): Promise<void> {
276
+
277
+ }
278
+
279
+
280
+ }
@@ -11,3 +11,5 @@ export * from './SpikaWallet';
11
11
  export * from './HyperPayWallet';
12
12
  export * from './FletchWallet';
13
13
  export * from './AptosSnap';
14
+ export * from './BitkeepWallet';
15
+ export * from './TokenPocketWallet';
@@ -229,20 +229,14 @@ export const WalletProvider: FC<WalletProviderProps> = ({
229
229
 
230
230
  // If autoConnect is enabled, try to connect when the adapter changes and is ready
231
231
  useEffect(() => {
232
- if (isConnecting.current || connected || !autoConnect || !adapter) return;
233
-
234
- // Handle wallet not installed in Auto-connect mode
235
- if (!(readyState === WalletReadyState.Installed || readyState === WalletReadyState.Loadable)) {
236
- // Clear the selected wallet
237
- setName(null);
238
-
239
- if (typeof window !== 'undefined') {
240
- window.open(adapter.url, '_blank');
241
- }
242
-
243
- handleError(new WalletNotReadyError());
232
+ if (
233
+ isConnecting.current ||
234
+ connected ||
235
+ !autoConnect ||
236
+ !adapter ||
237
+ !(readyState === WalletReadyState.Installed || readyState === WalletReadyState.Loadable)
238
+ )
244
239
  return;
245
- }
246
240
 
247
241
  (async function () {
248
242
  isConnecting.current = true;
@@ -274,7 +268,7 @@ export const WalletProvider: FC<WalletProviderProps> = ({
274
268
  window.open(adapter.url, '_blank');
275
269
  }
276
270
 
277
- throw handleError(new WalletNotReadyError());
271
+ throw handleError(new WalletNotReadyError('Wallet Not Ready'));
278
272
  }
279
273
  isConnecting.current = true;
280
274
  setConnecting(true);