@metamask-previews/utils 11.12.1-preview-9962b7e

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 (112) hide show
  1. package/CHANGELOG.md +584 -0
  2. package/LICENSE +15 -0
  3. package/README.md +106 -0
  4. package/dist/assert.d.ts +61 -0
  5. package/dist/assert.d.ts.map +1 -0
  6. package/dist/assert.js +115 -0
  7. package/dist/assert.js.map +1 -0
  8. package/dist/base64.d.ts +25 -0
  9. package/dist/base64.d.ts.map +1 -0
  10. package/dist/base64.js +30 -0
  11. package/dist/base64.js.map +1 -0
  12. package/dist/bytes.d.ts +198 -0
  13. package/dist/bytes.d.ts.map +1 -0
  14. package/dist/bytes.js +406 -0
  15. package/dist/bytes.js.map +1 -0
  16. package/dist/caip-types.d.ts +294 -0
  17. package/dist/caip-types.d.ts.map +1 -0
  18. package/dist/caip-types.js +369 -0
  19. package/dist/caip-types.js.map +1 -0
  20. package/dist/checksum.d.ts +2 -0
  21. package/dist/checksum.d.ts.map +1 -0
  22. package/dist/checksum.js +4 -0
  23. package/dist/checksum.js.map +1 -0
  24. package/dist/coercers.d.ts +97 -0
  25. package/dist/coercers.d.ts.map +1 -0
  26. package/dist/coercers.js +159 -0
  27. package/dist/coercers.js.map +1 -0
  28. package/dist/collections.d.ts +39 -0
  29. package/dist/collections.d.ts.map +1 -0
  30. package/dist/collections.js +105 -0
  31. package/dist/collections.js.map +1 -0
  32. package/dist/encryption-types.d.ts +7 -0
  33. package/dist/encryption-types.d.ts.map +1 -0
  34. package/dist/encryption-types.js +2 -0
  35. package/dist/encryption-types.js.map +1 -0
  36. package/dist/errors.d.ts +68 -0
  37. package/dist/errors.d.ts.map +1 -0
  38. package/dist/errors.js +121 -0
  39. package/dist/errors.js.map +1 -0
  40. package/dist/fs.d.ts +133 -0
  41. package/dist/fs.d.ts.map +1 -0
  42. package/dist/fs.js +210 -0
  43. package/dist/fs.js.map +1 -0
  44. package/dist/hashing.d.ts +28 -0
  45. package/dist/hashing.d.ts.map +1 -0
  46. package/dist/hashing.js +59 -0
  47. package/dist/hashing.js.map +1 -0
  48. package/dist/hex.d.ts +117 -0
  49. package/dist/hex.d.ts.map +1 -0
  50. package/dist/hex.js +174 -0
  51. package/dist/hex.js.map +1 -0
  52. package/dist/index.d.ts +26 -0
  53. package/dist/index.d.ts.map +1 -0
  54. package/dist/index.js +21 -0
  55. package/dist/index.js.map +1 -0
  56. package/dist/json.d.ts +398 -0
  57. package/dist/json.d.ts.map +1 -0
  58. package/dist/json.js +402 -0
  59. package/dist/json.js.map +1 -0
  60. package/dist/keyring.d.ts +243 -0
  61. package/dist/keyring.d.ts.map +1 -0
  62. package/dist/keyring.js +2 -0
  63. package/dist/keyring.js.map +1 -0
  64. package/dist/logging.d.ts +30 -0
  65. package/dist/logging.d.ts.map +1 -0
  66. package/dist/logging.js +35 -0
  67. package/dist/logging.js.map +1 -0
  68. package/dist/misc.d.ts +127 -0
  69. package/dist/misc.d.ts.map +1 -0
  70. package/dist/misc.js +142 -0
  71. package/dist/misc.js.map +1 -0
  72. package/dist/mnemonic.d.ts +14 -0
  73. package/dist/mnemonic.d.ts.map +1 -0
  74. package/dist/mnemonic.js +25 -0
  75. package/dist/mnemonic.js.map +1 -0
  76. package/dist/node.d.ts +3 -0
  77. package/dist/node.d.ts.map +1 -0
  78. package/dist/node.js +3 -0
  79. package/dist/node.js.map +1 -0
  80. package/dist/number.d.ts +74 -0
  81. package/dist/number.d.ts.map +1 -0
  82. package/dist/number.js +95 -0
  83. package/dist/number.js.map +1 -0
  84. package/dist/opaque.d.ts +6 -0
  85. package/dist/opaque.d.ts.map +1 -0
  86. package/dist/opaque.js +2 -0
  87. package/dist/opaque.js.map +1 -0
  88. package/dist/promise.d.ts +45 -0
  89. package/dist/promise.d.ts.map +1 -0
  90. package/dist/promise.js +40 -0
  91. package/dist/promise.js.map +1 -0
  92. package/dist/superstruct.d.ts +20 -0
  93. package/dist/superstruct.d.ts.map +1 -0
  94. package/dist/superstruct.js +24 -0
  95. package/dist/superstruct.js.map +1 -0
  96. package/dist/time.d.ts +49 -0
  97. package/dist/time.d.ts.map +1 -0
  98. package/dist/time.js +62 -0
  99. package/dist/time.js.map +1 -0
  100. package/dist/transaction-types.d.ts +117 -0
  101. package/dist/transaction-types.d.ts.map +1 -0
  102. package/dist/transaction-types.js +2 -0
  103. package/dist/transaction-types.js.map +1 -0
  104. package/dist/unitsConversion.d.ts +80 -0
  105. package/dist/unitsConversion.d.ts.map +1 -0
  106. package/dist/unitsConversion.js +209 -0
  107. package/dist/unitsConversion.js.map +1 -0
  108. package/dist/versions.d.ts +101 -0
  109. package/dist/versions.d.ts.map +1 -0
  110. package/dist/versions.js +85 -0
  111. package/dist/versions.js.map +1 -0
  112. package/package.json +122 -0
