@solana/addresses 2.0.0-experimental.0eb69ae → 2.0.0-experimental.0f3404a

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.
@@ -14,23 +14,6 @@ this.globalThis.solanaWeb3 = (function (exports) {
14
14
  throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
15
15
  }
16
16
  }
17
- var mergeBytes = (byteArrays) => {
18
- const nonEmptyByteArrays = byteArrays.filter((arr) => arr.length);
19
- if (nonEmptyByteArrays.length === 0) {
20
- return byteArrays.length ? byteArrays[0] : new Uint8Array();
21
- }
22
- if (nonEmptyByteArrays.length === 1) {
23
- return nonEmptyByteArrays[0];
24
- }
25
- const totalLength = nonEmptyByteArrays.reduce((total, arr) => total + arr.length, 0);
26
- const result = new Uint8Array(totalLength);
27
- let offset = 0;
28
- nonEmptyByteArrays.forEach((arr) => {
29
- result.set(arr, offset);
30
- offset += arr.length;
31
- });
32
- return result;
33
- };
34
17
  var padBytes = (bytes, length) => {
35
18
  if (bytes.length >= length)
36
19
  return bytes;
@@ -39,66 +22,86 @@ this.globalThis.solanaWeb3 = (function (exports) {
39
22
  return paddedBytes;
40
23
  };
41
24
  var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
42
- function combineCodec(encoder, decoder, description) {
43
- if (encoder.fixedSize !== decoder.fixedSize) {
25
+ function getEncodedSize(value, encoder) {
26
+ return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value);
27
+ }
28
+ function createEncoder(encoder) {
29
+ return Object.freeze({
30
+ ...encoder,
31
+ encode: (value) => {
32
+ const bytes = new Uint8Array(getEncodedSize(value, encoder));
33
+ encoder.write(value, bytes, 0);
34
+ return bytes;
35
+ }
36
+ });
37
+ }
38
+ function createDecoder(decoder) {
39
+ return Object.freeze({
40
+ ...decoder,
41
+ decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0]
42
+ });
43
+ }
44
+ function isFixedSize(codec) {
45
+ return "fixedSize" in codec && typeof codec.fixedSize === "number";
46
+ }
47
+ function isVariableSize(codec) {
48
+ return !isFixedSize(codec);
49
+ }
50
+ function combineCodec(encoder, decoder) {
51
+ if (isFixedSize(encoder) !== isFixedSize(decoder)) {
52
+ throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`);
53
+ }
54
+ if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) {
44
55
  throw new Error(
45
56
  `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
46
57
  );
47
58
  }
48
- if (encoder.maxSize !== decoder.maxSize) {
59
+ if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) {
49
60
  throw new Error(
50
61
  `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
51
62
  );
52
63
  }
53
- if (description === void 0 && encoder.description !== decoder.description) {
54
- throw new Error(
55
- `Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.`
56
- );
57
- }
58
64
  return {
65
+ ...decoder,
66
+ ...encoder,
59
67
  decode: decoder.decode,
60
- description: description != null ? description : encoder.description,
61
68
  encode: encoder.encode,
62
- fixedSize: encoder.fixedSize,
63
- maxSize: encoder.maxSize
69
+ read: decoder.read,
70
+ write: encoder.write
64
71
  };
65
72
  }
66
- function fixCodecHelper(data, fixedBytes, description) {
67
- return {
68
- description: description != null ? description : `fixed(${fixedBytes}, ${data.description})`,
73
+ function fixEncoder(encoder, fixedBytes) {
74
+ return createEncoder({
69
75
  fixedSize: fixedBytes,
70
- maxSize: fixedBytes
71
- };
72
- }
73
- function fixEncoder(encoder, fixedBytes, description) {
74
- return {
75
- ...fixCodecHelper(encoder, fixedBytes, description),
76
- encode: (value) => fixBytes(encoder.encode(value), fixedBytes)
77
- };
76
+ write: (value, bytes, offset) => {
77
+ const variableByteArray = encoder.encode(value);
78
+ const fixedByteArray = variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray;
79
+ bytes.set(fixedByteArray, offset);
80
+ return offset + fixedBytes;
81
+ }
82
+ });
78
83
  }
79
- function fixDecoder(decoder, fixedBytes, description) {
80
- return {
81
- ...fixCodecHelper(decoder, fixedBytes, description),
82
- decode: (bytes, offset = 0) => {
84
+ function fixDecoder(decoder, fixedBytes) {
85
+ return createDecoder({
86
+ fixedSize: fixedBytes,
87
+ read: (bytes, offset) => {
83
88
  assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
84
89
  if (offset > 0 || bytes.length > fixedBytes) {
85
90
  bytes = bytes.slice(offset, offset + fixedBytes);
86
91
  }
87
- if (decoder.fixedSize !== null) {
92
+ if (isFixedSize(decoder)) {
88
93
  bytes = fixBytes(bytes, decoder.fixedSize);
89
94
  }
90
- const [value] = decoder.decode(bytes, 0);
95
+ const [value] = decoder.read(bytes, 0);
91
96
  return [value, offset + fixedBytes];
92
97
  }
93
- };
98
+ });
94
99
  }
95
100
  function mapEncoder(encoder, unmap) {
96
- return {
97
- description: encoder.description,
98
- encode: (value) => encoder.encode(unmap(value)),
99
- fixedSize: encoder.fixedSize,
100
- maxSize: encoder.maxSize
101
- };
101
+ return createEncoder({
102
+ ...isVariableSize(encoder) ? { ...encoder, getSizeFromValue: (value) => encoder.getSizeFromValue(unmap(value)) } : encoder,
103
+ write: (value, bytes, offset) => encoder.write(unmap(value), bytes, offset)
104
+ });
102
105
  }
103
106
 
104
107
  // ../codecs-numbers/dist/index.browser.js
@@ -109,50 +112,33 @@ this.globalThis.solanaWeb3 = (function (exports) {
109
112
  );
110
113
  }
111
114
  }
112
- function sharedNumberFactory(input) {
113
- var _a;
114
- let littleEndian;
115
- let defaultDescription = input.name;
116
- if (input.size > 1) {
117
- littleEndian = !("endian" in input.config) || input.config.endian === 0;
118
- defaultDescription += littleEndian ? "(le)" : "(be)";
119
- }
120
- return {
121
- description: (_a = input.config.description) != null ? _a : defaultDescription,
122
- fixedSize: input.size,
123
- littleEndian,
124
- maxSize: input.size
125
- };
115
+ function isLittleEndian(config) {
116
+ return (config == null ? void 0 : config.endian) === 1 ? false : true;
126
117
  }
127
118
  function numberEncoderFactory(input) {
128
- const codecData = sharedNumberFactory(input);
129
- return {
130
- description: codecData.description,
131
- encode(value) {
119
+ return createEncoder({
120
+ fixedSize: input.size,
121
+ write(value, bytes, offset) {
132
122
  if (input.range) {
133
123
  assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
134
124
  }
135
125
  const arrayBuffer = new ArrayBuffer(input.size);
136
- input.set(new DataView(arrayBuffer), value, codecData.littleEndian);
137
- return new Uint8Array(arrayBuffer);
138
- },
139
- fixedSize: codecData.fixedSize,
140
- maxSize: codecData.maxSize
141
- };
126
+ input.set(new DataView(arrayBuffer), value, isLittleEndian(input.config));
127
+ bytes.set(new Uint8Array(arrayBuffer), offset);
128
+ return offset + input.size;
129
+ }
130
+ });
142
131
  }
143
132
  function numberDecoderFactory(input) {
144
- const codecData = sharedNumberFactory(input);
145
- return {
146
- decode(bytes, offset = 0) {
147
- assertByteArrayIsNotEmptyForCodec(codecData.description, bytes, offset);
148
- assertByteArrayHasEnoughBytesForCodec(codecData.description, input.size, bytes, offset);
133
+ return createDecoder({
134
+ fixedSize: input.size,
135
+ read(bytes, offset = 0) {
136
+ assertByteArrayIsNotEmptyForCodec(input.name, bytes, offset);
137
+ assertByteArrayHasEnoughBytesForCodec(input.name, input.size, bytes, offset);
149
138
  const view = new DataView(toArrayBuffer(bytes, offset, input.size));
150
- return [input.get(view, codecData.littleEndian), offset + input.size];
151
- },
152
- description: codecData.description,
153
- fixedSize: codecData.fixedSize,
154
- maxSize: codecData.maxSize
155
- };
139
+ return [input.get(view, isLittleEndian(input.config)), offset + input.size];
140
+ }
141
+ });
156
142
  }
157
143
  function toArrayBuffer(bytes, offset, length) {
158
144
  const bytesOffset = bytes.byteOffset + (offset != null ? offset : 0);
@@ -180,43 +166,38 @@ this.globalThis.solanaWeb3 = (function (exports) {
180
166
  }
181
167
  }
182
168
  var getBaseXEncoder = (alphabet4) => {
183
- const base = alphabet4.length;
184
- const baseBigInt = BigInt(base);
185
- return {
186
- description: `base${base}`,
187
- encode(value) {
169
+ return createEncoder({
170
+ getSizeFromValue: (value) => {
171
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
172
+ if (tailChars === "")
173
+ return value.length;
174
+ const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
175
+ return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
176
+ },
177
+ write(value, bytes, offset) {
188
178
  assertValidBaseString(alphabet4, value);
189
179
  if (value === "")
190
- return new Uint8Array();
191
- const chars = [...value];
192
- let trailIndex = chars.findIndex((c) => c !== alphabet4[0]);
193
- trailIndex = trailIndex === -1 ? chars.length : trailIndex;
194
- const leadingZeroes = Array(trailIndex).fill(0);
195
- if (trailIndex === chars.length)
196
- return Uint8Array.from(leadingZeroes);
197
- const tailChars = chars.slice(trailIndex);
198
- let base10Number = 0n;
199
- let baseXPower = 1n;
200
- for (let i = tailChars.length - 1; i >= 0; i -= 1) {
201
- base10Number += baseXPower * BigInt(alphabet4.indexOf(tailChars[i]));
202
- baseXPower *= baseBigInt;
180
+ return offset;
181
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
182
+ if (tailChars === "") {
183
+ bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
184
+ return offset + leadingZeroes.length;
203
185
  }
186
+ let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
204
187
  const tailBytes = [];
205
188
  while (base10Number > 0n) {
206
189
  tailBytes.unshift(Number(base10Number % 256n));
207
190
  base10Number /= 256n;
208
191
  }
209
- return Uint8Array.from(leadingZeroes.concat(tailBytes));
210
- },
211
- fixedSize: null,
212
- maxSize: null
213
- };
192
+ const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
193
+ bytes.set(bytesToAdd, offset);
194
+ return offset + bytesToAdd.length;
195
+ }
196
+ });
214
197
  };
215
198
  var getBaseXDecoder = (alphabet4) => {
216
- const base = alphabet4.length;
217
- const baseBigInt = BigInt(base);
218
- return {
219
- decode(rawBytes, offset = 0) {
199
+ return createDecoder({
200
+ read(rawBytes, offset) {
220
201
  const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
221
202
  if (bytes.length === 0)
222
203
  return ["", 0];
@@ -225,19 +206,29 @@ this.globalThis.solanaWeb3 = (function (exports) {
225
206
  const leadingZeroes = alphabet4[0].repeat(trailIndex);
226
207
  if (trailIndex === bytes.length)
227
208
  return [leadingZeroes, rawBytes.length];
228
- let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
229
- const tailChars = [];
230
- while (base10Number > 0n) {
231
- tailChars.unshift(alphabet4[Number(base10Number % baseBigInt)]);
232
- base10Number /= baseBigInt;
233
- }
234
- return [leadingZeroes + tailChars.join(""), rawBytes.length];
235
- },
236
- description: `base${base}`,
237
- fixedSize: null,
238
- maxSize: null
239
- };
209
+ const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
210
+ const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
211
+ return [leadingZeroes + tailChars, rawBytes.length];
212
+ }
213
+ });
240
214
  };
215
+ function partitionLeadingZeroes(value, zeroCharacter) {
216
+ const leadingZeroIndex = [...value].findIndex((c) => c !== zeroCharacter);
217
+ return leadingZeroIndex === -1 ? [value, ""] : [value.slice(0, leadingZeroIndex), value.slice(leadingZeroIndex)];
218
+ }
219
+ function getBigIntFromBaseX(value, alphabet4) {
220
+ const base = BigInt(alphabet4.length);
221
+ return [...value].reduce((sum, char) => sum * base + BigInt(alphabet4.indexOf(char)), 0n);
222
+ }
223
+ function getBaseXFromBigInt(value, alphabet4) {
224
+ const base = BigInt(alphabet4.length);
225
+ const tailChars = [];
226
+ while (value > 0n) {
227
+ tailChars.unshift(alphabet4[Number(value % base)]);
228
+ value /= base;
229
+ }
230
+ return tailChars.join("");
231
+ }
241
232
  var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
242
233
  var getBase58Encoder = () => getBaseXEncoder(alphabet2);
243
234
  var getBase58Decoder = () => getBaseXDecoder(alphabet2);
@@ -249,77 +240,69 @@ this.globalThis.solanaWeb3 = (function (exports) {
249
240
  var o = globalThis.TextEncoder;
250
241
  var getUtf8Encoder = () => {
251
242
  let textEncoder;
252
- return {
253
- description: "utf8",
254
- encode: (value) => new Uint8Array((textEncoder || (textEncoder = new o())).encode(value)),
255
- fixedSize: null,
256
- maxSize: null
257
- };
243
+ return createEncoder({
244
+ getSizeFromValue: (value) => (textEncoder || (textEncoder = new o())).encode(value).length,
245
+ write: (value, bytes, offset) => {
246
+ const bytesToAdd = (textEncoder || (textEncoder = new o())).encode(value);
247
+ bytes.set(bytesToAdd, offset);
248
+ return offset + bytesToAdd.length;
249
+ }
250
+ });
258
251
  };
259
252
  var getUtf8Decoder = () => {
260
253
  let textDecoder;
261
- return {
262
- decode(bytes, offset = 0) {
254
+ return createDecoder({
255
+ read(bytes, offset) {
263
256
  const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
264
257
  return [removeNullCharacters(value), bytes.length];
265
- },
266
- description: "utf8",
267
- fixedSize: null,
268
- maxSize: null
269
- };
258
+ }
259
+ });
270
260
  };
271
- var getStringEncoder = (config = {}) => {
272
- var _a, _b, _c;
261
+ function getStringEncoder(config = {}) {
262
+ var _a, _b;
273
263
  const size = (_a = config.size) != null ? _a : getU32Encoder();
274
264
  const encoding = (_b = config.encoding) != null ? _b : getUtf8Encoder();
275
- const description = (_c = config.description) != null ? _c : `string(${encoding.description}; ${getSizeDescription(size)})`;
276
265
  if (size === "variable") {
277
- return { ...encoding, description };
266
+ return encoding;
278
267
  }
279
268
  if (typeof size === "number") {
280
- return fixEncoder(encoding, size, description);
269
+ return fixEncoder(encoding, size);
281
270
  }
282
- return {
283
- description,
284
- encode: (value) => {
285
- const contentBytes = encoding.encode(value);
286
- const lengthBytes = size.encode(contentBytes.length);
287
- return mergeBytes([lengthBytes, contentBytes]);
271
+ return createEncoder({
272
+ getSizeFromValue: (value) => {
273
+ const contentSize = getEncodedSize(value, encoding);
274
+ return getEncodedSize(contentSize, size) + contentSize;
288
275
  },
289
- fixedSize: null,
290
- maxSize: null
291
- };
292
- };
293
- var getStringDecoder = (config = {}) => {
294
- var _a, _b, _c;
276
+ write: (value, bytes, offset) => {
277
+ const contentSize = getEncodedSize(value, encoding);
278
+ offset = size.write(contentSize, bytes, offset);
279
+ return encoding.write(value, bytes, offset);
280
+ }
281
+ });
282
+ }
283
+ function getStringDecoder(config = {}) {
284
+ var _a, _b;
295
285
  const size = (_a = config.size) != null ? _a : getU32Decoder();
296
286
  const encoding = (_b = config.encoding) != null ? _b : getUtf8Decoder();
297
- const description = (_c = config.description) != null ? _c : `string(${encoding.description}; ${getSizeDescription(size)})`;
298
287
  if (size === "variable") {
299
- return { ...encoding, description };
288
+ return encoding;
300
289
  }
301
290
  if (typeof size === "number") {
302
- return fixDecoder(encoding, size, description);
291
+ return fixDecoder(encoding, size);
303
292
  }
304
- return {
305
- decode: (bytes, offset = 0) => {
293
+ return createDecoder({
294
+ read: (bytes, offset = 0) => {
306
295
  assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
307
- const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
296
+ const [lengthBigInt, lengthOffset] = size.read(bytes, offset);
308
297
  const length = Number(lengthBigInt);
309
298
  offset = lengthOffset;
310
299
  const contentBytes = bytes.slice(offset, offset + length);
311
300
  assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
312
- const [value, contentOffset] = encoding.decode(contentBytes);
301
+ const [value, contentOffset] = encoding.read(contentBytes, 0);
313
302
  offset += contentOffset;
314
303
  return [value, offset];
315
- },
316
- description,
317
- fixedSize: null,
318
- maxSize: null
319
- };
320
- };
321
- function getSizeDescription(size) {
322
- return typeof size === "object" ? size.description : `${size}`;
304
+ }
305
+ });
323
306
  }
324
307
 
325
308
  // src/address.ts
@@ -376,27 +359,17 @@ this.globalThis.solanaWeb3 = (function (exports) {
376
359
  assertIsAddress(putativeAddress);
377
360
  return putativeAddress;
378
361
  }
379
- function getAddressEncoder(config) {
380
- var _a;
362
+ function getAddressEncoder() {
381
363
  return mapEncoder(
382
- getStringEncoder({
383
- description: (_a = config == null ? void 0 : config.description) != null ? _a : "Address",
384
- encoding: getMemoizedBase58Encoder(),
385
- size: 32
386
- }),
364
+ getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }),
387
365
  (putativeAddress) => address(putativeAddress)
388
366
  );
389
367
  }
390
- function getAddressDecoder(config) {
391
- var _a;
392
- return getStringDecoder({
393
- description: (_a = config == null ? void 0 : config.description) != null ? _a : "Address",
394
- encoding: getMemoizedBase58Decoder(),
395
- size: 32
396
- });
368
+ function getAddressDecoder() {
369
+ return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 });
397
370
  }
398
- function getAddressCodec(config) {
399
- return combineCodec(getAddressEncoder(config), getAddressDecoder(config));
371
+ function getAddressCodec() {
372
+ return combineCodec(getAddressEncoder(), getAddressDecoder());
400
373
  }
401
374
  function getAddressComparator() {
402
375
  return new Intl.Collator("en", {
@@ -590,7 +563,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
590
563
  if (await compressedPointBytesAreOnCurve(addressBytes)) {
591
564
  throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
592
565
  }
593
- return base58EncodedAddressCodec.decode(addressBytes)[0];
566
+ return base58EncodedAddressCodec.decode(addressBytes);
594
567
  }
595
568
  async function getProgramDerivedAddress({
596
569
  programAddress,
@@ -629,7 +602,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
629
602
  new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
630
603
  );
631
604
  const addressBytes = new Uint8Array(addressBytesBuffer);
632
- return decode(addressBytes)[0];
605
+ return decode(addressBytes);
633
606
  }
634
607
 
635
608
  // src/public-key.ts
@@ -639,8 +612,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
639
612
  throw new Error("The `CryptoKey` must be an `Ed25519` public key");
640
613
  }
641
614
  const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
642
- const [base58EncodedAddress] = getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
643
- return base58EncodedAddress;
615
+ return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
644
616
  }
645
617
 
646
618
  exports.address = address;