1
1
import { assertSigningCapabilityIsAvailable , assertVerificationCapabilityIsAvailable } from '@solana/assertions' ;
2
2
import { Encoder } from '@solana/codecs-core' ;
3
3
import { getBase58Encoder } from '@solana/codecs-strings' ;
4
+ import {
5
+ SOLANA_ERROR__KEYS_INVALID_SIGNATURE_BYTE_LENGTH ,
6
+ SOLANA_ERROR__KEYS_SIGNATURE_STRING_LENGTH_OUT_OF_RANGE ,
7
+ SolanaError ,
8
+ } from '@solana/errors' ;
4
9
5
10
export type Signature = string & { readonly __brand : unique symbol } ;
6
11
export type SignatureBytes = Uint8Array & { readonly __brand : unique symbol } ;
@@ -9,26 +14,23 @@ let base58Encoder: Encoder<string> | undefined;
9
14
10
15
export function assertIsSignature ( putativeSignature : string ) : asserts putativeSignature is Signature {
11
16
if ( ! base58Encoder ) base58Encoder = getBase58Encoder ( ) ;
12
-
13
- try {
14
- // Fast-path; see if the input string is of an acceptable length.
15
- if (
16
- // Lowest value (64 bytes of zeroes)
17
- putativeSignature . length < 64 ||
18
- // Highest value (64 bytes of 255)
19
- putativeSignature . length > 88
20
- ) {
21
- throw new Error ( 'Expected input string to decode to a byte array of length 64.' ) ;
22
- }
23
- // Slow-path; actually attempt to decode the input string.
24
- const bytes = base58Encoder . encode ( putativeSignature ) ;
25
- const numBytes = bytes . byteLength ;
26
- if ( numBytes !== 64 ) {
27
- throw new Error ( `Expected input string to decode to a byte array of length 64. Actual length: ${ numBytes } ` ) ;
28
- }
29
- } catch ( e ) {
30
- throw new Error ( `\`${ putativeSignature } \` is not a signature` , {
31
- cause : e ,
17
+ // Fast-path; see if the input string is of an acceptable length.
18
+ if (
19
+ // Lowest value (64 bytes of zeroes)
20
+ putativeSignature . length < 64 ||
21
+ // Highest value (64 bytes of 255)
22
+ putativeSignature . length > 88
23
+ ) {
24
+ throw new SolanaError ( SOLANA_ERROR__KEYS_SIGNATURE_STRING_LENGTH_OUT_OF_RANGE , {
25
+ actualLength : putativeSignature . length ,
26
+ } ) ;
27
+ }
28
+ // Slow-path; actually attempt to decode the input string.
29
+ const bytes = base58Encoder . encode ( putativeSignature ) ;
30
+ const numBytes = bytes . byteLength ;
31
+ if ( numBytes !== 64 ) {
32
+ throw new SolanaError ( SOLANA_ERROR__KEYS_INVALID_SIGNATURE_BYTE_LENGTH , {
33
+ actualLength : numBytes ,
32
34
} ) ;
33
35
}
34
36
}
0 commit comments