@solana/addresses 2.0.0-experimental.433f475 → 2.0.0-experimental.445121c

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 ?? 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 ?? `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,53 +112,37 @@ this.globalThis.solanaWeb3 = (function (exports) {
109
112
  );
110
113
  }
111
114
  }
112
- function sharedNumberFactory(input) {
113
- let littleEndian;
114
- let defaultDescription = input.name;
115
- if (input.size > 1) {
116
- littleEndian = !("endian" in input.config) || input.config.endian === 0;
117
- defaultDescription += littleEndian ? "(le)" : "(be)";
118
- }
119
- return {
120
- description: input.config.description ?? defaultDescription,
121
- fixedSize: input.size,
122
- littleEndian,
123
- maxSize: input.size
124
- };
115
+ function isLittleEndian(config) {
116
+ return (config == null ? void 0 : config.endian) === 1 ? false : true;
125
117
  }
126
118
  function numberEncoderFactory(input) {
127
- const codecData = sharedNumberFactory(input);
128
- return {
129
- description: codecData.description,
130
- encode(value) {
119
+ return createEncoder({
120
+ fixedSize: input.size,
121
+ write(value, bytes, offset) {
131
122
  if (input.range) {
132
123
  assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
133
124
  }
134
125
  const arrayBuffer = new ArrayBuffer(input.size);
135
- input.set(new DataView(arrayBuffer), value, codecData.littleEndian);
136
- return new Uint8Array(arrayBuffer);
137
- },
138
- fixedSize: codecData.fixedSize,
139
- maxSize: codecData.maxSize
140
- };
126
+ input.set(new DataView(arrayBuffer), value, isLittleEndian(input.config));
127
+ bytes.set(new Uint8Array(arrayBuffer), offset);
128
+ return offset + input.size;
129
+ }
130
+ });
141
131
  }
142
132
  function numberDecoderFactory(input) {
143
- const codecData = sharedNumberFactory(input);
144
- return {
145
- decode(bytes, offset = 0) {
146
- assertByteArrayIsNotEmptyForCodec(codecData.description, bytes, offset);
147
- 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);
148
138
  const view = new DataView(toArrayBuffer(bytes, offset, input.size));
149
- return [input.get(view, codecData.littleEndian), offset + input.size];
150
- },
151
- description: codecData.description,
152
- fixedSize: codecData.fixedSize,
153
- maxSize: codecData.maxSize
154
- };
139
+ return [input.get(view, isLittleEndian(input.config)), offset + input.size];
140
+ }
141
+ });
155
142
  }
156
143
  function toArrayBuffer(bytes, offset, length) {
157
- const bytesOffset = bytes.byteOffset + (offset ?? 0);
158
- const bytesLength = length ?? bytes.byteLength;
144
+ const bytesOffset = bytes.byteOffset + (offset != null ? offset : 0);
145
+ const bytesLength = length != null ? length : bytes.byteLength;
159
146
  return bytes.buffer.slice(bytesOffset, bytesOffset + bytesLength);
160
147
  }
161
148
  var getU32Encoder = (config = {}) => numberEncoderFactory({
@@ -179,43 +166,38 @@ this.globalThis.solanaWeb3 = (function (exports) {
179
166
  }
180
167
  }
181
168
  var getBaseXEncoder = (alphabet4) => {
182
- const base = alphabet4.length;
183
- const baseBigInt = BigInt(base);
184
- return {
185
- description: `base${base}`,
186
- 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) {
187
178
  assertValidBaseString(alphabet4, value);
188
179
  if (value === "")
189
- return new Uint8Array();
190
- const chars = [...value];
191
- let trailIndex = chars.findIndex((c) => c !== alphabet4[0]);
192
- trailIndex = trailIndex === -1 ? chars.length : trailIndex;
193
- const leadingZeroes = Array(trailIndex).fill(0);
194
- if (trailIndex === chars.length)
195
- return Uint8Array.from(leadingZeroes);
196
- const tailChars = chars.slice(trailIndex);
197
- let base10Number = 0n;
198
- let baseXPower = 1n;
199
- for (let i = tailChars.length - 1; i >= 0; i -= 1) {
200
- base10Number += baseXPower * BigInt(alphabet4.indexOf(tailChars[i]));
201
- 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;
202
185
  }
186
+ let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
203
187
  const tailBytes = [];
204
188
  while (base10Number > 0n) {
205
189
  tailBytes.unshift(Number(base10Number % 256n));
206
190
  base10Number /= 256n;
207
191
  }
208
- return Uint8Array.from(leadingZeroes.concat(tailBytes));
209
- },
210
- fixedSize: null,
211
- maxSize: null
212
- };
192
+ const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
193
+ bytes.set(bytesToAdd, offset);
194
+ return offset + bytesToAdd.length;
195
+ }
196
+ });
213
197
  };
214
198
  var getBaseXDecoder = (alphabet4) => {
215
- const base = alphabet4.length;
216
- const baseBigInt = BigInt(base);
217
- return {
218
- decode(rawBytes, offset = 0) {
199
+ return createDecoder({
200
+ read(rawBytes, offset) {
219
201
  const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
220
202
  if (bytes.length === 0)
221
203
  return ["", 0];
@@ -224,19 +206,29 @@ this.globalThis.solanaWeb3 = (function (exports) {
224
206
  const leadingZeroes = alphabet4[0].repeat(trailIndex);
225
207
  if (trailIndex === bytes.length)
226
208
  return [leadingZeroes, rawBytes.length];
227
- let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
228
- const tailChars = [];
229
- while (base10Number > 0n) {
230
- tailChars.unshift(alphabet4[Number(base10Number % baseBigInt)]);
231
- base10Number /= baseBigInt;
232
- }
233
- return [leadingZeroes + tailChars.join(""), rawBytes.length];
234
- },
235
- description: `base${base}`,
236
- fixedSize: null,
237
- maxSize: null
238
- };
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
+ });
239
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
+ }
240
232
  var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
241
233
  var getBase58Encoder = () => getBaseXEncoder(alphabet2);
242
234
  var getBase58Decoder = () => getBaseXDecoder(alphabet2);
@@ -248,75 +240,69 @@ this.globalThis.solanaWeb3 = (function (exports) {
248
240
  var o = globalThis.TextEncoder;
249
241
  var getUtf8Encoder = () => {
250
242
  let textEncoder;
251
- return {
252
- description: "utf8",
253
- encode: (value) => new Uint8Array((textEncoder || (textEncoder = new o())).encode(value)),
254
- fixedSize: null,
255
- maxSize: null
256
- };
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
+ });
257
251
  };
258
252
  var getUtf8Decoder = () => {
259
253
  let textDecoder;
260
- return {
261
- decode(bytes, offset = 0) {
254
+ return createDecoder({
255
+ read(bytes, offset) {
262
256
  const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
263
257
  return [removeNullCharacters(value), bytes.length];
264
- },
265
- description: "utf8",
266
- fixedSize: null,
267
- maxSize: null
268
- };
258
+ }
259
+ });
269
260
  };
270
- var getStringEncoder = (config = {}) => {
271
- const size = config.size ?? getU32Encoder();
272
- const encoding = config.encoding ?? getUtf8Encoder();
273
- const description = config.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
261
+ function getStringEncoder(config = {}) {
262
+ var _a, _b;
263
+ const size = (_a = config.size) != null ? _a : getU32Encoder();
264
+ const encoding = (_b = config.encoding) != null ? _b : getUtf8Encoder();
274
265
  if (size === "variable") {
275
- return { ...encoding, description };
266
+ return encoding;
276
267
  }
277
268
  if (typeof size === "number") {
278
- return fixEncoder(encoding, size, description);
269
+ return fixEncoder(encoding, size);
279
270
  }
280
- return {
281
- description,
282
- encode: (value) => {
283
- const contentBytes = encoding.encode(value);
284
- const lengthBytes = size.encode(contentBytes.length);
285
- return mergeBytes([lengthBytes, contentBytes]);
271
+ return createEncoder({
272
+ getSizeFromValue: (value) => {
273
+ const contentSize = getEncodedSize(value, encoding);
274
+ return getEncodedSize(contentSize, size) + contentSize;
286
275
  },
287
- fixedSize: null,
288
- maxSize: null
289
- };
290
- };
291
- var getStringDecoder = (config = {}) => {
292
- const size = config.size ?? getU32Decoder();
293
- const encoding = config.encoding ?? getUtf8Decoder();
294
- const description = config.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
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;
285
+ const size = (_a = config.size) != null ? _a : getU32Decoder();
286
+ const encoding = (_b = config.encoding) != null ? _b : getUtf8Decoder();
295
287
  if (size === "variable") {
296
- return { ...encoding, description };
288
+ return encoding;
297
289
  }
298
290
  if (typeof size === "number") {
299
- return fixDecoder(encoding, size, description);
291
+ return fixDecoder(encoding, size);
300
292
  }
301
- return {
302
- decode: (bytes, offset = 0) => {
293
+ return createDecoder({
294
+ read: (bytes, offset = 0) => {
303
295
  assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
304
- const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
296
+ const [lengthBigInt, lengthOffset] = size.read(bytes, offset);
305
297
  const length = Number(lengthBigInt);
306
298
  offset = lengthOffset;
307
299
  const contentBytes = bytes.slice(offset, offset + length);
308
300
  assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
309
- const [value, contentOffset] = encoding.decode(contentBytes);
301
+ const [value, contentOffset] = encoding.read(contentBytes, 0);
310
302
  offset += contentOffset;
311
303
  return [value, offset];
312
- },
313
- description,
314
- fixedSize: null,
315
- maxSize: null
316
- };
317
- };
318
- function getSizeDescription(size) {
319
- return typeof size === "object" ? size.description : `${size}`;
304
+ }
305
+ });
320
306
  }
321
307
 
322
308
  // src/address.ts
@@ -373,25 +359,17 @@ this.globalThis.solanaWeb3 = (function (exports) {
373
359
  assertIsAddress(putativeAddress);
374
360
  return putativeAddress;
375
361
  }
376
- function getAddressEncoder(config) {
362
+ function getAddressEncoder() {
377
363
  return mapEncoder(
378
- getStringEncoder({
379
- description: config?.description ?? "Address",
380
- encoding: getMemoizedBase58Encoder(),
381
- size: 32
382
- }),
364
+ getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }),
383
365
  (putativeAddress) => address(putativeAddress)
384
366
  );
385
367
  }
386
- function getAddressDecoder(config) {
387
- return getStringDecoder({
388
- description: config?.description ?? "Address",
389
- encoding: getMemoizedBase58Decoder(),
390
- size: 32
391
- });
368
+ function getAddressDecoder() {
369
+ return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 });
392
370
  }
393
- function getAddressCodec(config) {
394
- return combineCodec(getAddressEncoder(config), getAddressDecoder(config));
371
+ function getAddressCodec() {
372
+ return combineCodec(getAddressEncoder(), getAddressDecoder());
395
373
  }
396
374
  function getAddressComparator() {
397
375
  return new Intl.Collator("en", {
@@ -413,14 +391,16 @@ this.globalThis.solanaWeb3 = (function (exports) {
413
391
  }
414
392
  }
415
393
  async function assertDigestCapabilityIsAvailable() {
394
+ var _a;
416
395
  assertIsSecureContext();
417
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.digest !== "function") {
396
+ if (typeof globalThis.crypto === "undefined" || typeof ((_a = globalThis.crypto.subtle) == null ? void 0 : _a.digest) !== "function") {
418
397
  throw new Error("No digest implementation could be found");
419
398
  }
420
399
  }
421
400
  async function assertKeyExporterIsAvailable() {
401
+ var _a;
422
402
  assertIsSecureContext();
423
- if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
403
+ if (typeof globalThis.crypto === "undefined" || typeof ((_a = globalThis.crypto.subtle) == null ? void 0 : _a.exportKey) !== "function") {
424
404
  throw new Error("No key export implementation could be found");
425
405
  }
426
406
  }
@@ -583,7 +563,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
583
563
  if (await compressedPointBytesAreOnCurve(addressBytes)) {
584
564
  throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
585
565
  }
586
- return base58EncodedAddressCodec.decode(addressBytes)[0];
566
+ return base58EncodedAddressCodec.decode(addressBytes);
587
567
  }
588
568
  async function getProgramDerivedAddress({
589
569
  programAddress,
@@ -622,7 +602,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
622
602
  new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
623
603
  );
624
604
  const addressBytes = new Uint8Array(addressBytesBuffer);
625
- return decode(addressBytes)[0];
605
+ return decode(addressBytes);
626
606
  }
627
607
 
628
608
  // src/public-key.ts
@@ -632,8 +612,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
632
612
  throw new Error("The `CryptoKey` must be an `Ed25519` public key");
633
613
  }
634
614
  const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
635
- const [base58EncodedAddress] = getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
636
- return base58EncodedAddress;
615
+ return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
637
616
  }
638
617
 
639
618
  exports.address = address;