forked from hippospace/aptos-wallet-adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PontemWallet.ts
333 lines (291 loc) · 9.48 KB
/
PontemWallet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { MaybeHexString, Types } from 'aptos';
import {
WalletAccountChangeError,
WalletDisconnectionError,
WalletGetNetworkError,
WalletNetworkChangeError,
WalletNotConnectedError,
WalletNotReadyError,
WalletSignAndSubmitMessageError,
WalletSignMessageError,
WalletSignTransactionError
} from '../WalletProviders/errors';
import {
AccountKeys,
BaseWalletAdapter,
NetworkInfo,
scopePollingDetectionStrategy,
SignMessagePayload,
SignMessageResponse,
WalletAdapterNetwork,
WalletName,
WalletReadyState
} from './BaseAdapter';
interface ConnectPontemAccount {
address: MaybeHexString;
method: string;
publicKey: MaybeHexString;
status: number;
}
interface PontemAccount {
address: MaybeHexString;
publicKey?: MaybeHexString;
authKey?: MaybeHexString;
isConnected: boolean;
}
interface IPontemWallet {
connect: () => Promise<ConnectPontemAccount>;
account(): Promise<MaybeHexString>;
publicKey(): Promise<MaybeHexString>;
generateTransaction(sender: MaybeHexString, payload: any): Promise<any>;
signAndSubmit(
transaction: Types.TransactionPayload,
options?: any
): Promise<{
success: boolean;
result: {
hash: Types.HexEncodedBytes;
};
}>;
isConnected(): Promise<boolean>;
signTransaction(transaction: Types.TransactionPayload, options?: any): Promise<Uint8Array>;
signMessage(message: SignMessagePayload): Promise<{
success: boolean;
result: SignMessageResponse;
}>;
disconnect(): Promise<void>;
network(): Promise<NetworkInfo>;
onAccountChange(listener: (address: string | undefined) => void): Promise<void>;
onNetworkChange(listener: (network: NetworkInfo) => void): Promise<void>;
}
interface PontemWindow extends Window {
pontem?: IPontemWallet;
}
declare const window: PontemWindow;
export const PontemWalletName = 'Pontem' as WalletName<'Pontem'>;
export interface PontemWalletAdapterConfig {
provider?: IPontemWallet;
// network?: WalletAdapterNetwork;
timeout?: number;
}
export class PontemWalletAdapter extends BaseWalletAdapter {
name = PontemWalletName;
url = 'https://chrome.google.com/webstore/detail/pontem-wallet/phkbamefinggmakgklpkljjmgibohnba';
icon =
'https://www.gitbook.com/cdn-cgi/image/width=20,height=20,fit=contain,dpr=2,format=auto/https%3A%2F%2F736486047-files.gitbook.io%2F~%2Ffiles%2Fv0%2Fb%2Fgitbook-legacy-files%2Fo%2Fspaces%252F-MVVJKmKQGx983dZy_jr%252Favatar-1619180126965.png%3Fgeneration%3D1619180127194239%26alt%3Dmedia';
protected _provider: IPontemWallet | undefined;
protected _network: WalletAdapterNetwork;
protected _chainId: string;
protected _api: string;
protected _timeout: number;
protected _readyState: WalletReadyState =
typeof window === 'undefined' || typeof document === 'undefined'
? WalletReadyState.Unsupported
: WalletReadyState.NotDetected;
protected _connecting: boolean;
protected _wallet: PontemAccount | null;
constructor({
// provider,
// network = WalletAdapterNetwork.Testnet,
timeout = 10000
}: PontemWalletAdapterConfig = {}) {
super();
this._provider = typeof window !== 'undefined' ? window.pontem : undefined;
this._network = undefined;
this._timeout = timeout;
this._connecting = false;
this._wallet = null;
if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
scopePollingDetectionStrategy(() => {
if (window.pontem) {
this._readyState = WalletReadyState.Installed;
this.emit('readyStateChange', this._readyState);
return true;
}
return false;
});
}
}
get publicAccount(): AccountKeys {
return {
publicKey: this._wallet?.publicKey || null,
address: this._wallet?.address || null,
authKey: this._wallet?.authKey || null
};
}
get network(): NetworkInfo {
return {
name: this._network,
api: this._api,
chainId: this._chainId
};
}
get connecting(): boolean {
return this._connecting;
}
get connected(): boolean {
return !!this._wallet?.isConnected;
}
get readyState(): WalletReadyState {
return this._readyState;
}
async connect(): Promise<void> {
try {
if (this.connected || this.connecting) return;
if (
!(
this._readyState === WalletReadyState.Loadable ||
this._readyState === WalletReadyState.Installed
)
)
throw new WalletNotReadyError();
this._connecting = true;
const provider = this._provider || window.pontem;
const isConnected = await provider?.isConnected();
if (isConnected) {
await provider?.disconnect();
}
const response = await provider?.connect();
if (!response) {
throw new WalletNotConnectedError('No connect response');
}
const walletAccount = response.address;
const publicKey = response.publicKey;
if (walletAccount) {
this._wallet = {
address: walletAccount,
publicKey,
isConnected: true
};
try {
const networkInfo = await provider?.network();
this._network = networkInfo.name;
this._chainId = networkInfo.chainId;
this._api = networkInfo.api;
} catch (error: any) {
const errMsg = error.message;
this.emit('error', new WalletGetNetworkError(errMsg));
throw error;
}
}
this.emit('connect', this._wallet?.address || '');
} catch (error: any) {
this.emit('error', new Error('User has rejected the connection'));
throw error;
} finally {
this._connecting = false;
}
}
async disconnect(): Promise<void> {
const wallet = this._wallet;
const provider = this._provider || window.pontem;
if (wallet) {
this._wallet = null;
try {
await provider?.disconnect();
} catch (error: any) {
this.emit('error', new WalletDisconnectionError(error?.message, error));
}
}
this.emit('disconnect');
}
async signTransaction(
transactionPyld: Types.TransactionPayload,
options?: any
): Promise<Uint8Array> {
try {
const wallet = this._wallet;
const provider = this._provider || window.pontem;
if (!wallet || !provider) throw new WalletNotConnectedError();
const response = await provider?.signTransaction(transactionPyld, options);
return response as Uint8Array;
} catch (error: any) {
this.emit('error', new WalletSignTransactionError(error));
throw error;
}
}
async signAndSubmitTransaction(
transactionPyld: Types.TransactionPayload,
options?: any
): Promise<{ hash: Types.HexEncodedBytes }> {
try {
const wallet = this._wallet;
const provider = this._provider || window.pontem;
if (!wallet || !provider) throw new WalletNotConnectedError();
const response = await provider?.signAndSubmit(transactionPyld, options);
if (!response || !response.success) {
throw new Error('No response');
}
return { hash: response.result.hash };
} catch (error: any) {
this.emit('error', new WalletSignAndSubmitMessageError(error.message));
throw error;
}
}
async signMessage(messagePayload: SignMessagePayload): Promise<SignMessageResponse> {
try {
const wallet = this._wallet;
const provider = this._provider || window.pontem;
if (!wallet || !provider) throw new WalletNotConnectedError();
const response = await provider?.signMessage(messagePayload);
if (response.success) {
return response.result;
} else {
throw new Error('Sign Message failed');
}
} catch (error: any) {
const errMsg = error.message;
this.emit('error', new WalletSignMessageError(errMsg));
throw error;
}
}
async onAccountChange(): Promise<void> {
try {
const wallet = this._wallet;
const provider = this._provider || window.pontem;
if (!wallet || !provider) throw new WalletNotConnectedError();
const handleAccountChange = async (newAccount: string | undefined) => {
// disconnect wallet if newAccount is undefined
if (newAccount) {
const newPublicKey = await provider?.publicKey();
this._wallet = {
...this._wallet,
address: newAccount,
publicKey: newPublicKey
};
} else {
const response = await provider?.connect();
this._wallet = {
...this._wallet,
address: response?.address || this._wallet?.address,
publicKey: response?.publicKey || this._wallet?.publicKey
};
}
this.emit('accountChange', newAccount);
};
await provider?.onAccountChange(handleAccountChange);
} catch (error: any) {
const errMsg = error.message;
this.emit('error', new WalletAccountChangeError(errMsg));
throw error;
}
}
async onNetworkChange(): Promise<void> {
try {
const wallet = this._wallet;
const provider = this._provider || window.pontem;
if (!wallet || !provider) throw new WalletNotConnectedError();
const handleNetworkChange = (network: NetworkInfo) => {
this._network = network.name;
this._api = network.api;
this._chainId = network.chainId;
this.emit('networkChange', this._network);
};
await provider?.onNetworkChange(handleNetworkChange);
} catch (error: any) {
const errMsg = error.message;
this.emit('error', new WalletNetworkChangeError(errMsg));
throw error;
}
}
}