@account-kit/signer 4.0.0-beta.0 → 4.0.0-beta.10

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.
Files changed (77) hide show
  1. package/dist/esm/base.d.ts +43 -8
  2. package/dist/esm/base.js +186 -29
  3. package/dist/esm/base.js.map +1 -1
  4. package/dist/esm/client/base.d.ts +22 -4
  5. package/dist/esm/client/base.js +36 -2
  6. package/dist/esm/client/base.js.map +1 -1
  7. package/dist/esm/client/index.d.ts +108 -7
  8. package/dist/esm/client/index.js +282 -14
  9. package/dist/esm/client/index.js.map +1 -1
  10. package/dist/esm/client/types.d.ts +31 -1
  11. package/dist/esm/client/types.js.map +1 -1
  12. package/dist/esm/index.d.ts +1 -1
  13. package/dist/esm/index.js +1 -1
  14. package/dist/esm/index.js.map +1 -1
  15. package/dist/esm/metrics.d.ts +17 -0
  16. package/dist/esm/metrics.js +7 -0
  17. package/dist/esm/metrics.js.map +1 -0
  18. package/dist/esm/oauth.d.ts +19 -0
  19. package/dist/esm/oauth.js +26 -0
  20. package/dist/esm/oauth.js.map +1 -0
  21. package/dist/esm/session/manager.d.ts +3 -2
  22. package/dist/esm/session/manager.js +29 -15
  23. package/dist/esm/session/manager.js.map +1 -1
  24. package/dist/esm/session/types.d.ts +1 -1
  25. package/dist/esm/session/types.js.map +1 -1
  26. package/dist/esm/signer.d.ts +52 -7
  27. package/dist/esm/signer.js +46 -3
  28. package/dist/esm/signer.js.map +1 -1
  29. package/dist/esm/types.d.ts +8 -1
  30. package/dist/esm/types.js +3 -1
  31. package/dist/esm/types.js.map +1 -1
  32. package/dist/esm/utils/typeAssertions.d.ts +1 -0
  33. package/dist/esm/utils/typeAssertions.js +4 -0
  34. package/dist/esm/utils/typeAssertions.js.map +1 -0
  35. package/dist/esm/version.d.ts +1 -1
  36. package/dist/esm/version.js +1 -1
  37. package/dist/esm/version.js.map +1 -1
  38. package/dist/types/base.d.ts +43 -8
  39. package/dist/types/base.d.ts.map +1 -1
  40. package/dist/types/client/base.d.ts +22 -4
  41. package/dist/types/client/base.d.ts.map +1 -1
  42. package/dist/types/client/index.d.ts +108 -7
  43. package/dist/types/client/index.d.ts.map +1 -1
  44. package/dist/types/client/types.d.ts +31 -1
  45. package/dist/types/client/types.d.ts.map +1 -1
  46. package/dist/types/index.d.ts +1 -1
  47. package/dist/types/index.d.ts.map +1 -1
  48. package/dist/types/metrics.d.ts +18 -0
  49. package/dist/types/metrics.d.ts.map +1 -0
  50. package/dist/types/oauth.d.ts +20 -0
  51. package/dist/types/oauth.d.ts.map +1 -0
  52. package/dist/types/session/manager.d.ts +3 -2
  53. package/dist/types/session/manager.d.ts.map +1 -1
  54. package/dist/types/session/types.d.ts +1 -1
  55. package/dist/types/session/types.d.ts.map +1 -1
  56. package/dist/types/signer.d.ts +52 -7
  57. package/dist/types/signer.d.ts.map +1 -1
  58. package/dist/types/types.d.ts +8 -1
  59. package/dist/types/types.d.ts.map +1 -1
  60. package/dist/types/utils/typeAssertions.d.ts +2 -0
  61. package/dist/types/utils/typeAssertions.d.ts.map +1 -0
  62. package/dist/types/version.d.ts +1 -1
  63. package/dist/types/version.d.ts.map +1 -1
  64. package/package.json +7 -10
  65. package/src/base.ts +261 -66
  66. package/src/client/base.ts +49 -4
  67. package/src/client/index.ts +317 -20
  68. package/src/client/types.ts +33 -1
  69. package/src/index.ts +5 -1
  70. package/src/metrics.ts +23 -0
  71. package/src/oauth.ts +36 -0
  72. package/src/session/manager.ts +46 -19
  73. package/src/session/types.ts +1 -1
  74. package/src/signer.ts +91 -4
  75. package/src/types.ts +9 -1
  76. package/src/utils/typeAssertions.ts +3 -0
  77. package/src/version.ts +1 -1
@@ -1,13 +1,14 @@
1
1
  import { type SmartAccountAuthenticator } from "@aa-sdk/core";
2
- import { type CustomSource, type Hex, type LocalAccount, type SignableMessage, type TypedData, type TypedDataDefinition } from "viem";
2
+ import { type GetTransactionType, type Hex, type IsNarrowable, type LocalAccount, type SerializeTransactionFn, type SignableMessage, type TransactionSerializable, type TransactionSerialized, type TypedData, type TypedDataDefinition } from "viem";
3
3
  import type { BaseSignerClient } from "./client/base";
4
- import type { User } from "./client/types";
4
+ import type { OauthConfig, User } from "./client/types";
5
5
  import { type SessionManagerParams } from "./session/manager.js";
6
6
  import type { AuthParams } from "./signer";
7
- import { type AlchemySignerEvents } from "./types.js";
7
+ import { type AlchemySignerEvents, type ErrorInfo } from "./types.js";
8
8
  export interface BaseAlchemySignerParams<TClient extends BaseSignerClient> {
9
9
  client: TClient;
10
10
  sessionConfig?: Omit<SessionManagerParams, "client">;
11
+ initialError?: ErrorInfo;
11
12
  }
12
13
  /**
13
14
  * Base abstract class for Alchemy Signer, providing authentication and session management for smart accounts.
@@ -26,8 +27,9 @@ export declare abstract class BaseAlchemySigner<TClient extends BaseSignerClient
26
27
  * @param {BaseAlchemySignerParams<TClient>} param0 Object containing the client and session configuration
27
28
  * @param {TClient} param0.client The client instance to be used internally
28
29
  * @param {SessionConfig} param0.sessionConfig Configuration for managing sessions
30
+ * @param {ErrorInfo | undefined} param0.initialError Error already present on the signer when initialized, if any
29
31
  */
30
- constructor({ client, sessionConfig }: BaseAlchemySignerParams<TClient>);
32
+ constructor({ client, sessionConfig, initialError, }: BaseAlchemySignerParams<TClient>);
31
33
  /**
32
34
  * Allows you to subscribe to events emitted by the signer
33
35
  *
@@ -36,6 +38,36 @@ export declare abstract class BaseAlchemySigner<TClient extends BaseSignerClient
36
38
  * @returns {() => void} a function to remove the listener
37
39
  */
38
40
  on: <E extends keyof AlchemySignerEvents>(event: E, listener: AlchemySignerEvents[E]) => () => void;
41
+ /**
42
+ * Prepares the config needed to use popup-based OAuth login. This must be
43
+ * called before calling `.authenticate` with params `{ type: "oauth", mode:
44
+ * "popup" }`, and is recommended to be called on page load.
45
+ *
46
+ * This method exists because browsers may prevent popups from opening unless
47
+ * triggered by user interaction, and so the OAuth config must already have
48
+ * been fetched at the time a user clicks a social login button.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * import { AlchemyWebSigner } from "@account-kit/signer";
53
+ *
54
+ * const signer = new AlchemyWebSigner({
55
+ * client: {
56
+ * connection: {
57
+ * rpcUrl: "/api/rpc",
58
+ * },
59
+ * iframeConfig: {
60
+ * iframeContainerId: "alchemy-signer-iframe-container",
61
+ * },
62
+ * },
63
+ * });
64
+ *
65
+ * await signer.preparePopupOauth();
66
+ * ```
67
+ * @returns {Promise<OauthConfig>} the config which must be loaded before
68
+ * using popup-based OAuth
69
+ */
70
+ preparePopupOauth: () => Promise<OauthConfig>;
39
71
  /**
40
72
  * Authenticate a user with either an email or a passkey and create a session for that user
41
73
  *
@@ -64,6 +96,7 @@ export declare abstract class BaseAlchemySigner<TClient extends BaseSignerClient
64
96
  * @returns {Promise<User>} the user that was authenticated
65
97
  */
