@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.
- package/dist/src/api/auth.d.ts +7 -3
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/hooks/constants.d.ts +1 -0
- package/dist/src/hooks/constants.d.ts.map +1 -1
- package/dist/src/hooks/constants.js +1 -0
- package/dist/src/hooks/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -0
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +1 -0
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.js +2 -1
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
- package/dist/src/hooks/useBorrowData.d.ts.map +1 -1
- package/dist/src/hooks/useBorrowData.js +3 -1
- package/dist/src/hooks/useBorrowData.js.map +1 -1
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +22 -1
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +4 -2
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.js +7 -4
- package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
- package/dist/src/hooks/useIsUsernameValid.d.ts +7 -0
- package/dist/src/hooks/useIsUsernameValid.d.ts.map +1 -0
- package/dist/src/hooks/useIsUsernameValid.js +41 -0
- package/dist/src/hooks/useIsUsernameValid.js.map +1 -0
- package/dist/src/hooks/useLinkAccount.d.ts +19 -13
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +20 -1
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
- package/dist/src/hooks/useWalletAccount.js.map +1 -1
- package/dist/src/utils/numbers.d.ts +7 -0
- package/dist/src/utils/numbers.d.ts.map +1 -1
- package/dist/src/utils/numbers.js +7 -0
- package/dist/src/utils/numbers.js.map +1 -1
- package/dist/src/utils/numbers.test.js +21 -1
- package/dist/src/utils/numbers.test.js.map +1 -1
- package/dist/src/utils/siww.d.ts +1 -0
- package/dist/src/utils/siww.d.ts.map +1 -1
- package/dist/src/utils/siww.js +50 -12
- package/dist/src/utils/siww.js.map +1 -1
- package/dist/src/utils/validation.d.ts +2 -0
- package/dist/src/utils/validation.d.ts.map +1 -0
- package/dist/src/utils/validation.js +37 -0
- package/dist/src/utils/validation.js.map +1 -0
- package/dist/src/utils/wagmi.d.ts +3 -0
- package/dist/src/utils/wagmi.d.ts.map +1 -0
- package/dist/src/utils/wagmi.js +7 -0
- package/dist/src/utils/wagmi.js.map +1 -0
- package/dist/src/wallet/index.d.ts +1 -1
- package/dist/src/wallet/index.d.ts.map +1 -1
- package/dist/src/wallet/index.js +1 -1
- package/dist/src/wallet/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/auth.ts +5 -3
- package/src/hooks/constants.ts +1 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useAuthenticateWithWallet.ts +10 -1
- package/src/hooks/useBorrowData.ts +7 -1
- package/src/hooks/useCreateAccount.ts +28 -2
- package/src/hooks/useGetAccountByMezoId.ts +14 -5
- package/src/hooks/useIsUsernameValid.ts +52 -0
- package/src/hooks/useLinkAccount.ts +36 -4
- package/src/hooks/useWalletAccount.ts +1 -5
- package/src/utils/numbers.test.ts +27 -0
- package/src/utils/numbers.ts +9 -0
- package/src/utils/siww.ts +71 -16
- package/src/utils/validation.ts +42 -0
- package/src/utils/wagmi.ts +12 -0
- package/src/wallet/index.ts +3 -2
package/src/utils/siww.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
Date.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
+
}
|
package/src/wallet/index.ts
CHANGED
|
@@ -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"
|