@phantom/browser-injected-sdk 1.0.0 → 1.0.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/dist/auto-confirm/index.d.ts +2 -1
- package/dist/{chunk-MDTCVDFZ.mjs → chunk-JJH6SEIK.mjs} +231 -57
- package/dist/ethereum/index.d.ts +2 -1
- package/dist/ethereum/index.js +234 -59
- package/dist/ethereum/index.mjs +5 -3
- package/dist/index-673af1e4.d.ts +93 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +232 -57
- package/dist/index.mjs +4 -2
- package/dist/solana/index.d.ts +5 -94
- package/dist/solana/index.js +93 -60
- package/dist/solana/index.mjs +93 -60
- package/package.json +6 -3
- package/dist/index-3a750f13.d.ts +0 -208
|
@@ -3,6 +3,129 @@ import {
|
|
|
3
3
|
__privateMethod
|
|
4
4
|
} from "./chunk-GV6AIHPN.mjs";
|
|
5
5
|
|
|
6
|
+
// src/ethereum/siwe.ts
|
|
7
|
+
var ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
|
|
8
|
+
var DOMAIN_REGEX = /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(:[0-9]{1,5})?$/;
|
|
9
|
+
var IP_REGEX = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]{1,5})?$/;
|
|
10
|
+
var LOCALHOST_REGEX = /^localhost(:[0-9]{1,5})?$/;
|
|
11
|
+
var NONCE_REGEX = /^[a-zA-Z0-9]{8,}$/;
|
|
12
|
+
var SCHEME_REGEX = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/;
|
|
13
|
+
function createSiweMessage({
|
|
14
|
+
address,
|
|
15
|
+
chainId,
|
|
16
|
+
domain,
|
|
17
|
+
nonce,
|
|
18
|
+
uri,
|
|
19
|
+
version,
|
|
20
|
+
scheme,
|
|
21
|
+
statement: _statement,
|
|
22
|
+
requestId,
|
|
23
|
+
resources,
|
|
24
|
+
issuedAt = /* @__PURE__ */ new Date(),
|
|
25
|
+
expirationTime,
|
|
26
|
+
notBefore
|
|
27
|
+
}) {
|
|
28
|
+
if (!ADDRESS_REGEX.test(address)) {
|
|
29
|
+
throw new Error("address must be a hex value of 20 bytes (40 hex characters).");
|
|
30
|
+
}
|
|
31
|
+
if (chainId !== Math.floor(chainId)) {
|
|
32
|
+
throw new Error("chainId must be a EIP-155 chain ID.");
|
|
33
|
+
}
|
|
34
|
+
if (!(DOMAIN_REGEX.test(domain) || IP_REGEX.test(domain) || LOCALHOST_REGEX.test(domain))) {
|
|
35
|
+
throw new Error("domain must be an RFC 3986 authority.");
|
|
36
|
+
}
|
|
37
|
+
if (!NONCE_REGEX.test(nonce)) {
|
|
38
|
+
throw new Error("nonce must be at least 8 characters.");
|
|
39
|
+
}
|
|
40
|
+
if (!_isUri(uri)) {
|
|
41
|
+
throw new Error("uri must be a RFC 3986 URI referring to the resource that is the subject of the signing.");
|
|
42
|
+
}
|
|
43
|
+
if (version !== "1") {
|
|
44
|
+
throw new Error("version must be '1'.");
|
|
45
|
+
}
|
|
46
|
+
if (scheme && !SCHEME_REGEX.test(scheme)) {
|
|
47
|
+
throw new Error("scheme must be an RFC 3986 URI scheme.");
|
|
48
|
+
}
|
|
49
|
+
if (_statement?.includes("\n")) {
|
|
50
|
+
throw new Error("statement must not include '\\n'.");
|
|
51
|
+
}
|
|
52
|
+
const origin = scheme ? `${scheme}://${domain}` : domain;
|
|
53
|
+
const statement = _statement ? `${_statement}
|
|
54
|
+
` : "";
|
|
55
|
+
const prefix = `${origin} wants you to sign in with your Ethereum account:
|
|
56
|
+
${address}
|
|
57
|
+
|
|
58
|
+
${statement}`;
|
|
59
|
+
let suffix = `URI: ${uri}
|
|
60
|
+
Version: ${version}
|
|
61
|
+
Chain ID: ${chainId}
|
|
62
|
+
Nonce: ${nonce}
|
|
63
|
+
Issued At: ${issuedAt.toISOString()}`;
|
|
64
|
+
if (expirationTime) {
|
|
65
|
+
suffix += `
|
|
66
|
+
Expiration Time: ${expirationTime.toISOString()}`;
|
|
67
|
+
}
|
|
68
|
+
if (notBefore) {
|
|
69
|
+
suffix += `
|
|
70
|
+
Not Before: ${notBefore.toISOString()}`;
|
|
71
|
+
}
|
|
72
|
+
if (requestId) {
|
|
73
|
+
suffix += `
|
|
74
|
+
Request ID: ${requestId}`;
|
|
75
|
+
}
|
|
76
|
+
if (resources) {
|
|
77
|
+
let content = "\nResources:";
|
|
78
|
+
for (const resource of resources) {
|
|
79
|
+
if (!_isUri(resource)) {
|
|
80
|
+
throw new Error("resources must be RFC 3986 URIs.");
|
|
81
|
+
}
|
|
82
|
+
content += `
|
|
83
|
+
- ${resource}`;
|
|
84
|
+
}
|
|
85
|
+
suffix += content;
|
|
86
|
+
}
|
|
87
|
+
return `${prefix}
|
|
88
|
+
${suffix}`;
|
|
89
|
+
}
|
|
90
|
+
function _isUri(value) {
|
|
91
|
+
if (/[^a-z0-9:/?#[\]@!$&'()*+,;=.\-_~%]/i.test(value))
|
|
92
|
+
return false;
|
|
93
|
+
if (/%[^0-9a-f]/i.test(value))
|
|
94
|
+
return false;
|
|
95
|
+
if (/%[0-9a-f](:?[^0-9a-f]|$)/i.test(value))
|
|
96
|
+
return false;
|
|
97
|
+
const splitted = splitUri(value);
|
|
98
|
+
const scheme = splitted[1];
|
|
99
|
+
const authority = splitted[2];
|
|
100
|
+
const path = splitted[3];
|
|
101
|
+
const query = splitted[4];
|
|
102
|
+
const fragment = splitted[5];
|
|
103
|
+
if (!(scheme?.length && path.length >= 0))
|
|
104
|
+
return false;
|
|
105
|
+
if (authority?.length) {
|
|
106
|
+
if (!(path.length === 0 || /^\//.test(path)))
|
|
107
|
+
return false;
|
|
108
|
+
} else {
|
|
109
|
+
if (/^\/\//.test(path))
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
if (!/^[a-z][a-z0-9+\-.]*$/.test(scheme.toLowerCase()))
|
|
113
|
+
return false;
|
|
114
|
+
let out = "";
|
|
115
|
+
out += `${scheme}:`;
|
|
116
|
+
if (authority?.length)
|
|
117
|
+
out += `//${authority}`;
|
|
118
|
+
out += path;
|
|
119
|
+
if (query?.length)
|
|
120
|
+
out += `?${query}`;
|
|
121
|
+
if (fragment?.length)
|
|
122
|
+
out += `#${fragment}`;
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
function splitUri(value) {
|
|
126
|
+
return value.match(/(?:([^:/?#]+):)?(?:\/\/([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/);
|
|
127
|
+
}
|
|
128
|
+
|
|
6
129
|
// src/ethereum/strategies/injected.ts
|
|
7
130
|
var MAX_RETRIES = 4;
|
|
8
131
|
var BASE_DELAY = 100;
|
|
@@ -119,7 +242,7 @@ var InjectedEthereumStrategy = class {
|
|
|
119
242
|
if (!provider) {
|
|
120
243
|
throw new Error("Provider not found.");
|
|
121
244
|
}
|
|
122
|
-
const message =
|
|
245
|
+
const message = createSiweMessage(signInData);
|
|
123
246
|
const address = provider.selectedAddress;
|
|
124
247
|
if (!address) {
|
|
125
248
|
throw new Error("No address available.");
|
|
@@ -215,7 +338,7 @@ function addEventListener(event, callback) {
|
|
|
215
338
|
eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
216
339
|
}
|
|
217
340
|
const listeners = eventListeners.get(event);
|
|
218
|
-
listeners
|
|
341
|
+
listeners?.add(callback);
|
|
219
342
|
return () => removeEventListener(event, callback);
|
|
220
343
|
}
|
|
221
344
|
function removeEventListener(event, callback) {
|
|
@@ -289,16 +412,6 @@ async function getAccounts() {
|
|
|
289
412
|
}
|
|
290
413
|
|
|
291
414
|
// src/ethereum/signMessage.ts
|
|
292
|
-
async function signMessage(message, address) {
|
|
293
|
-
const provider = await getProvider();
|
|
294
|
-
if (!provider) {
|
|
295
|
-
throw new Error("Provider not found.");
|
|
296
|
-
}
|
|
297
|
-
if (!provider.isConnected) {
|
|
298
|
-
await provider.connect({ onlyIfTrusted: false });
|
|
299
|
-
}
|
|
300
|
-
return provider.signMessage(message, address);
|
|
301
|
-
}
|
|
302
415
|
async function signPersonalMessage(message, address) {
|
|
303
416
|
const provider = await getProvider();
|
|
304
417
|
if (!provider) {
|
|
@@ -320,18 +433,6 @@ async function signTypedData(typedData, address) {
|
|
|
320
433
|
return provider.signTypedData(typedData, address);
|
|
321
434
|
}
|
|
322
435
|
|
|
323
|
-
// src/ethereum/signIn.ts
|
|
324
|
-
async function signIn(signInData) {
|
|
325
|
-
const provider = await getProvider();
|
|
326
|
-
if (!provider) {
|
|
327
|
-
throw new Error("Provider not found.");
|
|
328
|
-
}
|
|
329
|
-
if (!provider.isConnected) {
|
|
330
|
-
await provider.connect({ onlyIfTrusted: false });
|
|
331
|
-
}
|
|
332
|
-
return provider.signIn(signInData);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
436
|
// src/ethereum/sendTransaction.ts
|
|
336
437
|
async function sendTransaction(transaction) {
|
|
337
438
|
const provider = await getProvider();
|
|
@@ -371,51 +472,124 @@ async function switchChain(chainId) {
|
|
|
371
472
|
}
|
|
372
473
|
|
|
373
474
|
// src/ethereum/plugin.ts
|
|
374
|
-
var
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
475
|
+
var Ethereum = class {
|
|
476
|
+
constructor() {
|
|
477
|
+
this._chainId = "0x1";
|
|
478
|
+
this._accounts = [];
|
|
479
|
+
this.bindProviderEvents();
|
|
480
|
+
}
|
|
481
|
+
get chainId() {
|
|
482
|
+
return this._chainId;
|
|
483
|
+
}
|
|
484
|
+
get accounts() {
|
|
485
|
+
return this._accounts;
|
|
486
|
+
}
|
|
487
|
+
async request(args) {
|
|
488
|
+
const provider = await getProvider();
|
|
489
|
+
if (!provider) {
|
|
490
|
+
throw new Error("Provider not found.");
|
|
491
|
+
}
|
|
492
|
+
const providerInstance = provider.getProvider();
|
|
493
|
+
if (!providerInstance) {
|
|
494
|
+
throw new Error("Provider instance not found.");
|
|
495
|
+
}
|
|
496
|
+
return providerInstance.request(args);
|
|
497
|
+
}
|
|
498
|
+
async connect() {
|
|
499
|
+
const accounts = await connect();
|
|
500
|
+
this._accounts = accounts;
|
|
501
|
+
return accounts;
|
|
502
|
+
}
|
|
503
|
+
async disconnect() {
|
|
504
|
+
await disconnect();
|
|
505
|
+
this._accounts = [];
|
|
506
|
+
}
|
|
507
|
+
signPersonalMessage(message, address) {
|
|
508
|
+
return signPersonalMessage(message, address);
|
|
509
|
+
}
|
|
510
|
+
signTypedData(data, address) {
|
|
511
|
+
return signTypedData(data, address);
|
|
512
|
+
}
|
|
513
|
+
signTransaction(transaction) {
|
|
514
|
+
return signTransaction(transaction);
|
|
515
|
+
}
|
|
516
|
+
sendTransaction(transaction) {
|
|
517
|
+
return sendTransaction(transaction);
|
|
518
|
+
}
|
|
519
|
+
async switchChain(chainId) {
|
|
520
|
+
const hexChainId = typeof chainId === "string" ? chainId.toLowerCase().startsWith("0x") ? chainId.toLowerCase() : `0x${parseInt(chainId, 10).toString(16)}` : `0x${chainId.toString(16)}`;
|
|
521
|
+
await switchChain(hexChainId);
|
|
522
|
+
this._chainId = hexChainId;
|
|
523
|
+
}
|
|
524
|
+
async getChainId() {
|
|
525
|
+
const chainId = await getChainId();
|
|
526
|
+
const parsed = parseInt(chainId, 16);
|
|
527
|
+
this._chainId = chainId;
|
|
528
|
+
return parsed;
|
|
529
|
+
}
|
|
530
|
+
async getAccounts() {
|
|
531
|
+
const accounts = await getAccounts();
|
|
532
|
+
this._accounts = accounts;
|
|
533
|
+
return accounts;
|
|
534
|
+
}
|
|
535
|
+
isConnected() {
|
|
536
|
+
return this._accounts.length > 0;
|
|
537
|
+
}
|
|
538
|
+
on(event, listener) {
|
|
539
|
+
addEventListener(event, listener);
|
|
540
|
+
}
|
|
541
|
+
off(event, listener) {
|
|
542
|
+
removeEventListener(event, listener);
|
|
543
|
+
}
|
|
544
|
+
async bindProviderEvents() {
|
|
545
|
+
try {
|
|
546
|
+
const strategy = await getProvider();
|
|
547
|
+
const provider = strategy.getProvider();
|
|
548
|
+
if (provider) {
|
|
549
|
+
provider.on("connect", async () => {
|
|
550
|
+
try {
|
|
551
|
+
const accounts = await provider.request({ method: "eth_accounts" });
|
|
552
|
+
if (accounts?.length > 0) {
|
|
553
|
+
this._accounts = accounts;
|
|
554
|
+
triggerEvent("connect", accounts);
|
|
555
|
+
}
|
|
556
|
+
} catch {
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
provider.on("disconnect", () => {
|
|
560
|
+
this._accounts = [];
|
|
561
|
+
const error = {
|
|
562
|
+
code: 4900,
|
|
563
|
+
message: "Provider disconnected"
|
|
564
|
+
};
|
|
565
|
+
triggerEvent("disconnect", error);
|
|
566
|
+
});
|
|
567
|
+
provider.on("accountsChanged", (accounts) => {
|
|
568
|
+
this._accounts = accounts;
|
|
569
|
+
triggerEvent("accountsChanged", accounts);
|
|
570
|
+
if (accounts && accounts.length > 0) {
|
|
398
571
|
triggerEvent("connect", accounts);
|
|
399
|
-
|
|
572
|
+
}
|
|
400
573
|
});
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
574
|
+
provider.on("chainChanged", (chainId) => {
|
|
575
|
+
this._chainId = chainId;
|
|
576
|
+
triggerEvent("chainChanged", chainId);
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
} catch (error) {
|
|
405
580
|
}
|
|
406
|
-
} catch (error) {
|
|
407
581
|
}
|
|
408
|
-
}
|
|
582
|
+
};
|
|
409
583
|
function createEthereumPlugin() {
|
|
410
584
|
return {
|
|
411
585
|
name: "ethereum",
|
|
412
586
|
create: () => {
|
|
413
|
-
|
|
414
|
-
return ethereum;
|
|
587
|
+
return new Ethereum();
|
|
415
588
|
}
|
|
416
589
|
};
|
|
417
590
|
}
|
|
418
591
|
|
|
419
592
|
export {
|
|
593
|
+
createSiweMessage,
|
|
420
594
|
createEthereumPlugin
|
|
421
595
|
};
|
package/dist/ethereum/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { e as EthereumEventType, d as EthereumSignInData, E as EthereumTransaction, b as PhantomEthereumProvider, c as createEthereumPlugin, a as createSiweMessage } from '../index-673af1e4.js';
|
|
2
|
+
import '@phantom/chain-interfaces';
|
package/dist/ethereum/index.js
CHANGED
|
@@ -33,10 +33,134 @@ var __privateMethod = (obj, member, method) => {
|
|
|
33
33
|
// src/ethereum/index.ts
|
|
34
34
|
var ethereum_exports = {};
|
|
35
35
|
__export(ethereum_exports, {
|
|
36
|
-
createEthereumPlugin: () => createEthereumPlugin
|
|
36
|
+
createEthereumPlugin: () => createEthereumPlugin,
|
|
37
|
+
createSiweMessage: () => createSiweMessage
|
|
37
38
|
});
|
|
38
39
|
module.exports = __toCommonJS(ethereum_exports);
|
|
39
40
|
|
|
41
|
+
// src/ethereum/siwe.ts
|
|
42
|
+
var ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
|
|
43
|
+
var DOMAIN_REGEX = /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(:[0-9]{1,5})?$/;
|
|
44
|
+
var IP_REGEX = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]{1,5})?$/;
|
|
45
|
+
var LOCALHOST_REGEX = /^localhost(:[0-9]{1,5})?$/;
|
|
46
|
+
var NONCE_REGEX = /^[a-zA-Z0-9]{8,}$/;
|
|
47
|
+
var SCHEME_REGEX = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/;
|
|
48
|
+
function createSiweMessage({
|
|
49
|
+
address,
|
|
50
|
+
chainId,
|
|
51
|
+
domain,
|
|
52
|
+
nonce,
|
|
53
|
+
uri,
|
|
54
|
+
version,
|
|
55
|
+
scheme,
|
|
56
|
+
statement: _statement,
|
|
57
|
+
requestId,
|
|
58
|
+
resources,
|
|
59
|
+
issuedAt = /* @__PURE__ */ new Date(),
|
|
60
|
+
expirationTime,
|
|
61
|
+
notBefore
|
|
62
|
+
}) {
|
|
63
|
+
if (!ADDRESS_REGEX.test(address)) {
|
|
64
|
+
throw new Error("address must be a hex value of 20 bytes (40 hex characters).");
|
|
65
|
+
}
|
|
66
|
+
if (chainId !== Math.floor(chainId)) {
|
|
67
|
+
throw new Error("chainId must be a EIP-155 chain ID.");
|
|
68
|
+
}
|
|
69
|
+
if (!(DOMAIN_REGEX.test(domain) || IP_REGEX.test(domain) || LOCALHOST_REGEX.test(domain))) {
|
|
70
|
+
throw new Error("domain must be an RFC 3986 authority.");
|
|
71
|
+
}
|
|
72
|
+
if (!NONCE_REGEX.test(nonce)) {
|
|
73
|
+
throw new Error("nonce must be at least 8 characters.");
|
|
74
|
+
}
|
|
75
|
+
if (!_isUri(uri)) {
|
|
76
|
+
throw new Error("uri must be a RFC 3986 URI referring to the resource that is the subject of the signing.");
|
|
77
|
+
}
|
|
78
|
+
if (version !== "1") {
|
|
79
|
+
throw new Error("version must be '1'.");
|
|
80
|
+
}
|
|
81
|
+
if (scheme && !SCHEME_REGEX.test(scheme)) {
|
|
82
|
+
throw new Error("scheme must be an RFC 3986 URI scheme.");
|
|
83
|
+
}
|
|
84
|
+
if (_statement?.includes("\n")) {
|
|
85
|
+
throw new Error("statement must not include '\\n'.");
|
|
86
|
+
}
|
|
87
|
+
const origin = scheme ? `${scheme}://${domain}` : domain;
|
|
88
|
+
const statement = _statement ? `${_statement}
|
|
89
|
+
` : "";
|
|
90
|
+
const prefix = `${origin} wants you to sign in with your Ethereum account:
|
|
91
|
+
${address}
|
|
92
|
+
|
|
93
|
+
${statement}`;
|
|
94
|
+
let suffix = `URI: ${uri}
|
|
95
|
+
Version: ${version}
|
|
96
|
+
Chain ID: ${chainId}
|
|
97
|
+
Nonce: ${nonce}
|
|
98
|
+
Issued At: ${issuedAt.toISOString()}`;
|
|
99
|
+
if (expirationTime) {
|
|
100
|
+
suffix += `
|
|
101
|
+
Expiration Time: ${expirationTime.toISOString()}`;
|
|
102
|
+
}
|
|
103
|
+
if (notBefore) {
|
|
104
|
+
suffix += `
|
|
105
|
+
Not Before: ${notBefore.toISOString()}`;
|
|
106
|
+
}
|
|
107
|
+
if (requestId) {
|
|
108
|
+
suffix += `
|
|
109
|
+
Request ID: ${requestId}`;
|
|
110
|
+
}
|
|
111
|
+
if (resources) {
|
|
112
|
+
let content = "\nResources:";
|
|
113
|
+
for (const resource of resources) {
|
|
114
|
+
if (!_isUri(resource)) {
|
|
115
|
+
throw new Error("resources must be RFC 3986 URIs.");
|
|
116
|
+
}
|
|
117
|
+
content += `
|
|
118
|
+
- ${resource}`;
|
|
119
|
+
}
|
|
120
|
+
suffix += content;
|
|
121
|
+
}
|
|
122
|
+
return `${prefix}
|
|
123
|
+
${suffix}`;
|
|
124
|
+
}
|
|
125
|
+
function _isUri(value) {
|
|
126
|
+
if (/[^a-z0-9:/?#[\]@!$&'()*+,;=.\-_~%]/i.test(value))
|
|
127
|
+
return false;
|
|
128
|
+
if (/%[^0-9a-f]/i.test(value))
|
|
129
|
+
return false;
|
|
130
|
+
if (/%[0-9a-f](:?[^0-9a-f]|$)/i.test(value))
|
|
131
|
+
return false;
|
|
132
|
+
const splitted = splitUri(value);
|
|
133
|
+
const scheme = splitted[1];
|
|
134
|
+
const authority = splitted[2];
|
|
135
|
+
const path = splitted[3];
|
|
136
|
+
const query = splitted[4];
|
|
137
|
+
const fragment = splitted[5];
|
|
138
|
+
if (!(scheme?.length && path.length >= 0))
|
|
139
|
+
return false;
|
|
140
|
+
if (authority?.length) {
|
|
141
|
+
if (!(path.length === 0 || /^\//.test(path)))
|
|
142
|
+
return false;
|
|
143
|
+
} else {
|
|
144
|
+
if (/^\/\//.test(path))
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
if (!/^[a-z][a-z0-9+\-.]*$/.test(scheme.toLowerCase()))
|
|
148
|
+
return false;
|
|
149
|
+
let out = "";
|
|
150
|
+
out += `${scheme}:`;
|
|
151
|
+
if (authority?.length)
|
|
152
|
+
out += `//${authority}`;
|
|
153
|
+
out += path;
|
|
154
|
+
if (query?.length)
|
|
155
|
+
out += `?${query}`;
|
|
156
|
+
if (fragment?.length)
|
|
157
|
+
out += `#${fragment}`;
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
function splitUri(value) {
|
|
161
|
+
return value.match(/(?:([^:/?#]+):)?(?:\/\/([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/);
|
|
162
|
+
}
|
|
163
|
+
|
|
40
164
|
// src/ethereum/strategies/injected.ts
|
|
41
165
|
var MAX_RETRIES = 4;
|
|
42
166
|
var BASE_DELAY = 100;
|
|
@@ -153,7 +277,7 @@ var InjectedEthereumStrategy = class {
|
|
|
153
277
|
if (!provider) {
|
|
154
278
|
throw new Error("Provider not found.");
|
|
155
279
|
}
|
|
156
|
-
const message =
|
|
280
|
+
const message = createSiweMessage(signInData);
|
|
157
281
|
const address = provider.selectedAddress;
|
|
158
282
|
if (!address) {
|
|
159
283
|
throw new Error("No address available.");
|
|
@@ -249,7 +373,7 @@ function addEventListener(event, callback) {
|
|
|
249
373
|
eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
250
374
|
}
|
|
251
375
|
const listeners = eventListeners.get(event);
|
|
252
|
-
listeners
|
|
376
|
+
listeners?.add(callback);
|
|
253
377
|
return () => removeEventListener(event, callback);
|
|
254
378
|
}
|
|
255
379
|
function removeEventListener(event, callback) {
|
|
@@ -323,16 +447,6 @@ async function getAccounts() {
|
|
|
323
447
|
}
|
|
324
448
|
|
|
325
449
|
// src/ethereum/signMessage.ts
|
|
326
|
-
async function signMessage(message, address) {
|
|
327
|
-
const provider = await getProvider();
|
|
328
|
-
if (!provider) {
|
|
329
|
-
throw new Error("Provider not found.");
|
|
330
|
-
}
|
|
331
|
-
if (!provider.isConnected) {
|
|
332
|
-
await provider.connect({ onlyIfTrusted: false });
|
|
333
|
-
}
|
|
334
|
-
return provider.signMessage(message, address);
|
|
335
|
-
}
|
|
336
450
|
async function signPersonalMessage(message, address) {
|
|
337
451
|
const provider = await getProvider();
|
|
338
452
|
if (!provider) {
|
|
@@ -354,18 +468,6 @@ async function signTypedData(typedData, address) {
|
|
|
354
468
|
return provider.signTypedData(typedData, address);
|
|
355
469
|
}
|
|
356
470
|
|
|
357
|
-
// src/ethereum/signIn.ts
|
|
358
|
-
async function signIn(signInData) {
|
|
359
|
-
const provider = await getProvider();
|
|
360
|
-
if (!provider) {
|
|
361
|
-
throw new Error("Provider not found.");
|
|
362
|
-
}
|
|
363
|
-
if (!provider.isConnected) {
|
|
364
|
-
await provider.connect({ onlyIfTrusted: false });
|
|
365
|
-
}
|
|
366
|
-
return provider.signIn(signInData);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
471
|
// src/ethereum/sendTransaction.ts
|
|
370
472
|
async function sendTransaction(transaction) {
|
|
371
473
|
const provider = await getProvider();
|
|
@@ -405,51 +507,124 @@ async function switchChain(chainId) {
|
|
|
405
507
|
}
|
|
406
508
|
|
|
407
509
|
// src/ethereum/plugin.ts
|
|
408
|
-
var
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
510
|
+
var Ethereum = class {
|
|
511
|
+
constructor() {
|
|
512
|
+
this._chainId = "0x1";
|
|
513
|
+
this._accounts = [];
|
|
514
|
+
this.bindProviderEvents();
|
|
515
|
+
}
|
|
516
|
+
get chainId() {
|
|
517
|
+
return this._chainId;
|
|
518
|
+
}
|
|
519
|
+
get accounts() {
|
|
520
|
+
return this._accounts;
|
|
521
|
+
}
|
|
522
|
+
async request(args) {
|
|
523
|
+
const provider = await getProvider();
|
|
524
|
+
if (!provider) {
|
|
525
|
+
throw new Error("Provider not found.");
|
|
526
|
+
}
|
|
527
|
+
const providerInstance = provider.getProvider();
|
|
528
|
+
if (!providerInstance) {
|
|
529
|
+
throw new Error("Provider instance not found.");
|
|
530
|
+
}
|
|
531
|
+
return providerInstance.request(args);
|
|
532
|
+
}
|
|
533
|
+
async connect() {
|
|
534
|
+
const accounts = await connect();
|
|
535
|
+
this._accounts = accounts;
|
|
536
|
+
return accounts;
|
|
537
|
+
}
|
|
538
|
+
async disconnect() {
|
|
539
|
+
await disconnect();
|
|
540
|
+
this._accounts = [];
|
|
541
|
+
}
|
|
542
|
+
signPersonalMessage(message, address) {
|
|
543
|
+
return signPersonalMessage(message, address);
|
|
544
|
+
}
|
|
545
|
+
signTypedData(data, address) {
|
|
546
|
+
return signTypedData(data, address);
|
|
547
|
+
}
|
|
548
|
+
signTransaction(transaction) {
|
|
549
|
+
return signTransaction(transaction);
|
|
550
|
+
}
|
|
551
|
+
sendTransaction(transaction) {
|
|
552
|
+
return sendTransaction(transaction);
|
|
553
|
+
}
|
|
554
|
+
async switchChain(chainId) {
|
|
555
|
+
const hexChainId = typeof chainId === "string" ? chainId.toLowerCase().startsWith("0x") ? chainId.toLowerCase() : `0x${parseInt(chainId, 10).toString(16)}` : `0x${chainId.toString(16)}`;
|
|
556
|
+
await switchChain(hexChainId);
|
|
557
|
+
this._chainId = hexChainId;
|
|
558
|
+
}
|
|
559
|
+
async getChainId() {
|
|
560
|
+
const chainId = await getChainId();
|
|
561
|
+
const parsed = parseInt(chainId, 16);
|
|
562
|
+
this._chainId = chainId;
|
|
563
|
+
return parsed;
|
|
564
|
+
}
|
|
565
|
+
async getAccounts() {
|
|
566
|
+
const accounts = await getAccounts();
|
|
567
|
+
this._accounts = accounts;
|
|
568
|
+
return accounts;
|
|
569
|
+
}
|
|
570
|
+
isConnected() {
|
|
571
|
+
return this._accounts.length > 0;
|
|
572
|
+
}
|
|
573
|
+
on(event, listener) {
|
|
574
|
+
addEventListener(event, listener);
|
|
575
|
+
}
|
|
576
|
+
off(event, listener) {
|
|
577
|
+
removeEventListener(event, listener);
|
|
578
|
+
}
|
|
579
|
+
async bindProviderEvents() {
|
|
580
|
+
try {
|
|
581
|
+
const strategy = await getProvider();
|
|
582
|
+
const provider = strategy.getProvider();
|
|
583
|
+
if (provider) {
|
|
584
|
+
provider.on("connect", async () => {
|
|
585
|
+
try {
|
|
586
|
+
const accounts = await provider.request({ method: "eth_accounts" });
|
|
587
|
+
if (accounts?.length > 0) {
|
|
588
|
+
this._accounts = accounts;
|
|
589
|
+
triggerEvent("connect", accounts);
|
|
590
|
+
}
|
|
591
|
+
} catch {
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
provider.on("disconnect", () => {
|
|
595
|
+
this._accounts = [];
|
|
596
|
+
const error = {
|
|
597
|
+
code: 4900,
|
|
598
|
+
message: "Provider disconnected"
|
|
599
|
+
};
|
|
600
|
+
triggerEvent("disconnect", error);
|
|
601
|
+
});
|
|
602
|
+
provider.on("accountsChanged", (accounts) => {
|
|
603
|
+
this._accounts = accounts;
|
|
604
|
+
triggerEvent("accountsChanged", accounts);
|
|
605
|
+
if (accounts && accounts.length > 0) {
|
|
432
606
|
triggerEvent("connect", accounts);
|
|
433
|
-
|
|
607
|
+
}
|
|
434
608
|
});
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
609
|
+
provider.on("chainChanged", (chainId) => {
|
|
610
|
+
this._chainId = chainId;
|
|
611
|
+
triggerEvent("chainChanged", chainId);
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
} catch (error) {
|
|
439
615
|
}
|
|
440
|
-
} catch (error) {
|
|
441
616
|
}
|
|
442
|
-
}
|
|
617
|
+
};
|
|
443
618
|
function createEthereumPlugin() {
|
|
444
619
|
return {
|
|
445
620
|
name: "ethereum",
|
|
446
621
|
create: () => {
|
|
447
|
-
|
|
448
|
-
return ethereum;
|
|
622
|
+
return new Ethereum();
|
|
449
623
|
}
|
|
450
624
|
};
|
|
451
625
|
}
|
|
452
626
|
// Annotate the CommonJS export names for ESM import in node:
|
|
453
627
|
0 && (module.exports = {
|
|
454
|
-
createEthereumPlugin
|
|
628
|
+
createEthereumPlugin,
|
|
629
|
+
createSiweMessage
|
|
455
630
|
});
|