@manahippo/aptos-wallet-adapter 0.4.1 → 0.4.3
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 +5 -2
- package/dist/WalletAdapters/BaseAdapter.js +14 -6
- package/dist/WalletAdapters/BaseAdapter.js.map +1 -1
- package/dist/WalletAdapters/FewchaWallet.js +152 -127
- package/dist/WalletAdapters/FewchaWallet.js.map +1 -1
- package/dist/WalletAdapters/HippoExtensionWallet.js +119 -97
- package/dist/WalletAdapters/HippoExtensionWallet.js.map +1 -1
- package/dist/WalletAdapters/HippoWallet.js +131 -105
- package/dist/WalletAdapters/HippoWallet.js.map +1 -1
- package/dist/WalletAdapters/HyperPayWallet.d.ts +56 -0
- package/dist/WalletAdapters/HyperPayWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/HyperPayWallet.js +180 -0
- package/dist/WalletAdapters/HyperPayWallet.js.map +1 -0
- package/dist/WalletAdapters/MartianWallet.js +133 -110
- package/dist/WalletAdapters/MartianWallet.js.map +1 -1
- package/dist/WalletAdapters/NightlyWallet.js +5 -1
- package/dist/WalletAdapters/NightlyWallet.js.map +1 -1
- package/dist/WalletAdapters/PetraWallet.js +127 -101
- package/dist/WalletAdapters/PetraWallet.js.map +1 -1
- package/dist/WalletAdapters/PontemWallet.js +128 -102
- package/dist/WalletAdapters/PontemWallet.js.map +1 -1
- package/dist/WalletAdapters/RiseWallet.d.ts +53 -0
- package/dist/WalletAdapters/RiseWallet.d.ts.map +1 -0
- package/dist/WalletAdapters/RiseWallet.js +187 -0
- package/dist/WalletAdapters/RiseWallet.js.map +1 -0
- package/dist/WalletAdapters/SpikaWallet.js +136 -111
- package/dist/WalletAdapters/SpikaWallet.js.map +1 -1
- package/dist/WalletAdapters/index.d.ts +2 -0
- package/dist/WalletAdapters/index.d.ts.map +1 -1
- package/dist/WalletAdapters/index.js +26 -8
- package/dist/WalletAdapters/index.js.map +1 -1
- package/dist/WalletProviders/WalletProvider.js +82 -71
- package/dist/WalletProviders/WalletProvider.js.map +1 -1
- package/dist/WalletProviders/errors.js +41 -19
- package/dist/WalletProviders/errors.js.map +1 -1
- package/dist/WalletProviders/index.js +19 -3
- package/dist/WalletProviders/index.js.map +1 -1
- package/dist/WalletProviders/useWallet.js +8 -4
- package/dist/WalletProviders/useWallet.js.map +1 -1
- package/dist/config/aptosConstants.js +13 -10
- package/dist/config/aptosConstants.js.map +1 -1
- package/dist/hooks/useLocalStorage.js +9 -5
- package/dist/hooks/useLocalStorage.js.map +1 -1
- package/dist/hooks/useSSR.js +8 -4
- package/dist/hooks/useSSR.js.map +1 -1
- package/dist/index.js +18 -2
- package/dist/index.js.map +1 -1
- package/dist/utilities/util.js +5 -1
- package/dist/utilities/util.js.map +1 -1
- package/package.json +1 -1
- package/src/WalletAdapters/HyperPayWallet.ts +237 -0
- package/src/WalletAdapters/RiseWallet.ts +235 -0
- package/src/WalletAdapters/index.ts +2 -0
package/dist/hooks/useSSR.js
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useSSR = void 0;
|
4
|
+
const react_1 = require("react");
|
5
|
+
const useSSR = () => {
|
6
|
+
const [isClient, setIsClient] = (0, react_1.useState)(false);
|
7
|
+
(0, react_1.useEffect)(() => {
|
5
8
|
if (typeof window !== 'undefined') {
|
6
9
|
setIsClient(true);
|
7
10
|
}
|
@@ -10,4 +13,5 @@ export const useSSR = () => {
|
|
10
13
|
isClient
|
11
14
|
};
|
12
15
|
};
|
16
|
+
exports.useSSR = useSSR;
|
13
17
|
//# sourceMappingURL=useSSR.js.map
|
package/dist/hooks/useSSR.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useSSR.js","sourceRoot":"","sources":["../../src/hooks/useSSR.ts"],"names":[],"mappings":"AAAA,
|
1
|
+
{"version":3,"file":"useSSR.js","sourceRoot":"","sources":["../../src/hooks/useSSR.ts"],"names":[],"mappings":";;;AAAA,iCAA4C;AAErC,MAAM,MAAM,GAAG,GAAG,EAAE;IACzB,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,IAAA,gBAAQ,EAAC,KAAK,CAAC,CAAC;IAEhD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;YACjC,WAAW,CAAC,IAAI,CAAC,CAAC;SACnB;IACH,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO;QACL,QAAQ;KACT,CAAC;AACJ,CAAC,CAAC;AAZW,QAAA,MAAM,UAYjB"}
|
package/dist/index.js
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./WalletProviders"), exports);
|
18
|
+
__exportStar(require("./WalletAdapters"), exports);
|
3
19
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,mDAAiC"}
|
package/dist/utilities/util.js
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.payloadV1ToV0 = void 0;
|
4
|
+
const payloadV1ToV0 = (payload) => {
|
2
5
|
const v1 = payload;
|
3
6
|
return {
|
4
7
|
type: 'script_function_payload',
|
@@ -7,4 +10,5 @@ export const payloadV1ToV0 = (payload) => {
|
|
7
10
|
arguments: v1.arguments
|
8
11
|
};
|
9
12
|
};
|
13
|
+
exports.payloadV1ToV0 = payloadV1ToV0;
|
10
14
|
//# sourceMappingURL=util.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/utilities/util.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/utilities/util.ts"],"names":[],"mappings":";;;AAEO,MAAM,aAAa,GAAG,CAAC,OAA2B,EAAE,EAAE;IAC3D,MAAM,EAAE,GAAG,OAAkD,CAAC;IAC9D,OAAO;QACL,IAAI,EAAE,yBAAyB;QAC/B,QAAQ,EAAE,EAAE,CAAC,QAAQ;QACrB,cAAc,EAAE,EAAE,CAAC,cAAc;QACjC,SAAS,EAAE,EAAE,CAAC,SAAS;KACxB,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,aAAa,iBAQxB"}
|
package/package.json
CHANGED
@@ -0,0 +1,237 @@
|
|
1
|
+
import { MaybeHexString } from 'aptos';
|
2
|
+
import { TransactionPayload, HexEncodedBytes } from 'aptos/src/generated';
|
3
|
+
import {
|
4
|
+
WalletDisconnectionError,
|
5
|
+
WalletNotConnectedError,
|
6
|
+
WalletNotReadyError,
|
7
|
+
WalletSignAndSubmitMessageError,
|
8
|
+
WalletSignMessageError,
|
9
|
+
WalletSignTransactionError
|
10
|
+
} from '../WalletProviders/errors';
|
11
|
+
import {
|
12
|
+
AccountKeys,
|
13
|
+
BaseWalletAdapter,
|
14
|
+
scopePollingDetectionStrategy,
|
15
|
+
WalletName,
|
16
|
+
WalletReadyState
|
17
|
+
} from './BaseAdapter';
|
18
|
+
|
19
|
+
interface ConnectHyperPayAccount {
|
20
|
+
address: MaybeHexString;
|
21
|
+
method: string;
|
22
|
+
publicKey: MaybeHexString;
|
23
|
+
status: number;
|
24
|
+
}
|
25
|
+
|
26
|
+
interface HyperPayAccount {
|
27
|
+
address: MaybeHexString;
|
28
|
+
publicKey: MaybeHexString;
|
29
|
+
authKey: MaybeHexString;
|
30
|
+
isConnected: boolean;
|
31
|
+
}
|
32
|
+
interface IHyperPayWallet {
|
33
|
+
connect: () => Promise<ConnectHyperPayAccount>;
|
34
|
+
account(): Promise<HyperPayAccount>;
|
35
|
+
isConnected(): Promise<boolean>;
|
36
|
+
generateTransaction(sender: MaybeHexString, payload: any, options?: any): Promise<any>;
|
37
|
+
signAndSubmitTransaction(transaction: TransactionPayload): Promise<HexEncodedBytes>;
|
38
|
+
signTransaction(transaction: TransactionPayload): Promise<Uint8Array>;
|
39
|
+
signMessage(message: string): Promise<{ signature: string }>;
|
40
|
+
disconnect(): Promise<void>;
|
41
|
+
}
|
42
|
+
|
43
|
+
interface HyperPayWindow extends Window {
|
44
|
+
hyperpay?: IHyperPayWallet;
|
45
|
+
}
|
46
|
+
|
47
|
+
declare const window: HyperPayWindow;
|
48
|
+
|
49
|
+
export const HyperPayWalletName = 'HyperPay' as WalletName<'HyperPay'>;
|
50
|
+
|
51
|
+
export interface HyperPayWalletAdapterConfig {
|
52
|
+
provider?: IHyperPayWallet;
|
53
|
+
// network?: WalletAdapterNetwork;
|
54
|
+
timeout?: number;
|
55
|
+
}
|
56
|
+
|
57
|
+
export class HyperPayWalletAdapter extends BaseWalletAdapter {
|
58
|
+
name = HyperPayWalletName;
|
59
|
+
|
60
|
+
url = 'https://www.hyperpay.tech/';
|
61
|
+
|
62
|
+
icon =
|
63
|
+
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMjE1IDEyMTUiPjxwYXRoIGQ9Ik02MDcuNSAwQzk0My4wMTMgMCAxMjE1IDI3MS45ODcgMTIxNSA2MDcuNVM5NDMuMDEzIDEyMTUgNjA3LjUgMTIxNSAwIDk0My4wMTMgMCA2MDcuNSAyNzEuOTg3IDAgNjA3LjUgMHpNMzUxLjY4IDM3MS4zMTVzMy43MDgtNzIuOSA3Ny43Ni03Mi45aDIwNC4xMnMxMTkuNDM3LS4xIDIwNC4xMiA3NS4zM2MwIDAgMTExLjg0MyA5My42MTIgMTE2LjY0IDIzNS43MSAwIDAgMS45NjIgMTE5LjA0NS03Mi45IDIxMS40MSAwIDAtNzcuOTMxIDEyMS41LTI2Mi40NCAxMjEuNUg0NDEuNTlzLTg5LjkxIDExLjA0OS04OS45MS04NS4wNXYtNDg2ek0xMTkuMzQ0IDU1My42NTlzLjkwNi0xNy44MiAxOS4wMDgtMTcuODJoNDkuOXMyOS4yLS4wMjMgNDkuOSAxOC40MTRjMCAwIDI3LjMzOSAyMi44ODMgMjguNTEyIDU3LjYxOCAwIDAgLjQ4IDI5LjEtMTcuODIgNTEuNjc4IDAgMC0xOS4wNSAyOS43LTY0LjE1MiAyOS43aC00My4zN3MtMjEuOTc4IDIuNy0yMS45NzgtMjAuNzl2LTExOC44eiIgZmlsbD0iIzE1N0VGQiIgZmlsbC1ydWxlPSJldmVub2RkIi8+PC9zdmc+';
|
64
|
+
|
65
|
+
protected _provider: IHyperPayWallet | undefined;
|
66
|
+
|
67
|
+
// protected _network: WalletAdapterNetwork;
|
68
|
+
protected _timeout: number;
|
69
|
+
|
70
|
+
protected _readyState: WalletReadyState =
|
71
|
+
typeof window === 'undefined' || typeof document === 'undefined'
|
72
|
+
? WalletReadyState.Unsupported
|
73
|
+
: WalletReadyState.NotDetected;
|
74
|
+
|
75
|
+
protected _connecting: boolean;
|
76
|
+
|
77
|
+
protected _wallet: HyperPayAccount | null;
|
78
|
+
|
79
|
+
constructor({
|
80
|
+
// provider,
|
81
|
+
// network = WalletAdapterNetwork.Mainnet,
|
82
|
+
timeout = 10000
|
83
|
+
}: HyperPayWalletAdapterConfig = {}) {
|
84
|
+
super();
|
85
|
+
|
86
|
+
this._provider = typeof window !== 'undefined' ? window.hyperpay : undefined;
|
87
|
+
// this._network = network;
|
88
|
+
this._timeout = timeout;
|
89
|
+
this._connecting = false;
|
90
|
+
this._wallet = null;
|
91
|
+
|
92
|
+
if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
|
93
|
+
scopePollingDetectionStrategy(() => {
|
94
|
+
if (window.hyperpay) {
|
95
|
+
this._readyState = WalletReadyState.Installed;
|
96
|
+
this.emit('readyStateChange', this._readyState);
|
97
|
+
return true;
|
98
|
+
}
|
99
|
+
return false;
|
100
|
+
});
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
get publicAccount(): AccountKeys {
|
105
|
+
return {
|
106
|
+
publicKey: this._wallet?.publicKey || null,
|
107
|
+
address: this._wallet?.address || null,
|
108
|
+
authKey: this._wallet?.authKey || null
|
109
|
+
};
|
110
|
+
}
|
111
|
+
|
112
|
+
get connecting(): boolean {
|
113
|
+
return this._connecting;
|
114
|
+
}
|
115
|
+
|
116
|
+
get connected(): boolean {
|
117
|
+
return !!this._wallet?.isConnected;
|
118
|
+
}
|
119
|
+
|
120
|
+
get readyState(): WalletReadyState {
|
121
|
+
return this._readyState;
|
122
|
+
}
|
123
|
+
|
124
|
+
async connect(): Promise<void> {
|
125
|
+
try {
|
126
|
+
if (this.connected || this.connecting) return;
|
127
|
+
if (
|
128
|
+
!(
|
129
|
+
this._readyState === WalletReadyState.Loadable ||
|
130
|
+
this._readyState === WalletReadyState.Installed
|
131
|
+
)
|
132
|
+
)
|
133
|
+
throw new WalletNotReadyError();
|
134
|
+
this._connecting = true;
|
135
|
+
|
136
|
+
const provider = this._provider || window.hyperpay;
|
137
|
+
const isConnected = await provider?.isConnected();
|
138
|
+
if (isConnected) {
|
139
|
+
await provider?.disconnect();
|
140
|
+
}
|
141
|
+
const response = await provider?.connect();
|
142
|
+
|
143
|
+
if (!response) {
|
144
|
+
throw new WalletNotConnectedError('No connect response');
|
145
|
+
}
|
146
|
+
|
147
|
+
const walletAccount = await provider?.account();
|
148
|
+
if (walletAccount) {
|
149
|
+
this._wallet = {
|
150
|
+
...walletAccount,
|
151
|
+
isConnected: true
|
152
|
+
};
|
153
|
+
}
|
154
|
+
this.emit('connect', this._wallet?.address || '');
|
155
|
+
} catch (error: any) {
|
156
|
+
this.emit('error', new Error(error));
|
157
|
+
throw error;
|
158
|
+
} finally {
|
159
|
+
this._connecting = false;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
async disconnect(): Promise<void> {
|
164
|
+
const wallet = this._wallet;
|
165
|
+
const provider = this._provider || window.hyperpay;
|
166
|
+
if (wallet) {
|
167
|
+
this._wallet = null;
|
168
|
+
|
169
|
+
try {
|
170
|
+
await provider?.disconnect();
|
171
|
+
} catch (error: any) {
|
172
|
+
this.emit('error', new WalletDisconnectionError(error?.message, error));
|
173
|
+
}
|
174
|
+
}
|
175
|
+
|
176
|
+
this.emit('disconnect');
|
177
|
+
}
|
178
|
+
|
179
|
+
async signTransaction(transactionPyld: TransactionPayload, options?: any): Promise<Uint8Array> {
|
180
|
+
try {
|
181
|
+
const wallet = this._wallet;
|
182
|
+
const provider = this._provider || window.hyperpay;
|
183
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
184
|
+
const tx = await provider.generateTransaction(wallet.address || '', transactionPyld, options);
|
185
|
+
if (!tx) throw new Error('Cannot generate transaction');
|
186
|
+
const response = await provider?.signTransaction(tx);
|
187
|
+
|
188
|
+
if (!response) {
|
189
|
+
throw new Error('No response');
|
190
|
+
}
|
191
|
+
return response;
|
192
|
+
} catch (error: any) {
|
193
|
+
this.emit('error', new WalletSignTransactionError(error));
|
194
|
+
throw error;
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
async signAndSubmitTransaction(
|
199
|
+
transactionPyld: TransactionPayload,
|
200
|
+
options?: any
|
201
|
+
): Promise<{ hash: HexEncodedBytes }> {
|
202
|
+
try {
|
203
|
+
const wallet = this._wallet;
|
204
|
+
const provider = this._provider || window.hyperpay;
|
205
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
206
|
+
const tx = await provider.generateTransaction(wallet.address || '', transactionPyld, options);
|
207
|
+
if (!tx) throw new Error('Cannot generate transaction');
|
208
|
+
const response = await provider?.signAndSubmitTransaction(tx);
|
209
|
+
|
210
|
+
if (!response) {
|
211
|
+
throw new Error('No response');
|
212
|
+
}
|
213
|
+
return { hash: response };
|
214
|
+
} catch (error: any) {
|
215
|
+
this.emit('error', new WalletSignAndSubmitMessageError(error));
|
216
|
+
throw error;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
async signMessage(message: string): Promise<string> {
|
221
|
+
try {
|
222
|
+
const wallet = this._wallet;
|
223
|
+
const provider = this._provider || window.hyperpay;
|
224
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
225
|
+
const response = await provider?.signMessage(message);
|
226
|
+
if (response?.signature) {
|
227
|
+
return response?.signature;
|
228
|
+
} else {
|
229
|
+
throw new Error('Sign Message failed');
|
230
|
+
}
|
231
|
+
} catch (error: any) {
|
232
|
+
const errMsg = error.message;
|
233
|
+
this.emit('error', new WalletSignMessageError(errMsg));
|
234
|
+
throw error;
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}
|
@@ -0,0 +1,235 @@
|
|
1
|
+
import { HexEncodedBytes, TransactionPayload } from 'aptos/src/generated';
|
2
|
+
import {
|
3
|
+
WalletDisconnectionError,
|
4
|
+
WalletNotConnectedError,
|
5
|
+
WalletNotReadyError,
|
6
|
+
WalletSignMessageError,
|
7
|
+
WalletSignTransactionError
|
8
|
+
} from '../WalletProviders';
|
9
|
+
import {
|
10
|
+
AccountKeys,
|
11
|
+
BaseWalletAdapter,
|
12
|
+
scopePollingDetectionStrategy,
|
13
|
+
WalletName,
|
14
|
+
WalletReadyState
|
15
|
+
} from './BaseAdapter';
|
16
|
+
import { MaybeHexString } from 'aptos';
|
17
|
+
|
18
|
+
interface RiseAccount {
|
19
|
+
address: MaybeHexString;
|
20
|
+
publicKey: MaybeHexString;
|
21
|
+
authKey: MaybeHexString;
|
22
|
+
isConnected: boolean;
|
23
|
+
}
|
24
|
+
|
25
|
+
interface IRiseWallet {
|
26
|
+
connect: () => Promise<{ address: string }>;
|
27
|
+
account(): Promise<RiseAccount>;
|
28
|
+
isConnected: () => Promise<boolean>;
|
29
|
+
signAndSubmitTransaction(transaction: any): Promise<{ hash: HexEncodedBytes }>;
|
30
|
+
signTransaction(transaction: any, options?: any): Promise<Uint8Array>;
|
31
|
+
signMessage(message: string): Promise<{ signature: string }>;
|
32
|
+
disconnect(): Promise<void>;
|
33
|
+
}
|
34
|
+
|
35
|
+
interface RiseWindow extends Window {
|
36
|
+
rise?: IRiseWallet;
|
37
|
+
}
|
38
|
+
|
39
|
+
declare const window: RiseWindow;
|
40
|
+
|
41
|
+
export const RiseWalletName = 'Rise Wallet' as WalletName<'Rise Wallet'>;
|
42
|
+
|
43
|
+
export interface RiseWalletAdapterConfig {
|
44
|
+
provider?: IRiseWallet;
|
45
|
+
// network?: WalletAdapterNetwork;
|
46
|
+
timeout?: number;
|
47
|
+
}
|
48
|
+
|
49
|
+
export class RiseWalletAdapter extends BaseWalletAdapter {
|
50
|
+
name = RiseWalletName;
|
51
|
+
|
52
|
+
url = 'https://chrome.google.com/webstore/detail/hbbgbephgojikajhfbomhlmmollphcad';
|
53
|
+
|
54
|
+
icon = 'https://static.risewallet.io/logo.png';
|
55
|
+
|
56
|
+
protected _provider: IRiseWallet | undefined;
|
57
|
+
|
58
|
+
// protected _network: WalletAdapterNetwork;
|
59
|
+
protected _timeout: number;
|
60
|
+
|
61
|
+
protected _readyState: WalletReadyState =
|
62
|
+
typeof window === 'undefined' || typeof document === 'undefined'
|
63
|
+
? WalletReadyState.Unsupported
|
64
|
+
: WalletReadyState.NotDetected;
|
65
|
+
|
66
|
+
protected _connecting: boolean;
|
67
|
+
|
68
|
+
protected _wallet: any | null;
|
69
|
+
|
70
|
+
constructor({
|
71
|
+
// provider,
|
72
|
+
// network = WalletAdapterNetwork.Mainnet,
|
73
|
+
timeout = 10000
|
74
|
+
}: RiseWalletAdapterConfig = {}) {
|
75
|
+
super();
|
76
|
+
|
77
|
+
this._provider = typeof window !== 'undefined' ? window.rise : undefined;
|
78
|
+
// this._network = network;
|
79
|
+
this._timeout = timeout;
|
80
|
+
this._connecting = false;
|
81
|
+
this._wallet = null;
|
82
|
+
|
83
|
+
if (typeof window !== 'undefined' && this._readyState !== WalletReadyState.Unsupported) {
|
84
|
+
scopePollingDetectionStrategy(() => {
|
85
|
+
if (window.rise) {
|
86
|
+
this._readyState = WalletReadyState.Installed;
|
87
|
+
this.emit('readyStateChange', this._readyState);
|
88
|
+
return true;
|
89
|
+
}
|
90
|
+
return false;
|
91
|
+
});
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
get publicAccount(): AccountKeys {
|
96
|
+
return {
|
97
|
+
publicKey: this._wallet?.publicKey || null,
|
98
|
+
address: this._wallet?.address || null,
|
99
|
+
authKey: this._wallet?.authKey || null
|
100
|
+
};
|
101
|
+
}
|
102
|
+
|
103
|
+
get connecting(): boolean {
|
104
|
+
return this._connecting;
|
105
|
+
}
|
106
|
+
|
107
|
+
get connected(): boolean {
|
108
|
+
return !!this._wallet?.isConnected;
|
109
|
+
}
|
110
|
+
|
111
|
+
get readyState(): WalletReadyState {
|
112
|
+
return this._readyState;
|
113
|
+
}
|
114
|
+
|
115
|
+
async connect(): Promise<void> {
|
116
|
+
try {
|
117
|
+
if (this.connected || this.connecting) return;
|
118
|
+
if (
|
119
|
+
!(
|
120
|
+
this._readyState === WalletReadyState.Loadable ||
|
121
|
+
this._readyState === WalletReadyState.Installed
|
122
|
+
)
|
123
|
+
)
|
124
|
+
throw new WalletNotReadyError();
|
125
|
+
this._connecting = true;
|
126
|
+
|
127
|
+
const provider = this._provider || window.rise;
|
128
|
+
const isConnected = await this._provider?.isConnected();
|
129
|
+
if (isConnected === true) {
|
130
|
+
await provider?.disconnect();
|
131
|
+
}
|
132
|
+
|
133
|
+
const response = await provider?.connect();
|
134
|
+
|
135
|
+
if (!response) {
|
136
|
+
throw new WalletNotConnectedError('User has rejected the request');
|
137
|
+
}
|
138
|
+
|
139
|
+
const account = await provider?.account();
|
140
|
+
if (account) {
|
141
|
+
const { publicKey, address, authKey } = account;
|
142
|
+
|
143
|
+
this._wallet = {
|
144
|
+
publicKey,
|
145
|
+
address,
|
146
|
+
authKey,
|
147
|
+
isConnected: true
|
148
|
+
};
|
149
|
+
|
150
|
+
this.emit('connect', this._wallet.publicKey);
|
151
|
+
}
|
152
|
+
} catch (error: any) {
|
153
|
+
this.emit('error', error);
|
154
|
+
throw error;
|
155
|
+
} finally {
|
156
|
+
this._connecting = false;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
async disconnect(): Promise<void> {
|
161
|
+
const wallet = this._wallet;
|
162
|
+
if (wallet) {
|
163
|
+
this._wallet = null;
|
164
|
+
|
165
|
+
try {
|
166
|
+
const provider = this._provider || window.rise;
|
167
|
+
await provider?.disconnect();
|
168
|
+
} catch (error: any) {
|
169
|
+
this.emit('error', new WalletDisconnectionError(error?.message, error));
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
this.emit('disconnect');
|
174
|
+
}
|
175
|
+
|
176
|
+
async signTransaction(transaction: TransactionPayload): Promise<Uint8Array> {
|
177
|
+
try {
|
178
|
+
const wallet = this._wallet;
|
179
|
+
const provider = this._provider || window.rise;
|
180
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
181
|
+
|
182
|
+
const response = await provider?.signTransaction(transaction);
|
183
|
+
if (response) {
|
184
|
+
return response;
|
185
|
+
} else {
|
186
|
+
throw new Error('Sign Transaction failed');
|
187
|
+
}
|
188
|
+
} catch (error: any) {
|
189
|
+
const errMsg = error.message;
|
190
|
+
this.emit('error', new WalletSignTransactionError(errMsg));
|
191
|
+
throw error;
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
195
|
+
async signAndSubmitTransaction(
|
196
|
+
transaction: TransactionPayload
|
197
|
+
): Promise<{ hash: HexEncodedBytes }> {
|
198
|
+
try {
|
199
|
+
const wallet = this._wallet;
|
200
|
+
const provider = this._provider || window.rise;
|
201
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
202
|
+
|
203
|
+
const response = await provider?.signAndSubmitTransaction(transaction);
|
204
|
+
if (response) {
|
205
|
+
return response;
|
206
|
+
} else {
|
207
|
+
throw new Error('Transaction failed');
|
208
|
+
}
|
209
|
+
} catch (error: any) {
|
210
|
+
const errMsg = error.message;
|
211
|
+
this.emit('error', new WalletSignTransactionError(errMsg));
|
212
|
+
throw error;
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
async signMessage(message: string): Promise<string> {
|
217
|
+
try {
|
218
|
+
const wallet = this._wallet;
|
219
|
+
const provider = this._provider || window.rise;
|
220
|
+
if (!wallet || !provider) throw new WalletNotConnectedError();
|
221
|
+
|
222
|
+
const response = await provider?.signMessage(message);
|
223
|
+
if (response?.signature) {
|
224
|
+
return response.signature;
|
225
|
+
} else {
|
226
|
+
throw new Error('Sign Message failed');
|
227
|
+
}
|
228
|
+
} catch (error: any) {
|
229
|
+
console.log('err', error);
|
230
|
+
const errMsg = error.message;
|
231
|
+
this.emit('error', new WalletSignMessageError(errMsg));
|
232
|
+
throw error;
|
233
|
+
}
|
234
|
+
}
|
235
|
+
}
|