thirdweb 5.29.3-nightly-ffaff744f72d2cb22896e7501677b48f76ea857a-20240614184424 → 5.29.3-nightly-f8e0c5f84bb3a07a2911b5fce51da3983bb2e9e9-20240614190203

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 (150) hide show
  1. package/dist/cjs/exports/{react-native.js → react.native.js} +1 -1
  2. package/dist/cjs/exports/{react-native.js.map → react.native.js.map} +1 -1
  3. package/dist/cjs/exports/wallets/embedded.js +4 -4
  4. package/dist/cjs/exports/wallets/embedded.js.map +1 -1
  5. package/dist/cjs/exports/wallets/in-app.js +4 -3
  6. package/dist/cjs/exports/wallets/in-app.js.map +1 -1
  7. package/dist/cjs/exports/wallets/in-app.native.js +17 -0
  8. package/dist/cjs/exports/wallets/in-app.native.js.map +1 -0
  9. package/dist/cjs/exports/wallets.js +10 -7
  10. package/dist/cjs/exports/wallets.js.map +1 -1
  11. package/dist/cjs/exports/{react-native/wallets.js → wallets.native.js} +13 -11
  12. package/dist/cjs/exports/wallets.native.js.map +1 -0
  13. package/dist/cjs/react/web/ui/ConnectWallet/Details.js +1 -1
  14. package/dist/cjs/react/web/ui/ConnectWallet/Details.js.map +1 -1
  15. package/dist/cjs/react/web/wallets/in-app/InAppWalletOTPLoginUI.js +1 -1
  16. package/dist/cjs/react/web/wallets/in-app/InAppWalletOTPLoginUI.js.map +1 -1
  17. package/dist/cjs/version.js +1 -1
  18. package/dist/cjs/wallets/create-wallet.js +3 -128
  19. package/dist/cjs/wallets/create-wallet.js.map +1 -1
  20. package/dist/cjs/wallets/in-app/core/authentication/type.js +6 -1
  21. package/dist/cjs/wallets/in-app/core/authentication/type.js.map +1 -1
  22. package/dist/cjs/wallets/in-app/core/wallet/in-app-core.js +100 -0
  23. package/dist/cjs/wallets/in-app/core/wallet/in-app-core.js.map +1 -0
  24. package/dist/cjs/wallets/in-app/core/wallet/index.js +14 -6
  25. package/dist/cjs/wallets/in-app/core/wallet/index.js.map +1 -1
  26. package/dist/cjs/wallets/in-app/{core/authentication → native/auth}/index.js +15 -42
  27. package/dist/cjs/wallets/in-app/native/auth/index.js.map +1 -0
  28. package/dist/cjs/wallets/in-app/native/{auth.js → auth/native-auth.js} +9 -9
  29. package/dist/cjs/wallets/in-app/native/auth/native-auth.js.map +1 -0
  30. package/dist/cjs/wallets/in-app/native/in-app.js +69 -0
  31. package/dist/cjs/wallets/in-app/native/in-app.js.map +1 -0
  32. package/dist/cjs/wallets/in-app/native/native-connector.js +9 -10
  33. package/dist/cjs/wallets/in-app/native/native-connector.js.map +1 -1
  34. package/dist/cjs/wallets/in-app/web/in-app.js +69 -0
  35. package/dist/cjs/wallets/in-app/web/in-app.js.map +1 -0
  36. package/dist/cjs/wallets/in-app/web/lib/auth/iframe-auth.js +219 -0
  37. package/dist/cjs/wallets/in-app/web/lib/auth/iframe-auth.js.map +1 -0
  38. package/dist/cjs/wallets/in-app/web/lib/auth/index.js +121 -208
  39. package/dist/cjs/wallets/in-app/web/lib/auth/index.js.map +1 -1
  40. package/dist/cjs/wallets/in-app/web/lib/web-connector.js +3 -4
  41. package/dist/cjs/wallets/in-app/web/lib/web-connector.js.map +1 -1
  42. package/dist/esm/exports/{react-native.js → react.native.js} +1 -1
  43. package/dist/esm/exports/{react-native.js.map → react.native.js.map} +1 -1
  44. package/dist/esm/exports/wallets/embedded.js +3 -2
  45. package/dist/esm/exports/wallets/embedded.js.map +1 -1
  46. package/dist/esm/exports/wallets/in-app.js +4 -2
  47. package/dist/esm/exports/wallets/in-app.js.map +1 -1
  48. package/dist/esm/exports/wallets/in-app.native.js +9 -0
  49. package/dist/esm/exports/wallets/in-app.native.js.map +1 -0
  50. package/dist/esm/exports/wallets.js +7 -4
  51. package/dist/esm/exports/wallets.js.map +1 -1
  52. package/dist/esm/exports/wallets.native.js +23 -0
  53. package/dist/esm/exports/wallets.native.js.map +1 -0
  54. package/dist/esm/react/web/ui/ConnectWallet/Details.js +1 -1
  55. package/dist/esm/react/web/ui/ConnectWallet/Details.js.map +1 -1
  56. package/dist/esm/react/web/wallets/in-app/InAppWalletOTPLoginUI.js +1 -1
  57. package/dist/esm/react/web/wallets/in-app/InAppWalletOTPLoginUI.js.map +1 -1
  58. package/dist/esm/version.js +1 -1
  59. package/dist/esm/wallets/create-wallet.js +1 -125
  60. package/dist/esm/wallets/create-wallet.js.map +1 -1
  61. package/dist/esm/wallets/in-app/core/authentication/type.js +5 -0
  62. package/dist/esm/wallets/in-app/core/authentication/type.js.map +1 -1
  63. package/dist/esm/wallets/in-app/core/wallet/in-app-core.js +95 -0
  64. package/dist/esm/wallets/in-app/core/wallet/in-app-core.js.map +1 -0
  65. package/dist/esm/wallets/in-app/core/wallet/index.js +14 -6
  66. package/dist/esm/wallets/in-app/core/wallet/index.js.map +1 -1
  67. package/dist/esm/wallets/in-app/{core/authentication → native/auth}/index.js +14 -40
  68. package/dist/esm/wallets/in-app/native/auth/index.js.map +1 -0
  69. package/dist/esm/wallets/in-app/native/{auth.js → auth/native-auth.js} +9 -9
  70. package/dist/esm/wallets/in-app/native/auth/native-auth.js.map +1 -0
  71. package/dist/esm/wallets/in-app/native/in-app.js +65 -0
  72. package/dist/esm/wallets/in-app/native/in-app.js.map +1 -0
  73. package/dist/esm/wallets/in-app/native/native-connector.js +2 -3
  74. package/dist/esm/wallets/in-app/native/native-connector.js.map +1 -1
  75. package/dist/esm/wallets/in-app/web/in-app.js +65 -0
  76. package/dist/esm/wallets/in-app/web/in-app.js.map +1 -0
  77. package/dist/esm/wallets/in-app/web/lib/auth/iframe-auth.js +215 -0
  78. package/dist/esm/wallets/in-app/web/lib/auth/iframe-auth.js.map +1 -0
  79. package/dist/esm/wallets/in-app/web/lib/auth/index.js +115 -206
  80. package/dist/esm/wallets/in-app/web/lib/auth/index.js.map +1 -1
  81. package/dist/esm/wallets/in-app/web/lib/web-connector.js +2 -3
  82. package/dist/esm/wallets/in-app/web/lib/web-connector.js.map +1 -1
  83. package/dist/types/exports/{react-native.d.ts → react.native.d.ts} +1 -1
  84. package/dist/types/exports/{react-native.d.ts.map → react.native.d.ts.map} +1 -1
  85. package/dist/types/exports/wallets/embedded.d.ts +3 -2
  86. package/dist/types/exports/wallets/embedded.d.ts.map +1 -1
  87. package/dist/types/exports/wallets/in-app.d.ts +3 -2
  88. package/dist/types/exports/wallets/in-app.d.ts.map +1 -1
  89. package/dist/types/exports/wallets/in-app.native.d.ts +5 -0
  90. package/dist/types/exports/wallets/in-app.native.d.ts.map +1 -0
  91. package/dist/types/exports/wallets.d.ts +4 -3
  92. package/dist/types/exports/wallets.d.ts.map +1 -1
  93. package/dist/types/exports/{react-native/wallets.d.ts → wallets.native.d.ts} +20 -19
  94. package/dist/types/exports/wallets.native.d.ts.map +1 -0
  95. package/dist/types/version.d.ts +1 -1
  96. package/dist/types/wallets/create-wallet.d.ts +0 -53
  97. package/dist/types/wallets/create-wallet.d.ts.map +1 -1
  98. package/dist/types/wallets/in-app/core/authentication/type.d.ts +4 -0
  99. package/dist/types/wallets/in-app/core/authentication/type.d.ts.map +1 -1
  100. package/dist/types/wallets/in-app/core/wallet/in-app-core.d.ts +16 -0
  101. package/dist/types/wallets/in-app/core/wallet/in-app-core.d.ts.map +1 -0
  102. package/dist/types/wallets/in-app/core/wallet/index.d.ts +3 -2
  103. package/dist/types/wallets/in-app/core/wallet/index.d.ts.map +1 -1
  104. package/dist/types/wallets/in-app/{core/authentication → native/auth}/index.d.ts +3 -12
  105. package/dist/types/wallets/in-app/native/auth/index.d.ts.map +1 -0
  106. package/dist/types/wallets/in-app/native/{auth.d.ts → auth/native-auth.d.ts} +3 -3
  107. package/dist/types/wallets/in-app/native/auth/native-auth.d.ts.map +1 -0
  108. package/dist/types/wallets/in-app/native/in-app.d.ts +56 -0
  109. package/dist/types/wallets/in-app/native/in-app.d.ts.map +1 -0
  110. package/dist/types/wallets/in-app/native/native-connector.d.ts.map +1 -1
  111. package/dist/types/wallets/in-app/web/in-app.d.ts +56 -0
  112. package/dist/types/wallets/in-app/web/in-app.d.ts.map +1 -0
  113. package/dist/types/wallets/in-app/web/lib/auth/iframe-auth.d.ts +141 -0
  114. package/dist/types/wallets/in-app/web/lib/auth/iframe-auth.d.ts.map +1 -0
  115. package/dist/types/wallets/in-app/web/lib/auth/index.d.ts +78 -137
  116. package/dist/types/wallets/in-app/web/lib/auth/index.d.ts.map +1 -1
  117. package/dist/types/wallets/in-app/web/lib/web-connector.d.ts +1 -1
  118. package/dist/types/wallets/in-app/web/lib/web-connector.d.ts.map +1 -1
  119. package/package.json +9 -3
  120. package/src/exports/wallets/embedded.ts +4 -3
  121. package/src/exports/wallets/in-app.native.ts +18 -0
  122. package/src/exports/wallets/in-app.ts +6 -3
  123. package/src/exports/{react-native/wallets.ts → wallets.native.ts} +21 -19
  124. package/src/exports/wallets.ts +11 -5
  125. package/src/react/web/ui/ConnectWallet/Details.tsx +1 -1
  126. package/src/react/web/wallets/in-app/InAppWalletOTPLoginUI.tsx +1 -1
  127. package/src/version.ts +1 -1
  128. package/src/wallets/create-wallet.ts +1 -146
  129. package/src/wallets/in-app/core/authentication/type.ts +13 -0
  130. package/src/wallets/in-app/core/wallet/in-app-core.ts +134 -0
  131. package/src/wallets/in-app/core/wallet/index.ts +16 -5
  132. package/src/wallets/in-app/{core/authentication → native/auth}/index.ts +15 -57
  133. package/src/wallets/in-app/native/{auth.ts → auth/native-auth.ts} +10 -10
  134. package/src/wallets/in-app/native/in-app.ts +70 -0
  135. package/src/wallets/in-app/native/native-connector.ts +2 -2
  136. package/src/wallets/in-app/web/in-app.ts +70 -0
  137. package/src/wallets/in-app/web/lib/auth/iframe-auth.ts +266 -0
  138. package/src/wallets/in-app/web/lib/auth/index.ts +124 -249
  139. package/src/wallets/in-app/web/lib/web-connector.ts +2 -2
  140. package/dist/cjs/exports/react-native/wallets.js.map +0 -1
  141. package/dist/cjs/wallets/in-app/core/authentication/index.js.map +0 -1
  142. package/dist/cjs/wallets/in-app/native/auth.js.map +0 -1
  143. package/dist/esm/exports/react-native/wallets.js +0 -23
  144. package/dist/esm/exports/react-native/wallets.js.map +0 -1
  145. package/dist/esm/wallets/in-app/core/authentication/index.js.map +0 -1
  146. package/dist/esm/wallets/in-app/native/auth.js.map +0 -1
  147. package/dist/types/exports/react-native/wallets.d.ts.map +0 -1
  148. package/dist/types/wallets/in-app/core/authentication/index.d.ts.map +0 -1
  149. package/dist/types/wallets/in-app/native/auth.d.ts.map +0 -1
  150. /package/src/exports/{react-native.ts → react.native.ts} +0 -0