66
98
  authenticate: (params: AuthParams) => Promise<User>;
99
+ private trackAuthenticateType;
67
100
  /**
68
101
  * Clear a user session and log them out
69
102
  *
@@ -176,9 +209,7 @@ export declare abstract class BaseAlchemySigner<TClient extends BaseSignerClient
176
209
  * @param {TypedDataDefinition<TTypedData, TPrimaryType>} params The parameters for the typed message to be hashed and signed
177
210
  * @returns {Promise<any>} A promise that resolves to the signed message
178
211
  */
179
- signTypedData: <const TTypedData extends TypedData | {
180
- [key: string]: unknown;
181
- }, TPrimaryType extends keyof TTypedData | "EIP712Domain" = keyof TTypedData>(params: TypedDataDefinition<TTypedData, TPrimaryType>) => Promise<Hex>;
212
+ signTypedData: <const TTypedData extends TypedData | Record<string, unknown>, TPrimaryType extends keyof TTypedData | "EIP712Domain" = keyof TTypedData>(params: TypedDataDefinition<TTypedData, TPrimaryType>) => Promise<Hex>;
182
213
  /**
183
214
  * Serializes a transaction, signs it with a raw message, and then returns the serialized transaction with the signature.
184
215
  *
@@ -209,7 +240,9 @@ export declare abstract class BaseAlchemySigner<TClient extends BaseSignerClient
209
240
  * @param {() => Hex} [args.serializer] an optional serializer function. If not provided, the default `serializeTransaction` function will be used
210
241
  * @returns {Promise<string>} a promise that resolves to the serialized transaction with the signature
211
242
  */
212
- signTransaction: CustomSource["signTransaction"];
243
+ signTransaction: <serializer extends SerializeTransactionFn<TransactionSerializable> = SerializeTransactionFn<TransactionSerializable>, transaction extends Parameters<serializer>[0] = Parameters<serializer>[0]>(transaction: transaction, options?: {
244
+ serializer?: serializer | undefined;
245
+ } | undefined) => Promise<IsNarrowable<TransactionSerialized<GetTransactionType<transaction>>, Hex> extends true ? TransactionSerialized<GetTransactionType<transaction>> : Hex>;
213
246
  /**
214
247
  * Unauthenticated call to look up a user's organizationId by email
215
248
  *
@@ -318,5 +351,7 @@ export declare abstract class BaseAlchemySigner<TClient extends BaseSignerClient
318
351
  toViemAccount: () => LocalAccount;
319
352
  private authenticateWithEmail;
320
353
  private authenticateWithPasskey;
354
+ private authenticateWithOauth;
355
+ private handleOauthReturn;
321
356
  private registerListeners;
322
357
  }
package/dist/esm/base.js CHANGED
@@ -4,8 +4,10 @@ import { toAccount } from "viem/accounts";
4
4
  import { subscribeWithSelector } from "zustand/middleware";
5
5
  import { createStore } from "zustand/vanilla";
6
6
  import { NotAuthenticatedError } from "./errors.js";
7
+ import { SignerLogger } from "./metrics.js";
7
8
  import { SessionManager, } from "./session/manager.js";
8
9
  import { AlchemySignerStatus, } from "./types.js";
10
+ import { assertNever } from "./utils/typeAssertions.js";
9
11
  /**
10
12
  * Base abstract class for Alchemy Signer, providing authentication and session management for smart accounts.
11
13
  * Implements the `SmartAccountAuthenticator` interface and handles various signer events.
@@ -19,8 +21,9 @@ export class BaseAlchemySigner {
19
21
  * @param {BaseAlchemySignerParams<TClient>} param0 Object containing the client and session configuration
20
22
  * @param {TClient} param0.client The client instance to be used internally
21
23
  * @param {SessionConfig} param0.sessionConfig Configuration for managing sessions
24
+ * @param {ErrorInfo | undefined} param0.initialError Error already present on the signer when initialized, if any
22
25
  */
23
- constructor({ client, sessionConfig }) {
26
+ constructor({ client, sessionConfig, initialError, }) {
24
27
  Object.defineProperty(this, "signerType", {
25
28
  enumerable: true,
26
29
  configurable: true,
@@ -69,11 +72,48 @@ export class BaseAlchemySigner {
69
72
  listener(), { fireImmediately: true });
70
73
  case "statusChanged":
71
74
  return this.store.subscribe(({ status }) => status, listener, { fireImmediately: true });
75
+ case "errorChanged":
76
+ return this.store.subscribe(({ error }) => error, (error) => listener(error ?? undefined), { fireImmediately: true });
72
77
  default:
73
- throw new Error(`Uknown event type ${event}`);
78
+ assertNever(event, `Unknown event type ${event}`);
74
79
  }
75
80
  }
76
81
  });
