@solana/addresses 2.0.0-experimental.9baeda8 → 2.0.0-experimental.9c8a30e
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 +1 -1
- package/README.md +117 -10
- package/dist/index.browser.cjs +85 -35
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +78 -32
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +319 -216
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +78 -32
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +85 -35
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +78 -32
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +13 -9
- package/dist/types/address.d.ts +18 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/program-derived-address.d.ts +38 -0
- package/dist/types/public-key.d.ts +1 -1
- package/package.json +10 -9
- package/dist/types/base58.d.ts +0 -12
- package/dist/types/computed-address.d.ts +0 -18
|
@@ -2,19 +2,30 @@ this.globalThis = this.globalThis || {};
|
|
|
2
2
|
this.globalThis.solanaWeb3 = (function (exports) {
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
+
}
|
|
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);
|
|
15
26
|
const result = new Uint8Array(totalLength);
|
|
16
27
|
let offset = 0;
|
|
17
|
-
|
|
28
|
+
nonEmptyByteArrays.forEach((arr) => {
|
|
18
29
|
result.set(arr, offset);
|
|
19
30
|
offset += arr.length;
|
|
20
31
|
});
|
|
@@ -27,69 +38,157 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
27
38
|
paddedBytes.set(bytes);
|
|
28
39
|
return paddedBytes;
|
|
29
40
|
};
|
|
30
|
-
var fixBytes = (bytes, length) => padBytes(bytes.slice(0, length), length);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
__publicField(this, "name", "DeserializingEmptyBufferError");
|
|
41
|
+
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) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].`
|
|
46
|
+
);
|
|
37
47
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
if (encoder.maxSize !== decoder.maxSize) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
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
|
+
);
|
|
43
57
|
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-core@0.8.9/node_modules/@metaplex-foundation/umi-serializers-core/dist/esm/fixSerializer.mjs
|
|
47
|
-
function fixSerializer(serializer, fixedBytes, description) {
|
|
48
58
|
return {
|
|
49
|
-
|
|
59
|
+
decode: decoder.decode,
|
|
60
|
+
description: description ?? encoder.description,
|
|
61
|
+
encode: encoder.encode,
|
|
62
|
+
fixedSize: encoder.fixedSize,
|
|
63
|
+
maxSize: encoder.maxSize
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function fixCodecHelper(data, fixedBytes, description) {
|
|
67
|
+
return {
|
|
68
|
+
description: description ?? `fixed(${fixedBytes}, ${data.description})`,
|
|
50
69
|
fixedSize: fixedBytes,
|
|
51
|
-
maxSize: fixedBytes
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function fixDecoder(decoder, fixedBytes, description) {
|
|
80
|
+
return {
|
|
81
|
+
...fixCodecHelper(decoder, fixedBytes, description),
|
|
82
|
+
decode: (bytes, offset = 0) => {
|
|
83
|
+
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset);
|
|
84
|
+
if (offset > 0 || bytes.length > fixedBytes) {
|
|
85
|
+
bytes = bytes.slice(offset, offset + fixedBytes);
|
|
57
86
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
87
|
+
if (decoder.fixedSize !== null) {
|
|
88
|
+
bytes = fixBytes(bytes, decoder.fixedSize);
|
|
60
89
|
}
|
|
61
|
-
const [value] =
|
|
90
|
+
const [value] = decoder.decode(bytes, 0);
|
|
62
91
|
return [value, offset + fixedBytes];
|
|
63
92
|
}
|
|
64
93
|
};
|
|
65
94
|
}
|
|
95
|
+
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
|
+
};
|
|
102
|
+
}
|
|
66
103
|
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.cause = cause;
|
|
104
|
+
// ../codecs-numbers/dist/index.browser.js
|
|
105
|
+
function assertNumberIsBetweenForCodec(codecDescription, min, max, value) {
|
|
106
|
+
if (value < min || value > max) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Codec [${codecDescription}] expected number to be in the range [${min}, ${max}], got ${value}.`
|
|
109
|
+
);
|
|
74
110
|
}
|
|
75
|
-
}
|
|
111
|
+
}
|
|
112
|
+
function sharedNumberFactory(input) {
|
|
113
|
+
let littleEndian;
|
|
114
|
+
let defaultDescription = input.name;
|
|
115
|
+
if (input.size > 1) {
|
|
116
|
+
littleEndian = !("endian" in input.options) || input.options.endian === 0;
|
|
117
|
+
defaultDescription += littleEndian ? "(le)" : "(be)";
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
description: input.options.description ?? defaultDescription,
|
|
121
|
+
fixedSize: input.size,
|
|
122
|
+
littleEndian,
|
|
123
|
+
maxSize: input.size
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function numberEncoderFactory(input) {
|
|
127
|
+
const codecData = sharedNumberFactory(input);
|
|
128
|
+
return {
|
|
129
|
+
description: codecData.description,
|
|
130
|
+
encode(value) {
|
|
131
|
+
if (input.range) {
|
|
132
|
+
assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
|
|
133
|
+
}
|
|
134
|
+
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
|
+
};
|
|
141
|
+
}
|
|
142
|
+
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);
|
|
148
|
+
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
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function toArrayBuffer(bytes, offset, length) {
|
|
157
|
+
const bytesOffset = bytes.byteOffset + (offset ?? 0);
|
|
158
|
+
const bytesLength = length ?? bytes.byteLength;
|
|
159
|
+
return bytes.buffer.slice(bytesOffset, bytesOffset + bytesLength);
|
|
160
|
+
}
|
|
161
|
+
var getU32Encoder = (options = {}) => numberEncoderFactory({
|
|
162
|
+
name: "u32",
|
|
163
|
+
options,
|
|
164
|
+
range: [0, Number("0xffffffff")],
|
|
165
|
+
set: (view, value, le) => view.setUint32(0, value, le),
|
|
166
|
+
size: 4
|
|
167
|
+
});
|
|
168
|
+
var getU32Decoder = (options = {}) => numberDecoderFactory({
|
|
169
|
+
get: (view, le) => view.getUint32(0, le),
|
|
170
|
+
name: "u32",
|
|
171
|
+
options,
|
|
172
|
+
size: 4
|
|
173
|
+
});
|
|
76
174
|
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
175
|
+
// ../codecs-strings/dist/index.browser.js
|
|
176
|
+
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
|
|
177
|
+
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
|
|
178
|
+
throw new Error(`Expected a string of base ${alphabet4.length}, got [${givenValue}].`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
var getBaseXEncoder = (alphabet4) => {
|
|
182
|
+
const base = alphabet4.length;
|
|
80
183
|
const baseBigInt = BigInt(base);
|
|
81
184
|
return {
|
|
82
185
|
description: `base${base}`,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
serialize(value) {
|
|
86
|
-
if (!value.match(new RegExp(`^[${alphabet}]*$`))) {
|
|
87
|
-
throw new InvalidBaseStringError(value, base);
|
|
88
|
-
}
|
|
186
|
+
encode(value) {
|
|
187
|
+
assertValidBaseString(alphabet4, value);
|
|
89
188
|
if (value === "")
|
|
90
189
|
return new Uint8Array();
|
|
91
190
|
const chars = [...value];
|
|
92
|
-
let trailIndex = chars.findIndex((c) => c !==
|
|
191
|
+
let trailIndex = chars.findIndex((c) => c !== alphabet4[0]);
|
|
93
192
|
trailIndex = trailIndex === -1 ? chars.length : trailIndex;
|
|
94
193
|
const leadingZeroes = Array(trailIndex).fill(0);
|
|
95
194
|
if (trailIndex === chars.length)
|
|
@@ -98,7 +197,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
98
197
|
let base10Number = 0n;
|
|
99
198
|
let baseXPower = 1n;
|
|
100
199
|
for (let i = tailChars.length - 1; i >= 0; i -= 1) {
|
|
101
|
-
base10Number += baseXPower * BigInt(
|
|
200
|
+
base10Number += baseXPower * BigInt(alphabet4.indexOf(tailChars[i]));
|
|
102
201
|
baseXPower *= baseBigInt;
|
|
103
202
|
}
|
|
104
203
|
const tailBytes = [];
|
|
@@ -108,166 +207,132 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
108
207
|
}
|
|
109
208
|
return Uint8Array.from(leadingZeroes.concat(tailBytes));
|
|
110
209
|
},
|
|
111
|
-
|
|
112
|
-
|
|
210
|
+
fixedSize: null,
|
|
211
|
+
maxSize: null
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
var getBaseXDecoder = (alphabet4) => {
|
|
215
|
+
const base = alphabet4.length;
|
|
216
|
+
const baseBigInt = BigInt(base);
|
|
217
|
+
return {
|
|
218
|
+
decode(rawBytes, offset = 0) {
|
|
219
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
220
|
+
if (bytes.length === 0)
|
|
113
221
|
return ["", 0];
|
|
114
|
-
const bytes = buffer.slice(offset);
|
|
115
222
|
let trailIndex = bytes.findIndex((n) => n !== 0);
|
|
116
223
|
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
|
|
117
|
-
const leadingZeroes =
|
|
224
|
+
const leadingZeroes = alphabet4[0].repeat(trailIndex);
|
|
118
225
|
if (trailIndex === bytes.length)
|
|
119
|
-
return [leadingZeroes,
|
|
226
|
+
return [leadingZeroes, rawBytes.length];
|
|
120
227
|
let base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
|
|
121
228
|
const tailChars = [];
|
|
122
229
|
while (base10Number > 0n) {
|
|
123
|
-
tailChars.unshift(
|
|
230
|
+
tailChars.unshift(alphabet4[Number(base10Number % baseBigInt)]);
|
|
124
231
|
base10Number /= baseBigInt;
|
|
125
232
|
}
|
|
126
|
-
return [leadingZeroes + tailChars.join(""),
|
|
127
|
-
}
|
|
233
|
+
return [leadingZeroes + tailChars.join(""), rawBytes.length];
|
|
234
|
+
},
|
|
235
|
+
description: `base${base}`,
|
|
236
|
+
fixedSize: null,
|
|
237
|
+
maxSize: null
|
|
128
238
|
};
|
|
129
239
|
};
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
var
|
|
133
|
-
|
|
134
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.9/node_modules/@metaplex-foundation/umi-serializers-encodings/dist/esm/nullCharacters.mjs
|
|
240
|
+
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
241
|
+
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
|
|
242
|
+
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
|
|
135
243
|
var removeNullCharacters = (value) => (
|
|
136
244
|
// eslint-disable-next-line no-control-regex
|
|
137
245
|
value.replace(/\u0000/g, "")
|
|
138
246
|
);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
var
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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.9/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.9/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
|
-
}
|
|
247
|
+
var e = globalThis.TextDecoder;
|
|
248
|
+
var o = globalThis.TextEncoder;
|
|
249
|
+
var getUtf8Encoder = () => {
|
|
250
|
+
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
|
+
};
|
|
167
257
|
};
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
}
|
|
258
|
+
var getUtf8Decoder = () => {
|
|
259
|
+
let textDecoder;
|
|
177
260
|
return {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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);
|
|
261
|
+
decode(bytes, offset = 0) {
|
|
262
|
+
const value = (textDecoder || (textDecoder = new e())).decode(bytes.slice(offset));
|
|
263
|
+
return [removeNullCharacters(value), bytes.length];
|
|
188
264
|
},
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const view = toDataView(slice);
|
|
193
|
-
return [input.get(view, littleEndian), offset + input.size];
|
|
194
|
-
}
|
|
265
|
+
description: "utf8",
|
|
266
|
+
fixedSize: null,
|
|
267
|
+
maxSize: null
|
|
195
268
|
};
|
|
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
|
-
}
|
|
203
269
|
};
|
|
204
|
-
var
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
if (bytes.length < expected) {
|
|
209
|
-
throw new NotEnoughBytesError(serializer, expected, bytes.length);
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-numbers@0.8.9/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.9/node_modules/@metaplex-foundation/umi-serializers/dist/esm/utils.mjs
|
|
224
|
-
function getSizeDescription(size) {
|
|
225
|
-
return typeof size === "object" ? size.description : `${size}`;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers@0.8.9/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;
|
|
270
|
+
var getStringEncoder = (options = {}) => {
|
|
271
|
+
const size = options.size ?? getU32Encoder();
|
|
272
|
+
const encoding = options.encoding ?? getUtf8Encoder();
|
|
232
273
|
const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
|
|
233
274
|
if (size === "variable") {
|
|
234
|
-
return {
|
|
235
|
-
...encoding,
|
|
236
|
-
description
|
|
237
|
-
};
|
|
275
|
+
return { ...encoding, description };
|
|
238
276
|
}
|
|
239
277
|
if (typeof size === "number") {
|
|
240
|
-
return
|
|
278
|
+
return fixEncoder(encoding, size, description);
|
|
241
279
|
}
|
|
242
280
|
return {
|
|
243
281
|
description,
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const contentBytes = encoding.serialize(value);
|
|
248
|
-
const lengthBytes = size.serialize(contentBytes.length);
|
|
282
|
+
encode: (value) => {
|
|
283
|
+
const contentBytes = encoding.encode(value);
|
|
284
|
+
const lengthBytes = size.encode(contentBytes.length);
|
|
249
285
|
return mergeBytes([lengthBytes, contentBytes]);
|
|
250
286
|
},
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
287
|
+
fixedSize: null,
|
|
288
|
+
maxSize: null
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
var getStringDecoder = (options = {}) => {
|
|
292
|
+
const size = options.size ?? getU32Decoder();
|
|
293
|
+
const encoding = options.encoding ?? getUtf8Decoder();
|
|
294
|
+
const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
|
|
295
|
+
if (size === "variable") {
|
|
296
|
+
return { ...encoding, description };
|
|
297
|
+
}
|
|
298
|
+
if (typeof size === "number") {
|
|
299
|
+
return fixDecoder(encoding, size, description);
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
decode: (bytes, offset = 0) => {
|
|
303
|
+
assertByteArrayIsNotEmptyForCodec("string", bytes, offset);
|
|
304
|
+
const [lengthBigInt, lengthOffset] = size.decode(bytes, offset);
|
|
256
305
|
const length = Number(lengthBigInt);
|
|
257
306
|
offset = lengthOffset;
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
const [value, contentOffset] = encoding.deserialize(contentBuffer);
|
|
307
|
+
const contentBytes = bytes.slice(offset, offset + length);
|
|
308
|
+
assertByteArrayHasEnoughBytesForCodec("string", length, contentBytes);
|
|
309
|
+
const [value, contentOffset] = encoding.decode(contentBytes);
|
|
263
310
|
offset += contentOffset;
|
|
264
311
|
return [value, offset];
|
|
265
|
-
}
|
|
312
|
+
},
|
|
313
|
+
description,
|
|
314
|
+
fixedSize: null,
|
|
315
|
+
maxSize: null
|
|
266
316
|
};
|
|
317
|
+
};
|
|
318
|
+
function getSizeDescription(size) {
|
|
319
|
+
return typeof size === "object" ? size.description : `${size}`;
|
|
267
320
|
}
|
|
268
321
|
|
|
269
|
-
// src/
|
|
270
|
-
|
|
322
|
+
// src/address.ts
|
|
323
|
+
var memoizedBase58Encoder;
|
|
324
|
+
var memoizedBase58Decoder;
|
|
325
|
+
function getMemoizedBase58Encoder() {
|
|
326
|
+
if (!memoizedBase58Encoder)
|
|
327
|
+
memoizedBase58Encoder = getBase58Encoder();
|
|
328
|
+
return memoizedBase58Encoder;
|
|
329
|
+
}
|
|
330
|
+
function getMemoizedBase58Decoder() {
|
|
331
|
+
if (!memoizedBase58Decoder)
|
|
332
|
+
memoizedBase58Decoder = getBase58Decoder();
|
|
333
|
+
return memoizedBase58Decoder;
|
|
334
|
+
}
|
|
335
|
+
function isAddress(putativeBase58EncodedAddress) {
|
|
271
336
|
if (
|
|
272
337
|
// Lowest address (32 bytes of zeroes)
|
|
273
338
|
putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
@@ -275,14 +340,15 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
275
340
|
) {
|
|
276
341
|
return false;
|
|
277
342
|
}
|
|
278
|
-
const
|
|
343
|
+
const base58Encoder = getMemoizedBase58Encoder();
|
|
344
|
+
const bytes = base58Encoder.encode(putativeBase58EncodedAddress);
|
|
279
345
|
const numBytes = bytes.byteLength;
|
|
280
346
|
if (numBytes !== 32) {
|
|
281
347
|
return false;
|
|
282
348
|
}
|
|
283
349
|
return true;
|
|
284
350
|
}
|
|
285
|
-
function
|
|
351
|
+
function assertIsAddress(putativeBase58EncodedAddress) {
|
|
286
352
|
try {
|
|
287
353
|
if (
|
|
288
354
|
// Lowest address (32 bytes of zeroes)
|
|
@@ -291,29 +357,43 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
291
357
|
) {
|
|
292
358
|
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
293
359
|
}
|
|
294
|
-
const
|
|
360
|
+
const base58Encoder = getMemoizedBase58Encoder();
|
|
361
|
+
const bytes = base58Encoder.encode(putativeBase58EncodedAddress);
|
|
295
362
|
const numBytes = bytes.byteLength;
|
|
296
363
|
if (numBytes !== 32) {
|
|
297
364
|
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
298
365
|
}
|
|
299
|
-
} catch (
|
|
366
|
+
} catch (e2) {
|
|
300
367
|
throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
|
|
301
|
-
cause:
|
|
368
|
+
cause: e2
|
|
302
369
|
});
|
|
303
370
|
}
|
|
304
371
|
}
|
|
305
372
|
function address(putativeBase58EncodedAddress) {
|
|
306
|
-
|
|
373
|
+
assertIsAddress(putativeBase58EncodedAddress);
|
|
307
374
|
return putativeBase58EncodedAddress;
|
|
308
375
|
}
|
|
309
|
-
function
|
|
310
|
-
return
|
|
311
|
-
|
|
312
|
-
|
|
376
|
+
function getAddressEncoder(config) {
|
|
377
|
+
return mapEncoder(
|
|
378
|
+
getStringEncoder({
|
|
379
|
+
description: config?.description ?? "Base58EncodedAddress",
|
|
380
|
+
encoding: getMemoizedBase58Encoder(),
|
|
381
|
+
size: 32
|
|
382
|
+
}),
|
|
383
|
+
(putativeAddress) => address(putativeAddress)
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
function getAddressDecoder(config) {
|
|
387
|
+
return getStringDecoder({
|
|
388
|
+
description: config?.description ?? "Base58EncodedAddress",
|
|
389
|
+
encoding: getMemoizedBase58Decoder(),
|
|
313
390
|
size: 32
|
|
314
391
|
});
|
|
315
392
|
}
|
|
316
|
-
function
|
|
393
|
+
function getAddressCodec(config) {
|
|
394
|
+
return combineCodec(getAddressEncoder(config), getAddressDecoder(config));
|
|
395
|
+
}
|
|
396
|
+
function getAddressComparator() {
|
|
317
397
|
return new Intl.Collator("en", {
|
|
318
398
|
caseFirst: "lower",
|
|
319
399
|
ignorePunctuation: false,
|
|
@@ -435,7 +515,22 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
435
515
|
return pointIsOnCurve(y, bytes[31]);
|
|
436
516
|
}
|
|
437
517
|
|
|
438
|
-
// src/
|
|
518
|
+
// src/program-derived-address.ts
|
|
519
|
+
function isProgramDerivedAddress(value) {
|
|
520
|
+
return Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number" && value[1] >= 0 && value[1] <= 255 && isAddress(value[0]);
|
|
521
|
+
}
|
|
522
|
+
function assertIsProgramDerivedAddress(value) {
|
|
523
|
+
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
524
|
+
if (!validFormat) {
|
|
525
|
+
throw new Error(
|
|
526
|
+
`Expected given program derived address to have the following format: [Base58EncodedAddress, ProgramDerivedAddressBump].`
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
if (value[1] < 0 || value[1] > 255) {
|
|
530
|
+
throw new Error(`Expected program derived address bump to be in the range [0, 255], got: ${value[1]}.`);
|
|
531
|
+
}
|
|
532
|
+
assertIsAddress(value[0]);
|
|
533
|
+
}
|
|
439
534
|
var MAX_SEED_LENGTH = 32;
|
|
440
535
|
var MAX_SEEDS = 16;
|
|
441
536
|
var PDA_MARKER_BYTES = [
|
|
@@ -464,7 +559,10 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
464
559
|
];
|
|
465
560
|
var PointOnCurveError = class extends Error {
|
|
466
561
|
};
|
|
467
|
-
async function createProgramDerivedAddress({
|
|
562
|
+
async function createProgramDerivedAddress({
|
|
563
|
+
programAddress,
|
|
564
|
+
seeds
|
|
565
|
+
}) {
|
|
468
566
|
await assertDigestCapabilityIsAvailable();
|
|
469
567
|
if (seeds.length > MAX_SEEDS) {
|
|
470
568
|
throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);
|
|
@@ -478,8 +576,8 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
478
576
|
acc.push(...bytes);
|
|
479
577
|
return acc;
|
|
480
578
|
}, []);
|
|
481
|
-
const base58EncodedAddressCodec =
|
|
482
|
-
const programAddressBytes = base58EncodedAddressCodec.
|
|
579
|
+
const base58EncodedAddressCodec = getAddressCodec();
|
|
580
|
+
const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);
|
|
483
581
|
const addressBytesBuffer = await crypto.subtle.digest(
|
|
484
582
|
"SHA-256",
|
|
485
583
|
new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES])
|
|
@@ -488,24 +586,25 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
488
586
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
489
587
|
throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
|
|
490
588
|
}
|
|
491
|
-
return base58EncodedAddressCodec.
|
|
589
|
+
return base58EncodedAddressCodec.decode(addressBytes)[0];
|
|
492
590
|
}
|
|
493
|
-
async function getProgramDerivedAddress({
|
|
591
|
+
async function getProgramDerivedAddress({
|
|
592
|
+
programAddress,
|
|
593
|
+
seeds
|
|
594
|
+
}) {
|
|
494
595
|
let bumpSeed = 255;
|
|
495
596
|
while (bumpSeed > 0) {
|
|
496
597
|
try {
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
} catch (e) {
|
|
505
|
-
if (e instanceof PointOnCurveError) {
|
|
598
|
+
const address2 = await createProgramDerivedAddress({
|
|
599
|
+
programAddress,
|
|
600
|
+
seeds: [...seeds, new Uint8Array([bumpSeed])]
|
|
601
|
+
});
|
|
602
|
+
return [address2, bumpSeed];
|
|
603
|
+
} catch (e2) {
|
|
604
|
+
if (e2 instanceof PointOnCurveError) {
|
|
506
605
|
bumpSeed--;
|
|
507
606
|
} else {
|
|
508
|
-
throw
|
|
607
|
+
throw e2;
|
|
509
608
|
}
|
|
510
609
|
}
|
|
511
610
|
}
|
|
@@ -516,21 +615,21 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
516
615
|
programAddress,
|
|
517
616
|
seed
|
|
518
617
|
}) {
|
|
519
|
-
const {
|
|
618
|
+
const { encode, decode } = getAddressCodec();
|
|
520
619
|
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
521
620
|
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
522
621
|
throw new Error(`The seed exceeds the maximum length of 32 bytes`);
|
|
523
622
|
}
|
|
524
|
-
const programAddressBytes =
|
|
623
|
+
const programAddressBytes = encode(programAddress);
|
|
525
624
|
if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
|
|
526
625
|
throw new Error(`programAddress cannot end with the PDA marker`);
|
|
527
626
|
}
|
|
528
627
|
const addressBytesBuffer = await crypto.subtle.digest(
|
|
529
628
|
"SHA-256",
|
|
530
|
-
new Uint8Array([...
|
|
629
|
+
new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
|
|
531
630
|
);
|
|
532
631
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
533
|
-
return
|
|
632
|
+
return decode(addressBytes)[0];
|
|
534
633
|
}
|
|
535
634
|
|
|
536
635
|
// src/public-key.ts
|
|
@@ -540,18 +639,22 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
540
639
|
throw new Error("The `CryptoKey` must be an `Ed25519` public key");
|
|
541
640
|
}
|
|
542
641
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
543
|
-
const [base58EncodedAddress] =
|
|
642
|
+
const [base58EncodedAddress] = getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
544
643
|
return base58EncodedAddress;
|
|
545
644
|
}
|
|
546
645
|
|
|
547
646
|
exports.address = address;
|
|
548
|
-
exports.
|
|
647
|
+
exports.assertIsAddress = assertIsAddress;
|
|
648
|
+
exports.assertIsProgramDerivedAddress = assertIsProgramDerivedAddress;
|
|
549
649
|
exports.createAddressWithSeed = createAddressWithSeed;
|
|
650
|
+
exports.getAddressCodec = getAddressCodec;
|
|
651
|
+
exports.getAddressComparator = getAddressComparator;
|
|
652
|
+
exports.getAddressDecoder = getAddressDecoder;
|
|
653
|
+
exports.getAddressEncoder = getAddressEncoder;
|
|
550
654
|
exports.getAddressFromPublicKey = getAddressFromPublicKey;
|
|
551
|
-
exports.getBase58EncodedAddressCodec = getBase58EncodedAddressCodec;
|
|
552
|
-
exports.getBase58EncodedAddressComparator = getBase58EncodedAddressComparator;
|
|
553
655
|
exports.getProgramDerivedAddress = getProgramDerivedAddress;
|
|
554
|
-
exports.
|
|
656
|
+
exports.isAddress = isAddress;
|
|
657
|
+
exports.isProgramDerivedAddress = isProgramDerivedAddress;
|
|
555
658
|
|
|
556
659
|
return exports;
|
|
557
660
|
|