@solana/codecs-strings 2.0.0-20241006045741
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/LICENSE +20 -0
- package/README.md +201 -0
- package/dist/index.browser.cjs +288 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.mjs +263 -0
- package/dist/index.browser.mjs.map +1 -0
- package/dist/index.native.mjs +231 -0
- package/dist/index.native.mjs.map +1 -0
- package/dist/index.node.cjs +267 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.mjs +242 -0
- package/dist/index.node.mjs.map +1 -0
- package/dist/types/assertions.d.ts +5 -0
- package/dist/types/assertions.d.ts.map +1 -0
- package/dist/types/base10.d.ts +7 -0
- package/dist/types/base10.d.ts.map +1 -0
- package/dist/types/base16.d.ts +8 -0
- package/dist/types/base16.d.ts.map +1 -0
- package/dist/types/base58.d.ts +7 -0
- package/dist/types/base58.d.ts.map +1 -0
- package/dist/types/base64.d.ts +8 -0
- package/dist/types/base64.d.ts.map +1 -0
- package/dist/types/baseX-reslice.d.ts +20 -0
- package/dist/types/baseX-reslice.d.ts.map +1 -0
- package/dist/types/baseX.d.ts +24 -0
- package/dist/types/baseX.d.ts.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/null-characters.d.ts +5 -0
- package/dist/types/null-characters.d.ts.map +1 -0
- package/dist/types/utf8.d.ts +8 -0
- package/dist/types/utf8.d.ts.map +1 -0
- package/package.json +93 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { SolanaError, SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE } from '@solana/errors';
|
|
2
|
+
import { createEncoder, createDecoder, combineCodec } from '@solana/codecs-core';
|
|
3
|
+
|
|
4
|
+
// src/assertions.ts
|
|
5
|
+
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
|
|
6
|
+
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
|
|
7
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
8
|
+
alphabet: alphabet4,
|
|
9
|
+
base: alphabet4.length,
|
|
10
|
+
value: givenValue
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
var getBaseXEncoder = (alphabet4) => {
|
|
15
|
+
return createEncoder({
|
|
16
|
+
getSizeFromValue: (value) => {
|
|
17
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
18
|
+
if (!tailChars) return value.length;
|
|
19
|
+
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
20
|
+
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
|
|
21
|
+
},
|
|
22
|
+
write(value, bytes, offset) {
|
|
23
|
+
assertValidBaseString(alphabet4, value);
|
|
24
|
+
if (value === "") return offset;
|
|
25
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
26
|
+
if (!tailChars) {
|
|
27
|
+
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
|
|
28
|
+
return offset + leadingZeroes.length;
|
|
29
|
+
}
|
|
30
|
+
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
31
|
+
const tailBytes = [];
|
|
32
|
+
while (base10Number > 0n) {
|
|
33
|
+
tailBytes.unshift(Number(base10Number % 256n));
|
|
34
|
+
base10Number /= 256n;
|
|
35
|
+
}
|
|
36
|
+
const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
|
|
37
|
+
bytes.set(bytesToAdd, offset);
|
|
38
|
+
return offset + bytesToAdd.length;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
var getBaseXDecoder = (alphabet4) => {
|
|
43
|
+
return createDecoder({
|
|
44
|
+
read(rawBytes, offset) {
|
|
45
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
46
|
+
if (bytes.length === 0) return ["", 0];
|
|
47
|
+
let trailIndex = bytes.findIndex((n) => n !== 0);
|
|
48
|
+
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
|
|
49
|
+
const leadingZeroes = alphabet4[0].repeat(trailIndex);
|
|
50
|
+
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
|
|
51
|
+
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
|
|
52
|
+
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
|
|
53
|
+
return [leadingZeroes + tailChars, rawBytes.length];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
|
|
58
|
+
function partitionLeadingZeroes(value, zeroCharacter) {
|
|
59
|
+
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
|
|
60
|
+
return [leadingZeros, tailChars];
|
|
61
|
+
}
|
|
62
|
+
function getBigIntFromBaseX(value, alphabet4) {
|
|
63
|
+
const base = BigInt(alphabet4.length);
|
|
64
|
+
let sum = 0n;
|
|
65
|
+
for (const char of value) {
|
|
66
|
+
sum *= base;
|
|
67
|
+
sum += BigInt(alphabet4.indexOf(char));
|
|
68
|
+
}
|
|
69
|
+
return sum;
|
|
70
|
+
}
|
|
71
|
+
function getBaseXFromBigInt(value, alphabet4) {
|
|
72
|
+
const base = BigInt(alphabet4.length);
|
|
73
|
+
const tailChars = [];
|
|
74
|
+
while (value > 0n) {
|
|
75
|
+
tailChars.unshift(alphabet4[Number(value % base)]);
|
|
76
|
+
value /= base;
|
|
77
|
+
}
|
|
78
|
+
return tailChars.join("");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/base10.ts
|
|
82
|
+
var alphabet = "0123456789";
|
|
83
|
+
var getBase10Encoder = () => getBaseXEncoder(alphabet);
|
|
84
|
+
var getBase10Decoder = () => getBaseXDecoder(alphabet);
|
|
85
|
+
var getBase10Codec = () => getBaseXCodec(alphabet);
|
|
86
|
+
var INVALID_STRING_ERROR_BASE_CONFIG = {
|
|
87
|
+
alphabet: "0123456789abcdef",
|
|
88
|
+
base: 16
|
|
89
|
+
};
|
|
90
|
+
function charCodeToBase16(char) {
|
|
91
|
+
if (char >= 48 /* ZERO */ && char <= 57 /* NINE */) return char - 48 /* ZERO */;
|
|
92
|
+
if (char >= 65 /* A_UP */ && char <= 70 /* F_UP */) return char - (65 /* A_UP */ - 10);
|
|
93
|
+
if (char >= 97 /* A_LO */ && char <= 102 /* F_LO */) return char - (97 /* A_LO */ - 10);
|
|
94
|
+
}
|
|
95
|
+
var getBase16Encoder = () => createEncoder({
|
|
96
|
+
getSizeFromValue: (value) => Math.ceil(value.length / 2),
|
|
97
|
+
write(value, bytes, offset) {
|
|
98
|
+
const len = value.length;
|
|
99
|
+
const al = len / 2;
|
|
100
|
+
if (len === 1) {
|
|
101
|
+
const c = value.charCodeAt(0);
|
|
102
|
+
const n = charCodeToBase16(c);
|
|
103
|
+
if (n === void 0) {
|
|
104
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
105
|
+
...INVALID_STRING_ERROR_BASE_CONFIG,
|
|
106
|
+
value
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
bytes.set([n], offset);
|
|
110
|
+
return 1 + offset;
|
|
111
|
+
}
|
|
112
|
+
const hexBytes = new Uint8Array(al);
|
|
113
|
+
for (let i = 0, j = 0; i < al; i++) {
|
|
114
|
+
const c1 = value.charCodeAt(j++);
|
|
115
|
+
const c2 = value.charCodeAt(j++);
|
|
116
|
+
const n1 = charCodeToBase16(c1);
|
|
117
|
+
const n2 = charCodeToBase16(c2);
|
|
118
|
+
if (n1 === void 0 || n2 === void 0 && !Number.isNaN(c2)) {
|
|
119
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
120
|
+
...INVALID_STRING_ERROR_BASE_CONFIG,
|
|
121
|
+
value
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
hexBytes[i] = !Number.isNaN(c2) ? n1 << 4 | (n2 ?? 0) : n1;
|
|
125
|
+
}
|
|
126
|
+
bytes.set(hexBytes, offset);
|
|
127
|
+
return hexBytes.length + offset;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
var getBase16Decoder = () => createDecoder({
|
|
131
|
+
read(bytes, offset) {
|
|
132
|
+
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
|
|
133
|
+
return [value, bytes.length];
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
|
|
137
|
+
|
|
138
|
+
// src/base58.ts
|
|
139
|
+
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
140
|
+
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
|
|
141
|
+
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
|
|
142
|
+
var getBase58Codec = () => getBaseXCodec(alphabet2);
|
|
143
|
+
var getBaseXResliceEncoder = (alphabet4, bits) => createEncoder({
|
|
144
|
+
getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
|
|
145
|
+
write(value, bytes, offset) {
|
|
146
|
+
assertValidBaseString(alphabet4, value);
|
|
147
|
+
if (value === "") return offset;
|
|
148
|
+
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
|
|
149
|
+
const reslicedBytes = reslice(charIndices, bits, 8, false);
|
|
150
|
+
bytes.set(reslicedBytes, offset);
|
|
151
|
+
return reslicedBytes.length + offset;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
var getBaseXResliceDecoder = (alphabet4, bits) => createDecoder({
|
|
155
|
+
read(rawBytes, offset = 0) {
|
|
156
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
157
|
+
if (bytes.length === 0) return ["", rawBytes.length];
|
|
158
|
+
const charIndices = reslice([...bytes], 8, bits, true);
|
|
159
|
+
return [charIndices.map((i) => alphabet4[i]).join(""), rawBytes.length];
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
|
|
163
|
+
function reslice(input, inputBits, outputBits, useRemainder) {
|
|
164
|
+
const output = [];
|
|
165
|
+
let accumulator = 0;
|
|
166
|
+
let bitsInAccumulator = 0;
|
|
167
|
+
const mask = (1 << outputBits) - 1;
|
|
168
|
+
for (const value of input) {
|
|
169
|
+
accumulator = accumulator << inputBits | value;
|
|
170
|
+
bitsInAccumulator += inputBits;
|
|
171
|
+
while (bitsInAccumulator >= outputBits) {
|
|
172
|
+
bitsInAccumulator -= outputBits;
|
|
173
|
+
output.push(accumulator >> bitsInAccumulator & mask);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (useRemainder && bitsInAccumulator > 0) {
|
|
177
|
+
output.push(accumulator << outputBits - bitsInAccumulator & mask);
|
|
178
|
+
}
|
|
179
|
+
return output;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/base64.ts
|
|
183
|
+
var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
184
|
+
var getBase64Encoder = () => {
|
|
185
|
+
{
|
|
186
|
+
return createEncoder({
|
|
187
|
+
getSizeFromValue: (value) => {
|
|
188
|
+
try {
|
|
189
|
+
return atob(value).length;
|
|
190
|
+
} catch (e2) {
|
|
191
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
192
|
+
alphabet: alphabet3,
|
|
193
|
+
base: 64,
|
|
194
|
+
value
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
write(value, bytes, offset) {
|
|
199
|
+
try {
|
|
200
|
+
const bytesToAdd = atob(value).split("").map((c) => c.charCodeAt(0));
|
|
201
|
+
bytes.set(bytesToAdd, offset);
|
|
202
|
+
return bytesToAdd.length + offset;
|
|
203
|
+
} catch (e2) {
|
|
204
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
205
|
+
alphabet: alphabet3,
|
|
206
|
+
base: 64,
|
|
207
|
+
value
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
var getBase64Decoder = () => {
|
|
215
|
+
{
|
|
216
|
+
return createDecoder({
|
|
217
|
+
read(bytes, offset = 0) {
|
|
218
|
+
const slice = bytes.slice(offset);
|
|
219
|
+
const value = btoa(String.fromCharCode(...slice));
|
|
220
|
+
return [value, bytes.length];
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
|
|
226
|
+
|
|
227
|
+
// src/null-characters.ts
|
|
228
|
+
var removeNullCharacters = (value) => (
|
|
229
|
+
// eslint-disable-next-line no-control-regex
|
|
230
|
+
value.replace(/\u0000/g, "")
|
|
231
|
+
);
|
|
232
|
+
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
|
|
233
|
+
|
|
234
|
+
// ../text-encoding-impl/dist/index.browser.mjs
|
|
235
|
+
var e = globalThis.TextDecoder;
|
|
236
|
+
var o = globalThis.TextEncoder;
|
|
237
|
+
|
|
238
|
+
// src/utf8.ts
|
|
239
|
+
var getUtf8Encoder = () => {
|
|
240
|
+
let textEncoder;
|
|
241
|
+
return createEncoder({
|
|
242
|
+
getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
|
|
243
|
+
write: (value, bytes, offset) => {
|
|
244
|
+
const bytesToAdd = (textEncoder ||= new o()).encode(value);
|
|
245
|
+
bytes.set(bytesToAdd, offset);
|
|
246
|
+
return offset + bytesToAdd.length;
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
var getUtf8Decoder = () => {
|
|
251
|
+
let textDecoder;
|
|
252
|
+
return createDecoder({
|
|
253
|
+
read(bytes, offset) {
|
|
254
|
+
const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
|
|
255
|
+
return [removeNullCharacters(value), bytes.length];
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
|
|
260
|
+
|
|
261
|
+
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 };
|
|
262
|
+
//# sourceMappingURL=index.browser.mjs.map
|
|
263
|
+
//# sourceMappingURL=index.browser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assertions.ts","../src/baseX.ts","../src/base10.ts","../src/base16.ts","../src/base58.ts","../src/baseX-reslice.ts","../src/base64.ts","../src/null-characters.ts","../../text-encoding-impl/src/index.browser.ts","../src/utf8.ts"],"names":["alphabet","createEncoder","SolanaError","SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE","createDecoder","combineCodec","e","TextDecoder","TextEncoder"],"mappings":";;;;AAKO,SAAS,qBAAsBA,CAAAA,SAAAA,EAAkB,SAAmB,EAAA,UAAA,GAAa,SAAW,EAAA;AAC/F,EAAI,IAAA,CAAC,UAAU,KAAM,CAAA,IAAI,OAAO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R_BASE, SolanaError } from '@solana/errors';\n\n/**\n * Asserts that a given string matches a given alphabet.\n */\nexport function assertValidBaseString(alphabet: string, testValue: string, givenValue = testValue) {\n if (!testValue.match(new RegExp(`^[${alphabet}]*$`))) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n alphabet,\n base: alphabet.length,\n value: givenValue,\n });\n }\n}\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/**\n * Encodes a string using a custom alphabet by dividing\n * by the base and handling leading zeroes.\n * @see {@link getBaseXCodec} for a more detailed description.\n */\nexport const getBaseXEncoder = (alphabet: string): VariableSizeEncoder<string> => {\n return createEncoder({\n getSizeFromValue: (value: string): number => {\n const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);\n if (!tailChars) return value.length;\n\n const base10Number = getBigIntFromBaseX(tailChars, alphabet);\n return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);\n },\n write(value: string, bytes, offset) {\n // Check if the value is valid.\n assertValidBaseString(alphabet, value);\n if (value === '') return offset;\n\n // Handle leading zeroes.\n const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);\n if (!tailChars) {\n bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);\n return offset + leadingZeroes.length;\n }\n\n // From baseX to base10.\n let base10Number = getBigIntFromBaseX(tailChars, alphabet);\n\n // From base10 to bytes.\n const tailBytes: number[] = [];\n while (base10Number > 0n) {\n tailBytes.unshift(Number(base10Number % 256n));\n base10Number /= 256n;\n }\n\n const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];\n bytes.set(bytesToAdd, offset);\n return offset + bytesToAdd.length;\n },\n });\n};\n\n/**\n * Decodes a string using a custom alphabet by dividing\n * by the base and handling leading zeroes.\n * @see {@link getBaseXCodec} for a more detailed description.\n */\nexport const getBaseXDecoder = (alphabet: string): VariableSizeDecoder<string> => {\n return createDecoder({\n read(rawBytes, offset): [string, number] {\n const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);\n if (bytes.length === 0) return ['', 0];\n\n // Handle leading zeroes.\n let trailIndex = bytes.findIndex(n => n !== 0);\n trailIndex = trailIndex === -1 ? bytes.length : trailIndex;\n const leadingZeroes = alphabet[0].repeat(trailIndex);\n if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];\n\n // From bytes to base10.\n const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);\n\n // From base10 to baseX.\n const tailChars = getBaseXFromBigInt(base10Number, alphabet);\n\n return [leadingZeroes + tailChars, rawBytes.length];\n },\n });\n};\n\n/**\n * A string codec that requires a custom alphabet and uses\n * the length of that alphabet as the base. It then divides\n * the input by the base as many times as necessary to get\n * the output. It also supports leading zeroes by using the\n * first character of the alphabet as the zero character.\n *\n * This can be used to create codecs such as base10 or base58.\n */\nexport const getBaseXCodec = (alphabet: string): VariableSizeCodec<string> =>\n combineCodec(getBaseXEncoder(alphabet), getBaseXDecoder(alphabet));\n\nfunction partitionLeadingZeroes(\n value: string,\n zeroCharacter: string,\n): [leadingZeros: string, tailChars: string | undefined] {\n const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));\n return [leadingZeros, tailChars];\n}\n\nfunction getBigIntFromBaseX(value: string, alphabet: string): bigint {\n const base = BigInt(alphabet.length);\n let sum = 0n;\n for (const char of value) {\n sum *= base;\n sum += BigInt(alphabet.indexOf(char));\n }\n return sum;\n}\n\nfunction getBaseXFromBigInt(value: bigint, alphabet: string): string {\n const base = BigInt(alphabet.length);\n const tailChars = [];\n while (value > 0n) {\n tailChars.unshift(alphabet[Number(value % base)]);\n value /= base;\n }\n return tailChars.join('');\n}\n","import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';\n\nconst alphabet = '0123456789';\n\n/** Encodes strings in base10. */\nexport const getBase10Encoder = () => getBaseXEncoder(alphabet);\n\n/** Decodes strings in base10. */\nexport const getBase10Decoder = () => getBaseXDecoder(alphabet);\n\n/** Encodes and decodes strings in base10. */\nexport const getBase10Codec = () => getBaseXCodec(alphabet);\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\nimport { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';\n\nconst enum HexC {\n ZERO = 48, // 0\n NINE = 57, // 9\n A_UP = 65, // A\n F_UP = 70, // F\n A_LO = 97, // a\n F_LO = 102, // f\n}\n\nconst INVALID_STRING_ERROR_BASE_CONFIG = {\n alphabet: '0123456789abcdef',\n base: 16,\n} as const;\n\nfunction charCodeToBase16(char: number) {\n if (char >= HexC.ZERO && char <= HexC.NINE) return char - HexC.ZERO;\n if (char >= HexC.A_UP && char <= HexC.F_UP) return char - (HexC.A_UP - 10);\n if (char >= HexC.A_LO && char <= HexC.F_LO) return char - (HexC.A_LO - 10);\n}\n\n/** Encodes strings in base16. */\nexport const getBase16Encoder = (): VariableSizeEncoder<string> =>\n createEncoder({\n getSizeFromValue: (value: string) => Math.ceil(value.length / 2),\n write(value: string, bytes, offset) {\n const len = value.length;\n const al = len / 2;\n if (len === 1) {\n const c = value.charCodeAt(0);\n const n = charCodeToBase16(c);\n if (n === undefined) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n ...INVALID_STRING_ERROR_BASE_CONFIG,\n value,\n });\n }\n bytes.set([n], offset);\n return 1 + offset;\n }\n const hexBytes = new Uint8Array(al);\n for (let i = 0, j = 0; i < al; i++) {\n const c1 = value.charCodeAt(j++);\n const c2 = value.charCodeAt(j++);\n\n const n1 = charCodeToBase16(c1);\n const n2 = charCodeToBase16(c2);\n if (n1 === undefined || (n2 === undefined && !Number.isNaN(c2))) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n ...INVALID_STRING_ERROR_BASE_CONFIG,\n value,\n });\n }\n hexBytes[i] = !Number.isNaN(c2) ? (n1 << 4) | (n2 ?? 0) : n1;\n }\n\n bytes.set(hexBytes, offset);\n return hexBytes.length + offset;\n },\n });\n\n/** Decodes strings in base16. */\nexport const getBase16Decoder = (): VariableSizeDecoder<string> =>\n createDecoder({\n read(bytes, offset) {\n const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');\n return [value, bytes.length];\n },\n });\n\n/** Encodes and decodes strings in base16. */\nexport const getBase16Codec = (): VariableSizeCodec<string> => combineCodec(getBase16Encoder(), getBase16Decoder());\n","import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';\n\nconst alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';\n\n/** Encodes strings in base58. */\nexport const getBase58Encoder = () => getBaseXEncoder(alphabet);\n\n/** Decodes strings in base58. */\nexport const getBase58Decoder = () => getBaseXDecoder(alphabet);\n\n/** Encodes and decodes strings in base58. */\nexport const getBase58Codec = () => getBaseXCodec(alphabet);\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/**\n * Encodes a string using a custom alphabet by reslicing the bits of the byte array.\n * @see {@link getBaseXResliceCodec} for a more detailed description.\n */\nexport const getBaseXResliceEncoder = (alphabet: string, bits: number): VariableSizeEncoder<string> =>\n createEncoder({\n getSizeFromValue: (value: string) => Math.floor((value.length * bits) / 8),\n write(value: string, bytes, offset) {\n assertValidBaseString(alphabet, value);\n if (value === '') return offset;\n const charIndices = [...value].map(c => alphabet.indexOf(c));\n const reslicedBytes = reslice(charIndices, bits, 8, false);\n bytes.set(reslicedBytes, offset);\n return reslicedBytes.length + offset;\n },\n });\n\n/**\n * Decodes a string using a custom alphabet by reslicing the bits of the byte array.\n * @see {@link getBaseXResliceCodec} for a more detailed description.\n */\nexport const getBaseXResliceDecoder = (alphabet: string, bits: number): VariableSizeDecoder<string> =>\n createDecoder({\n read(rawBytes, offset = 0): [string, number] {\n const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);\n if (bytes.length === 0) return ['', rawBytes.length];\n const charIndices = reslice([...bytes], 8, bits, true);\n return [charIndices.map(i => alphabet[i]).join(''), rawBytes.length];\n },\n });\n\n/**\n * A string serializer that reslices bytes into custom chunks\n * of bits that are then mapped to a custom alphabet.\n *\n * This can be used to create serializers whose alphabet\n * is a power of 2 such as base16 or base64.\n */\nexport const getBaseXResliceCodec = (alphabet: string, bits: number): VariableSizeCodec<string> =>\n combineCodec(getBaseXResliceEncoder(alphabet, bits), getBaseXResliceDecoder(alphabet, bits));\n\n/** Helper function to reslice the bits inside bytes. */\nfunction reslice(input: number[], inputBits: number, outputBits: number, useRemainder: boolean): number[] {\n const output = [];\n let accumulator = 0;\n let bitsInAccumulator = 0;\n const mask = (1 << outputBits) - 1;\n for (const value of input) {\n accumulator = (accumulator << inputBits) | value;\n bitsInAccumulator += inputBits;\n while (bitsInAccumulator >= outputBits) {\n bitsInAccumulator -= outputBits;\n output.push((accumulator >> bitsInAccumulator) & mask);\n }\n }\n if (useRemainder && bitsInAccumulator > 0) {\n output.push((accumulator << (outputBits - bitsInAccumulator)) & mask);\n }\n return output;\n}\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n transformDecoder,\n transformEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\nimport { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';\n\nimport { assertValidBaseString } from './assertions';\nimport { getBaseXResliceDecoder, getBaseXResliceEncoder } from './baseX-reslice';\n\nconst alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';\n\n/** Encodes strings in base64. */\nexport const getBase64Encoder = (): VariableSizeEncoder<string> => {\n if (__BROWSER__) {\n return createEncoder({\n getSizeFromValue: (value: string) => {\n try {\n return (atob as Window['atob'])(value).length;\n } catch (e) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n alphabet,\n base: 64,\n value,\n });\n }\n },\n write(value: string, bytes, offset) {\n try {\n const bytesToAdd = (atob as Window['atob'])(value)\n .split('')\n .map(c => c.charCodeAt(0));\n bytes.set(bytesToAdd, offset);\n return bytesToAdd.length + offset;\n } catch (e) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n alphabet,\n base: 64,\n value,\n });\n }\n },\n });\n }\n\n if (__NODEJS__) {\n return createEncoder({\n getSizeFromValue: (value: string) => Buffer.from(value, 'base64').length,\n write(value: string, bytes, offset) {\n assertValidBaseString(alphabet, value.replace(/=/g, ''));\n const buffer = Buffer.from(value, 'base64');\n bytes.set(buffer, offset);\n return buffer.length + offset;\n },\n });\n }\n\n return transformEncoder(getBaseXResliceEncoder(alphabet, 6), (value: string): string => value.replace(/=/g, ''));\n};\n\n/** Decodes strings in base64. */\nexport const getBase64Decoder = (): VariableSizeDecoder<string> => {\n if (__BROWSER__) {\n return createDecoder({\n read(bytes, offset = 0) {\n const slice = bytes.slice(offset);\n const value = (btoa as Window['btoa'])(String.fromCharCode(...slice));\n return [value, bytes.length];\n },\n });\n }\n\n if (__NODEJS__) {\n return createDecoder({\n read: (bytes, offset = 0) => [Buffer.from(bytes, offset).toString('base64'), bytes.length],\n });\n }\n\n return transformDecoder(getBaseXResliceDecoder(alphabet, 6), (value: string): string =>\n value.padEnd(Math.ceil(value.length / 4) * 4, '='),\n );\n};\n\n/** Encodes and decodes strings in base64. */\nexport const getBase64Codec = (): VariableSizeCodec<string> => combineCodec(getBase64Encoder(), getBase64Decoder());\n","/**Removes null characters from a string. */\nexport const removeNullCharacters = (value: string) =>\n // eslint-disable-next-line no-control-regex\n value.replace(/\\u0000/g, '');\n\n/** Pads a string with null characters at the end. */\nexport const padNullCharacters = (value: string, chars: number) => value.padEnd(chars, '\\u0000');\n","export const TextDecoder = globalThis.TextDecoder;\nexport const TextEncoder = globalThis.TextEncoder;\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\nimport { TextDecoder, TextEncoder } from '@solana/text-encoding-impl';\n\nimport { removeNullCharacters } from './null-characters';\n\n/** Encodes UTF-8 strings using the native `TextEncoder` API. */\nexport const getUtf8Encoder = (): VariableSizeEncoder<string> => {\n let textEncoder: TextEncoder;\n return createEncoder({\n getSizeFromValue: value => (textEncoder ||= new TextEncoder()).encode(value).length,\n write: (value: string, bytes, offset) => {\n const bytesToAdd = (textEncoder ||= new TextEncoder()).encode(value);\n bytes.set(bytesToAdd, offset);\n return offset + bytesToAdd.length;\n },\n });\n};\n\n/** Decodes UTF-8 strings using the native `TextDecoder` API. */\nexport const getUtf8Decoder = (): VariableSizeDecoder<string> => {\n let textDecoder: TextDecoder;\n return createDecoder({\n read(bytes, offset) {\n const value = (textDecoder ||= new TextDecoder()).decode(bytes.slice(offset));\n return [removeNullCharacters(value), bytes.length];\n },\n });\n};\n\n/** Encodes and decodes UTF-8 strings using the native `TextEncoder` and `TextDecoder` API. */\nexport const getUtf8Codec = (): VariableSizeCodec<string> => combineCodec(getUtf8Encoder(), getUtf8Decoder());\n"]}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { SolanaError, SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE } from '@solana/errors';
|
|
2
|
+
import { createEncoder, createDecoder, combineCodec, transformEncoder, transformDecoder } from '@solana/codecs-core';
|
|
3
|
+
|
|
4
|
+
// src/assertions.ts
|
|
5
|
+
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
|
|
6
|
+
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
|
|
7
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
8
|
+
alphabet: alphabet4,
|
|
9
|
+
base: alphabet4.length,
|
|
10
|
+
value: givenValue
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
var getBaseXEncoder = (alphabet4) => {
|
|
15
|
+
return createEncoder({
|
|
16
|
+
getSizeFromValue: (value) => {
|
|
17
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
18
|
+
if (!tailChars) return value.length;
|
|
19
|
+
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
20
|
+
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
|
|
21
|
+
},
|
|
22
|
+
write(value, bytes, offset) {
|
|
23
|
+
assertValidBaseString(alphabet4, value);
|
|
24
|
+
if (value === "") return offset;
|
|
25
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
26
|
+
if (!tailChars) {
|
|
27
|
+
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
|
|
28
|
+
return offset + leadingZeroes.length;
|
|
29
|
+
}
|
|
30
|
+
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
31
|
+
const tailBytes = [];
|
|
32
|
+
while (base10Number > 0n) {
|
|
33
|
+
tailBytes.unshift(Number(base10Number % 256n));
|
|
34
|
+
base10Number /= 256n;
|
|
35
|
+
}
|
|
36
|
+
const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
|
|
37
|
+
bytes.set(bytesToAdd, offset);
|
|
38
|
+
return offset + bytesToAdd.length;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
var getBaseXDecoder = (alphabet4) => {
|
|
43
|
+
return createDecoder({
|
|
44
|
+
read(rawBytes, offset) {
|
|
45
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
46
|
+
if (bytes.length === 0) return ["", 0];
|
|
47
|
+
let trailIndex = bytes.findIndex((n) => n !== 0);
|
|
48
|
+
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
|
|
49
|
+
const leadingZeroes = alphabet4[0].repeat(trailIndex);
|
|
50
|
+
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
|
|
51
|
+
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
|
|
52
|
+
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
|
|
53
|
+
return [leadingZeroes + tailChars, rawBytes.length];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
var getBaseXCodec = (alphabet4) => combineCodec(getBaseXEncoder(alphabet4), getBaseXDecoder(alphabet4));
|
|
58
|
+
function partitionLeadingZeroes(value, zeroCharacter) {
|
|
59
|
+
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
|
|
60
|
+
return [leadingZeros, tailChars];
|
|
61
|
+
}
|
|
62
|
+
function getBigIntFromBaseX(value, alphabet4) {
|
|
63
|
+
const base = BigInt(alphabet4.length);
|
|
64
|
+
let sum = 0n;
|
|
65
|
+
for (const char of value) {
|
|
66
|
+
sum *= base;
|
|
67
|
+
sum += BigInt(alphabet4.indexOf(char));
|
|
68
|
+
}
|
|
69
|
+
return sum;
|
|
70
|
+
}
|
|
71
|
+
function getBaseXFromBigInt(value, alphabet4) {
|
|
72
|
+
const base = BigInt(alphabet4.length);
|
|
73
|
+
const tailChars = [];
|
|
74
|
+
while (value > 0n) {
|
|
75
|
+
tailChars.unshift(alphabet4[Number(value % base)]);
|
|
76
|
+
value /= base;
|
|
77
|
+
}
|
|
78
|
+
return tailChars.join("");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/base10.ts
|
|
82
|
+
var alphabet = "0123456789";
|
|
83
|
+
var getBase10Encoder = () => getBaseXEncoder(alphabet);
|
|
84
|
+
var getBase10Decoder = () => getBaseXDecoder(alphabet);
|
|
85
|
+
var getBase10Codec = () => getBaseXCodec(alphabet);
|
|
86
|
+
var INVALID_STRING_ERROR_BASE_CONFIG = {
|
|
87
|
+
alphabet: "0123456789abcdef",
|
|
88
|
+
base: 16
|
|
89
|
+
};
|
|
90
|
+
function charCodeToBase16(char) {
|
|
91
|
+
if (char >= 48 /* ZERO */ && char <= 57 /* NINE */) return char - 48 /* ZERO */;
|
|
92
|
+
if (char >= 65 /* A_UP */ && char <= 70 /* F_UP */) return char - (65 /* A_UP */ - 10);
|
|
93
|
+
if (char >= 97 /* A_LO */ && char <= 102 /* F_LO */) return char - (97 /* A_LO */ - 10);
|
|
94
|
+
}
|
|
95
|
+
var getBase16Encoder = () => createEncoder({
|
|
96
|
+
getSizeFromValue: (value) => Math.ceil(value.length / 2),
|
|
97
|
+
write(value, bytes, offset) {
|
|
98
|
+
const len = value.length;
|
|
99
|
+
const al = len / 2;
|
|
100
|
+
if (len === 1) {
|
|
101
|
+
const c = value.charCodeAt(0);
|
|
102
|
+
const n = charCodeToBase16(c);
|
|
103
|
+
if (n === void 0) {
|
|
104
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
105
|
+
...INVALID_STRING_ERROR_BASE_CONFIG,
|
|
106
|
+
value
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
bytes.set([n], offset);
|
|
110
|
+
return 1 + offset;
|
|
111
|
+
}
|
|
112
|
+
const hexBytes = new Uint8Array(al);
|
|
113
|
+
for (let i = 0, j = 0; i < al; i++) {
|
|
114
|
+
const c1 = value.charCodeAt(j++);
|
|
115
|
+
const c2 = value.charCodeAt(j++);
|
|
116
|
+
const n1 = charCodeToBase16(c1);
|
|
117
|
+
const n2 = charCodeToBase16(c2);
|
|
118
|
+
if (n1 === void 0 || n2 === void 0 && !Number.isNaN(c2)) {
|
|
119
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
120
|
+
...INVALID_STRING_ERROR_BASE_CONFIG,
|
|
121
|
+
value
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
hexBytes[i] = !Number.isNaN(c2) ? n1 << 4 | (n2 ?? 0) : n1;
|
|
125
|
+
}
|
|
126
|
+
bytes.set(hexBytes, offset);
|
|
127
|
+
return hexBytes.length + offset;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
var getBase16Decoder = () => createDecoder({
|
|
131
|
+
read(bytes, offset) {
|
|
132
|
+
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
|
|
133
|
+
return [value, bytes.length];
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
var getBase16Codec = () => combineCodec(getBase16Encoder(), getBase16Decoder());
|
|
137
|
+
|
|
138
|
+
// src/base58.ts
|
|
139
|
+
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
140
|
+
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
|
|
141
|
+
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
|
|
142
|
+
var getBase58Codec = () => getBaseXCodec(alphabet2);
|
|
143
|
+
var getBaseXResliceEncoder = (alphabet4, bits) => createEncoder({
|
|
144
|
+
getSizeFromValue: (value) => Math.floor(value.length * bits / 8),
|
|
145
|
+
write(value, bytes, offset) {
|
|
146
|
+
assertValidBaseString(alphabet4, value);
|
|
147
|
+
if (value === "") return offset;
|
|
148
|
+
const charIndices = [...value].map((c) => alphabet4.indexOf(c));
|
|
149
|
+
const reslicedBytes = reslice(charIndices, bits, 8, false);
|
|
150
|
+
bytes.set(reslicedBytes, offset);
|
|
151
|
+
return reslicedBytes.length + offset;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
var getBaseXResliceDecoder = (alphabet4, bits) => createDecoder({
|
|
155
|
+
read(rawBytes, offset = 0) {
|
|
156
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
157
|
+
if (bytes.length === 0) return ["", rawBytes.length];
|
|
158
|
+
const charIndices = reslice([...bytes], 8, bits, true);
|
|
159
|
+
return [charIndices.map((i) => alphabet4[i]).join(""), rawBytes.length];
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
var getBaseXResliceCodec = (alphabet4, bits) => combineCodec(getBaseXResliceEncoder(alphabet4, bits), getBaseXResliceDecoder(alphabet4, bits));
|
|
163
|
+
function reslice(input, inputBits, outputBits, useRemainder) {
|
|
164
|
+
const output = [];
|
|
165
|
+
let accumulator = 0;
|
|
166
|
+
let bitsInAccumulator = 0;
|
|
167
|
+
const mask = (1 << outputBits) - 1;
|
|
168
|
+
for (const value of input) {
|
|
169
|
+
accumulator = accumulator << inputBits | value;
|
|
170
|
+
bitsInAccumulator += inputBits;
|
|
171
|
+
while (bitsInAccumulator >= outputBits) {
|
|
172
|
+
bitsInAccumulator -= outputBits;
|
|
173
|
+
output.push(accumulator >> bitsInAccumulator & mask);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (useRemainder && bitsInAccumulator > 0) {
|
|
177
|
+
output.push(accumulator << outputBits - bitsInAccumulator & mask);
|
|
178
|
+
}
|
|
179
|
+
return output;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/base64.ts
|
|
183
|
+
var alphabet3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
184
|
+
var getBase64Encoder = () => {
|
|
185
|
+
return transformEncoder(getBaseXResliceEncoder(alphabet3, 6), (value) => value.replace(/=/g, ""));
|
|
186
|
+
};
|
|
187
|
+
var getBase64Decoder = () => {
|
|
188
|
+
return transformDecoder(
|
|
189
|
+
getBaseXResliceDecoder(alphabet3, 6),
|
|
190
|
+
(value) => value.padEnd(Math.ceil(value.length / 4) * 4, "=")
|
|
191
|
+
);
|
|
192
|
+
};
|
|
193
|
+
var getBase64Codec = () => combineCodec(getBase64Encoder(), getBase64Decoder());
|
|
194
|
+
|
|
195
|
+
// src/null-characters.ts
|
|
196
|
+
var removeNullCharacters = (value) => (
|
|
197
|
+
// eslint-disable-next-line no-control-regex
|
|
198
|
+
value.replace(/\u0000/g, "")
|
|
199
|
+
);
|
|
200
|
+
var padNullCharacters = (value, chars) => value.padEnd(chars, "\0");
|
|
201
|
+
|
|
202
|
+
// ../text-encoding-impl/dist/index.browser.mjs
|
|
203
|
+
var e = globalThis.TextDecoder;
|
|
204
|
+
var o = globalThis.TextEncoder;
|
|
205
|
+
|
|
206
|
+
// src/utf8.ts
|
|
207
|
+
var getUtf8Encoder = () => {
|
|
208
|
+
let textEncoder;
|
|
209
|
+
return createEncoder({
|
|
210
|
+
getSizeFromValue: (value) => (textEncoder ||= new o()).encode(value).length,
|
|
211
|
+
write: (value, bytes, offset) => {
|
|
212
|
+
const bytesToAdd = (textEncoder ||= new o()).encode(value);
|
|
213
|
+
bytes.set(bytesToAdd, offset);
|
|
214
|
+
return offset + bytesToAdd.length;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
var getUtf8Decoder = () => {
|
|
219
|
+
let textDecoder;
|
|
220
|
+
return createDecoder({
|
|
221
|
+
read(bytes, offset) {
|
|
222
|
+
const value = (textDecoder ||= new e()).decode(bytes.slice(offset));
|
|
223
|
+
return [removeNullCharacters(value), bytes.length];
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
var getUtf8Codec = () => combineCodec(getUtf8Encoder(), getUtf8Decoder());
|
|
228
|
+
|
|
229
|
+
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 };
|
|
230
|
+
//# sourceMappingURL=index.native.mjs.map
|
|
231
|
+
//# sourceMappingURL=index.native.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assertions.ts","../src/baseX.ts","../src/base10.ts","../src/base16.ts","../src/base58.ts","../src/baseX-reslice.ts","../src/base64.ts","../src/null-characters.ts","../../text-encoding-impl/src/index.browser.ts","../src/utf8.ts"],"names":["alphabet","createEncoder","SolanaError","SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE","createDecoder","combineCodec","TextDecoder","TextEncoder"],"mappings":";;;;AAKO,SAAS,qBAAsBA,CAAAA,SAAAA,EAAkB,SAAmB,EAAA,UAAA,GAAa,SAAW,EAAA;AAC/F,EAAI,IAAA,CAAC,UAAU,KAAM,CAAA,IAAI,OAAO,CAAKA,EAAAA,EAAAA,SAAQ,CAAK,GAAA,CAAA,CAAC,CAAG,EAAA;AAClD,IAAM,MAAA,IAAI,YAAY,6CAA+C,EAAA;AAAA,MACjE,QAAAA,EAAAA,SAAAA;AAAA,MACA,MAAMA,SAAS,CAAA,MAAA;AAAA,MACf,KAAO,EAAA,UAAA;AAAA,KACV,CAAA,CAAA;AAAA,GACL;AACJ,CAAA;ACGa,IAAA,eAAA,GAAkB,CAACA,SAAkD,KAAA;AAC9E,EAAA,OAAO,aAAc,CAAA;AAAA,IACjB,gBAAA,EAAkB,CAAC,KAA0B,KAAA;AACzC,MAAM,MAAA,CAAC,eAAe,SAAS,CAAA,GAAI,uBAAuB,KAAOA,EAAAA,SAAAA,CAAS,CAAC,CAAC,CAAA,CAAA;AAC5E,MAAI,IAAA,CAAC,SAAW,EAAA,OAAO,KAAM,CAAA,MAAA,CAAA;AAE7B,MAAM,MAAA,YAAA,GAAe,kBAAmB,CAAA,SAAA,EAAWA,SAAQ,CAAA,CAAA;AAC3D,MAAO,OAAA,aAAA,CAAc,SAAS,IAAK,CAAA,IAAA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K,CAAE,CAAA,MAAA;AAAA,IAC7E,KAAO,EAAA,CAAC,KAAe,EAAA,KAAA,EAAO,MAAW,KAAA;AACrC,MAAA,MAAM,cAAc,WAAgB,KAAA,IAAI,CAAY,EAAA,EAAG,OAAO,KAAK,CAAA,CAAA;AACnE,MAAM,KAAA,CAAA,GAAA,CAAI,YAAY,MAAM,CAAA,CAAA;AAC5B,MAAA,OAAO,SAAS,UAAW,CAAA,MAAA,CAAA;AAAA,KAC/B;AAAA,GACH,CAAA,CAAA;AACL,EAAA;AAGO,IAAM,iBAAiB,MAAmC;AAC7D,EAAI,IAAA,WAAA,CAAA;AACJ,EAAA,OAAOG,aAAc,CAAA;AAAA,IACjB,IAAA,CAAK,OAAO,MAAQ,EAAA;AAChB,MAAM,MAAA,KAAA,GAAA,CAAS,gBAAgB,IAAI,CAAA,IAAe,MAAO,CAAA,KAAA,CAAM,KAAM,CAAA,MAAM,CAAC,CAAA,CAAA;AAC5E,MAAA,OAAO,CAAC,oBAAA,CAAqB,KAAK,CAAA,EAAG,MAAM,MAAM,CAAA,CAAA;AAAA,KACrD;AAAA,GACH,CAAA,CAAA;AACL,EAAA;AAGO,IAAM,eAAe,MAAiCC,YAAAA,CAAa,cAAe,EAAA,EAAG,gBAAgB","file":"index.native.mjs","sourcesContent":["import { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';\n\n/**\n * Asserts that a given string matches a given alphabet.\n */\nexport function assertValidBaseString(alphabet: string, testValue: string, givenValue = testValue) {\n if (!testValue.match(new RegExp(`^[${alphabet}]*$`))) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n alphabet,\n base: alphabet.length,\n value: givenValue,\n });\n }\n}\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/**\n * Encodes a string using a custom alphabet by dividing\n * by the base and handling leading zeroes.\n * @see {@link getBaseXCodec} for a more detailed description.\n */\nexport const getBaseXEncoder = (alphabet: string): VariableSizeEncoder<string> => {\n return createEncoder({\n getSizeFromValue: (value: string): number => {\n const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);\n if (!tailChars) return value.length;\n\n const base10Number = getBigIntFromBaseX(tailChars, alphabet);\n return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);\n },\n write(value: string, bytes, offset) {\n // Check if the value is valid.\n assertValidBaseString(alphabet, value);\n if (value === '') return offset;\n\n // Handle leading zeroes.\n const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet[0]);\n if (!tailChars) {\n bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);\n return offset + leadingZeroes.length;\n }\n\n // From baseX to base10.\n let base10Number = getBigIntFromBaseX(tailChars, alphabet);\n\n // From base10 to bytes.\n const tailBytes: number[] = [];\n while (base10Number > 0n) {\n tailBytes.unshift(Number(base10Number % 256n));\n base10Number /= 256n;\n }\n\n const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];\n bytes.set(bytesToAdd, offset);\n return offset + bytesToAdd.length;\n },\n });\n};\n\n/**\n * Decodes a string using a custom alphabet by dividing\n * by the base and handling leading zeroes.\n * @see {@link getBaseXCodec} for a more detailed description.\n */\nexport const getBaseXDecoder = (alphabet: string): VariableSizeDecoder<string> => {\n return createDecoder({\n read(rawBytes, offset): [string, number] {\n const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);\n if (bytes.length === 0) return ['', 0];\n\n // Handle leading zeroes.\n let trailIndex = bytes.findIndex(n => n !== 0);\n trailIndex = trailIndex === -1 ? bytes.length : trailIndex;\n const leadingZeroes = alphabet[0].repeat(trailIndex);\n if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];\n\n // From bytes to base10.\n const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);\n\n // From base10 to baseX.\n const tailChars = getBaseXFromBigInt(base10Number, alphabet);\n\n return [leadingZeroes + tailChars, rawBytes.length];\n },\n });\n};\n\n/**\n * A string codec that requires a custom alphabet and uses\n * the length of that alphabet as the base. It then divides\n * the input by the base as many times as necessary to get\n * the output. It also supports leading zeroes by using the\n * first character of the alphabet as the zero character.\n *\n * This can be used to create codecs such as base10 or base58.\n */\nexport const getBaseXCodec = (alphabet: string): VariableSizeCodec<string> =>\n combineCodec(getBaseXEncoder(alphabet), getBaseXDecoder(alphabet));\n\nfunction partitionLeadingZeroes(\n value: string,\n zeroCharacter: string,\n): [leadingZeros: string, tailChars: string | undefined] {\n const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));\n return [leadingZeros, tailChars];\n}\n\nfunction getBigIntFromBaseX(value: string, alphabet: string): bigint {\n const base = BigInt(alphabet.length);\n let sum = 0n;\n for (const char of value) {\n sum *= base;\n sum += BigInt(alphabet.indexOf(char));\n }\n return sum;\n}\n\nfunction getBaseXFromBigInt(value: bigint, alphabet: string): string {\n const base = BigInt(alphabet.length);\n const tailChars = [];\n while (value > 0n) {\n tailChars.unshift(alphabet[Number(value % base)]);\n value /= base;\n }\n return tailChars.join('');\n}\n","import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';\n\nconst alphabet = '0123456789';\n\n/** Encodes strings in base10. */\nexport const getBase10Encoder = () => getBaseXEncoder(alphabet);\n\n/** Decodes strings in base10. */\nexport const getBase10Decoder = () => getBaseXDecoder(alphabet);\n\n/** Encodes and decodes strings in base10. */\nexport const getBase10Codec = () => getBaseXCodec(alphabet);\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\nimport { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';\n\nconst enum HexC {\n ZERO = 48, // 0\n NINE = 57, // 9\n A_UP = 65, // A\n F_UP = 70, // F\n A_LO = 97, // a\n F_LO = 102, // f\n}\n\nconst INVALID_STRING_ERROR_BASE_CONFIG = {\n alphabet: '0123456789abcdef',\n base: 16,\n} as const;\n\nfunction charCodeToBase16(char: number) {\n if (char >= HexC.ZERO && char <= HexC.NINE) return char - HexC.ZERO;\n if (char >= HexC.A_UP && char <= HexC.F_UP) return char - (HexC.A_UP - 10);\n if (char >= HexC.A_LO && char <= HexC.F_LO) return char - (HexC.A_LO - 10);\n}\n\n/** Encodes strings in base16. */\nexport const getBase16Encoder = (): VariableSizeEncoder<string> =>\n createEncoder({\n getSizeFromValue: (value: string) => Math.ceil(value.length / 2),\n write(value: string, bytes, offset) {\n const len = value.length;\n const al = len / 2;\n if (len === 1) {\n const c = value.charCodeAt(0);\n const n = charCodeToBase16(c);\n if (n === undefined) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n ...INVALID_STRING_ERROR_BASE_CONFIG,\n value,\n });\n }\n bytes.set([n], offset);\n return 1 + offset;\n }\n const hexBytes = new Uint8Array(al);\n for (let i = 0, j = 0; i < al; i++) {\n const c1 = value.charCodeAt(j++);\n const c2 = value.charCodeAt(j++);\n\n const n1 = charCodeToBase16(c1);\n const n2 = charCodeToBase16(c2);\n if (n1 === undefined || (n2 === undefined && !Number.isNaN(c2))) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n ...INVALID_STRING_ERROR_BASE_CONFIG,\n value,\n });\n }\n hexBytes[i] = !Number.isNaN(c2) ? (n1 << 4) | (n2 ?? 0) : n1;\n }\n\n bytes.set(hexBytes, offset);\n return hexBytes.length + offset;\n },\n });\n\n/** Decodes strings in base16. */\nexport const getBase16Decoder = (): VariableSizeDecoder<string> =>\n createDecoder({\n read(bytes, offset) {\n const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');\n return [value, bytes.length];\n },\n });\n\n/** Encodes and decodes strings in base16. */\nexport const getBase16Codec = (): VariableSizeCodec<string> => combineCodec(getBase16Encoder(), getBase16Decoder());\n","import { getBaseXCodec, getBaseXDecoder, getBaseXEncoder } from './baseX';\n\nconst alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';\n\n/** Encodes strings in base58. */\nexport const getBase58Encoder = () => getBaseXEncoder(alphabet);\n\n/** Decodes strings in base58. */\nexport const getBase58Decoder = () => getBaseXDecoder(alphabet);\n\n/** Encodes and decodes strings in base58. */\nexport const getBase58Codec = () => getBaseXCodec(alphabet);\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\n\nimport { assertValidBaseString } from './assertions';\n\n/**\n * Encodes a string using a custom alphabet by reslicing the bits of the byte array.\n * @see {@link getBaseXResliceCodec} for a more detailed description.\n */\nexport const getBaseXResliceEncoder = (alphabet: string, bits: number): VariableSizeEncoder<string> =>\n createEncoder({\n getSizeFromValue: (value: string) => Math.floor((value.length * bits) / 8),\n write(value: string, bytes, offset) {\n assertValidBaseString(alphabet, value);\n if (value === '') return offset;\n const charIndices = [...value].map(c => alphabet.indexOf(c));\n const reslicedBytes = reslice(charIndices, bits, 8, false);\n bytes.set(reslicedBytes, offset);\n return reslicedBytes.length + offset;\n },\n });\n\n/**\n * Decodes a string using a custom alphabet by reslicing the bits of the byte array.\n * @see {@link getBaseXResliceCodec} for a more detailed description.\n */\nexport const getBaseXResliceDecoder = (alphabet: string, bits: number): VariableSizeDecoder<string> =>\n createDecoder({\n read(rawBytes, offset = 0): [string, number] {\n const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);\n if (bytes.length === 0) return ['', rawBytes.length];\n const charIndices = reslice([...bytes], 8, bits, true);\n return [charIndices.map(i => alphabet[i]).join(''), rawBytes.length];\n },\n });\n\n/**\n * A string serializer that reslices bytes into custom chunks\n * of bits that are then mapped to a custom alphabet.\n *\n * This can be used to create serializers whose alphabet\n * is a power of 2 such as base16 or base64.\n */\nexport const getBaseXResliceCodec = (alphabet: string, bits: number): VariableSizeCodec<string> =>\n combineCodec(getBaseXResliceEncoder(alphabet, bits), getBaseXResliceDecoder(alphabet, bits));\n\n/** Helper function to reslice the bits inside bytes. */\nfunction reslice(input: number[], inputBits: number, outputBits: number, useRemainder: boolean): number[] {\n const output = [];\n let accumulator = 0;\n let bitsInAccumulator = 0;\n const mask = (1 << outputBits) - 1;\n for (const value of input) {\n accumulator = (accumulator << inputBits) | value;\n bitsInAccumulator += inputBits;\n while (bitsInAccumulator >= outputBits) {\n bitsInAccumulator -= outputBits;\n output.push((accumulator >> bitsInAccumulator) & mask);\n }\n }\n if (useRemainder && bitsInAccumulator > 0) {\n output.push((accumulator << (outputBits - bitsInAccumulator)) & mask);\n }\n return output;\n}\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n transformDecoder,\n transformEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\nimport { SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, SolanaError } from '@solana/errors';\n\nimport { assertValidBaseString } from './assertions';\nimport { getBaseXResliceDecoder, getBaseXResliceEncoder } from './baseX-reslice';\n\nconst alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';\n\n/** Encodes strings in base64. */\nexport const getBase64Encoder = (): VariableSizeEncoder<string> => {\n if (__BROWSER__) {\n return createEncoder({\n getSizeFromValue: (value: string) => {\n try {\n return (atob as Window['atob'])(value).length;\n } catch (e) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n alphabet,\n base: 64,\n value,\n });\n }\n },\n write(value: string, bytes, offset) {\n try {\n const bytesToAdd = (atob as Window['atob'])(value)\n .split('')\n .map(c => c.charCodeAt(0));\n bytes.set(bytesToAdd, offset);\n return bytesToAdd.length + offset;\n } catch (e) {\n throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {\n alphabet,\n base: 64,\n value,\n });\n }\n },\n });\n }\n\n if (__NODEJS__) {\n return createEncoder({\n getSizeFromValue: (value: string) => Buffer.from(value, 'base64').length,\n write(value: string, bytes, offset) {\n assertValidBaseString(alphabet, value.replace(/=/g, ''));\n const buffer = Buffer.from(value, 'base64');\n bytes.set(buffer, offset);\n return buffer.length + offset;\n },\n });\n }\n\n return transformEncoder(getBaseXResliceEncoder(alphabet, 6), (value: string): string => value.replace(/=/g, ''));\n};\n\n/** Decodes strings in base64. */\nexport const getBase64Decoder = (): VariableSizeDecoder<string> => {\n if (__BROWSER__) {\n return createDecoder({\n read(bytes, offset = 0) {\n const slice = bytes.slice(offset);\n const value = (btoa as Window['btoa'])(String.fromCharCode(...slice));\n return [value, bytes.length];\n },\n });\n }\n\n if (__NODEJS__) {\n return createDecoder({\n read: (bytes, offset = 0) => [Buffer.from(bytes, offset).toString('base64'), bytes.length],\n });\n }\n\n return transformDecoder(getBaseXResliceDecoder(alphabet, 6), (value: string): string =>\n value.padEnd(Math.ceil(value.length / 4) * 4, '='),\n );\n};\n\n/** Encodes and decodes strings in base64. */\nexport const getBase64Codec = (): VariableSizeCodec<string> => combineCodec(getBase64Encoder(), getBase64Decoder());\n","/**Removes null characters from a string. */\nexport const removeNullCharacters = (value: string) =>\n // eslint-disable-next-line no-control-regex\n value.replace(/\\u0000/g, '');\n\n/** Pads a string with null characters at the end. */\nexport const padNullCharacters = (value: string, chars: number) => value.padEnd(chars, '\\u0000');\n","export const TextDecoder = globalThis.TextDecoder;\nexport const TextEncoder = globalThis.TextEncoder;\n","import {\n combineCodec,\n createDecoder,\n createEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\nimport { TextDecoder, TextEncoder } from '@solana/text-encoding-impl';\n\nimport { removeNullCharacters } from './null-characters';\n\n/** Encodes UTF-8 strings using the native `TextEncoder` API. */\nexport const getUtf8Encoder = (): VariableSizeEncoder<string> => {\n let textEncoder: TextEncoder;\n return createEncoder({\n getSizeFromValue: value => (textEncoder ||= new TextEncoder()).encode(value).length,\n write: (value: string, bytes, offset) => {\n const bytesToAdd = (textEncoder ||= new TextEncoder()).encode(value);\n bytes.set(bytesToAdd, offset);\n return offset + bytesToAdd.length;\n },\n });\n};\n\n/** Decodes UTF-8 strings using the native `TextDecoder` API. */\nexport const getUtf8Decoder = (): VariableSizeDecoder<string> => {\n let textDecoder: TextDecoder;\n return createDecoder({\n read(bytes, offset) {\n const value = (textDecoder ||= new TextDecoder()).decode(bytes.slice(offset));\n return [removeNullCharacters(value), bytes.length];\n },\n });\n};\n\n/** Encodes and decodes UTF-8 strings using the native `TextEncoder` and `TextDecoder` API. */\nexport const getUtf8Codec = (): VariableSizeCodec<string> => combineCodec(getUtf8Encoder(), getUtf8Decoder());\n"]}
|