@solana/addresses 2.0.0-experimental.fe07532 → 2.0.0-experimental.feaeef2
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/README.md +21 -21
- package/dist/index.browser.cjs +59 -67
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +59 -67
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +59 -67
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +59 -67
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +59 -67
- package/dist/index.node.js.map +1 -1
- package/dist/types/address.d.ts +8 -14
- 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 +14 -34
- package/dist/index.development.js +0 -663
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -22
package/dist/index.browser.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { mapEncoder, combineCodec } from '@solana/codecs-core';
|
|
2
2
|
import { getStringEncoder, getStringDecoder, getBase58Encoder, getBase58Decoder } from '@solana/codecs-strings';
|
|
3
|
+
import { SolanaError, SOLANA_ERROR__ADDRESSES__STRING_LENGTH_OUT_OF_RANGE, SOLANA_ERROR__ADDRESSES__INVALID_BYTE_LENGTH, SOLANA_ERROR__ADDRESSES__MALFORMED_PDA, SOLANA_ERROR__ADDRESSES__PDA_BUMP_SEED_OUT_OF_RANGE, isSolanaError, SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE, SOLANA_ERROR__ADDRESSES__FAILED_TO_FIND_VIABLE_PDA_BUMP_SEED, SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, SOLANA_ERROR__ADDRESSES__PDA_ENDS_WITH_PDA_MARKER, SOLANA_ERROR__ADDRESSES__INVALID_ED25519_PUBLIC_KEY, SOLANA_ERROR__ADDRESSES__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED } from '@solana/errors';
|
|
3
4
|
import { assertKeyExporterIsAvailable, assertDigestCapabilityIsAvailable } from '@solana/assertions';
|
|
4
5
|
|
|
5
6
|
// src/address.ts
|
|
@@ -15,66 +16,56 @@ function getMemoizedBase58Decoder() {
|
|
|
15
16
|
memoizedBase58Decoder = getBase58Decoder();
|
|
16
17
|
return memoizedBase58Decoder;
|
|
17
18
|
}
|
|
18
|
-
function isAddress(
|
|
19
|
+
function isAddress(putativeAddress) {
|
|
19
20
|
if (
|
|
20
21
|
// Lowest address (32 bytes of zeroes)
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
23
|
+
putativeAddress.length > 44
|
|
23
24
|
) {
|
|
24
25
|
return false;
|
|
25
26
|
}
|
|
26
27
|
const base58Encoder = getMemoizedBase58Encoder();
|
|
27
|
-
const bytes = base58Encoder.encode(
|
|
28
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
28
29
|
const numBytes = bytes.byteLength;
|
|
29
30
|
if (numBytes !== 32) {
|
|
30
31
|
return false;
|
|
31
32
|
}
|
|
32
33
|
return true;
|
|
33
34
|
}
|
|
34
|
-
function assertIsAddress(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
|
|
51
|
-
cause: e
|
|
35
|
+
function assertIsAddress(putativeAddress) {
|
|
36
|
+
if (
|
|
37
|
+
// Lowest address (32 bytes of zeroes)
|
|
38
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
39
|
+
putativeAddress.length > 44
|
|
40
|
+
) {
|
|
41
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__STRING_LENGTH_OUT_OF_RANGE, {
|
|
42
|
+
actualLength: putativeAddress.length
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const base58Encoder = getMemoizedBase58Encoder();
|
|
46
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
47
|
+
const numBytes = bytes.byteLength;
|
|
48
|
+
if (numBytes !== 32) {
|
|
49
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_BYTE_LENGTH, {
|
|
50
|
+
actualLength: numBytes
|
|
52
51
|
});
|
|
53
52
|
}
|
|
54
53
|
}
|
|
55
|
-
function address(
|
|
56
|
-
assertIsAddress(
|
|
57
|
-
return
|
|
54
|
+
function address(putativeAddress) {
|
|
55
|
+
assertIsAddress(putativeAddress);
|
|
56
|
+
return putativeAddress;
|
|
58
57
|
}
|
|
59
|
-
function getAddressEncoder(
|
|
58
|
+
function getAddressEncoder() {
|
|
60
59
|
return mapEncoder(
|
|
61
|
-
getStringEncoder({
|
|
62
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
63
|
-
encoding: getMemoizedBase58Encoder(),
|
|
64
|
-
size: 32
|
|
65
|
-
}),
|
|
60
|
+
getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }),
|
|
66
61
|
(putativeAddress) => address(putativeAddress)
|
|
67
62
|
);
|
|
68
63
|
}
|
|
69
|
-
function getAddressDecoder(
|
|
70
|
-
return getStringDecoder({
|
|
71
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
72
|
-
encoding: getMemoizedBase58Decoder(),
|
|
73
|
-
size: 32
|
|
74
|
-
});
|
|
64
|
+
function getAddressDecoder() {
|
|
65
|
+
return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 });
|
|
75
66
|
}
|
|
76
|
-
function getAddressCodec(
|
|
77
|
-
return combineCodec(getAddressEncoder(
|
|
67
|
+
function getAddressCodec() {
|
|
68
|
+
return combineCodec(getAddressEncoder(), getAddressDecoder());
|
|
78
69
|
}
|
|
79
70
|
function getAddressComparator() {
|
|
80
71
|
return new Intl.Collator("en", {
|
|
@@ -184,12 +175,12 @@ function isProgramDerivedAddress(value) {
|
|
|
184
175
|
function assertIsProgramDerivedAddress(value) {
|
|
185
176
|
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
186
177
|
if (!validFormat) {
|
|
187
|
-
throw new
|
|
188
|
-
`Expected given program derived address to have the following format: [Base58EncodedAddress, ProgramDerivedAddressBump].`
|
|
189
|
-
);
|
|
178
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MALFORMED_PDA);
|
|
190
179
|
}
|
|
191
180
|
if (value[1] < 0 || value[1] > 255) {
|
|
192
|
-
throw new
|
|
181
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__PDA_BUMP_SEED_OUT_OF_RANGE, {
|
|
182
|
+
bump: value[1]
|
|
183
|
+
});
|
|
193
184
|
}
|
|
194
185
|
assertIsAddress(value[0]);
|
|
195
186
|
}
|
|
@@ -219,21 +210,23 @@ var PDA_MARKER_BYTES = [
|
|
|
219
210
|
115,
|
|
220
211
|
115
|
|
221
212
|
];
|
|
222
|
-
|
|
223
|
-
};
|
|
224
|
-
async function createProgramDerivedAddress({
|
|
225
|
-
programAddress,
|
|
226
|
-
seeds
|
|
227
|
-
}) {
|
|
213
|
+
async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
228
214
|
await assertDigestCapabilityIsAvailable();
|
|
229
215
|
if (seeds.length > MAX_SEEDS) {
|
|
230
|
-
throw new
|
|
216
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED, {
|
|
217
|
+
actual: seeds.length,
|
|
218
|
+
maxSeeds: MAX_SEEDS
|
|
219
|
+
});
|
|
231
220
|
}
|
|
232
221
|
let textEncoder;
|
|
233
222
|
const seedBytes = seeds.reduce((acc, seed, ii) => {
|
|
234
|
-
const bytes = typeof seed === "string" ? (textEncoder
|
|
223
|
+
const bytes = typeof seed === "string" ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;
|
|
235
224
|
if (bytes.byteLength > MAX_SEED_LENGTH) {
|
|
236
|
-
throw new
|
|
225
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
226
|
+
actual: bytes.byteLength,
|
|
227
|
+
index: ii,
|
|
228
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
229
|
+
});
|
|
237
230
|
}
|
|
238
231
|
acc.push(...bytes);
|
|
239
232
|
return acc;
|
|
@@ -246,9 +239,9 @@ async function createProgramDerivedAddress({
|
|
|
246
239
|
);
|
|
247
240
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
248
241
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
249
|
-
throw new
|
|
242
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE);
|
|
250
243
|
}
|
|
251
|
-
return base58EncodedAddressCodec.decode(addressBytes)
|
|
244
|
+
return base58EncodedAddressCodec.decode(addressBytes);
|
|
252
245
|
}
|
|
253
246
|
async function getProgramDerivedAddress({
|
|
254
247
|
programAddress,
|
|
@@ -263,44 +256,43 @@ async function getProgramDerivedAddress({
|
|
|
263
256
|
});
|
|
264
257
|
return [address2, bumpSeed];
|
|
265
258
|
} catch (e) {
|
|
266
|
-
if (e
|
|
259
|
+
if (isSolanaError(e, SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE)) {
|
|
267
260
|
bumpSeed--;
|
|
268
261
|
} else {
|
|
269
262
|
throw e;
|
|
270
263
|
}
|
|
271
264
|
}
|
|
272
265
|
}
|
|
273
|
-
throw new
|
|
266
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__FAILED_TO_FIND_VIABLE_PDA_BUMP_SEED);
|
|
274
267
|
}
|
|
275
|
-
async function createAddressWithSeed({
|
|
276
|
-
baseAddress,
|
|
277
|
-
programAddress,
|
|
278
|
-
seed
|
|
279
|
-
}) {
|
|
268
|
+
async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
280
269
|
const { encode, decode } = getAddressCodec();
|
|
281
270
|
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
282
271
|
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
283
|
-
throw new
|
|
272
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
273
|
+
actual: seedBytes.byteLength,
|
|
274
|
+
index: 0,
|
|
275
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
276
|
+
});
|
|
284
277
|
}
|
|
285
278
|
const programAddressBytes = encode(programAddress);
|
|
286
279
|
if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
|
|
287
|
-
throw new
|
|
280
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__PDA_ENDS_WITH_PDA_MARKER);
|
|
288
281
|
}
|
|
289
282
|
const addressBytesBuffer = await crypto.subtle.digest(
|
|
290
283
|
"SHA-256",
|
|
291
284
|
new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
|
|
292
285
|
);
|
|
293
286
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
294
|
-
return decode(addressBytes)
|
|
287
|
+
return decode(addressBytes);
|
|
295
288
|
}
|
|
296
289
|
async function getAddressFromPublicKey(publicKey) {
|
|
297
290
|
await assertKeyExporterIsAvailable();
|
|
298
291
|
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
299
|
-
throw new
|
|
292
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_ED25519_PUBLIC_KEY);
|
|
300
293
|
}
|
|
301
294
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
302
|
-
|
|
303
|
-
return base58EncodedAddress;
|
|
295
|
+
return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
304
296
|
}
|
|
305
297
|
|
|
306
298
|
export { address, assertIsAddress, assertIsProgramDerivedAddress, createAddressWithSeed, getAddressCodec, getAddressComparator, getAddressDecoder, getAddressEncoder, getAddressFromPublicKey, getProgramDerivedAddress, isAddress, isProgramDerivedAddress };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/address.ts","../src/program-derived-address.ts","../src/vendor/noble/ed25519.ts","../src/curve.ts","../src/public-key.ts"],"names":["address"],"mappings":";AAAA,SAAgB,cAAgC,kBAAkB;AAClE,SAAS,kBAAkB,kBAAkB,kBAAkB,wBAAwB;AAMvF,IAAI;AACJ,IAAI;AAEJ,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEA,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEO,SAAS,UACZ,8BACyF;AAEzF;AAAA;AAAA,IAEI,6BAA6B,SAAS;AAAA,IAEtC,6BAA6B,SAAS;AAAA,IACxC;AACE,WAAO;AAAA,EACX;AAEA,QAAM,gBAAgB,yBAAyB;AAC/C,QAAM,QAAQ,cAAc,OAAO,4BAA4B;AAC/D,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEO,SAAS,gBACZ,8BACiG;AACjG,MAAI;AAEA;AAAA;AAAA,MAEI,6BAA6B,SAAS;AAAA,MAEtC,6BAA6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EE,UAAM,IAAI,MAAM,+CAA+C;AAAA,EACnE;AAEA,QAAM,qBAAqB,MAAM,OAAO,OAAO;AAAA,IAC3C;AAAA,IACA,IAAI,WAAW,CAAC,GAAG,OAAO,WAAW,GAAG,GAAG,WAAW,GAAG,mBAAmB,CAAC;AAAA,EACjF;AACA,QAAM,eAAe,IAAI,WAAW,kBAAkB;AAEtD,SAAO,OAAO,YAAY,EAAE,CAAC;AACjC;;;AGxKA,SAAS,oCAAoC;AAI7C,eAAsB,wBAAwB,WAAqD;AAC/F,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AAEvE,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACrE;AACA,QAAM,iBAAiB,MAAM,OAAO,OAAO,UAAU,OAAO,SAAS;AACrE,QAAM,CAAC,oBAAoB,IAAI,kBAAkB,EAAE,OAAO,IAAI,WAAW,cAAc,CAAC;AACxF,SAAO;AACX","sourcesContent":["import { Codec, combineCodec, Decoder, Encoder, mapEncoder } from '@solana/codecs-core';\nimport { getBase58Decoder, getBase58Encoder, getStringDecoder, getStringEncoder } from '@solana/codecs-strings';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __brand: unique symbol;\n};\n\nlet memoizedBase58Encoder: Encoder<string> | undefined;\nlet memoizedBase58Decoder: Decoder<string> | undefined;\n\nfunction getMemoizedBase58Encoder(): Encoder<string> {\n if (!memoizedBase58Encoder) memoizedBase58Encoder = getBase58Encoder();\n return memoizedBase58Encoder;\n}\n\nfunction getMemoizedBase58Decoder(): Decoder<string> {\n if (!memoizedBase58Decoder) memoizedBase58Decoder = getBase58Decoder();\n return memoizedBase58Decoder;\n}\n\nexport function isAddress(\n putativeBase58EncodedAddress: string\n): putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n return false;\n }\n return true;\n}\n\nexport function assertIsAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n\nexport function address<TAddress extends string = string>(\n putativeBase58EncodedAddress: TAddress\n): Base58EncodedAddress<TAddress> {\n assertIsAddress(putativeBase58EncodedAddress);\n return putativeBase58EncodedAddress as Base58EncodedAddress<TAddress>;\n}\n\nexport function getAddressEncoder(config?: Readonly<{ description: string }>): Encoder<Base58EncodedAddress> {\n return mapEncoder(\n getStringEncoder({\n description: config?.description ?? 'Base58EncodedAddress',\n encoding: getMemoizedBase58Encoder(),\n size: 32,\n }),\n putativeAddress => address(putativeAddress)\n );\n}\n\nexport function getAddressDecoder(config?: Readonly<{ description: string }>): Decoder<Base58EncodedAddress> {\n return getStringDecoder({\n description: config?.description ?? 'Base58EncodedAddress',\n encoding: getMemoizedBase58Decoder(),\n size: 32,\n }) as Decoder<Base58EncodedAddress>;\n}\n\nexport function getAddressCodec(config?: Readonly<{ description: string }>): Codec<Base58EncodedAddress> {\n return combineCodec(getAddressEncoder(config), getAddressDecoder(config));\n}\n\nexport function getAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n","import { assertDigestCapabilityIsAvailable } from '@solana/assertions';\n\nimport { assertIsAddress, Base58EncodedAddress, getAddressCodec, isAddress } from './address';\nimport { compressedPointBytesAreOnCurve } from './curve';\n\n/**\n * An address derived from a program address and a set of seeds.\n * It includes the bump seed used to derive the address and\n * ensure the address is not on the Ed25519 curve.\n */\nexport type ProgramDerivedAddress<TAddress extends string = string> = Readonly<\n [Base58EncodedAddress<TAddress>, ProgramDerivedAddressBump]\n>;\n\n/**\n * A number between 0 and 255, inclusive.\n */\nexport type ProgramDerivedAddressBump = number & {\n readonly __brand: unique symbol;\n};\n\n/**\n * Returns true if the input value is a program derived address.\n */\nexport function isProgramDerivedAddress<TAddress extends string = string>(\n value: unknown\n): value is ProgramDerivedAddress<TAddress> {\n return (\n Array.isArray(value) &&\n value.length === 2 &&\n typeof value[0] === 'string' &&\n typeof value[1] === 'number' &&\n value[1] >= 0 &&\n value[1] <= 255 &&\n isAddress(value[0])\n );\n}\n\n/**\n * Fails if the input value is not a program derived address.\n */\nexport function assertIsProgramDerivedAddress<TAddress extends string = string>(\n value: unknown\n): asserts value is ProgramDerivedAddress<TAddress> {\n const validFormat =\n Array.isArray(value) && value.length === 2 && typeof value[0] === 'string' && typeof value[1] === 'number';\n if (!validFormat) {\n // TODO: Coded error.\n throw new Error(\n `Expected given program derived address to have the following format: [Base58EncodedAddress, ProgramDerivedAddressBump].`\n );\n }\n if (value[1] < 0 || value[1] > 255) {\n // TODO: Coded error.\n throw new Error(`Expected program derived address bump to be in the range [0, 255], got: ${value[1]}.`);\n }\n assertIsAddress(value[0]);\n}\n\ntype ProgramDerivedAddressInput = Readonly<{\n programAddress: Base58EncodedAddress;\n seeds: Seed[];\n}>;\n\ntype SeedInput = Readonly<{\n baseAddress: Base58EncodedAddress;\n programAddress: Base58EncodedAddress;\n seed: Seed;\n}>;\n\ntype Seed = string | Uint8Array;\n\nconst MAX_SEED_LENGTH = 32;\nconst MAX_SEEDS = 16;\nconst PDA_MARKER_BYTES = [\n // The string 'ProgramDerivedAddress'\n 80, 114, 111, 103, 114, 97, 109, 68, 101, 114, 105, 118, 101, 100, 65, 100, 100, 114, 101, 115, 115,\n] as const;\n\n// TODO: Coded error.\nclass PointOnCurveError extends Error {}\n\nasync function createProgramDerivedAddress({\n programAddress,\n seeds,\n}: ProgramDerivedAddressInput): Promise<Base58EncodedAddress> {\n await assertDigestCapabilityIsAvailable();\n if (seeds.length > MAX_SEEDS) {\n // TODO: Coded error.\n throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);\n }\n let textEncoder: TextEncoder;\n const seedBytes = seeds.reduce((acc, seed, ii) => {\n const bytes = typeof seed === 'string' ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;\n if (bytes.byteLength > MAX_SEED_LENGTH) {\n // TODO: Coded error.\n throw new Error(`The seed at index ${ii} exceeds the maximum length of 32 bytes`);\n }\n acc.push(...bytes);\n return acc;\n }, [] as number[]);\n const base58EncodedAddressCodec = getAddressCodec();\n const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES])\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n if (await compressedPointBytesAreOnCurve(addressBytes)) {\n // TODO: Coded error.\n throw new PointOnCurveError('Invalid seeds; point must fall off the Ed25519 curve');\n }\n return base58EncodedAddressCodec.decode(addressBytes)[0];\n}\n\nexport async function getProgramDerivedAddress({\n programAddress,\n seeds,\n}: ProgramDerivedAddressInput): Promise<ProgramDerivedAddress> {\n let bumpSeed = 255;\n while (bumpSeed > 0) {\n try {\n const address = await createProgramDerivedAddress({\n programAddress,\n seeds: [...seeds, new Uint8Array([bumpSeed])],\n });\n return [address, bumpSeed as ProgramDerivedAddressBump];\n } catch (e) {\n if (e instanceof PointOnCurveError) {\n bumpSeed--;\n } else {\n throw e;\n }\n }\n }\n // TODO: Coded error.\n throw new Error('Unable to find a viable program address bump seed');\n}\n\nexport async function createAddressWithSeed({\n baseAddress,\n programAddress,\n seed,\n}: SeedInput): Promise<Base58EncodedAddress> {\n const { encode, decode } = getAddressCodec();\n\n const seedBytes = typeof seed === 'string' ? new TextEncoder().encode(seed) : seed;\n if (seedBytes.byteLength > MAX_SEED_LENGTH) {\n // TODO: Coded error.\n throw new Error(`The seed exceeds the maximum length of 32 bytes`);\n }\n\n const programAddressBytes = encode(programAddress);\n if (\n programAddressBytes.length >= PDA_MARKER_BYTES.length &&\n programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])\n ) {\n // TODO: Coded error.\n throw new Error(`programAddress cannot end with the PDA marker`);\n }\n\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n\n return decode(addressBytes)[0];\n}\n","/**!\n * noble-ed25519\n *\n * The MIT License (MIT)\n *\n * Copyright (c) 2019 Paul Miller (https://paulmillr.com)\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the “Software”), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n */\nconst D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;\nconst P = 57896044618658097711785492504343953926634992332820282019728792003956564819949n; // 2n ** 255n - 19n; ed25519 is twisted edwards curve\nconst RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n; // √-1\n\n// mod division\nfunction mod(a: bigint): bigint {\n const r = a % P;\n return r >= 0n ? r : P + r;\n}\nfunction pow2(x: bigint, power: bigint): bigint {\n // pow2(x, 4) == x^(2^4)\n let r = x;\n while (power-- > 0n) {\n r *= r;\n r %= P;\n }\n return r;\n}\nfunction pow_2_252_3(x: bigint): bigint {\n // x^(2^252-3) unrolled util for square root\n const x2 = (x * x) % P; // x^2, bits 1\n const b2 = (x2 * x) % P; // x^3, bits 11\n const b4 = (pow2(b2, 2n) * b2) % P; // x^(2^4-1), bits 1111\n const b5 = (pow2(b4, 1n) * x) % P; // x^(2^5-1), bits 11111\n const b10 = (pow2(b5, 5n) * b5) % P; // x^(2^10)\n const b20 = (pow2(b10, 10n) * b10) % P; // x^(2^20)\n const b40 = (pow2(b20, 20n) * b20) % P; // x^(2^40)\n const b80 = (pow2(b40, 40n) * b40) % P; // x^(2^80)\n const b160 = (pow2(b80, 80n) * b80) % P; // x^(2^160)\n const b240 = (pow2(b160, 80n) * b80) % P; // x^(2^240)\n const b250 = (pow2(b240, 10n) * b10) % P; // x^(2^250)\n const pow_p_5_8 = (pow2(b250, 2n) * x) % P; // < To pow to (p+3)/8, multiply it by x.\n return pow_p_5_8;\n}\nfunction uvRatio(u: bigint, v: bigint): bigint | null {\n // for sqrt comp\n const v3 = mod(v * v * v); // v³\n const v7 = mod(v3 * v3 * v); // v⁷\n const pow = pow_2_252_3(u * v7); // (uv⁷)^(p-5)/8\n let x = mod(u * v3 * pow); // (uv³)(uv⁷)^(p-5)/8\n const vx2 = mod(v * x * x); // vx²\n const root1 = x; // First root candidate\n const root2 = mod(x * RM1); // Second root candidate; RM1 is √-1\n const useRoot1 = vx2 === u; // If vx² = u (mod p), x is a square root\n const useRoot2 = vx2 === mod(-u); // If vx² = -u, set x <-- x * 2^((p-1)/4)\n const noRoot = vx2 === mod(-u * RM1); // There is no valid root, vx² = -u√-1\n if (useRoot1) x = root1;\n if (useRoot2 || noRoot) x = root2; // We return root2 anyway, for const-time\n if ((mod(x) & 1n) === 1n) x = mod(-x); // edIsNegative\n if (!useRoot1 && !useRoot2) {\n return null;\n }\n return x;\n}\n// https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.3\nexport function pointIsOnCurve(y: bigint, lastByte: number): boolean {\n const y2 = mod(y * y); // y²\n const u = mod(y2 - 1n); // u=y²-1\n const v = mod(D * y2 + 1n);\n const x = uvRatio(u, v); // (uv³)(uv⁷)^(p-5)/8; square root\n if (x === null) {\n return false;\n }\n const isLastByteOdd = (lastByte & 0x80) !== 0; // x_0, last bit\n if (x === 0n && isLastByteOdd) {\n return false;\n }\n return true;\n}\n","import { pointIsOnCurve } from './vendor/noble/ed25519';\n\nfunction byteToHex(byte: number): string {\n const hexString = byte.toString(16);\n if (hexString.length === 1) {\n return `0${hexString}`;\n } else {\n return hexString;\n }\n}\n\nfunction decompressPointBytes(bytes: Uint8Array): bigint {\n const hexString = bytes.reduce((acc, byte, ii) => `${byteToHex(ii === 31 ? byte & ~0x80 : byte)}${acc}`, '');\n const integerLiteralString = `0x${hexString}`;\n return BigInt(integerLiteralString);\n}\n\nexport async function compressedPointBytesAreOnCurve(bytes: Uint8Array): Promise<boolean> {\n if (bytes.byteLength !== 32) {\n return false;\n }\n const y = decompressPointBytes(bytes);\n return pointIsOnCurve(y, bytes[31]);\n}\n","import { assertKeyExporterIsAvailable } from '@solana/assertions';\n\nimport { Base58EncodedAddress, getAddressDecoder } from './address';\n\nexport async function getAddressFromPublicKey(publicKey: CryptoKey): Promise<Base58EncodedAddress> {\n await assertKeyExporterIsAvailable();\n if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'Ed25519') {\n // TODO: Coded error.\n throw new Error('The `CryptoKey` must be an `Ed25519` public key');\n }\n const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);\n const [base58EncodedAddress] = getAddressDecoder().decode(new Uint8Array(publicKeyBytes));\n return base58EncodedAddress;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/address.ts","../src/program-derived-address.ts","../src/vendor/noble/ed25519.ts","../src/curve.ts","../src/public-key.ts"],"names":["SolanaError","address"],"mappings":";AAAA;AAAA,EACI;AAAA,EAMA;AAAA,OACG;AACP,SAAS,kBAAkB,kBAAkB,kBAAkB,wBAAwB;AACvF;AAAA,EACI;AAAA,EACA;AAAA,EACA;AAAA,OACG;AAMP,IAAI;AACJ,IAAI;AAEJ,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEA,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEO,SAAS,UAAU,iBAA6E;AAEnG;AAAA;AAAA,IAEI,gBAAgB,SAAS;AAAA,IAEzB,gBAAgB,SAAS;AAAA,IAC3B;AACE,WAAO;AAAA,EACX;AAEA,QAAM,gBAAgB,yBAAyB;AAC/C,QAAM,QAAQ,cAAc,OAAO,eAAe;AAClD,QAAM,WAAW,MAAM;AACvB,MAAI,aAAa,IAAI;AACjB,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEO,SAAS,gBAAgB,iBAAqF;AAEjH;AAAA;AAAA,IAEI,gBA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oBAAoB,MAAM,CAAC,iBAAiB,MAAM,EAAE,MAAM,CAAC,MAAM,UAAU,SAAS,iBAAiB,KAAK,CAAC,GAC7G;AACE,UAAM,IAAIA,aAAY,iDAAiD;AAAA,EAC3E;AAEA,QAAM,qBAAqB,MAAM,OAAO,OAAO;AAAA,IAC3C;AAAA,IACA,IAAI,WAAW,CAAC,GAAG,OAAO,WAAW,GAAG,GAAG,WAAW,GAAG,mBAAmB,CAAC;AAAA,EACjF;AACA,QAAM,eAAe,IAAI,WAAW,kBAAkB;AAEtD,SAAO,OAAO,YAAY;AAC9B;;;AG5KA,SAAS,oCAAoC;AAC7C,SAAS,qDAAqD,eAAAA,oBAAmB;AAIjF,eAAsB,wBAAwB,WAAwC;AAClF,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AACvE,UAAM,IAAIA,aAAY,mDAAmD;AAAA,EAC7E;AACA,QAAM,iBAAiB,MAAM,OAAO,OAAO,UAAU,OAAO,SAAS;AACrE,SAAO,kBAAkB,EAAE,OAAO,IAAI,WAAW,cAAc,CAAC;AACpE","sourcesContent":["import {\n combineCodec,\n Decoder,\n Encoder,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n mapEncoder,\n} from '@solana/codecs-core';\nimport { getBase58Decoder, getBase58Encoder, getStringDecoder, getStringEncoder } from '@solana/codecs-strings';\nimport {\n SOLANA_ERROR__ADDRESSES__INVALID_BYTE_LENGTH,\n SOLANA_ERROR__ADDRESSES__STRING_LENGTH_OUT_OF_RANGE,\n SolanaError,\n} from '@solana/errors';\n\nexport type Address<TAddress extends string = string> = TAddress & {\n readonly __brand: unique symbol;\n};\n\nlet memoizedBase58Encoder: Encoder<string> | undefined;\nlet memoizedBase58Decoder: Decoder<string> | undefined;\n\nfunction getMemoizedBase58Encoder(): Encoder<string> {\n if (!memoizedBase58Encoder) memoizedBase58Encoder = getBase58Encoder();\n return memoizedBase58Encoder;\n}\n\nfunction getMemoizedBase58Decoder(): Decoder<string> {\n if (!memoizedBase58Decoder) memoizedBase58Decoder = getBase58Decoder();\n return memoizedBase58Decoder;\n}\n\nexport function isAddress(putativeAddress: string): putativeAddress is Address<typeof putativeAddress> {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeAddress.length > 44\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n return false;\n }\n return true;\n}\n\nexport function assertIsAddress(putativeAddress: string): asserts putativeAddress is Address<typeof putativeAddress> {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeAddress.length > 44\n ) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__STRING_LENGTH_OUT_OF_RANGE, {\n actualLength: putativeAddress.length,\n });\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_BYTE_LENGTH, {\n actualLength: numBytes,\n });\n }\n}\n\nexport function address<TAddress extends string = string>(putativeAddress: TAddress): Address<TAddress> {\n assertIsAddress(putativeAddress);\n return putativeAddress as Address<TAddress>;\n}\n\nexport function getAddressEncoder(): FixedSizeEncoder<Address, 32> {\n return mapEncoder(getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }), putativeAddress =>\n address(putativeAddress),\n );\n}\n\nexport function getAddressDecoder(): FixedSizeDecoder<Address, 32> {\n return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 }) as FixedSizeDecoder<Address, 32>;\n}\n\nexport function getAddressCodec(): FixedSizeCodec<Address, Address, 32> {\n return combineCodec(getAddressEncoder(), getAddressDecoder());\n}\n\nexport function getAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n","import { assertDigestCapabilityIsAvailable } from '@solana/assertions';\nimport {\n isSolanaError,\n SOLANA_ERROR__ADDRESSES__FAILED_TO_FIND_VIABLE_PDA_BUMP_SEED,\n SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE,\n SOLANA_ERROR__ADDRESSES__MALFORMED_PDA,\n SOLANA_ERROR__ADDRESSES__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED,\n SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED,\n SOLANA_ERROR__ADDRESSES__PDA_BUMP_SEED_OUT_OF_RANGE,\n SOLANA_ERROR__ADDRESSES__PDA_ENDS_WITH_PDA_MARKER,\n SolanaError,\n} from '@solana/errors';\n\nimport { Address, assertIsAddress, getAddressCodec, isAddress } from './address';\nimport { compressedPointBytesAreOnCurve } from './curve';\n\n/**\n * An address derived from a program address and a set of seeds.\n * It includes the bump seed used to derive the address and\n * ensure the address is not on the Ed25519 curve.\n */\nexport type ProgramDerivedAddress<TAddress extends string = string> = Readonly<\n [Address<TAddress>, ProgramDerivedAddressBump]\n>;\n\n/**\n * A number between 0 and 255, inclusive.\n */\nexport type ProgramDerivedAddressBump = number & {\n readonly __brand: unique symbol;\n};\n\n/**\n * Returns true if the input value is a program derived address.\n */\nexport function isProgramDerivedAddress<TAddress extends string = string>(\n value: unknown,\n): value is ProgramDerivedAddress<TAddress> {\n return (\n Array.isArray(value) &&\n value.length === 2 &&\n typeof value[0] === 'string' &&\n typeof value[1] === 'number' &&\n value[1] >= 0 &&\n value[1] <= 255 &&\n isAddress(value[0])\n );\n}\n\n/**\n * Fails if the input value is not a program derived address.\n */\nexport function assertIsProgramDerivedAddress<TAddress extends string = string>(\n value: unknown,\n): asserts value is ProgramDerivedAddress<TAddress> {\n const validFormat =\n Array.isArray(value) && value.length === 2 && typeof value[0] === 'string' && typeof value[1] === 'number';\n if (!validFormat) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__MALFORMED_PDA);\n }\n if (value[1] < 0 || value[1] > 255) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__PDA_BUMP_SEED_OUT_OF_RANGE, {\n bump: value[1],\n });\n }\n assertIsAddress(value[0]);\n}\n\ntype ProgramDerivedAddressInput = Readonly<{\n programAddress: Address;\n seeds: Seed[];\n}>;\n\ntype SeedInput = Readonly<{\n baseAddress: Address;\n programAddress: Address;\n seed: Seed;\n}>;\n\ntype Seed = string | Uint8Array;\n\nconst MAX_SEED_LENGTH = 32;\nconst MAX_SEEDS = 16;\nconst PDA_MARKER_BYTES = [\n // The string 'ProgramDerivedAddress'\n 80, 114, 111, 103, 114, 97, 109, 68, 101, 114, 105, 118, 101, 100, 65, 100, 100, 114, 101, 115, 115,\n] as const;\n\nasync function createProgramDerivedAddress({ programAddress, seeds }: ProgramDerivedAddressInput): Promise<Address> {\n await assertDigestCapabilityIsAvailable();\n if (seeds.length > MAX_SEEDS) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED, {\n actual: seeds.length,\n maxSeeds: MAX_SEEDS,\n });\n }\n let textEncoder: TextEncoder;\n const seedBytes = seeds.reduce((acc, seed, ii) => {\n const bytes = typeof seed === 'string' ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;\n if (bytes.byteLength > MAX_SEED_LENGTH) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, {\n actual: bytes.byteLength,\n index: ii,\n maxSeedLength: MAX_SEED_LENGTH,\n });\n }\n acc.push(...bytes);\n return acc;\n }, [] as number[]);\n const base58EncodedAddressCodec = getAddressCodec();\n const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES]),\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n if (await compressedPointBytesAreOnCurve(addressBytes)) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE);\n }\n return base58EncodedAddressCodec.decode(addressBytes);\n}\n\nexport async function getProgramDerivedAddress({\n programAddress,\n seeds,\n}: ProgramDerivedAddressInput): Promise<ProgramDerivedAddress> {\n let bumpSeed = 255;\n while (bumpSeed > 0) {\n try {\n const address = await createProgramDerivedAddress({\n programAddress,\n seeds: [...seeds, new Uint8Array([bumpSeed])],\n });\n return [address, bumpSeed as ProgramDerivedAddressBump];\n } catch (e) {\n if (isSolanaError(e, SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE)) {\n bumpSeed--;\n } else {\n throw e;\n }\n }\n }\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__FAILED_TO_FIND_VIABLE_PDA_BUMP_SEED);\n}\n\nexport async function createAddressWithSeed({ baseAddress, programAddress, seed }: SeedInput): Promise<Address> {\n const { encode, decode } = getAddressCodec();\n\n const seedBytes = typeof seed === 'string' ? new TextEncoder().encode(seed) : seed;\n if (seedBytes.byteLength > MAX_SEED_LENGTH) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, {\n actual: seedBytes.byteLength,\n index: 0,\n maxSeedLength: MAX_SEED_LENGTH,\n });\n }\n\n const programAddressBytes = encode(programAddress);\n if (\n programAddressBytes.length >= PDA_MARKER_BYTES.length &&\n programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])\n ) {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__PDA_ENDS_WITH_PDA_MARKER);\n }\n\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes]),\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n\n return decode(addressBytes);\n}\n","/**!\n * noble-ed25519\n *\n * The MIT License (MIT)\n *\n * Copyright (c) 2019 Paul Miller (https://paulmillr.com)\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the “Software”), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n */\nconst D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;\nconst P = 57896044618658097711785492504343953926634992332820282019728792003956564819949n; // 2n ** 255n - 19n; ed25519 is twisted edwards curve\nconst RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n; // √-1\n\n// mod division\nfunction mod(a: bigint): bigint {\n const r = a % P;\n return r >= 0n ? r : P + r;\n}\nfunction pow2(x: bigint, power: bigint): bigint {\n // pow2(x, 4) == x^(2^4)\n let r = x;\n while (power-- > 0n) {\n r *= r;\n r %= P;\n }\n return r;\n}\nfunction pow_2_252_3(x: bigint): bigint {\n // x^(2^252-3) unrolled util for square root\n const x2 = (x * x) % P; // x^2, bits 1\n const b2 = (x2 * x) % P; // x^3, bits 11\n const b4 = (pow2(b2, 2n) * b2) % P; // x^(2^4-1), bits 1111\n const b5 = (pow2(b4, 1n) * x) % P; // x^(2^5-1), bits 11111\n const b10 = (pow2(b5, 5n) * b5) % P; // x^(2^10)\n const b20 = (pow2(b10, 10n) * b10) % P; // x^(2^20)\n const b40 = (pow2(b20, 20n) * b20) % P; // x^(2^40)\n const b80 = (pow2(b40, 40n) * b40) % P; // x^(2^80)\n const b160 = (pow2(b80, 80n) * b80) % P; // x^(2^160)\n const b240 = (pow2(b160, 80n) * b80) % P; // x^(2^240)\n const b250 = (pow2(b240, 10n) * b10) % P; // x^(2^250)\n const pow_p_5_8 = (pow2(b250, 2n) * x) % P; // < To pow to (p+3)/8, multiply it by x.\n return pow_p_5_8;\n}\nfunction uvRatio(u: bigint, v: bigint): bigint | null {\n // for sqrt comp\n const v3 = mod(v * v * v); // v³\n const v7 = mod(v3 * v3 * v); // v⁷\n const pow = pow_2_252_3(u * v7); // (uv⁷)^(p-5)/8\n let x = mod(u * v3 * pow); // (uv³)(uv⁷)^(p-5)/8\n const vx2 = mod(v * x * x); // vx²\n const root1 = x; // First root candidate\n const root2 = mod(x * RM1); // Second root candidate; RM1 is √-1\n const useRoot1 = vx2 === u; // If vx² = u (mod p), x is a square root\n const useRoot2 = vx2 === mod(-u); // If vx² = -u, set x <-- x * 2^((p-1)/4)\n const noRoot = vx2 === mod(-u * RM1); // There is no valid root, vx² = -u√-1\n if (useRoot1) x = root1;\n if (useRoot2 || noRoot) x = root2; // We return root2 anyway, for const-time\n if ((mod(x) & 1n) === 1n) x = mod(-x); // edIsNegative\n if (!useRoot1 && !useRoot2) {\n return null;\n }\n return x;\n}\n// https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.3\nexport function pointIsOnCurve(y: bigint, lastByte: number): boolean {\n const y2 = mod(y * y); // y²\n const u = mod(y2 - 1n); // u=y²-1\n const v = mod(D * y2 + 1n);\n const x = uvRatio(u, v); // (uv³)(uv⁷)^(p-5)/8; square root\n if (x === null) {\n return false;\n }\n const isLastByteOdd = (lastByte & 0x80) !== 0; // x_0, last bit\n if (x === 0n && isLastByteOdd) {\n return false;\n }\n return true;\n}\n","import { pointIsOnCurve } from './vendor/noble/ed25519';\n\nfunction byteToHex(byte: number): string {\n const hexString = byte.toString(16);\n if (hexString.length === 1) {\n return `0${hexString}`;\n } else {\n return hexString;\n }\n}\n\nfunction decompressPointBytes(bytes: Uint8Array): bigint {\n const hexString = bytes.reduce((acc, byte, ii) => `${byteToHex(ii === 31 ? byte & ~0x80 : byte)}${acc}`, '');\n const integerLiteralString = `0x${hexString}`;\n return BigInt(integerLiteralString);\n}\n\nexport async function compressedPointBytesAreOnCurve(bytes: Uint8Array): Promise<boolean> {\n if (bytes.byteLength !== 32) {\n return false;\n }\n const y = decompressPointBytes(bytes);\n return pointIsOnCurve(y, bytes[31]);\n}\n","import { assertKeyExporterIsAvailable } from '@solana/assertions';\nimport { SOLANA_ERROR__ADDRESSES__INVALID_ED25519_PUBLIC_KEY, SolanaError } from '@solana/errors';\n\nimport { Address, getAddressDecoder } from './address';\n\nexport async function getAddressFromPublicKey(publicKey: CryptoKey): Promise<Address> {\n await assertKeyExporterIsAvailable();\n if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'Ed25519') {\n throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_ED25519_PUBLIC_KEY);\n }\n const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);\n return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));\n}\n"]}
|
package/dist/index.native.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { mapEncoder, combineCodec } from '@solana/codecs-core';
|
|
2
2
|
import { getStringEncoder, getStringDecoder, getBase58Encoder, getBase58Decoder } from '@solana/codecs-strings';
|
|
3
|
+
import { SolanaError, SOLANA_ERROR__ADDRESSES__STRING_LENGTH_OUT_OF_RANGE, SOLANA_ERROR__ADDRESSES__INVALID_BYTE_LENGTH, SOLANA_ERROR__ADDRESSES__MALFORMED_PDA, SOLANA_ERROR__ADDRESSES__PDA_BUMP_SEED_OUT_OF_RANGE, isSolanaError, SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE, SOLANA_ERROR__ADDRESSES__FAILED_TO_FIND_VIABLE_PDA_BUMP_SEED, SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, SOLANA_ERROR__ADDRESSES__PDA_ENDS_WITH_PDA_MARKER, SOLANA_ERROR__ADDRESSES__INVALID_ED25519_PUBLIC_KEY, SOLANA_ERROR__ADDRESSES__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED } from '@solana/errors';
|
|
3
4
|
import { assertKeyExporterIsAvailable, assertDigestCapabilityIsAvailable } from '@solana/assertions';
|
|
4
5
|
|
|
5
6
|
// src/address.ts
|
|
@@ -15,66 +16,56 @@ function getMemoizedBase58Decoder() {
|
|
|
15
16
|
memoizedBase58Decoder = getBase58Decoder();
|
|
16
17
|
return memoizedBase58Decoder;
|
|
17
18
|
}
|
|
18
|
-
function isAddress(
|
|
19
|
+
function isAddress(putativeAddress) {
|
|
19
20
|
if (
|
|
20
21
|
// Lowest address (32 bytes of zeroes)
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
23
|
+
putativeAddress.length > 44
|
|
23
24
|
) {
|
|
24
25
|
return false;
|
|
25
26
|
}
|
|
26
27
|
const base58Encoder = getMemoizedBase58Encoder();
|
|
27
|
-
const bytes = base58Encoder.encode(
|
|
28
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
28
29
|
const numBytes = bytes.byteLength;
|
|
29
30
|
if (numBytes !== 32) {
|
|
30
31
|
return false;
|
|
31
32
|
}
|
|
32
33
|
return true;
|
|
33
34
|
}
|
|
34
|
-
function assertIsAddress(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
throw new Error(`\`${putativeBase58EncodedAddress}\` is not a base-58 encoded address`, {
|
|
51
|
-
cause: e
|
|
35
|
+
function assertIsAddress(putativeAddress) {
|
|
36
|
+
if (
|
|
37
|
+
// Lowest address (32 bytes of zeroes)
|
|
38
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
39
|
+
putativeAddress.length > 44
|
|
40
|
+
) {
|
|
41
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__STRING_LENGTH_OUT_OF_RANGE, {
|
|
42
|
+
actualLength: putativeAddress.length
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const base58Encoder = getMemoizedBase58Encoder();
|
|
46
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
47
|
+
const numBytes = bytes.byteLength;
|
|
48
|
+
if (numBytes !== 32) {
|
|
49
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_BYTE_LENGTH, {
|
|
50
|
+
actualLength: numBytes
|
|
52
51
|
});
|
|
53
52
|
}
|
|
54
53
|
}
|
|
55
|
-
function address(
|
|
56
|
-
assertIsAddress(
|
|
57
|
-
return
|
|
54
|
+
function address(putativeAddress) {
|
|
55
|
+
assertIsAddress(putativeAddress);
|
|
56
|
+
return putativeAddress;
|
|
58
57
|
}
|
|
59
|
-
function getAddressEncoder(
|
|
58
|
+
function getAddressEncoder() {
|
|
60
59
|
return mapEncoder(
|
|
61
|
-
getStringEncoder({
|
|
62
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
63
|
-
encoding: getMemoizedBase58Encoder(),
|
|
64
|
-
size: 32
|
|
65
|
-
}),
|
|
60
|
+
getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }),
|
|
66
61
|
(putativeAddress) => address(putativeAddress)
|
|
67
62
|
);
|
|
68
63
|
}
|
|
69
|
-
function getAddressDecoder(
|
|
70
|
-
return getStringDecoder({
|
|
71
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
72
|
-
encoding: getMemoizedBase58Decoder(),
|
|
73
|
-
size: 32
|
|
74
|
-
});
|
|
64
|
+
function getAddressDecoder() {
|
|
65
|
+
return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 });
|
|
75
66
|
}
|
|
76
|
-
function getAddressCodec(
|
|
77
|
-
return combineCodec(getAddressEncoder(
|
|
67
|
+
function getAddressCodec() {
|
|
68
|
+
return combineCodec(getAddressEncoder(), getAddressDecoder());
|
|
78
69
|
}
|
|
79
70
|
function getAddressComparator() {
|
|
80
71
|
return new Intl.Collator("en", {
|
|
@@ -184,12 +175,12 @@ function isProgramDerivedAddress(value) {
|
|
|
184
175
|
function assertIsProgramDerivedAddress(value) {
|
|
185
176
|
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
186
177
|
if (!validFormat) {
|
|
187
|
-
throw new
|
|
188
|
-
`Expected given program derived address to have the following format: [Base58EncodedAddress, ProgramDerivedAddressBump].`
|
|
189
|
-
);
|
|
178
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MALFORMED_PDA);
|
|
190
179
|
}
|
|
191
180
|
if (value[1] < 0 || value[1] > 255) {
|
|
192
|
-
throw new
|
|
181
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__PDA_BUMP_SEED_OUT_OF_RANGE, {
|
|
182
|
+
bump: value[1]
|
|
183
|
+
});
|
|
193
184
|
}
|
|
194
185
|
assertIsAddress(value[0]);
|
|
195
186
|
}
|
|
@@ -219,21 +210,23 @@ var PDA_MARKER_BYTES = [
|
|
|
219
210
|
115,
|
|
220
211
|
115
|
|
221
212
|
];
|
|
222
|
-
|
|
223
|
-
};
|
|
224
|
-
async function createProgramDerivedAddress({
|
|
225
|
-
programAddress,
|
|
226
|
-
seeds
|
|
227
|
-
}) {
|
|
213
|
+
async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
228
214
|
await assertDigestCapabilityIsAvailable();
|
|
229
215
|
if (seeds.length > MAX_SEEDS) {
|
|
230
|
-
throw new
|
|
216
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED, {
|
|
217
|
+
actual: seeds.length,
|
|
218
|
+
maxSeeds: MAX_SEEDS
|
|
219
|
+
});
|
|
231
220
|
}
|
|
232
221
|
let textEncoder;
|
|
233
222
|
const seedBytes = seeds.reduce((acc, seed, ii) => {
|
|
234
|
-
const bytes = typeof seed === "string" ? (textEncoder
|
|
223
|
+
const bytes = typeof seed === "string" ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;
|
|
235
224
|
if (bytes.byteLength > MAX_SEED_LENGTH) {
|
|
236
|
-
throw new
|
|
225
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
226
|
+
actual: bytes.byteLength,
|
|
227
|
+
index: ii,
|
|
228
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
229
|
+
});
|
|
237
230
|
}
|
|
238
231
|
acc.push(...bytes);
|
|
239
232
|
return acc;
|
|
@@ -246,9 +239,9 @@ async function createProgramDerivedAddress({
|
|
|
246
239
|
);
|
|
247
240
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
248
241
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
249
|
-
throw new
|
|
242
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE);
|
|
250
243
|
}
|
|
251
|
-
return base58EncodedAddressCodec.decode(addressBytes)
|
|
244
|
+
return base58EncodedAddressCodec.decode(addressBytes);
|
|
252
245
|
}
|
|
253
246
|
async function getProgramDerivedAddress({
|
|
254
247
|
programAddress,
|
|
@@ -263,44 +256,43 @@ async function getProgramDerivedAddress({
|
|
|
263
256
|
});
|
|
264
257
|
return [address2, bumpSeed];
|
|
265
258
|
} catch (e) {
|
|
266
|
-
if (e
|
|
259
|
+
if (isSolanaError(e, SOLANA_ERROR__ADDRESSES__INVALID_SEEDS_POINT_ON_CURVE)) {
|
|
267
260
|
bumpSeed--;
|
|
268
261
|
} else {
|
|
269
262
|
throw e;
|
|
270
263
|
}
|
|
271
264
|
}
|
|
272
265
|
}
|
|
273
|
-
throw new
|
|
266
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__FAILED_TO_FIND_VIABLE_PDA_BUMP_SEED);
|
|
274
267
|
}
|
|
275
|
-
async function createAddressWithSeed({
|
|
276
|
-
baseAddress,
|
|
277
|
-
programAddress,
|
|
278
|
-
seed
|
|
279
|
-
}) {
|
|
268
|
+
async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
280
269
|
const { encode, decode } = getAddressCodec();
|
|
281
270
|
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
282
271
|
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
283
|
-
throw new
|
|
272
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
273
|
+
actual: seedBytes.byteLength,
|
|
274
|
+
index: 0,
|
|
275
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
276
|
+
});
|
|
284
277
|
}
|
|
285
278
|
const programAddressBytes = encode(programAddress);
|
|
286
279
|
if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
|
|
287
|
-
throw new
|
|
280
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__PDA_ENDS_WITH_PDA_MARKER);
|
|
288
281
|
}
|
|
289
282
|
const addressBytesBuffer = await crypto.subtle.digest(
|
|
290
283
|
"SHA-256",
|
|
291
284
|
new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
|
|
292
285
|
);
|
|
293
286
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
294
|
-
return decode(addressBytes)
|
|
287
|
+
return decode(addressBytes);
|
|
295
288
|
}
|
|
296
289
|
async function getAddressFromPublicKey(publicKey) {
|
|
297
290
|
await assertKeyExporterIsAvailable();
|
|
298
291
|
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
299
|
-
throw new
|
|
292
|
+
throw new SolanaError(SOLANA_ERROR__ADDRESSES__INVALID_ED25519_PUBLIC_KEY);
|
|
300
293
|
}
|
|
301
294
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
302
|
-
|
|
303
|
-
return base58EncodedAddress;
|
|
295
|
+
return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
304
296
|
}
|
|
305
297
|
|
|
306
298
|
export { address, assertIsAddress, assertIsProgramDerivedAddress, createAddressWithSeed, getAddressCodec, getAddressComparator, getAddressDecoder, getAddressEncoder, getAddressFromPublicKey, getProgramDerivedAddress, isAddress, isProgramDerivedAddress };
|