@mezo-org/passport 0.4.0-dev.64 → 0.4.0-dev.66

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 (73) hide show
  1. package/dist/src/api/auth.d.ts +7 -3
  2. package/dist/src/api/auth.d.ts.map +1 -1
  3. package/dist/src/api/auth.js.map +1 -1
  4. package/dist/src/hooks/constants.d.ts +1 -0
  5. package/dist/src/hooks/constants.d.ts.map +1 -1
  6. package/dist/src/hooks/constants.js +1 -0
  7. package/dist/src/hooks/constants.js.map +1 -1
  8. package/dist/src/hooks/index.d.ts +1 -0
  9. package/dist/src/hooks/index.d.ts.map +1 -1
  10. package/dist/src/hooks/index.js +1 -0
  11. package/dist/src/hooks/index.js.map +1 -1
  12. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
  13. package/dist/src/hooks/useAuthenticateWithWallet.js +2 -1
  14. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
  15. package/dist/src/hooks/useBorrowData.d.ts.map +1 -1
  16. package/dist/src/hooks/useBorrowData.js +3 -1
  17. package/dist/src/hooks/useBorrowData.js.map +1 -1
  18. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  19. package/dist/src/hooks/useCreateAccount.js +22 -1
  20. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  21. package/dist/src/hooks/useGetAccountByMezoId.d.ts +4 -2
  22. package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
  23. package/dist/src/hooks/useGetAccountByMezoId.js +7 -4
  24. package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
  25. package/dist/src/hooks/useIsUsernameValid.d.ts +7 -0
  26. package/dist/src/hooks/useIsUsernameValid.d.ts.map +1 -0
  27. package/dist/src/hooks/useIsUsernameValid.js +41 -0
  28. package/dist/src/hooks/useIsUsernameValid.js.map +1 -0
  29. package/dist/src/hooks/useLinkAccount.d.ts +19 -13
  30. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  31. package/dist/src/hooks/useLinkAccount.js +20 -1
  32. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  33. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
  34. package/dist/src/hooks/useWalletAccount.js.map +1 -1
  35. package/dist/src/utils/numbers.d.ts +7 -0
  36. package/dist/src/utils/numbers.d.ts.map +1 -1
  37. package/dist/src/utils/numbers.js +7 -0
  38. package/dist/src/utils/numbers.js.map +1 -1
  39. package/dist/src/utils/numbers.test.js +21 -1
  40. package/dist/src/utils/numbers.test.js.map +1 -1
  41. package/dist/src/utils/siww.d.ts +1 -0
  42. package/dist/src/utils/siww.d.ts.map +1 -1
  43. package/dist/src/utils/siww.js +50 -12
  44. package/dist/src/utils/siww.js.map +1 -1
  45. package/dist/src/utils/validation.d.ts +2 -0
  46. package/dist/src/utils/validation.d.ts.map +1 -0
  47. package/dist/src/utils/validation.js +37 -0
  48. package/dist/src/utils/validation.js.map +1 -0
  49. package/dist/src/utils/wagmi.d.ts +3 -0
  50. package/dist/src/utils/wagmi.d.ts.map +1 -0
  51. package/dist/src/utils/wagmi.js +7 -0
  52. package/dist/src/utils/wagmi.js.map +1 -0
  53. package/dist/src/wallet/index.d.ts +1 -1
  54. package/dist/src/wallet/index.d.ts.map +1 -1
  55. package/dist/src/wallet/index.js +1 -1
  56. package/dist/src/wallet/index.js.map +1 -1
  57. package/package.json +2 -2
  58. package/src/api/auth.ts +5 -3
  59. package/src/hooks/constants.ts +1 -0
  60. package/src/hooks/index.ts +1 -0
  61. package/src/hooks/useAuthenticateWithWallet.ts +10 -1
  62. package/src/hooks/useBorrowData.ts +7 -1
  63. package/src/hooks/useCreateAccount.ts +28 -2
  64. package/src/hooks/useGetAccountByMezoId.ts +14 -5
  65. package/src/hooks/useIsUsernameValid.ts +52 -0
  66. package/src/hooks/useLinkAccount.ts +36 -4
  67. package/src/hooks/useWalletAccount.ts +1 -5
  68. package/src/utils/numbers.test.ts +27 -0
  69. package/src/utils/numbers.ts +9 -0
  70. package/src/utils/siww.ts +71 -16
  71. package/src/utils/validation.ts +42 -0
  72. package/src/utils/wagmi.ts +12 -0
  73. package/src/wallet/index.ts +3 -2
package/src/utils/siww.ts CHANGED
@@ -1,4 +1,7 @@
1
- import { SignInWithWalletMessage } from "@mezo-org/sign-in-with-wallet"
1
+ import {
2
+ SignInWithWalletErrorType,
3
+ SignInWithWalletMessage,
4
+ } from "@mezo-org/sign-in-with-wallet"
2
5
  import { ONE_DAY_MS } from "./time"
3
6
 
4
7
  const SESSION_EXPIRATION_DURATION_MS = 14 * ONE_DAY_MS
