@phantom/embedded-provider-core 1.0.5 → 1.0.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/dist/index.d.mts +6 -10
- package/dist/index.d.ts +6 -10
- package/dist/index.js +47 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +47 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -11,7 +11,7 @@ import { Buffer } from "buffer";
|
|
|
11
11
|
import bs582 from "bs58";
|
|
12
12
|
|
|
13
13
|
// src/constants.ts
|
|
14
|
-
var AUTHENTICATOR_EXPIRATION_TIME_MS =
|
|
14
|
+
var AUTHENTICATOR_EXPIRATION_TIME_MS = 31 * 24 * 60 * 60 * 1e3;
|
|
15
15
|
var AUTHENTICATOR_RENEWAL_WINDOW_MS = 2 * 24 * 60 * 60 * 1e3;
|
|
16
16
|
var EMBEDDED_PROVIDER_AUTH_TYPES = ["google", "apple", "phantom"];
|
|
17
17
|
|
|
@@ -336,7 +336,7 @@ async function retryWithBackoff(operation, operationName, logger, maxRetries = 3
|
|
|
336
336
|
let lastError;
|
|
337
337
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
338
338
|
try {
|
|
339
|
-
logger.
|
|
339
|
+
logger.debug("EMBEDDED_PROVIDER", `Attempting ${operationName}`, {
|
|
340
340
|
attempt,
|
|
341
341
|
maxRetries
|
|
342
342
|
});
|
|
@@ -355,7 +355,7 @@ async function retryWithBackoff(operation, operationName, logger, maxRetries = 3
|
|
|
355
355
|
break;
|
|
356
356
|
}
|
|
357
357
|
const delay = baseDelay * Math.pow(2, attempt - 1);
|
|
358
|
-
logger.
|
|
358
|
+
logger.debug("EMBEDDED_PROVIDER", `Retrying ${operationName} in ${delay}ms`, {
|
|
359
359
|
attempt: attempt + 1,
|
|
360
360
|
delay
|
|
361
361
|
});
|
|
@@ -371,6 +371,16 @@ function generateSessionId() {
|
|
|
371
371
|
}
|
|
372
372
|
|
|
373
373
|
// src/embedded-provider.ts
|
|
374
|
+
var noopLogger = {
|
|
375
|
+
info: () => {
|
|
376
|
+
},
|
|
377
|
+
warn: () => {
|
|
378
|
+
},
|
|
379
|
+
error: () => {
|
|
380
|
+
},
|
|
381
|
+
debug: () => {
|
|
382
|
+
}
|
|
383
|
+
};
|
|
374
384
|
var EmbeddedProvider = class {
|
|
375
385
|
constructor(config, platform, logger) {
|
|
376
386
|
this.client = null;
|
|
@@ -378,7 +388,7 @@ var EmbeddedProvider = class {
|
|
|
378
388
|
this.addresses = [];
|
|
379
389
|
this.eventListeners = /* @__PURE__ */ new Map();
|
|
380
390
|
this.logger = logger;
|
|
381
|
-
this.logger.
|
|
391
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Initializing EmbeddedProvider", { config });
|
|
382
392
|
if (config.embeddedWalletType === "app-wallet") {
|
|
383
393
|
throw new Error("app-wallet type is not currently supported. Please use 'user-wallet' instead.");
|
|
384
394
|
}
|
|
@@ -401,19 +411,19 @@ var EmbeddedProvider = class {
|
|
|
401
411
|
this.eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
402
412
|
}
|
|
403
413
|
this.eventListeners.get(event)?.add(callback);
|
|
404
|
-
this.logger.
|
|
414
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Event listener added", { event });
|
|
405
415
|
}
|
|
406
416
|
off(event, callback) {
|
|
407
417
|
const listeners = this.eventListeners.get(event);
|
|
408
418
|
if (listeners) {
|
|
409
419
|
listeners.delete(callback);
|
|
410
|
-
this.logger.
|
|
420
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Event listener removed", { event });
|
|
411
421
|
}
|
|
412
422
|
}
|
|
413
423
|
emit(event, data) {
|
|
414
424
|
const listeners = this.eventListeners.get(event);
|
|
415
425
|
if (listeners && listeners.size > 0) {
|
|
416
|
-
this.logger.
|
|
426
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Emitting event", { event, listenerCount: listeners.size, data });
|
|
417
427
|
listeners.forEach((callback) => {
|
|
418
428
|
try {
|
|
419
429
|
callback(data);
|
|
@@ -481,7 +491,7 @@ var EmbeddedProvider = class {
|
|
|
481
491
|
async validateAndCleanSession(session) {
|
|
482
492
|
if (!session)
|
|
483
493
|
return null;
|
|
484
|
-
this.logger.
|
|
494
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Found existing session, validating", {
|
|
485
495
|
sessionId: session.sessionId,
|
|
486
496
|
status: session.status,
|
|
487
497
|
walletId: session.walletId
|
|
@@ -527,11 +537,11 @@ var EmbeddedProvider = class {
|
|
|
527
537
|
* Returns ConnectResult if connection succeeds, null if should continue with new auth flow.
|
|
528
538
|
*/
|
|
529
539
|
async tryExistingConnection(isAutoConnect) {
|
|
530
|
-
this.logger.
|
|
540
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Getting existing session");
|
|
531
541
|
let session = await this.storage.getSession();
|
|
532
542
|
session = await this.validateAndCleanSession(session);
|
|
533
543
|
if (!session) {
|
|
534
|
-
this.logger.
|
|
544
|
+
this.logger.debug("EMBEDDED_PROVIDER", "No existing session found");
|
|
535
545
|
return null;
|
|
536
546
|
}
|
|
537
547
|
if (session.status === "completed") {
|
|
@@ -560,7 +570,7 @@ var EmbeddedProvider = class {
|
|
|
560
570
|
});
|
|
561
571
|
return result;
|
|
562
572
|
}
|
|
563
|
-
this.logger.
|
|
573
|
+
this.logger.debug("EMBEDDED_PROVIDER", "No completed session found, checking for redirect resume");
|
|
564
574
|
if (this.authProvider.resumeAuthFromRedirect) {
|
|
565
575
|
const authResult = await this.authProvider.resumeAuthFromRedirect(session.authProvider);
|
|
566
576
|
if (authResult) {
|
|
@@ -610,7 +620,7 @@ var EmbeddedProvider = class {
|
|
|
610
620
|
return false;
|
|
611
621
|
}
|
|
612
622
|
if (!session.walletId || !session.organizationId || !session.stamperInfo) {
|
|
613
|
-
this.logger.
|
|
623
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Session missing required fields", {
|
|
614
624
|
hasWalletId: !!session.walletId,
|
|
615
625
|
hasOrganizationId: !!session.organizationId,
|
|
616
626
|
hasStamperInfo: !!session.stamperInfo
|
|
@@ -618,23 +628,23 @@ var EmbeddedProvider = class {
|
|
|
618
628
|
return false;
|
|
619
629
|
}
|
|
620
630
|
if (session.status !== "completed") {
|
|
621
|
-
this.logger.
|
|
631
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Session not completed", { status: session.status });
|
|
622
632
|
return false;
|
|
623
633
|
}
|
|
624
634
|
if (!session.authenticatorExpiresAt) {
|
|
625
|
-
this.logger.
|
|
635
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Session invalid - missing authenticator timing", {
|
|
626
636
|
sessionId: session.sessionId
|
|
627
637
|
});
|
|
628
638
|
return false;
|
|
629
639
|
}
|
|
630
640
|
if (Date.now() >= session.authenticatorExpiresAt) {
|
|
631
|
-
this.logger.
|
|
641
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Authenticator expired, session invalid", {
|
|
632
642
|
authenticatorExpiresAt: new Date(session.authenticatorExpiresAt).toISOString(),
|
|
633
643
|
now: (/* @__PURE__ */ new Date()).toISOString()
|
|
634
644
|
});
|
|
635
645
|
return false;
|
|
636
646
|
}
|
|
637
|
-
this.logger.
|
|
647
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Session is valid", {
|
|
638
648
|
sessionId: session.sessionId,
|
|
639
649
|
walletId: session.walletId,
|
|
640
650
|
authenticatorExpires: new Date(session.authenticatorExpiresAt).toISOString()
|
|
@@ -648,7 +658,7 @@ var EmbeddedProvider = class {
|
|
|
648
658
|
*/
|
|
649
659
|
async autoConnect() {
|
|
650
660
|
try {
|
|
651
|
-
this.logger.
|
|
661
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Starting auto-connect attempt");
|
|
652
662
|
this.emit("connect_start", { source: "auto-connect" });
|
|
653
663
|
const result = await this.tryExistingConnection(true);
|
|
654
664
|
if (result) {
|
|
@@ -662,7 +672,7 @@ var EmbeddedProvider = class {
|
|
|
662
672
|
});
|
|
663
673
|
return;
|
|
664
674
|
}
|
|
665
|
-
this.logger.
|
|
675
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Auto-connect failed: no valid session found");
|
|
666
676
|
this.emit("connect_error", {
|
|
667
677
|
error: "No valid session found",
|
|
668
678
|
source: "auto-connect"
|
|
@@ -683,11 +693,11 @@ var EmbeddedProvider = class {
|
|
|
683
693
|
* This is the first step when no existing session is found and we need to set up a new wallet.
|
|
684
694
|
*/
|
|
685
695
|
async initializeStamper() {
|
|
686
|
-
this.logger.
|
|
696
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Initializing stamper");
|
|
687
697
|
await this.stamper.init();
|
|
688
|
-
this.logger.
|
|
698
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Resetting keypair to avoid conflicts with existing keypairs");
|
|
689
699
|
const stamperInfo = await this.stamper.resetKeyPair();
|
|
690
|
-
this.logger.
|
|
700
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Stamper initialized with fresh keypair", {
|
|
691
701
|
publicKey: stamperInfo.publicKey,
|
|
692
702
|
keyId: stamperInfo.keyId,
|
|
693
703
|
algorithm: this.stamper.algorithm
|
|
@@ -704,12 +714,13 @@ var EmbeddedProvider = class {
|
|
|
704
714
|
apiBaseUrl: this.config.apiBaseUrl,
|
|
705
715
|
headers: {
|
|
706
716
|
...this.platform.analyticsHeaders || {}
|
|
707
|
-
}
|
|
717
|
+
},
|
|
718
|
+
logger: noopLogger
|
|
708
719
|
});
|
|
709
720
|
const platformName = this.platform.name || "unknown";
|
|
710
721
|
const shortPubKey = stamperInfo.publicKey.slice(0, 8);
|
|
711
722
|
const organizationName = `${this.config.appId.substring(0, 8)}-${platformName}-${shortPubKey}`;
|
|
712
|
-
this.logger.
|
|
723
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Creating organization for app-wallet", {
|
|
713
724
|
organizationName,
|
|
714
725
|
publicKey: stamperInfo.publicKey,
|
|
715
726
|
platform: platformName
|
|
@@ -829,7 +840,7 @@ var EmbeddedProvider = class {
|
|
|
829
840
|
async disconnect(shouldClearPreviousSession = true) {
|
|
830
841
|
const wasConnected = this.client !== null;
|
|
831
842
|
await this.storage.setShouldClearPreviousSession(shouldClearPreviousSession);
|
|
832
|
-
this.logger.
|
|
843
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Set flag to clear previous session on next login");
|
|
833
844
|
await this.storage.clearSession();
|
|
834
845
|
this.client = null;
|
|
835
846
|
this.walletId = null;
|
|
@@ -946,7 +957,7 @@ var EmbeddedProvider = class {
|
|
|
946
957
|
const parsedTransaction = await parseToKmsTransaction(params.transaction, params.networkId);
|
|
947
958
|
const session = await this.storage.getSession();
|
|
948
959
|
const derivationIndex = session?.accountDerivationIndex ?? 0;
|
|
949
|
-
this.logger.
|
|
960
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Parsed transaction for signing", {
|
|
950
961
|
walletId: this.walletId,
|
|
951
962
|
transaction: parsedTransaction,
|
|
952
963
|
derivationIndex
|
|
@@ -985,7 +996,7 @@ var EmbeddedProvider = class {
|
|
|
985
996
|
const parsedTransaction = await parseToKmsTransaction(params.transaction, params.networkId);
|
|
986
997
|
const session = await this.storage.getSession();
|
|
987
998
|
const derivationIndex = session?.accountDerivationIndex ?? 0;
|
|
988
|
-
this.logger.
|
|
999
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Parsed transaction for signing", {
|
|
989
1000
|
walletId: this.walletId,
|
|
990
1001
|
transaction: parsedTransaction,
|
|
991
1002
|
derivationIndex
|
|
@@ -1049,7 +1060,8 @@ var EmbeddedProvider = class {
|
|
|
1049
1060
|
organizationId,
|
|
1050
1061
|
headers: {
|
|
1051
1062
|
...this.platform.analyticsHeaders || {}
|
|
1052
|
-
}
|
|
1063
|
+
},
|
|
1064
|
+
logger: noopLogger
|
|
1053
1065
|
},
|
|
1054
1066
|
this.stamper
|
|
1055
1067
|
);
|
|
@@ -1128,7 +1140,7 @@ var EmbeddedProvider = class {
|
|
|
1128
1140
|
lastRenewalAttempt: void 0,
|
|
1129
1141
|
authUserId: authResult.authUserId
|
|
1130
1142
|
};
|
|
1131
|
-
this.logger.
|
|
1143
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Saving Phantom session");
|
|
1132
1144
|
await this.storage.saveSession(session);
|
|
1133
1145
|
return session;
|
|
1134
1146
|
}
|
|
@@ -1163,7 +1175,7 @@ var EmbeddedProvider = class {
|
|
|
1163
1175
|
authenticatorExpiresAt: now + AUTHENTICATOR_EXPIRATION_TIME_MS,
|
|
1164
1176
|
lastRenewalAttempt: void 0
|
|
1165
1177
|
};
|
|
1166
|
-
this.logger.
|
|
1178
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Saving temporary session before redirect", {
|
|
1167
1179
|
sessionId: tempSession.sessionId,
|
|
1168
1180
|
tempWalletId: tempSession.walletId
|
|
1169
1181
|
});
|
|
@@ -1210,14 +1222,14 @@ var EmbeddedProvider = class {
|
|
|
1210
1222
|
const now2 = Date.now();
|
|
1211
1223
|
tempSession.authenticatorCreatedAt = now2;
|
|
1212
1224
|
tempSession.authenticatorExpiresAt = now2 + authResult.expiresInMs;
|
|
1213
|
-
this.logger.
|
|
1225
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Updated authenticator expiration from immediate auth response", {
|
|
1214
1226
|
expiresInMs: authResult.expiresInMs,
|
|
1215
1227
|
expiresAt: new Date(tempSession.authenticatorExpiresAt).toISOString()
|
|
1216
1228
|
});
|
|
1217
1229
|
}
|
|
1218
1230
|
await this.storage.saveSession(tempSession);
|
|
1219
1231
|
await this.storage.setShouldClearPreviousSession(false);
|
|
1220
|
-
this.logger.
|
|
1232
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Cleared logout flag after successful authentication");
|
|
1221
1233
|
return tempSession;
|
|
1222
1234
|
}
|
|
1223
1235
|
this.logger.info("EMBEDDED_PROVIDER", "Redirect authentication initiated, waiting for redirect completion");
|
|
@@ -1240,14 +1252,14 @@ var EmbeddedProvider = class {
|
|
|
1240
1252
|
const now = Date.now();
|
|
1241
1253
|
session.authenticatorCreatedAt = now;
|
|
1242
1254
|
session.authenticatorExpiresAt = now + authResult.expiresInMs;
|
|
1243
|
-
this.logger.
|
|
1255
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Updated authenticator expiration from auth response", {
|
|
1244
1256
|
expiresInMs: authResult.expiresInMs,
|
|
1245
1257
|
expiresAt: new Date(session.authenticatorExpiresAt).toISOString()
|
|
1246
1258
|
});
|
|
1247
1259
|
}
|
|
1248
1260
|
await this.storage.saveSession(session);
|
|
1249
1261
|
await this.storage.setShouldClearPreviousSession(false);
|
|
1250
|
-
this.logger.
|
|
1262
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Cleared logout flag after successful authentication");
|
|
1251
1263
|
await this.initializeClientFromSession(session);
|
|
1252
1264
|
await this.ensureValidAuthenticator();
|
|
1253
1265
|
return {
|
|
@@ -1274,7 +1286,7 @@ var EmbeddedProvider = class {
|
|
|
1274
1286
|
throw new Error("Invalid session - missing authenticator timing");
|
|
1275
1287
|
}
|
|
1276
1288
|
const timeUntilExpiry = session.authenticatorExpiresAt - now;
|
|
1277
|
-
this.logger.
|
|
1289
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Checking authenticator expiration", {
|
|
1278
1290
|
expiresAt: new Date(session.authenticatorExpiresAt).toISOString(),
|
|
1279
1291
|
timeUntilExpiry
|
|
1280
1292
|
});
|
|
@@ -1289,7 +1301,7 @@ var EmbeddedProvider = class {
|
|
|
1289
1301
|
* This is the final step that sets up the provider's client state and retrieves available addresses.
|
|
1290
1302
|
*/
|
|
1291
1303
|
async initializeClientFromSession(session) {
|
|
1292
|
-
this.logger.
|
|
1304
|
+
this.logger.debug("EMBEDDED_PROVIDER", "Initializing PhantomClient from session", {
|
|
1293
1305
|
organizationId: session.organizationId,
|
|
1294
1306
|
walletId: session.walletId,
|
|
1295
1307
|
appId: session.appId
|