viem 0.0.0-main.20240521T214213 → 0.0.0-main.20240521T215546

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 (88) hide show
  1. package/_cjs/actions/public/verifyMessage.js.map +1 -1
  2. package/_cjs/actions/siwe/verifySiweMessage.js +32 -0
  3. package/_cjs/actions/siwe/verifySiweMessage.js.map +1 -0
  4. package/_cjs/chains/definitions/flowPreviewnet.js +1 -1
  5. package/_cjs/chains/definitions/metis.js.map +1 -1
  6. package/_cjs/clients/decorators/public.js +2 -0
  7. package/_cjs/clients/decorators/public.js.map +1 -1
  8. package/_cjs/errors/siwe.js +22 -0
  9. package/_cjs/errors/siwe.js.map +1 -0
  10. package/_cjs/errors/version.js +1 -1
  11. package/_cjs/siwe/index.js +16 -0
  12. package/_cjs/siwe/index.js.map +1 -0
  13. package/_cjs/utils/siwe/createSiweMessage.js +122 -0
  14. package/_cjs/utils/siwe/createSiweMessage.js.map +1 -0
  15. package/_cjs/utils/siwe/generateSiweNonce.js +9 -0
  16. package/_cjs/utils/siwe/generateSiweNonce.js.map +1 -0
  17. package/_cjs/utils/siwe/parseSiweMessage.js +25 -0
  18. package/_cjs/utils/siwe/parseSiweMessage.js.map +1 -0
  19. package/_cjs/utils/siwe/types.js +3 -0
  20. package/_cjs/utils/siwe/types.js.map +1 -0
  21. package/_cjs/utils/siwe/utils.js +44 -0
  22. package/_cjs/utils/siwe/utils.js.map +1 -0
  23. package/_cjs/utils/siwe/validateSiweMessage.js +29 -0
  24. package/_cjs/utils/siwe/validateSiweMessage.js.map +1 -0
  25. package/_esm/actions/public/verifyMessage.js.map +1 -1
  26. package/_esm/actions/siwe/verifySiweMessage.js +39 -0
  27. package/_esm/actions/siwe/verifySiweMessage.js.map +1 -0
  28. package/_esm/chains/definitions/flowPreviewnet.js +1 -1
  29. package/_esm/chains/definitions/metis.js.map +1 -1
  30. package/_esm/clients/decorators/public.js +2 -0
  31. package/_esm/clients/decorators/public.js.map +1 -1
  32. package/_esm/errors/siwe.js +18 -0
  33. package/_esm/errors/siwe.js.map +1 -0
  34. package/_esm/errors/version.js +1 -1
  35. package/_esm/siwe/index.js +7 -0
  36. package/_esm/siwe/index.js.map +1 -0
  37. package/_esm/utils/siwe/createSiweMessage.js +137 -0
  38. package/_esm/utils/siwe/createSiweMessage.js.map +1 -0
  39. package/_esm/utils/siwe/generateSiweNonce.js +15 -0
  40. package/_esm/utils/siwe/generateSiweNonce.js.map +1 -0
  41. package/_esm/utils/siwe/parseSiweMessage.js +30 -0
  42. package/_esm/utils/siwe/parseSiweMessage.js.map +1 -0
  43. package/_esm/utils/siwe/types.js +2 -0
  44. package/_esm/utils/siwe/types.js.map +1 -0
  45. package/_esm/utils/siwe/utils.js +49 -0
  46. package/_esm/utils/siwe/utils.js.map +1 -0
  47. package/_esm/utils/siwe/validateSiweMessage.js +30 -0
  48. package/_esm/utils/siwe/validateSiweMessage.js.map +1 -0
  49. package/_types/actions/public/verifyMessage.d.ts +3 -2
  50. package/_types/actions/public/verifyMessage.d.ts.map +1 -1
  51. package/_types/actions/siwe/verifySiweMessage.d.ts +34 -0
  52. package/_types/actions/siwe/verifySiweMessage.d.ts.map +1 -0
  53. package/_types/chains/definitions/metis.d.ts.map +1 -1
  54. package/_types/clients/decorators/public.d.ts +30 -0
  55. package/_types/clients/decorators/public.d.ts.map +1 -1
  56. package/_types/errors/siwe.d.ts +13 -0
  57. package/_types/errors/siwe.d.ts.map +1 -0
  58. package/_types/errors/version.d.ts +1 -1
  59. package/_types/siwe/index.d.ts +8 -0
  60. package/_types/siwe/index.d.ts.map +1 -0
  61. package/_types/utils/siwe/createSiweMessage.d.ts +24 -0
  62. package/_types/utils/siwe/createSiweMessage.d.ts.map +1 -0
  63. package/_types/utils/siwe/generateSiweNonce.d.ts +12 -0
  64. package/_types/utils/siwe/generateSiweNonce.d.ts.map +1 -0
  65. package/_types/utils/siwe/parseSiweMessage.d.ts +11 -0
  66. package/_types/utils/siwe/parseSiweMessage.d.ts.map +1 -0
  67. package/_types/utils/siwe/types.d.ts +61 -0
  68. package/_types/utils/siwe/types.d.ts.map +1 -0
  69. package/_types/utils/siwe/utils.d.ts +2 -0
  70. package/_types/utils/siwe/utils.d.ts.map +1 -0
  71. package/_types/utils/siwe/validateSiweMessage.d.ts +39 -0
  72. package/_types/utils/siwe/validateSiweMessage.d.ts.map +1 -0
  73. package/actions/public/verifyMessage.ts +11 -8
  74. package/actions/siwe/verifySiweMessage.ts +90 -0
  75. package/chains/definitions/flowPreviewnet.ts +1 -1
  76. package/chains/definitions/metis.ts +2 -1
  77. package/clients/decorators/public.ts +37 -0
  78. package/errors/siwe.ts +20 -0
  79. package/errors/version.ts +1 -1
  80. package/package.json +9 -1
  81. package/siwe/index.ts +29 -0
  82. package/siwe/package.json +6 -0
  83. package/utils/siwe/createSiweMessage.ts +168 -0
  84. package/utils/siwe/generateSiweNonce.ts +15 -0
  85. package/utils/siwe/parseSiweMessage.ts +55 -0
  86. package/utils/siwe/types.ts +61 -0
  87. package/utils/siwe/utils.ts +51 -0
  88. package/utils/siwe/validateSiweMessage.ts +70 -0
