@@ -31,6 +31,24 @@ function getMemoizedBase58Decoder(): Decoder<string> {
31
31
return memoizedBase58Decoder ;
32
32
}
33
33
34
+ /**
35
+ * A type guard that returns `true` if the input string conforms to the {@link Blockhash} type, and
36
+ * refines its type for use in your program.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { isBlockhash } from '@solana/rpc-types';
41
+ *
42
+ * if (isBlockhash(blockhash)) {
43
+ * // At this point, `blockhash` has been refined to a
44
+ * // `Blockhash` that can be used with the RPC.
45
+ * const { value: isValid } = await rpc.isBlockhashValid(blockhash).send();
46
+ * setBlockhashIsFresh(isValid);
47
+ * } else {
48
+ * setError(`${blockhash} is not a blockhash`);
49
+ * }
50
+ * ```
51
+ */
34
52
export function isBlockhash ( putativeBlockhash : string ) : putativeBlockhash is Blockhash {
35
53
// Fast-path; see if the input string is of an acceptable length.
36
54
if (
@@ -51,6 +69,31 @@ export function isBlockhash(putativeBlockhash: string): putativeBlockhash is Blo
51
69
return true ;
52
70
}
53
71
72
+ /**
73
+ * From time to time you might acquire a string, that you expect to validate as a blockhash, from an
74
+ * untrusted network API or user input. Use this function to assert that such an arbitrary string is
75
+ * a base58-encoded blockhash.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * import { assertIsBlockhash } from '@solana/rpc-types';
80
+ *
81
+ * // Imagine a function that determines whether a blockhash is fresh when a user submits a form.
82
+ * function handleSubmit() {
83
+ * // We know only that what the user typed conforms to the `string` type.
84
+ * const blockhash: string = blockhashInput.value;
85
+ * try {
86
+ * // If this type assertion function doesn't throw, then
87
+ * // Typescript will upcast `blockhash` to `Blockhash`.
88
+ * assertIsBlockhash(blockhash);
89
+ * // At this point, `blockhash` is a `Blockhash` that can be used with the RPC.
90
+ * const { value: isValid } = await rpc.isBlockhashValid(blockhash).send();
91
+ * } catch (e) {
92
+ * // `blockhash` turned out not to be a base58-encoded blockhash
93
+ * }
94
+ * }
95
+ * ```
96
+ */
54
97
export function assertIsBlockhash ( putativeBlockhash : string ) : asserts putativeBlockhash is Blockhash {
55
98
// Fast-path; see if the input string is of an acceptable length.
56
99
if (
@@ -74,21 +117,85 @@ export function assertIsBlockhash(putativeBlockhash: string): asserts putativeBl
74
117
}
75
118
}
76
119
120
+ /**
121
+ * Combines _asserting_ that a string is a blockhash with _coercing_ it to the {@link Blockhash}
122
+ * type. It's most useful with untrusted input.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * import { blockhash } from '@solana/rpc-types';
127
+ *
128
+ * const { value: isValid } = await rpc.isBlockhashValid(blockhash(blockhashFromUserInput)).send();
129
+ * ```
130
+ *
131
+ * > [!TIP]
132
+ * > When starting from a known-good blockhash as a string, it's more efficient to typecast it
133
+ * rather than to use the {@link blockhash} helper, because the helper unconditionally performs
134
+ * validation on its input.
135
+ * >
136
+ * > ```ts
137
+ * > import { Blockhash } from '@solana/rpc-types';
138
+ * >
139
+ * > const blockhash = 'ABmPH5KDXX99u6woqFS5vfBGSNyKG42SzpvBMWWqAy48' as Blockhash;
140
+ * > ```
141
+ */
77
142
export function blockhash ( putativeBlockhash : string ) : Blockhash {
78
143
assertIsBlockhash ( putativeBlockhash ) ;
79
144
return putativeBlockhash ;
80
145
}
81
146
147
+ /**
148
+ * Returns an encoder that you can use to encode a base58-encoded blockhash to a byte array.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * import { getBlockhashEncoder } from '@solana/rpc-types';
153
+ *
154
+ * const blockhash = 'ABmPH5KDXX99u6woqFS5vfBGSNyKG42SzpvBMWWqAy48' as Blockhash;
155
+ * const blockhashEncoder = getBlockhashEncoder();
156
+ * const blockhashBytes = blockhashEncoder.encode(blockhash);
157
+ * // Uint8Array(32) [
158
+ * // 136, 123, 44, 249, 43, 19, 60, 14,
159
+ * // 144, 16, 168, 241, 121, 111, 70, 232,
160
+ * // 186, 26, 140, 202, 213, 64, 231, 82,
161
+ * // 179, 66, 103, 237, 52, 117, 217, 93
162
+ * // ]
163
+ * ```
164
+ */
82
165
export function getBlockhashEncoder ( ) : FixedSizeEncoder < Blockhash , 32 > {
83
166
return transformEncoder ( fixEncoderSize ( getMemoizedBase58Encoder ( ) , 32 ) , putativeBlockhash =>
84
167
blockhash ( putativeBlockhash ) ,
85
168
) ;
86
169
}
87
170
171
+ /**
172
+ * Returns a decoder that you can use to convert an array of 32 bytes representing a blockhash to
173
+ * the base58-encoded representation of that blockhash.
174
+ *
175
+ * @example
176
+ * ```ts
177
+ * import { getBlockhashDecoder } from '@solana/rpc-types';
178
+ *
179
+ * const blockhashBytes = new Uint8Array([
180
+ * 136, 123, 44, 249, 43, 19, 60, 14,
181
+ * 144, 16, 168, 241, 121, 111, 70, 232,
182
+ * 186, 26, 140, 202, 213, 64, 231, 82,
183
+ * 179, 66, 103, 237, 52, 117, 217, 93
184
+ * ]);
185
+ * const blockhashDecoder = getBlockhashDecoder();
186
+ * const blockhash = blockhashDecoder.decode(blockhashBytes); // ABmPH5KDXX99u6woqFS5vfBGSNyKG42SzpvBMWWqAy48
187
+ * ```
188
+ */
88
189
export function getBlockhashDecoder ( ) : FixedSizeDecoder < Blockhash , 32 > {
89
190
return fixDecoderSize ( getMemoizedBase58Decoder ( ) , 32 ) as FixedSizeDecoder < Blockhash , 32 > ;
90
191
}
91
192
193
+ /**
194
+ * Returns a codec that you can use to encode from or decode to a base-58 encoded blockhash.
195
+ *
196
+ * @see {@link getBlockhashDecoder }
197
+ * @see {@link getBlockhashEncoder }
198
+ */
92
199
export function getBlockhashCodec ( ) : FixedSizeCodec < Blockhash , Blockhash , 32 > {
93
200
return combineCodec ( getBlockhashEncoder ( ) , getBlockhashDecoder ( ) ) ;
94
201
}
0 commit comments