@solana/addresses 2.0.0-experimental.f5c87d2 → 2.0.0-experimental.fc4e943
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 +10 -10
- package/dist/index.browser.cjs +121 -28
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +111 -25
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +355 -209
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +111 -27
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +121 -28
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +111 -25
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +13 -6
- package/dist/types/address.d.ts +18 -0
- package/dist/types/address.d.ts.map +1 -0
- package/dist/types/curve.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/program-derived-address.d.ts +32 -6
- package/dist/types/program-derived-address.d.ts.map +1 -0
- package/dist/types/public-key.d.ts +2 -2
- package/dist/types/public-key.d.ts.map +1 -0
- package/dist/types/vendor/noble/ed25519.d.ts.map +1 -0
- package/package.json +15 -13
- package/dist/types/base58.d.ts +0 -10
|
@@ -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
|
-
__publicField(this, "name", "NotEnoughBytesError");
|
|
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
|
+
);
|
|
43
52
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
+
return {
|
|
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) {
|
|
48
67
|
return {
|
|
49
|
-
description: description ?? `fixed(${fixedBytes}, ${
|
|
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,148 @@ 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.2/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.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
|
-
}
|
|
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
|
-
};
|
|
204
|
-
var assertEnoughBytes = (serializer, bytes, expected) => {
|
|
205
|
-
if (bytes.length === 0) {
|
|
206
|
-
throw new DeserializingEmptyBufferError(serializer);
|
|
207
|
-
}
|
|
208
|
-
if (bytes.length < expected) {
|
|
209
|
-
throw new NotEnoughBytesError(serializer, expected, bytes.length);
|
|
210
|
-
}
|
|
211
269
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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}`;
|
|
226
|
-
}
|
|
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;
|
|
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) {
|
|
336
|
+
if (
|
|
337
|
+
// Lowest address (32 bytes of zeroes)
|
|
338
|
+
putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
339
|
+
putativeBase58EncodedAddress.length > 44
|
|
340
|
+
) {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
const base58Encoder = getMemoizedBase58Encoder();
|
|
344
|
+
const bytes = base58Encoder.encode(putativeBase58EncodedAddress);
|
|
345
|
+
const numBytes = bytes.byteLength;
|
|
346
|
+
if (numBytes !== 32) {
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
function assertIsAddress(putativeBase58EncodedAddress) {
|
|
271
352
|
try {
|
|
272
353
|
if (
|
|
273
354
|
// Lowest address (32 bytes of zeroes)
|
|
@@ -276,25 +357,43 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
276
357
|
) {
|
|
277
358
|
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
278
359
|
}
|
|
279
|
-
const
|
|
360
|
+
const base58Encoder = getMemoizedBase58Encoder();
|
|
361
|
+
const bytes = base58Encoder.encode(putativeBase58EncodedAddress);
|
|
280
362
|
const numBytes = bytes.byteLength;
|
|
281
363
|
if (numBytes !== 32) {
|
|
282
364
|
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
283
365
|
}
|
|
284
|
-
} catch (
|
|
366
|
+
} catch (e2) {
|
|
285
367
|
throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
|
|
286
|
-
cause:
|
|
368
|
+
cause: e2
|
|
287
369
|
});
|
|
288
370
|
}
|
|
289
371
|
}
|
|
290
|
-
function
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
372
|
+
function address(putativeBase58EncodedAddress) {
|
|
373
|
+
assertIsAddress(putativeBase58EncodedAddress);
|
|
374
|
+
return putativeBase58EncodedAddress;
|
|
375
|
+
}
|
|
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(),
|
|
294
390
|
size: 32
|
|
295
391
|
});
|
|
296
392
|
}
|
|
297
|
-
function
|
|
393
|
+
function getAddressCodec(config) {
|
|
394
|
+
return combineCodec(getAddressEncoder(config), getAddressDecoder(config));
|
|
395
|
+
}
|
|
396
|
+
function getAddressComparator() {
|
|
298
397
|
return new Intl.Collator("en", {
|
|
299
398
|
caseFirst: "lower",
|
|
300
399
|
ignorePunctuation: false,
|
|
@@ -417,6 +516,21 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
417
516
|
}
|
|
418
517
|
|
|
419
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
|
+
}
|
|
420
534
|
var MAX_SEED_LENGTH = 32;
|
|
421
535
|
var MAX_SEEDS = 16;
|
|
422
536
|
var PDA_MARKER_BYTES = [
|
|
@@ -445,7 +559,10 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
445
559
|
];
|
|
446
560
|
var PointOnCurveError = class extends Error {
|
|
447
561
|
};
|
|
448
|
-
async function createProgramDerivedAddress({
|
|
562
|
+
async function createProgramDerivedAddress({
|
|
563
|
+
programAddress,
|
|
564
|
+
seeds
|
|
565
|
+
}) {
|
|
449
566
|
await assertDigestCapabilityIsAvailable();
|
|
450
567
|
if (seeds.length > MAX_SEEDS) {
|
|
451
568
|
throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);
|
|
@@ -459,8 +576,8 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
459
576
|
acc.push(...bytes);
|
|
460
577
|
return acc;
|
|
461
578
|
}, []);
|
|
462
|
-
const base58EncodedAddressCodec =
|
|
463
|
-
const programAddressBytes = base58EncodedAddressCodec.
|
|
579
|
+
const base58EncodedAddressCodec = getAddressCodec();
|
|
580
|
+
const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);
|
|
464
581
|
const addressBytesBuffer = await crypto.subtle.digest(
|
|
465
582
|
"SHA-256",
|
|
466
583
|
new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES])
|
|
@@ -469,46 +586,75 @@ this.globalThis.solanaWeb3 = (function (exports) {
|
|
|
469
586
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
470
587
|
throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
|
|
471
588
|
}
|
|
472
|
-
return base58EncodedAddressCodec.
|
|
589
|
+
return base58EncodedAddressCodec.decode(addressBytes)[0];
|
|
473
590
|
}
|
|
474
|
-
async function getProgramDerivedAddress({
|
|
591
|
+
async function getProgramDerivedAddress({
|
|
592
|
+
programAddress,
|
|
593
|
+
seeds
|
|
594
|
+
}) {
|
|
475
595
|
let bumpSeed = 255;
|
|
476
596
|
while (bumpSeed > 0) {
|
|
477
597
|
try {
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
} catch (e) {
|
|
486
|
-
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) {
|
|
487
605
|
bumpSeed--;
|
|
488
606
|
} else {
|
|
489
|
-
throw
|
|
607
|
+
throw e2;
|
|
490
608
|
}
|
|
491
609
|
}
|
|
492
610
|
}
|
|
493
611
|
throw new Error("Unable to find a viable program address bump seed");
|
|
494
612
|
}
|
|
613
|
+
async function createAddressWithSeed({
|
|
614
|
+
baseAddress,
|
|
615
|
+
programAddress,
|
|
616
|
+
seed
|
|
617
|
+
}) {
|
|
618
|
+
const { encode, decode } = getAddressCodec();
|
|
619
|
+
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
620
|
+
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
621
|
+
throw new Error(`The seed exceeds the maximum length of 32 bytes`);
|
|
622
|
+
}
|
|
623
|
+
const programAddressBytes = encode(programAddress);
|
|
624
|
+
if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
|
|
625
|
+
throw new Error(`programAddress cannot end with the PDA marker`);
|
|
626
|
+
}
|
|
627
|
+
const addressBytesBuffer = await crypto.subtle.digest(
|
|
628
|
+
"SHA-256",
|
|
629
|
+
new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
|
|
630
|
+
);
|
|
631
|
+
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
632
|
+
return decode(addressBytes)[0];
|
|
633
|
+
}
|
|
495
634
|
|
|
496
635
|
// src/public-key.ts
|
|
497
|
-
async function
|
|
636
|
+
async function getAddressFromPublicKey(publicKey) {
|
|
498
637
|
await assertKeyExporterIsAvailable();
|
|
499
638
|
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
500
639
|
throw new Error("The `CryptoKey` must be an `Ed25519` public key");
|
|
501
640
|
}
|
|
502
641
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
503
|
-
const [base58EncodedAddress] =
|
|
642
|
+
const [base58EncodedAddress] = getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
504
643
|
return base58EncodedAddress;
|
|
505
644
|
}
|
|
506
645
|
|
|
507
|
-
exports.
|
|
508
|
-
exports.
|
|
509
|
-
exports.
|
|
510
|
-
exports.
|
|
646
|
+
exports.address = address;
|
|
647
|
+
exports.assertIsAddress = assertIsAddress;
|
|
648
|
+
exports.assertIsProgramDerivedAddress = assertIsProgramDerivedAddress;
|
|
649
|
+
exports.createAddressWithSeed = createAddressWithSeed;
|
|
650
|
+
exports.getAddressCodec = getAddressCodec;
|
|
651
|
+
exports.getAddressComparator = getAddressComparator;
|
|
652
|
+
exports.getAddressDecoder = getAddressDecoder;
|
|
653
|
+
exports.getAddressEncoder = getAddressEncoder;
|
|
654
|
+
exports.getAddressFromPublicKey = getAddressFromPublicKey;
|
|
511
655
|
exports.getProgramDerivedAddress = getProgramDerivedAddress;
|
|
656
|
+
exports.isAddress = isAddress;
|
|
657
|
+
exports.isProgramDerivedAddress = isProgramDerivedAddress;
|
|
512
658
|
|
|
513
659
|
return exports;
|
|
514
660
|
|