unwallet-client-sdk 0.18.2 → 0.19.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/dist/index.cjs +293 -411
- package/dist/index.d.cts +80 -75
- package/dist/index.d.ts +80 -75
- package/dist/index.js +291 -388
- package/package.json +14 -9
package/dist/index.cjs
CHANGED
|
@@ -1,423 +1,305 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let viem = require("viem");
|
|
3
|
+
let zod = require("zod");
|
|
4
|
+
//#region src/error.ts
|
|
5
|
+
var UWError = class extends Error {
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message !== void 0 ? `${code}: ${message}` : code);
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
static {
|
|
11
|
+
this.prototype.name = "UWError";
|
|
12
|
+
}
|
|
9
13
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/config.ts
|
|
16
|
+
const UNWALLET_CONFIG_PROD = {
|
|
17
|
+
frontend: { baseURL: "https://id.unwallet.world" },
|
|
18
|
+
xapi: {
|
|
19
|
+
url: "wss://xapi.id.unwallet.world",
|
|
20
|
+
connectionTimeout: 1e4
|
|
21
|
+
}
|
|
17
22
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
UnWallet: () => UnWallet
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
|
|
28
|
-
// src/error.ts
|
|
29
|
-
var _UWError = class _UWError extends Error {
|
|
30
|
-
constructor(code, message) {
|
|
31
|
-
super(message !== void 0 ? `${code}: ${message}` : code);
|
|
32
|
-
this.code = code;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
_UWError.prototype.name = "UWError";
|
|
36
|
-
var UWError = _UWError;
|
|
37
|
-
|
|
38
|
-
// src/unwallet.ts
|
|
39
|
-
var import_viem = require("viem");
|
|
40
|
-
|
|
41
|
-
// src/config.ts
|
|
42
|
-
var UNWALLET_CONFIG_PROD = {
|
|
43
|
-
frontend: {
|
|
44
|
-
baseURL: "https://id.unwallet.world"
|
|
45
|
-
},
|
|
46
|
-
xapi: {
|
|
47
|
-
url: "wss://xapi.id.unwallet.world",
|
|
48
|
-
connectionTimeout: 1e4
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
var UNWALLET_CONFIG_DEV = {
|
|
52
|
-
frontend: {
|
|
53
|
-
baseURL: "http://localhost:4200"
|
|
54
|
-
},
|
|
55
|
-
xapi: {
|
|
56
|
-
url: "wss://xapi.id.test.unwallet.dev",
|
|
57
|
-
connectionTimeout: 1e4
|
|
58
|
-
}
|
|
23
|
+
const UNWALLET_CONFIG_DEV = {
|
|
24
|
+
frontend: { baseURL: "http://localhost:4200" },
|
|
25
|
+
xapi: {
|
|
26
|
+
url: "wss://xapi.id.test.unwallet.dev",
|
|
27
|
+
connectionTimeout: 1e4
|
|
28
|
+
}
|
|
59
29
|
};
|
|
60
30
|
function getUnWalletConfigByEnv(env) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return UNWALLET_CONFIG_DEV;
|
|
66
|
-
}
|
|
31
|
+
switch (env) {
|
|
32
|
+
case "prod": return UNWALLET_CONFIG_PROD;
|
|
33
|
+
case "dev": return UNWALLET_CONFIG_DEV;
|
|
34
|
+
}
|
|
67
35
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"error"
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/x.ts
|
|
38
|
+
const VALID_X_RESPONSE_TYPES = [
|
|
39
|
+
"connectionID",
|
|
40
|
+
"signature",
|
|
41
|
+
"transactionID",
|
|
42
|
+
"error"
|
|
76
43
|
];
|
|
77
|
-
var XConnection = class
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
resp = result.data;
|
|
154
|
-
}
|
|
155
|
-
if (resp.type !== "connectionID") {
|
|
156
|
-
reject(
|
|
157
|
-
new UWError(
|
|
158
|
-
"INVALID_RESPONSE",
|
|
159
|
-
`unexpected response type: ${resp.type}`
|
|
160
|
-
)
|
|
161
|
-
);
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
resolve(resp.value);
|
|
165
|
-
};
|
|
166
|
-
socket.onerror = () => reject(new UWError("CONNECTION_FAILED"));
|
|
167
|
-
socket.onclose = (event) => reject(new UWError("CONNECTION_CLOSED", event.reason));
|
|
168
|
-
});
|
|
169
|
-
return new _XConnection({ socket, id });
|
|
170
|
-
}
|
|
44
|
+
var XConnection = class XConnection {
|
|
45
|
+
constructor(args) {
|
|
46
|
+
this.responseHandler = null;
|
|
47
|
+
this.socket = args.socket;
|
|
48
|
+
this.id = args.id;
|
|
49
|
+
this.initListeners();
|
|
50
|
+
}
|
|
51
|
+
initListeners() {
|
|
52
|
+
this.socket.onopen = null;
|
|
53
|
+
this.socket.onmessage = (event) => {
|
|
54
|
+
if (this.responseHandler === null) return;
|
|
55
|
+
let resp;
|
|
56
|
+
{
|
|
57
|
+
const result = safeParseMessageEventDataToXResponse(event.data);
|
|
58
|
+
if (!result.success) {
|
|
59
|
+
this.responseHandler.reject(result.error);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
resp = result.data;
|
|
63
|
+
}
|
|
64
|
+
switch (resp.type) {
|
|
65
|
+
case "error":
|
|
66
|
+
switch (resp.value) {
|
|
67
|
+
case "rejected":
|
|
68
|
+
this.responseHandler.reject(new UWError("REQUEST_REJECTED"));
|
|
69
|
+
break;
|
|
70
|
+
default: this.responseHandler.reject(new UWError("INVALID_RESPONSE", `unexpected error response value: ${resp.value}`));
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
default: this.responseHandler.resolve(resp);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
this.socket.onerror = null;
|
|
77
|
+
this.socket.onclose = (event) => {
|
|
78
|
+
if (this.responseHandler === null) return;
|
|
79
|
+
this.responseHandler.reject(new UWError("CONNECTION_CLOSED", event.reason));
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
get readyState() {
|
|
83
|
+
return this.socket.readyState;
|
|
84
|
+
}
|
|
85
|
+
get hasResponseHandler() {
|
|
86
|
+
return this.responseHandler !== null;
|
|
87
|
+
}
|
|
88
|
+
setResponseHandler(handler) {
|
|
89
|
+
this.responseHandler = handler;
|
|
90
|
+
}
|
|
91
|
+
static async init(config) {
|
|
92
|
+
const socket = new WebSocket(config.url);
|
|
93
|
+
return new XConnection({
|
|
94
|
+
socket,
|
|
95
|
+
id: await new Promise((resolve, reject) => {
|
|
96
|
+
setInterval(() => reject(new UWError("CONNECTION_TIMEOUT")), config.connectionTimeout);
|
|
97
|
+
socket.onopen = () => socket.send(JSON.stringify({ action: "getConnectionID" }));
|
|
98
|
+
socket.onmessage = (event) => {
|
|
99
|
+
let resp;
|
|
100
|
+
{
|
|
101
|
+
const result = safeParseMessageEventDataToXResponse(event.data);
|
|
102
|
+
if (!result.success) {
|
|
103
|
+
reject(result.error);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
resp = result.data;
|
|
107
|
+
}
|
|
108
|
+
if (resp.type !== "connectionID") {
|
|
109
|
+
reject(new UWError("INVALID_RESPONSE", `unexpected response type: ${resp.type}`));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
resolve(resp.value);
|
|
113
|
+
};
|
|
114
|
+
socket.onerror = () => reject(new UWError("CONNECTION_FAILED"));
|
|
115
|
+
socket.onclose = (event) => reject(new UWError("CONNECTION_CLOSED", event.reason));
|
|
116
|
+
})
|
|
117
|
+
});
|
|
118
|
+
}
|
|
171
119
|
};
|
|
172
120
|
function safeParseMessageEventDataToXResponse(data) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
error: new UWError(
|
|
196
|
-
"INVALID_RESPONSE",
|
|
197
|
-
`invalid message event data: ${result.error.message}`
|
|
198
|
-
)
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
return {
|
|
202
|
-
success: true,
|
|
203
|
-
data: result.data
|
|
204
|
-
};
|
|
121
|
+
const result = zod.z.string().refine((val) => {
|
|
122
|
+
try {
|
|
123
|
+
JSON.parse(val);
|
|
124
|
+
} catch (e) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}, {
|
|
129
|
+
abort: true,
|
|
130
|
+
message: "Invalid JSON string"
|
|
131
|
+
}).transform((val) => JSON.parse(val)).pipe(zod.z.object({
|
|
132
|
+
type: zod.z.enum(VALID_X_RESPONSE_TYPES),
|
|
133
|
+
value: zod.z.string()
|
|
134
|
+
})).safeParse(data);
|
|
135
|
+
if (!result.success) return {
|
|
136
|
+
success: false,
|
|
137
|
+
error: new UWError("INVALID_RESPONSE", `invalid message event data: ${result.error.message}`)
|
|
138
|
+
};
|
|
139
|
+
return {
|
|
140
|
+
success: true,
|
|
141
|
+
data: result.data
|
|
142
|
+
};
|
|
205
143
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
var UnWallet = class
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
reject(new UWError("REQUEST_IN_PROGRESS"));
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
if (args.value === void 0 && args.data === void 0) {
|
|
362
|
-
reject(
|
|
363
|
-
new UWError("INVALID_REQUEST", "either value or data is required")
|
|
364
|
-
);
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
this.xConnection.setResponseHandler({
|
|
368
|
-
resolve: (resp) => {
|
|
369
|
-
this.xConnection.setResponseHandler(null);
|
|
370
|
-
if (resp.type !== "transactionID") {
|
|
371
|
-
reject(
|
|
372
|
-
new UWError(
|
|
373
|
-
"INVALID_RESPONSE",
|
|
374
|
-
`unexpected response type: ${resp.type}`
|
|
375
|
-
)
|
|
376
|
-
);
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
resolve({ transactionID: resp.value });
|
|
380
|
-
},
|
|
381
|
-
reject: (err) => {
|
|
382
|
-
this.xConnection.setResponseHandler(null);
|
|
383
|
-
reject(err);
|
|
384
|
-
}
|
|
385
|
-
});
|
|
386
|
-
const url = new URL(
|
|
387
|
-
`${this.unWalletConfig.frontend.baseURL}/x/sendTransaction`
|
|
388
|
-
);
|
|
389
|
-
{
|
|
390
|
-
url.searchParams.set("connectionID", this.xConnection.id);
|
|
391
|
-
url.searchParams.set("clientID", this.clientID);
|
|
392
|
-
url.searchParams.set("chainID", args.chainID.toString());
|
|
393
|
-
url.searchParams.set("toAddress", args.toAddress);
|
|
394
|
-
url.searchParams.set(
|
|
395
|
-
"value",
|
|
396
|
-
args.value !== void 0 ? args.value : "0x0"
|
|
397
|
-
);
|
|
398
|
-
url.searchParams.set(
|
|
399
|
-
"data",
|
|
400
|
-
args.data !== void 0 ? args.data : "0x"
|
|
401
|
-
);
|
|
402
|
-
url.searchParams.set("ticketToken", args.ticketToken);
|
|
403
|
-
}
|
|
404
|
-
openWindow(url);
|
|
405
|
-
});
|
|
406
|
-
}
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/unwallet.ts
|
|
146
|
+
var UnWallet = class UnWallet {
|
|
147
|
+
constructor(args) {
|
|
148
|
+
this.env = args.env;
|
|
149
|
+
this.clientID = args.clientID;
|
|
150
|
+
this.xConnection = args.xConnection;
|
|
151
|
+
}
|
|
152
|
+
static async init(config) {
|
|
153
|
+
const env = config.env !== void 0 ? config.env : "prod";
|
|
154
|
+
return new UnWallet({
|
|
155
|
+
env,
|
|
156
|
+
clientID: config.clientID,
|
|
157
|
+
xConnection: await XConnection.init(getUnWalletConfigByEnv(env).xapi)
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
get unWalletConfig() {
|
|
161
|
+
return getUnWalletConfigByEnv(this.env);
|
|
162
|
+
}
|
|
163
|
+
authorize(args) {
|
|
164
|
+
const url = new URL(`${this.unWalletConfig.frontend.baseURL}/${args.isVirtual !== void 0 ? args.isVirtual ? "v" : "" : "v"}authorize`);
|
|
165
|
+
url.searchParams.set("response_type", "id_token");
|
|
166
|
+
url.searchParams.set("response_mode", args.responseMode !== void 0 ? args.responseMode : "fragment");
|
|
167
|
+
url.searchParams.set("client_id", this.clientID);
|
|
168
|
+
url.searchParams.set("scope", "openid");
|
|
169
|
+
url.searchParams.set("redirect_uri", args.redirectURL);
|
|
170
|
+
if (args.nonce !== void 0) url.searchParams.set("nonce", args.nonce);
|
|
171
|
+
if (args.chainID !== void 0) url.searchParams.set("chain_id", args.chainID.toString());
|
|
172
|
+
location.assign(url);
|
|
173
|
+
}
|
|
174
|
+
sign(args) {
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
if (this.xConnection.readyState !== WebSocket.OPEN) {
|
|
177
|
+
reject(new UWError("CONNECTION_NOT_OPENED"));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (this.xConnection.hasResponseHandler) {
|
|
181
|
+
reject(new UWError("REQUEST_IN_PROGRESS"));
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const digest = (0, viem.sha256)((0, viem.toBytes)(args.message));
|
|
185
|
+
this.xConnection.setResponseHandler({
|
|
186
|
+
resolve: (resp) => {
|
|
187
|
+
this.xConnection.setResponseHandler(null);
|
|
188
|
+
if (resp.type !== "signature") {
|
|
189
|
+
reject(new UWError("INVALID_RESPONSE", `unexpected response type: ${resp.type}`));
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
resolve({
|
|
193
|
+
digest,
|
|
194
|
+
signature: resp.value
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
reject: (err) => {
|
|
198
|
+
this.xConnection.setResponseHandler(null);
|
|
199
|
+
reject(err);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
const url = new URL(`${this.unWalletConfig.frontend.baseURL}/x/sign`);
|
|
203
|
+
url.searchParams.set("connectionID", this.xConnection.id);
|
|
204
|
+
url.searchParams.set("clientID", this.clientID);
|
|
205
|
+
url.searchParams.set("message", args.message);
|
|
206
|
+
url.searchParams.set("ticketToken", args.ticketToken);
|
|
207
|
+
openWindow(url);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
signEIP712TypedData(args) {
|
|
211
|
+
return new Promise((resolve, reject) => {
|
|
212
|
+
if (this.xConnection.readyState !== WebSocket.OPEN) {
|
|
213
|
+
reject(new UWError("CONNECTION_NOT_OPENED"));
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (this.xConnection.hasResponseHandler) {
|
|
217
|
+
reject(new UWError("REQUEST_IN_PROGRESS"));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const { EIP712Domain: _, ...typedDataTypes } = args.typedData.types;
|
|
221
|
+
const typedData = {
|
|
222
|
+
...args.typedData,
|
|
223
|
+
types: typedDataTypes
|
|
224
|
+
};
|
|
225
|
+
try {
|
|
226
|
+
(0, viem.validateTypedData)(typedData);
|
|
227
|
+
} catch (e) {
|
|
228
|
+
reject(new UWError("INVALID_REQUEST", `invalid typed data: ${e.message}`));
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const digest = (0, viem.hashTypedData)(typedData);
|
|
232
|
+
this.xConnection.setResponseHandler({
|
|
233
|
+
resolve: (resp) => {
|
|
234
|
+
this.xConnection.setResponseHandler(null);
|
|
235
|
+
if (resp.type !== "signature") {
|
|
236
|
+
reject(new UWError("INVALID_RESPONSE", `unexpected response type: ${resp.type}`));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
resolve({
|
|
240
|
+
digest,
|
|
241
|
+
signature: resp.value
|
|
242
|
+
});
|
|
243
|
+
},
|
|
244
|
+
reject: (err) => {
|
|
245
|
+
this.xConnection.setResponseHandler(null);
|
|
246
|
+
reject(err);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
const url = new URL(`${this.unWalletConfig.frontend.baseURL}/x/signEIP712TypedData`);
|
|
250
|
+
url.searchParams.set("connectionID", this.xConnection.id);
|
|
251
|
+
url.searchParams.set("clientID", this.clientID);
|
|
252
|
+
url.searchParams.set("typedData", JSON.stringify(args.typedData));
|
|
253
|
+
url.searchParams.set("ticketToken", args.ticketToken);
|
|
254
|
+
openWindow(url);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
sendTransaction(args) {
|
|
258
|
+
return new Promise((resolve, reject) => {
|
|
259
|
+
if (this.xConnection.readyState !== WebSocket.OPEN) {
|
|
260
|
+
reject(new UWError("CONNECTION_NOT_OPENED"));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (this.xConnection.hasResponseHandler) {
|
|
264
|
+
reject(new UWError("REQUEST_IN_PROGRESS"));
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (args.value === void 0 && args.data === void 0) {
|
|
268
|
+
reject(new UWError("INVALID_REQUEST", "either value or data is required"));
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
this.xConnection.setResponseHandler({
|
|
272
|
+
resolve: (resp) => {
|
|
273
|
+
this.xConnection.setResponseHandler(null);
|
|
274
|
+
if (resp.type !== "transactionID") {
|
|
275
|
+
reject(new UWError("INVALID_RESPONSE", `unexpected response type: ${resp.type}`));
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
resolve({ transactionID: resp.value });
|
|
279
|
+
},
|
|
280
|
+
reject: (err) => {
|
|
281
|
+
this.xConnection.setResponseHandler(null);
|
|
282
|
+
reject(err);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
const url = new URL(`${this.unWalletConfig.frontend.baseURL}/x/sendTransaction`);
|
|
286
|
+
url.searchParams.set("connectionID", this.xConnection.id);
|
|
287
|
+
url.searchParams.set("clientID", this.clientID);
|
|
288
|
+
url.searchParams.set("chainID", args.chainID.toString());
|
|
289
|
+
url.searchParams.set("toAddress", args.toAddress);
|
|
290
|
+
url.searchParams.set("value", args.value !== void 0 ? args.value : "0x0");
|
|
291
|
+
url.searchParams.set("data", args.data !== void 0 ? args.data : "0x");
|
|
292
|
+
url.searchParams.set("ticketToken", args.ticketToken);
|
|
293
|
+
openWindow(url);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
407
296
|
};
|
|
408
297
|
function openWindow(url) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
window.open(
|
|
414
|
-
url.toString(),
|
|
415
|
-
"_blank",
|
|
416
|
-
`width=${width},height=${height},left=${left},top=${top}`
|
|
417
|
-
);
|
|
298
|
+
const width = screen.width / 2;
|
|
299
|
+
const height = screen.height;
|
|
300
|
+
const left = screen.width / 4;
|
|
301
|
+
window.open(url.toString(), "_blank", `width=${width},height=${height},left=${left},top=0`);
|
|
418
302
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
UnWallet
|
|
423
|
-
});
|
|
303
|
+
//#endregion
|
|
304
|
+
exports.UWError = UWError;
|
|
305
|
+
exports.UnWallet = UnWallet;
|