@manahippo/aptos-wallet-adapter 0.1.1
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/.eslintignore +6 -0
- package/.eslintrc.json +52 -0
- package/.prettierrc.json +9 -0
- package/README.md +1 -0
- package/package.json +25 -0
- package/src/WalletAdatpers/AptosWallet.ts +194 -0
- package/src/WalletAdatpers/BaseAdapter.ts +148 -0
- package/src/WalletAdatpers/HippoExtensionWallet.ts +194 -0
- package/src/WalletAdatpers/HippoWallet.ts +183 -0
- package/src/WalletAdatpers/MartianWallet.ts +237 -0
- package/src/WalletAdatpers/MultiMaskWallet.ts +240 -0
- package/src/WalletAdatpers/index.ts +6 -0
- package/src/WalletProviders/WalletProvider.tsx +262 -0
- package/src/WalletProviders/errors.ts +81 -0
- package/src/WalletProviders/index.ts +3 -0
- package/src/WalletProviders/useWallet.ts +54 -0
- package/src/config/aptosConstants.ts +1 -0
- package/src/hooks/useLocalStorage.ts +40 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +104 -0
@@ -0,0 +1,183 @@
|
|
1
|
+
import { MaybeHexString } from 'aptos';
|
2
|
+
import {
|
3
|
+
PendingTransaction,
|
4
|
+
SubmitTransactionRequest,
|
5
|
+
TransactionPayload
|
6
|
+
} from 'aptos/dist/api/data-contracts';
|
7
|
+
import { WEBWALLET_URL } from '../config/aptosConstants';
|
8
|
+
import {
|
9
|
+
WalletNotConnectedError,
|
10
|
+
WalletNotReadyError,
|
11
|
+
WalletSignAndSubmitMessageError
|
12
|
+
} from '../WalletProviders/errors';
|
13
|
+
import { BaseWalletAdapter, PublicKey, WalletName, WalletReadyState } from './BaseAdapter';
|
14
|
+
|
15
|
+
export const HippoWalletName = 'Hippo Wallet' as WalletName<'Hippo Wallet'>;
|
16
|
+
|
17
|
+
export interface HippoWalletAdapterConfig {
|
18
|
+
provider?: string;
|
19
|
+
// network?: WalletAdapterNetwork;
|
20
|
+
timeout?: number;
|
21
|
+
}
|
22
|
+
|
23
|
+
export class HippoWalletAdapter extends BaseWalletAdapter {
|
24
|
+
name = HippoWalletName;
|
25
|
+
|
26
|
+
url = 'https://hippo-wallet-test.web.app';
|
27
|
+
|
28
|
+
icon = 'https://ui-test1-22e7c.web.app/static/media/hippo_logo.ecded6bf411652de9b7f.png';
|
29
|
+
|
30
|
+
protected _provider: string | undefined;
|
31
|
+
|
32
|
+
// protected _network: WalletAdapterNetwork;
|
33
|
+
protected _timeout: number;
|
34
|
+
|
35
|
+
protected _readyState: WalletReadyState = WalletReadyState.Installed;
|
36
|
+
|
37
|
+
protected _connecting: boolean;
|
38
|
+
|
39
|
+
protected _wallet: any | null;
|
40
|
+
|
41
|
+
constructor({
|
42
|
+
// provider = WEBWALLET_URL,
|
43
|
+
// network = WalletAdapterNetwork.Mainnet,
|
44
|
+
timeout = 10000
|
45
|
+
}: HippoWalletAdapterConfig = {}) {
|
46
|
+
super();
|
47
|
+
|
48
|
+
this._provider = WEBWALLET_URL || 'https://hippo-wallet-test.web.app';
|
49
|
+
// this._network = network;
|
50
|
+
this._timeout = timeout;
|
51
|
+
this._connecting = false;
|
52
|
+
this._wallet = null;
|
53
|
+
this._readyState = WalletReadyState.Installed;
|
54
|
+
}
|
55
|
+
|
56
|
+
get publicKey(): PublicKey | null {
|
57
|
+
return this._wallet?.publicKey || null;
|
58
|
+
}
|
59
|
+
|
60
|
+
get connecting(): boolean {
|
61
|
+
return this._connecting;
|
62
|
+
}
|
63
|
+
|
64
|
+
get connected(): boolean {
|
65
|
+
return !!this._wallet?.connected;
|
66
|
+
}
|
67
|
+
|
68
|
+
get readyState(): WalletReadyState {
|
69
|
+
return this._readyState;
|
70
|
+
}
|
71
|
+
|
72
|
+
handleMessage = (
|
73
|
+
e: MessageEvent<{
|
74
|
+
id: number;
|
75
|
+
method: string;
|
76
|
+
address?: {
|
77
|
+
hexString: MaybeHexString;
|
78
|
+
};
|
79
|
+
error?: string;
|
80
|
+
}>
|
81
|
+
): void => {
|
82
|
+
if (e.origin === this._provider) {
|
83
|
+
if (e.data.method === 'account') {
|
84
|
+
this._wallet = {
|
85
|
+
connected: true,
|
86
|
+
publicKey: e.data.address?.hexString || null
|
87
|
+
};
|
88
|
+
this.emit('connect', this._wallet.publicKey);
|
89
|
+
} else if (e.data.method === 'success') {
|
90
|
+
this.emit('success', 'Transaction Success');
|
91
|
+
} else if (e.data.method === 'fail') {
|
92
|
+
this.emit('error', new WalletSignAndSubmitMessageError(e.data.error));
|
93
|
+
} else if (e.data.method === 'disconnected') {
|
94
|
+
this.disconnect();
|
95
|
+
}
|
96
|
+
}
|
97
|
+
};
|
98
|
+
|
99
|
+
async connect(): Promise<void> {
|
100
|
+
try {
|
101
|
+
if (this.connected || this.connecting) return;
|
102
|
+
if (
|
103
|
+
!(
|
104
|
+
this._readyState === WalletReadyState.Loadable ||
|
105
|
+
this._readyState === WalletReadyState.Installed
|
106
|
+
)
|
107
|
+
)
|
108
|
+
throw new WalletNotReadyError();
|
109
|
+
|
110
|
+
this._connecting = true;
|
111
|
+
|
112
|
+
window.addEventListener('message', this.handleMessage);
|
113
|
+
window.addEventListener('beforeunload', this._beforeUnload);
|
114
|
+
} catch (error: any) {
|
115
|
+
this.emit('error', error);
|
116
|
+
throw error;
|
117
|
+
} finally {
|
118
|
+
this._connecting = false;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
async disconnect(): Promise<void> {
|
123
|
+
window.removeEventListener('message', this.handleMessage);
|
124
|
+
window.removeEventListener('beforeunload', this._beforeUnload);
|
125
|
+
this.emit('disconnect');
|
126
|
+
}
|
127
|
+
|
128
|
+
async signTransaction(transaction: TransactionPayload): Promise<SubmitTransactionRequest> {
|
129
|
+
try {
|
130
|
+
const request = new URLSearchParams({
|
131
|
+
request: JSON.stringify({
|
132
|
+
method: 'signTransaction',
|
133
|
+
payload: transaction
|
134
|
+
}),
|
135
|
+
origin: window.location.origin
|
136
|
+
}).toString();
|
137
|
+
const popup = window.open(
|
138
|
+
`${WEBWALLET_URL}?${request}`,
|
139
|
+
'Transaction Confirmation',
|
140
|
+
'scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=440,height=700'
|
141
|
+
);
|
142
|
+
if (!popup) throw new WalletNotConnectedError();
|
143
|
+
const promise = await new Promise((resolve, reject) => {
|
144
|
+
this.once('success', resolve);
|
145
|
+
this.once('error', reject);
|
146
|
+
});
|
147
|
+
return promise as SubmitTransactionRequest;
|
148
|
+
} catch (error: any) {
|
149
|
+
this.emit('error', error);
|
150
|
+
throw error;
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
async signAndSubmitTransaction(transaction: TransactionPayload): Promise<PendingTransaction> {
|
155
|
+
try {
|
156
|
+
const request = new URLSearchParams({
|
157
|
+
request: JSON.stringify({
|
158
|
+
method: 'signAndSubmit',
|
159
|
+
payload: transaction
|
160
|
+
}),
|
161
|
+
origin: window.location.origin
|
162
|
+
}).toString();
|
163
|
+
const popup = window.open(
|
164
|
+
`${WEBWALLET_URL}?${request}`,
|
165
|
+
'Transaction Confirmation',
|
166
|
+
'scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=440,height=700'
|
167
|
+
);
|
168
|
+
if (!popup) throw new WalletNotConnectedError();
|
169
|
+
const promise = await new Promise((resolve, reject) => {
|
170
|
+
this.once('success', resolve);
|
171
|
+
this.once('error', reject);
|
172
|
+
});
|
173
|
+
return promise as PendingTransaction;
|
174
|
+
} catch (error: any) {
|
175
|
+
this.emit('error', error);
|
176
|
+
throw error;
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
private _beforeUnload = (): void => {
|
181
|
+
void this.disconnect();
|
182
|
+
};
|
183
|
+
}
|
@@ -0,0 +1,237 @@
|
|
1
|
+
import {
|
2
|
+
PendingTransaction,
|
3
|
+
ScriptFunctionPayload,
|
4
|
+
SubmitTransactionRequest,
|
5
|
+
TransactionPayload
|
6
|
+
} from 'aptos/dist/api/data-contracts';
|
7
|
+
import {
|
8
|
+
WalletDisconnectionError,
|
9
|
+
WalletNotConnectedError,
|
10
|
+
WalletNotReadyError,
|
11
|
+
WalletSignTransactionError
|
12
|
+
} from '../WalletProviders/errors';
|
13
|
+
import {
|
14
|
+
BaseWalletAdapter,
|
15
|
+
PublicKey,
|
16
|
+
scopePollingDetectionStrategy,
|
17
|
+
WalletName,
|
18
|
+
WalletReadyState
|
19
|
+
} from './BaseAdapter';
|
20
|
+
|
21
|
+
interface IMartianWallet {
|
22
|
+
connect: (params?: any) => Promise<any>;
|
23
|
+
publicKey?: string;
|
24
|
+
isConnected?: boolean;
|
25
|
+
signGenericTransaction(transaction: any): Promise<void>;
|
26
|
+
// signTransaction(transaction: any): Promise<void>;
|
27
|
+
disconnect(): Promise<void>;
|
28
|
+
}
|
29
|
+
|
30
|
+
interface MartianWindow extends Window {
|
31
|
+
aptos?: IMartianWallet;
|
32
|
+
}
|
33
|
+
|
34
|
+
declare const window: MartianWindow;
|
35
|
+
|
36
|
+
export const MartianWalletName = 'MartianWallet' as WalletName<'MartianWallet'>;
|
37
|
+
|
38
|
+
export interface MartianWalletAdapterConfig {
|
39
|
+
provider?: IMartianWallet;
|
40
|
+
// network?: WalletAdapterNetwork;
|
41
|
+
timeout?: number;
|
42
|
+
}
|
43
|
+
|
44
|
+
export class MartianWalletAdapter extends BaseWalletAdapter {
|
45
|
+
name = MartianWalletName;
|
46
|
+
|
47
|
+
url = 'https://chrome.google.com/webstore/detail/martian-wallet/efbglgofoippbgcjepnhiblaibcnclgk';
|
48
|
+
|
49
|
+
icon =
|
50
|
+
'https://www.gitbook.com/cdn-cgi/image/width=40,height=40,fit=contain,dpr=2,format=auto/https%3A%2F%2F1159842905-files.gitbook.io%2F~%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FXillBNDwQOz0oPJ4OtRH%252Ficon%252FaBwgf6d32iEu3YE56Jvk%252Flogo128_squ.png%3Falt%3Dmedia%26token%3D0f5bef1f-a4bd-495e-a447-289c235bb76a';
|
51
|
+
|
52
|
+
protected _provider: IMartianWallet | undefined;
|
53
|
+
|
54
|
+
// protected _network: WalletAdapterNetwork;
|
55
|
+
protected _timeout: number;
|
56
|
+
|
57
|
+
protected _readyState: WalletReadyState =
|
58
|
+
typeof window === 'undefined' || typeof document === 'undefined'
|
59
|
+
? WalletReadyState.Unsupported
|
60
|
+
: WalletReadyState.NotDetected;
|
61
|
+
|
62
|
+
protected _connecting: boolean;
|
63
|
+
|
64
|
+
protected _wallet: any | null;
|
65
|
+
|
66
|
+
constructor({
|
67
|
+
// provider,
|
68
|
+
// network = WalletAdapterNetwork.Mainnet,
|
69
|
+
timeout = 10000
|
70
|
+
}: MartianWalletAdapterConfig = {}) {
|
71
|
+
super();
|
72
|
+
|
73
|
+
this._provider = window.aptos;
|
74
|
+
// this._network = network;
|
75
|
+
this._timeout = timeout;
|
76
|
+
this._connecting = false;
|
77
|
+
this._wallet = null;
|
78
|
+
|
79
|
+
if (this._readyState !== WalletReadyState.Unsupported) {
|
80
|
+
scopePollingDetectionStrategy(() => {
|
81
|
+
if (window.aptos) {
|
82
|
+
this._readyState = WalletReadyState.Installed;
|
83
|
+
this.emit('readyStateChange', this._readyState);
|
84
|
+
return true;
|
85
|
+
}
|
86
|
+
return false;
|
87
|
+
});
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
get publicKey(): PublicKey | null {
|
92
|
+
return this._wallet?.publicKey || null;
|
93
|
+
}
|
94
|
+
|
95
|
+
get connecting(): boolean {
|
96
|
+
return this._connecting;
|
97
|
+
}
|
98
|
+
|
99
|
+
get connected(): boolean {
|
100
|
+
return !!this._wallet?.isConnected;
|
101
|
+
}
|
102
|
+
|
103
|
+
get readyState(): WalletReadyState {
|
104
|
+
return this._readyState;
|
105
|
+
}
|
106
|
+
|
107
|
+
async connect(): Promise<void> {
|
108
|
+
try {
|
109
|
+
// console.log(1);
|
110
|
+
if (this.connected || this.connecting) return;
|
111
|
+
// console.log(2);
|
112
|
+
if (
|
113
|
+
!(
|
114
|
+
this._readyState === WalletReadyState.Loadable ||
|
115
|
+
this._readyState === WalletReadyState.Installed
|
116
|
+
)
|
117
|
+
)
|
118
|
+
throw new WalletNotReadyError();
|
119
|
+
// console.log(3);
|
120
|
+
this._connecting = true;
|
121
|
+
|
122
|
+
const provider = window.aptos;
|
123
|
+
// console.log(4);
|
124
|
+
const loggedInAddress = await new Promise<string>((resolve, reject) => {
|
125
|
+
provider?.disconnect();
|
126
|
+
// console.log(5);
|
127
|
+
provider?.connect((respAddress: string) => {
|
128
|
+
// console.log(6);
|
129
|
+
try {
|
130
|
+
resolve(respAddress);
|
131
|
+
} catch (err) {
|
132
|
+
reject(err);
|
133
|
+
}
|
134
|
+
// 0xc4265dc8a5d90715f8a60bebf16688819427bca928a537ad35f798d4d1267716
|
135
|
+
});
|
136
|
+
});
|
137
|
+
// console.log(7, loggedInAddress, window.aptos?.publicKey);
|
138
|
+
if (loggedInAddress === window.aptos?.publicKey) {
|
139
|
+
// console.log(8);
|
140
|
+
this._wallet = window.aptos;
|
141
|
+
}
|
142
|
+
// console.log(9);
|
143
|
+
this.emit('connect', this._wallet.publicKey);
|
144
|
+
} catch (error: any) {
|
145
|
+
// console.log(10, error);
|
146
|
+
this.emit('error', error);
|
147
|
+
throw error;
|
148
|
+
} finally {
|
149
|
+
// console.log(11);
|
150
|
+
this._connecting = false;
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
async disconnect(): Promise<void> {
|
155
|
+
const wallet = this._wallet;
|
156
|
+
if (wallet) {
|
157
|
+
this._wallet = null;
|
158
|
+
|
159
|
+
try {
|
160
|
+
await new Promise<void>((resolve, reject) => {
|
161
|
+
const timeout = setTimeout(() => resolve(), 250);
|
162
|
+
|
163
|
+
try {
|
164
|
+
wallet.disconnect(() => {
|
165
|
+
clearTimeout(timeout);
|
166
|
+
resolve();
|
167
|
+
});
|
168
|
+
} catch (err) {
|
169
|
+
reject(err);
|
170
|
+
}
|
171
|
+
});
|
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: TransactionPayload): Promise<SubmitTransactionRequest> {
|
181
|
+
try {
|
182
|
+
const wallet = this._wallet;
|
183
|
+
if (!wallet) throw new WalletNotConnectedError();
|
184
|
+
|
185
|
+
try {
|
186
|
+
const response = await new Promise<SubmitTransactionRequest>((resolve, reject) => {
|
187
|
+
wallet.signGenericTransaction(transaction, (resp: any) => {
|
188
|
+
// console.log('signTransaction', resp);
|
189
|
+
if (resp.status === 200) {
|
190
|
+
// console.log('Transaction is Signed successfully.');
|
191
|
+
resolve(resp);
|
192
|
+
} else {
|
193
|
+
reject(resp.message);
|
194
|
+
}
|
195
|
+
});
|
196
|
+
});
|
197
|
+
return response;
|
198
|
+
} catch (error: any) {
|
199
|
+
throw new WalletSignTransactionError(error?.message, error);
|
200
|
+
}
|
201
|
+
} catch (error: any) {
|
202
|
+
this.emit('error', error);
|
203
|
+
throw error;
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
async signAndSubmitTransaction(tempTransaction: TransactionPayload): Promise<PendingTransaction> {
|
208
|
+
try {
|
209
|
+
const wallet = this._wallet;
|
210
|
+
if (!wallet) throw new WalletNotConnectedError();
|
211
|
+
const transaction = tempTransaction as ScriptFunctionPayload;
|
212
|
+
|
213
|
+
try {
|
214
|
+
// console.log('trans', 1);
|
215
|
+
const response = await new Promise<PendingTransaction>((resolve, reject) => {
|
216
|
+
// const args = [...transaction.type_arguments, transaction.arguments[0] / 1000];
|
217
|
+
// console.log('trans 2', wallet, transaction, args);
|
218
|
+
wallet.signGenericTransaction(transaction.type, transaction.arguments, (resp: any) => {
|
219
|
+
// console.log('signTransaction', resp);
|
220
|
+
if (resp.status === 200) {
|
221
|
+
// console.log('Transaction is Signed successfully.');
|
222
|
+
resolve(resp);
|
223
|
+
} else {
|
224
|
+
reject(resp.message);
|
225
|
+
}
|
226
|
+
});
|
227
|
+
});
|
228
|
+
return response;
|
229
|
+
} catch (error: any) {
|
230
|
+
throw new WalletSignTransactionError(error);
|
231
|
+
}
|
232
|
+
} catch (error: any) {
|
233
|
+
this.emit('error', error);
|
234
|
+
throw error;
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}
|
@@ -0,0 +1,240 @@
|
|
1
|
+
import {
|
2
|
+
PendingTransaction,
|
3
|
+
ScriptFunctionPayload,
|
4
|
+
SubmitTransactionRequest,
|
5
|
+
TransactionPayload
|
6
|
+
} from 'aptos/dist/api/data-contracts';
|
7
|
+
import {
|
8
|
+
WalletDisconnectionError,
|
9
|
+
WalletNotConnectedError,
|
10
|
+
WalletNotReadyError,
|
11
|
+
WalletSignTransactionError
|
12
|
+
} from '../WalletProviders/errors';
|
13
|
+
import {
|
14
|
+
BaseWalletAdapter,
|
15
|
+
PublicKey,
|
16
|
+
scopePollingDetectionStrategy,
|
17
|
+
WalletName,
|
18
|
+
WalletReadyState
|
19
|
+
} from './BaseAdapter';
|
20
|
+
|
21
|
+
interface IMultiMaskWallet {
|
22
|
+
currentProvider: {
|
23
|
+
enable: () => Promise<PublicKey[]>;
|
24
|
+
sendAsync(payload: any, callback: (error: Error | null, result?: any) => void): void;
|
25
|
+
};
|
26
|
+
request: (params?: any) => Promise<any>;
|
27
|
+
publicKey?: string;
|
28
|
+
isConnected?: boolean;
|
29
|
+
signGenericTransaction(transaction: any): Promise<void>;
|
30
|
+
// signTransaction(transaction: any): Promise<void>;
|
31
|
+
disconnect(): Promise<void>;
|
32
|
+
}
|
33
|
+
|
34
|
+
interface MultiMaskWindow extends Window {
|
35
|
+
aptosWeb3?: IMultiMaskWallet;
|
36
|
+
}
|
37
|
+
|
38
|
+
declare const window: MultiMaskWindow;
|
39
|
+
|
40
|
+
export const MultiMaskWalletName = 'MultiMaskWallet' as WalletName<'MultiMaskWallet'>;
|
41
|
+
|
42
|
+
export interface MultiMaskWalletAdapterConfig {
|
43
|
+
provider?: IMultiMaskWallet;
|
44
|
+
// network?: WalletAdapterNetwork;
|
45
|
+
timeout?: number;
|
46
|
+
}
|
47
|
+
|
48
|
+
export class MultiMaskWalletAdapter extends BaseWalletAdapter {
|
49
|
+
name = MultiMaskWalletName;
|
50
|
+
|
51
|
+
url = 'https://github.com/pontem-network/aptos-chrome-extension/releases';
|
52
|
+
|
53
|
+
icon = 'https://liquidswap.pontem.network/img/logo.87454209.svg';
|
54
|
+
|
55
|
+
protected _provider: IMultiMaskWallet | undefined;
|
56
|
+
|
57
|
+
// protected _network: WalletAdapterNetwork;
|
58
|
+
protected _timeout: number;
|
59
|
+
|
60
|
+
protected _readyState: WalletReadyState =
|
61
|
+
typeof window === 'undefined' || typeof document === 'undefined'
|
62
|
+
? WalletReadyState.Unsupported
|
63
|
+
: WalletReadyState.NotDetected;
|
64
|
+
|
65
|
+
protected _connecting: boolean;
|
66
|
+
|
67
|
+
protected _wallet: any | null;
|
68
|
+
|
69
|
+
constructor({
|
70
|
+
// provider,
|
71
|
+
// network = WalletAdapterNetwork.Mainnet,
|
72
|
+
timeout = 10000
|
73
|
+
}: MultiMaskWalletAdapterConfig = {}) {
|
74
|
+
super();
|
75
|
+
|
76
|
+
this._provider = window.aptosWeb3;
|
77
|
+
// this._network = network;
|
78
|
+
this._timeout = timeout;
|
79
|
+
this._connecting = false;
|
80
|
+
this._wallet = null;
|
81
|
+
|
82
|
+
if (this._readyState !== WalletReadyState.Unsupported) {
|
83
|
+
scopePollingDetectionStrategy(() => {
|
84
|
+
if (this._provider) {
|
85
|
+
this._readyState = WalletReadyState.Installed;
|
86
|
+
this.emit('readyStateChange', this._readyState);
|
87
|
+
return true;
|
88
|
+
}
|
89
|
+
return false;
|
90
|
+
});
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
get publicKey(): PublicKey | null {
|
95
|
+
return this._wallet?.publicKey || null;
|
96
|
+
}
|
97
|
+
|
98
|
+
get connecting(): boolean {
|
99
|
+
return this._connecting;
|
100
|
+
}
|
101
|
+
|
102
|
+
get connected(): boolean {
|
103
|
+
return !!this._wallet?.isConnected;
|
104
|
+
}
|
105
|
+
|
106
|
+
get readyState(): WalletReadyState {
|
107
|
+
return this._readyState;
|
108
|
+
}
|
109
|
+
|
110
|
+
async connect(): Promise<void> {
|
111
|
+
try {
|
112
|
+
// console.log(1);
|
113
|
+
if (this.connected || this.connecting) return;
|
114
|
+
// console.log(2);
|
115
|
+
if (
|
116
|
+
!(
|
117
|
+
this._readyState === WalletReadyState.Loadable ||
|
118
|
+
this._readyState === WalletReadyState.Installed
|
119
|
+
)
|
120
|
+
)
|
121
|
+
throw new WalletNotReadyError();
|
122
|
+
// console.log(3);
|
123
|
+
this._connecting = true;
|
124
|
+
|
125
|
+
const provider = this._provider;
|
126
|
+
// console.log(4);
|
127
|
+
|
128
|
+
const wallets = await provider?.currentProvider.enable();
|
129
|
+
if (wallets && wallets.length) {
|
130
|
+
this._wallet = {
|
131
|
+
publicKey: wallets[0],
|
132
|
+
isConnected: true
|
133
|
+
};
|
134
|
+
// console.log(9, this._wallet);
|
135
|
+
this.emit('connect', this._wallet);
|
136
|
+
}
|
137
|
+
} catch (error: any) {
|
138
|
+
// console.log(10, error);
|
139
|
+
this.emit('error', error);
|
140
|
+
throw error;
|
141
|
+
} finally {
|
142
|
+
// console.log(11);
|
143
|
+
this._connecting = false;
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
async disconnect(): Promise<void> {
|
148
|
+
const wallet = this._wallet;
|
149
|
+
if (wallet) {
|
150
|
+
this._wallet = null;
|
151
|
+
|
152
|
+
try {
|
153
|
+
await new Promise<void>((resolve, reject) => {
|
154
|
+
const timeout = setTimeout(() => resolve(), 250);
|
155
|
+
|
156
|
+
try {
|
157
|
+
wallet.disconnect(() => {
|
158
|
+
clearTimeout(timeout);
|
159
|
+
resolve();
|
160
|
+
});
|
161
|
+
} catch (err) {
|
162
|
+
reject(err);
|
163
|
+
}
|
164
|
+
});
|
165
|
+
} catch (error: any) {
|
166
|
+
this.emit('error', new WalletDisconnectionError(error?.message, error));
|
167
|
+
}
|
168
|
+
}
|
169
|
+
|
170
|
+
this.emit('disconnect');
|
171
|
+
}
|
172
|
+
|
173
|
+
async signTransaction(transaction: TransactionPayload): Promise<SubmitTransactionRequest> {
|
174
|
+
try {
|
175
|
+
const wallet = this._wallet;
|
176
|
+
if (!wallet) throw new WalletNotConnectedError();
|
177
|
+
|
178
|
+
try {
|
179
|
+
const response = await new Promise<SubmitTransactionRequest>((resolve, reject) => {
|
180
|
+
wallet.signGenericTransaction(transaction, (resp: any) => {
|
181
|
+
// console.log('signTransaction', resp);
|
182
|
+
if (resp.status === 200) {
|
183
|
+
// console.log('Transaction is Signed successfully.');
|
184
|
+
resolve(resp);
|
185
|
+
} else {
|
186
|
+
reject(resp.message);
|
187
|
+
}
|
188
|
+
});
|
189
|
+
});
|
190
|
+
return response;
|
191
|
+
} catch (error: any) {
|
192
|
+
throw new WalletSignTransactionError(error?.message, error);
|
193
|
+
}
|
194
|
+
} catch (error: any) {
|
195
|
+
this.emit('error', error);
|
196
|
+
throw error;
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
async signAndSubmitTransaction(tempTransaction: TransactionPayload): Promise<PendingTransaction> {
|
201
|
+
try {
|
202
|
+
const wallet = this._provider;
|
203
|
+
if (!wallet) throw new WalletNotConnectedError();
|
204
|
+
const transaction = tempTransaction as ScriptFunctionPayload;
|
205
|
+
|
206
|
+
try {
|
207
|
+
// console.log('trans', 1);
|
208
|
+
const response = await new Promise<PendingTransaction>((resolve, reject) => {
|
209
|
+
// const args = [...transaction.type_arguments, transaction.arguments[0] / 1000];
|
210
|
+
// console.log('trans 2', transaction, transaction.function.split(':')[0]);
|
211
|
+
wallet.currentProvider.sendAsync(
|
212
|
+
{
|
213
|
+
method: 'eth_sendTransaction',
|
214
|
+
params: [{ from: transaction.function.split(':')[0] }]
|
215
|
+
},
|
216
|
+
(error, resp: any) => {
|
217
|
+
console.log('signTransaction', error, resp);
|
218
|
+
if (error) {
|
219
|
+
reject(error.message);
|
220
|
+
} else {
|
221
|
+
resolve(resp);
|
222
|
+
}
|
223
|
+
// if (resp.status === 200) {
|
224
|
+
// // console.log('Transaction is Signed successfully.');
|
225
|
+
// } else {
|
226
|
+
// reject(resp.message);
|
227
|
+
// }
|
228
|
+
}
|
229
|
+
);
|
230
|
+
});
|
231
|
+
return response;
|
232
|
+
} catch (error: any) {
|
233
|
+
throw new WalletSignTransactionError(error);
|
234
|
+
}
|
235
|
+
} catch (error: any) {
|
236
|
+
this.emit('error', error);
|
237
|
+
throw error;
|
238
|
+
}
|
239
|
+
}
|
240
|
+
}
|