@mezo-org/passport 0.4.0-dev.2 → 0.4.0-dev.20

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 (263) hide show
  1. package/dist/src/api/auth.d.ts +57 -52
  2. package/dist/src/api/auth.d.ts.map +1 -1
  3. package/dist/src/api/auth.js +22 -53
  4. package/dist/src/api/auth.js.map +1 -1
  5. package/dist/src/api/client.d.ts +24 -0
  6. package/dist/src/api/client.d.ts.map +1 -0
  7. package/dist/src/api/client.js +54 -0
  8. package/dist/src/api/client.js.map +1 -0
  9. package/dist/src/api/fetch-error.d.ts +5 -0
  10. package/dist/src/api/fetch-error.d.ts.map +1 -0
  11. package/dist/src/api/fetch-error.js +8 -0
  12. package/dist/src/api/fetch-error.js.map +1 -0
  13. package/dist/src/api/index.d.ts +3 -0
  14. package/dist/src/api/index.d.ts.map +1 -0
  15. package/dist/src/api/index.js +3 -0
  16. package/dist/src/api/index.js.map +1 -0
  17. package/dist/src/api/portal.d.ts +32 -0
  18. package/dist/src/api/portal.d.ts.map +1 -0
  19. package/dist/src/api/portal.js +23 -0
  20. package/dist/src/api/portal.js.map +1 -0
  21. package/dist/src/assets/DefaultAvatar.d.ts +5 -0
  22. package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
  23. package/dist/src/assets/DefaultAvatar.js +21 -0
  24. package/dist/src/assets/DefaultAvatar.js.map +1 -0
  25. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +8 -0
  26. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
  27. package/dist/src/components/Dropdown/ConnectedTrigger.js +39 -0
  28. package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
  29. package/dist/src/components/Dropdown/Content.d.ts +9 -0
  30. package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
  31. package/dist/src/components/Dropdown/Content.js +25 -0
  32. package/dist/src/components/Dropdown/Content.js.map +1 -0
  33. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
  34. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
  35. package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
  36. package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
  37. package/dist/src/components/Dropdown/Dropdown.d.ts +27 -0
  38. package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
  39. package/dist/src/components/Dropdown/Dropdown.js +64 -0
  40. package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
  41. package/dist/src/components/Dropdown/NestedViewLayout.d.ts +7 -0
  42. package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
  43. package/dist/src/components/Dropdown/NestedViewLayout.js +34 -0
  44. package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
  45. package/dist/src/components/Dropdown/Receive/Receive.d.ts +7 -0
  46. package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
  47. package/dist/src/components/Dropdown/Receive/Receive.js +53 -0
  48. package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
  49. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +7 -0
  50. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
  51. package/dist/src/components/Dropdown/Root/AccountAddressActions.js +48 -0
  52. package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
  53. package/dist/src/components/Dropdown/Root/AccountAssets.d.ts +13 -0
  54. package/dist/src/components/Dropdown/Root/AccountAssets.d.ts.map +1 -0
  55. package/dist/src/components/Dropdown/Root/AccountAssets.js +43 -0
  56. package/dist/src/components/Dropdown/Root/AccountAssets.js.map +1 -0
  57. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +7 -0
  58. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
  59. package/dist/src/components/Dropdown/Root/AccountBalance.js +16 -0
  60. package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
  61. package/dist/src/components/Dropdown/Root/Root.d.ts +8 -0
  62. package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
  63. package/dist/src/components/Dropdown/Root/Root.js +64 -0
  64. package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
  65. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +8 -0
  66. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
  67. package/dist/src/components/Dropdown/Root/WalletAddress.js +65 -0
  68. package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
  69. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +8 -0
  70. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
  71. package/dist/src/components/Dropdown/Root/WelcomeBlock.js +43 -0
  72. package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
  73. package/dist/src/components/Dropdown/index.d.ts +3 -0
  74. package/dist/src/components/Dropdown/index.d.ts.map +1 -0
  75. package/dist/src/components/Dropdown/index.js +2 -0
  76. package/dist/src/components/Dropdown/index.js.map +1 -0
  77. package/dist/src/components/index.d.ts +2 -0
  78. package/dist/src/components/index.d.ts.map +1 -0
  79. package/dist/src/components/index.js +2 -0
  80. package/dist/src/components/index.js.map +1 -0
  81. package/dist/src/hooks/constants.d.ts +3 -3
  82. package/dist/src/hooks/constants.js +4 -4
  83. package/dist/src/hooks/constants.js.map +1 -1
  84. package/dist/src/hooks/index.d.ts +2 -4
  85. package/dist/src/hooks/index.d.ts.map +1 -1
  86. package/dist/src/hooks/index.js +2 -4
  87. package/dist/src/hooks/index.js.map +1 -1
  88. package/dist/src/hooks/useAssetsUSDConversion.d.ts +8 -0
  89. package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +1 -0
  90. package/dist/src/hooks/useAssetsUSDConversion.js +21 -0
  91. package/dist/src/hooks/useAssetsUSDConversion.js.map +1 -0
  92. package/dist/src/hooks/useAuthApiClient.d.ts +1 -1
  93. package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
  94. package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
  95. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
  96. package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
  97. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
  98. package/dist/src/hooks/useCreateAccount.d.ts +41 -137
  99. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  100. package/dist/src/hooks/useCreateAccount.js +8 -10
  101. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  102. package/dist/src/hooks/useCreateSession.d.ts +12 -182
  103. package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
  104. package/dist/src/hooks/useCreateSession.js +12 -18
  105. package/dist/src/hooks/useCreateSession.js.map +1 -1
  106. package/dist/src/hooks/useDropdownData.d.ts +45 -0
  107. package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
  108. package/dist/src/hooks/useDropdownData.js +80 -0
  109. package/dist/src/hooks/useDropdownData.js.map +1 -0
  110. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
  111. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
  112. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +34 -0
  113. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
  114. package/dist/src/hooks/useGetAccountByAddress.d.ts +3 -5
  115. package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
  116. package/dist/src/hooks/useGetAccountByAddress.js +3 -4
  117. package/dist/src/hooks/useGetAccountByAddress.js.map +1 -1
  118. package/dist/src/hooks/useGetAccountByMezoId.d.ts +3 -5
  119. package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
  120. package/dist/src/hooks/useGetAccountByMezoId.js +3 -4
  121. package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
  122. package/dist/src/hooks/useGetCurrentAccount.d.ts +14 -8
  123. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
  124. package/dist/src/hooks/useGetCurrentAccount.js +28 -6
  125. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
  126. package/dist/src/hooks/useGetSession.d.ts +3 -20
  127. package/dist/src/hooks/useGetSession.d.ts.map +1 -1
  128. package/dist/src/hooks/useGetSession.js +2 -2
  129. package/dist/src/hooks/useGetSession.js.map +1 -1
  130. package/dist/src/hooks/useLinkAccount.d.ts +15 -121
  131. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  132. package/dist/src/hooks/useLinkAccount.js +8 -21
  133. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  134. package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
  135. package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
  136. package/dist/src/hooks/usePortalApiClient.js +6 -0
  137. package/dist/src/hooks/usePortalApiClient.js.map +1 -0
  138. package/dist/src/hooks/useRefreshPassport.d.ts +17 -0
  139. package/dist/src/hooks/useRefreshPassport.d.ts.map +1 -0
  140. package/dist/src/hooks/useRefreshPassport.js +29 -0
  141. package/dist/src/hooks/useRefreshPassport.js.map +1 -0
  142. package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -181
  143. package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
  144. package/dist/src/hooks/useSignInWithDiscord.js +11 -22
  145. package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
  146. package/dist/src/hooks/useSignInWithWallet.d.ts +12 -181
  147. package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
  148. package/dist/src/hooks/useSignInWithWallet.js +6 -44
  149. package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
  150. package/dist/src/hooks/useSignOut.d.ts +12 -28
  151. package/dist/src/hooks/useSignOut.d.ts.map +1 -1
  152. package/dist/src/hooks/useSignOut.js +11 -6
  153. package/dist/src/hooks/useSignOut.js.map +1 -1
  154. package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
  155. package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
  156. package/dist/src/hooks/useSignUpWithWallet.js +11 -0
  157. package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
  158. package/dist/src/hooks/useUpdateMezoId.d.ts +30 -82
  159. package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
  160. package/dist/src/hooks/useUpdateMezoId.js +8 -12
  161. package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
  162. package/dist/src/hooks/useWalletAccount.d.ts +12 -0
  163. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
  164. package/dist/src/hooks/useWalletAccount.js +28 -0
  165. package/dist/src/hooks/useWalletAccount.js.map +1 -0
  166. package/dist/src/index.d.ts +1 -0
  167. package/dist/src/index.d.ts.map +1 -1
  168. package/dist/src/index.js +1 -0
  169. package/dist/src/index.js.map +1 -1
  170. package/dist/src/provider.d.ts +10 -13
  171. package/dist/src/provider.d.ts.map +1 -1
  172. package/dist/src/provider.js +11 -20
  173. package/dist/src/provider.js.map +1 -1
  174. package/dist/src/stores/dropdownStore.d.ts +12 -0
  175. package/dist/src/stores/dropdownStore.d.ts.map +1 -0
  176. package/dist/src/stores/dropdownStore.js +13 -0
  177. package/dist/src/stores/dropdownStore.js.map +1 -0
  178. package/dist/src/utils/address.d.ts +15 -0
  179. package/dist/src/utils/address.d.ts.map +1 -0
  180. package/dist/src/utils/address.js +37 -0
  181. package/dist/src/utils/address.js.map +1 -0
  182. package/dist/src/utils/address.test.d.ts +2 -0
  183. package/dist/src/utils/address.test.d.ts.map +1 -0
  184. package/dist/src/utils/address.test.js +40 -0
  185. package/dist/src/utils/address.test.js.map +1 -0
  186. package/dist/src/utils/cryptoAssets.d.ts +28 -0
  187. package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
  188. package/dist/src/utils/cryptoAssets.js +73 -0
  189. package/dist/src/utils/cryptoAssets.js.map +1 -0
  190. package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
  191. package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
  192. package/dist/src/utils/cryptoAssets.test.js +50 -0
  193. package/dist/src/utils/cryptoAssets.test.js.map +1 -0
  194. package/dist/src/utils/currency.d.ts +14 -0
  195. package/dist/src/utils/currency.d.ts.map +1 -0
  196. package/dist/src/utils/currency.js +27 -0
  197. package/dist/src/utils/currency.js.map +1 -0
  198. package/dist/src/utils/currency.test.d.ts +2 -0
  199. package/dist/src/utils/currency.test.d.ts.map +1 -0
  200. package/dist/src/utils/currency.test.js +34 -0
  201. package/dist/src/utils/currency.test.js.map +1 -0
  202. package/dist/src/utils/numbers.d.ts +45 -0
  203. package/dist/src/utils/numbers.d.ts.map +1 -0
  204. package/dist/src/utils/numbers.js +86 -0
  205. package/dist/src/utils/numbers.js.map +1 -0
  206. package/dist/src/utils/numbers.test.d.ts +2 -0
  207. package/dist/src/utils/numbers.test.d.ts.map +1 -0
  208. package/dist/src/utils/numbers.test.js +170 -0
  209. package/dist/src/utils/numbers.test.js.map +1 -0
  210. package/package.json +10 -4
  211. package/src/api/auth.ts +104 -129
  212. package/src/api/client.ts +78 -0
  213. package/src/api/fetch-error.ts +8 -0
  214. package/src/api/index.ts +2 -0
  215. package/src/api/portal.ts +56 -0
  216. package/src/assets/DefaultAvatar.tsx +74 -0
  217. package/src/components/Dropdown/ConnectedTrigger.tsx +76 -0
  218. package/src/components/Dropdown/Content.tsx +62 -0
  219. package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
  220. package/src/components/Dropdown/Dropdown.tsx +124 -0
  221. package/src/components/Dropdown/NestedViewLayout.tsx +75 -0
  222. package/src/components/Dropdown/README.md +55 -0
  223. package/src/components/Dropdown/Receive/Receive.tsx +119 -0
  224. package/src/components/Dropdown/Root/AccountAddressActions.tsx +101 -0
  225. package/src/components/Dropdown/Root/AccountAssets.tsx +108 -0
  226. package/src/components/Dropdown/Root/AccountBalance.tsx +35 -0
  227. package/src/components/Dropdown/Root/Root.tsx +141 -0
  228. package/src/components/Dropdown/Root/WalletAddress.tsx +128 -0
  229. package/src/components/Dropdown/Root/WelcomeBlock.tsx +91 -0
  230. package/src/components/Dropdown/index.ts +2 -0
  231. package/src/components/index.ts +1 -0
  232. package/src/hooks/constants.ts +4 -4
  233. package/src/hooks/index.ts +2 -4
  234. package/src/hooks/useAssetsUSDConversion.ts +31 -0
  235. package/src/hooks/useAuthenticateWithWallet.ts +98 -0
  236. package/src/hooks/useCreateAccount.ts +20 -11
  237. package/src/hooks/useCreateSession.ts +24 -18
  238. package/src/hooks/useDropdownData.ts +160 -0
  239. package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +46 -0
  240. package/src/hooks/useGetAccountByAddress.ts +11 -5
  241. package/src/hooks/useGetAccountByMezoId.ts +11 -5
  242. package/src/hooks/useGetCurrentAccount.ts +53 -7
  243. package/src/hooks/useGetSession.ts +10 -3
  244. package/src/hooks/useLinkAccount.ts +19 -31
  245. package/src/hooks/usePortalApiClient.ts +6 -0
  246. package/src/hooks/useRefreshPassport.ts +40 -0
  247. package/src/hooks/useSignInWithDiscord.ts +19 -28
  248. package/src/hooks/useSignInWithWallet.ts +14 -55
  249. package/src/hooks/useSignOut.ts +22 -6
  250. package/src/hooks/useSignUpWithWallet.ts +21 -0
  251. package/src/hooks/useUpdateMezoId.ts +20 -12
  252. package/src/hooks/useWalletAccount.ts +53 -0
  253. package/src/index.ts +1 -0
  254. package/src/provider.ts +30 -37
  255. package/src/stores/dropdownStore.ts +20 -0
  256. package/src/utils/address.test.ts +48 -0
  257. package/src/utils/address.ts +45 -0
  258. package/src/utils/cryptoAssets.test.ts +61 -0
  259. package/src/utils/cryptoAssets.ts +93 -0
  260. package/src/utils/currency.test.ts +38 -0
  261. package/src/utils/currency.ts +32 -0
  262. package/src/utils/numbers.test.ts +220 -0
  263. package/src/utils/numbers.ts +120 -0