@@ -0,0 +1,61 @@
1
+ import type { Address } from 'abitype'
2
+
3
+ /**
4
+ * @description EIP-4361 message fields
5
+ *
6
+ * @see https://eips.ethereum.org/EIPS/eip-4361
7
+ */
8
+ export type SiweMessage = {
9
+ /**
10
+ * The Ethereum address performing the signing.
11
+ */
12
+ address: Address
13
+ /**
14
+ * The [EIP-155](https://eips.ethereum.org/EIPS/eip-155) Chain ID to which the session is bound,
15
+ */
16
+ chainId: number
17
+ /**
18
+ * [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority that is requesting the signing.
19
+ */
20
+ domain: string
21
+ /**
22
+ * Time when the signed authentication message is no longer valid.
23
+ */
24
+ expirationTime?: Date | undefined
25
+ /**
26
+ * Time when the message was generated, typically the current time.
27
+ */
28
+ issuedAt?: Date | undefined
29
+ /**
30
+ * A random string typically chosen by the relying party and used to prevent replay attacks.
31
+ */
32
+ nonce: string
33
+ /**
34
+ * Time when the signed authentication message will become valid.
35
+ */
36
+ notBefore?: Date | undefined
37
+ /**
38
+ * A system-specific identifier that may be used to uniquely refer to the sign-in request.
39
+ */
40
+ requestId?: string | undefined
41
+ /**
42
+ * A list of information or references to information the user wishes to have resolved as part of authentication by the relying party.
43
+ */
44
+ resources?: string[] | undefined
45
+ /**
46
+ * [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme of the origin of the request.
47
+ */
48
+ scheme?: string | undefined
49
+ /**
50
+ * A human-readable ASCII assertion that the user will sign.
51
+ */
52
+ statement?: string | undefined
53
+ /**
54
+ * [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) URI referring to the resource that is the subject of the signing (as in the subject of a claim).
55
+ */
56
+ uri: string
57
+ /**
58
+ * The current version of the SIWE Message.
59
+ */
60
+ version: '1'
61
+ }
@@ -0,0 +1,51 @@
1
+ export function isUri(value: string) {
2
+ // based on https://github.com/ogt/valid-url
3
+
4
+ // check for illegal characters
5
+ if (/[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i.test(value))
6
+ return false
7
+
8
+ // check for hex escapes that aren't complete
9
+ if (/%[^0-9a-f]/i.test(value)) return false
10
+ if (/%[0-9a-f](:?[^0-9a-f]|$)/i.test(value)) return false
11
+
12
+ // from RFC 3986
13
+ const splitted = splitUri(value)
14
+ const scheme = splitted[1]
15
+ const authority = splitted[2]
16
+ const path = splitted[3]
17
+ const query = splitted[4]
18
+ const fragment = splitted[5]
19
+
20
+ // scheme and path are required, though the path can be empty
21
+ if (!(scheme?.length && path.length >= 0)) return false
22
+
23
+ // if authority is present, the path must be empty or begin with a /
24
+ if (authority?.length) {
25
+ if (!(path.length === 0 || /^\//.test(path))) return false
26
+ } else {
27
+ // if authority is not present, the path must not start with //
28
+ if (/^\/\//.test(path)) return false
29
+ }
30
+
31
+ // scheme must begin with a letter, then consist of letters, digits, +, ., or -
32
+ if (!/^[a-z][a-z0-9\+\-\.]*$/.test(scheme.toLowerCase())) return false
33
+
34
+ let out = ''
35
+ // re-assemble the URL per section 5.3 in RFC 3986
36
+ out += `${scheme}:`
37
+ if (authority?.length) out += `//${authority}`
38
+
39
+ out += path
40
+
41
+ if (query?.length) out += `?${query}`
42
+ if (fragment?.length) out += `#${fragment}`
43
+
44
+ return out
45
+ }
46
+
47
+ function splitUri(value: string) {
48
+ return value.match(
49
+ /(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/,
50
+ )!
51
+ }
@@ -0,0 +1,70 @@
1
+ import type { Address } from 'abitype'
2
+
3
+ import type { ExactPartial } from '../../types/utils.js'
4
+ import { isAddressEqual } from '../address/isAddressEqual.js'
5
+ import type { SiweMessage } from './types.js'
6
+
7
+ export type ValidateSiweMessageParameters = {
8
+ /**
9
+ * Ethereum address to check against.
10
+ */
11
+ address?: Address | undefined
12
+ /**
13
+ * [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority to check against.
14
+ */
15
+ domain?: string | undefined
16
+ /**
17
+ * EIP-4361 message fields.
18
+ */
19
+ message: ExactPartial<SiweMessage>
20
+ /**
21
+ * Random string to check against.
22
+ */
23
+ nonce?: string | undefined
24
+ /**
25
+ * [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme to check against.
26
+ */
27
+ scheme?: string | undefined
28
+ /**
29
+ * Current time to check optional `expirationTime` and `notBefore` fields.
30
+ *
31
+ * @default new Date()
32
+ */
33
+ time?: Date | undefined
34
+ }
35
+
36
+ export type ValidateSiweMessageReturnType = boolean
37
+
38
+ /**
39
+ * @description Validates EIP-4361 message.
40
+ *
41
+ * @see https://eips.ethereum.org/EIPS/eip-4361
42
+ */
43
+ export function validateSiweMessage(
44
+ parameters: ValidateSiweMessageParameters,
45
+ ): ValidateSiweMessageReturnType {
46
+ const {
47
+ address,
48
+ domain,
49
+ message,
50
+ nonce,
51
+ scheme,
52
+ time = new Date(),
53
+ } = parameters
54
+
55
+ if (domain && message.domain !== domain) return false
56
+ if (nonce && message.nonce !== nonce) return false
57
+ if (scheme && message.scheme !== scheme) return false
58
+
59
+ if (message.expirationTime && time >= message.expirationTime) return false
60
+ if (message.notBefore && time < message.notBefore) return false
61
+
62
+ try {
63
+ if (!message.address) return false
64
+ if (address && !isAddressEqual(message.address, address)) return false
65
+ } catch {
66
+ return false
67
+ }
68
+
69
+ return true
70
+ }