FRE-600: Fix code review blockers

- Consolidated duplicate UndoManagers to single instance
- Fixed connection promise to only resolve on 'connected' status
- Fixed WebSocketProvider import (WebsocketProvider)
- Added proper doc.destroy() cleanup
- Renamed isPresenceInitialized property to avoid conflict

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-04-25 00:08:01 -04:00
parent 65b552bb08
commit 7c684a42cc
48450 changed files with 5679671 additions and 383 deletions

31
node_modules/@solana/codecs-strings/src/assertions.ts generated vendored Normal file
View File

@@ -0,0 +1,31 @@
import { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';
/**
* 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 function assertValidBaseString(alphabet: string, testValue: string, givenValue = testValue) {
if (!testValue.match(new RegExp(`^[${alphabet}]*$`))) {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet,
base: alphabet.length,
value: givenValue,
});
}
}

87
node_modules/@solana/codecs-strings/src/base10.ts generated vendored Normal file
View File

@@ -0,0 +1,87 @@
import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';
const alphabet = '0123456789';
/**
* 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 const getBase10Encoder = () => getBaseXEncoder(alphabet);
/**
* 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 const getBase10Decoder = () => getBaseXDecoder(alphabet);
/**
* 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 const getBase10Codec = () => getBaseXCodec(alphabet);

156
node_modules/@solana/codecs-strings/src/base16.ts generated vendored Normal file
View File

@@ -0,0 +1,156 @@
import {
combineCodec,
createDecoder,
createEncoder,
VariableSizeCodec,
VariableSizeDecoder,
VariableSizeEncoder,
} from '@solana/codecs-core';
import { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';
const enum HexC {
ZERO = 48, // 0
NINE = 57, // 9
A_UP = 65, // A
F_UP = 70, // F
A_LO = 97, // a
F_LO = 102, // f
}
const INVALID_STRING_ERROR_BASE_CONFIG = {
alphabet: '0123456789abcdef',
base: 16,
} as const;
function charCodeToBase16(char: number) {
if (char >= HexC.ZERO && char <= HexC.NINE) return char - HexC.ZERO;
if (char >= HexC.A_UP && char <= HexC.F_UP) return char - (HexC.A_UP - 10);
if (char >= HexC.A_LO && char <= HexC.F_LO) return char - (HexC.A_LO - 10);
}
/**
* 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 const getBase16Encoder = (): VariableSizeEncoder<string> =>
createEncoder({
getSizeFromValue: (value: string) => Math.ceil(value.length / 2),
write(value: string, bytes, offset) {
const len = value.length;
const al = len / 2;
if (len === 1) {
const c = value.charCodeAt(0);
const n = charCodeToBase16(c);
if (n === undefined) {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
...INVALID_STRING_ERROR_BASE_CONFIG,
value,
});
}
bytes.set([n], offset);
return 1 + offset;
}
const hexBytes = new Uint8Array(al);
for (let i = 0, j = 0; i < al; i++) {
const c1 = value.charCodeAt(j++);
const c2 = value.charCodeAt(j++);
const n1 = charCodeToBase16(c1);
const n2 = charCodeToBase16(c2);
if (n1 === undefined || (n2 === undefined && !Number.isNaN(c2))) {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
...INVALID_STRING_ERROR_BASE_CONFIG,
value,
});
}
hexBytes[i] = !Number.isNaN(c2) ? (n1 << 4) | (n2 ?? 0) : n1;
}
bytes.set(hexBytes, offset);
return hexBytes.length + offset;
},
});
/**
* 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 const getBase16Decoder = (): VariableSizeDecoder<string> =>
createDecoder({
read(bytes, offset) {
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
return [value, bytes.length];
},
});
/**
* 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 const getBase16Codec = (): VariableSizeCodec<string> => combineCodec(getBase16Encoder(), getBase16Decoder());

87
node_modules/@solana/codecs-strings/src/base58.ts generated vendored Normal file
View File

@@ -0,0 +1,87 @@
import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';
const alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
/**
* 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 const getBase58Encoder = () => getBaseXEncoder(alphabet);
/**
* 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 const getBase58Decoder = () => getBaseXDecoder(alphabet);
/**
* 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 const getBase58Codec = () => getBaseXCodec(alphabet);

166
node_modules/@solana/codecs-strings/src/base64.ts generated vendored Normal file
View File

@@ -0,0 +1,166 @@
import {
combineCodec,
createDecoder,
createEncoder,
toArrayBuffer,
transformDecoder,
transformEncoder,
VariableSizeCodec,
VariableSizeDecoder,
VariableSizeEncoder,
} from '@solana/codecs-core';
import { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';
import { assertValidBaseString } from './assertions';
import { getBaseXResliceDecoder, getBaseXResliceEncoder } from './baseX-reslice';
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
/**
* 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 const getBase64Encoder = (): VariableSizeEncoder<string> => {
if (__BROWSER__) {
return createEncoder({
getSizeFromValue: (value: string) => {
try {
return (atob as Window['atob'])(value).length;
} catch {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet,
base: 64,
value,
});
}
},
write(value: string, bytes, offset) {
try {
const bytesToAdd = (atob as Window['atob'])(value)
.split('')
.map(c => c.charCodeAt(0));
bytes.set(bytesToAdd, offset);
return bytesToAdd.length + offset;
} catch {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet,
base: 64,
value,
});
}
},
});
}
if (__NODEJS__) {
return createEncoder({
getSizeFromValue: (value: string) => Buffer.from(value, 'base64').length,
write(value: string, bytes, offset) {
assertValidBaseString(alphabet, value.replace(/=/g, ''));
const buffer = Buffer.from(value, 'base64');
bytes.set(buffer, offset);
return buffer.length + offset;
},
});
}
return transformEncoder(getBaseXResliceEncoder(alphabet, 6), (value: string): string => value.replace(/=/g, ''));
};
/**
* 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 const getBase64Decoder = (): VariableSizeDecoder<string> => {
if (__BROWSER__) {
return createDecoder({
read(bytes, offset = 0) {
const slice = bytes.slice(offset);
const value = (btoa as Window['btoa'])(String.fromCharCode(...slice));
return [value, bytes.length];
},
});
}
if (__NODEJS__) {
return createDecoder({
read: (bytes, offset = 0) => [Buffer.from(toArrayBuffer(bytes), offset).toString('base64'), bytes.length],
});
}
return transformDecoder(getBaseXResliceDecoder(alphabet, 6), (value: string): string =>
value.padEnd(Math.ceil(value.length / 4) * 4, '='),
);
};
/**
* 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 const getBase64Codec = (): VariableSizeCodec<string> => combineCodec(getBase64Encoder(), getBase64Decoder());

View File

@@ -0,0 +1,147 @@
import {
combineCodec,
createDecoder,
createEncoder,
VariableSizeCodec,
VariableSizeDecoder,
VariableSizeEncoder,
} from '@solana/codecs-core';
import { assertValidBaseString } from './assertions';
/**
* 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 const getBaseXResliceEncoder = (alphabet: string, bits: number): VariableSizeEncoder<string> =>
createEncoder({
getSizeFromValue: (value: string) => Math.floor((value.length * bits) / 8),
write(value: string, bytes, offset) {
assertValidBaseString(alphabet, value);
if (value === '') return offset;
const charIndices = [...value].map(c => alphabet.indexOf(c));
const reslicedBytes = reslice(charIndices, bits, 8, false);
bytes.set(reslicedBytes, offset);
return reslicedBytes.length + offset;
},
});
/**
* 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 const getBaseXResliceDecoder = (alphabet: string, bits: number): VariableSizeDecoder<string> =>
createDecoder({
read(rawBytes, offset = 0): [string, number] {
const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
if (bytes.length === 0) return ['', rawBytes.length];
const charIndices = reslice([...bytes], 8, bits, true);
return [charIndices.map(i => alphabet[i]).join(''), rawBytes.length];
},
});
/**
* Returns a codec for encoding and decoding base-X strings using bit re-slicing.
*
* 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 const getBaseXResliceCodec = (alphabet: string, bits: number): VariableSizeCodec<string> =>
combineCodec(getBaseXResliceEncoder(alphabet, bits), getBaseXResliceDecoder(alphabet, bits));
/** Helper function to reslice the bits inside bytes. */
function reslice(input: number[], inputBits: number, outputBits: number, useRemainder: boolean): number[] {
const output = [];
let accumulator = 0;
let bitsInAccumulator = 0;
const mask = (1 << outputBits) - 1;
for (const value of input) {
accumulator = (accumulator << inputBits) | value;
bitsInAccumulator += inputBits;
while (bitsInAccumulator >= outputBits) {
bitsInAccumulator -= outputBits;
output.push((accumulator >> bitsInAccumulator) & mask);
}
}
if (useRemainder && bitsInAccumulator > 0) {
output.push((accumulator << (outputBits - bitsInAccumulator)) & mask);
}
return output;
}

189
node_modules/@solana/codecs-strings/src/baseX.ts generated vendored Normal file
View File

@@ -0,0 +1,189 @@
import {
combineCodec,
createDecoder,
createEncoder,
VariableSizeCodec,
VariableSizeDecoder,
VariableSizeEncoder,
} from '@solana/codecs-core';
import { assertValidBaseString } from './assertions';
/**
* 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 const getBaseXEncoder = (alphabet: string): VariableSizeEncoder<string> => {
return createEncoder({
getSizeFromValue: (value: string): number => {
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);
if (!tailChars) return value.length;
const base10Number = getBigIntFromBaseX(tailChars, alphabet);
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
},
write(value: string, bytes, offset) {
// Check if the value is valid.
assertValidBaseString(alphabet, value);
if (value === '') return offset;
// Handle leading zeroes.
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);
if (!tailChars) {
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
return offset + leadingZeroes.length;
}
// From baseX to base10.
let base10Number = getBigIntFromBaseX(tailChars, alphabet);
// From base10 to bytes.
const tailBytes: number[] = [];
while (base10Number > 0n) {
tailBytes.unshift(Number(base10Number % 256n));
base10Number /= 256n;
}
const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
bytes.set(bytesToAdd, offset);
return offset + bytesToAdd.length;
},
});
};
/**
* 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 const getBaseXDecoder = (alphabet: string): VariableSizeDecoder<string> => {
return createDecoder({
read(rawBytes, offset): [string, number] {
const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
if (bytes.length === 0) return ['', 0];
// Handle leading zeroes.
let trailIndex = bytes.findIndex(n => n !== 0);
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
const leadingZeroes = alphabet[0].repeat(trailIndex);
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
// From bytes to base10.
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
// From base10 to baseX.
const tailChars = getBaseXFromBigInt(base10Number, alphabet);
return [leadingZeroes + tailChars, rawBytes.length];
},
});
};
/**
* Returns a codec for encoding and decoding base-X strings.
*
* 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 const getBaseXCodec = (alphabet: string): VariableSizeCodec<string> =>
combineCodec(getBaseXEncoder(alphabet), getBaseXDecoder(alphabet));
function partitionLeadingZeroes(
value: string,
zeroCharacter: string,
): [leadingZeros: string, tailChars: string | undefined] {
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
return [leadingZeros, tailChars];
}
function getBigIntFromBaseX(value: string, alphabet: string): bigint {
const base = BigInt(alphabet.length);
let sum = 0n;
for (const char of value) {
sum *= base;
sum += BigInt(alphabet.indexOf(char));
}
return sum;
}
function getBaseXFromBigInt(value: bigint, alphabet: string): string {
const base = BigInt(alphabet.length);
const tailChars = [];
while (value > 0n) {
tailChars.unshift(alphabet[Number(value % base)]);
value /= base;
}
return tailChars.join('');
}

210
node_modules/@solana/codecs-strings/src/index.ts generated vendored Normal file
View File

@@ -0,0 +1,210 @@
/**
* 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 alphabets 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';
export * from './base10';
export * from './base16';
export * from './base58';
export * from './base64';
export * from './baseX';
export * from './baseX-reslice';
export * from './null-characters';
export * from './utf8';

View File

@@ -0,0 +1,36 @@
/**
* 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 const removeNullCharacters = (value: string) =>
// eslint-disable-next-line no-control-regex
value.replace(/\u0000/g, '');
/**
* 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 const padNullCharacters = (value: string, chars: number) => value.padEnd(chars, '\u0000');

114
node_modules/@solana/codecs-strings/src/utf8.ts generated vendored Normal file
View File

@@ -0,0 +1,114 @@
import {
combineCodec,
createDecoder,
createEncoder,
VariableSizeCodec,
VariableSizeDecoder,
VariableSizeEncoder,
} from '@solana/codecs-core';
import { TextDecoder, TextEncoder } from '@solana/text-encoding-impl';
import { removeNullCharacters } from './null-characters';
/**
* 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 const getUtf8Encoder = (): VariableSizeEncoder<string> => {
let textEncoder: TextEncoder;
return createEncoder({
getSizeFromValue: value => (textEncoder ||= new TextEncoder()).encode(value).length,
write: (value: string, bytes, offset) => {
const bytesToAdd = (textEncoder ||= new TextEncoder()).encode(value);
bytes.set(bytesToAdd, offset);
return offset + bytesToAdd.length;
},
});
};
/**
* 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 const getUtf8Decoder = (): VariableSizeDecoder<string> => {
let textDecoder: TextDecoder;
return createDecoder({
read(bytes, offset) {
const value = (textDecoder ||= new TextDecoder()).decode(bytes.slice(offset));
return [removeNullCharacters(value), bytes.length];
},
});
};
/**
* 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 const getUtf8Codec = (): VariableSizeCodec<string> => combineCodec(getUtf8Encoder(), getUtf8Decoder());