@@ -1,59 +1,26 @@
1
1
  import type { ThirdwebClient } from "../../../../client/client.js";
2
- import { isBrowser, isReactNative } from "../../../../utils/platform.js";
3
- import type { InAppConnector } from "../interfaces/connector.js";
4
2
  import {
5
3
  type AuthArgsType,
6
4
  type AuthLoginReturnType,
7
- AuthProvider,
5
+ type GetAuthenticatedUserParams,
8
6
  type PreAuthArgsType,
9
7
  UserWalletStatus,
10
- } from "./type.js";
8
+ } from "../../core/authentication/type.js";
9
+ import { getOrCreateInAppWalletConnector } from "../../core/wallet/in-app-core.js";
11
10
 
12
- export type GetAuthenticatedUserParams = {
13
- client: ThirdwebClient;
14
- };
15
-
16
- const ewsSDKCache = new WeakMap<ThirdwebClient, InAppConnector>();
11
+ // ---- KEEP IN SYNC WITH /wallets/in-app/web/lib/auth/index.ts ---- //
12
+ // duplication needed for separate exports between web and native
17
13
 
18
14
  /**
19
15
  * @internal
20
16
  */
21
17
  async function getInAppWalletConnector(client: ThirdwebClient) {
22
- if (ewsSDKCache.has(client)) {
23
- return ewsSDKCache.get(client) as InAppConnector;
24
- }
25
-
26
- let ewSDK: InAppConnector;
27
- if (isBrowser()) {
28
- const { InAppWebConnector } = await import(
29
- "../../web/lib/web-connector.js"
30
- );
31
- ewSDK = new InAppWebConnector({
18
+ return getOrCreateInAppWalletConnector(client, async (client) => {
19
+ const { InAppNativeConnector } = await import("../native-connector.js");
20
+ return new InAppNativeConnector({
32
21
  client: client,
33
22
  });
34
- } else if (isReactNative()) {
35
- const {
36
- InAppNativeConnector,
37
- } = require("../../native/native-connector.js");
38
- ewSDK = new InAppNativeConnector({
39
- client,
40
- });
41
- } else {
42
- throw new Error("Unsupported platform");
43
- }
44
-
45
- ewsSDKCache.set(client, ewSDK);
46
- return ewSDK;
47
- }
48
-
49
- /**
50
- * @internal
51
- */
52
- export async function logoutAuthenticatedUser(
53
- options: GetAuthenticatedUserParams,
54
- ) {
55
- const ewSDK = await getInAppWalletConnector(options.client);
56
- return ewSDK.logout();
23
+ });
57
24
  }
58
25
 
59
26
  /**
@@ -75,8 +42,8 @@ export async function getAuthenticatedUser(
75
42
  options: GetAuthenticatedUserParams,
76
43
  ) {
77
44
  const { client } = options;
78
- const ewSDK = await getInAppWalletConnector(client);
79
- const user = await ewSDK.getUser();
45
+ const connector = await getInAppWalletConnector(client);
46
+ const user = await connector.getUser();
80
47
  switch (user.status) {
81
48
  case UserWalletStatus.LOGGED_IN_WALLET_INITIALIZED: {
82
49
  return user;
@@ -145,8 +112,8 @@ export async function getUserPhoneNumber(options: GetAuthenticatedUserParams) {
145
112
  * @wallet
146
113
  */
147
114
  export async function preAuthenticate(args: PreAuthArgsType) {
148
- const ewSDK = await getInAppWalletConnector(args.client);
149
- return ewSDK.preAuthenticate(args);
115
+ const connector = await getInAppWalletConnector(args.client);
116
+ return connector.preAuthenticate(args);
150
117
  }
151
118
 
152
119
  /**
@@ -169,15 +136,6 @@ export async function preAuthenticate(args: PreAuthArgsType) {
169
136
  export async function authenticate(
170
137
  args: AuthArgsType,
171
138
  ): Promise<AuthLoginReturnType> {
172
- const ewSDK = await getInAppWalletConnector(args.client);
173
- return ewSDK.authenticate(args);
139
+ const connector = await getInAppWalletConnector(args.client);
140
+ return connector.authenticate(args);
174
141
  }
175
-
176
- export const oauthStrategyToAuthProvider: Record<
177
- "google" | "facebook" | "apple",
178
- AuthProvider
179
- > = {
180
- google: AuthProvider.GOOGLE,
181
- facebook: AuthProvider.FACEBOOK,
182
- apple: AuthProvider.APPLE,
183
- };
@@ -2,42 +2,42 @@ import type { CognitoUser } from "amazon-cognito-identity-js";
2
2
 
3
3
  import { Auth } from "aws-amplify";
4
4
  import * as WebBrowser from "expo-web-browser";
5
- import type { ThirdwebClient } from "../../../client/client.js";
5
+ import type { ThirdwebClient } from "../../../../client/client.js";
6
6
  import {
7
7
  AuthProvider,
8
8
  type AuthStoredTokenWithCookieReturnType,
9
9
  type OauthOption,
10
10
  RecoveryShareManagement,
11
11
  type SendEmailOtpReturnType,
12
- } from "../core/authentication/type.js";
12
+ } from "../../core/authentication/type.js";
13
13
  import {
14
14
  deleteAccount,
15
15
  fetchUserDetails,
16
16
  generateAuthTokenFromCognitoEmailOtp,
17
17
  getSessionHeaders,
18
18
  verifyClientId,
19
- } from "./helpers/api/fetchers.js";
19
+ } from "../helpers/api/fetchers.js";
20
20
  import {
21
21
  cognitoEmailSignIn,
22
22
  cognitoEmailSignUp,
23
23
  cognitoPhoneSignIn,
24
24
  cognitoPhoneSignUp,
25
- } from "./helpers/auth/cognitoAuth.js";
25
+ } from "../helpers/auth/cognitoAuth.js";
26
26
  import {
27
27
  postAuth,
28
28
  postAuthUserManaged,
29
29
  preAuth,
30
- } from "./helpers/auth/middleware.js";
30
+ } from "../helpers/auth/middleware.js";
31
31
  import {
32
32
  DOMAIN_URL_2023,
33
33
  ROUTE_AUTH_ENDPOINT_CALLBACK,
34
34
  ROUTE_AUTH_JWT_CALLBACK,
35
35
  ROUTE_HEADLESS_OAUTH_LOGIN,
36
- } from "./helpers/constants.js";
37
- import { createErrorMessage } from "./helpers/errors.js";
38
- import { isDeviceSharePresentForUser } from "./helpers/storage/local.js";
39
- import { getCognitoUser, setCognitoUser } from "./helpers/storage/state.js";
40
- import type { VerifiedTokenResponse } from "./helpers/types.js";
36
+ } from "../helpers/constants.js";
37
+ import { createErrorMessage } from "../helpers/errors.js";
38
+ import { isDeviceSharePresentForUser } from "../helpers/storage/local.js";
39
+ import { getCognitoUser, setCognitoUser } from "../helpers/storage/state.js";
40
+ import type { VerifiedTokenResponse } from "../helpers/types.js";
41
41
 
42
42
  export async function sendVerificationEmail(options: {
43
43
  email: string;
@@ -0,0 +1,70 @@
1
+ import type { ThirdwebClient } from "../../../client/client.js";
2
+ import type { Wallet } from "../../interfaces/wallet.js";
3
+ import type { CreateWalletArgs } from "../../wallet-types.js";
4
+ import { createInAppWallet } from "../core/wallet/in-app-core.js";
5
+
6
+ /**
7
+ * Creates an in-app wallet.
8
+ * @param createOptions - configuration options
9
+ * @returns The created in-app wallet.
10
+ * @example
11
+ * ```ts
12
+ * import { inAppWallet } from "thirdweb/wallets";
13
+ *
14
+ * const wallet = inAppWallet();
15
+ *
16
+ * const account = await wallet.connect({
17
+ * client,
18
+ * chain,
19
+ * strategy: "google",
20
+ * });
21
+ * ```
22
+ *
23
+ * Enable smart accounts and sponsor gas for your users:
24
+ * ```ts
25
+ * import { inAppWallet } from "thirdweb/wallets";
26
+ * const wallet = inAppWallet({
27
+ * smartAccount: {
28
+ * chain: sepolia,
29
+ * sponsorGas: true,
30
+ * },
31
+ * });
32
+ * ```
33
+ *
34
+ * Specify a logo for your login page
35
+ * ```ts
36
+ * import { inAppWallet } from "thirdweb/wallets";
37
+ * const wallet = inAppWallet({
38
+ * metadata: {
39
+ * image: {
40
+ * src: "https://example.com/logo.png",
41
+ * alt: "My logo",
42
+ * width: 100,
43
+ * height: 100,
44
+ * },
45
+ * },
46
+ * });
47
+ * ```
48
+ *
49
+ * Hide the ability to export the private key within the Connect Modal
50
+ * ```ts
51
+ * import { inAppWallet } from "thirdweb/wallets";
52
+ * const wallet = inAppWallet({
53
+ * hidePrivateKeyExport: true
54
+ * });
55
+ * ```
56
+ * @wallet
57
+ */
58
+ export function inAppWallet(
59
+ createOptions?: CreateWalletArgs<"inApp">[1],
60
+ ): Wallet<"inApp"> {
61
+ return createInAppWallet({
62
+ createOptions,
63
+ connectorFactory: async (client: ThirdwebClient) => {
64
+ const { InAppNativeConnector } = await import("./native-connector.js");
65
+ return new InAppNativeConnector({
66
+ client,
67
+ });
68
+ },
69
+ });
70
+ }
@@ -1,6 +1,5 @@
1
1
  import type { ThirdwebClient } from "../../../client/client.js";
2
2
  import type { Account } from "../../interfaces/wallet.js";
3
- import { oauthStrategyToAuthProvider } from "../core/authentication/index.js";
4
3
  import {
5
4
  type AuthArgsType,
6
5
  type AuthLoginReturnType,
@@ -10,6 +9,7 @@ import {
10
9
  type PreAuthArgsType,
11
10
  type SendEmailOtpReturnType,
12
11
  UserWalletStatus,
12
+ oauthStrategyToAuthProvider,
13
13
  } from "../core/authentication/type.js";
14
14
  import type { InAppConnector } from "../core/interfaces/connector.js";
15
15
  import {
@@ -20,7 +20,7 @@ import {
20
20
  sendVerificationSms,
21
21
  socialLogin,
22
22
  validateEmailOTP,
23
- } from "./auth.js";
23
+ } from "./auth/native-auth.js";
24
24
  import { fetchUserDetails } from "./helpers/api/fetchers.js";
25
25
  import { logoutUser } from "./helpers/auth/logout.js";
26
26
  import { getWalletUserDetails } from "./helpers/storage/local.js";
@@ -0,0 +1,70 @@
1
+ import type { ThirdwebClient } from "../../../client/client.js";
2
+ import type { Wallet } from "../../interfaces/wallet.js";
3
+ import type { CreateWalletArgs } from "../../wallet-types.js";
4
+ import { createInAppWallet } from "../core/wallet/in-app-core.js";
5
+
6
+ /**
7
+ * Creates an in-app wallet.
8
+ * @param createOptions - configuration options
9
+ * @returns The created in-app wallet.
10
+ * @example
11
+ * ```ts
12
+ * import { inAppWallet } from "thirdweb/wallets";
13
+ *
14
+ * const wallet = inAppWallet();
15
+ *
16
+ * const account = await wallet.connect({
17
+ * client,
18
+ * chain,
19
+ * strategy: "google",
20
+ * });
21
+ * ```
22
+ *
23
+ * Enable smart accounts and sponsor gas for your users:
24
+ * ```ts
25
+ * import { inAppWallet } from "thirdweb/wallets";
26
+ * const wallet = inAppWallet({
27
+ * smartAccount: {
28
+ * chain: sepolia,
29
+ * sponsorGas: true,
30
+ * },
31
+ * });
32
+ * ```
33
+ *
34
+ * Specify a logo for your login page
35
+ * ```ts
36
+ * import { inAppWallet } from "thirdweb/wallets";
37
+ * const wallet = inAppWallet({
38
+ * metadata: {
39
+ * image: {
40
+ * src: "https://example.com/logo.png",
41
+ * alt: "My logo",
42
+ * width: 100,
43
+ * height: 100,
44
+ * },
45
+ * },
46
+ * });
47
+ * ```
48
+ *
49
+ * Hide the ability to export the private key within the Connect Modal
50
+ * ```ts
51
+ * import { inAppWallet } from "thirdweb/wallets";
52
+ * const wallet = inAppWallet({
53
+ * hidePrivateKeyExport: true
54
+ * });
55
+ * ```
56
+ * @wallet
57
+ */
58
+ export function inAppWallet(
59
+ createOptions?: CreateWalletArgs<"inApp">[1],
60
+ ): Wallet<"inApp"> {
61
+ return createInAppWallet({
62
+ createOptions,
63
+ connectorFactory: async (client: ThirdwebClient) => {
64
+ const { InAppWebConnector } = await import("./lib/web-connector.js");
65
+ return new InAppWebConnector({
66
+ client,
67
+ });
68
+ },
69
+ });
70
+ }
@@ -0,0 +1,266 @@
1
+ import type { ThirdwebClient } from "../../../../../client/client.js";
2
+ import type {
3
+ AuthAndWalletRpcReturnType,
4
+ AuthLoginReturnType,
5
+ AuthStoredTokenWithCookieReturnType,
6
+ LogoutReturnType,
7
+ SendEmailOtpReturnType,
8
+ } from "../../../core/authentication/type.js";
9
+ import type { ClientIdWithQuerierType } from "../../types.js";
10
+ import { LocalStorage } from "../../utils/Storage/LocalStorage.js";
11
+ import type { InAppWalletIframeCommunicator } from "../../utils/iFrameCommunication/InAppWalletIframeCommunicator.js";
12
+ import { BaseLogin } from "./base-login.js";
13
+
14
+ export type AuthQuerierTypes = {
15
+ logout: undefined;
16
+ initIframe: {
17
+ clientId: string;
18
+ authCookie: string;
19
+ walletUserId: string;
20
+ deviceShareStored: string;
21
+ };
22
+ loginWithStoredTokenDetails: {
23
+ storedToken: AuthStoredTokenWithCookieReturnType["storedToken"];
24
+ recoveryCode?: string;
25
+ };
26
+ };
27
+
28
+ /**
29
+ *
30
+ */
31
+ export class Auth {
32
+ protected client: ThirdwebClient;
33
+ protected AuthQuerier: InAppWalletIframeCommunicator<AuthQuerierTypes>;
34
+ protected localStorage: LocalStorage;
35
+ protected onAuthSuccess: (
36
+ authResults: AuthAndWalletRpcReturnType,
37
+ ) => Promise<AuthLoginReturnType>;
38
+ private BaseLogin: BaseLogin;
39
+
40
+ /**
41
+ * Used to manage the user's auth states. This should not be instantiated directly.
42
+ * Call {@link InAppWalletSdk.auth} instead.
43
+ * @internal
44
+ */
45
+ constructor({
46
+ client,
47
+ querier,
48
+ onAuthSuccess,
49
+ baseUrl,
50
+ }: ClientIdWithQuerierType & {
51
+ baseUrl: string;
52
+ onAuthSuccess: (
53
+ authDetails: AuthAndWalletRpcReturnType,
54
+ ) => Promise<AuthLoginReturnType>;
55
+ }) {
56
+ this.client = client;
57
+
58
+ this.AuthQuerier = querier;
59
+ this.localStorage = new LocalStorage({ clientId: client.clientId });
60
+ this.onAuthSuccess = onAuthSuccess;
61
+ this.BaseLogin = new BaseLogin({
62
+ postLogin: async (result) => {
63
+ return this.postLogin(result);
64
+ },
65
+ preLogin: async () => {
66
+ await this.preLogin();
67
+ },
68
+ querier: querier,
69
+ client,
70
+ baseUrl,
71
+ });
72
+ }
73
+
74
+ private async preLogin() {
75
+ await this.logout();
76
+ }
77
+
78
+ private async postLogin({
79
+ storedToken,
80
+ walletDetails,
81
+ }: AuthAndWalletRpcReturnType): Promise<AuthLoginReturnType> {
82
+ if (storedToken.shouldStoreCookieString) {
83
+ await this.localStorage.saveAuthCookie(storedToken.cookieString);
84
+ }
85
+ const initializedUser = await this.onAuthSuccess({
86
+ storedToken,
87
+ walletDetails,
88
+ });
89
+ return initializedUser;
90
+ }
91
+
92
+ async loginWithAuthToken(
93
+ authToken: AuthStoredTokenWithCookieReturnType,
94
+ recoveryCode?: string,
95
+ ): Promise<AuthLoginReturnType> {
96
+ await this.preLogin();
97
+ const result = await this.AuthQuerier.call<AuthAndWalletRpcReturnType>({
98
+ procedureName: "loginWithStoredTokenDetails",
99
+ params: {
100
+ storedToken: authToken.storedToken,
101
+ recoveryCode,
102
+ },
103
+ });
104
+ return this.postLogin(result);
105
+ }
106
+
107
+ /**
108
+ * Used to log the user into their thirdweb wallet on your platform via a myriad of auth providers
109
+ * @example
110
+ * ```typescript
111
+ * const thirdwebInAppWallet = new InAppWalletSdk({clientId: "YOUR_CLIENT_ID", chain: "Polygon"})
112
+ * try {
113
+ * const user = await thirdwebInAppWallet.auth.loginWithModal();
114
+ * // user is now logged in
115
+ * } catch (e) {
116
+ * // User closed modal or something else went wrong during the authentication process
117
+ * console.error(e)
118
+ * }
119
+ * ```
120
+ * @returns `{{user: InitializedUser}}` An InitializedUser object.
121
+ */
122
+ async loginWithModal(): Promise<AuthLoginReturnType> {
123
+ return this.BaseLogin.loginWithModal();
124
+ }
125
+
126
+ /**
127
+ * Used to log the user into their thirdweb wallet using email OTP
128
+ * @example
129
+ * ```typescript
130
+ * // Basic Flow
131
+ * const thirdwebInAppWallet = new InAppWalletSdk({clientId: "", chain: "Polygon"});
132
+ * try {
133
+ * // prompts user to enter the code they received
134
+ * const user = await thirdwebInAppWallet.auth.loginWithThirdwebEmailOtp({ email : "you@example.com" });
135
+ * // user is now logged in
136
+ * } catch (e) {
137
+ * // User closed the OTP modal or something else went wrong during the authentication process
138
+ * console.error(e)
139
+ * }
140
+ * ```
141
+ * @param args - args.email: We will send the email an OTP that needs to be entered in order for them to be logged in.
142
+ * @returns `{{user: InitializedUser}}` An InitializedUser object. See {@link InAppWalletSdk.getUser} for more
143
+ */
144
+ async loginWithEmailOtp(
145
+ args: Parameters<BaseLogin["loginWithEmailOtp"]>[0],
146
+ ): Promise<AuthLoginReturnType> {
147
+ return this.BaseLogin.loginWithEmailOtp(args);
148
+ }
149
+
150
+ /**
151
+ * @internal
152
+ */
153
+ async loginWithCustomJwt(
154
+ args: Parameters<BaseLogin["loginWithCustomJwt"]>[0],
155
+ ): Promise<AuthLoginReturnType> {
156
+ return this.BaseLogin.loginWithCustomJwt(args);
157
+ }
158
+
159
+ /**
160
+ * @internal
161
+ */
162
+ async loginWithCustomAuthEndpoint(
163
+ args: Parameters<BaseLogin["loginWithCustomAuthEndpoint"]>[0],
164
+ ): Promise<AuthLoginReturnType> {
165
+ return this.BaseLogin.loginWithCustomAuthEndpoint(args);
166
+ }
167
+
168
+ /**
169
+ * @internal
170
+ */
171
+ async loginWithOauth(
172
+ args: Parameters<BaseLogin["loginWithOauth"]>[0],
173
+ ): Promise<AuthLoginReturnType> {
174
+ return this.BaseLogin.loginWithOauth(args);
175
+ }
176
+
177
+ /**
178
+ * A headless way to send the users at the passed email an OTP code.
179
+ * You need to then call {@link Auth.verifyEmailLoginOtp} in order to complete the login process
180
+ * @example
181
+ * @param param0.email
182
+ * ```typescript
183
+ * const thirdwebInAppWallet = new InAppWalletSdk({clientId: "", chain: "Polygon"});
184
+ * // sends user an OTP code
185
+ * try {
186
+ * await thirdwebInAppWallet.auth.sendEmailLoginOtp({ email : "you@example.com" });
187
+ * } catch(e) {
188
+ * // Error Sending user's email an OTP code
189
+ * console.error(e);
190
+ * }
191
+ *
192
+ * // Then when your user is ready to verify their OTP
193
+ * try {
194
+ * const user = await thirdwebInAppWallet.auth.verifyEmailLoginOtp({ email: "you@example.com", otp: "6-DIGIT_CODE_HERE" });
195
+ * } catch(e) {
196
+ * // Error verifying the OTP code
197
+ * console.error(e)
198
+ * }
199
+ * ```
200
+ * @param param0 - param0.email We will send the email an OTP that needs to be entered in order for them to be logged in.
201
+ * @returns `{{ isNewUser: boolean }}` IsNewUser indicates if the user is a new user to your platform
202
+ * @internal
203
+ */
204
+ async sendEmailLoginOtp({
205
+ email,
206
+ }: Parameters<
207
+ BaseLogin["sendEmailLoginOtp"]
208
+ >[0]): Promise<SendEmailOtpReturnType> {
209
+ return this.BaseLogin.sendEmailLoginOtp({
210
+ email,
211
+ });
212
+ }
213
+
214
+ /**
215
+ * @internal
216
+ */
217
+ async sendSmsLoginOtp({
218
+ phoneNumber,
219
+ }: Parameters<
220
+ BaseLogin["sendSmsLoginOtp"]
221
+ >[0]): Promise<SendEmailOtpReturnType> {
222
+ return this.BaseLogin.sendSmsLoginOtp({
223
+ phoneNumber,
224
+ });
225
+ }
226
+
227
+ /**
228
+ * Used to verify the otp that the user receives from thirdweb
229
+ *
230
+ * See {@link Auth.sendEmailLoginOtp} for how the headless call flow looks like. Simply swap out the calls to `loginWithThirdwebEmailOtp` with `verifyThirdwebEmailLoginOtp`
231
+ * @param args - props.email We will send the email an OTP that needs to be entered in order for them to be logged in.
232
+ * props.otp The code that the user received in their email
233
+ * @returns `{{user: InitializedUser}}` An InitializedUser object containing the user's status, wallet, authDetails, and more
234
+ * @internal
235
+ */
236
+ async verifyEmailLoginOtp(
237
+ args: Parameters<BaseLogin["verifyEmailLoginOtp"]>[0],
238
+ ) {
239
+ return this.BaseLogin.verifyEmailLoginOtp(args);
240
+ }
241
+
242
+ /**
243
+ * @internal
244
+ */
245
+ async verifySmsLoginOtp(args: Parameters<BaseLogin["verifySmsLoginOtp"]>[0]) {
246
+ return this.BaseLogin.verifySmsLoginOtp(args);
247
+ }
248
+
249
+ /**
250
+ * Logs any existing user out of their wallet.
251
+ * @returns `{{success: boolean}}` true if a user is successfully logged out. false if there's no user currently logged in.
252
+ * @internal
253
+ */
254
+ async logout(): Promise<LogoutReturnType> {
255
+ const { success } = await this.AuthQuerier.call<LogoutReturnType>({
256
+ procedureName: "logout",
257
+ params: undefined,
258
+ });
259
+ const isRemoveAuthCookie = await this.localStorage.removeAuthCookie();
260
+ const isRemoveUserId = await this.localStorage.removeWalletUserId();
261
+
262
+ return {
263
+ success: success || isRemoveAuthCookie || isRemoveUserId,
264
+ };
265
+ }
266
+ }