@@ -13,19 +16,71 @@ export function createSignInWithWalletMessage(
13
16
  ) {
14
17
  const { host: domain, origin: uri } = window.location
15
18
 
16
- const message = new SignInWithWalletMessage({
17
- domain,
18
- address, // if "bitcoin" this should be btc address, not underlaying eth address
19
- uri,
20
- nonce,
21
- issuedAt: new Date().toISOString(),
22
- expirationTime: new Date(
23
- Date.now() + SESSION_EXPIRATION_DURATION_MS,
24
- ).toISOString(),
25
- version: "1",
26
- chainId: networkFamily === "evm" ? chainId : undefined,
27
- networkFamily,
28
- })
29
-
30
- return message.prepareMessage()
19
+ let siwwMessage
20
+ try {
21
+ siwwMessage = new SignInWithWalletMessage({
22
+ domain,
23
+ address, // if "bitcoin" this should be btc address, not underlaying eth address
24
+ uri,
25
+ nonce,
26
+ issuedAt: new Date().toISOString(),
27
+ expirationTime: new Date(
28
+ Date.now() + SESSION_EXPIRATION_DURATION_MS,
29
+ ).toISOString(),
30
+ version: "1",
31
+ chainId: networkFamily === "evm" ? chainId : undefined,
32
+ networkFamily,
33
+ })
34
+ } catch (error) {
35
+ throw new Error(`Failed to create sign in with wallet message: ${error}`)
36
+ }
37
+
38
+ return siwwMessage.prepareMessage()
39
+ }
40
+
41
+ class SIWWDomainError extends Error {
42
+ constructor(message: string) {
43
+ super(`Error when verifying domain in SIWW message: ${message}`)
44
+ this.name = "SIWWDomainError"
45
+ }
46
+ }
47
+
48
+ export async function verifyDomainInSignInWithWalletMessage(
49
+ message: string,
50
+ signature: string,
51
+ nonce: string,
52
+ ) {
53
+ const { host: domain } = window.location
54
+ let siwwMessage: SignInWithWalletMessage
55
+ try {
56
+ siwwMessage = new SignInWithWalletMessage(message)
57
+ } catch (error) {
58
+ throw new SIWWDomainError("Failed to parse SIWW message")
59
+ }
60
+
61
+ if (!siwwMessage.expirationTime) {
62
+ throw new SIWWDomainError("SIWW messages must have an expiration time set")
63
+ }
64
+
65
+ const result = await siwwMessage.verify(
66
+ {
67
+ signature,
68
+ // Nonce has to match the session ID carried in the request.
69
+ nonce,
70
+ domain,
71
+ // Time is used as a reference to verify the expiration time set in the
72
+ // message.
73
+ time: new Date().toISOString(),
74
+ },
75
+ { suppressExceptions: true },
76
+ )
77
+
78
+ if (
79
+ result.error &&
80
+ result.error.type === SignInWithWalletErrorType.DOMAIN_MISMATCH
81
+ ) {
82
+ throw new SIWWDomainError(
83
+ "Domain does not match provided domain for verification.",
84
+ )
85
+ }
31
86
  }
@@ -0,0 +1,42 @@
1
+ // NOTE: This was copied from mezo-portal and is based on the validation from
2
+ // workers/passport-auth/src/utils/mezo-id.ts.
3
+
4
+ // Most of these are restrictions for an DNS hostname label (i.e., the part
5
+ // before/after `.` in a full hostname) as outlined in RFC1034
6
+ // (https://www.rfc-editor.org/rfc/rfc1034).
7
+ //
8
+ // A few addenda are made to ensure that nothing can sneak through that could
9
+ // look like an Ethereum or Bitcoin address.
10
+ const DISALLOWED_USERNAME_PATTERNS: { pattern: RegExp; error: string }[] = [
11
+ // RFC1034.
12
+ { pattern: /^$/, error: "Must not be empty." },
13
+ { pattern: /^.{16,}$/, error: "Must have 15 characters or fewer." },
14
+ {
15
+ pattern: /^[^A-Za-z]/,
16
+ error: "Must start with a one of the letters A-Z or a-z.",
17
+ },
18
+ {
19
+ pattern: /[^A-Za-z0-9-]/,
20
+ error:
21
+ "Must only use letters or numbers (A-Z, a-z, or 0-9), or hyphen (-).",
22
+ },
23
+ { pattern: /-$/, error: "Must not end in a hyphen (-)." },
24
+ // On-chain shenanigan avoidance.
25
+ { pattern: /0x/i, error: "Must not contain 0x." },
26
+ {
27
+ pattern: /^(?:bc1|tb1|[a-z]pub|[a-z]priv)/i,
28
+ error: "Must not start with a Bitcoin magic string.",
29
+ },
30
+ {
31
+ pattern: /^m[0-9]+$/i,
32
+ error: 'No "m" + "numeric string" prefixes.',
33
+ },
34
+ ]
35
+
36
+ export function validateUsername(username: string): string[] {
37
+ const errors = DISALLOWED_USERNAME_PATTERNS.flatMap(({ pattern, error }) =>
38
+ pattern.test(username) ? [error] : [],
39
+ )
40
+
41
+ return errors
42
+ }
@@ -0,0 +1,12 @@
1
+ import { OrangeKitConnector } from "@mezo-org/orangekit"
2
+ import { Connector } from "wagmi"
3
+
4
+ export async function getBitcoinPublicKeyFromConnector(connector: Connector) {
5
+ if (connector.type !== "orangekit") return undefined
6
+
7
+ const provider = (
8
+ connector as unknown as OrangeKitConnector
9
+ ).getBitcoinProvider()
10
+
11
+ return provider.getPublicKey()
12
+ }
@@ -1,9 +1,10 @@
1
1
  export {
2
+ BitcoinWalletConnectionError,
2
3
  getOKXWallet,
3
4
  getUnisatWallet,
4
5
  getXverseWallet,
5
- BitcoinWalletConnectionError,
6
- WalletNetworkDoesNotMatchProviderChainError,
7
6
  isUnsupportedBitcoinAddressError,
8
7
  isWalletNetworkDoesNotMatchProviderChainError,
8
+ OrangeKitConnector,
9
+ WalletNetworkDoesNotMatchProviderChainError,
9
10
  } from "@mezo-org/orangekit"