@provablehq/aleo-wallet-adaptor-shield 0.3.0-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 +32 -0
- package/dist/index.d.mts +166 -0
- package/dist/index.d.ts +166 -0
- package/dist/index.js +396 -0
- package/dist/index.mjs +386 -0
- package/package.json +45 -0
- package/src/ShieldWalletAdapter.ts +463 -0
- package/src/index.ts +2 -0
- package/src/types.ts +50 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
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
|
+
ShieldWalletAdapter: () => ShieldWalletAdapter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/ShieldWalletAdapter.ts
|
|
28
|
+
var import_aleo_types = require("@provablehq/aleo-types");
|
|
29
|
+
var import_aleo_wallet_standard = require("@provablehq/aleo-wallet-standard");
|
|
30
|
+
var import_aleo_wallet_adaptor_core = require("@provablehq/aleo-wallet-adaptor-core");
|
|
31
|
+
var ShieldWalletAdapter = class extends import_aleo_wallet_adaptor_core.BaseAleoWalletAdapter {
|
|
32
|
+
/**
|
|
33
|
+
* Create a new Shield wallet adapter
|
|
34
|
+
* @param config Adapter configuration
|
|
35
|
+
*/
|
|
36
|
+
constructor(config) {
|
|
37
|
+
super();
|
|
38
|
+
/**
|
|
39
|
+
* The wallet name
|
|
40
|
+
*/
|
|
41
|
+
this.name = "Shield Wallet";
|
|
42
|
+
/**
|
|
43
|
+
* The wallet URL
|
|
44
|
+
*/
|
|
45
|
+
this.url = "https://provable.com/";
|
|
46
|
+
/**
|
|
47
|
+
* The wallet icon (base64-encoded SVG)
|
|
48
|
+
*/
|
|
49
|
+
this.icon = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAyNCIgaGVpZ2h0PSIxMDI0IiB2aWV3Qm94PSIwIDAgMTAyNCAxMDI0IiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cmVjdCB3aWR0aD0iMTAyNCIgaGVpZ2h0PSIxMDI0IiBmaWxsPSIjMDkwNzA3Ii8+CjxwYXRoIGQ9Ik01MTIgODcxQzcxMC4yNyA4NzEgODcxIDcxMC4yNyA4NzEgNTEyQzg3MSA1MDUuMTI0IDg3MC44MDcgNDk4LjI5MyA4NzAuNDI1IDQ5MS41MTJDNzQ2LjQzIDYyNy4wNTggNDYxLjk5NCA3NjIuNDcgMzE0LjM5OSA4MTAuNjY0TDMxMi42ODQgODEwLjYzM0MzNjkuNzA0IDg0OC43NjUgNDM4LjI1NSA4NzEgNTEyIDg3MVoiIGZpbGw9IndoaXRlIi8+CjxwYXRoIGQ9Ik0yNTAuNDA4IDc1Ny44NjhDMTkwLjAwNiA2OTMuNjI4IDE1MyA2MDcuMTM2IDE1MyA1MTJDMTUzIDMxMy43MyAzMTMuNzMgMTUzIDUxMiAxNTNDNjkyLjk5MyAxNTMgODQyLjcwMyAyODYuOTM4IDg2Ny40MiA0NjEuMTA1QzYxMS4yMTIgNjM4LjgyMyAzNzcuNjU5IDcxOC42OCAyNTEuMDQ2IDc1Ny44NjhIMjUwLjQwOFoiIGZpbGw9IndoaXRlIi8+Cjwvc3ZnPgo=";
|
|
50
|
+
/**
|
|
51
|
+
* The wallet's decrypt permission
|
|
52
|
+
*/
|
|
53
|
+
this.decryptPermission = import_aleo_wallet_standard.WalletDecryptPermission.NoDecrypt;
|
|
54
|
+
/**
|
|
55
|
+
* Public key
|
|
56
|
+
*/
|
|
57
|
+
this._publicKey = "";
|
|
58
|
+
this._readyState = typeof window === "undefined" || typeof document === "undefined" ? import_aleo_wallet_standard.WalletReadyState.UNSUPPORTED : import_aleo_wallet_standard.WalletReadyState.NOT_DETECTED;
|
|
59
|
+
/**
|
|
60
|
+
* EVENTS HANDLING
|
|
61
|
+
*/
|
|
62
|
+
// Network change listener
|
|
63
|
+
this._onNetworkChange = (network) => {
|
|
64
|
+
console.debug("Shield Wallet network changed to: ", network);
|
|
65
|
+
this.network = network;
|
|
66
|
+
this.emit("networkChange", network);
|
|
67
|
+
};
|
|
68
|
+
// Account change listener
|
|
69
|
+
this._onAccountChange = () => {
|
|
70
|
+
console.debug("Shield Wallet account change detected \u2013 reauthorization required");
|
|
71
|
+
this._publicKey = "";
|
|
72
|
+
this.account = void 0;
|
|
73
|
+
this.emit("accountChange");
|
|
74
|
+
};
|
|
75
|
+
// Disconnect listener
|
|
76
|
+
this._onDisconnect = () => {
|
|
77
|
+
console.debug("Shield Wallet disconnected");
|
|
78
|
+
this._cleanupListeners();
|
|
79
|
+
this._publicKey = "";
|
|
80
|
+
this.account = void 0;
|
|
81
|
+
this.emit("disconnect");
|
|
82
|
+
};
|
|
83
|
+
console.debug("ShieldWalletAdapter constructor", config);
|
|
84
|
+
this.network = import_aleo_types.Network.TESTNET3;
|
|
85
|
+
if (this._readyState !== import_aleo_wallet_standard.WalletReadyState.UNSUPPORTED) {
|
|
86
|
+
(0, import_aleo_wallet_adaptor_core.scopePollingDetectionStrategy)(() => this._checkAvailability());
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Check if Shield wallet is available
|
|
91
|
+
*/
|
|
92
|
+
_checkAvailability() {
|
|
93
|
+
this._window = window;
|
|
94
|
+
if (this._window.shield) {
|
|
95
|
+
this.readyState = import_aleo_wallet_standard.WalletReadyState.INSTALLED;
|
|
96
|
+
this._shieldWallet = this._window?.shield;
|
|
97
|
+
return true;
|
|
98
|
+
} else {
|
|
99
|
+
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
|
|
100
|
+
if (isMobile) {
|
|
101
|
+
this.readyState = import_aleo_wallet_standard.WalletReadyState.LOADABLE;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Connect to Shield wallet
|
|
109
|
+
* @returns The connected account
|
|
110
|
+
*/
|
|
111
|
+
async connect(network, decryptPermission, programs) {
|
|
112
|
+
try {
|
|
113
|
+
if (this.readyState !== import_aleo_wallet_standard.WalletReadyState.INSTALLED) {
|
|
114
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError("Shield Wallet is not available");
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
const connectResult = await this._shieldWallet?.connect(
|
|
118
|
+
network,
|
|
119
|
+
decryptPermission,
|
|
120
|
+
programs
|
|
121
|
+
);
|
|
122
|
+
this._publicKey = connectResult?.address || "";
|
|
123
|
+
this._onNetworkChange(network);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError(
|
|
126
|
+
error instanceof Error ? error.message : "Connection failed"
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (!this._publicKey) {
|
|
130
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError("No address returned from wallet");
|
|
131
|
+
}
|
|
132
|
+
this._setupListeners();
|
|
133
|
+
const account = {
|
|
134
|
+
address: this._publicKey
|
|
135
|
+
};
|
|
136
|
+
this.account = account;
|
|
137
|
+
this.decryptPermission = decryptPermission;
|
|
138
|
+
this.emit("connect", account);
|
|
139
|
+
return account;
|
|
140
|
+
} catch (err) {
|
|
141
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
142
|
+
throw new import_aleo_wallet_adaptor_core.WalletConnectionError(err instanceof Error ? err.message : "Connection failed");
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Disconnect from Shield wallet
|
|
147
|
+
*/
|
|
148
|
+
async disconnect() {
|
|
149
|
+
try {
|
|
150
|
+
this._cleanupListeners();
|
|
151
|
+
await this._shieldWallet?.disconnect();
|
|
152
|
+
this._onDisconnect();
|
|
153
|
+
} catch (err) {
|
|
154
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
155
|
+
throw new import_aleo_wallet_adaptor_core.WalletDisconnectionError(
|
|
156
|
+
err instanceof Error ? err.message : "Disconnection failed"
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Sign a transaction with Shield wallet
|
|
162
|
+
* @param message The message to sign
|
|
163
|
+
* @returns The signed message
|
|
164
|
+
*/
|
|
165
|
+
async signMessage(message) {
|
|
166
|
+
if (!this._publicKey || !this.account) {
|
|
167
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
const signature = await this._shieldWallet?.signMessage(message);
|
|
171
|
+
if (!signature) {
|
|
172
|
+
throw new import_aleo_wallet_adaptor_core.WalletSignMessageError("Failed to sign message");
|
|
173
|
+
}
|
|
174
|
+
return signature;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
throw new import_aleo_wallet_adaptor_core.WalletSignMessageError(
|
|
177
|
+
error instanceof Error ? error.message : "Failed to sign message"
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async decrypt(cipherText) {
|
|
182
|
+
if (!this._shieldWallet || !this._publicKey) {
|
|
183
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
184
|
+
}
|
|
185
|
+
switch (this.decryptPermission) {
|
|
186
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.NoDecrypt:
|
|
187
|
+
throw new import_aleo_wallet_adaptor_core.WalletDecryptionNotAllowedError();
|
|
188
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.UponRequest:
|
|
189
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.AutoDecrypt:
|
|
190
|
+
case import_aleo_wallet_standard.WalletDecryptPermission.OnChainHistory: {
|
|
191
|
+
try {
|
|
192
|
+
return await this._shieldWallet.decrypt(cipherText);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
throw new import_aleo_wallet_adaptor_core.WalletDecryptionError(
|
|
195
|
+
error instanceof Error ? error.message : "Failed to decrypt"
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
default:
|
|
200
|
+
throw new import_aleo_wallet_adaptor_core.WalletDecryptionError();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Execute a transaction with Shield wallet
|
|
205
|
+
* @param options Transaction options
|
|
206
|
+
* @returns The executed temporary transaction ID
|
|
207
|
+
*/
|
|
208
|
+
async executeTransaction(options) {
|
|
209
|
+
if (!this._publicKey || !this.account) {
|
|
210
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
const result = await this._shieldWallet?.executeTransaction({
|
|
214
|
+
...options,
|
|
215
|
+
network: this.network
|
|
216
|
+
});
|
|
217
|
+
if (!result?.transactionId) {
|
|
218
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not create transaction");
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
transactionId: result.transactionId
|
|
222
|
+
};
|
|
223
|
+
} catch (error) {
|
|
224
|
+
console.error("ShieldWalletAdapter executeTransaction error", error);
|
|
225
|
+
if (error instanceof import_aleo_wallet_adaptor_core.WalletError) {
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
229
|
+
error instanceof Error ? error.message : "Failed to execute transaction"
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Get transaction status
|
|
235
|
+
* @param transactionId The transaction ID
|
|
236
|
+
* @returns The transaction status
|
|
237
|
+
*/
|
|
238
|
+
async transactionStatus(transactionId) {
|
|
239
|
+
if (!this._publicKey || !this.account) {
|
|
240
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
const result = await this._shieldWallet?.transactionStatus(transactionId);
|
|
244
|
+
if (!result?.status) {
|
|
245
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not get transaction status");
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
} catch (error) {
|
|
249
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
250
|
+
error instanceof Error ? error.message : "Failed to get transaction status"
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Switch the network
|
|
256
|
+
* @param network The network to switch to
|
|
257
|
+
*/
|
|
258
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
259
|
+
async switchNetwork(_network) {
|
|
260
|
+
if (!this._publicKey || !this.account) {
|
|
261
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
await this._shieldWallet?.switchNetwork(_network);
|
|
265
|
+
this._onNetworkChange(_network);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
throw new import_aleo_wallet_adaptor_core.WalletSwitchNetworkError(
|
|
268
|
+
error instanceof Error ? error.message : "Failed to switch network"
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Request records from Shield 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 import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const result = await this._shieldWallet?.requestRecords(program, includePlaintext);
|
|
284
|
+
return result || [];
|
|
285
|
+
} catch (error) {
|
|
286
|
+
throw new import_aleo_wallet_adaptor_core.WalletError(error instanceof Error ? error.message : "Failed to request records");
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Execute a deployment
|
|
291
|
+
* @param deployment The deployment to execute
|
|
292
|
+
* @returns The executed transaction ID
|
|
293
|
+
*/
|
|
294
|
+
async executeDeployment(deployment) {
|
|
295
|
+
try {
|
|
296
|
+
if (!this._publicKey || !this.account) {
|
|
297
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
const result = await this._shieldWallet?.executeDeployment({
|
|
301
|
+
...deployment,
|
|
302
|
+
network: this.network
|
|
303
|
+
});
|
|
304
|
+
if (!result?.transactionId) {
|
|
305
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not create deployment");
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
transactionId: result.transactionId
|
|
309
|
+
};
|
|
310
|
+
} catch (error) {
|
|
311
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
312
|
+
error instanceof Error ? error.message : "Failed to execute deployment"
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
} catch (error) {
|
|
316
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
317
|
+
throw error;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* get transition view keys(tvk) for a transaction
|
|
322
|
+
* @param transactionId The transaction ID
|
|
323
|
+
* @returns The tvk array
|
|
324
|
+
*/
|
|
325
|
+
async transitionViewKeys(transactionId) {
|
|
326
|
+
try {
|
|
327
|
+
if (!this._publicKey || !this.account) {
|
|
328
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
329
|
+
}
|
|
330
|
+
try {
|
|
331
|
+
const result = await this._shieldWallet?.transitionViewKeys(transactionId);
|
|
332
|
+
if (!Array.isArray(result)) {
|
|
333
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not get transitionViewKeys");
|
|
334
|
+
}
|
|
335
|
+
return result;
|
|
336
|
+
} catch (error) {
|
|
337
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
338
|
+
error instanceof Error ? error.message : "Failed to get transitionViewKeys"
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
} catch (error) {
|
|
342
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* get transaction of specific program
|
|
348
|
+
* @param program The program ID
|
|
349
|
+
* @returns array of transactionId
|
|
350
|
+
*/
|
|
351
|
+
async requestTransactionHistory(program) {
|
|
352
|
+
try {
|
|
353
|
+
if (!this._publicKey || !this.account) {
|
|
354
|
+
throw new import_aleo_wallet_adaptor_core.WalletNotConnectedError();
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const result = await this._shieldWallet?.requestTransactionHistory(program);
|
|
358
|
+
if (!result?.transactions || !Array.isArray(result.transactions)) {
|
|
359
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError("Could not get TransactionHistory");
|
|
360
|
+
}
|
|
361
|
+
return result;
|
|
362
|
+
} catch (error) {
|
|
363
|
+
throw new import_aleo_wallet_adaptor_core.WalletTransactionError(
|
|
364
|
+
error instanceof Error ? error.message : "Failed to get transitionViewKeys"
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
} catch (error) {
|
|
368
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
369
|
+
throw error;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Set up event listeners with structured approach
|
|
374
|
+
*/
|
|
375
|
+
_setupListeners() {
|
|
376
|
+
if (!this._shieldWallet)
|
|
377
|
+
return;
|
|
378
|
+
this._shieldWallet.on("networkChanged", this._onNetworkChange);
|
|
379
|
+
this._shieldWallet.on("disconnect", this._onDisconnect);
|
|
380
|
+
this._shieldWallet.on("accountChanged", this._onAccountChange);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Clean up all event listeners
|
|
384
|
+
*/
|
|
385
|
+
_cleanupListeners() {
|
|
386
|
+
if (!this._shieldWallet)
|
|
387
|
+
return;
|
|
388
|
+
this._shieldWallet.off("networkChanged", this._onNetworkChange);
|
|
389
|
+
this._shieldWallet.off("disconnect", this._onDisconnect);
|
|
390
|
+
this._shieldWallet.off("accountChanged", this._onAccountChange);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
394
|
+
0 && (module.exports = {
|
|
395
|
+
ShieldWalletAdapter
|
|
396
|
+
});
|