@provablehq/aleo-wallet-adaptor-leo 0.1.1-alpha.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/LICENSE +674 -0
- package/README.md +31 -0
- package/dist/index.d.mts +219 -0
- package/dist/index.d.ts +219 -0
- package/dist/index.js +352 -0
- package/dist/index.mjs +339 -0
- package/package.json +44 -0
- package/src/LeoWalletAdapter.ts +405 -0
- package/src/index.ts +2 -0
- package/src/types.ts +141 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Deployment: () => Deployment,
|
|
24
|
+
LEO_NETWORK_MAP: () => LEO_NETWORK_MAP,
|
|
25
|
+
LeoWalletAdapter: () => LeoWalletAdapter,
|
|
26
|
+
Transition: () => Transition
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// src/LeoWalletAdapter.ts
|
|
31
|
+
var import_aleo_types2 = require("@provablehq/aleo-types");
|
|
32
|
+
var import_aleo_wallet_standard = require("@provablehq/aleo-wallet-standard");
|
|
33
|
+
var import_aleo_wallet_adaptor_core = require("@provablehq/aleo-wallet-adaptor-core");
|
|
34
|
+
|
|
35
|
+
// src/types.ts
|
|
36
|
+
var import_aleo_types = require("@provablehq/aleo-types");
|
|
37
|
+
var LEO_NETWORK_MAP = {
|
|
38
|
+
[import_aleo_types.Network.MAINNET]: "mainnet",
|
|
39
|
+
[import_aleo_types.Network.TESTNET3]: "testnetbeta",
|
|
40
|
+
[import_aleo_types.Network.CANARY]: "testnetbeta"
|
|
41
|
+
};
|
|
42
|
+
var Transition = class {
|
|
43
|
+
constructor(program, functionName, inputs) {
|
|
44
|
+
this.program = program;
|
|
45
|
+
this.functionName = functionName;
|
|
46
|
+
this.inputs = inputs;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var Deployment = class {
|
|
50
|
+
constructor(address, chainId, program, fee, feePrivate = true) {
|
|
51
|
+
this.address = address;
|
|
52
|
+
this.chainId = chainId;
|
|
53
|
+
this.program = program;
|
|
54
|
+
this.fee = fee;
|
|
55
|
+
this.feePrivate = feePrivate;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/LeoWalletAdapter.ts
|
|
60
|
+
var LeoWalletAdapter = class extends import_aleo_wallet_adaptor_core.BaseAleoWalletAdapter {
|
|
61
|
+
/**
|
|
62
|
+
* Create a new Leo wallet adapter
|
|
63
|
+
* @param config Adapter configuration
|
|
64
|
+
*/
|
|
65
|
+
constructor(config) {
|
|
66
|
+
super();
|
|
67
|
+
/**
|
|
68
|
+
* The wallet name
|
|
69
|
+
*/
|
|
70
|
+
this.name = "Leo Wallet";
|
|
71
|
+
/**
|
|
72
|
+
* The wallet URL
|
|
73
|
+
*/
|
|
74
|
+
this.url = "https://app.leo.app";
|
|
75
|
+
/**
|
|
76
|
+
* The wallet icon (base64-encoded SVG)
|
|
77
|
+
*/
|
|
78
|
+
this.icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAABcCAMAAADUMSJqAAAASFBMVEVjTP////9kTf9GIP/5+P9dRP+ajf9WO//Jwv9fR/9KJv+Ccf9ZP//08/+Xiv9aQf+upf/c2P+Qgf96aP9TNv/Nx/+Fdf+6sv91nL8+AAABCUlEQVRoge2YyQ6EIBBEccVWBHf//09H42FESNRJk+ik3s06vANCQ0oIAAAAf0ps48u8XHKXiYVZsyE5pbxgj8s63RPpTAhZROkZ9QV7nKSRRb7JT0kTyCGHHPInyceAcppyBz0zyQVlDlJzyT1k+XvkZKQ0FEROqmqKoqkU8ctV327fba+45ar7Jp3ilVO/j3pilat2H7WKU06VnVXEKDeNnTWGUX6cioV8izzosgT9ocetuKzKWw5R0OMfdnAtKyPDjVwR9LI48ny5lu6zgm0rztp9EE1cJ9THyDW4fLBNRcghhxxyDvmh+vOxybVdB16p/pzS0sewesz90vJGtfpD3QoAAOCNfADu9hzTpMe3fQAAAABJRU5ErkJggg==";
|
|
79
|
+
/**
|
|
80
|
+
* The wallet's decrypt permission
|
|
81
|
+
*/
|
|
82
|
+
this.decryptPermission = import_aleo_wallet_standard.WalletDecryptPermission.NoDecrypt;
|
|
83
|
+
/**
|
|
84
|
+
* Public key
|
|
85
|
+
*/
|
|
86
|
+
this._publicKey = "";
|
|
87
|
+
this._readyState = typeof window === "undefined" || typeof document === "undefined" ? import_aleo_wallet_standard.WalletReadyState.UNSUPPORTED : import_aleo_wallet_standard.WalletReadyState.NOT_DETECTED;
|
|
88
|
+
console.debug("LeoWalletAdapter constructor", config);
|
|
89
|
+
this.network = import_aleo_types2.Network.TESTNET3;
|
|
90
|
+
this._checkAvailability();
|
|
91
|
+
this._leoWallet = this._window?.leoWallet || this._window?.leo;
|
|
92
|
+
if (config?.isMobile) {
|
|
93
|
+
this.url = `https://app.leo.app/browser?url=${config.mobileWebviewUrl}`;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Check if Leo wallet is available
|
|
98
|
+
*/
|
|
99
|
+
_checkAvailability() {
|
|
100
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
101
|
+
this.readyState = import_aleo_wallet_standard.WalletReadyState.UNSUPPORTED;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this._window = window;
|
|
105
|
+
if (this._window.leoWallet || this._window.leo) {
|
|
106
|
+
this.readyState = import_aleo_wallet_standard.WalletReadyState.INSTALLED;
|
|
107
|
+
} else {
|
|
108
|
+
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
|
|
109
|
+
if (isMobile) {
|
|
110
|
+
this.readyState = import_aleo_wallet_standard.WalletReadyState.LOADABLE;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Connect to Leo wallet
|
|
116
|
+
* @returns The connected account
|
|
117
|
+
*/
|
|
118
|
+
async connect(network, decryptPermission, programs) {
|
|
119
|
+
try {
|
|
120
|
+
if (this.readyState !== import_aleo_wallet_standard.WalletReadyState.INSTALLED) {
|
|
121
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError("Leo Wallet is not available");
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
await this._leoWallet?.connect(decryptPermission, LEO_NETWORK_MAP[network], programs);
|
|
125
|
+
this.network = network;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error instanceof Object && "name" in error && (error.name === "InvalidParamsAleoWalletError" || error.name !== "NotGrantedAleoWalletError")) {
|
|
128
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError(
|
|
129
|
+
"Connection failed: Likely due to a difference in configured network and the selected wallet network. Configured network: " + network
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError(
|
|
133
|
+
error instanceof Error ? error.message : "Connection failed"
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
this._publicKey = this._leoWallet?.publicKey || "";
|
|
137
|
+
this.decryptPermission = decryptPermission;
|
|
138
|
+
if (!this._publicKey) {
|
|
139
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError("No address returned from wallet");
|
|
140
|
+
}
|
|
141
|
+
const account = {
|
|
142
|
+
address: this._publicKey
|
|
143
|
+
};
|
|
144
|
+
this.account = account;
|
|
145
|
+
this.emit("connect", account);
|
|
146
|
+
return account;
|
|
147
|
+
} catch (err) {
|
|
148
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
149
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError(err instanceof Error ? err.message : "Connection failed");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Disconnect from Leo wallet
|
|
154
|
+
*/
|
|
155
|
+
async disconnect() {
|
|
156
|
+
try {
|
|
157
|
+
await this._leoWallet?.disconnect();
|
|
158
|
+
this._publicKey = "";
|
|
159
|
+
this.account = void 0;
|
|
160
|
+
this.emit("disconnect");
|
|
161
|
+
} catch (err) {
|
|
162
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
163
|
+
throw new import_aleo_wallet_adaptor_core.WalletDisconnectionError(
|
|
164
|
+
err instanceof Error ? err.message : "Disconnection failed"
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Sign a transaction with Leo wallet
|
|
170
|
+
* @param options Transaction options
|
|
171
|
+
* @returns The signed transaction
|
|
172
|
+
*/
|
|
173
|
+
async signMessage(message) {
|
|
174
|
+
if (!this._publicKey || !this.account) {
|
|
175
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
const signature = await this._leoWallet?.signMessage(message);
|
|
179
|
+
if (!signature) {
|
|
180
|
+
throw new import_aleo_wallet_adaptor_core.WalletSignMessageError("Failed to sign message");
|
|
181
|
+
}
|
|
182
|
+
return signature.signature;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
throw new import_aleo_wallet_adaptor_core.WalletSignMessageError(
|
|
185
|
+
error instanceof Error ? error.message : "Failed to sign message"
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async decrypt(cipherText, tpk, programId, functionName, index) {
|
|
190
|
+
if (!this._leoWallet || !this._publicKey) {
|
|
191
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
192
|
+
}
|
|
193
|
+
switch (this.decryptPermission) {
|
|
194
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.NoDecrypt:
|
|
195
|
+
throw new import_aleo_wallet_adaptor_core.WalletDecryptionNotAllowedError();
|
|
196
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.UponRequest:
|
|
197
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.AutoDecrypt:
|
|
198
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.OnChainHistory: {
|
|
199
|
+
try {
|
|
200
|
+
const text = await this._leoWallet.decrypt(
|
|
201
|
+
cipherText,
|
|
202
|
+
tpk,
|
|
203
|
+
programId,
|
|
204
|
+
functionName,
|
|
205
|
+
index
|
|
206
|
+
);
|
|
207
|
+
return text.text;
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw new import_aleo_wallet_adaptor_core.WalletDecryptionError(
|
|
210
|
+
error instanceof Error ? error.message : "Failed to decrypt"
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
default:
|
|
215
|
+
throw new import_aleo_wallet_adaptor_core.WalletDecryptionError();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Execute a transaction with Leo wallet
|
|
220
|
+
* @param options Transaction options
|
|
221
|
+
* @returns The executed temporary transaction ID
|
|
222
|
+
*/
|
|
223
|
+
async executeTransaction(options) {
|
|
224
|
+
if (!this._publicKey || !this.account) {
|
|
225
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const requestData = {
|
|
229
|
+
address: this._publicKey,
|
|
230
|
+
chainId: LEO_NETWORK_MAP[this.network],
|
|
231
|
+
fee: options.fee ? options.fee : 1e-3,
|
|
232
|
+
feePrivate: options.privateFee ?? false,
|
|
233
|
+
transitions: [
|
|
234
|
+
{
|
|
235
|
+
program: options.program,
|
|
236
|
+
functionName: options.function,
|
|
237
|
+
inputs: options.inputs
|
|
238
|
+
}
|
|
239
|
+
]
|
|
240
|
+
};
|
|
241
|
+
const result = await this._leoWallet?.requestTransaction(requestData);
|
|
242
|
+
if (!result?.transactionId) {
|
|
243
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not create transaction");
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
transactionId: result.transactionId
|
|
247
|
+
};
|
|
248
|
+
} catch (error) {
|
|
249
|
+
console.error("Leo Wallet executeTransaction error", error);
|
|
250
|
+
if (error instanceof import_aleo_wallet_adaptor_core.WalletError) {
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
254
|
+
error instanceof Error ? error.message : "Failed to execute transaction"
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Get transaction status
|
|
260
|
+
* @param transactionId The transaction ID
|
|
261
|
+
* @returns The transaction status
|
|
262
|
+
*/
|
|
263
|
+
async transactionStatus(transactionId) {
|
|
264
|
+
if (!this._publicKey || !this.account) {
|
|
265
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
const result = await this._leoWallet?.transactionStatus(transactionId);
|
|
269
|
+
if (!result?.status) {
|
|
270
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not get transaction status");
|
|
271
|
+
}
|
|
272
|
+
const leoStatus = result.status;
|
|
273
|
+
console.log("leoStatus", leoStatus);
|
|
274
|
+
const status = leoStatus === "Finalized" ? import_aleo_types2.TransactionStatus.ACCEPTED : leoStatus === "Completed" ? import_aleo_types2.TransactionStatus.PENDING : leoStatus;
|
|
275
|
+
return {
|
|
276
|
+
status
|
|
277
|
+
};
|
|
278
|
+
} catch (error) {
|
|
279
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
280
|
+
error instanceof Error ? error.message : "Failed to get transaction status"
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Request records from Leo wallet
|
|
286
|
+
* @param program The program to request records from
|
|
287
|
+
* @param includePlaintext Whether to include plaintext on each record
|
|
288
|
+
* @returns The records
|
|
289
|
+
*/
|
|
290
|
+
async requestRecords(program, includePlaintext) {
|
|
291
|
+
if (!this._publicKey || !this.account) {
|
|
292
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
const result = includePlaintext ? await this._leoWallet?.requestRecordPlaintexts(program) : await this._leoWallet?.requestRecords(program);
|
|
296
|
+
return result?.records || [];
|
|
297
|
+
} catch (error) {
|
|
298
|
+
throw new import_aleo_wallet_adaptor_core.WalletError(error instanceof Error ? error.message : "Failed to request records");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Switch the network
|
|
303
|
+
* @param network The network to switch to
|
|
304
|
+
*/
|
|
305
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
306
|
+
async switchNetwork(_network) {
|
|
307
|
+
console.error("Leo Wallet does not support switching networks");
|
|
308
|
+
throw new import_aleo_wallet_adaptor_core.MethodNotImplementedError("switchNetwork");
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Execute a deployment
|
|
312
|
+
* @param deployment The deployment to execute
|
|
313
|
+
* @returns The executed transaction ID
|
|
314
|
+
*/
|
|
315
|
+
async executeDeployment(deployment) {
|
|
316
|
+
try {
|
|
317
|
+
if (!this._publicKey || !this.account) {
|
|
318
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
const leoDeployment = new Deployment(
|
|
322
|
+
this._publicKey,
|
|
323
|
+
LEO_NETWORK_MAP[this.network],
|
|
324
|
+
deployment.program,
|
|
325
|
+
deployment.fee,
|
|
326
|
+
deployment.feePrivate
|
|
327
|
+
);
|
|
328
|
+
const result = await this._leoWallet?.requestDeploy(leoDeployment);
|
|
329
|
+
if (!result?.transactionId) {
|
|
330
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not create deployment");
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
transactionId: result.transactionId
|
|
334
|
+
};
|
|
335
|
+
} catch (error) {
|
|
336
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
337
|
+
error instanceof Error ? error.message : "Failed to execute deployment"
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
} catch (error) {
|
|
341
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
347
|
+
0 && (module.exports = {
|
|
348
|
+
Deployment,
|
|
349
|
+
LEO_NETWORK_MAP,
|
|
350
|
+
LeoWalletAdapter,
|
|
351
|
+
Transition
|
|
352
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// src/LeoWalletAdapter.ts
|
|
2
|
+
import {
|
|
3
|
+
Network as Network2,
|
|
4
|
+
TransactionStatus
|
|
5
|
+
} from "@provablehq/aleo-types";
|
|
6
|
+
import {
|
|
7
|
+
WalletDecryptPermission,
|
|
8
|
+
WalletReadyState
|
|
9
|
+
} from "@provablehq/aleo-wallet-standard";
|
|
10
|
+
import {
|
|
11
|
+
BaseAleoWalletAdapter,
|
|
12
|
+
MethodNotImplementedError,
|
|
13
|
+
WalletConnectionError,
|
|
14
|
+
WalletDecryptionNotAllowedError,
|
|
15
|
+
WalletDecryptionError,
|
|
16
|
+
WalletDisconnectionError,
|
|
17
|
+
WalletError,
|
|
18
|
+
WalletNotConnectedError,
|
|
19
|
+
WalletSignMessageError,
|
|
20
|
+
WalletTransactionError
|
|
21
|
+
} from "@provablehq/aleo-wallet-adaptor-core";
|
|
22
|
+
|
|
23
|
+
// src/types.ts
|
|
24
|
+
import { Network } from "@provablehq/aleo-types";
|
|
25
|
+
var LEO_NETWORK_MAP = {
|
|
26
|
+
[Network.MAINNET]: "mainnet",
|
|
27
|
+
[Network.TESTNET3]: "testnetbeta",
|
|
28
|
+
[Network.CANARY]: "testnetbeta"
|
|
29
|
+
};
|
|
30
|
+
var Transition = class {
|
|
31
|
+
constructor(program, functionName, inputs) {
|
|
32
|
+
this.program = program;
|
|
33
|
+
this.functionName = functionName;
|
|
34
|
+
this.inputs = inputs;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var Deployment = class {
|
|
38
|
+
constructor(address, chainId, program, fee, feePrivate = true) {
|
|
39
|
+
this.address = address;
|
|
40
|
+
this.chainId = chainId;
|
|
41
|
+
this.program = program;
|
|
42
|
+
this.fee = fee;
|
|
43
|
+
this.feePrivate = feePrivate;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/LeoWalletAdapter.ts
|
|
48
|
+
var LeoWalletAdapter = class extends BaseAleoWalletAdapter {
|
|
49
|
+
/**
|
|
50
|
+
* Create a new Leo wallet adapter
|
|
51
|
+
* @param config Adapter configuration
|
|
52
|
+
*/
|
|
53
|
+
constructor(config) {
|
|
54
|
+
super();
|
|
55
|
+
/**
|
|
56
|
+
* The wallet name
|
|
57
|
+
*/
|
|
58
|
+
this.name = "Leo Wallet";
|
|
59
|
+
/**
|
|
60
|
+
* The wallet URL
|
|
61
|
+
*/
|
|
62
|
+
this.url = "https://app.leo.app";
|
|
63
|
+
/**
|
|
64
|
+
* The wallet icon (base64-encoded SVG)
|
|
65
|
+
*/
|
|
66
|
+
this.icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAABcCAMAAADUMSJqAAAASFBMVEVjTP////9kTf9GIP/5+P9dRP+ajf9WO//Jwv9fR/9KJv+Ccf9ZP//08/+Xiv9aQf+upf/c2P+Qgf96aP9TNv/Nx/+Fdf+6sv91nL8+AAABCUlEQVRoge2YyQ6EIBBEccVWBHf//09H42FESNRJk+ik3s06vANCQ0oIAAAAf0ps48u8XHKXiYVZsyE5pbxgj8s63RPpTAhZROkZ9QV7nKSRRb7JT0kTyCGHHPInyceAcppyBz0zyQVlDlJzyT1k+XvkZKQ0FEROqmqKoqkU8ctV327fba+45ar7Jp3ilVO/j3pilat2H7WKU06VnVXEKDeNnTWGUX6cioV8izzosgT9ocetuKzKWw5R0OMfdnAtKyPDjVwR9LI48ny5lu6zgm0rztp9EE1cJ9THyDW4fLBNRcghhxxyDvmh+vOxybVdB16p/pzS0sewesz90vJGtfpD3QoAAOCNfADu9hzTpMe3fQAAAABJRU5ErkJggg==";
|
|
67
|
+
/**
|
|
68
|
+
* The wallet's decrypt permission
|
|
69
|
+
*/
|
|
70
|
+
this.decryptPermission = WalletDecryptPermission.NoDecrypt;
|
|
71
|
+
/**
|
|
72
|
+
* Public key
|
|
73
|
+
*/
|
|
74
|
+
this._publicKey = "";
|
|
75
|
+
this._readyState = typeof window === "undefined" || typeof document === "undefined" ? WalletReadyState.UNSUPPORTED : WalletReadyState.NOT_DETECTED;
|
|
76
|
+
console.debug("LeoWalletAdapter constructor", config);
|
|
77
|
+
this.network = Network2.TESTNET3;
|
|
78
|
+
this._checkAvailability();
|
|
79
|
+
this._leoWallet = this._window?.leoWallet || this._window?.leo;
|
|
80
|
+
if (config?.isMobile) {
|
|
81
|
+
this.url = `https://app.leo.app/browser?url=${config.mobileWebviewUrl}`;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Check if Leo wallet is available
|
|
86
|
+
*/
|
|
87
|
+
_checkAvailability() {
|
|
88
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
89
|
+
this.readyState = WalletReadyState.UNSUPPORTED;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
this._window = window;
|
|
93
|
+
if (this._window.leoWallet || this._window.leo) {
|
|
94
|
+
this.readyState = WalletReadyState.INSTALLED;
|
|
95
|
+
} else {
|
|
96
|
+
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
|
|
97
|
+
if (isMobile) {
|
|
98
|
+
this.readyState = WalletReadyState.LOADABLE;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Connect to Leo wallet
|
|
104
|
+
* @returns The connected account
|
|
105
|
+
*/
|
|
106
|
+
async connect(network, decryptPermission, programs) {
|
|
107
|
+
try {
|
|
108
|
+
if (this.readyState !== WalletReadyState.INSTALLED) {
|
|
109
|
+
throw new WalletConnectionError("Leo Wallet is not available");
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
await this._leoWallet?.connect(decryptPermission, LEO_NETWORK_MAP[network], programs);
|
|
113
|
+
this.network = network;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (error instanceof Object && "name" in error && (error.name === "InvalidParamsAleoWalletError" || error.name !== "NotGrantedAleoWalletError")) {
|
|
116
|
+
throw new WalletConnectionError(
|
|
117
|
+
"Connection failed: Likely due to a difference in configured network and the selected wallet network. Configured network: " + network
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
throw new WalletConnectionError(
|
|
121
|
+
error instanceof Error ? error.message : "Connection failed"
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
this._publicKey = this._leoWallet?.publicKey || "";
|
|
125
|
+
this.decryptPermission = decryptPermission;
|
|
126
|
+
if (!this._publicKey) {
|
|
127
|
+
throw new WalletConnectionError("No address returned from wallet");
|
|
128
|
+
}
|
|
129
|
+
const account = {
|
|
130
|
+
address: this._publicKey
|
|
131
|
+
};
|
|
132
|
+
this.account = account;
|
|
133
|
+
this.emit("connect", account);
|
|
134
|
+
return account;
|
|
135
|
+
} catch (err) {
|
|
136
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
137
|
+
throw new WalletConnectionError(err instanceof Error ? err.message : "Connection failed");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Disconnect from Leo wallet
|
|
142
|
+
*/
|
|
143
|
+
async disconnect() {
|
|
144
|
+
try {
|
|
145
|
+
await this._leoWallet?.disconnect();
|
|
146
|
+
this._publicKey = "";
|
|
147
|
+
this.account = void 0;
|
|
148
|
+
this.emit("disconnect");
|
|
149
|
+
} catch (err) {
|
|
150
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
151
|
+
throw new WalletDisconnectionError(
|
|
152
|
+
err instanceof Error ? err.message : "Disconnection failed"
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Sign a transaction with Leo wallet
|
|
158
|
+
* @param options Transaction options
|
|
159
|
+
* @returns The signed transaction
|
|
160
|
+
*/
|
|
161
|
+
async signMessage(message) {
|
|
162
|
+
if (!this._publicKey || !this.account) {
|
|
163
|
+
throw new WalletNotConnectedError();
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const signature = await this._leoWallet?.signMessage(message);
|
|
167
|
+
if (!signature) {
|
|
168
|
+
throw new WalletSignMessageError("Failed to sign message");
|
|
169
|
+
}
|
|
170
|
+
return signature.signature;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
throw new WalletSignMessageError(
|
|
173
|
+
error instanceof Error ? error.message : "Failed to sign message"
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async decrypt(cipherText, tpk, programId, functionName, index) {
|
|
178
|
+
if (!this._leoWallet || !this._publicKey) {
|
|
179
|
+
throw new WalletNotConnectedError();
|
|
180
|
+
}
|
|
181
|
+
switch (this.decryptPermission) {
|
|
182
|
+
case WalletDecryptPermission.NoDecrypt:
|
|
183
|
+
throw new WalletDecryptionNotAllowedError();
|
|
184
|
+
case WalletDecryptPermission.UponRequest:
|
|
185
|
+
case WalletDecryptPermission.AutoDecrypt:
|
|
186
|
+
case WalletDecryptPermission.OnChainHistory: {
|
|
187
|
+
try {
|
|
188
|
+
const text = await this._leoWallet.decrypt(
|
|
189
|
+
cipherText,
|
|
190
|
+
tpk,
|
|
191
|
+
programId,
|
|
192
|
+
functionName,
|
|
193
|
+
index
|
|
194
|
+
);
|
|
195
|
+
return text.text;
|
|
196
|
+
} catch (error) {
|
|
197
|
+
throw new WalletDecryptionError(
|
|
198
|
+
error instanceof Error ? error.message : "Failed to decrypt"
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
default:
|
|
203
|
+
throw new WalletDecryptionError();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Execute a transaction with Leo wallet
|
|
208
|
+
* @param options Transaction options
|
|
209
|
+
* @returns The executed temporary transaction ID
|
|
210
|
+
*/
|
|
211
|
+
async executeTransaction(options) {
|
|
212
|
+
if (!this._publicKey || !this.account) {
|
|
213
|
+
throw new WalletNotConnectedError();
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const requestData = {
|
|
217
|
+
address: this._publicKey,
|
|
218
|
+
chainId: LEO_NETWORK_MAP[this.network],
|
|
219
|
+
fee: options.fee ? options.fee : 1e-3,
|
|
220
|
+
feePrivate: options.privateFee ?? false,
|
|
221
|
+
transitions: [
|
|
222
|
+
{
|
|
223
|
+
program: options.program,
|
|
224
|
+
functionName: options.function,
|
|
225
|
+
inputs: options.inputs
|
|
226
|
+
}
|
|
227
|
+
]
|
|
228
|
+
};
|
|
229
|
+
const result = await this._leoWallet?.requestTransaction(requestData);
|
|
230
|
+
if (!result?.transactionId) {
|
|
231
|
+
throw new WalletTransactionError("Could not create transaction");
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
transactionId: result.transactionId
|
|
235
|
+
};
|
|
236
|
+
} catch (error) {
|
|
237
|
+
console.error("Leo Wallet executeTransaction error", error);
|
|
238
|
+
if (error instanceof WalletError) {
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
241
|
+
throw new WalletTransactionError(
|
|
242
|
+
error instanceof Error ? error.message : "Failed to execute transaction"
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Get transaction status
|
|
248
|
+
* @param transactionId The transaction ID
|
|
249
|
+
* @returns The transaction status
|
|
250
|
+
*/
|
|
251
|
+
async transactionStatus(transactionId) {
|
|
252
|
+
if (!this._publicKey || !this.account) {
|
|
253
|
+
throw new WalletNotConnectedError();
|
|
254
|
+
}
|
|
255
|
+
try {
|
|
256
|
+
const result = await this._leoWallet?.transactionStatus(transactionId);
|
|
257
|
+
if (!result?.status) {
|
|
258
|
+
throw new WalletTransactionError("Could not get transaction status");
|
|
259
|
+
}
|
|
260
|
+
const leoStatus = result.status;
|
|
261
|
+
console.log("leoStatus", leoStatus);
|
|
262
|
+
const status = leoStatus === "Finalized" ? TransactionStatus.ACCEPTED : leoStatus === "Completed" ? TransactionStatus.PENDING : leoStatus;
|
|
263
|
+
return {
|
|
264
|
+
status
|
|
265
|
+
};
|
|
266
|
+
} catch (error) {
|
|
267
|
+
throw new WalletTransactionError(
|
|
268
|
+
error instanceof Error ? error.message : "Failed to get transaction status"
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Request records from Leo wallet
|
|
274
|
+
* @param program The program to request records from
|
|
275
|
+
* @param includePlaintext Whether to include plaintext on each record
|
|
276
|
+
* @returns The records
|
|
277
|
+
*/
|
|
278
|
+
async requestRecords(program, includePlaintext) {
|
|
279
|
+
if (!this._publicKey || !this.account) {
|
|
280
|
+
throw new WalletNotConnectedError();
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const result = includePlaintext ? await this._leoWallet?.requestRecordPlaintexts(program) : await this._leoWallet?.requestRecords(program);
|
|
284
|
+
return result?.records || [];
|
|
285
|
+
} catch (error) {
|
|
286
|
+
throw new WalletError(error instanceof Error ? error.message : "Failed to request records");
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Switch the network
|
|
291
|
+
* @param network The network to switch to
|
|
292
|
+
*/
|
|
293
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
294
|
+
async switchNetwork(_network) {
|
|
295
|
+
console.error("Leo Wallet does not support switching networks");
|
|
296
|
+
throw new MethodNotImplementedError("switchNetwork");
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Execute a deployment
|
|
300
|
+
* @param deployment The deployment to execute
|
|
301
|
+
* @returns The executed transaction ID
|
|
302
|
+
*/
|
|
303
|
+
async executeDeployment(deployment) {
|
|
304
|
+
try {
|
|
305
|
+
if (!this._publicKey || !this.account) {
|
|
306
|
+
throw new WalletNotConnectedError();
|
|
307
|
+
}
|
|
308
|
+
try {
|
|
309
|
+
const leoDeployment = new Deployment(
|
|
310
|
+
this._publicKey,
|
|
311
|
+
LEO_NETWORK_MAP[this.network],
|
|
312
|
+
deployment.program,
|
|
313
|
+
deployment.fee,
|
|
314
|
+
deployment.feePrivate
|
|
315
|
+
);
|
|
316
|
+
const result = await this._leoWallet?.requestDeploy(leoDeployment);
|
|
317
|
+
if (!result?.transactionId) {
|
|
318
|
+
throw new WalletTransactionError("Could not create deployment");
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
transactionId: result.transactionId
|
|
322
|
+
};
|
|
323
|
+
} catch (error) {
|
|
324
|
+
throw new WalletTransactionError(
|
|
325
|
+
error instanceof Error ? error.message : "Failed to execute deployment"
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
} catch (error) {
|
|
329
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
330
|
+
throw error;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
export {
|
|
335
|
+
Deployment,
|
|
336
|
+
LEO_NETWORK_MAP,
|
|
337
|
+
LeoWalletAdapter,
|
|
338
|
+
Transition
|
|
339
|
+
};
|