@solana/addresses 2.0.0-experimental.fcae73c → 2.0.0-experimental.fcff844

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.
@@ -2,24 +2,18 @@ this.globalThis = this.globalThis || {};
2
2
  this.globalThis.solanaWeb3 = (function (exports) {
3
3
  'use strict';
4
4
 
5
- var __defProp = Object.defineProperty;
6
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
- var __publicField = (obj, key, value) => {
8
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
9
- return value;
10
- };
11
-
12
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-core@0.8.2/node_modules/@metaplex-foundation/umi-serializers-core/dist/esm/bytes.mjs
13
- var mergeBytes = (bytesArr) => {
14
- const totalLength = bytesArr.reduce((total, arr) => total + arr.length, 0);
15
- const result = new Uint8Array(totalLength);
16
- let offset = 0;
17
- bytesArr.forEach((arr) => {
18
- result.set(arr, offset);
19
- offset += arr.length;
20
- });
21
- return result;
22
- };
5
+ // ../codecs-core/dist/index.browser.js
6
+ function assertByteArrayIsNotEmptyForCodec(codecDescription, bytes, offset = 0) {
7
+ if (bytes.length - offset <= 0) {
8
+ throw new Error(`Codec [${codecDescription}] cannot decode empty byte arrays.`);
9
+ }
10
+ }
11
+ function assertByteArrayHasEnoughBytesForCodec(codecDescription, expected, bytes, offset = 0) {
12
+ const bytesLength = bytes.length - offset;
13
+ if (bytesLength < expected) {
14
+ throw new Error(`Codec [${codecDescription}] expected ${expected} bytes, got ${bytesLength}.`);
15
+ }
16
+ }
23
17
  var padBytes = (bytes, length) => {
24
18
  if (bytes.length >= length)
25
19
  return bytes;
@@ -27,274 +21,357 @@ this.globalThis.solanaWeb3 = (function (exports) {
27
21
  paddedBytes.set(bytes);
28
22
  return paddedBytes;
29
23
  };
30
- var fixBytes = (bytes, length) => padBytes(bytes.slice(0, length), length);
31
-
32
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-core@0.8.2/node_modules/@metaplex-foundation/umi-serializers-core/dist/esm/errors.mjs
33
- var DeserializingEmptyBufferError = class extends Error {
34
- constructor(serializer) {
35
- super(`Serializer [${serializer}] cannot deserialize empty buffers.`);
36
- __publicField(this, "name", "DeserializingEmptyBufferError");
24
+ var fixBytes = (bytes, length) => padBytes(bytes.length <= length ? bytes : bytes.slice(0, length), length);
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.`);
37
53
  }
38
- };
39
- var NotEnoughBytesError = class extends Error {
40
- constructor(serializer, expected, actual) {
41
- super(`Serializer [${serializer}] expected ${expected} bytes, got ${actual}.`);
42
- __publicField(this, "name", "NotEnoughBytesError");
54
+ if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) {
55
+ throw new Error(
56
+ `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
57
+ );
58
+ }
59
+ if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) {
60
+ throw new Error(
61
+ `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
62
+ );
43
63
  }
44
- };
45
-
46
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-core@0.8.2/node_modules/@metaplex-foundation/umi-serializers-core/dist/esm/fixSerializer.mjs
47
- function fixSerializer(serializer, fixedBytes, description) {
48
64
  return {
49
- description: description ?? `fixed(${fixedBytes}, ${serializer.description})`,
65
+ ...decoder,
66
+ ...encoder,
67
+ decode: decoder.decode,
68
+ encode: encoder.encode,
69
+ read: decoder.read,
70
+ write: encoder.write
71
+ };
72
+ }
73
+ function fixEncoder(encoder, fixedBytes) {
74
+ return createEncoder({
50
75
  fixedSize: fixedBytes,
51
- maxSize: fixedBytes,
52
- serialize: (value) => fixBytes(serializer.serialize(value), fixedBytes),
53
- deserialize: (buffer, offset = 0) => {
54
- buffer = buffer.slice(offset, offset + fixedBytes);
55
- if (buffer.length < fixedBytes) {
56
- throw new NotEnoughBytesError("fixSerializer", fixedBytes, buffer.length);
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
+ });
83
+ }
84
+ function fixDecoder(decoder, fixedBytes) {
85
+ return createDecoder({
86
+ fixedSize: fixedBytes,
87
+ read: (bytes, offset) => {
88
+ assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
89
+ if (offset > 0 || bytes.length > fixedBytes) {
90
+ bytes = bytes.slice(offset, offset + fixedBytes);
57
91
  }
58
- if (serializer.fixedSize !== null) {
59
- buffer = fixBytes(buffer, serializer.fixedSize);
92
+ if (isFixedSize(decoder)) {
93
+ bytes = fixBytes(bytes, decoder.fixedSize);
60
94
  }
61
- const [value] = serializer.deserialize(buffer, 0);
95
+ const [value] = decoder.read(bytes, 0);
62
96
  return [value, offset + fixedBytes];
63
97
  }
64
- };
98
+ });
99
+ }
100
+ function mapEncoder(encoder, unmap) {
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
+ });
65
105
  }
66
106
 
67
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.2/node_modules/@metaplex-foundation/umi-serializers-encodings/dist/esm/errors.mjs
68
- var InvalidBaseStringError = class extends Error {
69
- constructor(value, base, cause) {
70
- const message = `Expected a string of base ${base}, got [${value}].`;
71
- super(message);
72
- __publicField(this, "name", "InvalidBaseStringError");
73
- this.cause = cause;
107
+ // ../codecs-numbers/dist/index.browser.js
108
+ function assertNumberIsBetweenForCodec(codecDescription, min, max, value) {
109
+ if (value < min || value > max) {
110
+ throw new Error(
111
+ `Codec [${codecDescription}] expected number to be in the range [${min}, ${max}], got ${value}.`
112
+ );
74
113
  }
75
- };
76
-
77
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.2/node_modules/@metaplex-foundation/umi-serializers-encodings/dist/esm/baseX.mjs
78
- var baseX = (alphabet) => {
79
- const base = alphabet.length;
80
- const baseBigInt = BigInt(base);
81
- return {
82
- description: `base${base}`,
83
- fixedSize: null,
84
- maxSize: null,
85
- serialize(value) {
86
- if (!value.match(new RegExp(`^[${alphabet}]*$`))) {
87
- throw new InvalidBaseStringError(value, base);
114
+ }
115
+ function isLittleEndian(config) {
116
+ return (config == null ? void 0 : config.endian) === 1 ? false : true;
117
+ }
118
+ function numberEncoderFactory(input) {
119
+ return createEncoder({
120
+ fixedSize: input.size,
121
+ write(value, bytes, offset) {
122
+ if (input.range) {
123
+ assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
88
124
  }
125
+ const arrayBuffer = new ArrayBuffer(input.size);
126
+ input.set(new DataView(arrayBuffer), value, isLittleEndian(input.config));
127
+ bytes.set(new Uint8Array(arrayBuffer), offset);
128
+ return offset + input.size;
129
+ }
130
+ });
131
+ }
132
+ function numberDecoderFactory(input) {
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);
138
+ const view = new DataView(toArrayBuffer(bytes, offset, input.size));
139
+ return [input.get(view, isLittleEndian(input.config)), offset + input.size];
140
+ }
141
+ });
142
+ }
143
+ function toArrayBuffer(bytes, offset, length) {
144
+ const bytesOffset = bytes.byteOffset + (offset != null ? offset : 0);
145
+ const bytesLength = length != null ? length : bytes.byteLength;
146
+ return bytes.buffer.slice(bytesOffset, bytesOffset + bytesLength);
147
+ }
148
+ var getU32Encoder = (config = {}) => numberEncoderFactory({
149
+ config,
150
+ name: "u32",
151
+ range: [0, Number("0xffffffff")],
152
+ set: (view, value, le) => view.setUint32(0, value, le),
153
+ size: 4
154
+ });
155
+ var getU32Decoder = (config = {}) => numberDecoderFactory({
156
+ config,
157
+ get: (view, le) => view.getUint32(0, le),
158
+ name: "u32",
159
+ size: 4
160
+ });
161
+
162
+ // ../codecs-strings/dist/index.browser.js
163
+ function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
164
+ if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
165
+ throw new Error(`Expected a string of base ${alphabet4.length}, got [${givenValue}].`);
166
+ }
167
+ }
168
+ var getBaseXEncoder = (alphabet4) => {
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) {
178
+ assertValidBaseString(alphabet4, value);
89
179
  if (value === "")
90
- return new Uint8Array();
91
- const chars = [...value];
92
- let trailIndex = chars.findIndex((c) => c !== alphabet[0]);
93
- trailIndex = trailIndex === -1 ? chars.length : trailIndex;
94
- const leadingZeroes = Array(trailIndex).fill(0);
95
- if (trailIndex === chars.length)
96
- return Uint8Array.from(leadingZeroes);
97
- const tailChars = chars.slice(trailIndex);
98
- let base10Number = 0n;
99
- let baseXPower = 1n;
100
- for (let i = tailChars.length - 1; i >= 0; i -= 1) {
101
- base10Number += baseXPower * BigInt(alphabet.indexOf(tailChars[i]));
102
- 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;
103
185
  }
186
+ let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
104
187
  const tailBytes = [];
105
188
  while (base10Number > 0n) {
106
189
  tailBytes.unshift(Number(base10Number % 256n));
107
190
  base10Number /= 256n;
108
191
  }
109
- return Uint8Array.from(leadingZeroes.concat(tailBytes));
110
- },
111
- deserialize(buffer, offset = 0) {
112
- if (buffer.length === 0)
192
+ const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
193
+ bytes.set(bytesToAdd, offset);
194
+ return offset + bytesToAdd.length;
195
+ }
196
+ });
197
+ };
198
+ var getBaseXDecoder = (alphabet4) => {
199
+ return createDecoder({
200
+ read(rawBytes, offset) {
201
+ const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
202
+ if (bytes.length === 0)
113
203
  return ["", 0];
114
- const bytes = buffer.slice(offset);
115
204
  let trailIndex = bytes.findIndex((n) => n !== 0);
116
205
  trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
117
- const leadingZeroes = alphabet[0].repeat(trailIndex);
206
+ const leadingZeroes = alphabet4[0].repeat(trailIndex);
118
207
  if (trailIndex === bytes.length)
119
- return [leadingZeroes, buffer.length];
120
- let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
121
- const tailChars = [];
122
- while (base10Number > 0n) {
123
- tailChars.unshift(alphabet[Number(base10Number % baseBigInt)]);
124
- base10Number /= baseBigInt;
125
- }
126
- return [leadingZeroes + tailChars.join(""), buffer.length];
208
+ return [leadingZeroes, rawBytes.length];
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];
127
212
  }
128
- };
213
+ });
129
214
  };
130
-
131
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.2/node_modules/@metaplex-foundation/umi-serializers-encodings/dist/esm/base58.mjs
132
- var base58 = baseX("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
133
-
134
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.2/node_modules/@metaplex-foundation/umi-serializers-encodings/dist/esm/nullCharacters.mjs
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
+ }
232
+ var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
233
+ var getBase58Encoder = () => getBaseXEncoder(alphabet2);
234
+ var getBase58Decoder = () => getBaseXDecoder(alphabet2);
135
235
  var removeNullCharacters = (value) => (
136
236
  // eslint-disable-next-line no-control-regex
137
237
  value.replace(/\u0000/g, "")
138
238
  );
139
-
140
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.2/node_modules/@metaplex-foundation/umi-serializers-encodings/dist/esm/utf8.mjs
141
- var utf8 = {
142
- description: "utf8",
143
- fixedSize: null,
144
- maxSize: null,
145
- serialize(value) {
146
- return new TextEncoder().encode(value);
147
- },
148
- deserialize(buffer, offset = 0) {
149
- const value = new TextDecoder().decode(buffer.slice(offset));
150
- return [removeNullCharacters(value), buffer.length];
151
- }
152
- };
153
-
154
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-numbers@0.8.2/node_modules/@metaplex-foundation/umi-serializers-numbers/dist/esm/common.mjs
155
- var Endian;
156
- (function(Endian2) {
157
- Endian2["Little"] = "le";
158
- Endian2["Big"] = "be";
159
- })(Endian || (Endian = {}));
160
-
161
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-numbers@0.8.2/node_modules/@metaplex-foundation/umi-serializers-numbers/dist/esm/errors.mjs
162
- var NumberOutOfRangeError = class extends RangeError {
163
- constructor(serializer, min, max, actual) {
164
- super(`Serializer [${serializer}] expected number to be between ${min} and ${max}, got ${actual}.`);
165
- __publicField(this, "name", "NumberOutOfRangeError");
166
- }
239
+ var e = globalThis.TextDecoder;
240
+ var o = globalThis.TextEncoder;
241
+ var getUtf8Encoder = () => {
242
+ let textEncoder;
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
+ });
167
251
  };
168
-
169
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-numbers@0.8.2/node_modules/@metaplex-foundation/umi-serializers-numbers/dist/esm/utils.mjs
170
- function numberFactory(input) {
171
- let littleEndian;
172
- let defaultDescription = input.name;
173
- if (input.size > 1) {
174
- littleEndian = !("endian" in input.options) || input.options.endian === Endian.Little;
175
- defaultDescription += littleEndian ? "(le)" : "(be)";
176
- }
177
- return {
178
- description: input.options.description ?? defaultDescription,
179
- fixedSize: input.size,
180
- maxSize: input.size,
181
- serialize(value) {
182
- if (input.range) {
183
- assertRange(input.name, input.range[0], input.range[1], value);
184
- }
185
- const buffer = new ArrayBuffer(input.size);
186
- input.set(new DataView(buffer), value, littleEndian);
187
- return new Uint8Array(buffer);
188
- },
189
- deserialize(bytes, offset = 0) {
190
- const slice = bytes.slice(offset, offset + input.size);
191
- assertEnoughBytes("i8", slice, input.size);
192
- const view = toDataView(slice);
193
- return [input.get(view, littleEndian), offset + input.size];
252
+ var getUtf8Decoder = () => {
253
+ let textDecoder;
254
+ return createDecoder({
255
+ read(bytes, offset) {
256
+ const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
257
+ return [removeNullCharacters(value), bytes.length];
194
258
  }
195
- };
196
- }
197
- var toArrayBuffer = (array) => array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset);
198
- var toDataView = (array) => new DataView(toArrayBuffer(array));
199
- var assertRange = (serializer, min, max, value) => {
200
- if (value < min || value > max) {
201
- throw new NumberOutOfRangeError(serializer, min, max, value);
202
- }
259
+ });
203
260
  };
204
- var assertEnoughBytes = (serializer, bytes, expected) => {
205
- if (bytes.length === 0) {
206
- throw new DeserializingEmptyBufferError(serializer);
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();
265
+ if (size === "variable") {
266
+ return encoding;
207
267
  }
208
- if (bytes.length < expected) {
209
- throw new NotEnoughBytesError(serializer, expected, bytes.length);
268
+ if (typeof size === "number") {
269
+ return fixEncoder(encoding, size);
210
270
  }
211
- };
212
-
213
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-numbers@0.8.2/node_modules/@metaplex-foundation/umi-serializers-numbers/dist/esm/u32.mjs
214
- var u32 = (options = {}) => numberFactory({
215
- name: "u32",
216
- size: 4,
217
- range: [0, Number("0xffffffff")],
218
- set: (view, value, le) => view.setUint32(0, Number(value), le),
219
- get: (view, le) => view.getUint32(0, le),
220
- options
221
- });
222
-
223
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers@0.8.5/node_modules/@metaplex-foundation/umi-serializers/dist/esm/utils.mjs
224
- function getSizeDescription(size) {
225
- return typeof size === "object" ? size.description : `${size}`;
271
+ return createEncoder({
272
+ getSizeFromValue: (value) => {
273
+ const contentSize = getEncodedSize(value, encoding);
274
+ return getEncodedSize(contentSize, size) + contentSize;
275
+ },
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
+ });
226
282
  }
227
-
228
- // ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers@0.8.5/node_modules/@metaplex-foundation/umi-serializers/dist/esm/string.mjs
229
- function string(options = {}) {
230
- const size = options.size ?? u32();
231
- const encoding = options.encoding ?? utf8;
232
- const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
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();
233
287
  if (size === "variable") {
234
- return {
235
- ...encoding,
236
- description
237
- };
288
+ return encoding;
238
289
  }
239
290
  if (typeof size === "number") {
240
- return fixSerializer(encoding, size, description);
291
+ return fixDecoder(encoding, size);
241
292
  }
242
- return {
243
- description,
244
- fixedSize: null,
245
- maxSize: null,
246
- serialize: (value) => {
247
- const contentBytes = encoding.serialize(value);
248
- const lengthBytes = size.serialize(contentBytes.length);
249
- return mergeBytes([lengthBytes, contentBytes]);
250
- },
251
- deserialize: (buffer, offset = 0) => {
252
- if (buffer.slice(offset).length === 0) {
253
- throw new DeserializingEmptyBufferError("string");
254
- }
255
- const [lengthBigInt, lengthOffset] = size.deserialize(buffer, offset);
293
+ return createDecoder({
294
+ read: (bytes, offset = 0) => {
295
+ assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
296
+ const [lengthBigInt, lengthOffset] = size.read(bytes, offset);
256
297
  const length = Number(lengthBigInt);
257
298
  offset = lengthOffset;
258
- const contentBuffer = buffer.slice(offset, offset + length);
259
- if (contentBuffer.length < length) {
260
- throw new NotEnoughBytesError("string", length, contentBuffer.length);
261
- }
262
- const [value, contentOffset] = encoding.deserialize(contentBuffer);
299
+ const contentBytes = bytes.slice(offset, offset + length);
300
+ assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
301
+ const [value, contentOffset] = encoding.read(contentBytes, 0);
263
302
  offset += contentOffset;
264
303
  return [value, offset];
265
304
  }
266
- };
305
+ });
267
306
  }
268
307
 
269
- // src/base58.ts
270
- function assertIsBase58EncodedAddress(putativeBase58EncodedAddress) {
308
+ // src/address.ts
309
+ var memoizedBase58Encoder;
310
+ var memoizedBase58Decoder;
311
+ function getMemoizedBase58Encoder() {
312
+ if (!memoizedBase58Encoder)
313
+ memoizedBase58Encoder = getBase58Encoder();
314
+ return memoizedBase58Encoder;
315
+ }
316
+ function getMemoizedBase58Decoder() {
317
+ if (!memoizedBase58Decoder)
318
+ memoizedBase58Decoder = getBase58Decoder();
319
+ return memoizedBase58Decoder;
320
+ }
321
+ function isAddress(putativeAddress) {
322
+ if (
323
+ // Lowest address (32 bytes of zeroes)
324
+ putativeAddress.length < 32 || // Highest address (32 bytes of 255)
325
+ putativeAddress.length > 44
326
+ ) {
327
+ return false;
328
+ }
329
+ const base58Encoder = getMemoizedBase58Encoder();
330
+ const bytes = base58Encoder.encode(putativeAddress);
331
+ const numBytes = bytes.byteLength;
332
+ if (numBytes !== 32) {
333
+ return false;
334
+ }
335
+ return true;
336
+ }
337
+ function assertIsAddress(putativeAddress) {
271
338
  try {
272
339
  if (
273
340
  // Lowest address (32 bytes of zeroes)
274
- putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
275
- putativeBase58EncodedAddress.length > 44
341
+ putativeAddress.length < 32 || // Highest address (32 bytes of 255)
342
+ putativeAddress.length > 44
276
343
  ) {
277
344
  throw new Error("Expected input string to decode to a byte array of length 32.");
278
345
  }
279
- const bytes = base58.serialize(putativeBase58EncodedAddress);
346
+ const base58Encoder = getMemoizedBase58Encoder();
347
+ const bytes = base58Encoder.encode(putativeAddress);
280
348
  const numBytes = bytes.byteLength;
281
349
  if (numBytes !== 32) {
282
350
  throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
283
351
  }
284
- } catch (e) {
285
- throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
286
- cause: e
352
+ } catch (e2) {
353
+ throw new Error(`\`${putativeAddress}\` is not a base-58 encoded address`, {
354
+ cause: e2
287
355
  });
288
356
  }
289
357
  }
290
- function getBase58EncodedAddressCodec(config) {
291
- return string({
292
- description: config?.description ?? ("A 32-byte account address" ),
293
- encoding: base58,
294
- size: 32
295
- });
358
+ function address(putativeAddress) {
359
+ assertIsAddress(putativeAddress);
360
+ return putativeAddress;
296
361
  }
297
- function getBase58EncodedAddressComparator() {
362
+ function getAddressEncoder() {
363
+ return mapEncoder(
364
+ getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }),
365
+ (putativeAddress) => address(putativeAddress)
366
+ );
367
+ }
368
+ function getAddressDecoder() {
369
+ return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 });
370
+ }
371
+ function getAddressCodec() {
372
+ return combineCodec(getAddressEncoder(), getAddressDecoder());
373
+ }
374
+ function getAddressComparator() {
298
375
  return new Intl.Collator("en", {
299
376
  caseFirst: "lower",
300
377
  ignorePunctuation: false,
@@ -314,14 +391,16 @@ this.globalThis.solanaWeb3 = (function (exports) {
314
391
  }
315
392
  }
316
393
  async function assertDigestCapabilityIsAvailable() {
394
+ var _a;
317
395
  assertIsSecureContext();
318
- 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") {
319
397
  throw new Error("No digest implementation could be found");
320
398
  }
321
399
  }
322
400
  async function assertKeyExporterIsAvailable() {
401
+ var _a;
323
402
  assertIsSecureContext();
324
- 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") {
325
404
  throw new Error("No key export implementation could be found");
326
405
  }
327
406
  }
@@ -417,6 +496,21 @@ this.globalThis.solanaWeb3 = (function (exports) {
417
496
  }
418
497
 
419
498
  // src/program-derived-address.ts
499
+ function isProgramDerivedAddress(value) {
500
+ return Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number" && value[1] >= 0 && value[1] <= 255 && isAddress(value[0]);
501
+ }
502
+ function assertIsProgramDerivedAddress(value) {
503
+ const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
504
+ if (!validFormat) {
505
+ throw new Error(
506
+ `Expected given program derived address to have the following format: [Address, ProgramDerivedAddressBump].`
507
+ );
508
+ }
509
+ if (value[1] < 0 || value[1] > 255) {
510
+ throw new Error(`Expected program derived address bump to be in the range [0, 255], got: ${value[1]}.`);
511
+ }
512
+ assertIsAddress(value[0]);
513
+ }
420
514
  var MAX_SEED_LENGTH = 32;
421
515
  var MAX_SEEDS = 16;
422
516
  var PDA_MARKER_BYTES = [
@@ -459,8 +553,8 @@ this.globalThis.solanaWeb3 = (function (exports) {
459
553
  acc.push(...bytes);
460
554
  return acc;
461
555
  }, []);
462
- const base58EncodedAddressCodec = getBase58EncodedAddressCodec();
463
- const programAddressBytes = base58EncodedAddressCodec.serialize(programAddress);
556
+ const base58EncodedAddressCodec = getAddressCodec();
557
+ const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);
464
558
  const addressBytesBuffer = await crypto.subtle.digest(
465
559
  "SHA-256",
466
560
  new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES])
@@ -469,46 +563,70 @@ this.globalThis.solanaWeb3 = (function (exports) {
469
563
  if (await compressedPointBytesAreOnCurve(addressBytes)) {
470
564
  throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
471
565
  }
472
- return base58EncodedAddressCodec.deserialize(addressBytes)[0];
566
+ return base58EncodedAddressCodec.decode(addressBytes);
473
567
  }
474
- async function getProgramDerivedAddress({ programAddress, seeds }) {
568
+ async function getProgramDerivedAddress({
569
+ programAddress,
570
+ seeds
571
+ }) {
475
572
  let bumpSeed = 255;
476
573
  while (bumpSeed > 0) {
477
574
  try {
478
- return {
479
- bumpSeed,
480
- pda: await createProgramDerivedAddress({
481
- programAddress,
482
- seeds: [...seeds, new Uint8Array([bumpSeed])]
483
- })
484
- };
485
- } catch (e) {
486
- if (e instanceof PointOnCurveError) {
575
+ const address2 = await createProgramDerivedAddress({
576
+ programAddress,
577
+ seeds: [...seeds, new Uint8Array([bumpSeed])]
578
+ });
579
+ return [address2, bumpSeed];
580
+ } catch (e2) {
581
+ if (e2 instanceof PointOnCurveError) {
487
582
  bumpSeed--;
488
583
  } else {
489
- throw e;
584
+ throw e2;
490
585
  }
491
586
  }
492
587
  }
493
588
  throw new Error("Unable to find a viable program address bump seed");
494
589
  }
590
+ async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
591
+ const { encode, decode } = getAddressCodec();
592
+ const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
593
+ if (seedBytes.byteLength > MAX_SEED_LENGTH) {
594
+ throw new Error(`The seed exceeds the maximum length of 32 bytes`);
595
+ }
596
+ const programAddressBytes = encode(programAddress);
597
+ if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
598
+ throw new Error(`programAddress cannot end with the PDA marker`);
599
+ }
600
+ const addressBytesBuffer = await crypto.subtle.digest(
601
+ "SHA-256",
602
+ new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
603
+ );
604
+ const addressBytes = new Uint8Array(addressBytesBuffer);
605
+ return decode(addressBytes);
606
+ }
495
607
 
496
608
  // src/public-key.ts
497
- async function getBase58EncodedAddressFromPublicKey(publicKey) {
609
+ async function getAddressFromPublicKey(publicKey) {
498
610
  await assertKeyExporterIsAvailable();
499
611
  if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
500
612
  throw new Error("The `CryptoKey` must be an `Ed25519` public key");
501
613
  }
502
614
  const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
503
- const [base58EncodedAddress] = getBase58EncodedAddressCodec().deserialize(new Uint8Array(publicKeyBytes));
504
- return base58EncodedAddress;
615
+ return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
505
616
  }
506
617
 
507
- exports.assertIsBase58EncodedAddress = assertIsBase58EncodedAddress;
508
- exports.getBase58EncodedAddressCodec = getBase58EncodedAddressCodec;
509
- exports.getBase58EncodedAddressComparator = getBase58EncodedAddressComparator;
510
- exports.getBase58EncodedAddressFromPublicKey = getBase58EncodedAddressFromPublicKey;
618
+ exports.address = address;
619
+ exports.assertIsAddress = assertIsAddress;
620
+ exports.assertIsProgramDerivedAddress = assertIsProgramDerivedAddress;
621
+ exports.createAddressWithSeed = createAddressWithSeed;
622
+ exports.getAddressCodec = getAddressCodec;
623
+ exports.getAddressComparator = getAddressComparator;
624
+ exports.getAddressDecoder = getAddressDecoder;
625
+ exports.getAddressEncoder = getAddressEncoder;
626
+ exports.getAddressFromPublicKey = getAddressFromPublicKey;
511
627
  exports.getProgramDerivedAddress = getProgramDerivedAddress;
628
+ exports.isAddress = isAddress;
629
+ exports.isProgramDerivedAddress = isProgramDerivedAddress;
512
630
 
513
631
  return exports;
514
632