@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.
@@ -179,28 +179,26 @@ function addEventListener(event, callback) {
179
179
  if (!eventCallbacks.has(event)) {
180
180
  eventCallbacks.set(event, /* @__PURE__ */ new Set());
181
181
  }
182
- eventCallbacks.get(event).add(callback);
182
+ eventCallbacks.get(event)?.add(callback);
183
183
  return () => {
184
184
  removeEventListener(event, callback);
185
185
  };
186
186
  }
187
187
  function removeEventListener(event, callback) {
188
188
  if (eventCallbacks.has(event)) {
189
- eventCallbacks.get(event).delete(callback);
190
- if (eventCallbacks.get(event).size === 0) {
189
+ eventCallbacks.get(event)?.delete(callback);
190
+ if (eventCallbacks.get(event)?.size === 0) {
191
191
  eventCallbacks.delete(event);
192
192
  }
193
193
  }
194
194
  }
195
195
  function triggerEvent(event, ...args) {
196
196
  if (eventCallbacks.has(event)) {
197
- eventCallbacks.get(event).forEach((cb) => {
198
- if (event === "connect" && args[0] && typeof args[0] === "string") {
199
- cb(args[0]);
200
- } else if (event === "disconnect") {
201
- cb();
202
- } else if (event === "accountChanged" && args[0] && typeof args[0] === "string") {
203
- cb(args[0]);
197
+ eventCallbacks.get(event)?.forEach((cb) => {
198
+ try {
199
+ cb(...args);
200
+ } catch (error) {
201
+ console.error(`Error in ${event} event listener:`, error);
204
202
  }
205
203
  });
206
204
  }
@@ -301,19 +299,6 @@ async function signAllTransactions(transactions) {
301
299
  return provider.signAllTransactions(transactions);
302
300
  }
303
301
 
304
- // src/solana/signIn.ts
305
- async function signIn(signInData) {
306
- const provider = await getProvider();
307
- if (!provider) {
308
- throw new Error("Provider not found.");
309
- }
310
- const result = await provider.signIn(signInData);
311
- if (result.address) {
312
- triggerEvent("connect", result.address);
313
- }
314
- return result;
315
- }
316
-
317
302
  // src/solana/signMessage.ts
318
303
  async function signMessage(message, display) {
319
304
  const provider = await getProvider();
@@ -327,43 +312,112 @@ async function signMessage(message, display) {
327
312
  }
328
313
 
329
314
  // src/solana/plugin.ts
330
- var solana = {
331
- connect,
332
- disconnect,
333
- getAccount,
334
- signMessage,
335
- signIn,
336
- signTransaction,
337
- signAllTransactions,
338
- signAndSendTransaction,
339
- signAndSendAllTransactions,
340
- addEventListener,
341
- removeEventListener
342
- };
343
- async function bindProviderEvents() {
344
- try {
345
- const strategy = await getProvider();
346
- const provider = strategy.getProvider();
347
- if (provider) {
348
- provider.on("connect", (publicKey) => {
349
- if (publicKey)
350
- triggerEvent("connect", publicKey.toString());
351
- });
352
- provider.on("disconnect", () => triggerEvent("disconnect"));
353
- provider.on("accountChanged", (publicKey) => {
354
- if (publicKey)
355
- triggerEvent("accountChanged", publicKey.toString());
356
- });
315
+ var Solana = class {
316
+ constructor() {
317
+ this._publicKey = null;
318
+ this.bindProviderEvents();
319
+ }
320
+ get publicKey() {
321
+ return this._publicKey;
322
+ }
323
+ get connected() {
324
+ return this._publicKey !== null;
325
+ }
326
+ async connect(options) {
327
+ const address = await connect(options);
328
+ if (!address) {
329
+ throw new Error("Failed to connect to Solana wallet");
357
330
  }
358
- } catch (error) {
331
+ this._publicKey = address;
332
+ return { publicKey: address };
359
333
  }
360
- }
334
+ async disconnect() {
335
+ await disconnect();
336
+ this._publicKey = null;
337
+ }
338
+ async signMessage(message) {
339
+ const messageBytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
340
+ const result = await signMessage(messageBytes);
341
+ return {
342
+ signature: result.signature instanceof Uint8Array ? result.signature : new Uint8Array(result.signature),
343
+ publicKey: result.address || this._publicKey || ""
344
+ };
345
+ }
346
+ signTransaction(transaction) {
347
+ return signTransaction(transaction);
348
+ }
349
+ async signAndSendTransaction(transaction) {
350
+ const result = await signAndSendTransaction(transaction);
351
+ return { signature: result.signature };
352
+ }
353
+ signAllTransactions(transactions) {
354
+ return signAllTransactions(transactions);
355
+ }
356
+ async signAndSendAllTransactions(transactions) {
357
+ const result = await signAndSendAllTransactions(transactions);
358
+ return { signatures: result.signatures };
359
+ }
360
+ async switchNetwork(_network) {
361
+ return Promise.resolve();
362
+ }
363
+ async getPublicKey() {
364
+ if (this._publicKey) {
365
+ return this._publicKey;
366
+ }
367
+ try {
368
+ const account = await getAccount();
369
+ this._publicKey = account || null;
370
+ return this._publicKey;
371
+ } catch {
372
+ return null;
373
+ }
374
+ }
375
+ isConnected() {
376
+ return this._publicKey !== null;
377
+ }
378
+ on(event, listener) {
379
+ addEventListener(event, listener);
380
+ }
381
+ off(event, listener) {
382
+ removeEventListener(event, listener);
383
+ }
384
+ async bindProviderEvents() {
385
+ try {
386
+ const strategy = await getProvider();
387
+ const provider = strategy.getProvider();
388
+ if (provider) {
389
+ provider.on("connect", (publicKey) => {
390
+ if (publicKey) {
391
+ const pubKey = publicKey.toString();
392
+ this._publicKey = pubKey;
393
+ triggerEvent("connect", pubKey);
394
+ }
395
+ });
396
+ provider.on("disconnect", () => {
397
+ this._publicKey = null;
398
+ triggerEvent("disconnect");
399
+ });
400
+ provider.on("accountChanged", (publicKey) => {
401
+ if (publicKey) {
402
+ const pubKey = publicKey.toString();
403
+ this._publicKey = pubKey;
404
+ triggerEvent("accountChanged", pubKey);
405
+ triggerEvent("connect", pubKey);
406
+ } else {
407
+ this._publicKey = null;
408
+ triggerEvent("accountChanged", null);
409
+ }
410
+ });
411
+ }
412
+ } catch (error) {
413
+ }
414
+ }
415
+ };
361
416
  function createSolanaPlugin() {
362
417
  return {
363
418
  name: "solana",
364
419
  create: () => {
365
- bindProviderEvents();
366
- return solana;
420
+ return new Solana();
367
421
  }
368
422
  };
369
423
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phantom/browser-injected-sdk",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/phantom/phantom-connect-sdk",
@@ -47,13 +47,16 @@
47
47
  "prettier": "prettier --write \"src/**/*.{ts,tsx}\""
48
48
  },
49
49
  "dependencies": {
50
- "@phantom/constants": "^1.0.0-beta.12",
51
- "@phantom/sdk-types": "^1.0.0-beta.12"
50
+ "@phantom/chain-interfaces": "^1.0.2",
51
+ "@phantom/constants": "^1.0.2",
52
+ "@phantom/sdk-types": "^1.0.2"
52
53
  },
53
54
  "devDependencies": {
55
+ "@jest/fake-timers": "^29.7.0",
54
56
  "@types/jest": "^29.5.14",
55
57
  "eslint": "8.53.0",
56
58
  "jest": "^29.7.0",
59
+ "jest-environment-jsdom": "^29.7.0",
57
60
  "prettier": "^3.5.2",
58
61
  "rimraf": "^6.0.1",
59
62
  "tsup": "^6.7.0",
@@ -1,208 +0,0 @@
1
- declare function isInstalled(): boolean;
2
-
3
- type Extension = {
4
- isInstalled: typeof isInstalled;
5
- };
6
- declare function createExtensionPlugin(): Plugin<Extension>;
7
-
8
- declare module "../index" {
9
- interface Phantom {
10
- extension: Extension;
11
- }
12
- }
13
-
14
- declare function connect({ onlyIfTrusted }?: {
15
- onlyIfTrusted?: boolean | undefined;
16
- }): Promise<string[]>;
17
-
18
- declare function disconnect(): Promise<void>;
19
-
20
- type EthereumTransaction = {
21
- to?: string;
22
- from?: string;
23
- value?: string;
24
- gas?: string;
25
- gasPrice?: string;
26
- maxFeePerGas?: string;
27
- maxPriorityFeePerGas?: string;
28
- data?: string;
29
- nonce?: string;
30
- type?: string;
31
- chainId?: string;
32
- };
33
- type EthereumSignInData = {
34
- domain?: string;
35
- address?: string;
36
- statement?: string;
37
- uri?: string;
38
- version?: string;
39
- chainId?: string;
40
- nonce?: string;
41
- issuedAt?: string;
42
- expirationTime?: string;
43
- notBefore?: string;
44
- requestId?: string;
45
- resources?: string[];
46
- };
47
- type EthereumEventType = "connect" | "disconnect" | "accountsChanged" | "chainChanged";
48
- interface PhantomEthereumProvider {
49
- isPhantom: boolean;
50
- selectedAddress: string | null;
51
- chainId: string;
52
- isConnected: boolean;
53
- request: (args: {
54
- method: string;
55
- params?: any[];
56
- }) => Promise<any>;
57
- on: (event: EthereumEventType, handler: (...args: any[]) => void) => void;
58
- off: (event: EthereumEventType, handler: (...args: any[]) => void) => void;
59
- removeAllListeners: (event?: EthereumEventType) => void;
60
- }
61
-
62
- type PhantomEthereumEventCallback = (data: any) => void;
63
-
64
- declare function getAccounts(): Promise<string[]>;
65
-
66
- /**
67
- * Signs a message using the Phantom Ethereum provider.
68
- * @param message The message to sign (as a string).
69
- * @param address The address to sign with.
70
- * @returns A promise that resolves with the signature.
71
- * @throws Error if Phantom provider is not found or if the operation fails.
72
- */
73
- declare function signMessage(message: string, address: string): Promise<string>;
74
- /**
75
- * Signs a personal message using the Phantom Ethereum provider.
76
- * @param message The message to sign (as a string).
77
- * @param address The address to sign with.
78
- * @returns A promise that resolves with the signature.
79
- * @throws Error if Phantom provider is not found or if the operation fails.
80
- */
81
- declare function signPersonalMessage(message: string, address: string): Promise<string>;
82
- /**
83
- * Signs typed data using the Phantom Ethereum provider.
84
- * @param typedData The typed data to sign.
85
- * @param address The address to sign with.
86
- * @returns A promise that resolves with the signature.
87
- * @throws Error if Phantom provider is not found or if the operation fails.
88
- */
89
- declare function signTypedData(typedData: any, address: string): Promise<string>;
90
-
91
- /**
92
- * Signs in using the Phantom Ethereum provider.
93
- * @param signInData The sign-in data.
94
- * @returns A promise that resolves with the signature data.
95
- * @throws Error if Phantom provider is not found or if the operation fails.
96
- */
97
- declare function signIn(signInData: EthereumSignInData): Promise<{
98
- address: string;
99
- signature: string;
100
- signedMessage: string;
101
- }>;
102
-
103
- /**
104
- * Sends a transaction using the Phantom Ethereum provider.
105
- * @param transaction The transaction to send.
106
- * @returns A promise that resolves with the transaction hash.
107
- * @throws Error if Phantom provider is not found or if the operation fails.
108
- */
109
- declare function sendTransaction(transaction: EthereumTransaction): Promise<string>;
110
- /**
111
- * Signs a transaction using the Phantom Ethereum provider.
112
- * @param transaction The transaction to sign.
113
- * @returns A promise that resolves with the signed transaction.
114
- * @throws Error if Phantom provider is not found or if the operation fails.
115
- */
116
- declare function signTransaction(transaction: EthereumTransaction): Promise<string>;
117
-
118
- /**
119
- * Gets the current chain ID.
120
- * @returns A promise that resolves with the chain ID.
121
- * @throws Error if Phantom provider is not found or if the operation fails.
122
- */
123
- declare function getChainId(): Promise<string>;
124
- /**
125
- * Switches to a different chain.
126
- * @param chainId The chain ID to switch to.
127
- * @returns A promise that resolves when the switch is complete.
128
- * @throws Error if Phantom provider is not found or if the operation fails.
129
- */
130
- declare function switchChain(chainId: string): Promise<void>;
131
-
132
- declare enum ProviderStrategy {
133
- INJECTED = "injected"
134
- }
135
-
136
- interface EthereumStrategy {
137
- readonly type: ProviderStrategy;
138
- isConnected: boolean;
139
- getProvider: () => PhantomEthereumProvider | null;
140
- connect: ({ onlyIfTrusted }: {
141
- onlyIfTrusted: boolean;
142
- }) => Promise<string[] | undefined>;
143
- disconnect: () => Promise<void>;
144
- getAccounts: () => Promise<string[]>;
145
- signMessage: (message: string, address: string) => Promise<string>;
146
- signPersonalMessage: (message: string, address: string) => Promise<string>;
147
- signTypedData: (typedData: any, address: string) => Promise<string>;
148
- signIn: (signInData: EthereumSignInData) => Promise<{
149
- address: string;
150
- signature: string;
151
- signedMessage: string;
152
- }>;
153
- sendTransaction: (transaction: EthereumTransaction) => Promise<string>;
154
- signTransaction: (transaction: EthereumTransaction) => Promise<string>;
155
- getChainId: () => Promise<string>;
156
- switchChain: (chainId: string) => Promise<void>;
157
- request: <T = any>(args: {
158
- method: string;
159
- params?: unknown[];
160
- }) => Promise<T>;
161
- }
162
-
163
- /**
164
- * Retrieves Phantom Ethereum provider and returns it if it exists.
165
- * @returns Phantom Ethereum provider or throws error if it doesn't exist.
166
- */
167
- declare function getProvider(strategy?: ProviderStrategy): Promise<EthereumStrategy>;
168
-
169
- type Ethereum = {
170
- connect: typeof connect;
171
- disconnect: typeof disconnect;
172
- getAccounts: typeof getAccounts;
173
- signMessage: typeof signMessage;
174
- signPersonalMessage: typeof signPersonalMessage;
175
- signTypedData: typeof signTypedData;
176
- signIn: typeof signIn;
177
- sendTransaction: typeof sendTransaction;
178
- signTransaction: typeof signTransaction;
179
- getChainId: typeof getChainId;
180
- switchChain: typeof switchChain;
181
- getProvider: typeof getProvider;
182
- addEventListener: (event: EthereumEventType, callback: PhantomEthereumEventCallback) => () => void;
183
- removeEventListener: (event: EthereumEventType, callback: PhantomEthereumEventCallback) => void;
184
- };
185
- declare function createEthereumPlugin(): Plugin<Ethereum>;
186
-
187
- declare module "../index" {
188
- interface Phantom {
189
- ethereum: Ethereum;
190
- }
191
- }
192
-
193
- type Plugin<T> = {
194
- name: string;
195
- create: () => T;
196
- };
197
- type CreatePhantomConfig = {
198
- plugins?: Plugin<unknown>[];
199
- };
200
- interface Phantom {
201
- }
202
- /**
203
- * Creates a Phantom instance with the provided plugins.
204
- * Each plugin extends the Phantom interface via declaration merging.
205
- */
206
- declare function createPhantom({ plugins }: CreatePhantomConfig): Phantom;
207
-
208
- export { CreatePhantomConfig as C, Ethereum as E, Plugin as P, PhantomEthereumProvider as a, EthereumTransaction as b, createEthereumPlugin as c, EthereumSignInData as d, EthereumEventType as e, Phantom as f, createPhantom as g, createExtensionPlugin as h, Extension as i, isInstalled as j };