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