82
+ /**
83
+ * Prepares the config needed to use popup-based OAuth login. This must be
84
+ * called before calling `.authenticate` with params `{ type: "oauth", mode:
85
+ * "popup" }`, and is recommended to be called on page load.
86
+ *
87
+ * This method exists because browsers may prevent popups from opening unless
88
+ * triggered by user interaction, and so the OAuth config must already have
89
+ * been fetched at the time a user clicks a social login button.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * import { AlchemyWebSigner } from "@account-kit/signer";
94
+ *
95
+ * const signer = new AlchemyWebSigner({
96
+ * client: {
97
+ * connection: {
98
+ * rpcUrl: "/api/rpc",
99
+ * },
100
+ * iframeConfig: {
101
+ * iframeContainerId: "alchemy-signer-iframe-container",
102
+ * },
103
+ * },
104
+ * });
105
+ *
106
+ * await signer.preparePopupOauth();
107
+ * ```
108
+ * @returns {Promise<OauthConfig>} the config which must be loaded before
109
+ * using popup-based OAuth
110
+ */
111
+ Object.defineProperty(this, "preparePopupOauth", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: () => this.inner.initOauth()
116
+ });
77
117
  /**
78
118
  * Authenticate a user with either an email or a passkey and create a session for that user
79
119
  *
@@ -105,11 +145,70 @@ export class BaseAlchemySigner {
105
145
  enumerable: true,
106
146
  configurable: true,
107
147
  writable: true,
108
- value: async (params) => {
109
- if (params.type === "email") {
110
- return this.authenticateWithEmail(params);
148
+ value: SignerLogger.profiled("BaseAlchemySigner.authenticate", async (params) => {
149
+ const { type } = params;
150
+ const result = (() => {
151
+ switch (type) {
152
+ case "email":
153
+ return this.authenticateWithEmail(params);
154
+ case "passkey":
155
+ return this.authenticateWithPasskey(params);
156
+ case "oauth":
157
+ return this.authenticateWithOauth(params);
158
+ case "oauthReturn":
159
+ return this.handleOauthReturn(params);
160
+ default:
161
+ assertNever(type, `Unknown auth type: ${type}`);
162
+ }
163
+ })();
164
+ this.trackAuthenticateType(params);
165
+ return result.catch((error) => {
166
+ this.store.setState({ error: toErrorInfo(error) });
167
+ throw error;
168
+ });
169
+ })
170
+ });
171
+ Object.defineProperty(this, "trackAuthenticateType", {
172
+ enumerable: true,
173
+ configurable: true,
174
+ writable: true,
175
+ value: (params) => {
176
+ const { type } = params;
177
+ switch (type) {
178
+ case "email": {
179
+ // we just want to track the start of email auth
180
+ if ("bundle" in params)
181
+ return;
182
+ SignerLogger.trackEvent({
183
+ name: "signer_authnticate",
184
+ data: { authType: "email" },
185
+ });
186
+ return;
187
+ }
188
+ case "passkey": {
189
+ const isAnon = !("email" in params) && params.createNew == null;
190
+ SignerLogger.trackEvent({
191
+ name: "signer_authnticate",
192
+ data: {
193
+ authType: isAnon ? "passkey_anon" : "passkey_email",
194
+ },
195
+ });
196
+ return;
197
+ }
198
+ case "oauth":
199
+ SignerLogger.trackEvent({
200
+ name: "signer_authnticate",
201
+ data: {
202
+ authType: "oauth",
203
+ provider: params.authProviderId,
204
+ },
205
+ });
206
+ break;
207
+ case "oauthReturn":
208
+ break;
209
+ default:
210
+ assertNever(type, `Unknown auth type: ${type}`);
111
211
  }
112
- return this.authenticateWithPasskey(params);
113
212
  }
114
213
  });
115
214
  /**
@@ -191,10 +290,10 @@ export class BaseAlchemySigner {
191
290
  enumerable: true,
192
291
  configurable: true,
193
292
  writable: true,
194
- value: async () => {
293
+ value: SignerLogger.profiled("BaseAlchemySigner.getAddress", async () => {
195
294
  const { address } = await this.inner.whoami();
196
295
  return address;
197
- }
296
+ })
198
297
  });
199
298
  /**
200
299
  * Signs a raw message after hashing it.
@@ -224,10 +323,14 @@ export class BaseAlchemySigner {
224
323
  enumerable: true,
225
324
  configurable: true,
226
325
  writable: true,
227
- value: async (msg) => {
326
+ value: SignerLogger.profiled("BaseAlchemySigner.signMessage", async (msg) => {
228
327
  const messageHash = hashMessage(msg);
229
- return this.inner.signRawMessage(messageHash);
230
- }
328
+ const result = await this.inner.signRawMessage(messageHash);
329
+ SignerLogger.trackEvent({
330
+ name: "signer_sign_message",
331
+ });
332
+ return result;
333
+ })
231
334
  });
232
335
  /**
233
336
  * Signs a typed message by first hashing it and then signing the hashed message using the `signRawMessage` method.
@@ -262,10 +365,10 @@ export class BaseAlchemySigner {
262
365
  enumerable: true,
263
366
  configurable: true,
264
367
  writable: true,
265
- value: async (params) => {
368
+ value: SignerLogger.profiled("BaseAlchemySigner.signTypedData", async (params) => {
266
369
  const messageHash = hashTypedData(params);
267
370
  return this.inner.signRawMessage(messageHash);
268
- }
371
+ })
269
372
  });
270
373
  /**
271
374
  * Serializes a transaction, signs it with a raw message, and then returns the serialized transaction with the signature.
@@ -301,7 +404,7 @@ export class BaseAlchemySigner {
301
404
  enumerable: true,
302
405
  configurable: true,
303
406
  writable: true,
304
- value: async (tx, args) => {
407
+ value: SignerLogger.profiled("BaseAlchemySigner.signTransaction", async (tx, args) => {
305
408
  const serializeFn = args?.serializer ?? serializeTransaction;
306
409
  const serializedTx = serializeFn(tx);
307
410
  const signatureHex = await this.inner.signRawMessage(keccak256(serializedTx));
@@ -311,7 +414,7 @@ export class BaseAlchemySigner {
311
414
  v: BigInt(takeBytes(signatureHex, { count: 1, offset: 64 })),
312
415
  };
313
416
  return serializeFn(tx, signature);
314
- }
417
+ })
315
418
  });
316
419
  /**
317
420
  * Unauthenticated call to look up a user's organizationId by email
@@ -341,7 +444,7 @@ export class BaseAlchemySigner {
341
444
  enumerable: true,
342
445
  configurable: true,
343
446
  writable: true,
344
- value: async (email) => {
447
+ value: SignerLogger.profiled("BaseAlchemySigner.getUser", async (email) => {
345
448
  const result = await this.inner.lookupUserByEmail(email);
346
449
  if (result.orgId == null) {
347
450
  return null;
@@ -349,7 +452,7 @@ export class BaseAlchemySigner {
349
452
  return {
350
453
  orgId: result.orgId,
351
454
  };
352
- }
455
+ })
353
456
  });
354
457
  /**
355
458
  * Adds a passkey to the user's account
@@ -379,9 +482,9 @@ export class BaseAlchemySigner {
379
482
  enumerable: true,
380
483
  configurable: true,
381
484
  writable: true,
382
- value: async (params) => {
485
+ value: SignerLogger.profiled("BaseAlchemySigner.addPasskey", async (params) => {
383
486
  return this.inner.addPasskey(params ?? {});
384
- }
487
+ })
385
488
  });
386
489
  /**
387
490
  * Used to export the wallet for a given user
@@ -468,20 +571,24 @@ export class BaseAlchemySigner {
468
571
  value: async (params) => {
469
572
  if ("email" in params) {
470
573
  const existingUser = await this.getUser(params.email);
574
+ const expirationSeconds = Math.floor(this.sessionManager.expirationTimeMs / 1000);
471
575
  const { orgId } = existingUser
472
576
  ? await this.inner.initEmailAuth({
473
577
  email: params.email,
474
- expirationSeconds: this.sessionManager.expirationTimeMs,
578
+ expirationSeconds,
475
579
  redirectParams: params.redirectParams,
476
580
  })
477
581
  : await this.inner.createAccount({
478
582
  type: "email",
479
583
  email: params.email,
480
- expirationSeconds: this.sessionManager.expirationTimeMs,
584
+ expirationSeconds,
481
585
  redirectParams: params.redirectParams,
482
586
  });
483
587
  this.sessionManager.setTemporarySession({ orgId });
484
- this.store.setState({ status: AlchemySignerStatus.AWAITING_EMAIL_AUTH });
588
+ this.store.setState({
589
+ status: AlchemySignerStatus.AWAITING_EMAIL_AUTH,
590
+ error: null,
591
+ });
485
592
  // We wait for the session manager to emit a connected event if
486
593
  // cross tab sessions are permitted
487
594
  return new Promise((resolve) => {
@@ -499,9 +606,11 @@ export class BaseAlchemySigner {
499
606
  this.store.setState({ status: AlchemySignerStatus.DISCONNECTED });
500
607
  throw new Error("Could not find email auth init session!");
501
608
  }
502
- const user = await this.inner.completeEmailAuth({
609
+ const user = await this.inner.completeAuthWithBundle({
503
610
  bundle: params.bundle,
504
611
  orgId: temporarySession.orgId,
612
+ connectedEventName: "connectedEmail",
613
+ authenticatingType: "email",
505
614
  });
506
615
  return user;
507
616
  }
@@ -540,6 +649,35 @@ export class BaseAlchemySigner {
540
649
  return user;
541
650
  }
542
651
  });
652
+ Object.defineProperty(this, "authenticateWithOauth", {
653
+ enumerable: true,
654
+ configurable: true,
655
+ writable: true,
656
+ value: async (args) => {
657
+ const params = {
658
+ ...args,
659
+ expirationSeconds: Math.floor(this.sessionManager.expirationTimeMs / 1000),
660
+ };
661
+ if (params.mode === "redirect") {
662
+ return this.inner.oauthWithRedirect(params);
663
+ }
664
+ else {
665
+ return this.inner.oauthWithPopup(params);
666
+ }
667
+ }
668
+ });
669
+ Object.defineProperty(this, "handleOauthReturn", {
670
+ enumerable: true,
671
+ configurable: true,
672
+ writable: true,
673
+ value: ({ bundle, orgId, idToken, }) => this.inner.completeAuthWithBundle({
674
+ bundle,
675
+ orgId,
676
+ connectedEventName: "connectedOauth",
677
+ authenticatingType: "oauth",
678
+ idToken,
679
+ })
680
+ });
543
681
  Object.defineProperty(this, "registerListeners", {
544
682
  enumerable: true,
545
683
  configurable: true,
@@ -549,6 +687,7 @@ export class BaseAlchemySigner {
549
687
  this.store.setState({
550
688
  user: session.user,
551
689
  status: AlchemySignerStatus.CONNECTED,
690
+ error: null,
552
691
  });
553
692
  });
554
693
  this.sessionManager.on("disconnected", () => {
@@ -562,10 +701,26 @@ export class BaseAlchemySigner {
562
701
  status: state.user
563
702
  ? AlchemySignerStatus.CONNECTED
564
703
  : AlchemySignerStatus.DISCONNECTED,
704
+ ...(state.user ? { error: null } : undefined),
565
705
  }));
566
706
  });
567
- this.inner.on("authenticating", () => {
568
- this.store.setState({ status: AlchemySignerStatus.AUTHENTICATING });
707
+ this.inner.on("authenticating", ({ type }) => {
708
+ const status = (() => {
709
+ switch (type) {
710
+ case "email":
711
+ return AlchemySignerStatus.AUTHENTICATING_EMAIL;
712
+ case "passkey":
713
+ return AlchemySignerStatus.AUTHENTICATING_PASSKEY;
714
+ case "oauth":
715
+ return AlchemySignerStatus.AUTHENTICATING_OAUTH;
716
+ default:
717
+ assertNever(type, "unhandled authenticating type");
718
+ }
719
+ })();
720
+ this.store.setState({
721
+ status,
722
+ error: null,
723
+ });
569
724
  });
570
725
  }
571
726
  });
@@ -573,6 +728,7 @@ export class BaseAlchemySigner {
573
728
  this.store = createStore(subscribeWithSelector(() => ({
574
729
  user: null,
575
730
  status: AlchemySignerStatus.INITIALIZING,
731
+ error: initialError ?? null,
576
732
  })));
577
733
  // NOTE: it's important that the session manager share a client
578
734
  // with the signer. The SessionManager leverages the Signer's client
@@ -581,14 +737,15 @@ export class BaseAlchemySigner {
581
737
  ...sessionConfig,
582
738
  client: this.inner,
583
739
  });
584
- this.store = createStore(subscribeWithSelector(() => ({
585
- user: null,
586
- status: AlchemySignerStatus.INITIALIZING,
587
- })));
588
740
  // register listeners first
589
741
  this.registerListeners();
590
742
  // then initialize so that we can catch those events
591
743
  this.sessionManager.initialize();
592
744
  }
593
745
  }
746
+ function toErrorInfo(error) {
747
+ return error instanceof Error
748
+ ? { name: error.name, message: error.message }
749
+ : { name: "Error", message: "Unknown error" };
750
+ }
594
751
  //# sourceMappingURL=base.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkC,MAAM,cAAc,CAAC;AACzE,OAAO,EACL,WAAW,EACX,aAAa,EACb,SAAS,EACT,oBAAoB,GAOrB,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG9C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EACL,cAAc,GAEf,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACL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,CAAC,YAAY;SACZ,CAAC,CAClC,CACF,CAAC;QACF,+DAA+D;QAC/D,oEAAoE;QACpE,2BAA2B;QAC3B,IAAI,CAAC,cAAc,GAAG,IAAI,cAAc,CAAC;YACvC,GAAG,aAAa;YAChB,MAAM,EAAE,IAAI,CAAC,KAAK;SACnB,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,GAAG,WAAW,CACtB,qBAAqB,CACnB,GAAG,EAAE,CACH,CAAC;YACC,IAAI,EAAE,IAAI;YACV,MAAM,EAAE,mBAAmB,CAAC,YAAY;SACZ,CAAC,CAClC,CACF,CAAC;QACF,2BAA2B;QAC3B,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,oDAAoD;QACpD,IAAI,CAAC,cAAc,CAAC,UAAU,EAAE,CAAC;IACnC,CAAC;CAuhBF","sourcesContent":["import { takeBytes, type SmartAccountAuthenticator } from \"@aa-sdk/core\";\nimport {\n hashMessage,\n hashTypedData,\n keccak256,\n serializeTransaction,\n type CustomSource,\n type Hex,\n type LocalAccount,\n type SignableMessage,\n type TypedData,\n type TypedDataDefinition,\n} from \"viem\";\nimport { toAccount } from \"viem/accounts\";\nimport type { Mutate, StoreApi } from \"zustand\";\nimport { subscribeWithSelector } from \"zustand/middleware\";\nimport { createStore } from \"zustand/vanilla\";\nimport type { BaseSignerClient } from \"./client/base\";\nimport type { User } from \"./client/types\";\nimport { NotAuthenticatedError } from \"./errors.js\";\nimport {\n SessionManager,\n type SessionManagerParams,\n} from \"./session/manager.js\";\nimport type { AuthParams } from \"./signer\";\nimport {\n AlchemySignerStatus,\n type AlchemySignerEvent,\n type AlchemySignerEvents,\n} from \"./types.js\";\n\nexport interface BaseAlchemySignerParams<TClient extends BaseSignerClient> {\n client: TClient;\n sessionConfig?: Omit<SessionManagerParams, \"client\">;\n}\n\ntype AlchemySignerStore = {\n user: User | null;\n status: AlchemySignerStatus;\n};\n\ntype InternalStore = Mutate<\n StoreApi<AlchemySignerStore>,\n [[\"zustand/subscribeWithSelector\", never]]\n>;\n\n/**\n * Base abstract class for Alchemy Signer, providing authentication and session management for smart accounts.\n * Implements the `SmartAccountAuthenticator` interface and handles various signer events.\n */\nexport abstract class BaseAlchemySigner<TClient extends BaseSignerClient>\n implements SmartAccountAuthenticator<AuthParams, User, TClient>\n{\n signerType: string = \"alchemy-signer\";\n inner: TClient;\n private sessionManager: SessionManager;\n private store: InternalStore;\n\n /**\n * Initializes an instance with the provided client and session configuration.\n * This function sets up the internal store, initializes the session manager,\n * registers listeners and initializes the session manager to manage session state.\n *\n * @param {BaseAlchemySignerParams<TClient>} param0 Object containing the client and session configuration\n * @param {TClient} param0.client The client instance to be used internally\n * @param {SessionConfig} param0.sessionConfig Configuration for managing sessions\n */\n constructor({ client, sessionConfig }: BaseAlchemySignerParams<TClient>) {\n this.inner = client;\n this.store = createStore(\n subscribeWithSelector(\n () =>\n ({\n user: null,\n status: AlchemySignerStatus.INITIALIZING,\n } satisfies AlchemySignerStore)\n )\n );\n // NOTE: it's important that the session manager share a client\n // with the signer. The SessionManager leverages the Signer's client\n // to manage session state.\n this.sessionManager = new SessionManager({\n ...sessionConfig,\n client: this.inner,\n });\n this.store = createStore(\n subscribeWithSelector(\n () =>\n ({\n user: null,\n status: AlchemySignerStatus.INITIALIZING,\n } satisfies AlchemySignerStore)\n )\n );\n // register listeners first\n this.registerListeners();\n // then initialize so that we can catch those events\n this.sessionManager.initialize();\n }\n\n /**\n * Allows you to subscribe to events emitted by the signer\n *\n * @param {AlchemySignerEvent} event the event to subscribe to\n * @param {AlchemySignerEvents[AlchemySignerEvent]} listener the function to run when the event is emitted\n * @returns {() => void} a function to remove the listener\n */\n on = <E extends AlchemySignerEvent>(\n event: E,\n listener: AlchemySignerEvents[E]\n ) => {\n // NOTE: we're using zustand here to handle this because we are able to use the fireImmediately\n // option which deals with a possible race condition where the listener is added after the event\n // is fired. In the Client and SessionManager we use EventEmitter because it's easier to handle internally\n switch (event) {\n case \"connected\":\n return this.store.subscribe(\n ({ status }) => status,\n (status) =>\n status === AlchemySignerStatus.CONNECTED &&\n (listener as AlchemySignerEvents[\"connected\"])(\n this.store.getState().user!\n ),\n { fireImmediately: true }\n );\n case \"disconnected\":\n return this.store.subscribe(\n ({ status }) => status,\n (status) =>\n status === AlchemySignerStatus.DISCONNECTED &&\n (listener as AlchemySignerEvents[\"disconnected\"])(),\n { fireImmediately: true }\n );\n case \"statusChanged\":\n return this.store.subscribe(\n ({ status }) => status,\n listener as AlchemySignerEvents[\"statusChanged\"],\n { fireImmediately: true }\n );\n default:\n throw new Error(`Uknown event type ${event}`);\n }\n };\n\n /**\n * Authenticate a user with either an email or a passkey and create a session for that user\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const result = await signer.authenticate({\n * type: \"email\",\n * email: \"foo@mail.com\",\n * });\n * ```\n *\n * @param {AuthParams} params - undefined if passkey login, otherwise an object with email and bundle to resolve\n * @returns {Promise<User>} the user that was authenticated\n */\n authenticate: (params: AuthParams) => Promise<User> = async (params) => {\n if (params.type === \"email\") {\n return this.authenticateWithEmail(params);\n }\n\n return this.authenticateWithPasskey(params);\n };\n\n /**\n * Clear a user session and log them out\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * await signer.disconnect();\n * ```\n *\n * @returns {Promise<void>} a promise that resolves when the user is logged out\n */\n disconnect: () => Promise<void> = async () => {\n await this.inner.disconnect();\n };\n\n /**\n * Gets the current logged in user\n * If a user has an ongoing session, it will use that session and\n * try to authenticate\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * // throws if not logged in\n * const user = await signer.getAuthDetails();\n * ```\n *\n * @throws if there is no user logged in\n * @returns {Promise<User>} the current user\n */\n getAuthDetails = async (): Promise<User> => {\n const sessionUser = await this.sessionManager.getSessionUser();\n if (sessionUser != null) {\n return sessionUser;\n }\n\n return this.inner.whoami();\n };\n\n /**\n * Retrieves the address of the current user by calling the `whoami` method on `this.inner`.\n *\n * @returns {Promise<string>} A promise that resolves to the address of the current user.\n */\n getAddress: () => Promise<`0x${string}`> = async () => {\n const { address } = await this.inner.whoami();\n\n return address;\n };\n\n /**\n * Signs a raw message after hashing it.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const signature = await signer.signMessage(\"Hello, world!\");\n * ```\n *\n * @param {string} msg the message to be hashed and then signed\n * @returns {Promise<string>} a promise that resolves to the signed message\n */\n signMessage: (msg: SignableMessage) => Promise<`0x${string}`> = async (\n msg\n ) => {\n const messageHash = hashMessage(msg);\n\n return this.inner.signRawMessage(messageHash);\n };\n\n /**\n * Signs a typed message by first hashing it and then signing the hashed message using the `signRawMessage` method.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const signature = await signer.signTypedData({\n * domain: {},\n * types: {},\n * primaryType: \"\",\n * message: {},\n * });\n * ```\n *\n * @param {TypedDataDefinition<TTypedData, TPrimaryType>} params The parameters for the typed message to be hashed and signed\n * @returns {Promise<any>} A promise that resolves to the signed message\n */\n signTypedData: <\n const TTypedData extends TypedData | { [key: string]: unknown },\n TPrimaryType extends keyof TTypedData | \"EIP712Domain\" = keyof TTypedData\n >(\n params: TypedDataDefinition<TTypedData, TPrimaryType>\n ) => Promise<Hex> = async (params) => {\n const messageHash = hashTypedData(params);\n\n return this.inner.signRawMessage(messageHash);\n };\n\n /**\n * Serializes a transaction, signs it with a raw message, and then returns the serialized transaction with the signature.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const tx = await signer.signTransaction({\n * to: \"0x1234\",\n * value: \"0x1234\",\n * data: \"0x1234\",\n * });\n * ```\n *\n * @param {Transaction} tx the transaction to be serialized and signed\n * @param {{serializer?: SerializeTransactionFn}} args options for serialization\n * @param {() => Hex} [args.serializer] an optional serializer function. If not provided, the default `serializeTransaction` function will be used\n * @returns {Promise<string>} a promise that resolves to the serialized transaction with the signature\n */\n signTransaction: CustomSource[\"signTransaction\"] = async (tx, args) => {\n const serializeFn = args?.serializer ?? serializeTransaction;\n const serializedTx = serializeFn(tx);\n const signatureHex = await this.inner.signRawMessage(\n keccak256(serializedTx)\n );\n\n const signature = {\n r: takeBytes(signatureHex, { count: 32 }),\n s: takeBytes(signatureHex, { count: 32, offset: 32 }),\n v: BigInt(takeBytes(signatureHex, { count: 1, offset: 64 })),\n };\n\n return serializeFn(tx, signature);\n };\n\n /**\n * Unauthenticated call to look up a user's organizationId by email\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const result = await signer.getUser(\"foo@mail.com\");\n * ```\n *\n * @param {string} email the email to lookup\n * @returns {Promise<{orgId: string}>} the organization id for the user if they exist\n */\n getUser: (email: string) => Promise<{ orgId: string } | null> = async (\n email\n ) => {\n const result = await this.inner.lookupUserByEmail(email);\n\n if (result.orgId == null) {\n return null;\n }\n\n return {\n orgId: result.orgId,\n };\n };\n\n /**\n * Adds a passkey to the user's account\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const result = await signer.addPasskey()\n * ```\n *\n * @param {CredentialCreationOptions | undefined} params optional parameters for the passkey creation\n * @returns {Promise<string[]>} an array of the authenticator ids added to the user\n */\n addPasskey: (params?: CredentialCreationOptions) => Promise<string[]> =\n async (params) => {\n return this.inner.addPasskey(params ?? {});\n };\n\n /**\n * Used to export the wallet for a given user\n * If the user is authenticated with an Email, this will return a seed phrase\n * If the user is authenticated with a Passkey, this will return a private key\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * // the params passed to this are different based on the specific signer\n * const result = signer.exportWallet()\n * ```\n *\n * @param {unknown} params export wallet parameters\n * @returns {boolean} true if the wallet was exported successfully\n */\n exportWallet: (\n params: Parameters<(typeof this.inner)[\"exportWallet\"]>[0]\n ) => Promise<boolean> = async (params) => {\n return this.inner.exportWallet(params);\n };\n\n /**\n * This method lets you adapt your AlchemySigner to a viem LocalAccount, which\n * will let you use the signer as an EOA directly.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const account = signer.toViemAccount();\n * ```\n *\n * @throws if your signer is not authenticated\n * @returns {LocalAccount} a LocalAccount object that can be used with viem's wallet client\n */\n toViemAccount = (): LocalAccount => {\n // if we want this method to be synchronous, then we need to do this check here\n // otherwise we can use the sessionManager to get the user\n if (!this.inner.getUser()) {\n throw new NotAuthenticatedError();\n }\n\n return toAccount({\n address: this.inner.getUser()!.address,\n signMessage: (msg) => this.signMessage(msg.message),\n signTypedData: <\n const typedData extends TypedData | Record<string, unknown>,\n primaryType extends keyof typedData | \"EIP712Domain\" = keyof typedData\n >(\n typedDataDefinition: TypedDataDefinition<typedData, primaryType>\n ) => this.signTypedData<typedData, primaryType>(typedDataDefinition),\n signTransaction: this.signTransaction,\n });\n };\n\n private authenticateWithEmail = async (\n params: Extract<AuthParams, { type: \"email\" }>\n ): Promise<User> => {\n if (\"email\" in params) {\n const existingUser = await this.getUser(params.email);\n\n const { orgId } = existingUser\n ? await this.inner.initEmailAuth({\n email: params.email,\n expirationSeconds: this.sessionManager.expirationTimeMs,\n redirectParams: params.redirectParams,\n })\n : await this.inner.createAccount({\n type: \"email\",\n email: params.email,\n expirationSeconds: this.sessionManager.expirationTimeMs,\n redirectParams: params.redirectParams,\n });\n\n this.sessionManager.setTemporarySession({ orgId });\n this.store.setState({ status: AlchemySignerStatus.AWAITING_EMAIL_AUTH });\n\n // We wait for the session manager to emit a connected event if\n // cross tab sessions are permitted\n return new Promise<User>((resolve) => {\n const removeListener = this.sessionManager.on(\n \"connected\",\n (session) => {\n resolve(session.user);\n removeListener();\n }\n );\n });\n } else {\n const temporarySession = params.orgId\n ? { orgId: params.orgId }\n : this.sessionManager.getTemporarySession();\n\n if (!temporarySession) {\n this.store.setState({ status: AlchemySignerStatus.DISCONNECTED });\n throw new Error(\"Could not find email auth init session!\");\n }\n\n const user = await this.inner.completeEmailAuth({\n bundle: params.bundle,\n orgId: temporarySession.orgId,\n });\n\n return user;\n }\n };\n\n private authenticateWithPasskey = async (\n args: Extract<AuthParams, { type: \"passkey\" }>\n ) => {\n let user: User;\n const shouldCreateNew = async () => {\n if (\"email\" in args) {\n const existingUser = await this.getUser(args.email);\n return existingUser == null;\n }\n\n return args.createNew;\n };\n\n if (await shouldCreateNew()) {\n const result = await this.inner.createAccount(\n args as Extract<\n AuthParams,\n { type: \"passkey\" } & ({ email: string } | { createNew: true })\n >\n );\n // account creation for passkeys returns the whoami response so we don't have to\n // call it again after signup\n user = {\n address: result.address!,\n userId: result.userId!,\n orgId: result.orgId,\n };\n } else {\n user = await this.inner.lookupUserWithPasskey();\n if (!user) {\n this.store.setState({ status: AlchemySignerStatus.DISCONNECTED });\n throw new Error(\"No user found\");\n }\n }\n\n return user;\n };\n\n private registerListeners = () => {\n this.sessionManager.on(\"connected\", (session) => {\n this.store.setState({\n user: session.user,\n status: AlchemySignerStatus.CONNECTED,\n });\n });\n\n this.sessionManager.on(\"disconnected\", () => {\n this.store.setState({\n user: null,\n status: AlchemySignerStatus.DISCONNECTED,\n });\n });\n\n this.sessionManager.on(\"initialized\", () => {\n this.store.setState((state) => ({\n status: state.user\n ? AlchemySignerStatus.CONNECTED\n : AlchemySignerStatus.DISCONNECTED,\n }));\n });\n\n this.inner.on(\"authenticating\", () => {\n this.store.setState({ status: AlchemySignerStatus.AUTHENTICATING });\n });\n };\n}\n"]}
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkC,MAAM,cAAc,CAAC;AACzE,OAAO,EACL,WAAW,EACX,aAAa,EACb,SAAS,EACT,oBAAoB,GAWrB,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,SA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C;4BACpD,KAAK,OAAO;gCACV,OAAO,mBAAmB,CAAC,oBAAoB,CAAC;4BAClD;gCACE,WAAW,CAAC,IAAI,EAAE,+BAA+B,CAAC,CAAC;wBACvD,CAAC;oBACH,CAAC,CAAC,EAAE,CAAC;oBAEL,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;wBAClB,MAAM;wBACN,KAAK,EAAE,IAAI;qBACZ,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;YACL,CAAC;WAAC;QAnuBA,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC;QACpB,IAAI,CAAC,KAAK,GAAG,WAAW,CACtB,qBAAqB,CACnB,GAAG,EAAE,CACH,CAAC;YACC,IAAI,EAAE,IAAI;YACV,MAAM,EAAE,mBAAmB,CAAC,YAAY;YACxC,KAAK,EAAE,YAAY,IAAI,IAAI;SACC,CAAC,CAClC,CACF,CAAC;QACF,+DAA+D;QAC/D,oEAAoE;QACpE,2BAA2B;QAC3B,IAAI,CAAC,cAAc,GAAG,IAAI,cAAc,CAAC;YACvC,GAAG,aAAa;YAChB,MAAM,EAAE,IAAI,CAAC,KAAK;SACnB,CAAC,CAAC;QACH,2BAA2B;QAC3B,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,oDAAoD;QACpD,IAAI,CAAC,cAAc,CAAC,UAAU,EAAE,CAAC;IACnC,CAAC;CA8sBF;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,OAAO,KAAK,YAAY,KAAK;QAC3B,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE;QAC9C,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,CAAC;AAClD,CAAC","sourcesContent":["import { takeBytes, type SmartAccountAuthenticator } from \"@aa-sdk/core\";\nimport {\n hashMessage,\n hashTypedData,\n keccak256,\n serializeTransaction,\n type GetTransactionType,\n type Hex,\n type IsNarrowable,\n type LocalAccount,\n type SerializeTransactionFn,\n type SignableMessage,\n type TransactionSerializable,\n type TransactionSerialized,\n type TypedData,\n type TypedDataDefinition,\n} from \"viem\";\nimport { toAccount } from \"viem/accounts\";\nimport type { Mutate, StoreApi } from \"zustand\";\nimport { subscribeWithSelector } from \"zustand/middleware\";\nimport { createStore } from \"zustand/vanilla\";\nimport type { BaseSignerClient } from \"./client/base\";\nimport type { OauthConfig, OauthParams, User } from \"./client/types\";\nimport { NotAuthenticatedError } from \"./errors.js\";\nimport { SignerLogger } from \"./metrics.js\";\nimport {\n SessionManager,\n type SessionManagerParams,\n} from \"./session/manager.js\";\nimport type { AuthParams } from \"./signer\";\nimport {\n AlchemySignerStatus,\n type AlchemySignerEvent,\n type AlchemySignerEvents,\n type ErrorInfo,\n} from \"./types.js\";\nimport { assertNever } from \"./utils/typeAssertions.js\";\n\nexport interface BaseAlchemySignerParams<TClient extends BaseSignerClient> {\n client: TClient;\n sessionConfig?: Omit<SessionManagerParams, \"client\">;\n initialError?: ErrorInfo;\n}\n\ntype AlchemySignerStore = {\n user: User | null;\n status: AlchemySignerStatus;\n error: ErrorInfo | null;\n};\n\ntype InternalStore = Mutate<\n StoreApi<AlchemySignerStore>,\n [[\"zustand/subscribeWithSelector\", never]]\n>;\n\n/**\n * Base abstract class for Alchemy Signer, providing authentication and session management for smart accounts.\n * Implements the `SmartAccountAuthenticator` interface and handles various signer events.\n */\nexport abstract class BaseAlchemySigner<TClient extends BaseSignerClient>\n implements SmartAccountAuthenticator<AuthParams, User, TClient>\n{\n signerType: string = \"alchemy-signer\";\n inner: TClient;\n private sessionManager: SessionManager;\n private store: InternalStore;\n\n /**\n * Initializes an instance with the provided client and session configuration.\n * This function sets up the internal store, initializes the session manager,\n * registers listeners and initializes the session manager to manage session state.\n *\n * @param {BaseAlchemySignerParams<TClient>} param0 Object containing the client and session configuration\n * @param {TClient} param0.client The client instance to be used internally\n * @param {SessionConfig} param0.sessionConfig Configuration for managing sessions\n * @param {ErrorInfo | undefined} param0.initialError Error already present on the signer when initialized, if any\n */\n constructor({\n client,\n sessionConfig,\n initialError,\n }: BaseAlchemySignerParams<TClient>) {\n this.inner = client;\n this.store = createStore(\n subscribeWithSelector(\n () =>\n ({\n user: null,\n status: AlchemySignerStatus.INITIALIZING,\n error: initialError ?? null,\n } satisfies AlchemySignerStore)\n )\n );\n // NOTE: it's important that the session manager share a client\n // with the signer. The SessionManager leverages the Signer's client\n // to manage session state.\n this.sessionManager = new SessionManager({\n ...sessionConfig,\n client: this.inner,\n });\n // register listeners first\n this.registerListeners();\n // then initialize so that we can catch those events\n this.sessionManager.initialize();\n }\n\n /**\n * Allows you to subscribe to events emitted by the signer\n *\n * @param {AlchemySignerEvent} event the event to subscribe to\n * @param {AlchemySignerEvents[AlchemySignerEvent]} listener the function to run when the event is emitted\n * @returns {() => void} a function to remove the listener\n */\n on = <E extends AlchemySignerEvent>(\n event: E,\n listener: AlchemySignerEvents[E]\n ) => {\n // NOTE: we're using zustand here to handle this because we are able to use the fireImmediately\n // option which deals with a possible race condition where the listener is added after the event\n // is fired. In the Client and SessionManager we use EventEmitter because it's easier to handle internally\n switch (event) {\n case \"connected\":\n return this.store.subscribe(\n ({ status }) => status,\n (status) =>\n status === AlchemySignerStatus.CONNECTED &&\n (listener as AlchemySignerEvents[\"connected\"])(\n this.store.getState().user!\n ),\n { fireImmediately: true }\n );\n case \"disconnected\":\n return this.store.subscribe(\n ({ status }) => status,\n (status) =>\n status === AlchemySignerStatus.DISCONNECTED &&\n (listener as AlchemySignerEvents[\"disconnected\"])(),\n { fireImmediately: true }\n );\n case \"statusChanged\":\n return this.store.subscribe(\n ({ status }) => status,\n listener as AlchemySignerEvents[\"statusChanged\"],\n { fireImmediately: true }\n );\n case \"errorChanged\":\n return this.store.subscribe(\n ({ error }) => error,\n (error) =>\n (listener as AlchemySignerEvents[\"errorChanged\"])(\n error ?? undefined\n ),\n { fireImmediately: true }\n );\n default:\n assertNever(event, `Unknown event type ${event}`);\n }\n };\n\n /**\n * Prepares the config needed to use popup-based OAuth login. This must be\n * called before calling `.authenticate` with params `{ type: \"oauth\", mode:\n * \"popup\" }`, and is recommended to be called on page load.\n *\n * This method exists because browsers may prevent popups from opening unless\n * triggered by user interaction, and so the OAuth config must already have\n * been fetched at the time a user clicks a social login button.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * await signer.preparePopupOauth();\n * ```\n * @returns {Promise<OauthConfig>} the config which must be loaded before\n * using popup-based OAuth\n */\n preparePopupOauth = (): Promise<OauthConfig> => this.inner.initOauth();\n\n /**\n * Authenticate a user with either an email or a passkey and create a session for that user\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const result = await signer.authenticate({\n * type: \"email\",\n * email: \"foo@mail.com\",\n * });\n * ```\n *\n * @param {AuthParams} params - undefined if passkey login, otherwise an object with email and bundle to resolve\n * @returns {Promise<User>} the user that was authenticated\n */\n authenticate: (params: AuthParams) => Promise<User> = SignerLogger.profiled(\n \"BaseAlchemySigner.authenticate\",\n async (params) => {\n const { type } = params;\n const result = (() => {\n switch (type) {\n case \"email\":\n return this.authenticateWithEmail(params);\n case \"passkey\":\n return this.authenticateWithPasskey(params);\n case \"oauth\":\n return this.authenticateWithOauth(params);\n case \"oauthReturn\":\n return this.handleOauthReturn(params);\n default:\n assertNever(type, `Unknown auth type: ${type}`);\n }\n })();\n\n this.trackAuthenticateType(params);\n\n return result.catch((error) => {\n this.store.setState({ error: toErrorInfo(error) });\n throw error;\n });\n }\n );\n\n private trackAuthenticateType = (params: AuthParams) => {\n const { type } = params;\n switch (type) {\n case \"email\": {\n // we just want to track the start of email auth\n if (\"bundle\" in params) return;\n SignerLogger.trackEvent({\n name: \"signer_authnticate\",\n data: { authType: \"email\" },\n });\n return;\n }\n case \"passkey\": {\n const isAnon = !(\"email\" in params) && params.createNew == null;\n SignerLogger.trackEvent({\n name: \"signer_authnticate\",\n data: {\n authType: isAnon ? \"passkey_anon\" : \"passkey_email\",\n },\n });\n return;\n }\n case \"oauth\":\n SignerLogger.trackEvent({\n name: \"signer_authnticate\",\n data: {\n authType: \"oauth\",\n provider: params.authProviderId,\n },\n });\n break;\n case \"oauthReturn\":\n break;\n default:\n assertNever(type, `Unknown auth type: ${type}`);\n }\n };\n\n /**\n * Clear a user session and log them out\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * await signer.disconnect();\n * ```\n *\n * @returns {Promise<void>} a promise that resolves when the user is logged out\n */\n disconnect: () => Promise<void> = async () => {\n await this.inner.disconnect();\n };\n\n /**\n * Gets the current logged in user\n * If a user has an ongoing session, it will use that session and\n * try to authenticate\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * // throws if not logged in\n * const user = await signer.getAuthDetails();\n * ```\n *\n * @throws if there is no user logged in\n * @returns {Promise<User>} the current user\n */\n getAuthDetails = async (): Promise<User> => {\n const sessionUser = await this.sessionManager.getSessionUser();\n if (sessionUser != null) {\n return sessionUser;\n }\n\n return this.inner.whoami();\n };\n\n /**\n * Retrieves the address of the current user by calling the `whoami` method on `this.inner`.\n *\n * @returns {Promise<string>} A promise that resolves to the address of the current user.\n */\n getAddress: () => Promise<`0x${string}`> = SignerLogger.profiled(\n \"BaseAlchemySigner.getAddress\",\n async () => {\n const { address } = await this.inner.whoami();\n\n return address;\n }\n );\n\n /**\n * Signs a raw message after hashing it.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const signature = await signer.signMessage(\"Hello, world!\");\n * ```\n *\n * @param {string} msg the message to be hashed and then signed\n * @returns {Promise<string>} a promise that resolves to the signed message\n */\n signMessage: (msg: SignableMessage) => Promise<`0x${string}`> =\n SignerLogger.profiled(\"BaseAlchemySigner.signMessage\", async (msg) => {\n const messageHash = hashMessage(msg);\n\n const result = await this.inner.signRawMessage(messageHash);\n\n SignerLogger.trackEvent({\n name: \"signer_sign_message\",\n });\n\n return result;\n });\n\n /**\n * Signs a typed message by first hashing it and then signing the hashed message using the `signRawMessage` method.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const signature = await signer.signTypedData({\n * domain: {},\n * types: {},\n * primaryType: \"\",\n * message: {},\n * });\n * ```\n *\n * @param {TypedDataDefinition<TTypedData, TPrimaryType>} params The parameters for the typed message to be hashed and signed\n * @returns {Promise<any>} A promise that resolves to the signed message\n */\n signTypedData: <\n const TTypedData extends TypedData | Record<string, unknown>,\n TPrimaryType extends keyof TTypedData | \"EIP712Domain\" = keyof TTypedData\n >(\n params: TypedDataDefinition<TTypedData, TPrimaryType>\n ) => Promise<Hex> = SignerLogger.profiled(\n \"BaseAlchemySigner.signTypedData\",\n async (params) => {\n const messageHash = hashTypedData(params);\n\n return this.inner.signRawMessage(messageHash);\n }\n );\n\n /**\n * Serializes a transaction, signs it with a raw message, and then returns the serialized transaction with the signature.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const tx = await signer.signTransaction({\n * to: \"0x1234\",\n * value: \"0x1234\",\n * data: \"0x1234\",\n * });\n * ```\n *\n * @param {Transaction} tx the transaction to be serialized and signed\n * @param {{serializer?: SerializeTransactionFn}} args options for serialization\n * @param {() => Hex} [args.serializer] an optional serializer function. If not provided, the default `serializeTransaction` function will be used\n * @returns {Promise<string>} a promise that resolves to the serialized transaction with the signature\n */\n signTransaction: <\n serializer extends SerializeTransactionFn<TransactionSerializable> = SerializeTransactionFn<TransactionSerializable>,\n transaction extends Parameters<serializer>[0] = Parameters<serializer>[0]\n >(\n transaction: transaction,\n options?:\n | {\n serializer?: serializer | undefined;\n }\n | undefined\n ) => Promise<\n IsNarrowable<\n TransactionSerialized<GetTransactionType<transaction>>,\n Hex\n > extends true\n ? TransactionSerialized<GetTransactionType<transaction>>\n : Hex\n > = SignerLogger.profiled(\n \"BaseAlchemySigner.signTransaction\",\n async (tx, args) => {\n const serializeFn = args?.serializer ?? serializeTransaction;\n const serializedTx = serializeFn(tx);\n const signatureHex = await this.inner.signRawMessage(\n keccak256(serializedTx)\n );\n\n const signature = {\n r: takeBytes(signatureHex, { count: 32 }),\n s: takeBytes(signatureHex, { count: 32, offset: 32 }),\n v: BigInt(takeBytes(signatureHex, { count: 1, offset: 64 })),\n };\n\n return serializeFn(tx, signature);\n }\n );\n\n /**\n * Unauthenticated call to look up a user's organizationId by email\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const result = await signer.getUser(\"foo@mail.com\");\n * ```\n *\n * @param {string} email the email to lookup\n * @returns {Promise<{orgId: string}>} the organization id for the user if they exist\n */\n getUser: (email: string) => Promise<{ orgId: string } | null> =\n SignerLogger.profiled(\"BaseAlchemySigner.getUser\", async (email) => {\n const result = await this.inner.lookupUserByEmail(email);\n\n if (result.orgId == null) {\n return null;\n }\n\n return {\n orgId: result.orgId,\n };\n });\n\n /**\n * Adds a passkey to the user's account\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const result = await signer.addPasskey()\n * ```\n *\n * @param {CredentialCreationOptions | undefined} params optional parameters for the passkey creation\n * @returns {Promise<string[]>} an array of the authenticator ids added to the user\n */\n addPasskey: (params?: CredentialCreationOptions) => Promise<string[]> =\n SignerLogger.profiled(\"BaseAlchemySigner.addPasskey\", async (params) => {\n return this.inner.addPasskey(params ?? {});\n });\n\n /**\n * Used to export the wallet for a given user\n * If the user is authenticated with an Email, this will return a seed phrase\n * If the user is authenticated with a Passkey, this will return a private key\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * // the params passed to this are different based on the specific signer\n * const result = signer.exportWallet()\n * ```\n *\n * @param {unknown} params export wallet parameters\n * @returns {boolean} true if the wallet was exported successfully\n */\n exportWallet: (\n params: Parameters<(typeof this.inner)[\"exportWallet\"]>[0]\n ) => Promise<boolean> = async (params) => {\n return this.inner.exportWallet(params);\n };\n\n /**\n * This method lets you adapt your AlchemySigner to a viem LocalAccount, which\n * will let you use the signer as an EOA directly.\n *\n * @example\n * ```ts\n * import { AlchemyWebSigner } from \"@account-kit/signer\";\n *\n * const signer = new AlchemyWebSigner({\n * client: {\n * connection: {\n * rpcUrl: \"/api/rpc\",\n * },\n * iframeConfig: {\n * iframeContainerId: \"alchemy-signer-iframe-container\",\n * },\n * },\n * });\n *\n * const account = signer.toViemAccount();\n * ```\n *\n * @throws if your signer is not authenticated\n * @returns {LocalAccount} a LocalAccount object that can be used with viem's wallet client\n */\n toViemAccount = (): LocalAccount => {\n // if we want this method to be synchronous, then we need to do this check here\n // otherwise we can use the sessionManager to get the user\n if (!this.inner.getUser()) {\n throw new NotAuthenticatedError();\n }\n\n return toAccount({\n address: this.inner.getUser()!.address,\n signMessage: (msg) => this.signMessage(msg.message),\n signTypedData: <\n const typedData extends TypedData | Record<string, unknown>,\n primaryType extends keyof typedData | \"EIP712Domain\" = keyof typedData\n >(\n typedDataDefinition: TypedDataDefinition<typedData, primaryType>\n ) => this.signTypedData<typedData, primaryType>(typedDataDefinition),\n signTransaction: this.signTransaction,\n });\n };\n\n private authenticateWithEmail = async (\n params: Extract<AuthParams, { type: \"email\" }>\n ): Promise<User> => {\n if (\"email\" in params) {\n const existingUser = await this.getUser(params.email);\n const expirationSeconds = Math.floor(\n this.sessionManager.expirationTimeMs / 1000\n );\n\n const { orgId } = existingUser\n ? await this.inner.initEmailAuth({\n email: params.email,\n expirationSeconds,\n redirectParams: params.redirectParams,\n })\n : await this.inner.createAccount({\n type: \"email\",\n email: params.email,\n expirationSeconds,\n redirectParams: params.redirectParams,\n });\n\n this.sessionManager.setTemporarySession({ orgId });\n this.store.setState({\n status: AlchemySignerStatus.AWAITING_EMAIL_AUTH,\n error: null,\n });\n\n // We wait for the session manager to emit a connected event if\n // cross tab sessions are permitted\n return new Promise<User>((resolve) => {\n const removeListener = this.sessionManager.on(\n \"connected\",\n (session) => {\n resolve(session.user);\n removeListener();\n }\n );\n });\n } else {\n const temporarySession = params.orgId\n ? { orgId: params.orgId }\n : this.sessionManager.getTemporarySession();\n\n if (!temporarySession) {\n this.store.setState({ status: AlchemySignerStatus.DISCONNECTED });\n throw new Error(\"Could not find email auth init session!\");\n }\n\n const user = await this.inner.completeAuthWithBundle({\n bundle: params.bundle,\n orgId: temporarySession.orgId,\n connectedEventName: \"connectedEmail\",\n authenticatingType: \"email\",\n });\n\n return user;\n }\n };\n\n private authenticateWithPasskey = async (\n args: Extract<AuthParams, { type: \"passkey\" }>\n ): Promise<User> => {\n let user: User;\n const shouldCreateNew = async () => {\n if (\"email\" in args) {\n const existingUser = await this.getUser(args.email);\n return existingUser == null;\n }\n\n return args.createNew;\n };\n\n if (await shouldCreateNew()) {\n const result = await this.inner.createAccount(\n args as Extract<\n AuthParams,\n { type: \"passkey\" } & ({ email: string } | { createNew: true })\n >\n );\n // account creation for passkeys returns the whoami response so we don't have to\n // call it again after signup\n user = {\n address: result.address!,\n userId: result.userId!,\n orgId: result.orgId,\n };\n } else {\n user = await this.inner.lookupUserWithPasskey();\n if (!user) {\n this.store.setState({ status: AlchemySignerStatus.DISCONNECTED });\n throw new Error(\"No user found\");\n }\n }\n\n return user;\n };\n\n private authenticateWithOauth = async (\n args: Extract<AuthParams, { type: \"oauth\" }>\n ): Promise<User> => {\n const params: OauthParams = {\n ...args,\n expirationSeconds: Math.floor(\n this.sessionManager.expirationTimeMs / 1000\n ),\n };\n if (params.mode === \"redirect\") {\n return this.inner.oauthWithRedirect(params);\n } else {\n return this.inner.oauthWithPopup(params);\n }\n };\n\n private handleOauthReturn = ({\n bundle,\n orgId,\n idToken,\n }: Extract<AuthParams, { type: \"oauthReturn\" }>): Promise<User> =>\n this.inner.completeAuthWithBundle({\n bundle,\n orgId,\n connectedEventName: \"connectedOauth\",\n authenticatingType: \"oauth\",\n idToken,\n });\n\n private registerListeners = () => {\n this.sessionManager.on(\"connected\", (session) => {\n this.store.setState({\n user: session.user,\n status: AlchemySignerStatus.CONNECTED,\n error: null,\n });\n });\n\n this.sessionManager.on(\"disconnected\", () => {\n this.store.setState({\n user: null,\n status: AlchemySignerStatus.DISCONNECTED,\n });\n });\n\n this.sessionManager.on(\"initialized\", () => {\n this.store.setState((state) => ({\n status: state.user\n ? AlchemySignerStatus.CONNECTED\n : AlchemySignerStatus.DISCONNECTED,\n ...(state.user ? { error: null } : undefined),\n }));\n });\n\n this.inner.on(\"authenticating\", ({ type }) => {\n const status = (() => {\n switch (type) {\n case \"email\":\n return AlchemySignerStatus.AUTHENTICATING_EMAIL;\n case \"passkey\":\n return AlchemySignerStatus.AUTHENTICATING_PASSKEY;\n case \"oauth\":\n return AlchemySignerStatus.AUTHENTICATING_OAUTH;\n default:\n assertNever(type, \"unhandled authenticating type\");\n }\n })();\n\n this.store.setState({\n status,\n error: null,\n });\n });\n };\n}\n\nfunction toErrorInfo(error: unknown): ErrorInfo {\n return error instanceof Error\n ? { name: error.name, message: error.message }\n : { name: \"Error\", message: \"Unknown error\" };\n}\n"]}