@manahippo/aptos-wallet-adapter 0.4.13 → 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.
- package/README.md +10 -4
- package/dist/WalletAdapters/BaseAdapter.d.ts +2 -1
- package/dist/WalletAdapters/BaseAdapter.d.ts.map +1 -1
- package/dist/WalletAdapters/BaseAdapter.js.map +1 -1
- package/dist/WalletAdapters/BloctoWallet.d.ts +43 -0
- package/dist/WalletAdapters/BloctoWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/BloctoWallet.js +240 -0
- package/dist/WalletAdapters/BloctoWallet.js.map +1 -0
- package/dist/WalletAdapters/ONTOWallet.d.ts +64 -0
- package/dist/WalletAdapters/ONTOWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/ONTOWallet.js +244 -0
- package/dist/WalletAdapters/ONTOWallet.js.map +1 -0
- package/dist/WalletAdapters/index.d.ts +2 -0
- package/dist/WalletAdapters/index.d.ts.map +1 -1
- package/dist/WalletAdapters/index.js +2 -0
- package/dist/WalletAdapters/index.js.map +1 -1
- package/package.json +2 -1
- package/src/WalletAdapters/BaseAdapter.ts +2 -1
- package/src/WalletAdapters/BloctoWallet.ts +264 -0
- package/src/WalletAdapters/ONTOWallet.ts +306 -0
- package/src/WalletAdapters/index.ts +2 -0
@@ -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
|
+
}
|