@solana/codecs-strings
Advanced tools
Comparing version 2.1.0-canary-20241211084732 to 2.1.0-canary-20250227211824
/** | ||
* Asserts that a given string matches a given alphabet. | ||
* Asserts that a given string contains only characters from the specified alphabet. | ||
* | ||
* This function validates whether a string consists exclusively of characters | ||
* from the provided `alphabet`. If the validation fails, it throws an error | ||
* indicating the invalid base string. | ||
* | ||
* @param alphabet - The allowed set of characters for the base encoding. | ||
* @param testValue - The string to validate against the given alphabet. | ||
* @param givenValue - The original string provided by the user (defaults to `testValue`). | ||
* | ||
* @throws {SolanaError} If `testValue` contains characters not present in `alphabet`. | ||
* | ||
* @example | ||
* Validating a base-8 encoded string. | ||
* ```ts | ||
* assertValidBaseString('01234567', '123047'); // Passes | ||
* assertValidBaseString('01234567', '128'); // Throws error | ||
* ``` | ||
*/ | ||
export declare function assertValidBaseString(alphabet: string, testValue: string, givenValue?: string): void; | ||
//# sourceMappingURL=assertions.d.ts.map |
@@ -1,7 +0,82 @@ | ||
/** Encodes strings in base10. */ | ||
/** | ||
* Returns an encoder for base-10 strings. | ||
* | ||
* This encoder serializes strings using a base-10 encoding scheme. | ||
* The output consists of bytes representing the numerical values of the input string. | ||
* | ||
* For more details, see {@link getBase10Codec}. | ||
* | ||
* @returns A `VariableSizeEncoder<string>` for encoding base-10 strings. | ||
* | ||
* @example | ||
* Encoding a base-10 string. | ||
* ```ts | ||
* const encoder = getBase10Encoder(); | ||
* const bytes = encoder.encode('1024'); // 0x0400 | ||
* ``` | ||
* | ||
* @see {@link getBase10Codec} | ||
*/ | ||
export declare const getBase10Encoder: () => import("@solana/codecs-core").VariableSizeEncoder<string>; | ||
/** Decodes strings in base10. */ | ||
/** | ||
* Returns a decoder for base-10 strings. | ||
* | ||
* This decoder deserializes base-10 encoded strings from a byte array. | ||
* | ||
* For more details, see {@link getBase10Codec}. | ||
* | ||
* @returns A `VariableSizeDecoder<string>` for decoding base-10 strings. | ||
* | ||
* @example | ||
* Decoding a base-10 string. | ||
* ```ts | ||
* const decoder = getBase10Decoder(); | ||
* const value = decoder.decode(new Uint8Array([0x04, 0x00])); // "1024" | ||
* ``` | ||
* | ||
* @see {@link getBase10Codec} | ||
*/ | ||
export declare const getBase10Decoder: () => import("@solana/codecs-core").VariableSizeDecoder<string>; | ||
/** Encodes and decodes strings in base10. */ | ||
/** | ||
* Returns a codec for encoding and decoding base-10 strings. | ||
* | ||
* This codec serializes strings using a base-10 encoding scheme. | ||
* The output consists of bytes representing the numerical values of the input string. | ||
* | ||
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-10 strings. | ||
* | ||
* @example | ||
* Encoding and decoding a base-10 string. | ||
* ```ts | ||
* const codec = getBase10Codec(); | ||
* const bytes = codec.encode('1024'); // 0x0400 | ||
* const value = codec.decode(bytes); // "1024" | ||
* ``` | ||
* | ||
* @remarks | ||
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string. | ||
* | ||
* If you need a fixed-size base-10 codec, consider using {@link fixCodecSize}. | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getBase10Codec(), 5); | ||
* ``` | ||
* | ||
* If you need a size-prefixed base-10 codec, consider using {@link addCodecSizePrefix}. | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getBase10Codec(), getU32Codec()); | ||
* ``` | ||
* | ||
* Separate {@link getBase10Encoder} and {@link getBase10Decoder} functions are available. | ||
* | ||
* ```ts | ||
* const bytes = getBase10Encoder().encode('1024'); | ||
* const value = getBase10Decoder().decode(bytes); | ||
* ``` | ||
* | ||
* @see {@link getBase10Encoder} | ||
* @see {@link getBase10Decoder} | ||
*/ | ||
export declare const getBase10Codec: () => import("@solana/codecs-core").VariableSizeCodec<string>; | ||
//# sourceMappingURL=base10.d.ts.map |
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core'; | ||
/** Encodes strings in base16. */ | ||
/** | ||
* Returns an encoder for base-16 (hexadecimal) strings. | ||
* | ||
* This encoder serializes strings using a base-16 encoding scheme. | ||
* The output consists of bytes representing the hexadecimal values of the input string. | ||
* | ||
* For more details, see {@link getBase16Codec}. | ||
* | ||
* @returns A `VariableSizeEncoder<string>` for encoding base-16 strings. | ||
* | ||
* @example | ||
* Encoding a base-16 string. | ||
* ```ts | ||
* const encoder = getBase16Encoder(); | ||
* const bytes = encoder.encode('deadface'); // 0xdeadface | ||
* ``` | ||
* | ||
* @see {@link getBase16Codec} | ||
*/ | ||
export declare const getBase16Encoder: () => VariableSizeEncoder<string>; | ||
/** Decodes strings in base16. */ | ||
/** | ||
* Returns a decoder for base-16 (hexadecimal) strings. | ||
* | ||
* This decoder deserializes base-16 encoded strings from a byte array. | ||
* | ||
* For more details, see {@link getBase16Codec}. | ||
* | ||
* @returns A `VariableSizeDecoder<string>` for decoding base-16 strings. | ||
* | ||
* @example | ||
* Decoding a base-16 string. | ||
* ```ts | ||
* const decoder = getBase16Decoder(); | ||
* const value = decoder.decode(new Uint8Array([0xde, 0xad, 0xfa, 0xce])); // "deadface" | ||
* ``` | ||
* | ||
* @see {@link getBase16Codec} | ||
*/ | ||
export declare const getBase16Decoder: () => VariableSizeDecoder<string>; | ||
/** Encodes and decodes strings in base16. */ | ||
/** | ||
* Returns a codec for encoding and decoding base-16 (hexadecimal) strings. | ||
* | ||
* This codec serializes strings using a base-16 encoding scheme. | ||
* The output consists of bytes representing the hexadecimal values of the input string. | ||
* | ||
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-16 strings. | ||
* | ||
* @example | ||
* Encoding and decoding a base-16 string. | ||
* ```ts | ||
* const codec = getBase16Codec(); | ||
* const bytes = codec.encode('deadface'); // 0xdeadface | ||
* const value = codec.decode(bytes); // "deadface" | ||
* ``` | ||
* | ||
* @remarks | ||
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string. | ||
* | ||
* If you need a fixed-size base-16 codec, consider using {@link fixCodecSize}. | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getBase16Codec(), 8); | ||
* ``` | ||
* | ||
* If you need a size-prefixed base-16 codec, consider using {@link addCodecSizePrefix}. | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getBase16Codec(), getU32Codec()); | ||
* ``` | ||
* | ||
* Separate {@link getBase16Encoder} and {@link getBase16Decoder} functions are available. | ||
* | ||
* ```ts | ||
* const bytes = getBase16Encoder().encode('deadface'); | ||
* const value = getBase16Decoder().decode(bytes); | ||
* ``` | ||
* | ||
* @see {@link getBase16Encoder} | ||
* @see {@link getBase16Decoder} | ||
*/ | ||
export declare const getBase16Codec: () => VariableSizeCodec<string>; | ||
//# sourceMappingURL=base16.d.ts.map |
@@ -1,7 +0,82 @@ | ||
/** Encodes strings in base58. */ | ||
/** | ||
* Returns an encoder for base-58 strings. | ||
* | ||
* This encoder serializes strings using a base-58 encoding scheme, | ||
* commonly used in cryptocurrency addresses and other compact representations. | ||
* | ||
* For more details, see {@link getBase58Codec}. | ||
* | ||
* @returns A `VariableSizeEncoder<string>` for encoding base-58 strings. | ||
* | ||
* @example | ||
* Encoding a base-58 string. | ||
* ```ts | ||
* const encoder = getBase58Encoder(); | ||
* const bytes = encoder.encode('heLLo'); // 0x1b6a3070 | ||
* ``` | ||
* | ||
* @see {@link getBase58Codec} | ||
*/ | ||
export declare const getBase58Encoder: () => import("@solana/codecs-core").VariableSizeEncoder<string>; | ||
/** Decodes strings in base58. */ | ||
/** | ||
* Returns a decoder for base-58 strings. | ||
* | ||
* This decoder deserializes base-58 encoded strings from a byte array. | ||
* | ||
* For more details, see {@link getBase58Codec}. | ||
* | ||
* @returns A `VariableSizeDecoder<string>` for decoding base-58 strings. | ||
* | ||
* @example | ||
* Decoding a base-58 string. | ||
* ```ts | ||
* const decoder = getBase58Decoder(); | ||
* const value = decoder.decode(new Uint8Array([0x1b, 0x6a, 0x30, 0x70])); // "heLLo" | ||
* ``` | ||
* | ||
* @see {@link getBase58Codec} | ||
*/ | ||
export declare const getBase58Decoder: () => import("@solana/codecs-core").VariableSizeDecoder<string>; | ||
/** Encodes and decodes strings in base58. */ | ||
/** | ||
* Returns a codec for encoding and decoding base-58 strings. | ||
* | ||
* This codec serializes strings using a base-58 encoding scheme, | ||
* commonly used in cryptocurrency addresses and other compact representations. | ||
* | ||
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-58 strings. | ||
* | ||
* @example | ||
* Encoding and decoding a base-58 string. | ||
* ```ts | ||
* const codec = getBase58Codec(); | ||
* const bytes = codec.encode('heLLo'); // 0x1b6a3070 | ||
* const value = codec.decode(bytes); // "heLLo" | ||
* ``` | ||
* | ||
* @remarks | ||
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string. | ||
* | ||
* If you need a fixed-size base-58 codec, consider using {@link fixCodecSize}. | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getBase58Codec(), 8); | ||
* ``` | ||
* | ||
* If you need a size-prefixed base-58 codec, consider using {@link addCodecSizePrefix}. | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getBase58Codec(), getU32Codec()); | ||
* ``` | ||
* | ||
* Separate {@link getBase58Encoder} and {@link getBase58Decoder} functions are available. | ||
* | ||
* ```ts | ||
* const bytes = getBase58Encoder().encode('heLLo'); | ||
* const value = getBase58Decoder().decode(bytes); | ||
* ``` | ||
* | ||
* @see {@link getBase58Encoder} | ||
* @see {@link getBase58Decoder} | ||
*/ | ||
export declare const getBase58Codec: () => import("@solana/codecs-core").VariableSizeCodec<string>; | ||
//# sourceMappingURL=base58.d.ts.map |
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core'; | ||
/** Encodes strings in base64. */ | ||
/** | ||
* Returns an encoder for base-64 strings. | ||
* | ||
* This encoder serializes strings using a base-64 encoding scheme, | ||
* commonly used for data encoding in URLs, cryptographic keys, and binary-to-text encoding. | ||
* | ||
* For more details, see {@link getBase64Codec}. | ||
* | ||
* @returns A `VariableSizeEncoder<string>` for encoding base-64 strings. | ||
* | ||
* @example | ||
* Encoding a base-64 string. | ||
* ```ts | ||
* const encoder = getBase64Encoder(); | ||
* const bytes = encoder.encode('hello+world'); // 0x85e965a3ec28ae57 | ||
* ``` | ||
* | ||
* @see {@link getBase64Codec} | ||
*/ | ||
export declare const getBase64Encoder: () => VariableSizeEncoder<string>; | ||
/** Decodes strings in base64. */ | ||
/** | ||
* Returns a decoder for base-64 strings. | ||
* | ||
* This decoder deserializes base-64 encoded strings from a byte array. | ||
* | ||
* For more details, see {@link getBase64Codec}. | ||
* | ||
* @returns A `VariableSizeDecoder<string>` for decoding base-64 strings. | ||
* | ||
* @example | ||
* Decoding a base-64 string. | ||
* ```ts | ||
* const decoder = getBase64Decoder(); | ||
* const value = decoder.decode(new Uint8Array([0x85, 0xe9, 0x65, 0xa3, 0xec, 0x28, 0xae, 0x57])); // "hello+world" | ||
* ``` | ||
* | ||
* @see {@link getBase64Codec} | ||
*/ | ||
export declare const getBase64Decoder: () => VariableSizeDecoder<string>; | ||
/** Encodes and decodes strings in base64. */ | ||
/** | ||
* Returns a codec for encoding and decoding base-64 strings. | ||
* | ||
* This codec serializes strings using a base-64 encoding scheme, | ||
* commonly used for data encoding in URLs, cryptographic keys, and binary-to-text encoding. | ||
* | ||
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-64 strings. | ||
* | ||
* @example | ||
* Encoding and decoding a base-64 string. | ||
* ```ts | ||
* const codec = getBase64Codec(); | ||
* const bytes = codec.encode('hello+world'); // 0x85e965a3ec28ae57 | ||
* const value = codec.decode(bytes); // "hello+world" | ||
* ``` | ||
* | ||
* @remarks | ||
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string. | ||
* | ||
* If you need a fixed-size base-64 codec, consider using {@link fixCodecSize}. | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getBase64Codec(), 8); | ||
* ``` | ||
* | ||
* If you need a size-prefixed base-64 codec, consider using {@link addCodecSizePrefix}. | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getBase64Codec(), getU32Codec()); | ||
* ``` | ||
* | ||
* Separate {@link getBase64Encoder} and {@link getBase64Decoder} functions are available. | ||
* | ||
* ```ts | ||
* const bytes = getBase64Encoder().encode('hello+world'); | ||
* const value = getBase64Decoder().decode(bytes); | ||
* ``` | ||
* | ||
* @see {@link getBase64Encoder} | ||
* @see {@link getBase64Decoder} | ||
*/ | ||
export declare const getBase64Codec: () => VariableSizeCodec<string>; | ||
//# sourceMappingURL=base64.d.ts.map |
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core'; | ||
/** | ||
* Encodes a string using a custom alphabet by reslicing the bits of the byte array. | ||
* @see {@link getBaseXResliceCodec} for a more detailed description. | ||
* Returns an encoder for base-X encoded strings using bit re-slicing. | ||
* | ||
* This encoder serializes strings by dividing the input into custom-sized bit chunks, | ||
* mapping them to an alphabet, and encoding the result into a byte array. | ||
* This approach is commonly used for encoding schemes where the alphabet's length is a power of 2, | ||
* such as base-16 or base-64. | ||
* | ||
* For more details, see {@link getBaseXResliceCodec}. | ||
* | ||
* @param alphabet - The set of characters defining the base-X encoding. | ||
* @param bits - The number of bits per encoded chunk, typically `log2(alphabet.length)`. | ||
* @returns A `VariableSizeEncoder<string>` for encoding base-X strings using bit re-slicing. | ||
* | ||
* @example | ||
* Encoding a base-X string using bit re-slicing. | ||
* ```ts | ||
* const encoder = getBaseXResliceEncoder('elho', 2); | ||
* const bytes = encoder.encode('hellolol'); // 0x4aee | ||
* ``` | ||
* | ||
* @see {@link getBaseXResliceCodec} | ||
*/ | ||
export declare const getBaseXResliceEncoder: (alphabet: string, bits: number) => VariableSizeEncoder<string>; | ||
/** | ||
* Decodes a string using a custom alphabet by reslicing the bits of the byte array. | ||
* @see {@link getBaseXResliceCodec} for a more detailed description. | ||
* Returns a decoder for base-X encoded strings using bit re-slicing. | ||
* | ||
* This decoder deserializes base-X encoded strings by re-slicing the bits of a byte array into | ||
* custom-sized chunks and mapping them to a specified alphabet. | ||
* This is typically used for encoding schemes where the alphabet's length is a power of 2, | ||
* such as base-16 or base-64. | ||
* | ||
* For more details, see {@link getBaseXResliceCodec}. | ||
* | ||
* @param alphabet - The set of characters defining the base-X encoding. | ||
* @param bits - The number of bits per encoded chunk, typically `log2(alphabet.length)`. | ||
* @returns A `VariableSizeDecoder<string>` for decoding base-X strings using bit re-slicing. | ||
* | ||
* @example | ||
* Decoding a base-X string using bit re-slicing. | ||
* ```ts | ||
* const decoder = getBaseXResliceDecoder('elho', 2); | ||
* const value = decoder.decode(new Uint8Array([0x4a, 0xee])); // "hellolol" | ||
* ``` | ||
* | ||
* @see {@link getBaseXResliceCodec} | ||
*/ | ||
export declare const getBaseXResliceDecoder: (alphabet: string, bits: number) => VariableSizeDecoder<string>; | ||
/** | ||
* A string serializer that reslices bytes into custom chunks | ||
* of bits that are then mapped to a custom alphabet. | ||
* Returns a codec for encoding and decoding base-X strings using bit re-slicing. | ||
* | ||
* This can be used to create serializers whose alphabet | ||
* is a power of 2 such as base16 or base64. | ||
* This codec serializes strings by dividing the input into custom-sized bit chunks, | ||
* mapping them to a given alphabet, and encoding the result into bytes. | ||
* It is particularly suited for encoding schemes where the alphabet's length is a power of 2, | ||
* such as base-16 or base-64. | ||
* | ||
* @param alphabet - The set of characters defining the base-X encoding. | ||
* @param bits - The number of bits per encoded chunk, typically `log2(alphabet.length)`. | ||
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-X strings using bit re-slicing. | ||
* | ||
* @example | ||
* Encoding and decoding a base-X string using bit re-slicing. | ||
* ```ts | ||
* const codec = getBaseXResliceCodec('elho', 2); | ||
* const bytes = codec.encode('hellolol'); // 0x4aee | ||
* const value = codec.decode(bytes); // "hellolol" | ||
* ``` | ||
* | ||
* @remarks | ||
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string. | ||
* | ||
* If you need a fixed-size base-X codec, consider using {@link fixCodecSize}. | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getBaseXResliceCodec('elho', 2), 8); | ||
* ``` | ||
* | ||
* If you need a size-prefixed base-X codec, consider using {@link addCodecSizePrefix}. | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getBaseXResliceCodec('elho', 2), getU32Codec()); | ||
* ``` | ||
* | ||
* Separate {@link getBaseXResliceEncoder} and {@link getBaseXResliceDecoder} functions are available. | ||
* | ||
* ```ts | ||
* const bytes = getBaseXResliceEncoder('elho', 2).encode('hellolol'); | ||
* const value = getBaseXResliceDecoder('elho', 2).decode(bytes); | ||
* ``` | ||
* | ||
* @see {@link getBaseXResliceEncoder} | ||
* @see {@link getBaseXResliceDecoder} | ||
*/ | ||
export declare const getBaseXResliceCodec: (alphabet: string, bits: number) => VariableSizeCodec<string>; | ||
//# sourceMappingURL=baseX-reslice.d.ts.map |
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core'; | ||
/** | ||
* Encodes a string using a custom alphabet by dividing | ||
* by the base and handling leading zeroes. | ||
* @see {@link getBaseXCodec} for a more detailed description. | ||
* Returns an encoder for base-X encoded strings. | ||
* | ||
* This encoder serializes strings using a custom alphabet, treating the length of the alphabet as the base. | ||
* The encoding process involves converting the input string to a numeric value in base-X, then | ||
* encoding that value into bytes while preserving leading zeroes. | ||
* | ||
* For more details, see {@link getBaseXCodec}. | ||
* | ||
* @param alphabet - The set of characters defining the base-X encoding. | ||
* @returns A `VariableSizeEncoder<string>` for encoding base-X strings. | ||
* | ||
* @example | ||
* Encoding a base-X string using a custom alphabet. | ||
* ```ts | ||
* const encoder = getBaseXEncoder('0123456789abcdef'); | ||
* const bytes = encoder.encode('deadface'); // 0xdeadface | ||
* ``` | ||
* | ||
* @see {@link getBaseXCodec} | ||
*/ | ||
export declare const getBaseXEncoder: (alphabet: string) => VariableSizeEncoder<string>; | ||
/** | ||
* Decodes a string using a custom alphabet by dividing | ||
* by the base and handling leading zeroes. | ||
* @see {@link getBaseXCodec} for a more detailed description. | ||
* Returns a decoder for base-X encoded strings. | ||
* | ||
* This decoder deserializes base-X encoded strings from a byte array using a custom alphabet. | ||
* The decoding process converts the byte array into a numeric value in base-10, then | ||
* maps that value back to characters in the specified base-X alphabet. | ||
* | ||
* For more details, see {@link getBaseXCodec}. | ||
* | ||
* @param alphabet - The set of characters defining the base-X encoding. | ||
* @returns A `VariableSizeDecoder<string>` for decoding base-X strings. | ||
* | ||
* @example | ||
* Decoding a base-X string using a custom alphabet. | ||
* ```ts | ||
* const decoder = getBaseXDecoder('0123456789abcdef'); | ||
* const value = decoder.decode(new Uint8Array([0xde, 0xad, 0xfa, 0xce])); // "deadface" | ||
* ``` | ||
* | ||
* @see {@link getBaseXCodec} | ||
*/ | ||
export declare const getBaseXDecoder: (alphabet: string) => VariableSizeDecoder<string>; | ||
/** | ||
* A string codec that requires a custom alphabet and uses | ||
* the length of that alphabet as the base. It then divides | ||
* the input by the base as many times as necessary to get | ||
* the output. It also supports leading zeroes by using the | ||
* first character of the alphabet as the zero character. | ||
* Returns a codec for encoding and decoding base-X strings. | ||
* | ||
* This can be used to create codecs such as base10 or base58. | ||
* This codec serializes strings using a custom alphabet, treating the length of the alphabet as the base. | ||
* The encoding process converts the input string into a numeric value in base-X, which is then encoded as bytes. | ||
* The decoding process reverses this transformation to reconstruct the original string. | ||
* | ||
* This codec supports leading zeroes by treating the first character of the alphabet as the zero character. | ||
* | ||
* @param alphabet - The set of characters defining the base-X encoding. | ||
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-X strings. | ||
* | ||
* @example | ||
* Encoding and decoding a base-X string using a custom alphabet. | ||
* ```ts | ||
* const codec = getBaseXCodec('0123456789abcdef'); | ||
* const bytes = codec.encode('deadface'); // 0xdeadface | ||
* const value = codec.decode(bytes); // "deadface" | ||
* ``` | ||
* | ||
* @remarks | ||
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string. | ||
* | ||
* If you need a fixed-size base-X codec, consider using {@link fixCodecSize}. | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getBaseXCodec('0123456789abcdef'), 8); | ||
* ``` | ||
* | ||
* If you need a size-prefixed base-X codec, consider using {@link addCodecSizePrefix}. | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getBaseXCodec('0123456789abcdef'), getU32Codec()); | ||
* ``` | ||
* | ||
* Separate {@link getBaseXEncoder} and {@link getBaseXDecoder} functions are available. | ||
* | ||
* ```ts | ||
* const bytes = getBaseXEncoder('0123456789abcdef').encode('deadface'); | ||
* const value = getBaseXDecoder('0123456789abcdef').decode(bytes); | ||
* ``` | ||
* | ||
* @see {@link getBaseXEncoder} | ||
* @see {@link getBaseXDecoder} | ||
*/ | ||
export declare const getBaseXCodec: (alphabet: string) => VariableSizeCodec<string>; | ||
//# sourceMappingURL=baseX.d.ts.map |
@@ -0,1 +1,202 @@ | ||
/** | ||
* This package contains codecs for strings of different sizes and encodings. | ||
* It can be used standalone, but it is also exported as part of Kit | ||
* [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit). | ||
* | ||
* This package is also part of the [`@solana/codecs` package](https://github.com/anza-xyz/kit/tree/main/packages/codecs) | ||
* which acts as an entry point for all codec packages as well as for their documentation. | ||
* | ||
* ## Sizing string codecs | ||
* | ||
* The `@solana/codecs-strings` package offers a variety of string codecs such as `utf8`, `base58`, `base64`, etc — | ||
* which we will discuss in more detail below. However, before digging into the available string codecs, | ||
* it's important to understand the different sizing strategies available for string codecs. | ||
* | ||
* By default, all available string codecs will return a `VariableSizeCodec<string>` meaning that: | ||
* | ||
* - When encoding a string, all bytes necessary to encode the string will be used. | ||
* - When decoding a byte array at a given offset, all bytes starting from that offset will be decoded as a string. | ||
* | ||
* For instance, here's how you can encode/decode `utf8` strings without any size boundary: | ||
* | ||
* ```ts | ||
* const codec = getUtf8Codec(); | ||
* | ||
* codec.encode('hello'); | ||
* // 0x68656c6c6f | ||
* // └-- Any bytes necessary to encode our content. | ||
* | ||
* codec.decode(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); | ||
* // 'hello' | ||
* ``` | ||
* | ||
* This might be what you want — e.g. when having a string at the end of a data structure — but in many cases, | ||
* you might want to have a size boundary for your string. You may achieve this by composing your string codec | ||
* with the [`fixCodecSize`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-core#fixing-the-size-of-codecs) | ||
* or [`addCodecSizePrefix`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-core#prefixing-the-size-of-codecs) functions. | ||
* | ||
* The `fixCodecSize` function accepts a fixed byte length and returns a `FixedSizeCodec<string>` that will always use | ||
* that amount of bytes to encode and decode a string. Any string longer or smaller than that size will be truncated | ||
* or padded respectively. Here's how you can use it with a `utf8` codec: | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getUtf8Codec(), 5); | ||
* | ||
* codec.encode('hello'); | ||
* // 0x68656c6c6f | ||
* // └-- The exact 5 bytes of content. | ||
* | ||
* codec.encode('hello world'); | ||
* // 0x68656c6c6f | ||
* // └-- The truncated 5 bytes of content. | ||
* | ||
* codec.encode('hell'); | ||
* // 0x68656c6c00 | ||
* // └-- The padded 5 bytes of content. | ||
* | ||
* codec.decode(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xff, 0xff, 0xff, 0xff])); | ||
* // 'hello' | ||
* ``` | ||
* | ||
* The `addCodecSizePrefix` function accepts an additional number codec that will be used to encode and | ||
* decode a size prefix for the string. This prefix allows us to know when to stop reading the string when | ||
* decoding a given byte array. Here's how you can use it with a `utf8` codec: | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getUtf8Codec(), getU32Codec()); | ||
* | ||
* codec.encode('hello'); | ||
* // 0x0500000068656c6c6f | ||
* // | └-- The 5 bytes of content. | ||
* // └-- 4-byte prefix telling us to read 5 bytes. | ||
* | ||
* codec.decode(new Uint8Array([0x05, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xff, 0xff, 0xff, 0xff])); | ||
* // "hello" | ||
* ``` | ||
* | ||
* Now, let's take a look at the available string encodings. Just remember that you can use | ||
* the `fixSizeCodec` or `prefixSizeCodec` functions on any of these encodings to add a size boundary to them. | ||
* | ||
* ## Utf8 codec | ||
* | ||
* The `getUtf8Codec` function encodes and decodes a UTF-8 string to and from a byte array. | ||
* | ||
* ```ts | ||
* const bytes = getUtf8Codec().encode('hello'); // 0x68656c6c6f | ||
* const value = getUtf8Codec().decode(bytes); // "hello" | ||
* ``` | ||
* | ||
* As usual, separate `getUtf8Encoder` and `getUtf8Decoder` functions are also available. | ||
* | ||
* ```ts | ||
* const bytes = getUtf8Encoder().encode('hello'); // 0x68656c6c6f | ||
* const value = getUtf8Decoder().decode(bytes); // "hello" | ||
* ``` | ||
* | ||
* ## Base 64 codec | ||
* | ||
* The `getBase64Codec` function encodes and decodes a base-64 string to and from a byte array. | ||
* | ||
* ```ts | ||
* const bytes = getBase64Codec().encode('hello+world'); // 0x85e965a3ec28ae57 | ||
* const value = getBase64Codec().decode(bytes); // "hello+world" | ||
* ``` | ||
* | ||
* As usual, separate `getBase64Encoder` and `getBase64Decoder` functions are also available. | ||
* | ||
* ```ts | ||
* const bytes = getBase64Encoder().encode('hello+world'); // 0x85e965a3ec28ae57 | ||
* const value = getBase64Decoder().decode(bytes); // "hello+world" | ||
* ``` | ||
* | ||
* ## Base 58 codec | ||
* | ||
* The `getBase58Codec` function encodes and decodes a base-58 string to and from a byte array. | ||
* | ||
* ```ts | ||
* const bytes = getBase58Codec().encode('heLLo'); // 0x1b6a3070 | ||
* const value = getBase58Codec().decode(bytes); // "heLLo" | ||
* ``` | ||
* | ||
* As usual, separate `getBase58Encoder` and `getBase58Decoder` functions are also available. | ||
* | ||
* ```ts | ||
* const bytes = getBase58Encoder().encode('heLLo'); // 0x1b6a3070 | ||
* const value = getBase58Decoder().decode(bytes); // "heLLo" | ||
* ``` | ||
* | ||
* ## Base 16 codec | ||
* | ||
* The `getBase16Codec` function encodes and decodes a base-16 string to and from a byte array. | ||
* | ||
* ```ts | ||
* const bytes = getBase16Codec().encode('deadface'); // 0xdeadface | ||
* const value = getBase16Codec().decode(bytes); // "deadface" | ||
* ``` | ||
* | ||
* As usual, separate `getBase16Encoder` and `getBase16Decoder` functions are also available. | ||
* | ||
* ```ts | ||
* const bytes = getBase16Encoder().encode('deadface'); // 0xdeadface | ||
* const value = getBase16Decoder().decode(bytes); // "deadface" | ||
* ``` | ||
* | ||
* ## Base 10 codec | ||
* | ||
* The `getBase10Codec` function encodes and decodes a base-10 string to and from a byte array. | ||
* | ||
* ```ts | ||
* const bytes = getBase10Codec().encode('1024'); // 0x0400 | ||
* const value = getBase10Codec().decode(bytes); // "1024" | ||
* ``` | ||
* | ||
* As usual, separate `getBase10Encoder` and `getBase10Decoder` functions are also available. | ||
* | ||
* ```ts | ||
* const bytes = getBase10Encoder().encode('1024'); // 0x0400 | ||
* const value = getBase10Decoder().decode(bytes); // "1024" | ||
* ``` | ||
* | ||
* ## Base X codec | ||
* | ||
* The `getBaseXCodec` accepts a custom `alphabet` of `X` characters and creates a base-X codec using that alphabet. | ||
* It does so by iteratively dividing by `X` and handling leading zeros. | ||
* | ||
* The base-10 and base-58 codecs use this base-x codec under the hood. | ||
* | ||
* ```ts | ||
* const alphabet = '0ehlo'; | ||
* const bytes = getBaseXCodec(alphabet).encode('hello'); // 0x05bd | ||
* const value = getBaseXCodec(alphabet).decode(bytes); // "hello" | ||
* ``` | ||
* | ||
* As usual, separate `getBaseXEncoder` and `getBaseXDecoder` functions are also available. | ||
* | ||
* ```ts | ||
* const bytes = getBaseXEncoder(alphabet).encode('hello'); // 0x05bd | ||
* const value = getBaseXDecoder(alphabet).decode(bytes); // "hello" | ||
* ``` | ||
* | ||
* ## Re-slicing base X codec | ||
* | ||
* The `getBaseXResliceCodec` also creates a base-x codec but uses a different strategy. | ||
* It re-slices bytes into custom chunks of bits that are then mapped to a provided `alphabet`. | ||
* The number of bits per chunk is also provided and should typically be set to `log2(alphabet.length)`. | ||
* | ||
* This is typically used to create codecs whose alphabet’s length is a power of 2 such as base-16 or base-64. | ||
* | ||
* ```ts | ||
* const bytes = getBaseXResliceCodec('elho', 2).encode('hellolol'); // 0x4aee | ||
* const value = getBaseXResliceCodec('elho', 2).decode(bytes); // "hellolol" | ||
* ``` | ||
* | ||
* As usual, separate `getBaseXResliceEncoder` and `getBaseXResliceDecoder` functions are also available. | ||
* | ||
* ```ts | ||
* const bytes = getBaseXResliceEncoder('elho', 2).encode('hellolol'); // 0x4aee | ||
* const value = getBaseXResliceDecoder('elho', 2).decode(bytes); // "hellolol" | ||
* ``` | ||
* | ||
* @packageDocumentation | ||
*/ | ||
export * from './assertions'; | ||
@@ -2,0 +203,0 @@ export * from './base10'; |
@@ -1,5 +0,34 @@ | ||
/**Removes null characters from a string. */ | ||
/** | ||
* Removes all null characters (`\u0000`) from a string. | ||
* | ||
* This function cleans a string by stripping out any null characters, | ||
* which are often used as padding in fixed-size string encodings. | ||
* | ||
* @param value - The string to process. | ||
* @returns The input string with all null characters removed. | ||
* | ||
* @example | ||
* Removing null characters from a string. | ||
* ```ts | ||
* removeNullCharacters('hello\u0000\u0000'); // "hello" | ||
* ``` | ||
*/ | ||
export declare const removeNullCharacters: (value: string) => string; | ||
/** Pads a string with null characters at the end. */ | ||
/** | ||
* Pads a string with null characters (`\u0000`) at the end to reach a fixed length. | ||
* | ||
* If the input string is shorter than the specified length, it is padded with null characters | ||
* until it reaches the desired size. If it is already long enough, it remains unchanged. | ||
* | ||
* @param value - The string to pad. | ||
* @param chars - The total length of the resulting string, including padding. | ||
* @returns The input string padded with null characters up to the specified length. | ||
* | ||
* @example | ||
* Padding a string with null characters. | ||
* ```ts | ||
* padNullCharacters('hello', 8); // "hello\u0000\u0000\u0000" | ||
* ``` | ||
*/ | ||
export declare const padNullCharacters: (value: string, chars: number) => string; | ||
//# sourceMappingURL=null-characters.d.ts.map |
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core'; | ||
/** Encodes UTF-8 strings using the native `TextEncoder` API. */ | ||
/** | ||
* Returns an encoder for UTF-8 strings. | ||
* | ||
* This encoder serializes strings using UTF-8 encoding. | ||
* The encoded output contains as many bytes as needed to represent the string. | ||
* | ||
* For more details, see {@link getUtf8Codec}. | ||
* | ||
* @returns A `VariableSizeEncoder<string>` for encoding UTF-8 strings. | ||
* | ||
* @example | ||
* Encoding a UTF-8 string. | ||
* ```ts | ||
* const encoder = getUtf8Encoder(); | ||
* const bytes = encoder.encode('hello'); // 0x68656c6c6f | ||
* ``` | ||
* | ||
* @see {@link getUtf8Codec} | ||
*/ | ||
export declare const getUtf8Encoder: () => VariableSizeEncoder<string>; | ||
/** Decodes UTF-8 strings using the native `TextDecoder` API. */ | ||
/** | ||
* Returns a decoder for UTF-8 strings. | ||
* | ||
* This decoder deserializes UTF-8 encoded strings from a byte array. | ||
* It reads all available bytes starting from the given offset. | ||
* | ||
* For more details, see {@link getUtf8Codec}. | ||
* | ||
* @returns A `VariableSizeDecoder<string>` for decoding UTF-8 strings. | ||
* | ||
* @example | ||
* Decoding a UTF-8 string. | ||
* ```ts | ||
* const decoder = getUtf8Decoder(); | ||
* const value = decoder.decode(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // "hello" | ||
* ``` | ||
* | ||
* @see {@link getUtf8Codec} | ||
*/ | ||
export declare const getUtf8Decoder: () => VariableSizeDecoder<string>; | ||
/** Encodes and decodes UTF-8 strings using the native `TextEncoder` and `TextDecoder` API. */ | ||
/** | ||
* Returns a codec for encoding and decoding UTF-8 strings. | ||
* | ||
* This codec serializes strings using UTF-8 encoding. | ||
* The encoded output contains as many bytes as needed to represent the string. | ||
* | ||
* @returns A `VariableSizeCodec<string>` for encoding and decoding UTF-8 strings. | ||
* | ||
* @example | ||
* Encoding and decoding a UTF-8 string. | ||
* ```ts | ||
* const codec = getUtf8Codec(); | ||
* const bytes = codec.encode('hello'); // 0x68656c6c6f | ||
* const value = codec.decode(bytes); // "hello" | ||
* ``` | ||
* | ||
* @remarks | ||
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string. | ||
* | ||
* If you need a fixed-size UTF-8 codec, consider using {@link fixCodecSize}. | ||
* | ||
* ```ts | ||
* const codec = fixCodecSize(getUtf8Codec(), 5); | ||
* ``` | ||
* | ||
* If you need a size-prefixed UTF-8 codec, consider using {@link addCodecSizePrefix}. | ||
* | ||
* ```ts | ||
* const codec = addCodecSizePrefix(getUtf8Codec(), getU32Codec()); | ||
* ``` | ||
* | ||
* Separate {@link getUtf8Encoder} and {@link getUtf8Decoder} functions are available. | ||
* | ||
* ```ts | ||
* const bytes = getUtf8Encoder().encode('hello'); | ||
* const value = getUtf8Decoder().decode(bytes); | ||
* ``` | ||
* | ||
* @see {@link getUtf8Encoder} | ||
* @see {@link getUtf8Decoder} | ||
*/ | ||
export declare const getUtf8Codec: () => VariableSizeCodec<string>; | ||
//# sourceMappingURL=utf8.d.ts.map |
{ | ||
"name": "@solana/codecs-strings", | ||
"version": "2.1.0-canary-20241211084732", | ||
"version": "2.1.0-canary-20250227211824", | ||
"description": "Codecs for strings of different sizes and encodings", | ||
@@ -47,6 +47,6 @@ "exports": { | ||
"type": "git", | ||
"url": "https://github.com/solana-labs/solana-web3.js" | ||
"url": "https://github.com/anza-xyz/kit" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/solana-labs/solana-web3.js/issues" | ||
"url": "https://github.com/anza-xyz/kit/issues" | ||
}, | ||
@@ -58,5 +58,5 @@ "browserslist": [ | ||
"dependencies": { | ||
"@solana/codecs-core": "2.1.0-canary-20241211084732", | ||
"@solana/codecs-numbers": "2.1.0-canary-20241211084732", | ||
"@solana/errors": "2.1.0-canary-20241211084732" | ||
"@solana/codecs-core": "2.1.0-canary-20250227211824", | ||
"@solana/codecs-numbers": "2.1.0-canary-20250227211824", | ||
"@solana/errors": "2.1.0-canary-20250227211824" | ||
}, | ||
@@ -63,0 +63,0 @@ "peerDependencies": { |
@@ -8,11 +8,11 @@ [![npm][npm-image]][npm-url] | ||
[code-style-prettier-url]: https://github.com/prettier/prettier | ||
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/codecs-strings/next.svg?style=flat | ||
[npm-image]: https://img.shields.io/npm/v/@solana/codecs-strings/next.svg?style=flat | ||
[npm-url]: https://www.npmjs.com/package/@solana/codecs-strings/v/next | ||
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/codecs-strings?style=flat | ||
[npm-image]: https://img.shields.io/npm/v/@solana/codecs-strings?style=flat | ||
[npm-url]: https://www.npmjs.com/package/@solana/codecs-strings | ||
# @solana/codecs-strings | ||
This package contains codecs for strings of different sizes and encodings. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@next`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library). | ||
This package contains codecs for strings of different sizes and encodings. It can be used standalone, but it is also exported as part of Kit [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit). | ||
This package is also part of the [`@solana/codecs` package](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs) which acts as an entry point for all codec packages as well as for their documentation. | ||
This package is also part of the [`@solana/codecs` package](https://github.com/anza-xyz/kit/tree/main/packages/codecs) which acts as an entry point for all codec packages as well as for their documentation. | ||
@@ -25,4 +25,4 @@ ## Sizing string codecs | ||
- When encoding a string, all bytes necessary to encode the string will be used. | ||
- When decoding a byte array at a given offset, all bytes starting from that offset will be decoded as a string. | ||
- When encoding a string, all bytes necessary to encode the string will be used. | ||
- When decoding a byte array at a given offset, all bytes starting from that offset will be decoded as a string. | ||
@@ -42,3 +42,3 @@ For instance, here's how you can encode/decode `utf8` strings without any size boundary: | ||
This might be what you want — e.g. when having a string at the end of a data structure — but in many cases, you might want to have a size boundary for your string. You may achieve this by composing your string codec with the [`fixCodecSize`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-core#fixing-the-size-of-codecs) or [`addCodecSizePrefix`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-core#prefixing-the-size-of-codecs) functions. | ||
This might be what you want — e.g. when having a string at the end of a data structure — but in many cases, you might want to have a size boundary for your string. You may achieve this by composing your string codec with the [`fixCodecSize`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-core#fixing-the-size-of-codecs) or [`addCodecSizePrefix`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-core#prefixing-the-size-of-codecs) functions. | ||
@@ -201,2 +201,2 @@ The `fixCodecSize` function accepts a fixed byte length and returns a `FixedSizeCodec<string>` that will always use that amount of bytes to encode and decode a string. Any string longer or smaller than that size will be truncated or padded respectively. Here's how you can use it with a `utf8` codec: | ||
To read more about the available codecs and how to use them, check out the documentation of the main [`@solana/codecs` package](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs). | ||
To read more about the available codecs and how to use them, check out the documentation of the main [`@solana/codecs` package](https://github.com/anza-xyz/kit/tree/main/packages/codecs). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
324573
2106
+ Added@solana/codecs-core@2.1.0-canary-20250227211824(transitive)
+ Added@solana/codecs-numbers@2.1.0-canary-20250227211824(transitive)
+ Added@solana/errors@2.1.0-canary-20250227211824(transitive)
+ Addedcommander@13.1.0(transitive)
- Removed@solana/codecs-core@2.1.0-canary-20241211084732(transitive)
- Removed@solana/codecs-numbers@2.1.0-canary-20241211084732(transitive)
- Removed@solana/errors@2.1.0-canary-20241211084732(transitive)
- Removedcommander@12.1.0(transitive)