@phantom/browser-sdk 0.3.4 → 0.3.6
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/README.md +158 -15
- package/dist/index.d.ts +55 -17
- package/dist/index.js +386 -59
- package/dist/index.mjs +386 -59
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -86,8 +86,12 @@ var InjectedProvider = class {
|
|
|
86
86
|
constructor(config) {
|
|
87
87
|
this.connected = false;
|
|
88
88
|
this.addresses = [];
|
|
89
|
+
// Event management
|
|
90
|
+
this.eventListeners = /* @__PURE__ */ new Map();
|
|
91
|
+
this.browserInjectedCleanupFunctions = [];
|
|
92
|
+
this.eventsInitialized = false;
|
|
89
93
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Initializing InjectedProvider", { config });
|
|
90
|
-
this.addressTypes = config.addressTypes
|
|
94
|
+
this.addressTypes = config.addressTypes;
|
|
91
95
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Address types configured", { addressTypes: this.addressTypes });
|
|
92
96
|
const plugins = [createExtensionPlugin()];
|
|
93
97
|
if (this.addressTypes.includes(AddressType.solana)) {
|
|
@@ -110,70 +114,108 @@ var InjectedProvider = class {
|
|
|
110
114
|
authOptionsIgnored: !!authOptions
|
|
111
115
|
// Note: authOptions are ignored for injected provider
|
|
112
116
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
this.emit("connect_start", {
|
|
118
|
+
source: "manual-connect",
|
|
119
|
+
providerType: "injected"
|
|
120
|
+
});
|
|
121
|
+
try {
|
|
122
|
+
if (!this.phantom.extension.isInstalled()) {
|
|
123
|
+
debug.error(DebugCategory.INJECTED_PROVIDER, "Phantom wallet extension not found");
|
|
124
|
+
const error = new Error("Phantom wallet not found");
|
|
125
|
+
this.emit("connect_error", {
|
|
126
|
+
error: error.message,
|
|
127
|
+
source: "manual-connect"
|
|
128
|
+
});
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Phantom extension detected");
|
|
132
|
+
const connectedAddresses = [];
|
|
133
|
+
if (this.addressTypes.includes(AddressType.solana)) {
|
|
134
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Attempting Solana connection");
|
|
135
|
+
try {
|
|
136
|
+
const publicKey = await this.phantom.solana.connect();
|
|
137
|
+
if (publicKey) {
|
|
138
|
+
connectedAddresses.push({
|
|
139
|
+
addressType: AddressType.solana,
|
|
140
|
+
address: publicKey
|
|
141
|
+
});
|
|
142
|
+
debug.info(DebugCategory.INJECTED_PROVIDER, "Solana connected successfully", { address: publicKey });
|
|
143
|
+
}
|
|
144
|
+
} catch (err) {
|
|
145
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Solana", { error: err });
|
|
129
146
|
}
|
|
130
|
-
} catch (err) {
|
|
131
|
-
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Solana", { error: err });
|
|
132
147
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
148
|
+
if (this.addressTypes.includes(AddressType.ethereum)) {
|
|
149
|
+
try {
|
|
150
|
+
const accounts = await this.phantom.ethereum.connect();
|
|
151
|
+
if (accounts && accounts.length > 0) {
|
|
152
|
+
connectedAddresses.push(
|
|
153
|
+
...accounts.map((address) => ({
|
|
154
|
+
addressType: AddressType.ethereum,
|
|
155
|
+
address
|
|
156
|
+
}))
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
} catch (err) {
|
|
160
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Ethereum", { error: err });
|
|
144
161
|
}
|
|
145
|
-
} catch (err) {
|
|
146
|
-
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Ethereum", { error: err });
|
|
147
162
|
}
|
|
163
|
+
if (connectedAddresses.length === 0) {
|
|
164
|
+
const error = new Error("Failed to connect to any supported wallet provider");
|
|
165
|
+
this.emit("connect_error", {
|
|
166
|
+
error: error.message,
|
|
167
|
+
source: "manual-connect"
|
|
168
|
+
});
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
this.addresses = connectedAddresses;
|
|
172
|
+
this.connected = true;
|
|
173
|
+
const result = {
|
|
174
|
+
addresses: this.addresses,
|
|
175
|
+
status: "completed"
|
|
176
|
+
// walletId is not applicable for injected providers
|
|
177
|
+
};
|
|
178
|
+
this.emit("connect", {
|
|
179
|
+
addresses: this.addresses,
|
|
180
|
+
source: "manual-connect"
|
|
181
|
+
});
|
|
182
|
+
return result;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (error instanceof Error && !error.message.includes("Phantom wallet not found") && !error.message.includes("Failed to connect to any supported wallet provider")) {
|
|
185
|
+
this.emit("connect_error", {
|
|
186
|
+
error: error.message,
|
|
187
|
+
source: "manual-connect"
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
throw error;
|
|
148
191
|
}
|
|
149
|
-
if (connectedAddresses.length === 0) {
|
|
150
|
-
throw new Error("Failed to connect to any supported wallet provider");
|
|
151
|
-
}
|
|
152
|
-
this.addresses = connectedAddresses;
|
|
153
|
-
this.connected = true;
|
|
154
|
-
return {
|
|
155
|
-
addresses: this.addresses,
|
|
156
|
-
status: "completed"
|
|
157
|
-
// walletId is not applicable for injected providers
|
|
158
|
-
};
|
|
159
192
|
}
|
|
160
193
|
async disconnect() {
|
|
194
|
+
debug.info(DebugCategory.INJECTED_PROVIDER, "Starting injected provider disconnect");
|
|
161
195
|
if (this.addressTypes.includes(AddressType.solana)) {
|
|
162
196
|
try {
|
|
163
197
|
await this.phantom.solana.disconnect();
|
|
198
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana disconnected successfully");
|
|
164
199
|
} catch (err) {
|
|
165
|
-
|
|
200
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to disconnect Solana", { error: err });
|
|
166
201
|
}
|
|
167
202
|
}
|
|
168
203
|
if (this.addressTypes.includes(AddressType.ethereum)) {
|
|
169
204
|
try {
|
|
170
205
|
await this.phantom.ethereum.disconnect();
|
|
206
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum disconnected successfully");
|
|
171
207
|
} catch (err) {
|
|
172
|
-
|
|
208
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to disconnect Ethereum", { error: err });
|
|
173
209
|
}
|
|
174
210
|
}
|
|
211
|
+
this.browserInjectedCleanupFunctions.forEach((cleanup) => cleanup());
|
|
212
|
+
this.browserInjectedCleanupFunctions = [];
|
|
175
213
|
this.connected = false;
|
|
176
214
|
this.addresses = [];
|
|
215
|
+
this.emit("disconnect", {
|
|
216
|
+
source: "manual-disconnect"
|
|
217
|
+
});
|
|
218
|
+
debug.info(DebugCategory.INJECTED_PROVIDER, "Injected provider disconnected successfully");
|
|
177
219
|
}
|
|
178
220
|
async signMessage(params) {
|
|
179
221
|
if (!this.connected) {
|
|
@@ -246,6 +288,139 @@ var InjectedProvider = class {
|
|
|
246
288
|
isConnected() {
|
|
247
289
|
return this.connected;
|
|
248
290
|
}
|
|
291
|
+
// Event management methods - implementing unified event interface
|
|
292
|
+
on(event, callback) {
|
|
293
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Adding event listener", { event });
|
|
294
|
+
if (!this.eventsInitialized) {
|
|
295
|
+
this.setupBrowserInjectedEvents();
|
|
296
|
+
this.eventsInitialized = true;
|
|
297
|
+
}
|
|
298
|
+
if (!this.eventListeners.has(event)) {
|
|
299
|
+
this.eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
300
|
+
}
|
|
301
|
+
this.eventListeners.get(event).add(callback);
|
|
302
|
+
}
|
|
303
|
+
off(event, callback) {
|
|
304
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Removing event listener", { event });
|
|
305
|
+
if (this.eventListeners.has(event)) {
|
|
306
|
+
this.eventListeners.get(event).delete(callback);
|
|
307
|
+
if (this.eventListeners.get(event).size === 0) {
|
|
308
|
+
this.eventListeners.delete(event);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
emit(event, data) {
|
|
313
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Emitting event", {
|
|
314
|
+
event,
|
|
315
|
+
listenerCount: this.eventListeners.get(event)?.size || 0,
|
|
316
|
+
data
|
|
317
|
+
});
|
|
318
|
+
const listeners = this.eventListeners.get(event);
|
|
319
|
+
if (listeners && listeners.size > 0) {
|
|
320
|
+
listeners.forEach((callback) => {
|
|
321
|
+
try {
|
|
322
|
+
callback(data);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
debug.error(DebugCategory.INJECTED_PROVIDER, "Event callback error", { event, error });
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
setupBrowserInjectedEvents() {
|
|
330
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up browser-injected-sdk event listeners");
|
|
331
|
+
if (this.addressTypes.includes(AddressType.solana) && this.phantom.solana) {
|
|
332
|
+
this.setupSolanaEvents();
|
|
333
|
+
}
|
|
334
|
+
if (this.addressTypes.includes(AddressType.ethereum) && this.phantom.ethereum) {
|
|
335
|
+
this.setupEthereumEvents();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
setupSolanaEvents() {
|
|
339
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Solana event listeners");
|
|
340
|
+
const solanaConnectCleanup = this.phantom.solana.addEventListener("connect", (publicKey) => {
|
|
341
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana connect event received", { publicKey });
|
|
342
|
+
const solanaAddress = { addressType: AddressType.solana, address: publicKey };
|
|
343
|
+
if (!this.addresses.find((addr) => addr.addressType === AddressType.solana)) {
|
|
344
|
+
this.addresses.push(solanaAddress);
|
|
345
|
+
}
|
|
346
|
+
this.connected = true;
|
|
347
|
+
this.emit("connect", {
|
|
348
|
+
addresses: this.addresses,
|
|
349
|
+
source: "injected-extension"
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
const solanaDisconnectCleanup = this.phantom.solana.addEventListener("disconnect", () => {
|
|
353
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana disconnect event received");
|
|
354
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== AddressType.solana);
|
|
355
|
+
this.connected = this.addresses.length > 0;
|
|
356
|
+
this.emit("disconnect", {
|
|
357
|
+
source: "injected-extension"
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
const solanaAccountChangedCleanup = this.phantom.solana.addEventListener("accountChanged", (publicKey) => {
|
|
361
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana account changed event received", { publicKey });
|
|
362
|
+
const solanaIndex = this.addresses.findIndex((addr) => addr.addressType === AddressType.solana);
|
|
363
|
+
if (solanaIndex >= 0) {
|
|
364
|
+
this.addresses[solanaIndex] = { addressType: AddressType.solana, address: publicKey };
|
|
365
|
+
} else {
|
|
366
|
+
this.addresses.push({ addressType: AddressType.solana, address: publicKey });
|
|
367
|
+
}
|
|
368
|
+
this.emit("connect", {
|
|
369
|
+
addresses: this.addresses,
|
|
370
|
+
source: "injected-extension-account-change"
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
this.browserInjectedCleanupFunctions.push(
|
|
374
|
+
solanaConnectCleanup,
|
|
375
|
+
solanaDisconnectCleanup,
|
|
376
|
+
solanaAccountChangedCleanup
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
setupEthereumEvents() {
|
|
380
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Ethereum event listeners");
|
|
381
|
+
const ethConnectCleanup = this.phantom.ethereum.addEventListener("connect", (accounts) => {
|
|
382
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum connect event received", { accounts });
|
|
383
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== AddressType.ethereum);
|
|
384
|
+
if (accounts && accounts.length > 0) {
|
|
385
|
+
this.addresses.push(...accounts.map((address) => ({
|
|
386
|
+
addressType: AddressType.ethereum,
|
|
387
|
+
address
|
|
388
|
+
})));
|
|
389
|
+
}
|
|
390
|
+
this.connected = this.addresses.length > 0;
|
|
391
|
+
this.emit("connect", {
|
|
392
|
+
addresses: this.addresses,
|
|
393
|
+
source: "injected-extension"
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
const ethDisconnectCleanup = this.phantom.ethereum.addEventListener("disconnect", () => {
|
|
397
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum disconnect event received");
|
|
398
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== AddressType.ethereum);
|
|
399
|
+
this.connected = this.addresses.length > 0;
|
|
400
|
+
this.emit("disconnect", {
|
|
401
|
+
source: "injected-extension"
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
const ethAccountChangedCleanup = this.phantom.ethereum.addEventListener("accountChanged", (accounts) => {
|
|
405
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum account changed event received", { accounts });
|
|
406
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== AddressType.ethereum);
|
|
407
|
+
if (accounts && accounts.length > 0) {
|
|
408
|
+
this.addresses.push(...accounts.map((address) => ({
|
|
409
|
+
addressType: AddressType.ethereum,
|
|
410
|
+
address
|
|
411
|
+
})));
|
|
412
|
+
}
|
|
413
|
+
this.emit("connect", {
|
|
414
|
+
addresses: this.addresses,
|
|
415
|
+
source: "injected-extension-account-change"
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
this.browserInjectedCleanupFunctions.push(
|
|
419
|
+
ethConnectCleanup,
|
|
420
|
+
ethDisconnectCleanup,
|
|
421
|
+
ethAccountChangedCleanup
|
|
422
|
+
);
|
|
423
|
+
}
|
|
249
424
|
};
|
|
250
425
|
|
|
251
426
|
// src/providers/embedded/index.ts
|
|
@@ -615,11 +790,15 @@ var EmbeddedProvider = class extends CoreEmbeddedProvider {
|
|
|
615
790
|
|
|
616
791
|
// src/ProviderManager.ts
|
|
617
792
|
var ProviderManager = class {
|
|
793
|
+
// Track which providers have forwarding set up
|
|
618
794
|
constructor(config) {
|
|
619
795
|
this.providers = /* @__PURE__ */ new Map();
|
|
620
796
|
this.currentProvider = null;
|
|
621
797
|
this.currentProviderKey = null;
|
|
622
798
|
this.walletId = null;
|
|
799
|
+
// Event management for forwarding provider events
|
|
800
|
+
this.eventListeners = /* @__PURE__ */ new Map();
|
|
801
|
+
this.providerForwardingSetup = /* @__PURE__ */ new WeakSet();
|
|
623
802
|
debug.log(DebugCategory.PROVIDER_MANAGER, "Initializing ProviderManager", { config });
|
|
624
803
|
this.config = config;
|
|
625
804
|
debug.log(DebugCategory.PROVIDER_MANAGER, "Setting default provider");
|
|
@@ -644,6 +823,7 @@ var ProviderManager = class {
|
|
|
644
823
|
this.currentProvider = this.providers.get(key);
|
|
645
824
|
this.currentProviderKey = key;
|
|
646
825
|
this.walletId = null;
|
|
826
|
+
this.ensureProviderEventForwarding();
|
|
647
827
|
return this.currentProvider;
|
|
648
828
|
}
|
|
649
829
|
/**
|
|
@@ -738,6 +918,79 @@ var ProviderManager = class {
|
|
|
738
918
|
getWalletId() {
|
|
739
919
|
return this.walletId;
|
|
740
920
|
}
|
|
921
|
+
/**
|
|
922
|
+
* Add event listener - stores callback and ensures current provider forwards events to ProviderManager
|
|
923
|
+
*/
|
|
924
|
+
on(event, callback) {
|
|
925
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Adding event listener", { event });
|
|
926
|
+
if (!this.eventListeners.has(event)) {
|
|
927
|
+
this.eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
928
|
+
}
|
|
929
|
+
this.eventListeners.get(event).add(callback);
|
|
930
|
+
this.ensureProviderEventForwarding();
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* Remove event listener
|
|
934
|
+
*/
|
|
935
|
+
off(event, callback) {
|
|
936
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Removing event listener", { event });
|
|
937
|
+
if (this.eventListeners.has(event)) {
|
|
938
|
+
this.eventListeners.get(event).delete(callback);
|
|
939
|
+
if (this.eventListeners.get(event).size === 0) {
|
|
940
|
+
this.eventListeners.delete(event);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
* Emit event to all registered callbacks
|
|
946
|
+
*/
|
|
947
|
+
emit(event, data) {
|
|
948
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Emitting event to stored callbacks", {
|
|
949
|
+
event,
|
|
950
|
+
listenerCount: this.eventListeners.get(event)?.size || 0,
|
|
951
|
+
data
|
|
952
|
+
});
|
|
953
|
+
const listeners = this.eventListeners.get(event);
|
|
954
|
+
if (listeners && listeners.size > 0) {
|
|
955
|
+
listeners.forEach((callback) => {
|
|
956
|
+
try {
|
|
957
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Calling stored callback for event", { event });
|
|
958
|
+
callback(data);
|
|
959
|
+
} catch (error) {
|
|
960
|
+
debug.error(DebugCategory.PROVIDER_MANAGER, "Event callback error", { event, error });
|
|
961
|
+
}
|
|
962
|
+
});
|
|
963
|
+
} else {
|
|
964
|
+
debug.warn(DebugCategory.PROVIDER_MANAGER, "No stored callbacks for event", { event });
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Ensure current provider forwards its events to this ProviderManager
|
|
969
|
+
* Only sets up forwarding once per provider instance to avoid accumulation
|
|
970
|
+
*/
|
|
971
|
+
ensureProviderEventForwarding() {
|
|
972
|
+
if (!this.currentProvider || !("on" in this.currentProvider)) {
|
|
973
|
+
debug.warn(DebugCategory.PROVIDER_MANAGER, "Current provider does not support events", {
|
|
974
|
+
providerType: this.getCurrentProviderInfo()?.type
|
|
975
|
+
});
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
if (this.providerForwardingSetup.has(this.currentProvider)) {
|
|
979
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Event forwarding already set up for current provider");
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Setting up event forwarding from current provider");
|
|
983
|
+
const eventsToForward = ["connect_start", "connect", "connect_error", "disconnect", "error"];
|
|
984
|
+
for (const event of eventsToForward) {
|
|
985
|
+
const forwardingCallback = (data) => {
|
|
986
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Forwarding event from provider", { event, data });
|
|
987
|
+
this.emit(event, data);
|
|
988
|
+
};
|
|
989
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Attaching forwarding callback for event", { event });
|
|
990
|
+
this.currentProvider.on(event, forwardingCallback);
|
|
991
|
+
}
|
|
992
|
+
this.providerForwardingSetup.add(this.currentProvider);
|
|
993
|
+
}
|
|
741
994
|
/**
|
|
742
995
|
* Set default provider based on initial config
|
|
743
996
|
*/
|
|
@@ -758,7 +1011,7 @@ var ProviderManager = class {
|
|
|
758
1011
|
if (type === "injected") {
|
|
759
1012
|
provider = new InjectedProvider({
|
|
760
1013
|
solanaProvider: this.config.solanaProvider || "web3js",
|
|
761
|
-
addressTypes: this.config.addressTypes
|
|
1014
|
+
addressTypes: this.config.addressTypes
|
|
762
1015
|
});
|
|
763
1016
|
} else {
|
|
764
1017
|
if (!this.config.apiBaseUrl || !this.config.organizationId) {
|
|
@@ -769,7 +1022,7 @@ var ProviderManager = class {
|
|
|
769
1022
|
organizationId: this.config.organizationId,
|
|
770
1023
|
authOptions: this.config.authOptions,
|
|
771
1024
|
embeddedWalletType: embeddedWalletType || "app-wallet",
|
|
772
|
-
addressTypes: this.config.addressTypes
|
|
1025
|
+
addressTypes: this.config.addressTypes,
|
|
773
1026
|
solanaProvider: this.config.solanaProvider || "web3js",
|
|
774
1027
|
appLogo: this.config.appLogo,
|
|
775
1028
|
// Optional app logo URL
|
|
@@ -824,20 +1077,10 @@ var ProviderManager = class {
|
|
|
824
1077
|
import { isPhantomExtensionInstalled } from "@phantom/browser-injected-sdk";
|
|
825
1078
|
var BrowserSDK = class {
|
|
826
1079
|
constructor(config) {
|
|
827
|
-
if (config.debug?.enabled) {
|
|
828
|
-
debug.enable();
|
|
829
|
-
if (config.debug.level !== void 0) {
|
|
830
|
-
debug.setLevel(config.debug.level);
|
|
831
|
-
}
|
|
832
|
-
if (config.debug.callback) {
|
|
833
|
-
debug.setCallback(config.debug.callback);
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
1080
|
debug.info(DebugCategory.BROWSER_SDK, "Initializing BrowserSDK", {
|
|
837
1081
|
providerType: config.providerType,
|
|
838
1082
|
embeddedWalletType: config.embeddedWalletType,
|
|
839
|
-
addressTypes: config.addressTypes
|
|
840
|
-
debugEnabled: config.debug?.enabled
|
|
1083
|
+
addressTypes: config.addressTypes
|
|
841
1084
|
});
|
|
842
1085
|
if (!["injected", "embedded"].includes(config.providerType)) {
|
|
843
1086
|
debug.error(DebugCategory.BROWSER_SDK, "Invalid providerType", { providerType: config.providerType });
|
|
@@ -991,13 +1234,97 @@ var BrowserSDK = class {
|
|
|
991
1234
|
getWalletId() {
|
|
992
1235
|
return this.providerManager.getWalletId();
|
|
993
1236
|
}
|
|
1237
|
+
/**
|
|
1238
|
+
* Add event listener for provider events (connect, connect_start, connect_error, disconnect, error)
|
|
1239
|
+
* Works with both embedded and injected providers
|
|
1240
|
+
*/
|
|
1241
|
+
on(event, callback) {
|
|
1242
|
+
debug.log(DebugCategory.BROWSER_SDK, "Adding event listener", { event });
|
|
1243
|
+
this.providerManager.on(event, callback);
|
|
1244
|
+
}
|
|
1245
|
+
/**
|
|
1246
|
+
* Remove event listener for provider events
|
|
1247
|
+
* Works with both embedded and injected providers
|
|
1248
|
+
*/
|
|
1249
|
+
off(event, callback) {
|
|
1250
|
+
debug.log(DebugCategory.BROWSER_SDK, "Removing event listener", { event });
|
|
1251
|
+
this.providerManager.off(event, callback);
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* Attempt auto-connection using existing session
|
|
1255
|
+
* Should be called after setting up event listeners
|
|
1256
|
+
* Only works with embedded providers
|
|
1257
|
+
*/
|
|
1258
|
+
async autoConnect() {
|
|
1259
|
+
debug.log(DebugCategory.BROWSER_SDK, "Attempting auto-connect");
|
|
1260
|
+
const currentProvider = this.providerManager.getCurrentProvider();
|
|
1261
|
+
if (currentProvider && "autoConnect" in currentProvider) {
|
|
1262
|
+
await currentProvider.autoConnect();
|
|
1263
|
+
} else {
|
|
1264
|
+
debug.warn(DebugCategory.BROWSER_SDK, "Current provider does not support auto-connect", {
|
|
1265
|
+
providerType: this.getCurrentProviderInfo()?.type
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Debug configuration methods
|
|
1271
|
+
* These allow dynamic debug configuration without SDK reinstantiation
|
|
1272
|
+
*/
|
|
1273
|
+
/**
|
|
1274
|
+
* Enable debug logging
|
|
1275
|
+
*/
|
|
1276
|
+
enableDebug() {
|
|
1277
|
+
debug.enable();
|
|
1278
|
+
debug.info(DebugCategory.BROWSER_SDK, "Debug logging enabled");
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* Disable debug logging
|
|
1282
|
+
*/
|
|
1283
|
+
disableDebug() {
|
|
1284
|
+
debug.disable();
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* Set debug level
|
|
1288
|
+
*/
|
|
1289
|
+
setDebugLevel(level) {
|
|
1290
|
+
debug.setLevel(level);
|
|
1291
|
+
debug.info(DebugCategory.BROWSER_SDK, "Debug level updated", { level });
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* Set debug callback function
|
|
1295
|
+
*/
|
|
1296
|
+
setDebugCallback(callback) {
|
|
1297
|
+
debug.setCallback(callback);
|
|
1298
|
+
debug.info(DebugCategory.BROWSER_SDK, "Debug callback updated");
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
* Configure debug settings all at once
|
|
1302
|
+
*/
|
|
1303
|
+
configureDebug(config) {
|
|
1304
|
+
if (config.enabled !== void 0) {
|
|
1305
|
+
if (config.enabled) {
|
|
1306
|
+
this.enableDebug();
|
|
1307
|
+
} else {
|
|
1308
|
+
this.disableDebug();
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
if (config.level !== void 0) {
|
|
1312
|
+
this.setDebugLevel(config.level);
|
|
1313
|
+
}
|
|
1314
|
+
if (config.callback !== void 0) {
|
|
1315
|
+
this.setDebugCallback(config.callback);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
994
1318
|
};
|
|
995
1319
|
|
|
1320
|
+
// src/types.ts
|
|
1321
|
+
import { AddressType as AddressType2 } from "@phantom/client";
|
|
1322
|
+
|
|
996
1323
|
// src/index.ts
|
|
997
1324
|
import { NetworkId } from "@phantom/constants";
|
|
998
|
-
import { AddressType as
|
|
1325
|
+
import { AddressType as AddressType3 } from "@phantom/client";
|
|
999
1326
|
export {
|
|
1000
|
-
|
|
1327
|
+
AddressType3 as AddressType,
|
|
1001
1328
|
BrowserSDK,
|
|
1002
1329
|
DEFAULT_AUTH_URL,
|
|
1003
1330
|
DEFAULT_WALLET_API_URL,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phantom/browser-sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Browser SDK for Phantom Wallet with unified interface",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"@phantom/browser-injected-sdk": "^0.0.9",
|
|
33
33
|
"@phantom/client": "^0.1.8",
|
|
34
34
|
"@phantom/constants": "^0.0.3",
|
|
35
|
-
"@phantom/embedded-provider-core": "^0.1.
|
|
35
|
+
"@phantom/embedded-provider-core": "^0.1.7",
|
|
36
36
|
"@phantom/indexed-db-stamper": "^0.1.4",
|
|
37
37
|
"@phantom/parsers": "^0.0.7",
|
|
38
38
|
"axios": "^1.10.0",
|