@solana/codecs-strings 6.3.1 → 6.3.2-canary-20260313112147
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -5
- package/src/assertions.ts +31 -0
- package/src/base10.ts +87 -0
- package/src/base16.ts +156 -0
- package/src/base58.ts +87 -0
- package/src/base64.ts +166 -0
- package/src/baseX-reslice.ts +147 -0
- package/src/baseX.ts +189 -0
- package/src/index.ts +210 -0
- package/src/null-characters.ts +36 -0
- package/src/utf8.ts +114 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/codecs-strings",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.2-canary-20260313112147",
|
|
4
4
|
"description": "Codecs for strings of different sizes and encodings",
|
|
5
5
|
"homepage": "https://www.solanakit.com/api#solanacodecs-strings",
|
|
6
6
|
"exports": {
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"types": "./dist/types/index.d.ts",
|
|
34
34
|
"type": "commonjs",
|
|
35
35
|
"files": [
|
|
36
|
-
"./dist/"
|
|
36
|
+
"./dist/",
|
|
37
|
+
"./src/"
|
|
37
38
|
],
|
|
38
39
|
"sideEffects": false,
|
|
39
40
|
"keywords": [
|
|
@@ -55,9 +56,9 @@
|
|
|
55
56
|
"maintained node versions"
|
|
56
57
|
],
|
|
57
58
|
"dependencies": {
|
|
58
|
-
"@solana/codecs-core": "6.3.
|
|
59
|
-
"@solana/
|
|
60
|
-
"@solana/
|
|
59
|
+
"@solana/codecs-core": "6.3.2-canary-20260313112147",
|
|
60
|
+
"@solana/errors": "6.3.2-canary-20260313112147",
|
|
61
|
+
"@solana/codecs-numbers": "6.3.2-canary-20260313112147"
|
|
61
62
|
},
|
|
62
63
|
"peerDependencies": {
|
|
63
64
|
"fastestsmallesttextencoderdecoder": "^1.0.22",
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that a given string contains only characters from the specified alphabet.
|
|
5
|
+
*
|
|
6
|
+
* This function validates whether a string consists exclusively of characters
|
|
7
|
+
* from the provided `alphabet`. If the validation fails, it throws an error
|
|
8
|
+
* indicating the invalid base string.
|
|
9
|
+
*
|
|
10
|
+
* @param alphabet - The allowed set of characters for the base encoding.
|
|
11
|
+
* @param testValue - The string to validate against the given alphabet.
|
|
12
|
+
* @param givenValue - The original string provided by the user (defaults to `testValue`).
|
|
13
|
+
*
|
|
14
|
+
* @throws {SolanaError} If `testValue` contains characters not present in `alphabet`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* Validating a base-8 encoded string.
|
|
18
|
+
* ```ts
|
|
19
|
+
* assertValidBaseString('01234567', '123047'); // Passes
|
|
20
|
+
* assertValidBaseString('01234567', '128'); // Throws error
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function assertValidBaseString(alphabet: string, testValue: string, givenValue = testValue) {
|
|
24
|
+
if (!testValue.match(new RegExp(`^[${alphabet}]*$`))) {
|
|
25
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
26
|
+
alphabet,
|
|
27
|
+
base: alphabet.length,
|
|
28
|
+
value: givenValue,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/base10.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';
|
|
2
|
+
|
|
3
|
+
const alphabet = '0123456789';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns an encoder for base-10 strings.
|
|
7
|
+
*
|
|
8
|
+
* This encoder serializes strings using a base-10 encoding scheme.
|
|
9
|
+
* The output consists of bytes representing the numerical values of the input string.
|
|
10
|
+
*
|
|
11
|
+
* For more details, see {@link getBase10Codec}.
|
|
12
|
+
*
|
|
13
|
+
* @returns A `VariableSizeEncoder<string>` for encoding base-10 strings.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* Encoding a base-10 string.
|
|
17
|
+
* ```ts
|
|
18
|
+
* const encoder = getBase10Encoder();
|
|
19
|
+
* const bytes = encoder.encode('1024'); // 0x0400
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @see {@link getBase10Codec}
|
|
23
|
+
*/
|
|
24
|
+
export const getBase10Encoder = () => getBaseXEncoder(alphabet);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns a decoder for base-10 strings.
|
|
28
|
+
*
|
|
29
|
+
* This decoder deserializes base-10 encoded strings from a byte array.
|
|
30
|
+
*
|
|
31
|
+
* For more details, see {@link getBase10Codec}.
|
|
32
|
+
*
|
|
33
|
+
* @returns A `VariableSizeDecoder<string>` for decoding base-10 strings.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* Decoding a base-10 string.
|
|
37
|
+
* ```ts
|
|
38
|
+
* const decoder = getBase10Decoder();
|
|
39
|
+
* const value = decoder.decode(new Uint8Array([0x04, 0x00])); // "1024"
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @see {@link getBase10Codec}
|
|
43
|
+
*/
|
|
44
|
+
export const getBase10Decoder = () => getBaseXDecoder(alphabet);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns a codec for encoding and decoding base-10 strings.
|
|
48
|
+
*
|
|
49
|
+
* This codec serializes strings using a base-10 encoding scheme.
|
|
50
|
+
* The output consists of bytes representing the numerical values of the input string.
|
|
51
|
+
*
|
|
52
|
+
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-10 strings.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* Encoding and decoding a base-10 string.
|
|
56
|
+
* ```ts
|
|
57
|
+
* const codec = getBase10Codec();
|
|
58
|
+
* const bytes = codec.encode('1024'); // 0x0400
|
|
59
|
+
* const value = codec.decode(bytes); // "1024"
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
|
|
64
|
+
*
|
|
65
|
+
* If you need a fixed-size base-10 codec, consider using {@link fixCodecSize}.
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* const codec = fixCodecSize(getBase10Codec(), 5);
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* If you need a size-prefixed base-10 codec, consider using {@link addCodecSizePrefix}.
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* const codec = addCodecSizePrefix(getBase10Codec(), getU32Codec());
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Separate {@link getBase10Encoder} and {@link getBase10Decoder} functions are available.
|
|
78
|
+
*
|
|
79
|
+
* ```ts
|
|
80
|
+
* const bytes = getBase10Encoder().encode('1024');
|
|
81
|
+
* const value = getBase10Decoder().decode(bytes);
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @see {@link getBase10Encoder}
|
|
85
|
+
* @see {@link getBase10Decoder}
|
|
86
|
+
*/
|
|
87
|
+
export const getBase10Codec = () => getBaseXCodec(alphabet);
|
package/src/base16.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import {
|
|
2
|
+
combineCodec,
|
|
3
|
+
createDecoder,
|
|
4
|
+
createEncoder,
|
|
5
|
+
VariableSizeCodec,
|
|
6
|
+
VariableSizeDecoder,
|
|
7
|
+
VariableSizeEncoder,
|
|
8
|
+
} from '@solana/codecs-core';
|
|
9
|
+
import { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';
|
|
10
|
+
|
|
11
|
+
const enum HexC {
|
|
12
|
+
ZERO = 48, // 0
|
|
13
|
+
NINE = 57, // 9
|
|
14
|
+
A_UP = 65, // A
|
|
15
|
+
F_UP = 70, // F
|
|
16
|
+
A_LO = 97, // a
|
|
17
|
+
F_LO = 102, // f
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const INVALID_STRING_ERROR_BASE_CONFIG = {
|
|
21
|
+
alphabet: '0123456789abcdef',
|
|
22
|
+
base: 16,
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
function charCodeToBase16(char: number) {
|
|
26
|
+
if (char >= HexC.ZERO && char <= HexC.NINE) return char - HexC.ZERO;
|
|
27
|
+
if (char >= HexC.A_UP && char <= HexC.F_UP) return char - (HexC.A_UP - 10);
|
|
28
|
+
if (char >= HexC.A_LO && char <= HexC.F_LO) return char - (HexC.A_LO - 10);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns an encoder for base-16 (hexadecimal) strings.
|
|
33
|
+
*
|
|
34
|
+
* This encoder serializes strings using a base-16 encoding scheme.
|
|
35
|
+
* The output consists of bytes representing the hexadecimal values of the input string.
|
|
36
|
+
*
|
|
37
|
+
* For more details, see {@link getBase16Codec}.
|
|
38
|
+
*
|
|
39
|
+
* @returns A `VariableSizeEncoder<string>` for encoding base-16 strings.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* Encoding a base-16 string.
|
|
43
|
+
* ```ts
|
|
44
|
+
* const encoder = getBase16Encoder();
|
|
45
|
+
* const bytes = encoder.encode('deadface'); // 0xdeadface
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @see {@link getBase16Codec}
|
|
49
|
+
*/
|
|
50
|
+
export const getBase16Encoder = (): VariableSizeEncoder<string> =>
|
|
51
|
+
createEncoder({
|
|
52
|
+
getSizeFromValue: (value: string) => Math.ceil(value.length / 2),
|
|
53
|
+
write(value: string, bytes, offset) {
|
|
54
|
+
const len = value.length;
|
|
55
|
+
const al = len / 2;
|
|
56
|
+
if (len === 1) {
|
|
57
|
+
const c = value.charCodeAt(0);
|
|
58
|
+
const n = charCodeToBase16(c);
|
|
59
|
+
if (n === undefined) {
|
|
60
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
61
|
+
...INVALID_STRING_ERROR_BASE_CONFIG,
|
|
62
|
+
value,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
bytes.set([n], offset);
|
|
66
|
+
return 1 + offset;
|
|
67
|
+
}
|
|
68
|
+
const hexBytes = new Uint8Array(al);
|
|
69
|
+
for (let i = 0, j = 0; i < al; i++) {
|
|
70
|
+
const c1 = value.charCodeAt(j++);
|
|
71
|
+
const c2 = value.charCodeAt(j++);
|
|
72
|
+
|
|
73
|
+
const n1 = charCodeToBase16(c1);
|
|
74
|
+
const n2 = charCodeToBase16(c2);
|
|
75
|
+
if (n1 === undefined || (n2 === undefined && !Number.isNaN(c2))) {
|
|
76
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
77
|
+
...INVALID_STRING_ERROR_BASE_CONFIG,
|
|
78
|
+
value,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
hexBytes[i] = !Number.isNaN(c2) ? (n1 << 4) | (n2 ?? 0) : n1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
bytes.set(hexBytes, offset);
|
|
85
|
+
return hexBytes.length + offset;
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns a decoder for base-16 (hexadecimal) strings.
|
|
91
|
+
*
|
|
92
|
+
* This decoder deserializes base-16 encoded strings from a byte array.
|
|
93
|
+
*
|
|
94
|
+
* For more details, see {@link getBase16Codec}.
|
|
95
|
+
*
|
|
96
|
+
* @returns A `VariableSizeDecoder<string>` for decoding base-16 strings.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* Decoding a base-16 string.
|
|
100
|
+
* ```ts
|
|
101
|
+
* const decoder = getBase16Decoder();
|
|
102
|
+
* const value = decoder.decode(new Uint8Array([0xde, 0xad, 0xfa, 0xce])); // "deadface"
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @see {@link getBase16Codec}
|
|
106
|
+
*/
|
|
107
|
+
export const getBase16Decoder = (): VariableSizeDecoder<string> =>
|
|
108
|
+
createDecoder({
|
|
109
|
+
read(bytes, offset) {
|
|
110
|
+
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
|
|
111
|
+
return [value, bytes.length];
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns a codec for encoding and decoding base-16 (hexadecimal) strings.
|
|
117
|
+
*
|
|
118
|
+
* This codec serializes strings using a base-16 encoding scheme.
|
|
119
|
+
* The output consists of bytes representing the hexadecimal values of the input string.
|
|
120
|
+
*
|
|
121
|
+
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-16 strings.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* Encoding and decoding a base-16 string.
|
|
125
|
+
* ```ts
|
|
126
|
+
* const codec = getBase16Codec();
|
|
127
|
+
* const bytes = codec.encode('deadface'); // 0xdeadface
|
|
128
|
+
* const value = codec.decode(bytes); // "deadface"
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @remarks
|
|
132
|
+
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
|
|
133
|
+
*
|
|
134
|
+
* If you need a fixed-size base-16 codec, consider using {@link fixCodecSize}.
|
|
135
|
+
*
|
|
136
|
+
* ```ts
|
|
137
|
+
* const codec = fixCodecSize(getBase16Codec(), 8);
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* If you need a size-prefixed base-16 codec, consider using {@link addCodecSizePrefix}.
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
* const codec = addCodecSizePrefix(getBase16Codec(), getU32Codec());
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* Separate {@link getBase16Encoder} and {@link getBase16Decoder} functions are available.
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* const bytes = getBase16Encoder().encode('deadface');
|
|
150
|
+
* const value = getBase16Decoder().decode(bytes);
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @see {@link getBase16Encoder}
|
|
154
|
+
* @see {@link getBase16Decoder}
|
|
155
|
+
*/
|
|
156
|
+
export const getBase16Codec = (): VariableSizeCodec<string> => combineCodec(getBase16Encoder(), getBase16Decoder());
|
package/src/base58.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';
|
|
2
|
+
|
|
3
|
+
const alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns an encoder for base-58 strings.
|
|
7
|
+
*
|
|
8
|
+
* This encoder serializes strings using a base-58 encoding scheme,
|
|
9
|
+
* commonly used in cryptocurrency addresses and other compact representations.
|
|
10
|
+
*
|
|
11
|
+
* For more details, see {@link getBase58Codec}.
|
|
12
|
+
*
|
|
13
|
+
* @returns A `VariableSizeEncoder<string>` for encoding base-58 strings.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* Encoding a base-58 string.
|
|
17
|
+
* ```ts
|
|
18
|
+
* const encoder = getBase58Encoder();
|
|
19
|
+
* const bytes = encoder.encode('heLLo'); // 0x1b6a3070
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @see {@link getBase58Codec}
|
|
23
|
+
*/
|
|
24
|
+
export const getBase58Encoder = () => getBaseXEncoder(alphabet);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns a decoder for base-58 strings.
|
|
28
|
+
*
|
|
29
|
+
* This decoder deserializes base-58 encoded strings from a byte array.
|
|
30
|
+
*
|
|
31
|
+
* For more details, see {@link getBase58Codec}.
|
|
32
|
+
*
|
|
33
|
+
* @returns A `VariableSizeDecoder<string>` for decoding base-58 strings.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* Decoding a base-58 string.
|
|
37
|
+
* ```ts
|
|
38
|
+
* const decoder = getBase58Decoder();
|
|
39
|
+
* const value = decoder.decode(new Uint8Array([0x1b, 0x6a, 0x30, 0x70])); // "heLLo"
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @see {@link getBase58Codec}
|
|
43
|
+
*/
|
|
44
|
+
export const getBase58Decoder = () => getBaseXDecoder(alphabet);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns a codec for encoding and decoding base-58 strings.
|
|
48
|
+
*
|
|
49
|
+
* This codec serializes strings using a base-58 encoding scheme,
|
|
50
|
+
* commonly used in cryptocurrency addresses and other compact representations.
|
|
51
|
+
*
|
|
52
|
+
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-58 strings.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* Encoding and decoding a base-58 string.
|
|
56
|
+
* ```ts
|
|
57
|
+
* const codec = getBase58Codec();
|
|
58
|
+
* const bytes = codec.encode('heLLo'); // 0x1b6a3070
|
|
59
|
+
* const value = codec.decode(bytes); // "heLLo"
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
|
|
64
|
+
*
|
|
65
|
+
* If you need a fixed-size base-58 codec, consider using {@link fixCodecSize}.
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* const codec = fixCodecSize(getBase58Codec(), 8);
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* If you need a size-prefixed base-58 codec, consider using {@link addCodecSizePrefix}.
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* const codec = addCodecSizePrefix(getBase58Codec(), getU32Codec());
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Separate {@link getBase58Encoder} and {@link getBase58Decoder} functions are available.
|
|
78
|
+
*
|
|
79
|
+
* ```ts
|
|
80
|
+
* const bytes = getBase58Encoder().encode('heLLo');
|
|
81
|
+
* const value = getBase58Decoder().decode(bytes);
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @see {@link getBase58Encoder}
|
|
85
|
+
* @see {@link getBase58Decoder}
|
|
86
|
+
*/
|
|
87
|
+
export const getBase58Codec = () => getBaseXCodec(alphabet);
|
package/src/base64.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {
|
|
2
|
+
combineCodec,
|
|
3
|
+
createDecoder,
|
|
4
|
+
createEncoder,
|
|
5
|
+
toArrayBuffer,
|
|
6
|
+
transformDecoder,
|
|
7
|
+
transformEncoder,
|
|
8
|
+
VariableSizeCodec,
|
|
9
|
+
VariableSizeDecoder,
|
|
10
|
+
VariableSizeEncoder,
|
|
11
|
+
} from '@solana/codecs-core';
|
|
12
|
+
import { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';
|
|
13
|
+
|
|
14
|
+
import { assertValidBaseString } from './assertions';
|
|
15
|
+
import { getBaseXResliceDecoder, getBaseXResliceEncoder } from './baseX-reslice';
|
|
16
|
+
|
|
17
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns an encoder for base-64 strings.
|
|
21
|
+
*
|
|
22
|
+
* This encoder serializes strings using a base-64 encoding scheme,
|
|
23
|
+
* commonly used for data encoding in URLs, cryptographic keys, and binary-to-text encoding.
|
|
24
|
+
*
|
|
25
|
+
* For more details, see {@link getBase64Codec}.
|
|
26
|
+
*
|
|
27
|
+
* @returns A `VariableSizeEncoder<string>` for encoding base-64 strings.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* Encoding a base-64 string.
|
|
31
|
+
* ```ts
|
|
32
|
+
* const encoder = getBase64Encoder();
|
|
33
|
+
* const bytes = encoder.encode('hello+world'); // 0x85e965a3ec28ae57
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @see {@link getBase64Codec}
|
|
37
|
+
*/
|
|
38
|
+
export const getBase64Encoder = (): VariableSizeEncoder<string> => {
|
|
39
|
+
if (__BROWSER__) {
|
|
40
|
+
return createEncoder({
|
|
41
|
+
getSizeFromValue: (value: string) => {
|
|
42
|
+
try {
|
|
43
|
+
return (atob as Window['atob'])(value).length;
|
|
44
|
+
} catch {
|
|
45
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
46
|
+
alphabet,
|
|
47
|
+
base: 64,
|
|
48
|
+
value,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
write(value: string, bytes, offset) {
|
|
53
|
+
try {
|
|
54
|
+
const bytesToAdd = (atob as Window['atob'])(value)
|
|
55
|
+
.split('')
|
|
56
|
+
.map(c => c.charCodeAt(0));
|
|
57
|
+
bytes.set(bytesToAdd, offset);
|
|
58
|
+
return bytesToAdd.length + offset;
|
|
59
|
+
} catch {
|
|
60
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
61
|
+
alphabet,
|
|
62
|
+
base: 64,
|
|
63
|
+
value,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (__NODEJS__) {
|
|
71
|
+
return createEncoder({
|
|
72
|
+
getSizeFromValue: (value: string) => Buffer.from(value, 'base64').length,
|
|
73
|
+
write(value: string, bytes, offset) {
|
|
74
|
+
assertValidBaseString(alphabet, value.replace(/=/g, ''));
|
|
75
|
+
const buffer = Buffer.from(value, 'base64');
|
|
76
|
+
bytes.set(buffer, offset);
|
|
77
|
+
return buffer.length + offset;
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return transformEncoder(getBaseXResliceEncoder(alphabet, 6), (value: string): string => value.replace(/=/g, ''));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns a decoder for base-64 strings.
|
|
87
|
+
*
|
|
88
|
+
* This decoder deserializes base-64 encoded strings from a byte array.
|
|
89
|
+
*
|
|
90
|
+
* For more details, see {@link getBase64Codec}.
|
|
91
|
+
*
|
|
92
|
+
* @returns A `VariableSizeDecoder<string>` for decoding base-64 strings.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* Decoding a base-64 string.
|
|
96
|
+
* ```ts
|
|
97
|
+
* const decoder = getBase64Decoder();
|
|
98
|
+
* const value = decoder.decode(new Uint8Array([0x85, 0xe9, 0x65, 0xa3, 0xec, 0x28, 0xae, 0x57])); // "hello+world"
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @see {@link getBase64Codec}
|
|
102
|
+
*/
|
|
103
|
+
export const getBase64Decoder = (): VariableSizeDecoder<string> => {
|
|
104
|
+
if (__BROWSER__) {
|
|
105
|
+
return createDecoder({
|
|
106
|
+
read(bytes, offset = 0) {
|
|
107
|
+
const slice = bytes.slice(offset);
|
|
108
|
+
const value = (btoa as Window['btoa'])(String.fromCharCode(...slice));
|
|
109
|
+
return [value, bytes.length];
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (__NODEJS__) {
|
|
115
|
+
return createDecoder({
|
|
116
|
+
read: (bytes, offset = 0) => [Buffer.from(toArrayBuffer(bytes), offset).toString('base64'), bytes.length],
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return transformDecoder(getBaseXResliceDecoder(alphabet, 6), (value: string): string =>
|
|
121
|
+
value.padEnd(Math.ceil(value.length / 4) * 4, '='),
|
|
122
|
+
);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns a codec for encoding and decoding base-64 strings.
|
|
127
|
+
*
|
|
128
|
+
* This codec serializes strings using a base-64 encoding scheme,
|
|
129
|
+
* commonly used for data encoding in URLs, cryptographic keys, and binary-to-text encoding.
|
|
130
|
+
*
|
|
131
|
+
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-64 strings.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* Encoding and decoding a base-64 string.
|
|
135
|
+
* ```ts
|
|
136
|
+
* const codec = getBase64Codec();
|
|
137
|
+
* const bytes = codec.encode('hello+world'); // 0x85e965a3ec28ae57
|
|
138
|
+
* const value = codec.decode(bytes); // "hello+world"
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* @remarks
|
|
142
|
+
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
|
|
143
|
+
*
|
|
144
|
+
* If you need a fixed-size base-64 codec, consider using {@link fixCodecSize}.
|
|
145
|
+
*
|
|
146
|
+
* ```ts
|
|
147
|
+
* const codec = fixCodecSize(getBase64Codec(), 8);
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* If you need a size-prefixed base-64 codec, consider using {@link addCodecSizePrefix}.
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* const codec = addCodecSizePrefix(getBase64Codec(), getU32Codec());
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* Separate {@link getBase64Encoder} and {@link getBase64Decoder} functions are available.
|
|
157
|
+
*
|
|
158
|
+
* ```ts
|
|
159
|
+
* const bytes = getBase64Encoder().encode('hello+world');
|
|
160
|
+
* const value = getBase64Decoder().decode(bytes);
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @see {@link getBase64Encoder}
|
|
164
|
+
* @see {@link getBase64Decoder}
|
|
165
|
+
*/
|
|
166
|
+
export const getBase64Codec = (): VariableSizeCodec<string> => combineCodec(getBase64Encoder(), getBase64Decoder());
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import {
|
|
2
|
+
combineCodec,
|
|
3
|
+
createDecoder,
|
|
4
|
+
createEncoder,
|
|
5
|
+
VariableSizeCodec,
|
|
6
|
+
VariableSizeDecoder,
|
|
7
|
+
VariableSizeEncoder,
|
|
8
|
+
} from '@solana/codecs-core';
|
|
9
|
+
|
|
10
|
+
import { assertValidBaseString } from './assertions';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns an encoder for base-X encoded strings using bit re-slicing.
|
|
14
|
+
*
|
|
15
|
+
* This encoder serializes strings by dividing the input into custom-sized bit chunks,
|
|
16
|
+
* mapping them to an alphabet, and encoding the result into a byte array.
|
|
17
|
+
* This approach is commonly used for encoding schemes where the alphabet's length is a power of 2,
|
|
18
|
+
* such as base-16 or base-64.
|
|
19
|
+
*
|
|
20
|
+
* For more details, see {@link getBaseXResliceCodec}.
|
|
21
|
+
*
|
|
22
|
+
* @param alphabet - The set of characters defining the base-X encoding.
|
|
23
|
+
* @param bits - The number of bits per encoded chunk, typically `log2(alphabet.length)`.
|
|
24
|
+
* @returns A `VariableSizeEncoder<string>` for encoding base-X strings using bit re-slicing.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* Encoding a base-X string using bit re-slicing.
|
|
28
|
+
* ```ts
|
|
29
|
+
* const encoder = getBaseXResliceEncoder('elho', 2);
|
|
30
|
+
* const bytes = encoder.encode('hellolol'); // 0x4aee
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @see {@link getBaseXResliceCodec}
|
|
34
|
+
*/
|
|
35
|
+
export const getBaseXResliceEncoder = (alphabet: string, bits: number): VariableSizeEncoder<string> =>
|
|
36
|
+
createEncoder({
|
|
37
|
+
getSizeFromValue: (value: string) => Math.floor((value.length * bits) / 8),
|
|
38
|
+
write(value: string, bytes, offset) {
|
|
39
|
+
assertValidBaseString(alphabet, value);
|
|
40
|
+
if (value === '') return offset;
|
|
41
|
+
const charIndices = [...value].map(c => alphabet.indexOf(c));
|
|
42
|
+
const reslicedBytes = reslice(charIndices, bits, 8, false);
|
|
43
|
+
bytes.set(reslicedBytes, offset);
|
|
44
|
+
return reslicedBytes.length + offset;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns a decoder for base-X encoded strings using bit re-slicing.
|
|
50
|
+
*
|
|
51
|
+
* This decoder deserializes base-X encoded strings by re-slicing the bits of a byte array into
|
|
52
|
+
* custom-sized chunks and mapping them to a specified alphabet.
|
|
53
|
+
* This is typically used for encoding schemes where the alphabet's length is a power of 2,
|
|
54
|
+
* such as base-16 or base-64.
|
|
55
|
+
*
|
|
56
|
+
* For more details, see {@link getBaseXResliceCodec}.
|
|
57
|
+
*
|
|
58
|
+
* @param alphabet - The set of characters defining the base-X encoding.
|
|
59
|
+
* @param bits - The number of bits per encoded chunk, typically `log2(alphabet.length)`.
|
|
60
|
+
* @returns A `VariableSizeDecoder<string>` for decoding base-X strings using bit re-slicing.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* Decoding a base-X string using bit re-slicing.
|
|
64
|
+
* ```ts
|
|
65
|
+
* const decoder = getBaseXResliceDecoder('elho', 2);
|
|
66
|
+
* const value = decoder.decode(new Uint8Array([0x4a, 0xee])); // "hellolol"
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @see {@link getBaseXResliceCodec}
|
|
70
|
+
*/
|
|
71
|
+
export const getBaseXResliceDecoder = (alphabet: string, bits: number): VariableSizeDecoder<string> =>
|
|
72
|
+
createDecoder({
|
|
73
|
+
read(rawBytes, offset = 0): [string, number] {
|
|
74
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
75
|
+
if (bytes.length === 0) return ['', rawBytes.length];
|
|
76
|
+
const charIndices = reslice([...bytes], 8, bits, true);
|
|
77
|
+
return [charIndices.map(i => alphabet[i]).join(''), rawBytes.length];
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns a codec for encoding and decoding base-X strings using bit re-slicing.
|
|
83
|
+
*
|
|
84
|
+
* This codec serializes strings by dividing the input into custom-sized bit chunks,
|
|
85
|
+
* mapping them to a given alphabet, and encoding the result into bytes.
|
|
86
|
+
* It is particularly suited for encoding schemes where the alphabet's length is a power of 2,
|
|
87
|
+
* such as base-16 or base-64.
|
|
88
|
+
*
|
|
89
|
+
* @param alphabet - The set of characters defining the base-X encoding.
|
|
90
|
+
* @param bits - The number of bits per encoded chunk, typically `log2(alphabet.length)`.
|
|
91
|
+
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-X strings using bit re-slicing.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* Encoding and decoding a base-X string using bit re-slicing.
|
|
95
|
+
* ```ts
|
|
96
|
+
* const codec = getBaseXResliceCodec('elho', 2);
|
|
97
|
+
* const bytes = codec.encode('hellolol'); // 0x4aee
|
|
98
|
+
* const value = codec.decode(bytes); // "hellolol"
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
|
|
103
|
+
*
|
|
104
|
+
* If you need a fixed-size base-X codec, consider using {@link fixCodecSize}.
|
|
105
|
+
*
|
|
106
|
+
* ```ts
|
|
107
|
+
* const codec = fixCodecSize(getBaseXResliceCodec('elho', 2), 8);
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* If you need a size-prefixed base-X codec, consider using {@link addCodecSizePrefix}.
|
|
111
|
+
*
|
|
112
|
+
* ```ts
|
|
113
|
+
* const codec = addCodecSizePrefix(getBaseXResliceCodec('elho', 2), getU32Codec());
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* Separate {@link getBaseXResliceEncoder} and {@link getBaseXResliceDecoder} functions are available.
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
* const bytes = getBaseXResliceEncoder('elho', 2).encode('hellolol');
|
|
120
|
+
* const value = getBaseXResliceDecoder('elho', 2).decode(bytes);
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @see {@link getBaseXResliceEncoder}
|
|
124
|
+
* @see {@link getBaseXResliceDecoder}
|
|
125
|
+
*/
|
|
126
|
+
export const getBaseXResliceCodec = (alphabet: string, bits: number): VariableSizeCodec<string> =>
|
|
127
|
+
combineCodec(getBaseXResliceEncoder(alphabet, bits), getBaseXResliceDecoder(alphabet, bits));
|
|
128
|
+
|
|
129
|
+
/** Helper function to reslice the bits inside bytes. */
|
|
130
|
+
function reslice(input: number[], inputBits: number, outputBits: number, useRemainder: boolean): number[] {
|
|
131
|
+
const output = [];
|
|
132
|
+
let accumulator = 0;
|
|
133
|
+
let bitsInAccumulator = 0;
|
|
134
|
+
const mask = (1 << outputBits) - 1;
|
|
135
|
+
for (const value of input) {
|
|
136
|
+
accumulator = (accumulator << inputBits) | value;
|
|
137
|
+
bitsInAccumulator += inputBits;
|
|
138
|
+
while (bitsInAccumulator >= outputBits) {
|
|
139
|
+
bitsInAccumulator -= outputBits;
|
|
140
|
+
output.push((accumulator >> bitsInAccumulator) & mask);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (useRemainder && bitsInAccumulator > 0) {
|
|
144
|
+
output.push((accumulator << (outputBits - bitsInAccumulator)) & mask);
|
|
145
|
+
}
|
|
146
|
+
return output;
|
|
147
|
+
}
|
package/src/baseX.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {
|
|
2
|
+
combineCodec,
|
|
3
|
+
createDecoder,
|
|
4
|
+
createEncoder,
|
|
5
|
+
VariableSizeCodec,
|
|
6
|
+
VariableSizeDecoder,
|
|
7
|
+
VariableSizeEncoder,
|
|
8
|
+
} from '@solana/codecs-core';
|
|
9
|
+
|
|
10
|
+
import { assertValidBaseString } from './assertions';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns an encoder for base-X encoded strings.
|
|
14
|
+
*
|
|
15
|
+
* This encoder serializes strings using a custom alphabet, treating the length of the alphabet as the base.
|
|
16
|
+
* The encoding process involves converting the input string to a numeric value in base-X, then
|
|
17
|
+
* encoding that value into bytes while preserving leading zeroes.
|
|
18
|
+
*
|
|
19
|
+
* For more details, see {@link getBaseXCodec}.
|
|
20
|
+
*
|
|
21
|
+
* @param alphabet - The set of characters defining the base-X encoding.
|
|
22
|
+
* @returns A `VariableSizeEncoder<string>` for encoding base-X strings.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* Encoding a base-X string using a custom alphabet.
|
|
26
|
+
* ```ts
|
|
27
|
+
* const encoder = getBaseXEncoder('0123456789abcdef');
|
|
28
|
+
* const bytes = encoder.encode('deadface'); // 0xdeadface
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see {@link getBaseXCodec}
|
|
32
|
+
*/
|
|
33
|
+
export const getBaseXEncoder = (alphabet: string): VariableSizeEncoder<string> => {
|
|
34
|
+
return createEncoder({
|
|
35
|
+
getSizeFromValue: (value: string): number => {
|
|
36
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);
|
|
37
|
+
if (!tailChars) return value.length;
|
|
38
|
+
|
|
39
|
+
const base10Number = getBigIntFromBaseX(tailChars, alphabet);
|
|
40
|
+
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
|
|
41
|
+
},
|
|
42
|
+
write(value: string, bytes, offset) {
|
|
43
|
+
// Check if the value is valid.
|
|
44
|
+
assertValidBaseString(alphabet, value);
|
|
45
|
+
if (value === '') return offset;
|
|
46
|
+
|
|
47
|
+
// Handle leading zeroes.
|
|
48
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);
|
|
49
|
+
if (!tailChars) {
|
|
50
|
+
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
|
|
51
|
+
return offset + leadingZeroes.length;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// From baseX to base10.
|
|
55
|
+
let base10Number = getBigIntFromBaseX(tailChars, alphabet);
|
|
56
|
+
|
|
57
|
+
// From base10 to bytes.
|
|
58
|
+
const tailBytes: number[] = [];
|
|
59
|
+
while (base10Number > 0n) {
|
|
60
|
+
tailBytes.unshift(Number(base10Number % 256n));
|
|
61
|
+
base10Number /= 256n;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
|
|
65
|
+
bytes.set(bytesToAdd, offset);
|
|
66
|
+
return offset + bytesToAdd.length;
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns a decoder for base-X encoded strings.
|
|
73
|
+
*
|
|
74
|
+
* This decoder deserializes base-X encoded strings from a byte array using a custom alphabet.
|
|
75
|
+
* The decoding process converts the byte array into a numeric value in base-10, then
|
|
76
|
+
* maps that value back to characters in the specified base-X alphabet.
|
|
77
|
+
*
|
|
78
|
+
* For more details, see {@link getBaseXCodec}.
|
|
79
|
+
*
|
|
80
|
+
* @param alphabet - The set of characters defining the base-X encoding.
|
|
81
|
+
* @returns A `VariableSizeDecoder<string>` for decoding base-X strings.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* Decoding a base-X string using a custom alphabet.
|
|
85
|
+
* ```ts
|
|
86
|
+
* const decoder = getBaseXDecoder('0123456789abcdef');
|
|
87
|
+
* const value = decoder.decode(new Uint8Array([0xde, 0xad, 0xfa, 0xce])); // "deadface"
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @see {@link getBaseXCodec}
|
|
91
|
+
*/
|
|
92
|
+
export const getBaseXDecoder = (alphabet: string): VariableSizeDecoder<string> => {
|
|
93
|
+
return createDecoder({
|
|
94
|
+
read(rawBytes, offset): [string, number] {
|
|
95
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
96
|
+
if (bytes.length === 0) return ['', 0];
|
|
97
|
+
|
|
98
|
+
// Handle leading zeroes.
|
|
99
|
+
let trailIndex = bytes.findIndex(n => n !== 0);
|
|
100
|
+
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
|
|
101
|
+
const leadingZeroes = alphabet[0].repeat(trailIndex);
|
|
102
|
+
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
|
|
103
|
+
|
|
104
|
+
// From bytes to base10.
|
|
105
|
+
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
|
|
106
|
+
|
|
107
|
+
// From base10 to baseX.
|
|
108
|
+
const tailChars = getBaseXFromBigInt(base10Number, alphabet);
|
|
109
|
+
|
|
110
|
+
return [leadingZeroes + tailChars, rawBytes.length];
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns a codec for encoding and decoding base-X strings.
|
|
117
|
+
*
|
|
118
|
+
* This codec serializes strings using a custom alphabet, treating the length of the alphabet as the base.
|
|
119
|
+
* The encoding process converts the input string into a numeric value in base-X, which is then encoded as bytes.
|
|
120
|
+
* The decoding process reverses this transformation to reconstruct the original string.
|
|
121
|
+
*
|
|
122
|
+
* This codec supports leading zeroes by treating the first character of the alphabet as the zero character.
|
|
123
|
+
*
|
|
124
|
+
* @param alphabet - The set of characters defining the base-X encoding.
|
|
125
|
+
* @returns A `VariableSizeCodec<string>` for encoding and decoding base-X strings.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* Encoding and decoding a base-X string using a custom alphabet.
|
|
129
|
+
* ```ts
|
|
130
|
+
* const codec = getBaseXCodec('0123456789abcdef');
|
|
131
|
+
* const bytes = codec.encode('deadface'); // 0xdeadface
|
|
132
|
+
* const value = codec.decode(bytes); // "deadface"
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @remarks
|
|
136
|
+
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
|
|
137
|
+
*
|
|
138
|
+
* If you need a fixed-size base-X codec, consider using {@link fixCodecSize}.
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* const codec = fixCodecSize(getBaseXCodec('0123456789abcdef'), 8);
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* If you need a size-prefixed base-X codec, consider using {@link addCodecSizePrefix}.
|
|
145
|
+
*
|
|
146
|
+
* ```ts
|
|
147
|
+
* const codec = addCodecSizePrefix(getBaseXCodec('0123456789abcdef'), getU32Codec());
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* Separate {@link getBaseXEncoder} and {@link getBaseXDecoder} functions are available.
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* const bytes = getBaseXEncoder('0123456789abcdef').encode('deadface');
|
|
154
|
+
* const value = getBaseXDecoder('0123456789abcdef').decode(bytes);
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @see {@link getBaseXEncoder}
|
|
158
|
+
* @see {@link getBaseXDecoder}
|
|
159
|
+
*/
|
|
160
|
+
export const getBaseXCodec = (alphabet: string): VariableSizeCodec<string> =>
|
|
161
|
+
combineCodec(getBaseXEncoder(alphabet), getBaseXDecoder(alphabet));
|
|
162
|
+
|
|
163
|
+
function partitionLeadingZeroes(
|
|
164
|
+
value: string,
|
|
165
|
+
zeroCharacter: string,
|
|
166
|
+
): [leadingZeros: string, tailChars: string | undefined] {
|
|
167
|
+
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
|
|
168
|
+
return [leadingZeros, tailChars];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function getBigIntFromBaseX(value: string, alphabet: string): bigint {
|
|
172
|
+
const base = BigInt(alphabet.length);
|
|
173
|
+
let sum = 0n;
|
|
174
|
+
for (const char of value) {
|
|
175
|
+
sum *= base;
|
|
176
|
+
sum += BigInt(alphabet.indexOf(char));
|
|
177
|
+
}
|
|
178
|
+
return sum;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function getBaseXFromBigInt(value: bigint, alphabet: string): string {
|
|
182
|
+
const base = BigInt(alphabet.length);
|
|
183
|
+
const tailChars = [];
|
|
184
|
+
while (value > 0n) {
|
|
185
|
+
tailChars.unshift(alphabet[Number(value % base)]);
|
|
186
|
+
value /= base;
|
|
187
|
+
}
|
|
188
|
+
return tailChars.join('');
|
|
189
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This package contains codecs for strings of different sizes and encodings.
|
|
3
|
+
* It can be used standalone, but it is also exported as part of Kit
|
|
4
|
+
* [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit).
|
|
5
|
+
*
|
|
6
|
+
* This package is also part of the [`@solana/codecs` package](https://github.com/anza-xyz/kit/tree/main/packages/codecs)
|
|
7
|
+
* which acts as an entry point for all codec packages as well as for their documentation.
|
|
8
|
+
*
|
|
9
|
+
* ## Sizing string codecs
|
|
10
|
+
*
|
|
11
|
+
* The `@solana/codecs-strings` package offers a variety of string codecs such as `utf8`, `base58`, `base64`, etc —
|
|
12
|
+
* which we will discuss in more detail below. However, before digging into the available string codecs,
|
|
13
|
+
* it's important to understand the different sizing strategies available for string codecs.
|
|
14
|
+
*
|
|
15
|
+
* By default, all available string codecs will return a `VariableSizeCodec<string>` meaning that:
|
|
16
|
+
*
|
|
17
|
+
* - When encoding a string, all bytes necessary to encode the string will be used.
|
|
18
|
+
* - When decoding a byte array at a given offset, all bytes starting from that offset will be decoded as a string.
|
|
19
|
+
*
|
|
20
|
+
* For instance, here's how you can encode/decode `utf8` strings without any size boundary:
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* const codec = getUtf8Codec();
|
|
24
|
+
*
|
|
25
|
+
* codec.encode('hello');
|
|
26
|
+
* // 0x68656c6c6f
|
|
27
|
+
* // └-- Any bytes necessary to encode our content.
|
|
28
|
+
*
|
|
29
|
+
* codec.decode(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]));
|
|
30
|
+
* // 'hello'
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* This might be what you want — e.g. when having a string at the end of a data structure — but in many cases,
|
|
34
|
+
* you might want to have a size boundary for your string. You may achieve this by composing your string codec
|
|
35
|
+
* with the [`fixCodecSize`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-core#fixing-the-size-of-codecs)
|
|
36
|
+
* or [`addCodecSizePrefix`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-core#prefixing-the-size-of-codecs) functions.
|
|
37
|
+
*
|
|
38
|
+
* The `fixCodecSize` function accepts a fixed byte length and returns a `FixedSizeCodec<string>` that will always use
|
|
39
|
+
* that amount of bytes to encode and decode a string. Any string longer or smaller than that size will be truncated
|
|
40
|
+
* or padded respectively. Here's how you can use it with a `utf8` codec:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const codec = fixCodecSize(getUtf8Codec(), 5);
|
|
44
|
+
*
|
|
45
|
+
* codec.encode('hello');
|
|
46
|
+
* // 0x68656c6c6f
|
|
47
|
+
* // └-- The exact 5 bytes of content.
|
|
48
|
+
*
|
|
49
|
+
* codec.encode('hello world');
|
|
50
|
+
* // 0x68656c6c6f
|
|
51
|
+
* // └-- The truncated 5 bytes of content.
|
|
52
|
+
*
|
|
53
|
+
* codec.encode('hell');
|
|
54
|
+
* // 0x68656c6c00
|
|
55
|
+
* // └-- The padded 5 bytes of content.
|
|
56
|
+
*
|
|
57
|
+
* codec.decode(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xff, 0xff, 0xff, 0xff]));
|
|
58
|
+
* // 'hello'
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* The `addCodecSizePrefix` function accepts an additional number codec that will be used to encode and
|
|
62
|
+
* decode a size prefix for the string. This prefix allows us to know when to stop reading the string when
|
|
63
|
+
* decoding a given byte array. Here's how you can use it with a `utf8` codec:
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* const codec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
|
|
67
|
+
*
|
|
68
|
+
* codec.encode('hello');
|
|
69
|
+
* // 0x0500000068656c6c6f
|
|
70
|
+
* // | └-- The 5 bytes of content.
|
|
71
|
+
* // └-- 4-byte prefix telling us to read 5 bytes.
|
|
72
|
+
*
|
|
73
|
+
* codec.decode(new Uint8Array([0x05, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xff, 0xff, 0xff, 0xff]));
|
|
74
|
+
* // "hello"
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Now, let's take a look at the available string encodings. Just remember that you can use
|
|
78
|
+
* the `fixSizeCodec` or `prefixSizeCodec` functions on any of these encodings to add a size boundary to them.
|
|
79
|
+
*
|
|
80
|
+
* ## Utf8 codec
|
|
81
|
+
*
|
|
82
|
+
* The `getUtf8Codec` function encodes and decodes a UTF-8 string to and from a byte array.
|
|
83
|
+
*
|
|
84
|
+
* ```ts
|
|
85
|
+
* const bytes = getUtf8Codec().encode('hello'); // 0x68656c6c6f
|
|
86
|
+
* const value = getUtf8Codec().decode(bytes); // "hello"
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* As usual, separate `getUtf8Encoder` and `getUtf8Decoder` functions are also available.
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* const bytes = getUtf8Encoder().encode('hello'); // 0x68656c6c6f
|
|
93
|
+
* const value = getUtf8Decoder().decode(bytes); // "hello"
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* ## Base 64 codec
|
|
97
|
+
*
|
|
98
|
+
* The `getBase64Codec` function encodes and decodes a base-64 string to and from a byte array.
|
|
99
|
+
*
|
|
100
|
+
* ```ts
|
|
101
|
+
* const bytes = getBase64Codec().encode('hello+world'); // 0x85e965a3ec28ae57
|
|
102
|
+
* const value = getBase64Codec().decode(bytes); // "hello+world"
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* As usual, separate `getBase64Encoder` and `getBase64Decoder` functions are also available.
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* const bytes = getBase64Encoder().encode('hello+world'); // 0x85e965a3ec28ae57
|
|
109
|
+
* const value = getBase64Decoder().decode(bytes); // "hello+world"
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* ## Base 58 codec
|
|
113
|
+
*
|
|
114
|
+
* The `getBase58Codec` function encodes and decodes a base-58 string to and from a byte array.
|
|
115
|
+
*
|
|
116
|
+
* ```ts
|
|
117
|
+
* const bytes = getBase58Codec().encode('heLLo'); // 0x1b6a3070
|
|
118
|
+
* const value = getBase58Codec().decode(bytes); // "heLLo"
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* As usual, separate `getBase58Encoder` and `getBase58Decoder` functions are also available.
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* const bytes = getBase58Encoder().encode('heLLo'); // 0x1b6a3070
|
|
125
|
+
* const value = getBase58Decoder().decode(bytes); // "heLLo"
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* ## Base 16 codec
|
|
129
|
+
*
|
|
130
|
+
* The `getBase16Codec` function encodes and decodes a base-16 string to and from a byte array.
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* const bytes = getBase16Codec().encode('deadface'); // 0xdeadface
|
|
134
|
+
* const value = getBase16Codec().decode(bytes); // "deadface"
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* As usual, separate `getBase16Encoder` and `getBase16Decoder` functions are also available.
|
|
138
|
+
*
|
|
139
|
+
* ```ts
|
|
140
|
+
* const bytes = getBase16Encoder().encode('deadface'); // 0xdeadface
|
|
141
|
+
* const value = getBase16Decoder().decode(bytes); // "deadface"
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* ## Base 10 codec
|
|
145
|
+
*
|
|
146
|
+
* The `getBase10Codec` function encodes and decodes a base-10 string to and from a byte array.
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* const bytes = getBase10Codec().encode('1024'); // 0x0400
|
|
150
|
+
* const value = getBase10Codec().decode(bytes); // "1024"
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* As usual, separate `getBase10Encoder` and `getBase10Decoder` functions are also available.
|
|
154
|
+
*
|
|
155
|
+
* ```ts
|
|
156
|
+
* const bytes = getBase10Encoder().encode('1024'); // 0x0400
|
|
157
|
+
* const value = getBase10Decoder().decode(bytes); // "1024"
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* ## Base X codec
|
|
161
|
+
*
|
|
162
|
+
* The `getBaseXCodec` accepts a custom `alphabet` of `X` characters and creates a base-X codec using that alphabet.
|
|
163
|
+
* It does so by iteratively dividing by `X` and handling leading zeros.
|
|
164
|
+
*
|
|
165
|
+
* The base-10 and base-58 codecs use this base-x codec under the hood.
|
|
166
|
+
*
|
|
167
|
+
* ```ts
|
|
168
|
+
* const alphabet = '0ehlo';
|
|
169
|
+
* const bytes = getBaseXCodec(alphabet).encode('hello'); // 0x05bd
|
|
170
|
+
* const value = getBaseXCodec(alphabet).decode(bytes); // "hello"
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* As usual, separate `getBaseXEncoder` and `getBaseXDecoder` functions are also available.
|
|
174
|
+
*
|
|
175
|
+
* ```ts
|
|
176
|
+
* const bytes = getBaseXEncoder(alphabet).encode('hello'); // 0x05bd
|
|
177
|
+
* const value = getBaseXDecoder(alphabet).decode(bytes); // "hello"
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* ## Re-slicing base X codec
|
|
181
|
+
*
|
|
182
|
+
* The `getBaseXResliceCodec` also creates a base-x codec but uses a different strategy.
|
|
183
|
+
* It re-slices bytes into custom chunks of bits that are then mapped to a provided `alphabet`.
|
|
184
|
+
* The number of bits per chunk is also provided and should typically be set to `log2(alphabet.length)`.
|
|
185
|
+
*
|
|
186
|
+
* This is typically used to create codecs whose alphabet’s length is a power of 2 such as base-16 or base-64.
|
|
187
|
+
*
|
|
188
|
+
* ```ts
|
|
189
|
+
* const bytes = getBaseXResliceCodec('elho', 2).encode('hellolol'); // 0x4aee
|
|
190
|
+
* const value = getBaseXResliceCodec('elho', 2).decode(bytes); // "hellolol"
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* As usual, separate `getBaseXResliceEncoder` and `getBaseXResliceDecoder` functions are also available.
|
|
194
|
+
*
|
|
195
|
+
* ```ts
|
|
196
|
+
* const bytes = getBaseXResliceEncoder('elho', 2).encode('hellolol'); // 0x4aee
|
|
197
|
+
* const value = getBaseXResliceDecoder('elho', 2).decode(bytes); // "hellolol"
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
* @packageDocumentation
|
|
201
|
+
*/
|
|
202
|
+
export * from './assertions';
|
|
203
|
+
export * from './base10';
|
|
204
|
+
export * from './base16';
|
|
205
|
+
export * from './base58';
|
|
206
|
+
export * from './base64';
|
|
207
|
+
export * from './baseX';
|
|
208
|
+
export * from './baseX-reslice';
|
|
209
|
+
export * from './null-characters';
|
|
210
|
+
export * from './utf8';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes all null characters (`\u0000`) from a string.
|
|
3
|
+
*
|
|
4
|
+
* This function cleans a string by stripping out any null characters,
|
|
5
|
+
* which are often used as padding in fixed-size string encodings.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The string to process.
|
|
8
|
+
* @returns The input string with all null characters removed.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* Removing null characters from a string.
|
|
12
|
+
* ```ts
|
|
13
|
+
* removeNullCharacters('hello\u0000\u0000'); // "hello"
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const removeNullCharacters = (value: string) =>
|
|
17
|
+
// eslint-disable-next-line no-control-regex
|
|
18
|
+
value.replace(/\u0000/g, '');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Pads a string with null characters (`\u0000`) at the end to reach a fixed length.
|
|
22
|
+
*
|
|
23
|
+
* If the input string is shorter than the specified length, it is padded with null characters
|
|
24
|
+
* until it reaches the desired size. If it is already long enough, it remains unchanged.
|
|
25
|
+
*
|
|
26
|
+
* @param value - The string to pad.
|
|
27
|
+
* @param chars - The total length of the resulting string, including padding.
|
|
28
|
+
* @returns The input string padded with null characters up to the specified length.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* Padding a string with null characters.
|
|
32
|
+
* ```ts
|
|
33
|
+
* padNullCharacters('hello', 8); // "hello\u0000\u0000\u0000"
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export const padNullCharacters = (value: string, chars: number) => value.padEnd(chars, '\u0000');
|
package/src/utf8.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
combineCodec,
|
|
3
|
+
createDecoder,
|
|
4
|
+
createEncoder,
|
|
5
|
+
VariableSizeCodec,
|
|
6
|
+
VariableSizeDecoder,
|
|
7
|
+
VariableSizeEncoder,
|
|
8
|
+
} from '@solana/codecs-core';
|
|
9
|
+
import { TextDecoder, TextEncoder } from '@solana/text-encoding-impl';
|
|
10
|
+
|
|
11
|
+
import { removeNullCharacters } from './null-characters';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns an encoder for UTF-8 strings.
|
|
15
|
+
*
|
|
16
|
+
* This encoder serializes strings using UTF-8 encoding.
|
|
17
|
+
* The encoded output contains as many bytes as needed to represent the string.
|
|
18
|
+
*
|
|
19
|
+
* For more details, see {@link getUtf8Codec}.
|
|
20
|
+
*
|
|
21
|
+
* @returns A `VariableSizeEncoder<string>` for encoding UTF-8 strings.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* Encoding a UTF-8 string.
|
|
25
|
+
* ```ts
|
|
26
|
+
* const encoder = getUtf8Encoder();
|
|
27
|
+
* const bytes = encoder.encode('hello'); // 0x68656c6c6f
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @see {@link getUtf8Codec}
|
|
31
|
+
*/
|
|
32
|
+
export const getUtf8Encoder = (): VariableSizeEncoder<string> => {
|
|
33
|
+
let textEncoder: TextEncoder;
|
|
34
|
+
return createEncoder({
|
|
35
|
+
getSizeFromValue: value => (textEncoder ||= new TextEncoder()).encode(value).length,
|
|
36
|
+
write: (value: string, bytes, offset) => {
|
|
37
|
+
const bytesToAdd = (textEncoder ||= new TextEncoder()).encode(value);
|
|
38
|
+
bytes.set(bytesToAdd, offset);
|
|
39
|
+
return offset + bytesToAdd.length;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns a decoder for UTF-8 strings.
|
|
46
|
+
*
|
|
47
|
+
* This decoder deserializes UTF-8 encoded strings from a byte array.
|
|
48
|
+
* It reads all available bytes starting from the given offset.
|
|
49
|
+
*
|
|
50
|
+
* For more details, see {@link getUtf8Codec}.
|
|
51
|
+
*
|
|
52
|
+
* @returns A `VariableSizeDecoder<string>` for decoding UTF-8 strings.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* Decoding a UTF-8 string.
|
|
56
|
+
* ```ts
|
|
57
|
+
* const decoder = getUtf8Decoder();
|
|
58
|
+
* const value = decoder.decode(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // "hello"
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @see {@link getUtf8Codec}
|
|
62
|
+
*/
|
|
63
|
+
export const getUtf8Decoder = (): VariableSizeDecoder<string> => {
|
|
64
|
+
let textDecoder: TextDecoder;
|
|
65
|
+
return createDecoder({
|
|
66
|
+
read(bytes, offset) {
|
|
67
|
+
const value = (textDecoder ||= new TextDecoder()).decode(bytes.slice(offset));
|
|
68
|
+
return [removeNullCharacters(value), bytes.length];
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns a codec for encoding and decoding UTF-8 strings.
|
|
75
|
+
*
|
|
76
|
+
* This codec serializes strings using UTF-8 encoding.
|
|
77
|
+
* The encoded output contains as many bytes as needed to represent the string.
|
|
78
|
+
*
|
|
79
|
+
* @returns A `VariableSizeCodec<string>` for encoding and decoding UTF-8 strings.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* Encoding and decoding a UTF-8 string.
|
|
83
|
+
* ```ts
|
|
84
|
+
* const codec = getUtf8Codec();
|
|
85
|
+
* const bytes = codec.encode('hello'); // 0x68656c6c6f
|
|
86
|
+
* const value = codec.decode(bytes); // "hello"
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @remarks
|
|
90
|
+
* This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
|
|
91
|
+
*
|
|
92
|
+
* If you need a fixed-size UTF-8 codec, consider using {@link fixCodecSize}.
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const codec = fixCodecSize(getUtf8Codec(), 5);
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* If you need a size-prefixed UTF-8 codec, consider using {@link addCodecSizePrefix}.
|
|
99
|
+
*
|
|
100
|
+
* ```ts
|
|
101
|
+
* const codec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* Separate {@link getUtf8Encoder} and {@link getUtf8Decoder} functions are available.
|
|
105
|
+
*
|
|
106
|
+
* ```ts
|
|
107
|
+
* const bytes = getUtf8Encoder().encode('hello');
|
|
108
|
+
* const value = getUtf8Decoder().decode(bytes);
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @see {@link getUtf8Encoder}
|
|
112
|
+
* @see {@link getUtf8Decoder}
|
|
113
|
+
*/
|
|
114
|
+
export const getUtf8Codec = (): VariableSizeCodec<string> => combineCodec(getUtf8Encoder(), getUtf8Decoder());
|