@solana/addresses 2.0.0-experimental.f8c941c → 2.0.0-experimental.fafdfb2
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/dist/index.browser.cjs +39 -31
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +39 -31
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +39 -31
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +39 -31
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +39 -31
- package/dist/index.node.js.map +1 -1
- package/dist/types/address.d.ts.map +1 -1
- package/dist/types/program-derived-address.d.ts.map +1 -1
- package/dist/types/public-key.d.ts.map +1 -1
- package/package.json +13 -33
package/dist/index.browser.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var codecsCore = require('@solana/codecs-core');
|
|
4
4
|
var codecsStrings = require('@solana/codecs-strings');
|
|
5
|
+
var errors = require('@solana/errors');
|
|
5
6
|
var assertions = require('@solana/assertions');
|
|
6
7
|
|
|
7
8
|
// src/address.ts
|
|
@@ -34,23 +35,21 @@ function isAddress(putativeAddress) {
|
|
|
34
35
|
return true;
|
|
35
36
|
}
|
|
36
37
|
function assertIsAddress(putativeAddress) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
throw new Error(`\`${putativeAddress}\` is not a base-58 encoded address`, {
|
|
53
|
-
cause: e
|
|
38
|
+
if (
|
|
39
|
+
// Lowest address (32 bytes of zeroes)
|
|
40
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
41
|
+
putativeAddress.length > 44
|
|
42
|
+
) {
|
|
43
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__ADDRESS_STRING_LENGTH_OUT_OF_RANGE, {
|
|
44
|
+
actualLength: putativeAddress.length
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const base58Encoder = getMemoizedBase58Encoder();
|
|
48
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
49
|
+
const numBytes = bytes.byteLength;
|
|
50
|
+
if (numBytes !== 32) {
|
|
51
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__ADDRESS_BYTE_LENGTH_OUT_OF_RANGE, {
|
|
52
|
+
actualLength: numBytes
|
|
54
53
|
});
|
|
55
54
|
}
|
|
56
55
|
}
|
|
@@ -178,12 +177,12 @@ function isProgramDerivedAddress(value) {
|
|
|
178
177
|
function assertIsProgramDerivedAddress(value) {
|
|
179
178
|
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
180
179
|
if (!validFormat) {
|
|
181
|
-
throw new
|
|
182
|
-
`Expected given program derived address to have the following format: [Address, ProgramDerivedAddressBump].`
|
|
183
|
-
);
|
|
180
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__MALFORMED_PROGRAM_DERIVED_ADDRESS);
|
|
184
181
|
}
|
|
185
182
|
if (value[1] < 0 || value[1] > 255) {
|
|
186
|
-
throw new
|
|
183
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__PROGRAM_DERIVED_ADDRESS_BUMP_SEED_OUT_OF_RANGE, {
|
|
184
|
+
bump: value[1]
|
|
185
|
+
});
|
|
187
186
|
}
|
|
188
187
|
assertIsAddress(value[0]);
|
|
189
188
|
}
|
|
@@ -213,18 +212,23 @@ var PDA_MARKER_BYTES = [
|
|
|
213
212
|
115,
|
|
214
213
|
115
|
|
215
214
|
];
|
|
216
|
-
var PointOnCurveError = class extends Error {
|
|
217
|
-
};
|
|
218
215
|
async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
219
216
|
await assertions.assertDigestCapabilityIsAvailable();
|
|
220
217
|
if (seeds.length > MAX_SEEDS) {
|
|
221
|
-
throw new
|
|
218
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED, {
|
|
219
|
+
actual: seeds.length,
|
|
220
|
+
maxSeeds: MAX_SEEDS
|
|
221
|
+
});
|
|
222
222
|
}
|
|
223
223
|
let textEncoder;
|
|
224
224
|
const seedBytes = seeds.reduce((acc, seed, ii) => {
|
|
225
225
|
const bytes = typeof seed === "string" ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;
|
|
226
226
|
if (bytes.byteLength > MAX_SEED_LENGTH) {
|
|
227
|
-
throw new
|
|
227
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
228
|
+
actual: bytes.byteLength,
|
|
229
|
+
index: ii,
|
|
230
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
231
|
+
});
|
|
228
232
|
}
|
|
229
233
|
acc.push(...bytes);
|
|
230
234
|
return acc;
|
|
@@ -237,7 +241,7 @@ async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
|
237
241
|
);
|
|
238
242
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
239
243
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
240
|
-
throw new
|
|
244
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__INVALID_SEEDS_POINT_ON_CURVE);
|
|
241
245
|
}
|
|
242
246
|
return base58EncodedAddressCodec.decode(addressBytes);
|
|
243
247
|
}
|
|
@@ -254,24 +258,28 @@ async function getProgramDerivedAddress({
|
|
|
254
258
|
});
|
|
255
259
|
return [address2, bumpSeed];
|
|
256
260
|
} catch (e) {
|
|
257
|
-
if (e
|
|
261
|
+
if (errors.isSolanaError(e, errors.SOLANA_ERROR__INVALID_SEEDS_POINT_ON_CURVE)) {
|
|
258
262
|
bumpSeed--;
|
|
259
263
|
} else {
|
|
260
264
|
throw e;
|
|
261
265
|
}
|
|
262
266
|
}
|
|
263
267
|
}
|
|
264
|
-
throw new
|
|
268
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__COULD_NOT_FIND_VIABLE_PDA_BUMP_SEED);
|
|
265
269
|
}
|
|
266
270
|
async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
267
271
|
const { encode, decode } = getAddressCodec();
|
|
268
272
|
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
269
273
|
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
270
|
-
throw new
|
|
274
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
275
|
+
actual: seedBytes.byteLength,
|
|
276
|
+
index: 0,
|
|
277
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
278
|
+
});
|
|
271
279
|
}
|
|
272
280
|
const programAddressBytes = encode(programAddress);
|
|
273
281
|
if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
|
|
274
|
-
throw new
|
|
282
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__PROGRAM_ADDRESS_ENDS_WITH_PDA_MARKER);
|
|
275
283
|
}
|
|
276
284
|
const addressBytesBuffer = await crypto.subtle.digest(
|
|
277
285
|
"SHA-256",
|
|
@@ -283,7 +291,7 @@ async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
|
283
291
|
async function getAddressFromPublicKey(publicKey) {
|
|
284
292
|
await assertions.assertKeyExporterIsAvailable();
|
|
285
293
|
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
286
|
-
throw new
|
|
294
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__NOT_AN_ED25519_PUBLIC_KEY);
|
|
287
295
|
}
|
|
288
296
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
289
297
|
return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
@@ -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;AAAA,EACI;AAAA,EAMA;AAAA,OACG;AACP,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,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;AACjH,MAAI;AAEA;AAAA;AAAA,MAEI,gBAAgB,SAAS;AAAA,MAEzB,gBAAgB,SAAS;AAAA,MAC3B;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,gBAAgB,yBAAyB;AAC/C,UAAM,QAAQ,cAAc,OAAO,eAAe;AAClD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,QAAQ,EAAE;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAG;AACR,UAAM,IAAI,MAAM,KAAK,eAAe,uCAAuC;AAAA,MACvE,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,QAA0C,iBAA8C;AACpG,kBAAgB,eAAe;AAC/B,SAAO;AACX;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,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n mapEncoder,\n} from '@solana/codecs-core';\nimport { getBase58Decoder, getBase58Encoder, getStringDecoder, getStringEncoder } from '@solana/codecs-strings';\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 try {\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 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(putativeAddress);\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(`\\`${putativeAddress}\\` is not a base-58 encoded address`, {\n cause: e,\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';\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 // TODO: Coded error.\n throw new Error(\n `Expected given program derived address to have the following format: [Address, 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: 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\n// TODO: Coded error.\nclass PointOnCurveError extends Error {}\n\nasync function createProgramDerivedAddress({ programAddress, seeds }: ProgramDerivedAddressInput): Promise<Address> {\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);\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({ 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 // 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);\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 { 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 // 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 return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));\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,y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,UAAM,IAAIA,aAAY,4CAA4C;AAAA,MAC9D,QAAQ,UAAU;AAAA,MAClB,OAAO;AAAA,MACP,eAAe;AAAA,IACnB,CAAC;AAAA,EACL;AAEA,QAAM,sBAAsB,OAAO,cAAc;AACjD,MACI,oBAAoB,UAAU,iBAAiB,UAC/C,oBAAoB,MAAM,CAAC,iBAAiB,MAAM,EAAE,MAAM,CAAC,MAAM,UAAU,SAAS,iBAAiB,KAAK,CAAC,GAC7G;AACE,UAAM,IAAIA,aAAY,kDAAkD;AAAA,EAC5E;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,yCAAyC,eAAAA,oBAAmB;AAIrE,eAAsB,wBAAwB,WAAwC;AAClF,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AACvE,UAAM,IAAIA,aAAY,uCAAuC;AAAA,EACjE;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__ADDRESS_BYTE_LENGTH_OUT_OF_RANGE,\n SOLANA_ERROR__ADDRESS_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__ADDRESS_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__ADDRESS_BYTE_LENGTH_OUT_OF_RANGE, {\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__COULD_NOT_FIND_VIABLE_PDA_BUMP_SEED,\n SOLANA_ERROR__INVALID_SEEDS_POINT_ON_CURVE,\n SOLANA_ERROR__MALFORMED_PROGRAM_DERIVED_ADDRESS,\n SOLANA_ERROR__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED,\n SOLANA_ERROR__MAX_PDA_SEED_LENGTH_EXCEEDED,\n SOLANA_ERROR__PROGRAM_ADDRESS_ENDS_WITH_PDA_MARKER,\n SOLANA_ERROR__PROGRAM_DERIVED_ADDRESS_BUMP_SEED_OUT_OF_RANGE,\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__MALFORMED_PROGRAM_DERIVED_ADDRESS);\n }\n if (value[1] < 0 || value[1] > 255) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_DERIVED_ADDRESS_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__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__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__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__INVALID_SEEDS_POINT_ON_CURVE)) {\n bumpSeed--;\n } else {\n throw e;\n }\n }\n }\n throw new SolanaError(SOLANA_ERROR__COULD_NOT_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__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__PROGRAM_ADDRESS_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__NOT_AN_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__NOT_AN_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.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__ADDRESS_STRING_LENGTH_OUT_OF_RANGE, SOLANA_ERROR__ADDRESS_BYTE_LENGTH_OUT_OF_RANGE, SOLANA_ERROR__MALFORMED_PROGRAM_DERIVED_ADDRESS, SOLANA_ERROR__PROGRAM_DERIVED_ADDRESS_BUMP_SEED_OUT_OF_RANGE, isSolanaError, SOLANA_ERROR__INVALID_SEEDS_POINT_ON_CURVE, SOLANA_ERROR__COULD_NOT_FIND_VIABLE_PDA_BUMP_SEED, SOLANA_ERROR__MAX_PDA_SEED_LENGTH_EXCEEDED, SOLANA_ERROR__PROGRAM_ADDRESS_ENDS_WITH_PDA_MARKER, SOLANA_ERROR__NOT_AN_ED25519_PUBLIC_KEY, SOLANA_ERROR__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED } from '@solana/errors';
|
|
3
4
|
import { assertKeyExporterIsAvailable, assertDigestCapabilityIsAvailable } from '@solana/assertions';
|
|
4
5
|
|
|
5
6
|
// src/address.ts
|
|
@@ -32,23 +33,21 @@ function isAddress(putativeAddress) {
|
|
|
32
33
|
return true;
|
|
33
34
|
}
|
|
34
35
|
function assertIsAddress(putativeAddress) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
throw new Error(`\`${putativeAddress}\` is not a base-58 encoded address`, {
|
|
51
|
-
cause: e
|
|
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__ADDRESS_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__ADDRESS_BYTE_LENGTH_OUT_OF_RANGE, {
|
|
50
|
+
actualLength: numBytes
|
|
52
51
|
});
|
|
53
52
|
}
|
|
54
53
|
}
|
|
@@ -176,12 +175,12 @@ function isProgramDerivedAddress(value) {
|
|
|
176
175
|
function assertIsProgramDerivedAddress(value) {
|
|
177
176
|
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
178
177
|
if (!validFormat) {
|
|
179
|
-
throw new
|
|
180
|
-
`Expected given program derived address to have the following format: [Address, ProgramDerivedAddressBump].`
|
|
181
|
-
);
|
|
178
|
+
throw new SolanaError(SOLANA_ERROR__MALFORMED_PROGRAM_DERIVED_ADDRESS);
|
|
182
179
|
}
|
|
183
180
|
if (value[1] < 0 || value[1] > 255) {
|
|
184
|
-
throw new
|
|
181
|
+
throw new SolanaError(SOLANA_ERROR__PROGRAM_DERIVED_ADDRESS_BUMP_SEED_OUT_OF_RANGE, {
|
|
182
|
+
bump: value[1]
|
|
183
|
+
});
|
|
185
184
|
}
|
|
186
185
|
assertIsAddress(value[0]);
|
|
187
186
|
}
|
|
@@ -211,18 +210,23 @@ var PDA_MARKER_BYTES = [
|
|
|
211
210
|
115,
|
|
212
211
|
115
|
|
213
212
|
];
|
|
214
|
-
var PointOnCurveError = class extends Error {
|
|
215
|
-
};
|
|
216
213
|
async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
217
214
|
await assertDigestCapabilityIsAvailable();
|
|
218
215
|
if (seeds.length > MAX_SEEDS) {
|
|
219
|
-
throw new
|
|
216
|
+
throw new SolanaError(SOLANA_ERROR__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED, {
|
|
217
|
+
actual: seeds.length,
|
|
218
|
+
maxSeeds: MAX_SEEDS
|
|
219
|
+
});
|
|
220
220
|
}
|
|
221
221
|
let textEncoder;
|
|
222
222
|
const seedBytes = seeds.reduce((acc, seed, ii) => {
|
|
223
223
|
const bytes = typeof seed === "string" ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;
|
|
224
224
|
if (bytes.byteLength > MAX_SEED_LENGTH) {
|
|
225
|
-
throw new
|
|
225
|
+
throw new SolanaError(SOLANA_ERROR__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
226
|
+
actual: bytes.byteLength,
|
|
227
|
+
index: ii,
|
|
228
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
229
|
+
});
|
|
226
230
|
}
|
|
227
231
|
acc.push(...bytes);
|
|
228
232
|
return acc;
|
|
@@ -235,7 +239,7 @@ async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
|
235
239
|
);
|
|
236
240
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
237
241
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
238
|
-
throw new
|
|
242
|
+
throw new SolanaError(SOLANA_ERROR__INVALID_SEEDS_POINT_ON_CURVE);
|
|
239
243
|
}
|
|
240
244
|
return base58EncodedAddressCodec.decode(addressBytes);
|
|
241
245
|
}
|
|
@@ -252,24 +256,28 @@ async function getProgramDerivedAddress({
|
|
|
252
256
|
});
|
|
253
257
|
return [address2, bumpSeed];
|
|
254
258
|
} catch (e) {
|
|
255
|
-
if (e
|
|
259
|
+
if (isSolanaError(e, SOLANA_ERROR__INVALID_SEEDS_POINT_ON_CURVE)) {
|
|
256
260
|
bumpSeed--;
|
|
257
261
|
} else {
|
|
258
262
|
throw e;
|
|
259
263
|
}
|
|
260
264
|
}
|
|
261
265
|
}
|
|
262
|
-
throw new
|
|
266
|
+
throw new SolanaError(SOLANA_ERROR__COULD_NOT_FIND_VIABLE_PDA_BUMP_SEED);
|
|
263
267
|
}
|
|
264
268
|
async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
265
269
|
const { encode, decode } = getAddressCodec();
|
|
266
270
|
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
267
271
|
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
268
|
-
throw new
|
|
272
|
+
throw new SolanaError(SOLANA_ERROR__MAX_PDA_SEED_LENGTH_EXCEEDED, {
|
|
273
|
+
actual: seedBytes.byteLength,
|
|
274
|
+
index: 0,
|
|
275
|
+
maxSeedLength: MAX_SEED_LENGTH
|
|
276
|
+
});
|
|
269
277
|
}
|
|
270
278
|
const programAddressBytes = encode(programAddress);
|
|
271
279
|
if (programAddressBytes.length >= PDA_MARKER_BYTES.length && programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])) {
|
|
272
|
-
throw new
|
|
280
|
+
throw new SolanaError(SOLANA_ERROR__PROGRAM_ADDRESS_ENDS_WITH_PDA_MARKER);
|
|
273
281
|
}
|
|
274
282
|
const addressBytesBuffer = await crypto.subtle.digest(
|
|
275
283
|
"SHA-256",
|
|
@@ -281,7 +289,7 @@ async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
|
281
289
|
async function getAddressFromPublicKey(publicKey) {
|
|
282
290
|
await assertKeyExporterIsAvailable();
|
|
283
291
|
if (publicKey.type !== "public" || publicKey.algorithm.name !== "Ed25519") {
|
|
284
|
-
throw new
|
|
292
|
+
throw new SolanaError(SOLANA_ERROR__NOT_AN_ED25519_PUBLIC_KEY);
|
|
285
293
|
}
|
|
286
294
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
287
295
|
return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
@@ -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;AAAA,EACI;AAAA,EAMA;AAAA,OACG;AACP,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,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;AACjH,MAAI;AAEA;AAAA;AAAA,MAEI,gBAAgB,SAAS;AAAA,MAEzB,gBAAgB,SAAS;AAAA,MAC3B;AACE,YAAM,IAAI,MAAM,+DAA+D;AAAA,IACnF;AAEA,UAAM,gBAAgB,yBAAyB;AAC/C,UAAM,QAAQ,cAAc,OAAO,eAAe;AAClD,UAAM,WAAW,MAAM;AACvB,QAAI,aAAa,IAAI;AACjB,YAAM,IAAI,MAAM,gFAAgF,QAAQ,EAAE;AAAA,IAC9G;AAAA,EACJ,SAAS,GAAG;AACR,UAAM,IAAI,MAAM,KAAK,eAAe,uCAAuC;AAAA,MACvE,OAAO;AAAA,IACX,CAAC;AAAA,EACL;AACJ;AAEO,SAAS,QAA0C,iBAA8C;AACpG,kBAAgB,eAAe;AAC/B,SAAO;AACX;AAEO,SAAS,oBAAmD;AAC/D,SAAO;AAAA,IAAW,iBAAiB,EAAE,UAAU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solana/codecs-core';\nimport { getBase58Decoder, getBase58Encoder, getStringDecoder, getStringEncoder } from '@solana/codecs-strings';\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 try {\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 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(putativeAddress);\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(`\\`${putativeAddress}\\` is not a base-58 encoded address`, {\n cause: e,\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';\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 // TODO: Coded error.\n throw new Error(\n `Expected given program derived address to have the following format: [Address, 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: 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\n// TODO: Coded error.\nclass PointOnCurveError extends Error {}\n\nasync function createProgramDerivedAddress({ programAddress, seeds }: ProgramDerivedAddressInput): Promise<Address> {\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);\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({ 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 // 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);\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 { 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 // 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 return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));\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,M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B,OAAO;AAAA,MACP,eAAe;AAAA,IACnB,CAAC;AAAA,EACL;AAEA,QAAM,sBAAsB,OAAO,cAAc;AACjD,MACI,oBAAoB,UAAU,iBAAiB,UAC/C,oBAAoB,MAAM,CAAC,iBAAiB,MAAM,EAAE,MAAM,CAAC,MAAM,UAAU,SAAS,iBAAiB,KAAK,CAAC,GAC7G;AACE,UAAM,IAAIA,aAAY,kDAAkD;AAAA,EAC5E;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,yCAAyC,eAAAA,oBAAmB;AAIrE,eAAsB,wBAAwB,WAAwC;AAClF,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AACvE,UAAM,IAAIA,aAAY,uCAAuC;AAAA,EACjE;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__ADDRESS_BYTE_LENGTH_OUT_OF_RANGE,\n SOLANA_ERROR__ADDRESS_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__ADDRESS_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__ADDRESS_BYTE_LENGTH_OUT_OF_RANGE, {\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__COULD_NOT_FIND_VIABLE_PDA_BUMP_SEED,\n SOLANA_ERROR__INVALID_SEEDS_POINT_ON_CURVE,\n SOLANA_ERROR__MALFORMED_PROGRAM_DERIVED_ADDRESS,\n SOLANA_ERROR__MAX_NUMBER_OF_PDA_SEEDS_EXCEEDED,\n SOLANA_ERROR__MAX_PDA_SEED_LENGTH_EXCEEDED,\n SOLANA_ERROR__PROGRAM_ADDRESS_ENDS_WITH_PDA_MARKER,\n SOLANA_ERROR__PROGRAM_DERIVED_ADDRESS_BUMP_SEED_OUT_OF_RANGE,\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__MALFORMED_PROGRAM_DERIVED_ADDRESS);\n }\n if (value[1] < 0 || value[1] > 255) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_DERIVED_ADDRESS_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__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__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__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__INVALID_SEEDS_POINT_ON_CURVE)) {\n bumpSeed--;\n } else {\n throw e;\n }\n }\n }\n throw new SolanaError(SOLANA_ERROR__COULD_NOT_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__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__PROGRAM_ADDRESS_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__NOT_AN_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__NOT_AN_ED25519_PUBLIC_KEY);\n }\n const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);\n return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));\n}\n"]}
|