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

20
node_modules/@solana/codecs-strings/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,20 @@
Copyright (c) 2023 Solana Labs, Inc
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

198
node_modules/@solana/codecs-strings/README.md generated vendored Normal file
View File

@@ -0,0 +1,198 @@
[![npm][npm-image]][npm-url]
[![npm-downloads][npm-downloads-image]][npm-url]
<br />
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
[code-style-prettier-url]: https://github.com/prettier/prettier
[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 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"
```
---
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).

View File

@@ -0,0 +1,288 @@
'use strict';
var errors = require('@solana/errors');
var codecsCore = require('@solana/codecs-core');
// src/assertions.ts
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet4,
base: alphabet4.length,
value: givenValue
});
}
}
var getBaseXEncoder = (alphabet4) => {
return codecsCore.createEncoder({
getSizeFromValue: (value) => {
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) return value.length;
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
},
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) {
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
return offset + leadingZeroes.length;
}
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
const tailBytes = [];
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;
}
});
};
var getBaseXDecoder = (alphabet4) => {
return codecsCore.createDecoder({
read(rawBytes, offset) {
const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
if (bytes.length === 0) return ["", 0];
let trailIndex = bytes.findIndex((n) => n !== 0);
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
const leadingZeroes = alphabet4[0].repeat(trailIndex);
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
return [leadingZeroes + tailChars, rawBytes.length];
}
});
};
var getBaseXCodec = (alphabet4) => codecsCore.combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
function partitionLeadingZeroes(value, zeroCharacter) {
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
return [leadingZeros, tailChars];
}
function getBigIntFromBaseX(value, alphabet4) {
const base = BigInt(alphabet4.length);
let sum = 0n;
for (const char of value) {
sum *= base;
sum += BigInt(alphabet4.indexOf(char));
}
return sum;
}
function getBaseXFromBigInt(value, alphabet4) {
const base = BigInt(alphabet4.length);
const tailChars = [];
while (value > 0n) {
tailChars.unshift(alphabet4[Number(value % base)]);
value /= base;
}
return tailChars.join("");
}
// src/base10.ts
var alphabet = "0123456789";
var getBase10Encoder = () => getBaseXEncoder(alphabet);
var getBase10Decoder = () => getBaseXDecoder(alphabet);
var getBase10Codec = () => getBaseXCodec(alphabet);
var INVALID_STRING_ERROR_BASE_CONFIG = {
alphabet: "0123456789abcdef",
base: 16
};
function charCodeToBase16(char) {
if (char >= 48 /* ZERO */ && char <= 57 /* NINE */) return char - 48 /* ZERO */;
if (char >= 65 /* A_UP */ && char <= 70 /* F_UP */) return char - (65 /* A_UP */ - 10);
if (char >= 97 /* A_LO */ && char <= 102 /* F_LO */) return char - (97 /* A_LO */ - 10);
}
var getBase16Encoder = () => codecsCore.createEncoder({
getSizeFromValue: (value) => Math.ceil(value.length / 2),
write(value, bytes, offset) {
const len = value.length;
const al = len / 2;
if (len === 1) {
const c = value.charCodeAt(0);
const n = charCodeToBase16(c);
if (n === void 0) {
throw new errors.SolanaError(errors.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 === void 0 || n2 === void 0 && !Number.isNaN(c2)) {
throw new errors.SolanaError(errors.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;
}
});
var getBase16Decoder = () => codecsCore.createDecoder({
read(bytes, offset) {
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
return [value, bytes.length];
}
});
var getBase16Codec = () => codecsCore.combineCodec(getBase16Encoder(), getBase16Decoder());
// src/base58.ts
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
var getBase58Codec = () => getBaseXCodec(alphabet2);
var getBaseXResliceEncoder = (alphabet4, bits) => codecsCore.createEncoder({
getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
const reslicedBytes = reslice(charIndices, bits, 8, false);
bytes.set(reslicedBytes, offset);
return reslicedBytes.length + offset;
}
});
var getBaseXResliceDecoder = (alphabet4, bits) => codecsCore.createDecoder({
read(rawBytes, offset = 0) {
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) => alphabet4[i]).join(""), rawBytes.length];
}
});
var getBaseXResliceCodec = (alphabet4, bits) => codecsCore.combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
function reslice(input, inputBits, outputBits, useRemainder) {
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;
}
// src/base64.ts
var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var getBase64Encoder = () => {
{
return codecsCore.createEncoder({
getSizeFromValue: (value) => {
try {
return atob(value).length;
} catch {
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet3,
base: 64,
value
});
}
},
write(value, bytes, offset) {
try {
const bytesToAdd = atob(value).split("").map((c) => c.charCodeAt(0));
bytes.set(bytesToAdd, offset);
return bytesToAdd.length + offset;
} catch {
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet3,
base: 64,
value
});
}
}
});
}
};
var getBase64Decoder = () => {
{
return codecsCore.createDecoder({
read(bytes, offset = 0) {
const slice = bytes.slice(offset);
const value = btoa(String.fromCharCode(...slice));
return [value, bytes.length];
}
});
}
};
var getBase64Codec = () => codecsCore.combineCodec(getBase64Encoder(), getBase64Decoder());
// src/null-characters.ts
var removeNullCharacters = (value) => (
// eslint-disable-next-line no-control-regex
value.replace(/\u0000/g, "")
);
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
// ../text-encoding-impl/dist/index.browser.mjs
var e = globalThis.TextDecoder;
var o = globalThis.TextEncoder;
// src/utf8.ts
var getUtf8Encoder = () => {
let textEncoder;
return codecsCore.createEncoder({
getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
write: (value, bytes, offset) => {
const bytesToAdd = (textEncoder ||= new o()).encode(value);
bytes.set(bytesToAdd, offset);
return offset + bytesToAdd.length;
}
});
};
var getUtf8Decoder = () => {
let textDecoder;
return codecsCore.createDecoder({
read(bytes, offset) {
const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
return [removeNullCharacters(value), bytes.length];
}
});
};
var getUtf8Codec = () => codecsCore.combineCodec(getUtf8Encoder(), getUtf8Decoder());
exports.assertValidBaseString = assertValidBaseString;
exports.getBase10Codec = getBase10Codec;
exports.getBase10Decoder = getBase10Decoder;
exports.getBase10Encoder = getBase10Encoder;
exports.getBase16Codec = getBase16Codec;
exports.getBase16Decoder = getBase16Decoder;
exports.getBase16Encoder = getBase16Encoder;
exports.getBase58Codec = getBase58Codec;
exports.getBase58Decoder = getBase58Decoder;
exports.getBase58Encoder = getBase58Encoder;
exports.getBase64Codec = getBase64Codec;
exports.getBase64Decoder = getBase64Decoder;
exports.getBase64Encoder = getBase64Encoder;
exports.getBaseXCodec = getBaseXCodec;
exports.getBaseXDecoder = getBaseXDecoder;
exports.getBaseXEncoder = getBaseXEncoder;
exports.getBaseXResliceCodec = getBaseXResliceCodec;
exports.getBaseXResliceDecoder = getBaseXResliceDecoder;
exports.getBaseXResliceEncoder = getBaseXResliceEncoder;
exports.getUtf8Codec = getUtf8Codec;
exports.getUtf8Decoder = getUtf8Decoder;
exports.getUtf8Encoder = getUtf8Encoder;
exports.padNullCharacters = padNullCharacters;
exports.removeNullCharacters = removeNullCharacters;
//# sourceMappingURL=index.browser.cjs.map
//# sourceMappingURL=index.browser.cjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,263 @@
import { SolanaError, SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE } from '@solana/errors';
import { combineCodec, createDecoder, createEncoder } from '@solana/codecs-core';
// src/assertions.ts
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet4,
base: alphabet4.length,
value: givenValue
});
}
}
var getBaseXEncoder = (alphabet4) => {
return createEncoder({
getSizeFromValue: (value) => {
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) return value.length;
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
},
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) {
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
return offset + leadingZeroes.length;
}
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
const tailBytes = [];
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;
}
});
};
var getBaseXDecoder = (alphabet4) => {
return createDecoder({
read(rawBytes, offset) {
const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
if (bytes.length === 0) return ["", 0];
let trailIndex = bytes.findIndex((n) => n !== 0);
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
const leadingZeroes = alphabet4[0].repeat(trailIndex);
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
return [leadingZeroes + tailChars, rawBytes.length];
}
});
};
var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
function partitionLeadingZeroes(value, zeroCharacter) {
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
return [leadingZeros, tailChars];
}
function getBigIntFromBaseX(value, alphabet4) {
const base = BigInt(alphabet4.length);
let sum = 0n;
for (const char of value) {
sum *= base;
sum += BigInt(alphabet4.indexOf(char));
}
return sum;
}
function getBaseXFromBigInt(value, alphabet4) {
const base = BigInt(alphabet4.length);
const tailChars = [];
while (value > 0n) {
tailChars.unshift(alphabet4[Number(value % base)]);
value /= base;
}
return tailChars.join("");
}
// src/base10.ts
var alphabet = "0123456789";
var getBase10Encoder = () => getBaseXEncoder(alphabet);
var getBase10Decoder = () => getBaseXDecoder(alphabet);
var getBase10Codec = () => getBaseXCodec(alphabet);
var INVALID_STRING_ERROR_BASE_CONFIG = {
alphabet: "0123456789abcdef",
base: 16
};
function charCodeToBase16(char) {
if (char >= 48 /* ZERO */ && char <= 57 /* NINE */) return char - 48 /* ZERO */;
if (char >= 65 /* A_UP */ && char <= 70 /* F_UP */) return char - (65 /* A_UP */ - 10);
if (char >= 97 /* A_LO */ && char <= 102 /* F_LO */) return char - (97 /* A_LO */ - 10);
}
var getBase16Encoder = () => createEncoder({
getSizeFromValue: (value) => Math.ceil(value.length / 2),
write(value, bytes, offset) {
const len = value.length;
const al = len / 2;
if (len === 1) {
const c = value.charCodeAt(0);
const n = charCodeToBase16(c);
if (n === void 0) {
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 === void 0 || n2 === void 0 && !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;
}
});
var getBase16Decoder = () => createDecoder({
read(bytes, offset) {
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
return [value, bytes.length];
}
});
var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
// src/base58.ts
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
var getBase58Codec = () => getBaseXCodec(alphabet2);
var getBaseXResliceEncoder = (alphabet4, bits) => createEncoder({
getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
const reslicedBytes = reslice(charIndices, bits, 8, false);
bytes.set(reslicedBytes, offset);
return reslicedBytes.length + offset;
}
});
var getBaseXResliceDecoder = (alphabet4, bits) => createDecoder({
read(rawBytes, offset = 0) {
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) => alphabet4[i]).join(""), rawBytes.length];
}
});
var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
function reslice(input, inputBits, outputBits, useRemainder) {
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;
}
// src/base64.ts
var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var getBase64Encoder = () => {
{
return createEncoder({
getSizeFromValue: (value) => {
try {
return atob(value).length;
} catch {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet3,
base: 64,
value
});
}
},
write(value, bytes, offset) {
try {
const bytesToAdd = 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: alphabet3,
base: 64,
value
});
}
}
});
}
};
var getBase64Decoder = () => {
{
return createDecoder({
read(bytes, offset = 0) {
const slice = bytes.slice(offset);
const value = btoa(String.fromCharCode(...slice));
return [value, bytes.length];
}
});
}
};
var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
// src/null-characters.ts
var removeNullCharacters = (value) => (
// eslint-disable-next-line no-control-regex
value.replace(/\u0000/g, "")
);
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
// ../text-encoding-impl/dist/index.browser.mjs
var e = globalThis.TextDecoder;
var o = globalThis.TextEncoder;
// src/utf8.ts
var getUtf8Encoder = () => {
let textEncoder;
return createEncoder({
getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
write: (value, bytes, offset) => {
const bytesToAdd = (textEncoder ||= new o()).encode(value);
bytes.set(bytesToAdd, offset);
return offset + bytesToAdd.length;
}
});
};
var getUtf8Decoder = () => {
let textDecoder;
return createDecoder({
read(bytes, offset) {
const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
return [removeNullCharacters(value), bytes.length];
}
});
};
var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
export { assertValidBaseString, getBase10Codec, getBase10Decoder, getBase10Encoder, getBase16Codec, getBase16Decoder, getBase16Encoder, getBase58Codec, getBase58Decoder, getBase58Encoder, getBase64Codec, getBase64Decoder, getBase64Encoder, getBaseXCodec, getBaseXDecoder, getBaseXEncoder, getBaseXResliceCodec, getBaseXResliceDecoder, getBaseXResliceEncoder, getUtf8Codec, getUtf8Decoder, getUtf8Encoder, padNullCharacters, removeNullCharacters };
//# sourceMappingURL=index.browser.mjs.map
//# sourceMappingURL=index.browser.mjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,231 @@
import { SolanaError, SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE } from '@solana/errors';
import { combineCodec, createDecoder, createEncoder, transformEncoder, transformDecoder } from '@solana/codecs-core';
// src/assertions.ts
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet4,
base: alphabet4.length,
value: givenValue
});
}
}
var getBaseXEncoder = (alphabet4) => {
return createEncoder({
getSizeFromValue: (value) => {
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) return value.length;
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
},
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) {
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
return offset + leadingZeroes.length;
}
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
const tailBytes = [];
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;
}
});
};
var getBaseXDecoder = (alphabet4) => {
return createDecoder({
read(rawBytes, offset) {
const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
if (bytes.length === 0) return ["", 0];
let trailIndex = bytes.findIndex((n) => n !== 0);
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
const leadingZeroes = alphabet4[0].repeat(trailIndex);
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
return [leadingZeroes + tailChars, rawBytes.length];
}
});
};
var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
function partitionLeadingZeroes(value, zeroCharacter) {
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
return [leadingZeros, tailChars];
}
function getBigIntFromBaseX(value, alphabet4) {
const base = BigInt(alphabet4.length);
let sum = 0n;
for (const char of value) {
sum *= base;
sum += BigInt(alphabet4.indexOf(char));
}
return sum;
}
function getBaseXFromBigInt(value, alphabet4) {
const base = BigInt(alphabet4.length);
const tailChars = [];
while (value > 0n) {
tailChars.unshift(alphabet4[Number(value % base)]);
value /= base;
}
return tailChars.join("");
}
// src/base10.ts
var alphabet = "0123456789";
var getBase10Encoder = () => getBaseXEncoder(alphabet);
var getBase10Decoder = () => getBaseXDecoder(alphabet);
var getBase10Codec = () => getBaseXCodec(alphabet);
var INVALID_STRING_ERROR_BASE_CONFIG = {
alphabet: "0123456789abcdef",
base: 16
};
function charCodeToBase16(char) {
if (char >= 48 /* ZERO */ && char <= 57 /* NINE */) return char - 48 /* ZERO */;
if (char >= 65 /* A_UP */ && char <= 70 /* F_UP */) return char - (65 /* A_UP */ - 10);
if (char >= 97 /* A_LO */ && char <= 102 /* F_LO */) return char - (97 /* A_LO */ - 10);
}
var getBase16Encoder = () => createEncoder({
getSizeFromValue: (value) => Math.ceil(value.length / 2),
write(value, bytes, offset) {
const len = value.length;
const al = len / 2;
if (len === 1) {
const c = value.charCodeAt(0);
const n = charCodeToBase16(c);
if (n === void 0) {
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 === void 0 || n2 === void 0 && !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;
}
});
var getBase16Decoder = () => createDecoder({
read(bytes, offset) {
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
return [value, bytes.length];
}
});
var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
// src/base58.ts
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
var getBase58Codec = () => getBaseXCodec(alphabet2);
var getBaseXResliceEncoder = (alphabet4, bits) => createEncoder({
getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
const reslicedBytes = reslice(charIndices, bits, 8, false);
bytes.set(reslicedBytes, offset);
return reslicedBytes.length + offset;
}
});
var getBaseXResliceDecoder = (alphabet4, bits) => createDecoder({
read(rawBytes, offset = 0) {
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) => alphabet4[i]).join(""), rawBytes.length];
}
});
var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
function reslice(input, inputBits, outputBits, useRemainder) {
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;
}
// src/base64.ts
var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var getBase64Encoder = () => {
return transformEncoder(getBaseXResliceEncoder(alphabet3, 6), (value) => value.replace(/=/g, ""));
};
var getBase64Decoder = () => {
return transformDecoder(
getBaseXResliceDecoder(alphabet3, 6),
(value) => value.padEnd(Math.ceil(value.length / 4) * 4, "=")
);
};
var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
// src/null-characters.ts
var removeNullCharacters = (value) => (
// eslint-disable-next-line no-control-regex
value.replace(/\u0000/g, "")
);
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
// ../text-encoding-impl/dist/index.browser.mjs
var e = globalThis.TextDecoder;
var o = globalThis.TextEncoder;
// src/utf8.ts
var getUtf8Encoder = () => {
let textEncoder;
return createEncoder({
getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
write: (value, bytes, offset) => {
const bytesToAdd = (textEncoder ||= new o()).encode(value);
bytes.set(bytesToAdd, offset);
return offset + bytesToAdd.length;
}
});
};
var getUtf8Decoder = () => {
let textDecoder;
return createDecoder({
read(bytes, offset) {
const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
return [removeNullCharacters(value), bytes.length];
}
});
};
var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
export { assertValidBaseString, getBase10Codec, getBase10Decoder, getBase10Encoder, getBase16Codec, getBase16Decoder, getBase16Encoder, getBase58Codec, getBase58Decoder, getBase58Encoder, getBase64Codec, getBase64Decoder, getBase64Encoder, getBaseXCodec, getBaseXDecoder, getBaseXEncoder, getBaseXResliceCodec, getBaseXResliceDecoder, getBaseXResliceEncoder, getUtf8Codec, getUtf8Decoder, getUtf8Encoder, padNullCharacters, removeNullCharacters };
//# sourceMappingURL=index.native.mjs.map
//# sourceMappingURL=index.native.mjs.map

File diff suppressed because one or more lines are too long

267
node_modules/@solana/codecs-strings/dist/index.node.cjs generated vendored Normal file
View File

@@ -0,0 +1,267 @@
'use strict';
var errors = require('@solana/errors');
var codecsCore = require('@solana/codecs-core');
// src/assertions.ts
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
throw new errors.SolanaError(errors.SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet4,
base: alphabet4.length,
value: givenValue
});
}
}
var getBaseXEncoder = (alphabet4) => {
return codecsCore.createEncoder({
getSizeFromValue: (value) => {
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) return value.length;
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
},
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) {
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
return offset + leadingZeroes.length;
}
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
const tailBytes = [];
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;
}
});
};
var getBaseXDecoder = (alphabet4) => {
return codecsCore.createDecoder({
read(rawBytes, offset) {
const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
if (bytes.length === 0) return ["", 0];
let trailIndex = bytes.findIndex((n) => n !== 0);
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
const leadingZeroes = alphabet4[0].repeat(trailIndex);
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
return [leadingZeroes + tailChars, rawBytes.length];
}
});
};
var getBaseXCodec = (alphabet4) => codecsCore.combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
function partitionLeadingZeroes(value, zeroCharacter) {
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
return [leadingZeros, tailChars];
}
function getBigIntFromBaseX(value, alphabet4) {
const base = BigInt(alphabet4.length);
let sum = 0n;
for (const char of value) {
sum *= base;
sum += BigInt(alphabet4.indexOf(char));
}
return sum;
}
function getBaseXFromBigInt(value, alphabet4) {
const base = BigInt(alphabet4.length);
const tailChars = [];
while (value > 0n) {
tailChars.unshift(alphabet4[Number(value % base)]);
value /= base;
}
return tailChars.join("");
}
// src/base10.ts
var alphabet = "0123456789";
var getBase10Encoder = () => getBaseXEncoder(alphabet);
var getBase10Decoder = () => getBaseXDecoder(alphabet);
var getBase10Codec = () => getBaseXCodec(alphabet);
var INVALID_STRING_ERROR_BASE_CONFIG = {
alphabet: "0123456789abcdef",
base: 16
};
function charCodeToBase16(char) {
if (char >= 48 /* ZERO */ && char <= 57 /* NINE */) return char - 48 /* ZERO */;
if (char >= 65 /* A_UP */ && char <= 70 /* F_UP */) return char - (65 /* A_UP */ - 10);
if (char >= 97 /* A_LO */ && char <= 102 /* F_LO */) return char - (97 /* A_LO */ - 10);
}
var getBase16Encoder = () => codecsCore.createEncoder({
getSizeFromValue: (value) => Math.ceil(value.length / 2),
write(value, bytes, offset) {
const len = value.length;
const al = len / 2;
if (len === 1) {
const c = value.charCodeAt(0);
const n = charCodeToBase16(c);
if (n === void 0) {
throw new errors.SolanaError(errors.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 === void 0 || n2 === void 0 && !Number.isNaN(c2)) {
throw new errors.SolanaError(errors.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;
}
});
var getBase16Decoder = () => codecsCore.createDecoder({
read(bytes, offset) {
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
return [value, bytes.length];
}
});
var getBase16Codec = () => codecsCore.combineCodec(getBase16Encoder(), getBase16Decoder());
// src/base58.ts
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
var getBase58Codec = () => getBaseXCodec(alphabet2);
var getBaseXResliceEncoder = (alphabet4, bits) => codecsCore.createEncoder({
getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
const reslicedBytes = reslice(charIndices, bits, 8, false);
bytes.set(reslicedBytes, offset);
return reslicedBytes.length + offset;
}
});
var getBaseXResliceDecoder = (alphabet4, bits) => codecsCore.createDecoder({
read(rawBytes, offset = 0) {
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) => alphabet4[i]).join(""), rawBytes.length];
}
});
var getBaseXResliceCodec = (alphabet4, bits) => codecsCore.combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
function reslice(input, inputBits, outputBits, useRemainder) {
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;
}
// src/base64.ts
var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var getBase64Encoder = () => {
{
return codecsCore.createEncoder({
getSizeFromValue: (value) => Buffer.from(value, "base64").length,
write(value, bytes, offset) {
assertValidBaseString(alphabet3, value.replace(/=/g, ""));
const buffer = Buffer.from(value, "base64");
bytes.set(buffer, offset);
return buffer.length + offset;
}
});
}
};
var getBase64Decoder = () => {
{
return codecsCore.createDecoder({
read: (bytes, offset = 0) => [Buffer.from(codecsCore.toArrayBuffer(bytes), offset).toString("base64"), bytes.length]
});
}
};
var getBase64Codec = () => codecsCore.combineCodec(getBase64Encoder(), getBase64Decoder());
// src/null-characters.ts
var removeNullCharacters = (value) => (
// eslint-disable-next-line no-control-regex
value.replace(/\u0000/g, "")
);
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
// ../text-encoding-impl/dist/index.node.mjs
var e = globalThis.TextDecoder;
var o = globalThis.TextEncoder;
// src/utf8.ts
var getUtf8Encoder = () => {
let textEncoder;
return codecsCore.createEncoder({
getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
write: (value, bytes, offset) => {
const bytesToAdd = (textEncoder ||= new o()).encode(value);
bytes.set(bytesToAdd, offset);
return offset + bytesToAdd.length;
}
});
};
var getUtf8Decoder = () => {
let textDecoder;
return codecsCore.createDecoder({
read(bytes, offset) {
const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
return [removeNullCharacters(value), bytes.length];
}
});
};
var getUtf8Codec = () => codecsCore.combineCodec(getUtf8Encoder(), getUtf8Decoder());
exports.assertValidBaseString = assertValidBaseString;
exports.getBase10Codec = getBase10Codec;
exports.getBase10Decoder = getBase10Decoder;
exports.getBase10Encoder = getBase10Encoder;
exports.getBase16Codec = getBase16Codec;
exports.getBase16Decoder = getBase16Decoder;
exports.getBase16Encoder = getBase16Encoder;
exports.getBase58Codec = getBase58Codec;
exports.getBase58Decoder = getBase58Decoder;
exports.getBase58Encoder = getBase58Encoder;
exports.getBase64Codec = getBase64Codec;
exports.getBase64Decoder = getBase64Decoder;
exports.getBase64Encoder = getBase64Encoder;
exports.getBaseXCodec = getBaseXCodec;
exports.getBaseXDecoder = getBaseXDecoder;
exports.getBaseXEncoder = getBaseXEncoder;
exports.getBaseXResliceCodec = getBaseXResliceCodec;
exports.getBaseXResliceDecoder = getBaseXResliceDecoder;
exports.getBaseXResliceEncoder = getBaseXResliceEncoder;
exports.getUtf8Codec = getUtf8Codec;
exports.getUtf8Decoder = getUtf8Decoder;
exports.getUtf8Encoder = getUtf8Encoder;
exports.padNullCharacters = padNullCharacters;
exports.removeNullCharacters = removeNullCharacters;
//# sourceMappingURL=index.node.cjs.map
//# sourceMappingURL=index.node.cjs.map

File diff suppressed because one or more lines are too long

242
node_modules/@solana/codecs-strings/dist/index.node.mjs generated vendored Normal file
View File

@@ -0,0 +1,242 @@
import { SolanaError, SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE } from '@solana/errors';
import { combineCodec, createDecoder, createEncoder, toArrayBuffer } from '@solana/codecs-core';
// src/assertions.ts
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
alphabet: alphabet4,
base: alphabet4.length,
value: givenValue
});
}
}
var getBaseXEncoder = (alphabet4) => {
return createEncoder({
getSizeFromValue: (value) => {
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) return value.length;
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
},
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
if (!tailChars) {
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
return offset + leadingZeroes.length;
}
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
const tailBytes = [];
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;
}
});
};
var getBaseXDecoder = (alphabet4) => {
return createDecoder({
read(rawBytes, offset) {
const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
if (bytes.length === 0) return ["", 0];
let trailIndex = bytes.findIndex((n) => n !== 0);
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
const leadingZeroes = alphabet4[0].repeat(trailIndex);
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
return [leadingZeroes + tailChars, rawBytes.length];
}
});
};
var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
function partitionLeadingZeroes(value, zeroCharacter) {
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
return [leadingZeros, tailChars];
}
function getBigIntFromBaseX(value, alphabet4) {
const base = BigInt(alphabet4.length);
let sum = 0n;
for (const char of value) {
sum *= base;
sum += BigInt(alphabet4.indexOf(char));
}
return sum;
}
function getBaseXFromBigInt(value, alphabet4) {
const base = BigInt(alphabet4.length);
const tailChars = [];
while (value > 0n) {
tailChars.unshift(alphabet4[Number(value % base)]);
value /= base;
}
return tailChars.join("");
}
// src/base10.ts
var alphabet = "0123456789";
var getBase10Encoder = () => getBaseXEncoder(alphabet);
var getBase10Decoder = () => getBaseXDecoder(alphabet);
var getBase10Codec = () => getBaseXCodec(alphabet);
var INVALID_STRING_ERROR_BASE_CONFIG = {
alphabet: "0123456789abcdef",
base: 16
};
function charCodeToBase16(char) {
if (char >= 48 /* ZERO */ && char <= 57 /* NINE */) return char - 48 /* ZERO */;
if (char >= 65 /* A_UP */ && char <= 70 /* F_UP */) return char - (65 /* A_UP */ - 10);
if (char >= 97 /* A_LO */ && char <= 102 /* F_LO */) return char - (97 /* A_LO */ - 10);
}
var getBase16Encoder = () => createEncoder({
getSizeFromValue: (value) => Math.ceil(value.length / 2),
write(value, bytes, offset) {
const len = value.length;
const al = len / 2;
if (len === 1) {
const c = value.charCodeAt(0);
const n = charCodeToBase16(c);
if (n === void 0) {
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 === void 0 || n2 === void 0 && !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;
}
});
var getBase16Decoder = () => createDecoder({
read(bytes, offset) {
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
return [value, bytes.length];
}
});
var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
// src/base58.ts
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
var getBase58Codec = () => getBaseXCodec(alphabet2);
var getBaseXResliceEncoder = (alphabet4, bits) => createEncoder({
getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
write(value, bytes, offset) {
assertValidBaseString(alphabet4, value);
if (value === "") return offset;
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
const reslicedBytes = reslice(charIndices, bits, 8, false);
bytes.set(reslicedBytes, offset);
return reslicedBytes.length + offset;
}
});
var getBaseXResliceDecoder = (alphabet4, bits) => createDecoder({
read(rawBytes, offset = 0) {
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) => alphabet4[i]).join(""), rawBytes.length];
}
});
var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
function reslice(input, inputBits, outputBits, useRemainder) {
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;
}
// src/base64.ts
var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var getBase64Encoder = () => {
{
return createEncoder({
getSizeFromValue: (value) => Buffer.from(value, "base64").length,
write(value, bytes, offset) {
assertValidBaseString(alphabet3, value.replace(/=/g, ""));
const buffer = Buffer.from(value, "base64");
bytes.set(buffer, offset);
return buffer.length + offset;
}
});
}
};
var getBase64Decoder = () => {
{
return createDecoder({
read: (bytes, offset = 0) => [Buffer.from(toArrayBuffer(bytes), offset).toString("base64"), bytes.length]
});
}
};
var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
// src/null-characters.ts
var removeNullCharacters = (value) => (
// eslint-disable-next-line no-control-regex
value.replace(/\u0000/g, "")
);
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
// ../text-encoding-impl/dist/index.node.mjs
var e = globalThis.TextDecoder;
var o = globalThis.TextEncoder;
// src/utf8.ts
var getUtf8Encoder = () => {
let textEncoder;
return createEncoder({
getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
write: (value, bytes, offset) => {
const bytesToAdd = (textEncoder ||= new o()).encode(value);
bytes.set(bytesToAdd, offset);
return offset + bytesToAdd.length;
}
});
};
var getUtf8Decoder = () => {
let textDecoder;
return createDecoder({
read(bytes, offset) {
const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
return [removeNullCharacters(value), bytes.length];
}
});
};
var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
export { assertValidBaseString, getBase10Codec, getBase10Decoder, getBase10Encoder, getBase16Codec, getBase16Decoder, getBase16Encoder, getBase58Codec, getBase58Decoder, getBase58Encoder, getBase64Codec, getBase64Decoder, getBase64Encoder, getBaseXCodec, getBaseXDecoder, getBaseXEncoder, getBaseXResliceCodec, getBaseXResliceDecoder, getBaseXResliceEncoder, getUtf8Codec, getUtf8Decoder, getUtf8Encoder, padNullCharacters, removeNullCharacters };
//# sourceMappingURL=index.node.mjs.map
//# sourceMappingURL=index.node.mjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,22 @@
/**
* 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

View File

@@ -0,0 +1 @@
{"version":3,"file":"assertions.d.ts","sourceRoot":"","sources":["../../src/assertions.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,SAAY,QAQhG"}

View File

@@ -0,0 +1,82 @@
/**
* 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>;
/**
* 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>;
/**
* 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

View File

@@ -0,0 +1 @@
{"version":3,"file":"base10.d.ts","sourceRoot":"","sources":["../../src/base10.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,gBAAgB,iEAAkC,CAAC;AAEhE;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,iEAAkC,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,cAAc,+DAAgC,CAAC"}

View File

@@ -0,0 +1,83 @@
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
/**
* 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>;
/**
* 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>;
/**
* 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

View File

@@ -0,0 +1 @@
{"version":3,"file":"base16.d.ts","sourceRoot":"","sources":["../../src/base16.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,qBAAqB,CAAC;AAuB7B;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,gBAAgB,QAAO,mBAAmB,CAAC,MAAM,CAqCxD,CAAC;AAEP;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,QAAO,mBAAmB,CAAC,MAAM,CAMxD,CAAC;AAEP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,cAAc,QAAO,iBAAiB,CAAC,MAAM,CAAyD,CAAC"}

View File

@@ -0,0 +1,82 @@
/**
* 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>;
/**
* 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>;
/**
* 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

View File

@@ -0,0 +1 @@
{"version":3,"file":"base58.d.ts","sourceRoot":"","sources":["../../src/base58.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,gBAAgB,iEAAkC,CAAC;AAEhE;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,iEAAkC,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,cAAc,+DAAgC,CAAC"}

View File

@@ -0,0 +1,83 @@
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
/**
* 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>;
/**
* 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>;
/**
* 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

View File

@@ -0,0 +1 @@
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,EAOH,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,qBAAqB,CAAC;AAQ7B;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,gBAAgB,QAAO,mBAAmB,CAAC,MAAM,CA6C7D,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,QAAO,mBAAmB,CAAC,MAAM,CAoB7D,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,cAAc,QAAO,iBAAiB,CAAC,MAAM,CAAyD,CAAC"}

View File

@@ -0,0 +1,96 @@
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
/**
* 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>;
/**
* 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>;
/**
* 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 declare const getBaseXResliceCodec: (alphabet: string, bits: number) => VariableSizeCodec<string>;
//# sourceMappingURL=baseX-reslice.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"baseX-reslice.d.ts","sourceRoot":"","sources":["../../src/baseX-reslice.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,qBAAqB,CAAC;AAI7B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,sBAAsB,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,KAAG,mBAAmB,CAAC,MAAM,CAW5F,CAAC;AAEP;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,sBAAsB,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,KAAG,mBAAmB,CAAC,MAAM,CAQ5F,CAAC;AAEP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,eAAO,MAAM,oBAAoB,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,KAAG,iBAAiB,CAAC,MAAM,CACE,CAAC"}

View File

@@ -0,0 +1,92 @@
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
/**
* 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>;
/**
* 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>;
/**
* 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 declare const getBaseXCodec: (alphabet: string) => VariableSizeCodec<string>;
//# sourceMappingURL=baseX.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"baseX.d.ts","sourceRoot":"","sources":["../../src/baseX.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,qBAAqB,CAAC;AAI7B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,KAAG,mBAAmB,CAAC,MAAM,CAoC5E,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,KAAG,mBAAmB,CAAC,MAAM,CAqB5E,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,eAAO,MAAM,aAAa,GAAI,UAAU,MAAM,KAAG,iBAAiB,CAAC,MAAM,CACH,CAAC"}

View File

@@ -0,0 +1,211 @@
/**
* 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';
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwMG;AACH,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC"}

View File

@@ -0,0 +1,34 @@
/**
* 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 (`\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

View File

@@ -0,0 +1 @@
{"version":3,"file":"null-characters.d.ts","sourceRoot":"","sources":["../../src/null-characters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,oBAAoB,GAAI,OAAO,MAAM,WAElB,CAAC;AAEjC;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,iBAAiB,GAAI,OAAO,MAAM,EAAE,OAAO,MAAM,WAAkC,CAAC"}

View File

@@ -0,0 +1,84 @@
import { VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from '@solana/codecs-core';
/**
* 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>;
/**
* 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>;
/**
* 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

View File

@@ -0,0 +1 @@
{"version":3,"file":"utf8.d.ts","sourceRoot":"","sources":["../../src/utf8.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACtB,MAAM,qBAAqB,CAAC;AAK7B;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,cAAc,QAAO,mBAAmB,CAAC,MAAM,CAU3D,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,cAAc,QAAO,mBAAmB,CAAC,MAAM,CAQ3D,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,YAAY,QAAO,iBAAiB,CAAC,MAAM,CAAqD,CAAC"}

96
node_modules/@solana/codecs-strings/package.json generated vendored Normal file
View File

@@ -0,0 +1,96 @@
{
"name": "@solana/codecs-strings",
"version": "6.8.0",
"description": "Codecs for strings of different sizes and encodings",
"homepage": "https://www.solanakit.com/api#solanacodecs-strings",
"exports": {
"edge-light": {
"import": "./dist/index.node.mjs",
"require": "./dist/index.node.cjs"
},
"workerd": {
"import": "./dist/index.node.mjs",
"require": "./dist/index.node.cjs"
},
"browser": {
"import": "./dist/index.browser.mjs",
"require": "./dist/index.browser.cjs"
},
"node": {
"import": "./dist/index.node.mjs",
"require": "./dist/index.node.cjs"
},
"react-native": "./dist/index.native.mjs",
"types": "./dist/types/index.d.ts"
},
"browser": {
"./dist/index.node.cjs": "./dist/index.browser.cjs",
"./dist/index.node.mjs": "./dist/index.browser.mjs"
},
"main": "./dist/index.node.cjs",
"module": "./dist/index.node.mjs",
"react-native": "./dist/index.native.mjs",
"types": "./dist/types/index.d.ts",
"type": "commonjs",
"files": [
"./dist/",
"./src/"
],
"sideEffects": false,
"keywords": [
"blockchain",
"solana",
"web3"
],
"author": "Solana Labs Maintainers <maintainers@solanalabs.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/anza-xyz/kit"
},
"bugs": {
"url": "https://github.com/anza-xyz/kit/issues"
},
"browserslist": [
"supports bigint and not dead",
"maintained node versions"
],
"dependencies": {
"@solana/codecs-core": "6.8.0",
"@solana/errors": "6.8.0",
"@solana/codecs-numbers": "6.8.0"
},
"peerDependencies": {
"fastestsmallesttextencoderdecoder": "^1.0.22",
"typescript": ">=5.0.0"
},
"peerDependenciesMeta": {
"fastestsmallesttextencoderdecoder": {
"optional": true
},
"typescript": {
"optional": true
}
},
"engines": {
"node": ">=20.18.0"
},
"scripts": {
"benchmark": "./src/__benchmarks__/run.ts",
"compile:docs": "typedoc",
"compile:js": "tsup --config build-scripts/tsup.config.package.ts",
"compile:typedefs": "tsc -p ./tsconfig.declarations.json",
"dev": "NODE_OPTIONS=\"--no-experimental-webstorage\" jest -c ../../node_modules/@solana/test-config/jest-dev.config.js --rootDir . --watch",
"publish-impl": "npm view $npm_package_name@$npm_package_version > /dev/null 2>&1 || (pnpm publish --tag ${PUBLISH_TAG:-canary} --access public --no-git-checks && (([ -n \"${GITHUB_OUTPUT:-}\" ] && echo 'published=true' >> \"$GITHUB_OUTPUT\") || true) && (([ \"$PUBLISH_TAG\" != \"canary\" ] && ../build-scripts/maybe-tag-latest.ts --token \"$GITHUB_TOKEN\" $npm_package_name@$npm_package_version) || true))",
"publish-packages": "pnpm prepublishOnly && pnpm publish-impl",
"style:fix": "pnpm eslint --fix src && pnpm prettier --log-level warn --ignore-unknown --write ./*",
"test:lint": "TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-lint.config.js --rootDir . --silent",
"test:prettier": "TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-prettier.config.js --rootDir . --silent",
"test:treeshakability:browser": "agadoo dist/index.browser.mjs",
"test:treeshakability:native": "agadoo dist/index.native.mjs",
"test:treeshakability:node": "agadoo dist/index.node.mjs",
"test:typecheck": "tsc --noEmit",
"test:unit:browser": "NODE_OPTIONS=\"--no-experimental-webstorage\" TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-unit.config.browser.js --rootDir . --silent",
"test:unit:node": "NODE_OPTIONS=\"--no-experimental-webstorage\" TERM_OVERRIDE=\"${TURBO_HASH:+dumb}\" TERM=${TERM_OVERRIDE:-$TERM} jest -c ../../node_modules/@solana/test-config/jest-unit.config.node.js --rootDir . --silent"
}
}

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());