@solana/addresses 2.0.0-experimental.f57da91 → 2.0.0-experimental.f5d64e6
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 +120 -13
- package/dist/index.browser.cjs +53 -40
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +52 -41
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +52 -41
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +53 -40
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +52 -41
- package/dist/index.node.js.map +1 -1
- package/dist/types/address.d.ts +8 -8
- package/dist/types/address.d.ts.map +1 -0
- package/dist/types/curve.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/program-derived-address.d.ts +6 -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 -14
- package/dist/index.development.js +0 -581
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -20
|
@@ -1,581 +0,0 @@
|
|
|
1
|
-
this.globalThis = this.globalThis || {};
|
|
2
|
-
this.globalThis.solanaWeb3 = (function (exports) {
|
|
3
|
-
'use strict';
|
|
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.9/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
|
-
};
|
|
23
|
-
var padBytes = (bytes, length) => {
|
|
24
|
-
if (bytes.length >= length)
|
|
25
|
-
return bytes;
|
|
26
|
-
const paddedBytes = new Uint8Array(length).fill(0);
|
|
27
|
-
paddedBytes.set(bytes);
|
|
28
|
-
return paddedBytes;
|
|
29
|
-
};
|
|
30
|
-
var fixBytes = (bytes, length) => padBytes(bytes.slice(0, length), length);
|
|
31
|
-
|
|
32
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-core@0.8.9/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");
|
|
37
|
-
}
|
|
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");
|
|
43
|
-
}
|
|
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
|
-
return {
|
|
49
|
-
description: description ?? `fixed(${fixedBytes}, ${serializer.description})`,
|
|
50
|
-
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);
|
|
57
|
-
}
|
|
58
|
-
if (serializer.fixedSize !== null) {
|
|
59
|
-
buffer = fixBytes(buffer, serializer.fixedSize);
|
|
60
|
-
}
|
|
61
|
-
const [value] = serializer.deserialize(buffer, 0);
|
|
62
|
-
return [value, offset + fixedBytes];
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.9/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;
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.9/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);
|
|
88
|
-
}
|
|
89
|
-
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;
|
|
103
|
-
}
|
|
104
|
-
const tailBytes = [];
|
|
105
|
-
while (base10Number > 0n) {
|
|
106
|
-
tailBytes.unshift(Number(base10Number % 256n));
|
|
107
|
-
base10Number /= 256n;
|
|
108
|
-
}
|
|
109
|
-
return Uint8Array.from(leadingZeroes.concat(tailBytes));
|
|
110
|
-
},
|
|
111
|
-
deserialize(buffer, offset = 0) {
|
|
112
|
-
if (buffer.length === 0)
|
|
113
|
-
return ["", 0];
|
|
114
|
-
const bytes = buffer.slice(offset);
|
|
115
|
-
let trailIndex = bytes.findIndex((n) => n !== 0);
|
|
116
|
-
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
|
|
117
|
-
const leadingZeroes = alphabet[0].repeat(trailIndex);
|
|
118
|
-
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];
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.9/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.9/node_modules/@metaplex-foundation/umi-serializers-encodings/dist/esm/nullCharacters.mjs
|
|
135
|
-
var removeNullCharacters = (value) => (
|
|
136
|
-
// eslint-disable-next-line no-control-regex
|
|
137
|
-
value.replace(/\u0000/g, "")
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-encodings@0.8.9/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.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
|
-
}
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
// ../../node_modules/.pnpm/@metaplex-foundation+umi-serializers-numbers@0.8.9/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];
|
|
194
|
-
}
|
|
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
|
-
}
|
|
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
|
-
};
|
|
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;
|
|
232
|
-
const description = options.description ?? `string(${encoding.description}; ${getSizeDescription(size)})`;
|
|
233
|
-
if (size === "variable") {
|
|
234
|
-
return {
|
|
235
|
-
...encoding,
|
|
236
|
-
description
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
if (typeof size === "number") {
|
|
240
|
-
return fixSerializer(encoding, size, description);
|
|
241
|
-
}
|
|
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);
|
|
256
|
-
const length = Number(lengthBigInt);
|
|
257
|
-
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);
|
|
263
|
-
offset += contentOffset;
|
|
264
|
-
return [value, offset];
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// src/address.ts
|
|
270
|
-
function isAddress(putativeBase58EncodedAddress) {
|
|
271
|
-
if (
|
|
272
|
-
// Lowest address (32 bytes of zeroes)
|
|
273
|
-
putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
274
|
-
putativeBase58EncodedAddress.length > 44
|
|
275
|
-
) {
|
|
276
|
-
return false;
|
|
277
|
-
}
|
|
278
|
-
const bytes = base58.serialize(putativeBase58EncodedAddress);
|
|
279
|
-
const numBytes = bytes.byteLength;
|
|
280
|
-
if (numBytes !== 32) {
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
return true;
|
|
284
|
-
}
|
|
285
|
-
function assertIsAddress(putativeBase58EncodedAddress) {
|
|
286
|
-
try {
|
|
287
|
-
if (
|
|
288
|
-
// Lowest address (32 bytes of zeroes)
|
|
289
|
-
putativeBase58EncodedAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
290
|
-
putativeBase58EncodedAddress.length > 44
|
|
291
|
-
) {
|
|
292
|
-
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
293
|
-
}
|
|
294
|
-
const bytes = base58.serialize(putativeBase58EncodedAddress);
|
|
295
|
-
const numBytes = bytes.byteLength;
|
|
296
|
-
if (numBytes !== 32) {
|
|
297
|
-
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
298
|
-
}
|
|
299
|
-
} catch (e) {
|
|
300
|
-
throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
|
|
301
|
-
cause: e
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
function address(putativeBase58EncodedAddress) {
|
|
306
|
-
assertIsAddress(putativeBase58EncodedAddress);
|
|
307
|
-
return putativeBase58EncodedAddress;
|
|
308
|
-
}
|
|
309
|
-
function getAddressCodec(config) {
|
|
310
|
-
return string({
|
|
311
|
-
description: config?.description ?? ("A 32-byte account address" ),
|
|
312
|
-
encoding: base58,
|
|
313
|
-
size: 32
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
function getAddressComparator() {
|
|
317
|
-
return new Intl.Collator("en", {
|
|
318
|
-
caseFirst: "lower",
|
|
319
|
-
ignorePunctuation: false,
|
|
320
|
-
localeMatcher: "best fit",
|
|
321
|
-
numeric: false,
|
|
322
|
-
sensitivity: "variant",
|
|
323
|
-
usage: "sort"
|
|
324
|
-
}).compare;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
// ../assertions/dist/index.browser.js
|
|
328
|
-
function assertIsSecureContext() {
|
|
329
|
-
if (!globalThis.isSecureContext) {
|
|
330
|
-
throw new Error(
|
|
331
|
-
"Cryptographic operations are only allowed in secure browser contexts. Read more here: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts"
|
|
332
|
-
);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
async function assertDigestCapabilityIsAvailable() {
|
|
336
|
-
assertIsSecureContext();
|
|
337
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.digest !== "function") {
|
|
338
|
-
throw new Error("No digest implementation could be found");
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
async function assertKeyExporterIsAvailable() {
|
|
342
|
-
assertIsSecureContext();
|
|
343
|
-
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle?.exportKey !== "function") {
|
|
344
|
-
throw new Error("No key export implementation could be found");
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// src/vendor/noble/ed25519.ts
|
|
349
|
-
var D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;
|
|
350
|
-
var P = 57896044618658097711785492504343953926634992332820282019728792003956564819949n;
|
|
351
|
-
var RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
|
|
352
|
-
function mod(a) {
|
|
353
|
-
const r = a % P;
|
|
354
|
-
return r >= 0n ? r : P + r;
|
|
355
|
-
}
|
|
356
|
-
function pow2(x, power) {
|
|
357
|
-
let r = x;
|
|
358
|
-
while (power-- > 0n) {
|
|
359
|
-
r *= r;
|
|
360
|
-
r %= P;
|
|
361
|
-
}
|
|
362
|
-
return r;
|
|
363
|
-
}
|
|
364
|
-
function pow_2_252_3(x) {
|
|
365
|
-
const x2 = x * x % P;
|
|
366
|
-
const b2 = x2 * x % P;
|
|
367
|
-
const b4 = pow2(b2, 2n) * b2 % P;
|
|
368
|
-
const b5 = pow2(b4, 1n) * x % P;
|
|
369
|
-
const b10 = pow2(b5, 5n) * b5 % P;
|
|
370
|
-
const b20 = pow2(b10, 10n) * b10 % P;
|
|
371
|
-
const b40 = pow2(b20, 20n) * b20 % P;
|
|
372
|
-
const b80 = pow2(b40, 40n) * b40 % P;
|
|
373
|
-
const b160 = pow2(b80, 80n) * b80 % P;
|
|
374
|
-
const b240 = pow2(b160, 80n) * b80 % P;
|
|
375
|
-
const b250 = pow2(b240, 10n) * b10 % P;
|
|
376
|
-
const pow_p_5_8 = pow2(b250, 2n) * x % P;
|
|
377
|
-
return pow_p_5_8;
|
|
378
|
-
}
|
|
379
|
-
function uvRatio(u, v) {
|
|
380
|
-
const v3 = mod(v * v * v);
|
|
381
|
-
const v7 = mod(v3 * v3 * v);
|
|
382
|
-
const pow = pow_2_252_3(u * v7);
|
|
383
|
-
let x = mod(u * v3 * pow);
|
|
384
|
-
const vx2 = mod(v * x * x);
|
|
385
|
-
const root1 = x;
|
|
386
|
-
const root2 = mod(x * RM1);
|
|
387
|
-
const useRoot1 = vx2 === u;
|
|
388
|
-
const useRoot2 = vx2 === mod(-u);
|
|
389
|
-
const noRoot = vx2 === mod(-u * RM1);
|
|
390
|
-
if (useRoot1)
|
|
391
|
-
x = root1;
|
|
392
|
-
if (useRoot2 || noRoot)
|
|
393
|
-
x = root2;
|
|
394
|
-
if ((mod(x) & 1n) === 1n)
|
|
395
|
-
x = mod(-x);
|
|
396
|
-
if (!useRoot1 && !useRoot2) {
|
|
397
|
-
return null;
|
|
398
|
-
}
|
|
399
|
-
return x;
|
|
400
|
-
}
|
|
401
|
-
function pointIsOnCurve(y, lastByte) {
|
|
402
|
-
const y2 = mod(y * y);
|
|
403
|
-
const u = mod(y2 - 1n);
|
|
404
|
-
const v = mod(D * y2 + 1n);
|
|
405
|
-
const x = uvRatio(u, v);
|
|
406
|
-
if (x === null) {
|
|
407
|
-
return false;
|
|
408
|
-
}
|
|
409
|
-
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
410
|
-
if (x === 0n && isLastByteOdd) {
|
|
411
|
-
return false;
|
|
412
|
-
}
|
|
413
|
-
return true;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// src/curve.ts
|
|
417
|
-
function byteToHex(byte) {
|
|
418
|
-
const hexString = byte.toString(16);
|
|
419
|
-
if (hexString.length === 1) {
|
|
420
|
-
return `0${hexString}`;
|
|
421
|
-
} else {
|
|
422
|
-
return hexString;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
function decompressPointBytes(bytes) {
|
|
426
|
-
const hexString = bytes.reduce((acc, byte, ii) => `${byteToHex(ii === 31 ? byte & ~128 : byte)}${acc}`, "");
|
|
427
|
-
const integerLiteralString = `0x${hexString}`;
|
|
428
|
-
return BigInt(integerLiteralString);
|
|
429
|
-
}
|
|
430
|
-
async function compressedPointBytesAreOnCurve(bytes) {
|
|
431
|
-
if (bytes.byteLength !== 32) {
|
|
432
|
-
return false;
|
|
433
|
-
}
|
|
434
|
-
const y = decompressPointBytes(bytes);
|
|
435
|
-
return pointIsOnCurve(y, bytes[31]);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// src/program-derived-address.ts
|
|
439
|
-
function isProgramDerivedAddress(value) {
|
|
440
|
-
return Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number" && value[1] >= 0 && value[1] <= 255 && isAddress(value[0]);
|
|
441
|
-
}
|
|
442
|
-
function assertIsProgramDerivedAddress(value) {
|
|
443
|
-
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
444
|
-
if (!validFormat) {
|
|
445
|
-
throw new Error(
|
|
446
|
-
`Expected given program derived address to have the following format: [Base58EncodedAddress, ProgramDerivedAddressBump].`
|
|
447
|
-
);
|
|
448
|
-
}
|
|
449
|
-
if (value[1] < 0 || value[1] > 255) {
|
|
450
|
-
throw new Error(`Expected program derived address bump to be in the range [0, 255], got: ${value[1]}.`);
|
|
451
|
-
}
|
|
452
|
-
assertIsAddress(value[0]);
|
|
453
|
-
}
|
|
454
|
-
var MAX_SEED_LENGTH = 32;
|
|
455
|
-
var MAX_SEEDS = 16;
|
|
456
|
-
var PDA_MARKER_BYTES = [
|
|
457
|
-
// The string 'ProgramDerivedAddress'
|
|
458
|
-
80,
|
|
459
|
-
114,
|
|
460
|
-
111,
|
|
461
|
-
103,
|
|
462
|
-
114,
|
|
463
|
-
97,
|
|
464
|
-
109,
|
|
465
|
-
68,
|
|
466
|
-
101,
|
|
467
|
-
114,
|
|
468
|
-
105,
|
|
469
|
-
118,
|
|
470
|
-
101,
|
|
471
|
-
100,
|
|
472
|
-
65,
|
|
473
|
-
100,
|
|
474
|
-
100,
|
|
475
|
-
114,
|
|
476
|
-
101,
|
|
477
|
-
115,
|
|
478
|
-
115
|
|
479
|
-
];
|
|
480
|
-
var PointOnCurveError = class extends Error {
|
|
481
|
-
};
|
|
482
|
-
async function createProgramDerivedAddress({
|
|
483
|
-
programAddress,
|
|
484
|
-
seeds
|
|
485
|
-
}) {
|
|
486
|
-
await assertDigestCapabilityIsAvailable();
|
|
487
|
-
if (seeds.length > MAX_SEEDS) {
|
|
488
|
-
throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);
|
|
489
|
-
}
|
|
490
|
-
let textEncoder;
|
|
491
|
-
const seedBytes = seeds.reduce((acc, seed, ii) => {
|
|
492
|
-
const bytes = typeof seed === "string" ? (textEncoder || (textEncoder = new TextEncoder())).encode(seed) : seed;
|
|
493
|
-
if (bytes.byteLength > MAX_SEED_LENGTH) {
|
|
494
|
-
throw new Error(`The seed at index ${ii} exceeds the maximum length of 32 bytes`);
|
|
495
|
-
}
|
|
496
|
-
acc.push(...bytes);
|
|
497
|
-
return acc;
|
|
498
|
-
}, []);
|
|
499
|
-
const base58EncodedAddressCodec = getAddressCodec();
|
|
500
|
-
const programAddressBytes = base58EncodedAddressCodec.serialize(programAddress);
|
|
501
|
-
const addressBytesBuffer = await crypto.subtle.digest(
|
|
502
|
-
"SHA-256",
|
|
503
|
-
new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES])
|
|
504
|
-
);
|
|
505
|
-
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
506
|
-
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
507
|
-
throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
|
|
508
|
-
}
|
|
509
|
-
return base58EncodedAddressCodec.deserialize(addressBytes)[0];
|
|
510
|
-
}
|
|
511
|
-
async function getProgramDerivedAddress({
|
|
512
|
-
programAddress,
|
|
513
|
-
seeds
|
|
514
|
-
}) {
|
|
515
|
-
let bumpSeed = 255;
|
|
516
|
-
while (bumpSeed > 0) {
|
|
517
|
-
try {
|
|
518
|
-
const address2 = await createProgramDerivedAddress({
|
|
519
|
-
programAddress,
|
|
520
|
-
seeds: [...seeds, new Uint8Array([bumpSeed])]
|
|
521
|
-
});
|
|
522
|
-
return [address2, bumpSeed];
|
|
523
|
-
} catch (e) {
|
|
524
|
-
if (e instanceof PointOnCurveError) {
|
|
525
|
-
bumpSeed--;
|
|
526
|
-
} else {
|
|
527
|
-
throw e;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
throw new Error("Unable to find a viable program address bump seed");
|
|
532
|
-
}
|
|
533
|
-
async function createAddressWithSeed({
|
|
534
|
-
baseAddress,
|
|
535
|
-
programAddress,
|
|
536
|
-
seed
|
|
537
|
-
}) {
|
|
538
|
-
const { serialize, deserialize } = getAddressCodec();
|
|
539
|
-
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
540
|
-
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
541
|
-
throw new Error(`The seed exceeds the maximum length of 32 bytes`);
|
|
542
|
-
}
|
|
543
|
-
const programAddressBytes = serialize(programAddress);
|
|
544
|
-
if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
|
|
545
|
-
throw new Error(`programAddress cannot end with the PDA marker`);
|
|
546
|
-
}
|
|
547
|
-
const addressBytesBuffer = await crypto.subtle.digest(
|
|
548
|
-
"SHA-256",
|
|
549
|
-
new Uint8Array([...serialize(baseAddress), ...seedBytes, ...programAddressBytes])
|
|
550
|
-
);
|
|
551
|
-
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
552
|
-
return deserialize(addressBytes)[0];
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
// src/public-key.ts
|
|
556
|
-
async function getAddressFromPublicKey(publicKey) {
|
|
557
|
-
await assertKeyExporterIsAvailable();
|
|
558
|
-
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
559
|
-
throw new Error("The `CryptoKey` must be an `Ed25519` public key");
|
|
560
|
-
}
|
|
561
|
-
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
562
|
-
const [base58EncodedAddress] = getAddressCodec().deserialize(new Uint8Array(publicKeyBytes));
|
|
563
|
-
return base58EncodedAddress;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
exports.address = address;
|
|
567
|
-
exports.assertIsAddress = assertIsAddress;
|
|
568
|
-
exports.assertIsProgramDerivedAddress = assertIsProgramDerivedAddress;
|
|
569
|
-
exports.createAddressWithSeed = createAddressWithSeed;
|
|
570
|
-
exports.getAddressCodec = getAddressCodec;
|
|
571
|
-
exports.getAddressComparator = getAddressComparator;
|
|
572
|
-
exports.getAddressFromPublicKey = getAddressFromPublicKey;
|
|
573
|
-
exports.getProgramDerivedAddress = getProgramDerivedAddress;
|
|
574
|
-
exports.isAddress = isAddress;
|
|
575
|
-
exports.isProgramDerivedAddress = isProgramDerivedAddress;
|
|
576
|
-
|
|
577
|
-
return exports;
|
|
578
|
-
|
|
579
|
-
})({});
|
|
580
|
-
//# sourceMappingURL=out.js.map
|
|
581
|
-
//# sourceMappingURL=index.development.js.map
|