@midnightntwrk/midnight-js-utils 0.0.0-stage → 5.0.2-0-pre.d76c0cbe
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/README.md +234 -2
- package/dist/index.d.ts +865 -0
- package/dist/index.js +1886 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -3
package/README.md
CHANGED
|
@@ -1,3 +1,235 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared utility functions for all Midnight.js modules.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
yarn add @midnightntwrk/midnight-js-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import {
|
|
15
|
+
toHex,
|
|
16
|
+
fromHex,
|
|
17
|
+
assertDefined,
|
|
18
|
+
assertIsContractAddress
|
|
19
|
+
} from '@midnightntwrk/midnight-js-utils';
|
|
20
|
+
|
|
21
|
+
// Convert bytes to hex
|
|
22
|
+
const hex = toHex(new Uint8Array([0xab, 0xcd])); // 'abcd'
|
|
23
|
+
|
|
24
|
+
// Convert hex to bytes
|
|
25
|
+
const bytes = fromHex('abcd');
|
|
26
|
+
|
|
27
|
+
// Assert value is defined
|
|
28
|
+
assertDefined(maybeValue, 'Value must be defined');
|
|
29
|
+
|
|
30
|
+
// Validate contract address
|
|
31
|
+
assertIsContractAddress(address);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
### Hex Utilities
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// Convert bytes to hex string
|
|
40
|
+
toHex(bytes: Uint8Array): string
|
|
41
|
+
|
|
42
|
+
// Convert hex string to bytes
|
|
43
|
+
fromHex(str: string): Buffer
|
|
44
|
+
|
|
45
|
+
// Check if string is valid hex
|
|
46
|
+
isHex(source: string, byteLen?: number): boolean
|
|
47
|
+
|
|
48
|
+
// Assert string is valid hex
|
|
49
|
+
assertIsHex(source: string, byteLen?: number): asserts source is string
|
|
50
|
+
|
|
51
|
+
// Parse hex string details
|
|
52
|
+
parseHex(source: string): ParsedHexString
|
|
53
|
+
|
|
54
|
+
type ParsedHexString = {
|
|
55
|
+
hasPrefix: boolean; // Has '0x' prefix
|
|
56
|
+
byteChars: string; // Valid hex byte characters
|
|
57
|
+
incompleteChars: string; // Invalid or incomplete characters
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Bech32m Key Parsing
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// Parse coin public key (Bech32m or hex) to hex
|
|
65
|
+
parseCoinPublicKeyToHex(possibleBech32: string, zswapNetworkId: NetworkId): string
|
|
66
|
+
|
|
67
|
+
// Parse encryption public key (Bech32m or hex) to hex
|
|
68
|
+
parseEncPublicKeyToHex(possibleBech32: string, zswapNetworkId: NetworkId): string
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Assertion Utilities
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// Assert value is non-null/undefined
|
|
75
|
+
assertDefined<A>(
|
|
76
|
+
value: A | null | undefined,
|
|
77
|
+
message?: string
|
|
78
|
+
): asserts value is NonNullable<A>
|
|
79
|
+
|
|
80
|
+
// Assert value is null/undefined
|
|
81
|
+
assertUndefined<A>(
|
|
82
|
+
value: A | null | undefined,
|
|
83
|
+
message?: string
|
|
84
|
+
): asserts value is undefined | null
|
|
85
|
+
|
|
86
|
+
// Close the `default` case of a switch over a discriminated union. Compiles only
|
|
87
|
+
// while the switch is exhaustive; `context` names the switch and is required.
|
|
88
|
+
assertNever(value: never, context: string): never
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Type Utilities
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
// Assert valid contract address format (32 bytes hex, no prefix)
|
|
95
|
+
assertIsContractAddress(
|
|
96
|
+
contractAddress: string
|
|
97
|
+
): asserts contractAddress is ContractAddress
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Date Utilities
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
// Get Date one hour from now (for TTL)
|
|
104
|
+
ttlOneHour(): Date
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Error Codes and Guards
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Per-layer error-code registries (each frozen; values are string literals)
|
|
111
|
+
CONTRACTS_ERROR_CODES
|
|
112
|
+
PROVIDER_ERROR_CODES
|
|
113
|
+
UTILS_ERROR_CODES
|
|
114
|
+
// Protocol-layer codes are not re-exported here — import PROTOCOL_ERROR_CODES
|
|
115
|
+
// from '@midnightntwrk/midnight-js-protocol/errors'.
|
|
116
|
+
|
|
117
|
+
// The combined, frozen registry of every code carried by a *coded* midnight-js
|
|
118
|
+
// error. Not every midnight-js error carries a code, so a `false` from
|
|
119
|
+
// hasErrorCode does not mean the error came from somewhere else.
|
|
120
|
+
MIDNIGHT_JS_ERROR_CODES: readonly MidnightJsErrorCode[]
|
|
121
|
+
|
|
122
|
+
// Type guard, two forms:
|
|
123
|
+
// - no-arg: true only if `e` is an Error and `e.code` is a member of
|
|
124
|
+
// MIDNIGHT_JS_ERROR_CODES
|
|
125
|
+
hasErrorCode(e: unknown): e is Error & { code: MidnightJsErrorCode }
|
|
126
|
+
// - with-code: true only if `e.code === code`. `code` must be one of this
|
|
127
|
+
// framework's own codes, so a typo is a compile error.
|
|
128
|
+
hasErrorCode<C extends MidnightJsErrorCode>(e: unknown, code: C): e is Error & { code: C }
|
|
129
|
+
|
|
130
|
+
// Same comparison for a code this framework does NOT own (Node's
|
|
131
|
+
// ECONNREFUSED, a driver's own vocabulary). Separate name so that reaching
|
|
132
|
+
// outside the framework's codes is deliberate and visible at the call site.
|
|
133
|
+
//
|
|
134
|
+
// Foreignness is enforced twice. A member of MidnightJsErrorCode does not
|
|
135
|
+
// compile: the `code` parameter resolves to `never` for one. A code that only
|
|
136
|
+
// LOOKS like ours — a misspelling, which is precisely what sends a caller to
|
|
137
|
+
// this form — compiles, so it THROWS at runtime on the MIDNIGHT_JS_ prefix.
|
|
138
|
+
// Answering false there would reinstate the silent guard the constraint on
|
|
139
|
+
// hasErrorCode exists to abolish.
|
|
140
|
+
hasForeignErrorCode<C extends string>(
|
|
141
|
+
e: unknown,
|
|
142
|
+
code: C extends MidnightJsErrorCode ? never : C
|
|
143
|
+
): e is Error & { code: C }
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Serialized Tag Parsing
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
// Parses the 'namespace:version:' prefix off the front of a serialized
|
|
150
|
+
// value's raw bytes. Only scans the first 64 bytes (MAX_TAG_PREFIX_BYTES) —
|
|
151
|
+
// throws TagParseError without reading further if no well-formed prefix is
|
|
152
|
+
// found there. `body` is a `.slice()` copy, independent of the input buffer.
|
|
153
|
+
// Both segments must be non-empty and match /^[a-z0-9_[\](),-]+$/i — the
|
|
154
|
+
// character set the ledger runtimes emit, e.g. 'midnight:contract-state[v8]:'.
|
|
155
|
+
// The tag is a defence-in-depth discriminant only; it is never the
|
|
156
|
+
// authority on the decoded body.
|
|
157
|
+
parseSerializedTag(bytes: Uint8Array): {
|
|
158
|
+
namespace: string;
|
|
159
|
+
version: string;
|
|
160
|
+
tag: string; // `${namespace}:${version}`
|
|
161
|
+
body: Uint8Array;
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Exports
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import {
|
|
169
|
+
// Hex utilities
|
|
170
|
+
toHex,
|
|
171
|
+
fromHex,
|
|
172
|
+
isHex,
|
|
173
|
+
assertIsHex,
|
|
174
|
+
parseHex,
|
|
175
|
+
type ParsedHexString,
|
|
176
|
+
|
|
177
|
+
// Bech32m parsing
|
|
178
|
+
parseCoinPublicKeyToHex,
|
|
179
|
+
parseEncPublicKeyToHex,
|
|
180
|
+
|
|
181
|
+
// Assertions
|
|
182
|
+
assertDefined,
|
|
183
|
+
assertUndefined,
|
|
184
|
+
assertNever,
|
|
185
|
+
|
|
186
|
+
// Type utilities
|
|
187
|
+
assertIsContractAddress,
|
|
188
|
+
|
|
189
|
+
// Date utilities
|
|
190
|
+
ttlOneHour,
|
|
191
|
+
|
|
192
|
+
// Error codes and guards
|
|
193
|
+
CONTRACTS_ERROR_CODES,
|
|
194
|
+
type ContractsErrorCode,
|
|
195
|
+
PROVIDER_ERROR_CODES,
|
|
196
|
+
type ProviderErrorCode,
|
|
197
|
+
UTILS_ERROR_CODES,
|
|
198
|
+
type UtilsErrorCode,
|
|
199
|
+
MIDNIGHT_JS_ERROR_CODES,
|
|
200
|
+
type MidnightJsErrorCode,
|
|
201
|
+
hasErrorCode,
|
|
202
|
+
hasForeignErrorCode,
|
|
203
|
+
|
|
204
|
+
// Serialized tag parsing
|
|
205
|
+
parseSerializedTag,
|
|
206
|
+
TagParseError
|
|
207
|
+
} from '@midnightntwrk/midnight-js-utils';
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Architecture Documents
|
|
211
|
+
|
|
212
|
+
The reasoning behind this package's shape lives in `docs/`, not in the source
|
|
213
|
+
docstrings. Each file is registered with TypeDoc through `projectDocuments`, so
|
|
214
|
+
it is a page in the generated API reference and `@see {@link Title}` in a
|
|
215
|
+
docstring resolves to it.
|
|
216
|
+
|
|
217
|
+
| Document | What it explains |
|
|
218
|
+
|---|---|
|
|
219
|
+
| [Error vocabulary](./docs/error-vocabulary.md) | Why the framework-code guard and the foreign-code guard are separate functions, why foreignness is enforced at both compile time and run time, and why a deserialization failure is classified rather than re-thrown |
|
|
220
|
+
| [Artifact runtime version](./docs/artifact-runtime-version.md) | Why only `runtime-version` is read out of a compiler description, why its absence is a refusal rather than a default, and why the integrity manifest is consulted before the description |
|
|
221
|
+
|
|
222
|
+
Docstrings in `src/` carry the API contract: what a symbol does, its
|
|
223
|
+
parameters, what it returns and what it throws. Anything that answers "why is
|
|
224
|
+
it built this way" belongs in a document above, stated once.
|
|
225
|
+
|
|
226
|
+
## Resources
|
|
227
|
+
|
|
228
|
+
- [Midnight Network](https://midnight.network)
|
|
229
|
+
- [Developer Hub](https://midnight.network/developer-hub)
|
|
230
|
+
|
|
231
|
+
## Terms & License
|
|
232
|
+
|
|
233
|
+
By using this package, you agree to [Midnight's Terms and Conditions](https://midnight.network/static/terms.pdf) and [Privacy Policy](https://midnight.network/static/privacy-policy.pdf).
|
|
234
|
+
|
|
235
|
+
Licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
|