@@ -0,0 +1,243 @@
1
+ import type { TypedTransaction, LegacyTxData } from '@ethereumjs/tx';
2
+ import type { Eip1024EncryptedData } from './encryption-types.js';
3
+ import type { Hex } from './hex.js';
4
+ import type { Json } from './json.js';
5
+ /**
6
+ * A Keyring class.
7
+ *
8
+ * This type is used to validate the constructor signature and the `type`
9
+ * static property on Keyring classes. See the {@link Keyring} type for more
10
+ * information.
11
+ *
12
+ * @deprecated This type has been moved to the `@metamask/keyring-utils` package.
13
+ * See {@link https://github.com/MetaMask/accounts/tree/main/packages/keyring-utils Keyring Utils}.
14
+ */
15
+ export type KeyringClass<State extends Json> = {
16
+ /**
17
+ * The Keyring constructor. Takes a single parameter, an "options" object.
18
+ * See the documentation for the specific keyring for more information about
19
+ * what these options are.
20
+ *
21
+ * @param options - The constructor options. Differs between keyring
22
+ * implementations.
23
+ */
24
+ new (options?: Record<string, unknown>): Keyring<State>;
25
+ /**
26
+ * The name of this type of keyring. This must uniquely identify the
27
+ * keyring type.
28
+ */
29
+ type: string;
30
+ };
31
+ /**
32
+ * A keyring is something that can sign messages. Keyrings are used to add new
33
+ * signing strategies; each strategy is a new keyring.
34
+ *
35
+ * Each keyring manages a collection of key pairs, which we call "accounts".
36
+ * Each account is referred to by its "address", which is a unique identifier
37
+ * derived from the public key. The address is always a "0x"-prefixed
38
+ * hexidecimal string.
39
+ *
40
+ * The keyring might store the private key for each account as well, but it's
41
+ * not guaranteed. Some keyrings delegate signing, so they don't need the
42
+ * private key directly. The keyring (and in particular the keyring state)
43
+ * should be treated with care though, just in case it does contain sensitive
44
+ * material such as a private key.
45
+ *
46
+ * @deprecated This type has been moved to the `@metamask/keyring-utils` package.
47
+ * See {@link https://github.com/MetaMask/accounts/tree/main/packages/keyring-utils Keyring Utils}.
48
+ */
49
+ export type Keyring<State extends Json> = {
50
+ /**
51
+ * The name of this type of keyring. This must match the `type` property of
52
+ * the keyring class.
53
+ */
54
+ type: string;
55
+ /**
56
+ * Get the addresses for all accounts in this keyring.
57
+ *
58
+ * @returns A list of the account addresses for this keyring
59
+ */
60
+ getAccounts(): Promise<Hex[]>;
61
+ /**
62
+ * Add an account to the keyring.
63
+ *
64
+ * @param number - The number of accounts to add. Usually defaults to 1.
65
+ * @returns A list of the newly added account addresses.
66
+ */
67
+ addAccounts(number: number): Promise<Hex[]>;
68
+ /**
69
+ * Serialize the keyring state as a JSON-serializable object.
70
+ *
71
+ * @returns A JSON-serializable representation of the keyring state.
72
+ */
73
+ serialize(): Promise<State>;
74
+ /**
75
+ * Deserialize the given keyring state, overwriting any existing state with
76
+ * the serialized state provided.
77
+ *
78
+ * @param state - A JSON-serializable representation of the keyring state.
79
+ */
80
+ deserialize(state: State): Promise<void>;
81
+ /**
82
+ * Method to include asynchronous configuration.
83
+ */
84
+ init?(): Promise<void>;
85
+ /**
86
+ * Remove an account from the keyring.
87
+ *
88
+ * @param address - The address of the account to remove.
89
+ */
90
+ removeAccount?(address: Hex): void;
91
+ /**
92
+ * Export the private key for one of the keyring accounts.
93
+ *
94
+ * Some keyrings accept an "options" parameter as well. See the documentation
95
+ * for the specific keyring for more information about what these options
96
+ * are. For some keyrings, the options parameter is used to allow exporting a
97
+ * private key that is derived from the given account, rather than exporting
98
+ * that account's private key directly.
99
+ *
100
+ * @param address - The address of the account to export.
101
+ * @param options - Export options; differs between keyrings.
102
+ * @returns The non-prefixed, hex-encoded private key that was requested.
103
+ */
104
+ exportAccount?(address: Hex, options?: Record<string, unknown>): Promise<string>;
105
+ /**
106
+ * Get the "app key" address for the given account and origin. An app key is
107
+ * an application-specific key pair. See {@link https://eips.ethereum.org/EIPS/eip-1775|EIP-1775}
108
+ * for more information. The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin}
109
+ * is used as the unique identifier for the application, and it's used as
110
+ * part of the key derivation process.
111
+ *
112
+ * @param address - The address of the account the app key is derived from.
113
+ * @param origin - The origin of the application.
114
+ * @returns The address of the app key for the given account and origin.
115
+ */
116
+ getAppKeyAddress?(address: Hex, origin: string): Promise<Hex>;
117
+ /**
118
+ * Sign a transaction. This is equivalent to the `eth_signTransaction`
119
+ * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for
120
+ * more details.
121
+ *
122
+ * Some keyrings accept an "options" parameter as well. See the documentation
123
+ * for the specific keyring for more information about what these options
124
+ * are. For some keyrings, the options parameter can even change which key is
125
+ * used for signing (e.g. signing with app keys).
126
+ *
127
+ * @param address - The address of the account to use for signing.
128
+ * @param transaction - The transaction to sign.
129
+ * @param options - Signing options; differs between keyrings.
130
+ * @returns The signed transaction.
131
+ */
132
+ signTransaction?(address: Hex, transaction: TypedTransaction, options?: Record<string, unknown>): Promise<LegacyTxData>;
133
+ /**
134
+ * Sign a message. This is equivalent to an older version of the the
135
+ * `eth_sign` Ethereum JSON-RPC method. The message is signed using ECDSA,
136
+ * using the curve secp256k1 the Keccak-256 hash function.
137
+ *
138
+ * For more information about this method and why we still support it, see
139
+ * the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.
140
+ *
141
+ * Some keyrings accept an "options" parameter as well. See the documentation
142
+ * for the specific keyring for more information about what these options
143
+ * are. For some keyrings, the options parameter can even change which key is
144
+ * used for signing (e.g. signing with app keys).
145
+ *
146
+ * @param address - The address of the account to use for signing.
147
+ * @param message - The message to sign.
148
+ * @param options - Signing options; differs between keyrings.
149
+ * @returns The signed message.
150
+ */
151
+ signMessage?(address: Hex, message: string, options?: Record<string, unknown>): Promise<string>;
152
+ /**
153
+ * Sign an EIP-7702 authorization. This is a signing method for authorizing a
154
+ * specific contract on a specific chain.
155
+ *
156
+ * @param address - The address of the account to use for signing.
157
+ * @param authorization - An array containing the chain ID, contract address,
158
+ * and nonce.
159
+ * @param options - Signing options; differs between keyrings.
160
+ * @returns The signed authorization as a hex string.
161
+ */
162
+ signEip7702Authorization?(address: Hex, authorization: [chainId: number, contractAddress: Hex, nonce: number], options?: Record<string, unknown>): Promise<string>;
163
+ /**
164
+ * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC
165
+ * method, which is exposed by MetaMask as the method `personal_sign`. See
166
+ * the Ethereum JSON-RPC API documentation for more details.
167
+ *
168
+ * For more information about this method and why we call it `personal_sign`,
169
+ * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.
170
+ *
171
+ * Some keyrings accept an "options" parameter as well. See the documentation
172
+ * for the specific keyring for more information about what these options
173
+ * are. For some keyrings, the options parameter can even change which key is
174
+ * used for signing (e.g. signing with app keys).
175
+ *
176
+ * @param address - The address of the account to use for signing.
177
+ * @param message - The message to sign.
178
+ * @param options - Signing options; differs between keyrings.
179
+ * @returns The signed message.
180
+ */
181
+ signPersonalMessage?(address: Hex, message: Hex, options?: {
182
+ version?: string;
183
+ } & Record<string, unknown>): Promise<string>;
184
+ /**
185
+ * Sign a message. This is equivalent to the `eth_signTypedData` Ethereum
186
+ * JSON-RPC method. See {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712}
187
+ * for more details.
188
+ *
189
+ * The "version" option dictates which version of `eth_signTypedData` is
190
+ * used. The latest version reflects the specification most closely, whereas
191
+ * earlier versions reflect earlier drafts of the specification that are
192
+ * still supported for backwards-compatibility reasons. For more information
193
+ * about why we support multiple versions, see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.
194
+ *
195
+ * Some keyrings accept additional options as well. See the documentation for
196
+ * the specific keyring for more information about what these options are.
197
+ * For some keyrings, the options parameter can even change which key is used
198
+ * for signing (e.g. signing with app keys).
199
+ *
200
+ * @param address - The address of the account to use for signing.
201
+ * @param typedData - The data to sign.
202
+ * @param options - Signing options; differs between keyrings.
203
+ * @returns The signed message.
204
+ */
205
+ signTypedData?(address: Hex, typedData: Record<string, unknown>, options?: Record<string, unknown>): Promise<string>;
206
+ /**
207
+ * Get a public key to use for encryption. This is equivalent to the
208
+ * ` eth_getEncryptionPublicKey` JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-getencryptionpublickey|MetaMask Docs}
209
+ * for more information.
210
+ *
211
+ * Some keyrings accept an "options" parameter as well. See the documentation
212
+ * for the specific keyring for more information about what these options
213
+ * are. For some keyrings, the options parameter can even change which key is
214
+ * used (e.g. encrypting with app keys).
215
+ *
216
+ * @param account - The address of the account you want the encryption key for.
217
+ * @param options - Options; differs between keyrings.
218
+ */
219
+ getEncryptionPublicKey?(account: Hex, options?: Record<string, unknown>): Promise<string>;
220
+ /**
221
+ * Decrypt an encrypted message. This is equivalent to the ` eth_decrypt`
222
+ * JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-decrypt|MetaMask Docs}
223
+ * for more information.
224
+ *
225
+ * @param account - The address of the account you want to use to decrypt
226
+ * the message.
227
+ * @param encryptedData - The encrypted data that you want to decrypt.
228
+ * @returns The decrypted data.
229
+ */
230
+ decryptMessage?(account: Hex, encryptedData: Eip1024EncryptedData): Promise<string>;
231
+ /**
232
+ * Generates the properties for the keyring based on the given
233
+ * BIP39-compliant mnemonic.
234
+ *
235
+ * @returns A promise resolving when the keyring has generated the properties.
236
+ */
237
+ generateRandomMnemonic?(): Promise<void>;
238
+ /**
239
+ * Destroy the keyring.
240
+ */
241
+ destroy?(): Promise<void>;
242
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AAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;;;;;;;;OAYG;IACH,sBAAsB,CAAC,CACrB,OAAO,EAAE,GAAG,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;;;;;OASG;IACH,cAAc,CAAC,CACb,OAAO,EAAE,GAAG,EACZ,aAAa,EAAE,oBAAoB,GAClC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;OAKG;IACH,sBAAsB,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzC;;OAEG;IACH,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=keyring.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyring.js","sourceRoot":"","sources":["../src/keyring.ts"],"names":[],"mappings":"","sourcesContent":["import type { TypedTransaction, LegacyTxData } from '@ethereumjs/tx';\n\nimport type { Eip1024EncryptedData } from './encryption-types.js';\nimport type { Hex } from './hex.js';\nimport type { Json } from './json.js';\n\n/**\n * A Keyring class.\n *\n * This type is used to validate the constructor signature and the `type`\n * static property on Keyring classes. See the {@link Keyring} type for more\n * information.\n *\n * @deprecated This type has been moved to the `@metamask/keyring-utils` package.\n * See {@link https://github.com/MetaMask/accounts/tree/main/packages/keyring-utils Keyring Utils}.\n */\nexport type KeyringClass<State extends Json> = {\n /**\n * The Keyring constructor. Takes a single parameter, an \"options\" object.\n * See the documentation for the specific keyring for more information about\n * what these options are.\n *\n * @param options - The constructor options. Differs between keyring\n * implementations.\n */\n new (options?: Record<string, unknown>): Keyring<State>;\n\n /**\n * The name of this type of keyring. This must uniquely identify the\n * keyring type.\n */\n type: string;\n};\n\n/**\n * A keyring is something that can sign messages. Keyrings are used to add new\n * signing strategies; each strategy is a new keyring.\n *\n * Each keyring manages a collection of key pairs, which we call \"accounts\".\n * Each account is referred to by its \"address\", which is a unique identifier\n * derived from the public key. The address is always a \"0x\"-prefixed\n * hexidecimal string.\n *\n * The keyring might store the private key for each account as well, but it's\n * not guaranteed. Some keyrings delegate signing, so they don't need the\n * private key directly. The keyring (and in particular the keyring state)\n * should be treated with care though, just in case it does contain sensitive\n * material such as a private key.\n *\n * @deprecated This type has been moved to the `@metamask/keyring-utils` package.\n * See {@link https://github.com/MetaMask/accounts/tree/main/packages/keyring-utils Keyring Utils}.\n */\nexport type Keyring<State extends Json> = {\n /**\n * The name of this type of keyring. This must match the `type` property of\n * the keyring class.\n */\n type: string;\n\n /**\n * Get the addresses for all accounts in this keyring.\n *\n * @returns A list of the account addresses for this keyring\n */\n getAccounts(): Promise<Hex[]>;\n\n /**\n * Add an account to the keyring.\n *\n * @param number - The number of accounts to add. Usually defaults to 1.\n * @returns A list of the newly added account addresses.\n */\n addAccounts(number: number): Promise<Hex[]>;\n\n /**\n * Serialize the keyring state as a JSON-serializable object.\n *\n * @returns A JSON-serializable representation of the keyring state.\n */\n serialize(): Promise<State>;\n\n /**\n * Deserialize the given keyring state, overwriting any existing state with\n * the serialized state provided.\n *\n * @param state - A JSON-serializable representation of the keyring state.\n */\n deserialize(state: State): Promise<void>;\n\n /**\n * Method to include asynchronous configuration.\n */\n init?(): Promise<void>;\n\n /**\n * Remove an account from the keyring.\n *\n * @param address - The address of the account to remove.\n */\n removeAccount?(address: Hex): void;\n\n /**\n * Export the private key for one of the keyring accounts.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter is used to allow exporting a\n * private key that is derived from the given account, rather than exporting\n * that account's private key directly.\n *\n * @param address - The address of the account to export.\n * @param options - Export options; differs between keyrings.\n * @returns The non-prefixed, hex-encoded private key that was requested.\n */\n exportAccount?(\n address: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get the \"app key\" address for the given account and origin. An app key is\n * an application-specific key pair. See {@link https://eips.ethereum.org/EIPS/eip-1775|EIP-1775}\n * for more information. The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin}\n * is used as the unique identifier for the application, and it's used as\n * part of the key derivation process.\n *\n * @param address - The address of the account the app key is derived from.\n * @param origin - The origin of the application.\n * @returns The address of the app key for the given account and origin.\n */\n getAppKeyAddress?(address: Hex, origin: string): Promise<Hex>;\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed transaction.\n */\n signTransaction?(\n address: Hex,\n transaction: TypedTransaction,\n options?: Record<string, unknown>,\n ): Promise<LegacyTxData>;\n\n /**\n * Sign a message. This is equivalent to an older version of the the\n * `eth_sign` Ethereum JSON-RPC method. The message is signed using ECDSA,\n * using the curve secp256k1 the Keccak-256 hash function.\n *\n * For more information about this method and why we still support it, see\n * the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signMessage?(\n address: Hex,\n message: string,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign an EIP-7702 authorization. This is a signing method for authorizing a\n * specific contract on a specific chain.\n *\n * @param address - The address of the account to use for signing.\n * @param authorization - An array containing the chain ID, contract address,\n * and nonce.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed authorization as a hex string.\n */\n signEip7702Authorization?(\n address: Hex,\n authorization: [chainId: number, contractAddress: Hex, nonce: number],\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signPersonalMessage?(\n address: Hex,\n message: Hex,\n options?: { version?: string } & Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` Ethereum\n * JSON-RPC method. See {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712}\n * for more details.\n *\n * The \"version\" option dictates which version of `eth_signTypedData` is\n * used. The latest version reflects the specification most closely, whereas\n * earlier versions reflect earlier drafts of the specification that are\n * still supported for backwards-compatibility reasons. For more information\n * about why we support multiple versions, see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept additional options as well. See the documentation for\n * the specific keyring for more information about what these options are.\n * For some keyrings, the options parameter can even change which key is used\n * for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param typedData - The data to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signTypedData?(\n address: Hex,\n typedData: Record<string, unknown>,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get a public key to use for encryption. This is equivalent to the\n * ` eth_getEncryptionPublicKey` JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-getencryptionpublickey|MetaMask Docs}\n * for more information.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used (e.g. encrypting with app keys).\n *\n * @param account - The address of the account you want the encryption key for.\n * @param options - Options; differs between keyrings.\n */\n getEncryptionPublicKey?(\n account: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Decrypt an encrypted message. This is equivalent to the ` eth_decrypt`\n * JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-decrypt|MetaMask Docs}\n * for more information.\n *\n * @param account - The address of the account you want to use to decrypt\n * the message.\n * @param encryptedData - The encrypted data that you want to decrypt.\n * @returns The decrypted data.\n */\n decryptMessage?(\n account: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string>;\n\n /**\n * Generates the properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @returns A promise resolving when the keyring has generated the properties.\n */\n generateRandomMnemonic?(): Promise<void>;\n\n /**\n * Destroy the keyring.\n */\n destroy?(): Promise<void>;\n};\n"]}
@@ -0,0 +1,30 @@
1
+ import type { Debugger } from 'debug';
2
+ /**
3
+ * Creates a logger via the `debug` library whose log messages will be tagged
4
+ * using the name of your project. By default, such messages will be
5
+ * suppressed, but you can reveal them by setting the `DEBUG` environment
6
+ * variable to `metamask:<projectName>`. You can also set this variable to
7
+ * `metamask:*` if you want to see log messages from all MetaMask projects that
8
+ * are also using this function to create their loggers.
9
+ *
10
+ * @param projectName - The name of your project. This should be the name of
11
+ * your NPM package if you're developing one.
12
+ * @returns An instance of `debug`.
13
+ */
14
+ export declare function createProjectLogger(projectName: string): Debugger;
15
+ /**
16
+ * Creates a logger via the `debug` library which is derived from the logger for
17
+ * the whole project whose log messages will be tagged using the name of your
18
+ * module. By default, such messages will be suppressed, but you can reveal them
19
+ * by setting the `DEBUG` environment variable to
20
+ * `metamask:<projectName>:<moduleName>`. You can also set this variable to
21
+ * `metamask:<projectName>:*` if you want to see log messages from the project,
22
+ * or `metamask:*` if you want to see log messages from all MetaMask projects.
23
+ *
24
+ * @param projectLogger - The logger created via {@link createProjectLogger}.
25
+ * @param moduleName - The name of your module. You could use the name of the
26
+ * file where you're using this logger or some other name.
27
+ * @returns An instance of `debug`.
28
+ */
29
+ export declare function createModuleLogger(projectLogger: Debugger, moduleName: string): Debugger;
30
+ //# sourceMappingURL=logging.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logging.d.ts","sourceRoot":"","sources":["../src/logging.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAKtC;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,QAAQ,CAEjE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAChC,aAAa,EAAE,QAAQ,EACvB,UAAU,EAAE,MAAM,GACjB,QAAQ,CAEV"}
@@ -0,0 +1,35 @@
1
+ import debug from 'debug';
2
+ const globalLogger = debug('metamask');
3
+ /**
4
+ * Creates a logger via the `debug` library whose log messages will be tagged
5
+ * using the name of your project. By default, such messages will be
6
+ * suppressed, but you can reveal them by setting the `DEBUG` environment
7
+ * variable to `metamask:<projectName>`. You can also set this variable to
8
+ * `metamask:*` if you want to see log messages from all MetaMask projects that
9
+ * are also using this function to create their loggers.
10
+ *
11
+ * @param projectName - The name of your project. This should be the name of
12
+ * your NPM package if you're developing one.
13
+ * @returns An instance of `debug`.
14
+ */
15
+ export function createProjectLogger(projectName) {
16
+ return globalLogger.extend(projectName);
17
+ }
18
+ /**
19
+ * Creates a logger via the `debug` library which is derived from the logger for
20
+ * the whole project whose log messages will be tagged using the name of your
21
+ * module. By default, such messages will be suppressed, but you can reveal them
22
+ * by setting the `DEBUG` environment variable to
23
+ * `metamask:<projectName>:<moduleName>`. You can also set this variable to
24
+ * `metamask:<projectName>:*` if you want to see log messages from the project,
25
+ * or `metamask:*` if you want to see log messages from all MetaMask projects.
26
+ *
27
+ * @param projectLogger - The logger created via {@link createProjectLogger}.
28
+ * @param moduleName - The name of your module. You could use the name of the
29
+ * file where you're using this logger or some other name.
30
+ * @returns An instance of `debug`.
31
+ */
32
+ export function createModuleLogger(projectLogger, moduleName) {
33
+ return projectLogger.extend(moduleName);
34
+ }
35
+ //# sourceMappingURL=logging.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logging.js","sourceRoot":"","sources":["../src/logging.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,YAAY,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;AAEvC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,mBAAmB,CAAC,WAAmB;IACrD,OAAO,YAAY,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,kBAAkB,CAChC,aAAuB,EACvB,UAAkB;IAElB,OAAO,aAAa,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["import type { Debugger } from 'debug';\nimport debug from 'debug';\n\nconst globalLogger = debug('metamask');\n\n/**\n * Creates a logger via the `debug` library whose log messages will be tagged\n * using the name of your project. By default, such messages will be\n * suppressed, but you can reveal them by setting the `DEBUG` environment\n * variable to `metamask:<projectName>`. You can also set this variable to\n * `metamask:*` if you want to see log messages from all MetaMask projects that\n * are also using this function to create their loggers.\n *\n * @param projectName - The name of your project. This should be the name of\n * your NPM package if you're developing one.\n * @returns An instance of `debug`.\n */\nexport function createProjectLogger(projectName: string): Debugger {\n return globalLogger.extend(projectName);\n}\n\n/**\n * Creates a logger via the `debug` library which is derived from the logger for\n * the whole project whose log messages will be tagged using the name of your\n * module. By default, such messages will be suppressed, but you can reveal them\n * by setting the `DEBUG` environment variable to\n * `metamask:<projectName>:<moduleName>`. You can also set this variable to\n * `metamask:<projectName>:*` if you want to see log messages from the project,\n * or `metamask:*` if you want to see log messages from all MetaMask projects.\n *\n * @param projectLogger - The logger created via {@link createProjectLogger}.\n * @param moduleName - The name of your module. You could use the name of the\n * file where you're using this logger or some other name.\n * @returns An instance of `debug`.\n */\nexport function createModuleLogger(\n projectLogger: Debugger,\n moduleName: string,\n): Debugger {\n return projectLogger.extend(moduleName);\n}\n"]}
package/dist/misc.d.ts ADDED
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Makes every specified property of the specified object type mutable.
3
+ *
4
+ * @template ObjectValue - The object whose readonly properties to make mutable.
5
+ * @template TargetKey - The property key(s) to make mutable.
6
+ */
7
+ export type Mutable<ObjectValue extends Record<string, unknown>, TargetKey extends keyof ObjectValue> = {
8
+ -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];
9
+ } & {
10
+ [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];
11
+ };
12
+ /**
13
+ * Get a type representing the public interface of the given type. The
14
+ * returned type will have all public properties, but will omit private
15
+ * properties.
16
+ *
17
+ * @template Interface - The interface to return a public representation of.
18
+ */
19
+ export type PublicInterface<Interface> = Pick<Interface, keyof Interface>;
20
+ /**
21
+ * Useful for representing some value that _might_ be present and / or complete.
22
+ *
23
+ * @template Value - The value that might be present or complete.
24
+ */
25
+ export type PartialOrAbsent<Value> = Partial<Value> | null | undefined;
26
+ /**
27
+ * Like {@link Array}, but always non-empty.
28
+ *
29
+ * @template Element - The non-empty array member type.
30
+ */
31
+ export type NonEmptyArray<Element> = [Element, ...Element[]];
32
+ /**
33
+ * A JavaScript object that is not `null`, a function, or an array.
34
+ */
35
+ export type RuntimeObject = Record<PropertyKey, unknown>;
36
+ /**
37
+ * A {@link NonEmptyArray} type guard.
38
+ *
39
+ * @template Element - The non-empty array member type.
40
+ * @param value - The value to check.
41
+ * @returns Whether the value is a non-empty array.
42
+ */
43
+ export declare function isNonEmptyArray<Element>(value: Element[]): value is NonEmptyArray<Element>;
44
+ /**
45
+ * Type guard for "nullishness".
46
+ *
47
+ * @param value - Any value.
48
+ * @returns `true` if the value is null or undefined, `false` otherwise.
49
+ */
50
+ export declare function isNullOrUndefined(value: unknown): value is null | undefined;
51
+ /**
52
+ * A type guard for {@link RuntimeObject}.
53
+ *
54
+ * @param value - The value to check.
55
+ * @returns Whether the specified value has a runtime type of `object` and is
56
+ * neither `null` nor an `Array`.
57
+ */
58
+ export declare function isObject(value: unknown): value is RuntimeObject;
59
+ /**
60
+ * A type guard for ensuring an object has a property.
61
+ *
62
+ * @param objectToCheck - The object to check.
63
+ * @param name - The property name to check for.
64
+ * @returns Whether the specified object has an own property with the specified
65
+ * name, regardless of whether it is enumerable or not.
66
+ */
67
+ export declare const hasProperty: <ObjectToCheck extends Object, Property extends PropertyKey>(objectToCheck: ObjectToCheck, name: Property) => objectToCheck is ObjectToCheck & Record<Property, Property extends keyof ObjectToCheck ? ObjectToCheck[Property] : unknown>;
68
+ /**
69
+ * `Object.getOwnPropertyNames()` is intentionally generic: it returns the
70
+ * immediate property names of an object, but it cannot make guarantees about
71
+ * the contents of that object, so the type of the property names is merely
72
+ * `string[]`. While this is technically accurate, it is also unnecessary if we
73
+ * have an object with a type that we own (such as an enum).
74
+ *
75
+ * @param object - The plain object.
76
+ * @returns The own property names of the object which are assigned a type
77
+ * derived from the object itself.
78
+ */
79
+ export declare function getKnownPropertyNames<Key extends PropertyKey>(object: Partial<Record<Key, any>>): Key[];
80
+ export type PlainObject = Record<number | string | symbol, unknown>;
81
+ /**
82
+ * Predefined sizes (in Bytes) of specific parts of JSON structure.
83
+ */
84
+ export declare enum JsonSize {
85
+ Null = 4,
86
+ Comma = 1,
87
+ Wrapper = 1,
88
+ True = 4,
89
+ False = 5,
90
+ Quote = 1,
91
+ Colon = 1,
92
+ Date = 24
93
+ }
94
+ /**
95
+ * Regular expression with pattern matching for (special) escaped characters.
96
+ */
97
+ export declare const ESCAPE_CHARACTERS_REGEXP: RegExp;
98
+ /**
99
+ * Check if the value is plain object.
100
+ *
101
+ * @param value - Value to be checked.
102
+ * @returns True if an object is the plain JavaScript object,
103
+ * false if the object is not plain (e.g. function).
104
+ */
105
+ export declare function isPlainObject(value: unknown): value is PlainObject;
106
+ /**
107
+ * Check if character is ASCII.
108
+ *
109
+ * @param character - Character.
110
+ * @returns True if a character code is ASCII, false if not.
111
+ */
112
+ export declare function isASCII(character: string): boolean;
113
+ /**
114
+ * Calculate string size.
115
+ *
116
+ * @param value - String value to calculate size.
117
+ * @returns Number of bytes used to store whole string value.
118
+ */
119
+ export declare function calculateStringSize(value: string): number;
120
+ /**
121
+ * Calculate size of a number ofter JSON serialization.
122
+ *
123
+ * @param value - Number value to calculate size.
124
+ * @returns Number of bytes used to store whole number in JSON.
125
+ */
126
+ export declare function calculateNumberSize(value: number): number;
127
+ //# sourceMappingURL=misc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.d.ts","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,MAAM,OAAO,CACjB,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC3C,SAAS,SAAS,MAAM,WAAW,IACjC;IACF,CAAC,UAAU,GAAG,IAAI,MAAM,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC;CACxE,GAAG;KACD,GAAG,IAAI,MAAM,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC;CAC9D,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,EAAE,MAAM,SAAS,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,MAAM,eAAe,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,GAA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GAEG;AACH,eAAO,MAAM,wBAAwB,QAAoB,CAAC;AAE1D;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAelE;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,MAAM,WAExC;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAUzD;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEzD"}
package/dist/misc.js ADDED
@@ -0,0 +1,142 @@
1
+ //
2
+ // Types
3
+ //
4
+ //
5
+ // Type Guards
6
+ //
7
+ /**
8
+ * A {@link NonEmptyArray} type guard.
9
+ *
10
+ * @template Element - The non-empty array member type.
11
+ * @param value - The value to check.
12
+ * @returns Whether the value is a non-empty array.
13
+ */
14
+ export function isNonEmptyArray(value) {
15
+ return Array.isArray(value) && value.length > 0;
16
+ }
17
+ /**
18
+ * Type guard for "nullishness".
19
+ *
20
+ * @param value - Any value.
21
+ * @returns `true` if the value is null or undefined, `false` otherwise.
22
+ */
23
+ export function isNullOrUndefined(value) {
24
+ return value === null || value === undefined;
25
+ }
26
+ /**
27
+ * A type guard for {@link RuntimeObject}.
28
+ *
29
+ * @param value - The value to check.
30
+ * @returns Whether the specified value has a runtime type of `object` and is
31
+ * neither `null` nor an `Array`.
32
+ */
33
+ export function isObject(value) {
34
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
35
+ }
36
+ //
37
+ // Other utility functions
38
+ //
39
+ /**
40
+ * A type guard for ensuring an object has a property.
41
+ *
42
+ * @param objectToCheck - The object to check.
43
+ * @param name - The property name to check for.
44
+ * @returns Whether the specified object has an own property with the specified
45
+ * name, regardless of whether it is enumerable or not.
46
+ */
47
+ export const hasProperty = (objectToCheck, name) => Object.hasOwnProperty.call(objectToCheck, name);
48
+ /**
49
+ * `Object.getOwnPropertyNames()` is intentionally generic: it returns the
50
+ * immediate property names of an object, but it cannot make guarantees about
51
+ * the contents of that object, so the type of the property names is merely
52
+ * `string[]`. While this is technically accurate, it is also unnecessary if we
53
+ * have an object with a type that we own (such as an enum).
54
+ *
55
+ * @param object - The plain object.
56
+ * @returns The own property names of the object which are assigned a type
57
+ * derived from the object itself.
58
+ */
59
+ export function getKnownPropertyNames(object) {
60
+ return Object.getOwnPropertyNames(object);
61
+ }
62
+ /**
63
+ * Predefined sizes (in Bytes) of specific parts of JSON structure.
64
+ */
65
+ /* eslint-disable @typescript-eslint/no-duplicate-enum-values --
66
+ These are byte sizes, so collisions are meaningful rather than mistakes:
67
+ a comma, a brace, a quote and a colon are all one byte, and `null` and
68
+ `true` are both four characters. */
69
+ export var JsonSize;
70
+ (function (JsonSize) {
71
+ JsonSize[JsonSize["Null"] = 4] = "Null";
72
+ JsonSize[JsonSize["Comma"] = 1] = "Comma";
73
+ JsonSize[JsonSize["Wrapper"] = 1] = "Wrapper";
74
+ JsonSize[JsonSize["True"] = 4] = "True";
75
+ JsonSize[JsonSize["False"] = 5] = "False";
76
+ JsonSize[JsonSize["Quote"] = 1] = "Quote";
77
+ JsonSize[JsonSize["Colon"] = 1] = "Colon";
78
+ // eslint-disable-next-line @typescript-eslint/no-shadow
79
+ JsonSize[JsonSize["Date"] = 24] = "Date";
80
+ })(JsonSize || (JsonSize = {}));
81
+ /* eslint-enable @typescript-eslint/no-duplicate-enum-values */
82
+ /**
83
+ * Regular expression with pattern matching for (special) escaped characters.
84
+ */
85
+ export const ESCAPE_CHARACTERS_REGEXP = /"|\\|\n|\r|\t/gu;
86
+ /**
87
+ * Check if the value is plain object.
88
+ *
89
+ * @param value - Value to be checked.
90
+ * @returns True if an object is the plain JavaScript object,
91
+ * false if the object is not plain (e.g. function).
92
+ */
93
+ export function isPlainObject(value) {
94
+ if (typeof value !== 'object' || value === null) {
95
+ return false;
96
+ }
97
+ try {
98
+ let proto = value;
99
+ while (Object.getPrototypeOf(proto) !== null) {
100
+ proto = Object.getPrototypeOf(proto);
101
+ }
102
+ return Object.getPrototypeOf(value) === proto;
103
+ }
104
+ catch {
105
+ return false;
106
+ }
107
+ }
108
+ /**
109
+ * Check if character is ASCII.
110
+ *
111
+ * @param character - Character.
112
+ * @returns True if a character code is ASCII, false if not.
113
+ */
114
+ export function isASCII(character) {
115
+ return character.charCodeAt(0) <= 127;
116
+ }
117
+ /**
118
+ * Calculate string size.
119
+ *
120
+ * @param value - String value to calculate size.
121
+ * @returns Number of bytes used to store whole string value.
122
+ */
123
+ export function calculateStringSize(value) {
124
+ const size = value.split('').reduce((total, character) => {
125
+ if (isASCII(character)) {
126
+ return total + 1;
127
+ }
128
+ return total + 2;
129
+ }, 0);
130
+ // Also detect characters that need backslash escape
131
+ return size + (value.match(ESCAPE_CHARACTERS_REGEXP) ?? []).length;
132
+ }
133
+ /**
134
+ * Calculate size of a number ofter JSON serialization.
135
+ *
136
+ * @param value - Number value to calculate size.
137
+ * @returns Number of bytes used to store whole number in JSON.
138
+ */
139
+ export function calculateNumberSize(value) {
140
+ return value.toString().length;
141
+ }
142
+ //# sourceMappingURL=misc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":"AAAA,EAAE;AACF,QAAQ;AACR,EAAE;AA6CF,EAAE;AACF,cAAc;AACd,EAAE;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC7B,KAAgB;IAEhB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,CAAC,KAAK,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AChD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,SAAiB;IACvC,OAAO,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;AACxC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa;IAC/C,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE;QACvD,IAAI,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;YACvB,OAAO,KAAK,GAAG,CAAC,CAAC;QACnB,CAAC;QACD,OAAO,KAAK,GAAG,CAAC,CAAC;IACnB,CAAC,EAAE,CAAC,CAAC,CAAC;IAEN,oDAAoD;IACpD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,wBAAwB,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC;AACrE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa;IAC/C,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC;AACjC,CAAC","sourcesContent":["//\n// Types\n//\n\n/**\n * Makes every specified property of the specified object type mutable.\n *\n * @template ObjectValue - The object whose readonly properties to make mutable.\n * @template TargetKey - The property key(s) to make mutable.\n */\nexport type Mutable<\n ObjectValue extends Record<string, unknown>,\n TargetKey extends keyof ObjectValue,\n> = {\n -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];\n} & {\n [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];\n};\n\n/**\n * Get a type representing the public interface of the given type. The\n * returned type will have all public properties, but will omit private\n * properties.\n *\n * @template Interface - The interface to return a public representation of.\n */\nexport type PublicInterface<Interface> = Pick<Interface, keyof Interface>;\n\n/**\n * Useful for representing some value that _might_ be present and / or complete.\n *\n * @template Value - The value that might be present or complete.\n */\nexport type PartialOrAbsent<Value> = Partial<Value> | null | undefined;\n\n/**\n * Like {@link Array}, but always non-empty.\n *\n * @template Element - The non-empty array member type.\n */\nexport type NonEmptyArray<Element> = [Element, ...Element[]];\n\n/**\n * A JavaScript object that is not `null`, a function, or an array.\n */\nexport type RuntimeObject = Record<PropertyKey, unknown>;\n\n//\n// Type Guards\n//\n\n/**\n * A {@link NonEmptyArray} type guard.\n *\n * @template Element - The non-empty array member type.\n * @param value - The value to check.\n * @returns Whether the value is a non-empty array.\n */\nexport function isNonEmptyArray<Element>(\n value: Element[],\n): value is NonEmptyArray<Element> {\n return Array.isArray(value) && value.length > 0;\n}\n\n/**\n * Type guard for \"nullishness\".\n *\n * @param value - Any value.\n * @returns `true` if the value is null or undefined, `false` otherwise.\n */\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * A type guard for {@link RuntimeObject}.\n *\n * @param value - The value to check.\n * @returns Whether the specified value has a runtime type of `object` and is\n * neither `null` nor an `Array`.\n */\nexport function isObject(value: unknown): value is RuntimeObject {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\n//\n// Other utility functions\n//\n\n/**\n * A type guard for ensuring an object has a property.\n *\n * @param objectToCheck - The object to check.\n * @param name - The property name to check for.\n * @returns Whether the specified object has an own property with the specified\n * name, regardless of whether it is enumerable or not.\n */\nexport const hasProperty = <\n // eslint-disable-next-line @typescript-eslint/no-wrapper-object-types -- `Object` is deliberate: it accepts boxed primitives, so callers can pass a string or number. Narrowing to `object` would be a breaking change.\n ObjectToCheck extends Object,\n Property extends PropertyKey,\n>(\n objectToCheck: ObjectToCheck,\n name: Property,\n): objectToCheck is ObjectToCheck &\n Record<\n Property,\n Property extends keyof ObjectToCheck ? ObjectToCheck[Property] : unknown\n > => Object.hasOwnProperty.call(objectToCheck, name);\n\n/**\n * `Object.getOwnPropertyNames()` is intentionally generic: it returns the\n * immediate property names of an object, but it cannot make guarantees about\n * the contents of that object, so the type of the property names is merely\n * `string[]`. While this is technically accurate, it is also unnecessary if we\n * have an object with a type that we own (such as an enum).\n *\n * @param object - The plain object.\n * @returns The own property names of the object which are assigned a type\n * derived from the object itself.\n */\nexport function getKnownPropertyNames<Key extends PropertyKey>(\n object: Partial<Record<Key, any>>,\n): Key[] {\n return Object.getOwnPropertyNames(object) as Key[];\n}\n\nexport type PlainObject = Record<number | string | symbol, unknown>;\n\n/**\n * Predefined sizes (in Bytes) of specific parts of JSON structure.\n */\n/* eslint-disable @typescript-eslint/no-duplicate-enum-values --\n These are byte sizes, so collisions are meaningful rather than mistakes:\n a comma, a brace, a quote and a colon are all one byte, and `null` and\n `true` are both four characters. */\nexport enum JsonSize {\n Null = 4,\n Comma = 1,\n Wrapper = 1,\n True = 4,\n False = 5,\n Quote = 1,\n Colon = 1,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Date = 24,\n}\n/* eslint-enable @typescript-eslint/no-duplicate-enum-values */\n\n/**\n * Regular expression with pattern matching for (special) escaped characters.\n */\nexport const ESCAPE_CHARACTERS_REGEXP = /\"|\\\\|\\n|\\r|\\t/gu;\n\n/**\n * Check if the value is plain object.\n *\n * @param value - Value to be checked.\n * @returns True if an object is the plain JavaScript object,\n * false if the object is not plain (e.g. function).\n */\nexport function isPlainObject(value: unknown): value is PlainObject {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n try {\n let proto = value;\n while (Object.getPrototypeOf(proto) !== null) {\n proto = Object.getPrototypeOf(proto);\n }\n\n return Object.getPrototypeOf(value) === proto;\n } catch {\n return false;\n }\n}\n\n/**\n * Check if character is ASCII.\n *\n * @param character - Character.\n * @returns True if a character code is ASCII, false if not.\n */\nexport function isASCII(character: string) {\n return character.charCodeAt(0) <= 127;\n}\n\n/**\n * Calculate string size.\n *\n * @param value - String value to calculate size.\n * @returns Number of bytes used to store whole string value.\n */\nexport function calculateStringSize(value: string): number {\n const size = value.split('').reduce((total, character) => {\n if (isASCII(character)) {\n return total + 1;\n }\n return total + 2;\n }, 0);\n\n // Also detect characters that need backslash escape\n return size + (value.match(ESCAPE_CHARACTERS_REGEXP) ?? []).length;\n}\n\n/**\n * Calculate size of a number ofter JSON serialization.\n *\n * @param value - Number value to calculate size.\n * @returns Number of bytes used to store whole number in JSON.\n */\nexport function calculateNumberSize(value: number): number {\n return value.toString().length;\n}\n"]}