@phantom/browser-injected-sdk 1.0.0-beta.9 → 1.0.2
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-CS23VDDD.mjs} +234 -57
- package/dist/ethereum/index.d.ts +2 -1
- package/dist/ethereum/index.js +237 -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 +235 -57
- package/dist/index.mjs +4 -2
- package/dist/solana/index.d.ts +5 -94
- package/dist/solana/index.js +108 -54
- package/dist/solana/index.mjs +108 -54
- package/package.json +6 -3
- package/dist/index-3a750f13.d.ts +0 -208
package/dist/index.js
CHANGED
|
@@ -36,6 +36,7 @@ __export(src_exports, {
|
|
|
36
36
|
createEthereumPlugin: () => createEthereumPlugin,
|
|
37
37
|
createExtensionPlugin: () => createExtensionPlugin,
|
|
38
38
|
createPhantom: () => createPhantom,
|
|
39
|
+
createSiweMessage: () => createSiweMessage,
|
|
39
40
|
isPhantomExtensionInstalled: () => isInstalled
|
|
40
41
|
});
|
|
41
42
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -61,6 +62,129 @@ function createExtensionPlugin() {
|
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
64
|
|
|
65
|
+
// src/ethereum/siwe.ts
|
|
66
|
+
var ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
|
|
67
|
+
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})?$/;
|
|
68
|
+
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})?$/;
|
|
69
|
+
var LOCALHOST_REGEX = /^localhost(:[0-9]{1,5})?$/;
|
|
70
|
+
var NONCE_REGEX = /^[a-zA-Z0-9]{8,}$/;
|
|
71
|
+
var SCHEME_REGEX = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/;
|
|
72
|
+
function createSiweMessage({
|
|
73
|
+
address,
|
|
74
|
+
chainId,
|
|
75
|
+
domain,
|
|
76
|
+
nonce,
|
|
77
|
+
uri,
|
|
78
|
+
version,
|
|
79
|
+
scheme,
|
|
80
|
+
statement: _statement,
|
|
81
|
+
requestId,
|
|
82
|
+
resources,
|
|
83
|
+
issuedAt = /* @__PURE__ */ new Date(),
|
|
84
|
+
expirationTime,
|
|
85
|
+
notBefore
|
|
86
|
+
}) {
|
|
87
|
+
if (!ADDRESS_REGEX.test(address)) {
|
|
88
|
+
throw new Error("address must be a hex value of 20 bytes (40 hex characters).");
|
|
89
|
+
}
|
|
90
|
+
if (chainId !== Math.floor(chainId)) {
|
|
91
|
+
throw new Error("chainId must be a EIP-155 chain ID.");
|
|
92
|
+
}
|
|
93
|
+
if (!(DOMAIN_REGEX.test(domain) || IP_REGEX.test(domain) || LOCALHOST_REGEX.test(domain))) {
|
|
94
|
+
throw new Error("domain must be an RFC 3986 authority.");
|
|
95
|
+
}
|
|
96
|
+
if (!NONCE_REGEX.test(nonce)) {
|
|
97
|
+
throw new Error("nonce must be at least 8 characters.");
|
|
98
|
+
}
|
|
99
|
+
if (!_isUri(uri)) {
|
|
100
|
+
throw new Error("uri must be a RFC 3986 URI referring to the resource that is the subject of the signing.");
|
|
101
|
+
}
|
|
102
|
+
if (version !== "1") {
|
|
103
|
+
throw new Error("version must be '1'.");
|
|
104
|
+
}
|
|
105
|
+
if (scheme && !SCHEME_REGEX.test(scheme)) {
|
|
106
|
+
throw new Error("scheme must be an RFC 3986 URI scheme.");
|
|
107
|
+
}
|
|
108
|
+
if (_statement?.includes("\n")) {
|
|
109
|
+
throw new Error("statement must not include '\\n'.");
|
|
110
|
+
}
|
|
111
|
+
const origin = scheme ? `${scheme}://${domain}` : domain;
|
|
112
|
+
const statement = _statement ? `${_statement}
|
|
113
|
+
` : "";
|
|
114
|
+
const prefix = `${origin} wants you to sign in with your Ethereum account:
|
|
115
|
+
${address}
|
|
116
|
+
|
|
117
|
+
${statement}`;
|
|
118
|
+
let suffix = `URI: ${uri}
|
|
119
|
+
Version: ${version}
|
|
120
|
+
Chain ID: ${chainId}
|
|
121
|
+
Nonce: ${nonce}
|
|
122
|
+
Issued At: ${issuedAt.toISOString()}`;
|
|
123
|
+
if (expirationTime) {
|
|
124
|
+
suffix += `
|
|
125
|
+
Expiration Time: ${expirationTime.toISOString()}`;
|
|
126
|
+
}
|
|
127
|
+
if (notBefore) {
|
|
128
|
+
suffix += `
|
|
129
|
+
Not Before: ${notBefore.toISOString()}`;
|
|
130
|
+
}
|
|
131
|
+
if (requestId) {
|
|
132
|
+
suffix += `
|
|
133
|
+
Request ID: ${requestId}`;
|
|
134
|
+
}
|
|
135
|
+
if (resources) {
|
|
136
|
+
let content = "\nResources:";
|
|
137
|
+
for (const resource of resources) {
|
|
138
|
+
if (!_isUri(resource)) {
|
|
139
|
+
throw new Error("resources must be RFC 3986 URIs.");
|
|
140
|
+
}
|
|
141
|
+
content += `
|
|
142
|
+
- ${resource}`;
|
|
143
|
+
}
|
|
144
|
+
suffix += content;
|
|
145
|
+
}
|
|
146
|
+
return `${prefix}
|
|
147
|
+
${suffix}`;
|
|
148
|
+
}
|
|
149
|
+
function _isUri(value) {
|
|
150
|
+
if (/[^a-z0-9:/?#[\]@!$&'()*+,;=.\-_~%]/i.test(value))
|
|
151
|
+
return false;
|
|
152
|
+
if (/%[^0-9a-f]/i.test(value))
|
|
153
|
+
return false;
|
|
154
|
+
if (/%[0-9a-f](:?[^0-9a-f]|$)/i.test(value))
|
|
155
|
+
return false;
|
|
156
|
+
const splitted = splitUri(value);
|
|
157
|
+
const scheme = splitted[1];
|
|
158
|
+
const authority = splitted[2];
|
|
159
|
+
const path = splitted[3];
|
|
160
|
+
const query = splitted[4];
|
|
161
|
+
const fragment = splitted[5];
|
|
162
|
+
if (!(scheme?.length && path.length >= 0))
|
|
163
|
+
return false;
|
|
164
|
+
if (authority?.length) {
|
|
165
|
+
if (!(path.length === 0 || /^\//.test(path)))
|
|
166
|
+
return false;
|
|
167
|
+
} else {
|
|
168
|
+
if (/^\/\//.test(path))
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
if (!/^[a-z][a-z0-9+\-.]*$/.test(scheme.toLowerCase()))
|
|
172
|
+
return false;
|
|
173
|
+
let out = "";
|
|
174
|
+
out += `${scheme}:`;
|
|
175
|
+
if (authority?.length)
|
|
176
|
+
out += `//${authority}`;
|
|
177
|
+
out += path;
|
|
178
|
+
if (query?.length)
|
|
179
|
+
out += `?${query}`;
|
|
180
|
+
if (fragment?.length)
|
|
181
|
+
out += `#${fragment}`;
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
function splitUri(value) {
|
|
185
|
+
return value.match(/(?:([^:/?#]+):)?(?:\/\/([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/);
|
|
186
|
+
}
|
|
187
|
+
|
|
64
188
|
// src/ethereum/strategies/injected.ts
|
|
65
189
|
var MAX_RETRIES = 4;
|
|
66
190
|
var BASE_DELAY = 100;
|
|
@@ -177,7 +301,7 @@ var InjectedEthereumStrategy = class {
|
|
|
177
301
|
if (!provider) {
|
|
178
302
|
throw new Error("Provider not found.");
|
|
179
303
|
}
|
|
180
|
-
const message =
|
|
304
|
+
const message = createSiweMessage(signInData);
|
|
181
305
|
const address = provider.selectedAddress;
|
|
182
306
|
if (!address) {
|
|
183
307
|
throw new Error("No address available.");
|
|
@@ -273,7 +397,7 @@ function addEventListener(event, callback) {
|
|
|
273
397
|
eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
274
398
|
}
|
|
275
399
|
const listeners = eventListeners.get(event);
|
|
276
|
-
listeners
|
|
400
|
+
listeners?.add(callback);
|
|
277
401
|
return () => removeEventListener(event, callback);
|
|
278
402
|
}
|
|
279
403
|
function removeEventListener(event, callback) {
|
|
@@ -347,16 +471,6 @@ async function getAccounts() {
|
|
|
347
471
|
}
|
|
348
472
|
|
|
349
473
|
// src/ethereum/signMessage.ts
|
|
350
|
-
async function signMessage(message, address) {
|
|
351
|
-
const provider = await getProvider();
|
|
352
|
-
if (!provider) {
|
|
353
|
-
throw new Error("Provider not found.");
|
|
354
|
-
}
|
|
355
|
-
if (!provider.isConnected) {
|
|
356
|
-
await provider.connect({ onlyIfTrusted: false });
|
|
357
|
-
}
|
|
358
|
-
return provider.signMessage(message, address);
|
|
359
|
-
}
|
|
360
474
|
async function signPersonalMessage(message, address) {
|
|
361
475
|
const provider = await getProvider();
|
|
362
476
|
if (!provider) {
|
|
@@ -378,18 +492,6 @@ async function signTypedData(typedData, address) {
|
|
|
378
492
|
return provider.signTypedData(typedData, address);
|
|
379
493
|
}
|
|
380
494
|
|
|
381
|
-
// src/ethereum/signIn.ts
|
|
382
|
-
async function signIn(signInData) {
|
|
383
|
-
const provider = await getProvider();
|
|
384
|
-
if (!provider) {
|
|
385
|
-
throw new Error("Provider not found.");
|
|
386
|
-
}
|
|
387
|
-
if (!provider.isConnected) {
|
|
388
|
-
await provider.connect({ onlyIfTrusted: false });
|
|
389
|
-
}
|
|
390
|
-
return provider.signIn(signInData);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
495
|
// src/ethereum/sendTransaction.ts
|
|
394
496
|
async function sendTransaction(transaction) {
|
|
395
497
|
const provider = await getProvider();
|
|
@@ -429,47 +531,122 @@ async function switchChain(chainId) {
|
|
|
429
531
|
}
|
|
430
532
|
|
|
431
533
|
// src/ethereum/plugin.ts
|
|
432
|
-
var
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
534
|
+
var Ethereum = class {
|
|
535
|
+
constructor() {
|
|
536
|
+
this._chainId = "0x1";
|
|
537
|
+
this._accounts = [];
|
|
538
|
+
this.bindProviderEvents();
|
|
539
|
+
}
|
|
540
|
+
get connected() {
|
|
541
|
+
return this._accounts.length > 0;
|
|
542
|
+
}
|
|
543
|
+
get chainId() {
|
|
544
|
+
return this._chainId;
|
|
545
|
+
}
|
|
546
|
+
get accounts() {
|
|
547
|
+
return this._accounts;
|
|
548
|
+
}
|
|
549
|
+
async request(args) {
|
|
550
|
+
const provider = await getProvider();
|
|
551
|
+
if (!provider) {
|
|
552
|
+
throw new Error("Provider not found.");
|
|
553
|
+
}
|
|
554
|
+
const providerInstance = provider.getProvider();
|
|
555
|
+
if (!providerInstance) {
|
|
556
|
+
throw new Error("Provider instance not found.");
|
|
557
|
+
}
|
|
558
|
+
return providerInstance.request(args);
|
|
559
|
+
}
|
|
560
|
+
async connect() {
|
|
561
|
+
const accounts = await connect();
|
|
562
|
+
this._accounts = accounts;
|
|
563
|
+
return accounts;
|
|
564
|
+
}
|
|
565
|
+
async disconnect() {
|
|
566
|
+
await disconnect();
|
|
567
|
+
this._accounts = [];
|
|
568
|
+
}
|
|
569
|
+
signPersonalMessage(message, address) {
|
|
570
|
+
return signPersonalMessage(message, address);
|
|
571
|
+
}
|
|
572
|
+
signTypedData(data, address) {
|
|
573
|
+
return signTypedData(data, address);
|
|
574
|
+
}
|
|
575
|
+
signTransaction(transaction) {
|
|
576
|
+
return signTransaction(transaction);
|
|
577
|
+
}
|
|
578
|
+
sendTransaction(transaction) {
|
|
579
|
+
return sendTransaction(transaction);
|
|
580
|
+
}
|
|
581
|
+
async switchChain(chainId) {
|
|
582
|
+
const hexChainId = typeof chainId === "string" ? chainId.toLowerCase().startsWith("0x") ? chainId.toLowerCase() : `0x${parseInt(chainId, 10).toString(16)}` : `0x${chainId.toString(16)}`;
|
|
583
|
+
await switchChain(hexChainId);
|
|
584
|
+
this._chainId = hexChainId;
|
|
585
|
+
}
|
|
586
|
+
async getChainId() {
|
|
587
|
+
const chainId = await getChainId();
|
|
588
|
+
const parsed = parseInt(chainId, 16);
|
|
589
|
+
this._chainId = chainId;
|
|
590
|
+
return parsed;
|
|
591
|
+
}
|
|
592
|
+
async getAccounts() {
|
|
593
|
+
const accounts = await getAccounts();
|
|
594
|
+
this._accounts = accounts;
|
|
595
|
+
return accounts;
|
|
596
|
+
}
|
|
597
|
+
isConnected() {
|
|
598
|
+
return this._accounts.length > 0;
|
|
599
|
+
}
|
|
600
|
+
on(event, listener) {
|
|
601
|
+
addEventListener(event, listener);
|
|
602
|
+
}
|
|
603
|
+
off(event, listener) {
|
|
604
|
+
removeEventListener(event, listener);
|
|
605
|
+
}
|
|
606
|
+
async bindProviderEvents() {
|
|
607
|
+
try {
|
|
608
|
+
const strategy = await getProvider();
|
|
609
|
+
const provider = strategy.getProvider();
|
|
610
|
+
if (provider) {
|
|
611
|
+
provider.on("connect", async () => {
|
|
612
|
+
try {
|
|
613
|
+
const accounts = await provider.request({ method: "eth_accounts" });
|
|
614
|
+
if (accounts?.length > 0) {
|
|
615
|
+
this._accounts = accounts;
|
|
616
|
+
triggerEvent("connect", accounts);
|
|
617
|
+
}
|
|
618
|
+
} catch {
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
provider.on("disconnect", () => {
|
|
622
|
+
this._accounts = [];
|
|
623
|
+
const error = {
|
|
624
|
+
code: 4900,
|
|
625
|
+
message: "Provider disconnected"
|
|
626
|
+
};
|
|
627
|
+
triggerEvent("disconnect", error);
|
|
628
|
+
});
|
|
629
|
+
provider.on("accountsChanged", (accounts) => {
|
|
630
|
+
this._accounts = accounts;
|
|
631
|
+
triggerEvent("accountsChanged", accounts);
|
|
632
|
+
if (accounts && accounts.length > 0) {
|
|
456
633
|
triggerEvent("connect", accounts);
|
|
457
|
-
|
|
634
|
+
}
|
|
458
635
|
});
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
636
|
+
provider.on("chainChanged", (chainId) => {
|
|
637
|
+
this._chainId = chainId;
|
|
638
|
+
triggerEvent("chainChanged", chainId);
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
} catch (error) {
|
|
463
642
|
}
|
|
464
|
-
} catch (error) {
|
|
465
643
|
}
|
|
466
|
-
}
|
|
644
|
+
};
|
|
467
645
|
function createEthereumPlugin() {
|
|
468
646
|
return {
|
|
469
647
|
name: "ethereum",
|
|
470
648
|
create: () => {
|
|
471
|
-
|
|
472
|
-
return ethereum;
|
|
649
|
+
return new Ethereum();
|
|
473
650
|
}
|
|
474
651
|
};
|
|
475
652
|
}
|
|
@@ -487,5 +664,6 @@ function createPhantom({ plugins = [] }) {
|
|
|
487
664
|
createEthereumPlugin,
|
|
488
665
|
createExtensionPlugin,
|
|
489
666
|
createPhantom,
|
|
667
|
+
createSiweMessage,
|
|
490
668
|
isPhantomExtensionInstalled
|
|
491
669
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
createEthereumPlugin
|
|
3
|
-
|
|
2
|
+
createEthereumPlugin,
|
|
3
|
+
createSiweMessage
|
|
4
|
+
} from "./chunk-CS23VDDD.mjs";
|
|
4
5
|
import "./chunk-WUKYLWAZ.mjs";
|
|
5
6
|
import "./chunk-GV6AIHPN.mjs";
|
|
6
7
|
|
|
@@ -37,5 +38,6 @@ export {
|
|
|
37
38
|
createEthereumPlugin,
|
|
38
39
|
createExtensionPlugin,
|
|
39
40
|
createPhantom,
|
|
41
|
+
createSiweMessage,
|
|
40
42
|
isInstalled as isPhantomExtensionInstalled
|
|
41
43
|
};
|
package/dist/solana/index.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import { P as Plugin } from '../index-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
declare function connect({ onlyIfTrusted }?: {
|
|
5
|
-
onlyIfTrusted?: boolean | undefined;
|
|
6
|
-
}): Promise<string | undefined>;
|
|
7
|
-
|
|
8
|
-
declare function disconnect(): Promise<void>;
|
|
1
|
+
import { P as Plugin } from '../index-673af1e4.js';
|
|
2
|
+
import { ISolanaChain } from '@phantom/chain-interfaces';
|
|
9
3
|
|
|
10
4
|
type SendOptions = {
|
|
11
5
|
skipPreflight?: boolean;
|
|
@@ -32,7 +26,6 @@ type SolanaSignInData = {
|
|
|
32
26
|
resources?: string[];
|
|
33
27
|
};
|
|
34
28
|
type DisplayEncoding = "utf8" | "hex";
|
|
35
|
-
type PhantomEventType = "connect" | "disconnect" | "accountChanged";
|
|
36
29
|
interface PhantomSolanaProvider {
|
|
37
30
|
isPhantom: boolean;
|
|
38
31
|
publicKey: PublicKey | null;
|
|
@@ -66,94 +59,12 @@ interface PhantomSolanaProvider {
|
|
|
66
59
|
off: (event: "connect" | "disconnect" | "accountChanged", handler: (publicKey?: PublicKey) => void) => void;
|
|
67
60
|
}
|
|
68
61
|
|
|
69
|
-
|
|
70
|
-
type DisconnectCallback = () => void;
|
|
71
|
-
type AccountChangedCallback = (publicKey: string) => void;
|
|
72
|
-
type PhantomEventCallback = ConnectCallback | DisconnectCallback | AccountChangedCallback;
|
|
73
|
-
|
|
74
|
-
declare function getAccount(): Promise<string | undefined>;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Signs and sends a transaction using the Phantom provider.
|
|
78
|
-
* @param transaction The transaction to sign and send (Web3.js format).
|
|
79
|
-
* @returns A promise that resolves with the transaction signature and optionally the public key.
|
|
80
|
-
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
81
|
-
*/
|
|
82
|
-
declare function signAndSendTransaction(transaction: VersionedTransaction | Transaction): Promise<{
|
|
83
|
-
signature: string;
|
|
84
|
-
address?: string;
|
|
85
|
-
}>;
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Signs and sends all transactions using the Phantom provider.
|
|
89
|
-
* @param transactions An array of transactions to sign and send (Web3.js format).
|
|
90
|
-
* @returns A promise that resolves with an array of transaction signatures and optionally the public key.
|
|
91
|
-
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
92
|
-
*/
|
|
93
|
-
declare function signAndSendAllTransactions(transactions: (VersionedTransaction | Transaction)[]): Promise<{
|
|
94
|
-
signatures: string[];
|
|
95
|
-
address?: string;
|
|
96
|
-
}>;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Signs a transaction using the Phantom provider without sending it.
|
|
100
|
-
* @param transaction The transaction to sign (Web3.js format).
|
|
101
|
-
* @returns A promise that resolves with the signed transaction.
|
|
102
|
-
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
103
|
-
*/
|
|
104
|
-
declare function signTransaction(transaction: VersionedTransaction | Transaction): Promise<VersionedTransaction | Transaction>;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Signs all transactions using the Phantom provider.
|
|
108
|
-
* @param transactions An array of transactions to sign (Web3.js format).
|
|
109
|
-
* @returns A promise that resolves with an array of signed transactions.
|
|
110
|
-
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
111
|
-
*/
|
|
112
|
-
declare function signAllTransactions(transactions: (VersionedTransaction | Transaction)[]): Promise<(VersionedTransaction | Transaction)[]>;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Signs in with Solana using the Phantom provider.
|
|
116
|
-
* @param signInData The sign-in data.
|
|
117
|
-
* @returns A promise that resolves with the address, signature, and signed message.
|
|
118
|
-
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
119
|
-
*/
|
|
120
|
-
declare function signIn(signInData: SolanaSignInData): Promise<{
|
|
121
|
-
address: string;
|
|
122
|
-
signature: Uint8Array;
|
|
123
|
-
signedMessage: Uint8Array;
|
|
124
|
-
}>;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Signs a message using the Phantom provider.
|
|
128
|
-
* @param message The message to sign (as a Uint8Array).
|
|
129
|
-
* @param display The display encoding for the message (optional, defaults to utf8).
|
|
130
|
-
* @returns A promise that resolves with the signature and public key.
|
|
131
|
-
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
132
|
-
*/
|
|
133
|
-
declare function signMessage(message: Uint8Array, display?: DisplayEncoding): Promise<{
|
|
134
|
-
signature: Uint8Array;
|
|
135
|
-
address: string;
|
|
136
|
-
}>;
|
|
137
|
-
|
|
138
|
-
type Solana = {
|
|
139
|
-
connect: typeof connect;
|
|
140
|
-
disconnect: typeof disconnect;
|
|
141
|
-
getAccount: typeof getAccount;
|
|
142
|
-
signMessage: typeof signMessage;
|
|
143
|
-
signIn: typeof signIn;
|
|
144
|
-
signTransaction: typeof signTransaction;
|
|
145
|
-
signAllTransactions: typeof signAllTransactions;
|
|
146
|
-
signAndSendTransaction: typeof signAndSendTransaction;
|
|
147
|
-
signAndSendAllTransactions: typeof signAndSendAllTransactions;
|
|
148
|
-
addEventListener: (event: PhantomEventType, callback: PhantomEventCallback) => () => void;
|
|
149
|
-
removeEventListener: (event: PhantomEventType, callback: PhantomEventCallback) => void;
|
|
150
|
-
};
|
|
151
|
-
declare function createSolanaPlugin(): Plugin<Solana>;
|
|
62
|
+
declare function createSolanaPlugin(): Plugin<ISolanaChain>;
|
|
152
63
|
|
|
153
64
|
declare module "../index" {
|
|
154
65
|
interface Phantom {
|
|
155
|
-
solana:
|
|
66
|
+
solana: ISolanaChain;
|
|
156
67
|
}
|
|
157
68
|
}
|
|
158
69
|
|
|
159
|
-
export { PhantomSolanaProvider,
|
|
70
|
+
export { PhantomSolanaProvider, SolanaSignInData, createSolanaPlugin };
|
package/dist/solana/index.js
CHANGED
|
@@ -212,28 +212,26 @@ function addEventListener(event, callback) {
|
|
|
212
212
|
if (!eventCallbacks.has(event)) {
|
|
213
213
|
eventCallbacks.set(event, /* @__PURE__ */ new Set());
|
|
214
214
|
}
|
|
215
|
-
eventCallbacks.get(event)
|
|
215
|
+
eventCallbacks.get(event)?.add(callback);
|
|
216
216
|
return () => {
|
|
217
217
|
removeEventListener(event, callback);
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
220
|
function removeEventListener(event, callback) {
|
|
221
221
|
if (eventCallbacks.has(event)) {
|
|
222
|
-
eventCallbacks.get(event)
|
|
223
|
-
if (eventCallbacks.get(event)
|
|
222
|
+
eventCallbacks.get(event)?.delete(callback);
|
|
223
|
+
if (eventCallbacks.get(event)?.size === 0) {
|
|
224
224
|
eventCallbacks.delete(event);
|
|
225
225
|
}
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
228
|
function triggerEvent(event, ...args) {
|
|
229
229
|
if (eventCallbacks.has(event)) {
|
|
230
|
-
eventCallbacks.get(event)
|
|
231
|
-
|
|
232
|
-
cb(args
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
} else if (event === "accountChanged" && args[0] && typeof args[0] === "string") {
|
|
236
|
-
cb(args[0]);
|
|
230
|
+
eventCallbacks.get(event)?.forEach((cb) => {
|
|
231
|
+
try {
|
|
232
|
+
cb(...args);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
console.error(`Error in ${event} event listener:`, error);
|
|
237
235
|
}
|
|
238
236
|
});
|
|
239
237
|
}
|
|
@@ -334,19 +332,6 @@ async function signAllTransactions(transactions) {
|
|
|
334
332
|
return provider.signAllTransactions(transactions);
|
|
335
333
|
}
|
|
336
334
|
|
|
337
|
-
// src/solana/signIn.ts
|
|
338
|
-
async function signIn(signInData) {
|
|
339
|
-
const provider = await getProvider();
|
|
340
|
-
if (!provider) {
|
|
341
|
-
throw new Error("Provider not found.");
|
|
342
|
-
}
|
|
343
|
-
const result = await provider.signIn(signInData);
|
|
344
|
-
if (result.address) {
|
|
345
|
-
triggerEvent("connect", result.address);
|
|
346
|
-
}
|
|
347
|
-
return result;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
335
|
// src/solana/signMessage.ts
|
|
351
336
|
async function signMessage(message, display) {
|
|
352
337
|
const provider = await getProvider();
|
|
@@ -360,43 +345,112 @@ async function signMessage(message, display) {
|
|
|
360
345
|
}
|
|
361
346
|
|
|
362
347
|
// src/solana/plugin.ts
|
|
363
|
-
var
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
const strategy = await getProvider();
|
|
379
|
-
const provider = strategy.getProvider();
|
|
380
|
-
if (provider) {
|
|
381
|
-
provider.on("connect", (publicKey) => {
|
|
382
|
-
if (publicKey)
|
|
383
|
-
triggerEvent("connect", publicKey.toString());
|
|
384
|
-
});
|
|
385
|
-
provider.on("disconnect", () => triggerEvent("disconnect"));
|
|
386
|
-
provider.on("accountChanged", (publicKey) => {
|
|
387
|
-
if (publicKey)
|
|
388
|
-
triggerEvent("accountChanged", publicKey.toString());
|
|
389
|
-
});
|
|
348
|
+
var Solana = class {
|
|
349
|
+
constructor() {
|
|
350
|
+
this._publicKey = null;
|
|
351
|
+
this.bindProviderEvents();
|
|
352
|
+
}
|
|
353
|
+
get publicKey() {
|
|
354
|
+
return this._publicKey;
|
|
355
|
+
}
|
|
356
|
+
get connected() {
|
|
357
|
+
return this._publicKey !== null;
|
|
358
|
+
}
|
|
359
|
+
async connect(options) {
|
|
360
|
+
const address = await connect(options);
|
|
361
|
+
if (!address) {
|
|
362
|
+
throw new Error("Failed to connect to Solana wallet");
|
|
390
363
|
}
|
|
391
|
-
|
|
364
|
+
this._publicKey = address;
|
|
365
|
+
return { publicKey: address };
|
|
392
366
|
}
|
|
393
|
-
|
|
367
|
+
async disconnect() {
|
|
368
|
+
await disconnect();
|
|
369
|
+
this._publicKey = null;
|
|
370
|
+
}
|
|
371
|
+
async signMessage(message) {
|
|
372
|
+
const messageBytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
373
|
+
const result = await signMessage(messageBytes);
|
|
374
|
+
return {
|
|
375
|
+
signature: result.signature instanceof Uint8Array ? result.signature : new Uint8Array(result.signature),
|
|
376
|
+
publicKey: result.address || this._publicKey || ""
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
signTransaction(transaction) {
|
|
380
|
+
return signTransaction(transaction);
|
|
381
|
+
}
|
|
382
|
+
async signAndSendTransaction(transaction) {
|
|
383
|
+
const result = await signAndSendTransaction(transaction);
|
|
384
|
+
return { signature: result.signature };
|
|
385
|
+
}
|
|
386
|
+
signAllTransactions(transactions) {
|
|
387
|
+
return signAllTransactions(transactions);
|
|
388
|
+
}
|
|
389
|
+
async signAndSendAllTransactions(transactions) {
|
|
390
|
+
const result = await signAndSendAllTransactions(transactions);
|
|
391
|
+
return { signatures: result.signatures };
|
|
392
|
+
}
|
|
393
|
+
async switchNetwork(_network) {
|
|
394
|
+
return Promise.resolve();
|
|
395
|
+
}
|
|
396
|
+
async getPublicKey() {
|
|
397
|
+
if (this._publicKey) {
|
|
398
|
+
return this._publicKey;
|
|
399
|
+
}
|
|
400
|
+
try {
|
|
401
|
+
const account = await getAccount();
|
|
402
|
+
this._publicKey = account || null;
|
|
403
|
+
return this._publicKey;
|
|
404
|
+
} catch {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
isConnected() {
|
|
409
|
+
return this._publicKey !== null;
|
|
410
|
+
}
|
|
411
|
+
on(event, listener) {
|
|
412
|
+
addEventListener(event, listener);
|
|
413
|
+
}
|
|
414
|
+
off(event, listener) {
|
|
415
|
+
removeEventListener(event, listener);
|
|
416
|
+
}
|
|
417
|
+
async bindProviderEvents() {
|
|
418
|
+
try {
|
|
419
|
+
const strategy = await getProvider();
|
|
420
|
+
const provider = strategy.getProvider();
|
|
421
|
+
if (provider) {
|
|
422
|
+
provider.on("connect", (publicKey) => {
|
|
423
|
+
if (publicKey) {
|
|
424
|
+
const pubKey = publicKey.toString();
|
|
425
|
+
this._publicKey = pubKey;
|
|
426
|
+
triggerEvent("connect", pubKey);
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
provider.on("disconnect", () => {
|
|
430
|
+
this._publicKey = null;
|
|
431
|
+
triggerEvent("disconnect");
|
|
432
|
+
});
|
|
433
|
+
provider.on("accountChanged", (publicKey) => {
|
|
434
|
+
if (publicKey) {
|
|
435
|
+
const pubKey = publicKey.toString();
|
|
436
|
+
this._publicKey = pubKey;
|
|
437
|
+
triggerEvent("accountChanged", pubKey);
|
|
438
|
+
triggerEvent("connect", pubKey);
|
|
439
|
+
} else {
|
|
440
|
+
this._publicKey = null;
|
|
441
|
+
triggerEvent("accountChanged", null);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
} catch (error) {
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
};
|
|
394
449
|
function createSolanaPlugin() {
|
|
395
450
|
return {
|
|
396
451
|
name: "solana",
|
|
397
452
|
create: () => {
|
|
398
|
-
|
|
399
|
-
return solana;
|
|
453
|
+
return new Solana();
|
|
400
454
|
}
|
|
401
455
|
};
|
|
402
456
|
}
|