@phantom/embedded-provider-core 1.0.0-beta.12 → 1.0.0-beta.13
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 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +82 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +85 -57
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.d.mts
CHANGED
|
@@ -114,6 +114,10 @@ interface SignMessageParams {
|
|
|
114
114
|
message: string;
|
|
115
115
|
networkId: NetworkId;
|
|
116
116
|
}
|
|
117
|
+
interface SignTypedDataV4Params {
|
|
118
|
+
typedData: any;
|
|
119
|
+
networkId: NetworkId;
|
|
120
|
+
}
|
|
117
121
|
interface SignMessageResult extends ParsedSignatureResult {
|
|
118
122
|
}
|
|
119
123
|
interface SignTransactionParams {
|
|
@@ -178,6 +182,7 @@ declare class EmbeddedProvider {
|
|
|
178
182
|
connect(authOptions?: AuthOptions): Promise<ConnectResult>;
|
|
179
183
|
disconnect(): Promise<void>;
|
|
180
184
|
signMessage(params: SignMessageParams): Promise<ParsedSignatureResult>;
|
|
185
|
+
signTypedDataV4(params: SignTypedDataV4Params): Promise<ParsedSignatureResult>;
|
|
181
186
|
signTransaction(params: SignTransactionParams): Promise<ParsedTransactionResult>;
|
|
182
187
|
signAndSendTransaction(params: SignAndSendTransactionParams): Promise<ParsedTransactionResult>;
|
|
183
188
|
getAddresses(): WalletAddress[];
|
|
@@ -292,4 +297,4 @@ declare const AUTHENTICATOR_EXPIRATION_TIME_MS: number;
|
|
|
292
297
|
*/
|
|
293
298
|
declare const AUTHENTICATOR_RENEWAL_WINDOW_MS: number;
|
|
294
299
|
|
|
295
|
-
export { AUTHENTICATOR_EXPIRATION_TIME_MS, AUTHENTICATOR_RENEWAL_WINDOW_MS, AuthOptions, AuthProvider, AuthResult, ConnectResult, DebugLogger, EmbeddedEthereumChain, EmbeddedProvider, EmbeddedProviderConfig, EmbeddedProviderEvent, EmbeddedSolanaChain, EmbeddedStorage, EventCallback, JWTAuth, JWTAuthOptions, Keypair, PhantomConnectOptions, PlatformAdapter, Session, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignTransactionParams, SignedTransaction, StamperInfo, URLParamsAccessor, WalletAddress, generateSessionId, retryWithBackoff };
|
|
300
|
+
export { AUTHENTICATOR_EXPIRATION_TIME_MS, AUTHENTICATOR_RENEWAL_WINDOW_MS, AuthOptions, AuthProvider, AuthResult, ConnectResult, DebugLogger, EmbeddedEthereumChain, EmbeddedProvider, EmbeddedProviderConfig, EmbeddedProviderEvent, EmbeddedSolanaChain, EmbeddedStorage, EventCallback, JWTAuth, JWTAuthOptions, Keypair, PhantomConnectOptions, PlatformAdapter, Session, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignTransactionParams, SignTypedDataV4Params, SignedTransaction, StamperInfo, URLParamsAccessor, WalletAddress, generateSessionId, retryWithBackoff };
|
package/dist/index.d.ts
CHANGED
|
@@ -114,6 +114,10 @@ interface SignMessageParams {
|
|
|
114
114
|
message: string;
|
|
115
115
|
networkId: NetworkId;
|
|
116
116
|
}
|
|
117
|
+
interface SignTypedDataV4Params {
|
|
118
|
+
typedData: any;
|
|
119
|
+
networkId: NetworkId;
|
|
120
|
+
}
|
|
117
121
|
interface SignMessageResult extends ParsedSignatureResult {
|
|
118
122
|
}
|
|
119
123
|
interface SignTransactionParams {
|
|
@@ -178,6 +182,7 @@ declare class EmbeddedProvider {
|
|
|
178
182
|
connect(authOptions?: AuthOptions): Promise<ConnectResult>;
|
|
179
183
|
disconnect(): Promise<void>;
|
|
180
184
|
signMessage(params: SignMessageParams): Promise<ParsedSignatureResult>;
|
|
185
|
+
signTypedDataV4(params: SignTypedDataV4Params): Promise<ParsedSignatureResult>;
|
|
181
186
|
signTransaction(params: SignTransactionParams): Promise<ParsedTransactionResult>;
|
|
182
187
|
signAndSendTransaction(params: SignAndSendTransactionParams): Promise<ParsedTransactionResult>;
|
|
183
188
|
getAddresses(): WalletAddress[];
|
|
@@ -292,4 +297,4 @@ declare const AUTHENTICATOR_EXPIRATION_TIME_MS: number;
|
|
|
292
297
|
*/
|
|
293
298
|
declare const AUTHENTICATOR_RENEWAL_WINDOW_MS: number;
|
|
294
299
|
|
|
295
|
-
export { AUTHENTICATOR_EXPIRATION_TIME_MS, AUTHENTICATOR_RENEWAL_WINDOW_MS, AuthOptions, AuthProvider, AuthResult, ConnectResult, DebugLogger, EmbeddedEthereumChain, EmbeddedProvider, EmbeddedProviderConfig, EmbeddedProviderEvent, EmbeddedSolanaChain, EmbeddedStorage, EventCallback, JWTAuth, JWTAuthOptions, Keypair, PhantomConnectOptions, PlatformAdapter, Session, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignTransactionParams, SignedTransaction, StamperInfo, URLParamsAccessor, WalletAddress, generateSessionId, retryWithBackoff };
|
|
300
|
+
export { AUTHENTICATOR_EXPIRATION_TIME_MS, AUTHENTICATOR_RENEWAL_WINDOW_MS, AuthOptions, AuthProvider, AuthResult, ConnectResult, DebugLogger, EmbeddedEthereumChain, EmbeddedProvider, EmbeddedProviderConfig, EmbeddedProviderEvent, EmbeddedSolanaChain, EmbeddedStorage, EventCallback, JWTAuth, JWTAuthOptions, Keypair, PhantomConnectOptions, PlatformAdapter, Session, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignTransactionParams, SignTypedDataV4Params, SignedTransaction, StamperInfo, URLParamsAccessor, WalletAddress, generateSessionId, retryWithBackoff };
|
package/dist/index.js
CHANGED
|
@@ -42,11 +42,11 @@ __export(src_exports, {
|
|
|
42
42
|
module.exports = __toCommonJS(src_exports);
|
|
43
43
|
|
|
44
44
|
// src/embedded-provider.ts
|
|
45
|
+
var import_base64url2 = require("@phantom/base64url");
|
|
45
46
|
var import_client = require("@phantom/client");
|
|
47
|
+
var import_parsers = require("@phantom/parsers");
|
|
46
48
|
var import_utils = require("@phantom/utils");
|
|
47
|
-
var import_base64url2 = require("@phantom/base64url");
|
|
48
49
|
var import_bs582 = __toESM(require("bs58"));
|
|
49
|
-
var import_parsers = require("@phantom/parsers");
|
|
50
50
|
|
|
51
51
|
// src/constants.ts
|
|
52
52
|
var AUTHENTICATOR_EXPIRATION_TIME_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
@@ -139,45 +139,6 @@ var JWTAuth = class {
|
|
|
139
139
|
}
|
|
140
140
|
};
|
|
141
141
|
|
|
142
|
-
// src/utils/session.ts
|
|
143
|
-
function generateSessionId() {
|
|
144
|
-
return "session_" + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + "_" + Date.now();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// src/utils/retry.ts
|
|
148
|
-
async function retryWithBackoff(operation, operationName, logger, maxRetries = 3, baseDelay = 1e3) {
|
|
149
|
-
let lastError;
|
|
150
|
-
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
151
|
-
try {
|
|
152
|
-
logger.log("EMBEDDED_PROVIDER", `Attempting ${operationName}`, {
|
|
153
|
-
attempt,
|
|
154
|
-
maxRetries
|
|
155
|
-
});
|
|
156
|
-
return await operation();
|
|
157
|
-
} catch (error) {
|
|
158
|
-
lastError = error;
|
|
159
|
-
logger.warn("EMBEDDED_PROVIDER", `${operationName} failed`, {
|
|
160
|
-
attempt,
|
|
161
|
-
maxRetries,
|
|
162
|
-
error: error instanceof Error ? error.message : String(error)
|
|
163
|
-
});
|
|
164
|
-
if (attempt === maxRetries) {
|
|
165
|
-
logger.error("EMBEDDED_PROVIDER", `${operationName} failed after ${maxRetries} attempts`, {
|
|
166
|
-
finalError: error instanceof Error ? error.message : String(error)
|
|
167
|
-
});
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
const delay = baseDelay * Math.pow(2, attempt - 1);
|
|
171
|
-
logger.log("EMBEDDED_PROVIDER", `Retrying ${operationName} in ${delay}ms`, {
|
|
172
|
-
attempt: attempt + 1,
|
|
173
|
-
delay
|
|
174
|
-
});
|
|
175
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
throw lastError;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
142
|
// src/chains/SolanaChain.ts
|
|
182
143
|
var import_eventemitter3 = require("eventemitter3");
|
|
183
144
|
var import_constants = require("@phantom/constants");
|
|
@@ -303,10 +264,10 @@ var EmbeddedSolanaChain = class {
|
|
|
303
264
|
};
|
|
304
265
|
|
|
305
266
|
// src/chains/EthereumChain.ts
|
|
306
|
-
var import_eventemitter32 = require("eventemitter3");
|
|
307
|
-
var import_constants2 = require("@phantom/constants");
|
|
308
267
|
var import_base64url = require("@phantom/base64url");
|
|
268
|
+
var import_constants2 = require("@phantom/constants");
|
|
309
269
|
var import_buffer = require("buffer");
|
|
270
|
+
var import_eventemitter32 = require("eventemitter3");
|
|
310
271
|
var EmbeddedEthereumChain = class {
|
|
311
272
|
constructor(provider) {
|
|
312
273
|
this.provider = provider;
|
|
@@ -444,10 +405,9 @@ var EmbeddedEthereumChain = class {
|
|
|
444
405
|
}
|
|
445
406
|
case "eth_signTypedData_v4": {
|
|
446
407
|
const [_typedDataAddress, typedDataStr] = args.params;
|
|
447
|
-
const
|
|
448
|
-
const typedDataResult = await this.provider.
|
|
449
|
-
|
|
450
|
-
// Pass the stringified typed data as message
|
|
408
|
+
const typedData = JSON.parse(typedDataStr);
|
|
409
|
+
const typedDataResult = await this.provider.signTypedDataV4({
|
|
410
|
+
typedData,
|
|
451
411
|
networkId: this.currentNetworkId
|
|
452
412
|
});
|
|
453
413
|
return typedDataResult.signature;
|
|
@@ -501,6 +461,45 @@ var EmbeddedEthereumChain = class {
|
|
|
501
461
|
}
|
|
502
462
|
};
|
|
503
463
|
|
|
464
|
+
// src/utils/retry.ts
|
|
465
|
+
async function retryWithBackoff(operation, operationName, logger, maxRetries = 3, baseDelay = 1e3) {
|
|
466
|
+
let lastError;
|
|
467
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
468
|
+
try {
|
|
469
|
+
logger.log("EMBEDDED_PROVIDER", `Attempting ${operationName}`, {
|
|
470
|
+
attempt,
|
|
471
|
+
maxRetries
|
|
472
|
+
});
|
|
473
|
+
return await operation();
|
|
474
|
+
} catch (error) {
|
|
475
|
+
lastError = error;
|
|
476
|
+
logger.warn("EMBEDDED_PROVIDER", `${operationName} failed`, {
|
|
477
|
+
attempt,
|
|
478
|
+
maxRetries,
|
|
479
|
+
error: error instanceof Error ? error.message : String(error)
|
|
480
|
+
});
|
|
481
|
+
if (attempt === maxRetries) {
|
|
482
|
+
logger.error("EMBEDDED_PROVIDER", `${operationName} failed after ${maxRetries} attempts`, {
|
|
483
|
+
finalError: error instanceof Error ? error.message : String(error)
|
|
484
|
+
});
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
const delay = baseDelay * Math.pow(2, attempt - 1);
|
|
488
|
+
logger.log("EMBEDDED_PROVIDER", `Retrying ${operationName} in ${delay}ms`, {
|
|
489
|
+
attempt: attempt + 1,
|
|
490
|
+
delay
|
|
491
|
+
});
|
|
492
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
throw lastError;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// src/utils/session.ts
|
|
499
|
+
function generateSessionId() {
|
|
500
|
+
return "session_" + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + "_" + Date.now();
|
|
501
|
+
}
|
|
502
|
+
|
|
504
503
|
// src/embedded-provider.ts
|
|
505
504
|
var EmbeddedProvider = class {
|
|
506
505
|
constructor(config, platform, logger) {
|
|
@@ -636,12 +635,16 @@ var EmbeddedProvider = class {
|
|
|
636
635
|
}
|
|
637
636
|
}
|
|
638
637
|
if (session.status === "completed" && !this.isSessionValid(session)) {
|
|
639
|
-
this.logger.warn(
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
638
|
+
this.logger.warn(
|
|
639
|
+
"EMBEDDED_PROVIDER",
|
|
640
|
+
"Session invalid due to authenticator expiration, will regenerate keypair",
|
|
641
|
+
{
|
|
642
|
+
sessionId: session.sessionId,
|
|
643
|
+
authenticatorExpiresAt: session.authenticatorExpiresAt,
|
|
644
|
+
currentTime: Date.now(),
|
|
645
|
+
expired: session.authenticatorExpiresAt < Date.now()
|
|
646
|
+
}
|
|
647
|
+
);
|
|
645
648
|
await this.storage.clearSession();
|
|
646
649
|
return null;
|
|
647
650
|
}
|
|
@@ -884,7 +887,10 @@ var EmbeddedProvider = class {
|
|
|
884
887
|
return existingResult;
|
|
885
888
|
}
|
|
886
889
|
this.validateAuthOptions(authOptions);
|
|
887
|
-
this.logger.info(
|
|
890
|
+
this.logger.info(
|
|
891
|
+
"EMBEDDED_PROVIDER",
|
|
892
|
+
"No existing connection available, creating new auth flow with fresh keypair"
|
|
893
|
+
);
|
|
888
894
|
const { stamperInfo, expiresInMs } = await this.initializeStamper();
|
|
889
895
|
const session = await this.handleAuthFlow(stamperInfo.publicKey, stamperInfo, authOptions, expiresInMs);
|
|
890
896
|
if (!session) {
|
|
@@ -984,6 +990,28 @@ var EmbeddedProvider = class {
|
|
|
984
990
|
});
|
|
985
991
|
return (0, import_parsers.parseSignMessageResponse)(rawResponse, params.networkId);
|
|
986
992
|
}
|
|
993
|
+
async signTypedDataV4(params) {
|
|
994
|
+
if (!this.client || !this.walletId) {
|
|
995
|
+
throw new Error("Not connected");
|
|
996
|
+
}
|
|
997
|
+
await this.ensureValidAuthenticator();
|
|
998
|
+
this.logger.info("EMBEDDED_PROVIDER", "Signing typed data", {
|
|
999
|
+
walletId: this.walletId,
|
|
1000
|
+
typedData: params.typedData
|
|
1001
|
+
});
|
|
1002
|
+
const session = await this.storage.getSession();
|
|
1003
|
+
const derivationIndex = session?.accountDerivationIndex ?? 0;
|
|
1004
|
+
const rawResponse = await this.client.signTypedData({
|
|
1005
|
+
walletId: this.walletId,
|
|
1006
|
+
typedData: params.typedData,
|
|
1007
|
+
networkId: params.networkId,
|
|
1008
|
+
derivationIndex
|
|
1009
|
+
});
|
|
1010
|
+
this.logger.info("EMBEDDED_PROVIDER", "Typed data signed successfully", {
|
|
1011
|
+
walletId: this.walletId
|
|
1012
|
+
});
|
|
1013
|
+
return (0, import_parsers.parseSignMessageResponse)(rawResponse, params.networkId);
|
|
1014
|
+
}
|
|
987
1015
|
async signTransaction(params) {
|
|
988
1016
|
if (!this.client || !this.walletId) {
|
|
989
1017
|
throw new Error("Not connected");
|