package/src/api/auth.ts CHANGED
@@ -1,152 +1,110 @@
1
- type RequestHandlerOptions = Omit<RequestInit, "credentials" | "body"> & {
2
- body?: Record<string, unknown>
3
- queryParams?: Record<string, string | undefined>
4
- }
5
- type PassportIdentityProvider = "wallet" | "discord" | "email"
1
+ import { ApiClient } from "./client"
2
+ import { FetchError } from "./fetch-error"
6
3
 
7
- type AccountPublicData = {
8
- mezoId?: string
9
- btcAddress: string
4
+ type AuthProvider = "discord"
5
+
6
+ export type LinkedWalletAccount = {
7
+ type: "wallet"
8
+ btcAddress?: string
10
9
  evmAddress: string
11
10
  }
12
11
 
12
+ export type LinkedProviderAccount = {
13
+ type: AuthProvider
14
+ name: string
15
+ id: string
16
+ avatarUrl: string
17
+ }
18
+
19
+ export type LinkedAccount = LinkedWalletAccount | LinkedProviderAccount
20
+
13
21
  type Account = {
14
- app_metadata: {
15
- provider: PassportIdentityProvider
16
- providers: PassportIdentityProvider[]
17
- }
18
- user_metadata: AccountPublicData
22
+ mezoId: string
23
+ hasModifiedMezoId: boolean
24
+ linkedAccounts: LinkedAccount[]
19
25
  }
20
26
 
21
- type Session = {
22
- access_token: string
23
- token_type: string
24
- expires_in: number
25
- expires_at: number
26
- refresh_token: string
27
- user: Account
27
+ export type Session = {
28
+ expiresIn: number
29
+ expiresAt: number
30
+ sessionLinks: {
31
+ _links: {
32
+ account: string
33
+ }
34
+ }
28
35
  }
29
36
 
30
- type AuthenticationProviderRedirectResponse = {
37
+ export type AuthenticationProviderRedirectResponse = {
31
38
  redirectTo: string
32
39
  }
33
40
 
34
- type WalletAccountCredentials = {
35
- type: "wallet"
41
+ type SignatureData = {
36
42
  message: string
37
43
  signature: string
38
44
  }
39
45
 
46
+ // Request and Response types
47
+ export type GetSessionResponse = Session | { nonce: string }
48
+
40
49
  export type CreateSessionRequest =
41
- | WalletAccountCredentials
50
+ | ({ type: "wallet" } & SignatureData)
42
51
  | { type: "discord" }
52
+ export type CreateSessionResponse =
53
+ | Session
54
+ | AuthenticationProviderRedirectResponse
43
55
 
44
- export type LinkAccountRequest = CreateSessionRequest
56
+ export type DeleteSessionResponse = { message: string }
45
57
 
46
- export type CreateAccountRequest = WalletAccountCredentials
58
+ export type GetCurrentAccountResponse = Partial<Account> | null
47
59
 
48
- export const API_ENDPOINTS_BY_ENV = {
49
- mainnet: "https://api.mezo.org",
50
- testnet: "https://api.test.mezo.org",
60
+ export type GetAccountByMezoIdOrAddressResponse = {
61
+ mezoId?: string
62
+ linkedAccounts?: Extract<LinkedAccount, { type: "wallet" }>[]
51
63
  }
52
64
 
53
- const DEFAULT_API_ENDPOINT = API_ENDPOINTS_BY_ENV.mainnet
54
-
55
- export class AuthApiClient {
56
- private apiUrl: string
57
-
58
- constructor(apiUrl?: string) {
59
- this.apiUrl = apiUrl ?? DEFAULT_API_ENDPOINT
60
- }
61
-
62
- /**
63
- * The request handler for the Passport API. It accepts an API endpoint and
64
- * request options.
65
- * @dev Generic type `D` is a type of returned object from the function
66
- * @param endpoint - The API endpoint to request. Should start with
67
- * forward slash ("/")
68
- * @param {RequestHandlerOptions<B>} options - The request options, the
69
- * `RequestInit` type with additional type-safe properties
70
- * @returns The promise of the API response
71
- */
72
- protected async handleRequest<D>(
73
- endpoint: `/${string}`,
74
- options: RequestHandlerOptions,
75
- ): Promise<D> {
76
- const {
77
- method,
78
- headers,
79
- body,
80
- queryParams = {},
81
- ...restOptions
82
- } = options || {}
83
-
84
- // If last character in apiUrl is forward slash we are removing it
85
- const authApiUrlRoute = this.apiUrl.replace(/\/$/, "")
86
- const url = new URL(`${authApiUrlRoute}${endpoint}`)
87
-
88
- // Removes falsy values (e.g. null, undefined etc.) from query params and
89
- // appends remaining ones to the URL
90
- Object.entries(queryParams)
91
- .filter(([, value]) => value) // Remove falsy values
92
- .forEach(([key, value]) => url.searchParams.append(key, value!))
93
-
94
- try {
95
- const response = await fetch(url, {
96
- method,
97
- credentials: "include",
98
- headers: {
99
- "Content-Type": "application/json",
100
- ...headers,
101
- },
102
- body: body ? JSON.stringify(body) : undefined,
103
- ...restOptions,
104
- })
65
+ export type LinkAccountRequest = CreateSessionRequest
66
+ export type LinkAccountResponse =
67
+ | Account
68
+ | AuthenticationProviderRedirectResponse
105
69
 
106
- const data = await response.json()
70
+ export type CreateAccountRequest = { type: "wallet" } & SignatureData
71
+ export type CreateAccountResponse = Account
107
72
 
108
- if (!response.ok || data?.error) {
109
- const error = data?.error || "An error occurred"
110
- throw new Error(error)
111
- }
73
+ export type UpdateMezoIdResponse = Account
112
74
 
113
- return data as D
114
- } catch (error) {
115
- const errorMessage =
116
- error instanceof Error ? error.message : "An error occurred"
75
+ export const AUTH_API_ENDPOINTS_BY_ENV = {
76
+ mainnet: "https://api.mezo.org",
77
+ testnet: "https://api.test.mezo.org",
78
+ }
117
79
 
118
- throw new Error(errorMessage)
119
- }
80
+ export class AuthApiClient extends ApiClient {
81
+ constructor(
82
+ environment: keyof typeof AUTH_API_ENDPOINTS_BY_ENV = "mainnet",
83
+ apiUrl?: string,
84
+ ) {
85
+ super(apiUrl ?? AUTH_API_ENDPOINTS_BY_ENV[environment])
120
86
  }
121
87
 
122
88
  async getSession(code?: string) {
123
- const data = await this.handleRequest<Session | { nonce: string }>(
124
- "/session",
125
- {
126
- method: "GET",
127
- queryParams: { code },
128
- },
129
- )
89
+ const data = await this.handleRequest<GetSessionResponse>("/session", {
90
+ method: "GET",
91
+ queryParams: { code },
92
+ })
130
93
 
131
94
  return data
132
95
  }
133
96
 
134
97
  async createSession(createSessionRequest: CreateSessionRequest) {
135
- const data = await this.handleRequest<Session | { redirectTo: string }>(
136
- "/session",
137
- {
138
- method: "POST",
139
- body: createSessionRequest,
140
- },
141
- )
98
+ const data = await this.handleRequest<CreateSessionResponse>("/session", {
99
+ method: "POST",
100
+ body: createSessionRequest,
101
+ })
142
102
 
143
103
  return data
144
104
  }
145
105
 
146
106
  async deleteSession() {
147
- const data = await this.handleRequest<{
148
- message: string
149
- }>("/session", {
107
+ const data = await this.handleRequest<DeleteSessionResponse>("/session", {
150
108
  method: "DELETE",
151
109
  })
152
110
 
@@ -154,37 +112,51 @@ export class AuthApiClient {
154
112
  }
155
113
 
156
114
  async getCurrentAccount() {
157
- const data = await this.handleRequest<Account>("/session/account", {
158
- method: "GET",
159
- })
115
+ const data = await this.handleRequest<GetCurrentAccountResponse>(
116
+ "/session/account",
117
+ {
118
+ method: "GET",
119
+ },
120
+ )
160
121
 
161
122
  return data
162
123
  }
163
124
 
164
125
  async getAccountByMezoIdOrAddress(mezoIdOrAddress: string) {
165
- const data = await this.handleRequest<AccountPublicData>(
166
- `/accounts/${mezoIdOrAddress}`,
167
- {
168
- method: "GET",
169
- },
170
- )
126
+ let data
127
+ try {
128
+ data = await this.handleRequest<GetAccountByMezoIdOrAddressResponse>(
129
+ `/accounts/${mezoIdOrAddress}`,
130
+ {
131
+ method: "GET",
132
+ },
133
+ )
134
+ } catch (error) {
135
+ // Return null if account is not found instead of throwing an error
136
+ if (error instanceof FetchError && error.status === 404) {
137
+ data = null
138
+ } else {
139
+ throw error
140
+ }
141
+ }
171
142
 
172
143
  return data
173
144
  }
174
145
 
175
146
  async linkAccount(linkAccountRequest: LinkAccountRequest) {
176
- const data = await this.handleRequest<
177
- Account | AuthenticationProviderRedirectResponse
178
- >("/session/account/linked-accounts", {
179
- method: "POST",
180
- body: linkAccountRequest,
181
- })
147
+ const data = await this.handleRequest<LinkAccountResponse>(
148
+ "/session/account/linked-accounts",
149
+ {
150
+ method: "POST",
151
+ body: linkAccountRequest,
152
+ },
153
+ )
182
154
 
183
155
  return data
184
156
  }
185
157
 
186
158
  async createAccount(createAccountRequest: CreateAccountRequest) {
187
- const data = await this.handleRequest<Account>("/accounts", {
159
+ const data = await this.handleRequest<CreateAccountResponse>("/accounts", {
188
160
  method: "POST",
189
161
  body: createAccountRequest,
190
162
  })
@@ -193,10 +165,13 @@ export class AuthApiClient {
193
165
  }
194
166
 
195
167
  async updateMezoId(newMezoId: string) {
196
- const data = await this.handleRequest<Account>("/session/account", {
197
- method: "PATCH",
198
- body: { newMezoId },
199
- })
168
+ const data = await this.handleRequest<UpdateMezoIdResponse>(
169
+ "/session/account",
170
+ {
171
+ method: "PATCH",
172
+ body: { mezoId: newMezoId },
173
+ },
174
+ )
200
175
 
201
176
  return data
202
177
  }
@@ -0,0 +1,78 @@
1
+ import { FetchError } from "./fetch-error"
2
+
3
+ type RequestHandlerOptions = Omit<RequestInit, "credentials" | "body"> & {
4
+ body?: Record<string, unknown>
5
+ queryParams?: Record<string, string | undefined>
6
+ }
7
+
8
+ export abstract class ApiClient {
9
+ private apiUrl: string
10
+
11
+ constructor(apiUrl: string) {
12
+ this.apiUrl = apiUrl
13
+ }
14
+
15
+ /**
16
+ * The request handler for the Passport API. It accepts an API endpoint and
17
+ * request options.
18
+ * @dev Generic type `D` is a type of returned object from the function
19
+ * @dev WARNING: This function hardcodes `credentials: "include"`, meaning
20
+ * cookies and authentication headers will be sent with every request. Ensure
21
+ * this is safe if you plan to use it in other contexts than auth api worker.
22
+ * @param endpoint - The API endpoint to request. Should start with
23
+ * forward slash ("/")
24
+ * @param options - The request options, the `RequestInit` type with
25
+ * additional type-safe properties
26
+ * @returns The promise of the API response
27
+ */
28
+ protected async handleRequest<D>(
29
+ endpoint: `/${string}`,
30
+ options: RequestHandlerOptions = {},
31
+ ): Promise<D> {
32
+ try {
33
+ const { method, body, queryParams = {}, ...restOptions } = options
34
+
35
+ // If last character in apiUrl is forward slash we are removing it
36
+ const authApiUrlRoute = this.apiUrl.replace(/\/$/, "")
37
+ const url = new URL(`${authApiUrlRoute}${endpoint}`)
38
+
39
+ // Removes falsy values (e.g. null, undefined etc.) from query params and
40
+ // appends remaining ones to the URL
41
+ Object.entries(queryParams)
42
+ .filter(([, value]) => value) // Remove falsy values
43
+ .forEach(([key, value]) => url.searchParams.append(key, value!))
44
+
45
+ const response = await fetch(url, {
46
+ method,
47
+ credentials: "include",
48
+ body: body ? JSON.stringify(body) : undefined,
49
+ ...restOptions,
50
+ })
51
+
52
+ const data = await response.json()
53
+
54
+ if (!response.ok || data?.error) {
55
+ const error =
56
+ data?.error ||
57
+ `An error occurred when handling API request: ${JSON.stringify(data)}`
58
+ throw new FetchError(
59
+ response.status,
60
+ `Error [${response.status}]: ${error}`,
61
+ )
62
+ }
63
+
64
+ return data as D
65
+ } catch (error) {
66
+ if (error instanceof FetchError) {
67
+ throw error
68
+ }
69
+
70
+ const errorMessage =
71
+ error instanceof Error ? error.message : JSON.stringify(error)
72
+ throw new FetchError(
73
+ 500,
74
+ `Error [500]: An error occurred when handling API request: ${errorMessage}`,
75
+ )
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,8 @@
1
+ export class FetchError extends Error {
2
+ constructor(
3
+ public status: number,
4
+ message?: string,
5
+ ) {
6
+ super(message)
7
+ }
8
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./auth"
2
+ export * from "./portal"
@@ -0,0 +1,56 @@
1
+ import { ApiClient } from "./client"
2
+
3
+ type PortalStatistics = {
4
+ hodlers: number
5
+ depositedBitcoin: string
6
+ tvl: number
7
+ currentUsdPerBtc: string
8
+ currentUsdPerEth: string
9
+ usdPerBtc24hChange: string
10
+ }
11
+
12
+ type PortalMats = {
13
+ address: string
14
+ totalMats: number
15
+ matsFromDeposits: number
16
+ matsFromReferrals: number
17
+ matsFromSecondaryReferrals: number
18
+ netMatsTransferred: number
19
+ dailyMats: number
20
+ dailyMatsFromDeposits: number
21
+ dailyMatsFromReferrals: number
22
+ dailyMatsFromSecondaryReferrals: number
23
+ }
24
+
25
+ export const PORTAL_API_ENDPOINTS_BY_ENV = {
26
+ mainnet: "https://api.mezo.org/api/v1",
27
+ testnet: "https://api.test.mezo.org/api/v1",
28
+ }
29
+
30
+ export class PortalApiClient extends ApiClient {
31
+ constructor(
32
+ environment: keyof typeof PORTAL_API_ENDPOINTS_BY_ENV = "mainnet",
33
+ apiUrl?: string,
34
+ ) {
35
+ super(apiUrl ?? PORTAL_API_ENDPOINTS_BY_ENV[environment])
36
+ }
37
+
38
+ async getPortalStatistics() {
39
+ const data = await this.handleRequest<PortalStatistics>("/statistics", {
40
+ method: "GET",
41
+ })
42
+
43
+ return data
44
+ }
45
+
46
+ async getPortalMats(address: string) {
47
+ const data = await this.handleRequest<PortalMats>(
48
+ `/accounts/${address}/mats`,
49
+ {
50
+ method: "GET",
51
+ },
52
+ )
53
+
54
+ return data
55
+ }
56
+ }
@@ -0,0 +1,74 @@
1
+ import { Block, BlockProps } from "@mezo-org/mezo-clay"
2
+ import React, { SVGProps } from "react"
3
+
4
+ function DefaultAvatar(props: SVGProps<SVGSVGElement>) {
5
+ return (
6
+ <svg viewBox="0 0 214 214" fill="none" {...props}>
7
+ <rect y="-0.478027" width="166.379" height="81.7091" fill="#FF004D" />
8
+ <rect x="166" y="-0.478027" width="48" height="82" fill="black" />
9
+ <rect
10
+ x="166.379"
11
+ y="81.231"
12
+ width="24.1044"
13
+ height="132.291"
14
+ fill="#DFFF80"
15
+ />
16
+ <rect
17
+ x="118.758"
18
+ y="81.231"
19
+ width="24.1044"
20
+ height="132.291"
21
+ fill="#FF5500"
22
+ />
23
+ <rect
24
+ x="71.1372"
25
+ y="81.231"
26
+ width="24.1044"
27
+ height="132.291"
28
+ fill="#5A59A7"
29
+ />
30
+ <rect
31
+ x="23.5166"
32
+ y="81.231"
33
+ width="24.1044"
34
+ height="132.291"
35
+ fill="#47B5FF"
36
+ />
37
+ <rect y="81.231" width="23.5165" height="132.291" fill="#A3C238" />
38
+ <rect
39
+ x="190.483"
40
+ y="81.231"
41
+ width="23.5165"
42
+ height="132.291"
43
+ fill="#6E6E70"
44
+ />
45
+ <rect
46
+ x="142.863"
47
+ y="81.231"
48
+ width="23.5165"
49
+ height="132.291"
50
+ fill="#0A3C01"
51
+ />
52
+ <rect
53
+ x="95.2417"
54
+ y="81.231"
55
+ width="23.5165"
56
+ height="132.291"
57
+ fill="#F2A91F"
58
+ />
59
+ <rect
60
+ x="47.6211"
61
+ y="81.231"
62
+ width="23.5165"
63
+ height="132.291"
64
+ fill="#6B3D01"
65
+ />
66
+ </svg>
67
+ )
68
+ }
69
+
70
+ function DefaultAvatarComponent(props: BlockProps) {
71
+ return <Block as={DefaultAvatar} {...props} />
72
+ }
73
+
74
+ export default DefaultAvatarComponent
@@ -0,0 +1,76 @@
1
+ import { Block, Button, ButtonProps, useStyletron } from "@mezo-org/mezo-clay"
2
+ import React, { forwardRef, ReactNode, useMemo } from "react"
3
+
4
+ type ConnectedTriggerProps = Omit<ButtonProps, "children" | "size"> & {
5
+ avatar: ReactNode
6
+ size?: "medium" | "large"
7
+ }
8
+
9
+ const ConnectedTrigger = forwardRef<HTMLButtonElement, ConnectedTriggerProps>(
10
+ (props, ref) => {
11
+ const { onClick, avatar, size = "medium", ...restProps } = props
12
+
13
+ const [, theme] = useStyletron()
14
+
15
+ const avatarSize = useMemo(() => {
16
+ const sizeMap = {
17
+ medium: theme.sizing.scale950,
18
+ large: theme.sizing.scale1400,
19
+ }
20
+
21
+ return sizeMap[size]
22
+ }, [theme, size])
23
+
24
+ const buttonPadding = useMemo(() => {
25
+ const paddingMap = {
26
+ medium: theme.sizing.scale0,
27
+ large: theme.sizing.scale100,
28
+ }
29
+
30
+ return paddingMap[size]
31
+ }, [theme, size])
32
+
33
+ return (
34
+ <Button
35
+ overrides={{
36
+ BaseButton: {
37
+ props: { ref, ...restProps },
38
+ style: {
39
+ borderRadius: "100%",
40
+ overflow: "hidden",
41
+ padding: buttonPadding,
42
+ },
43
+ },
44
+ }}
45
+ onClick={onClick}
46
+ >
47
+ {typeof avatar === "string" ? (
48
+ <Block
49
+ width={avatarSize}
50
+ height={avatarSize}
51
+ as="img"
52
+ src={avatar}
53
+ alt=""
54
+ />
55
+ ) : (
56
+ <Block
57
+ width={avatarSize}
58
+ height={avatarSize}
59
+ overrides={{
60
+ Block: {
61
+ style: {
62
+ borderRadius: "100%",
63
+ overflow: "hidden",
64
+ },
65
+ },
66
+ }}
67
+ >
68
+ {avatar}
69
+ </Block>
70
+ )}
71
+ </Button>
72
+ )
73
+ },
74
+ )
75
+
76
+ export default ConnectedTrigger
@@ -0,0 +1,62 @@
1
+ import { Block, useStyletron } from "@mezo-org/mezo-clay"
2
+ import React, { useEffect } from "react"
3
+ import { DropdownData } from "../../hooks/useDropdownData"
4
+ import useDropdownStore, { DropdownView } from "../../stores/dropdownStore"
5
+ import Root from "./Root/Root"
6
+ import Receive from "./Receive/Receive"
7
+
8
+ type ContentProps = {
9
+ data: DropdownData
10
+ onSignOut?: () => void
11
+ }
12
+
13
+ function Content(props: ContentProps) {
14
+ const { data, onSignOut } = props
15
+
16
+ const [_, theme] = useStyletron()
17
+
18
+ const currentView = useDropdownStore((state) => state.view)
19
+ const setView = useDropdownStore((state) => state.setView)
20
+
21
+ useEffect(
22
+ () => () => {
23
+ setView(DropdownView.ROOT)
24
+ },
25
+ [setView],
26
+ )
27
+
28
+ return (
29
+ <Block
30
+ display="inline-flex"
31
+ flexDirection="column"
32
+ backgroundColor={theme.colors.backgroundPrimary}
33
+ width="100%"
34
+ overrides={{
35
+ Block: {
36
+ style: {
37
+ boxSizing: "border-box",
38
+ },
39
+ },
40
+ }}
41
+ >
42
+ {currentView === DropdownView.ROOT && (
43
+ <Root
44
+ mezoId={data.mezoId}
45
+ accountAddress={data.accountAddress}
46
+ walletAddress={data.walletAddress}
47
+ walletType={data.walletType}
48
+ totalBalanceInUsd={data.totalBalanceInUsd}
49
+ formattedNativeAssets={data.formattedNativeAssets}
50
+ matsnet={data.matsnet}
51
+ onSignOut={onSignOut}
52
+ />
53
+ )}
54
+
55
+ {currentView === DropdownView.RECEIVE && (
56
+ <Receive address={data.accountAddress} />
57
+ )}
58
+ </Block>
59
+ )
60
+ }
61
+
62
+ export default Content