@manahippo/aptos-wallet-adapter 0.4.12 → 1.0.0

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,264 @@
1
+ import { Types } from 'aptos';
2
+ import BloctoSDK, { AptosProviderInterface as IBloctoAptos } from '@blocto/sdk';
3
+ import {
4
+ WalletAccountChangeError,
5
+ WalletDisconnectionError,
6
+ WalletNetworkChangeError,
7
+ WalletNotConnectedError,
8
+ WalletNotReadyError,
9
+ WalletSignAndSubmitMessageError,
10
+ WalletSignMessageError,
11
+ WalletSignTransactionError
12
+ } from '../WalletProviders/errors';
13
+ import {
14
+ AccountKeys,
15
+ BaseWalletAdapter,
16
+ scopePollingDetectionStrategy,
17
+ WalletName,
18
+ WalletReadyState,
19
+ SignMessagePayload,
20
+ SignMessageResponse,
21
+ NetworkInfo,
22
+ WalletAdapterNetwork
23
+ } from './BaseAdapter';
24
+
25
+ export const BloctoWalletName = 'Blocto' as WalletName<'Blocto'>;
26
+
27
+ export interface BloctoWalletAdapterConfig {
28
+ provider?: IBloctoAptos;
29
+ network: Exclude<WalletAdapterNetwork, WalletAdapterNetwork.Devnet>;
30
+ timeout?: number;
31
+ bloctoAppId?: string;
32
+ }
33
+
34
+ export const APTOS_NETWORK_CHAIN_ID_MAPPING = {
35
+ // MAINNET
36
+ [WalletAdapterNetwork.Mainnet]: 1,
37
+ // TESTNET
38
+ [WalletAdapterNetwork.Testnet]: 2
39
+ };
40
+
41
+ export class BloctoWalletAdapter extends BaseWalletAdapter {
42
+ name = BloctoWalletName;
43
+
44
+ url = 'https://portto.com/download';
45
+
46
+ icon = 'https://raw.githubusercontent.com/hippospace/aptos-wallet-adapter/main/logos/blocto.svg';
47
+
48
+ protected _provider: IBloctoAptos | undefined;
49
+
50
+ protected _network: Exclude<WalletAdapterNetwork, WalletAdapterNetwork.Devnet>;
51
+
52
+ protected _chainId: string;
53
+
54
+ protected _api: string;
55
+
56
+ protected _timeout: number;
57
+
58
+ protected _readyState: WalletReadyState =
59
+ typeof window === 'undefined' || typeof document === 'undefined'
60
+ ? WalletReadyState.Unsupported
61
+ : WalletReadyState.NotDetected;
62
+
63
+ protected _connecting: boolean;
64
+
65
+ protected _wallet: any | null;
66
+
67
+ constructor(
68
+ { network, timeout = 10000, bloctoAppId = '' }: BloctoWalletAdapterConfig = {
69
+ network: WalletAdapterNetwork.Testnet
70
+ }
71
+ ) {
72
+ super();
73
+
74
+ const sdk = new BloctoSDK({
75
+ aptos: {
76
+ chainId: APTOS_NETWORK_CHAIN_ID_MAPPING[network]
77
+ },
78
+ appId: bloctoAppId
79
+ });
80
+
81
+ this._provider = sdk.aptos;
82
+ this._network = network;
83
+ this._timeout = timeout;
84
+ this._connecting = false;
85
+ this._wallet = null;
86
+
87
+ if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
88
+ scopePollingDetectionStrategy(() => {
89
+ if (window) {
90
+ this._readyState = WalletReadyState.Installed;
91
+ return true;
92
+ }
93
+ return false;
94
+ });
95
+ }
96
+ }
97
+
98
+ get publicAccount(): AccountKeys {
99
+ return {
100
+ publicKey: this._wallet?.publicKey || null,
101
+ address: this._wallet?.address || null,
102
+ authKey: this._wallet?.authKey || null,
103
+ minKeysRequired: this._wallet?.minKeysRequired
104
+ };
105
+ }
106
+
107
+ get network(): NetworkInfo {
108
+ return {
109
+ name: this._network,
110
+ api: this._api,
111
+ chainId: this._chainId
112
+ };
113
+ }
114
+
115
+ get connecting(): boolean {
116
+ return this._connecting;
117
+ }
118
+
119
+ get connected(): boolean {
120
+ return !!this._wallet?.isConnected;
121
+ }
122
+
123
+ get readyState(): WalletReadyState {
124
+ return this._readyState;
125
+ }
126
+
127
+ async connect(): Promise<void> {
128
+ try {
129
+ if (this.connected || this.connecting) return;
130
+ if (
131
+ !(
132
+ this._readyState === WalletReadyState.Loadable ||
133
+ this._readyState === WalletReadyState.Installed
134
+ )
135
+ )
136
+ throw new WalletNotReadyError();
137
+
138
+ this._connecting = true;
139
+ const provider = this._provider;
140
+ const isConnected = await provider?.isConnected();
141
+ if (isConnected) {
142
+ await provider?.disconnect();
143
+ }
144
+
145
+ const { publicKey, ...rest } = await provider?.connect();
146
+ this._wallet = {
147
+ ...rest,
148
+ publicKey,
149
+ isConnected: true
150
+ };
151
+
152
+ const { api, chainId } = await provider.network();
153
+ this._api = api;
154
+ this._chainId = chainId;
155
+
156
+ this.emit('connect', this._wallet);
157
+ } catch (error: any) {
158
+ this.emit('error', error);
159
+ throw error;
160
+ } finally {
161
+ this._connecting = false;
162
+ }
163
+ }
164
+
165
+ async disconnect(): Promise<void> {
166
+ const wallet = this._wallet;
167
+ const provider = this._provider;
168
+ if (wallet) {
169
+ this._wallet = null;
170
+ try {
171
+ await provider?.disconnect();
172
+ } catch (error: any) {
173
+ this.emit('error', new WalletDisconnectionError(error?.message, error));
174
+ }
175
+ }
176
+
177
+ this.emit('disconnect');
178
+ }
179
+
180
+ async signTransaction(transaction: Types.TransactionPayload): Promise<Uint8Array> {
181
+ try {
182
+ try {
183
+ const provider = this._provider;
184
+ const response = await provider?.signTransaction(transaction as Types.EntryFunctionPayload);
185
+ if (response) {
186
+ return new Uint8Array([]);
187
+ } else {
188
+ throw new Error('Transaction failed');
189
+ }
190
+ } catch (error: any) {
191
+ throw new WalletSignTransactionError(error?.message, error);
192
+ }
193
+ } catch (error: any) {
194
+ this.emit('error', error);
195
+ throw error;
196
+ }
197
+ }
198
+
199
+ async signAndSubmitTransaction(
200
+ transaction: Types.TransactionPayload
201
+ ): Promise<{ hash: Types.HexEncodedBytes }> {
202
+ try {
203
+ try {
204
+ const provider = this._provider;
205
+ const response = await provider?.signAndSubmitTransaction(
206
+ transaction as Types.EntryFunctionPayload
207
+ );
208
+ if (response) {
209
+ return { hash: response.hash };
210
+ } else {
211
+ throw new Error('Transaction failed');
212
+ }
213
+ } catch (error: any) {
214
+ throw new WalletSignAndSubmitMessageError(error.message || error);
215
+ }
216
+ } catch (error: any) {
217
+ this.emit('error', error);
218
+ throw error;
219
+ }
220
+ }
221
+
222
+ async signMessage(message: SignMessagePayload): Promise<SignMessageResponse> {
223
+ try {
224
+ const provider = this._provider;
225
+ const response = await provider?.signMessage(message);
226
+
227
+ if (response) {
228
+ return response;
229
+ } else {
230
+ throw new Error('Sign Message failed');
231
+ }
232
+ } catch (error: any) {
233
+ const errMsg = error.message;
234
+ this.emit('error', new WalletSignMessageError(errMsg));
235
+ throw error;
236
+ }
237
+ }
238
+
239
+ async onAccountChange(): Promise<void> {
240
+ try {
241
+ const wallet = this._wallet;
242
+ const provider = this._provider;
243
+ if (!wallet || !provider) throw new WalletNotConnectedError();
244
+ //To be implemented
245
+ } catch (error: any) {
246
+ const errMsg = error.message;
247
+ this.emit('error', new WalletAccountChangeError(errMsg));
248
+ throw error;
249
+ }
250
+ }
251
+
252
+ async onNetworkChange(): Promise<void> {
253
+ try {
254
+ const wallet = this._wallet;
255
+ const provider = this._provider;
256
+ if (!wallet || !provider) throw new WalletNotConnectedError();
257
+ //To be implemented
258
+ } catch (error: any) {
259
+ const errMsg = error.message;
260
+ this.emit('error', new WalletNetworkChangeError(errMsg));
261
+ throw error;
262
+ }
263
+ }
264
+ }
@@ -0,0 +1,306 @@
1
+ import { Types } from 'aptos';
2
+ import {
3
+ WalletAccountChangeError,
4
+ WalletDisconnectionError,
5
+ WalletGetNetworkError,
6
+ WalletNetworkChangeError,
7
+ WalletNotConnectedError,
8
+ WalletNotReadyError,
9
+ WalletSignAndSubmitMessageError,
10
+ WalletSignMessageError,
11
+ WalletSignTransactionError
12
+ } from '../WalletProviders/errors';
13
+ import {
14
+ AccountKeys,
15
+ BaseWalletAdapter,
16
+ NetworkInfo,
17
+ scopePollingDetectionStrategy,
18
+ SignMessagePayload,
19
+ SignMessageResponse,
20
+ WalletAdapterNetwork,
21
+ WalletName,
22
+ WalletReadyState
23
+ } from './BaseAdapter';
24
+
25
+ interface IApotsErrorResult {
26
+ code: number;
27
+ name: string;
28
+ message: string;
29
+ }
30
+
31
+ type AddressInfo = { address: string; publicKey: string; authKey?: string };
32
+
33
+ interface IONTOWallet {
34
+ connect: () => Promise<AddressInfo>;
35
+ account: () => Promise<AddressInfo>;
36
+ isConnected: () => Promise<boolean>;
37
+ signAndSubmitTransaction(
38
+ transaction: any,
39
+ options?: any
40
+ ): Promise<{ hash: Types.HexEncodedBytes } | IApotsErrorResult>;
41
+ signTransaction(transaction: any, options?: any): Promise<Uint8Array | IApotsErrorResult>;
42
+ signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
43
+ disconnect(): Promise<void>;
44
+ network(): Promise<WalletAdapterNetwork>;
45
+ requestId: Promise<number>;
46
+ onAccountChange: (listener: (newAddress: AddressInfo) => void) => void;
47
+ onNetworkChange: (listener: (network: { networkName: string }) => void) => void;
48
+ }
49
+
50
+ interface ONTOWindow extends Window {
51
+ onto?: {
52
+ aptos: IONTOWallet;
53
+ };
54
+ }
55
+
56
+ declare const window: ONTOWindow;
57
+
58
+ export const ONTOWalletName = 'ONTO' as WalletName<'ONTO'>;
59
+
60
+ export interface ONTOWalletAdapterConfig {
61
+ provider?: IONTOWallet;
62
+ // network?: WalletAdapterNetwork;
63
+ timeout?: number;
64
+ }
65
+
66
+ export class ONTOWalletAdapter extends BaseWalletAdapter {
67
+ name = ONTOWalletName;
68
+
69
+ url =
70
+ 'https://onto.app';
71
+
72
+ icon =
73
+ 'https://app.ont.io/onto/ONTO_logo.png';
74
+
75
+ protected _provider: IONTOWallet | undefined;
76
+
77
+ protected _network: WalletAdapterNetwork;
78
+
79
+ protected _chainId: string;
80
+
81
+ protected _api: string;
82
+
83
+ protected _timeout: number;
84
+
85
+ protected _readyState: WalletReadyState =
86
+ typeof window === 'undefined' || typeof document === 'undefined'
87
+ ? WalletReadyState.Unsupported
88
+ : WalletReadyState.NotDetected;
89
+
90
+ protected _connecting: boolean;
91
+
92
+ protected _wallet: any | null;
93
+
94
+ constructor({
95
+ // provider,
96
+ // network = WalletAdapterNetwork.Testnet,
97
+ timeout = 10000
98
+ }: ONTOWalletAdapterConfig = {}) {
99
+ super();
100
+
101
+ this._provider = typeof window !== 'undefined' ? window.onto?.aptos : undefined;
102
+ this._network = undefined;
103
+ this._timeout = timeout;
104
+ this._connecting = false;
105
+ this._wallet = null;
106
+
107
+ if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
108
+ scopePollingDetectionStrategy(() => {
109
+ if (window.onto?.aptos) {
110
+ this._readyState = WalletReadyState.Installed;
111
+ this.emit('readyStateChange', this._readyState);
112
+ return true;
113
+ }
114
+ return false;
115
+ });
116
+ }
117
+ }
118
+
119
+ get publicAccount(): AccountKeys {
120
+ return {
121
+ publicKey: this._wallet?.publicKey || null,
122
+ address: this._wallet?.address || null,
123
+ authKey: this._wallet?.authKey || null
124
+ };
125
+ }
126
+
127
+ get network(): NetworkInfo {
128
+ return {
129
+ name: this._network,
130
+ api: this._api,
131
+ chainId: this._chainId
132
+ };
133
+ }
134
+
135
+ get connecting(): boolean {
136
+ return this._connecting;
137
+ }
138
+
139
+ get connected(): boolean {
140
+ return !!this._wallet?.isConnected;
141
+ }
142
+
143
+ get readyState(): WalletReadyState {
144
+ return this._readyState;
145
+ }
146
+
147
+ async connect(): Promise<void> {
148
+ try {
149
+ if (this.connected || this.connecting) return;
150
+ if (
151
+ !(
152
+ this._readyState === WalletReadyState.Loadable ||
153
+ this._readyState === WalletReadyState.Installed
154
+ )
155
+ )
156
+ throw new WalletNotReadyError();
157
+ this._connecting = true;
158
+
159
+ const provider = this._provider || window.onto?.aptos;
160
+ const response = await provider?.connect();
161
+ this._wallet = {
162
+ address: response?.address,
163
+ publicKey: response?.publicKey,
164
+ isConnected: true
165
+ };
166
+
167
+ try {
168
+ const name = await provider?.network();
169
+ const chainId = null;
170
+ const api = null;
171
+
172
+ this._network = name;
173
+ this._chainId = chainId;
174
+ this._api = api;
175
+ } catch (error: any) {
176
+ const errMsg = error.message;
177
+ this.emit('error', new WalletGetNetworkError(errMsg));
178
+ throw error;
179
+ }
180
+
181
+ this.emit('connect', this._wallet.publicKey);
182
+ } catch (error: any) {
183
+ this.emit('error', error);
184
+ throw error;
185
+ } finally {
186
+ this._connecting = false;
187
+ }
188
+ }
189
+
190
+ async disconnect(): Promise<void> {
191
+ const wallet = this._wallet;
192
+ const provider = this._provider || window.onto?.aptos;
193
+ if (wallet) {
194
+ this._wallet = null;
195
+
196
+ try {
197
+ await provider?.disconnect();
198
+ } catch (error: any) {
199
+ this.emit('error', new WalletDisconnectionError(error?.message, error));
200
+ }
201
+ }
202
+
203
+ this.emit('disconnect');
204
+ }
205
+
206
+ async signTransaction(transaction: Types.TransactionPayload, options?: any): Promise<Uint8Array> {
207
+ try {
208
+ const wallet = this._wallet;
209
+ const provider = this._provider || window.onto?.aptos;
210
+ if (!wallet || !provider) throw new WalletNotConnectedError();
211
+
212
+ const response = await provider.signTransaction(transaction, options);
213
+ if ((response as IApotsErrorResult).code) {
214
+ throw new Error((response as IApotsErrorResult).message);
215
+ }
216
+ return response as Uint8Array;
217
+ } catch (error: any) {
218
+ const errMsg = error.message;
219
+ this.emit('error', new WalletSignTransactionError(errMsg));
220
+ throw error;
221
+ }
222
+ }
223
+
224
+ async signAndSubmitTransaction(
225
+ transaction: Types.TransactionPayload,
226
+ options?: any
227
+ ): Promise<{ hash: Types.HexEncodedBytes }> {
228
+ try {
229
+ const wallet = this._wallet;
230
+ const provider = this._provider || window.onto?.aptos;
231
+ if (!wallet || !provider) throw new WalletNotConnectedError();
232
+
233
+ const response = await provider.signAndSubmitTransaction(transaction, options);
234
+ if ((response as IApotsErrorResult).code) {
235
+ throw new Error((response as IApotsErrorResult).message);
236
+ }
237
+ return response as { hash: Types.HexEncodedBytes };
238
+ } catch (error: any) {
239
+ const errMsg = error.message;
240
+ this.emit('error', new WalletSignAndSubmitMessageError(errMsg));
241
+ throw error;
242
+ }
243
+ }
244
+
245
+ async signMessage(msgPayload: SignMessagePayload): Promise<SignMessageResponse> {
246
+ try {
247
+ const wallet = this._wallet;
248
+ const provider = this._provider || window.onto?.aptos;
249
+ if (!wallet || !provider) throw new WalletNotConnectedError();
250
+ if (typeof msgPayload !== 'object' || !msgPayload.nonce) {
251
+ throw new WalletSignMessageError('Invalid signMessage Payload');
252
+ }
253
+ const response = await provider?.signMessage(msgPayload);
254
+ if (response) {
255
+ return response;
256
+ } else {
257
+ throw new Error('Sign Message failed');
258
+ }
259
+ } catch (error: any) {
260
+ const errMsg = error.message;
261
+ this.emit('error', new WalletSignMessageError(errMsg));
262
+ throw error;
263
+ }
264
+ }
265
+
266
+ async onAccountChange(): Promise<void> {
267
+ try {
268
+ const wallet = this._wallet;
269
+ const provider = this._provider || window.onto?.aptos;
270
+ if (!wallet || !provider) throw new WalletNotConnectedError();
271
+ const handleAccountChange = async (newAccount: AddressInfo) => {
272
+ console.log('account Changed >>>', newAccount);
273
+ this._wallet = {
274
+ ...this._wallet,
275
+ publicKey: newAccount.publicKey || this._wallet?.publicKey,
276
+ authKey: newAccount.authKey || this._wallet?.authKey,
277
+ address: newAccount.address || this._wallet?.address
278
+ };
279
+ this.emit('accountChange', newAccount.publicKey);
280
+ };
281
+ await provider?.onAccountChange(handleAccountChange);
282
+ } catch (error: any) {
283
+ const errMsg = error.message;
284
+ this.emit('error', new WalletAccountChangeError(errMsg));
285
+ throw error;
286
+ }
287
+ }
288
+
289
+ async onNetworkChange(): Promise<void> {
290
+ try {
291
+ const wallet = this._wallet;
292
+ const provider = this._provider || window.onto?.aptos;
293
+ if (!wallet || !provider) throw new WalletNotConnectedError();
294
+ const handleNetworkChange = async (newNetwork: { networkName: WalletAdapterNetwork }) => {
295
+ console.log('network Changed >>>', newNetwork);
296
+ this._network = newNetwork.networkName;
297
+ this.emit('networkChange', this._network);
298
+ };
299
+ await provider?.onNetworkChange(handleNetworkChange);
300
+ } catch (error: any) {
301
+ const errMsg = error.message;
302
+ this.emit('error', new WalletNetworkChangeError(errMsg));
303
+ throw error;
304
+ }
305
+ }
306
+ }
@@ -13,3 +13,5 @@ export * from './FletchWallet';
13
13
  export * from './AptosSnap';
14
14
  export * from './BitkeepWallet';
15
15
  export * from './TokenPocketWallet';
16
+ export * from './ONTOWallet';
17
+ export * from './BloctoWallet';