@solana/addresses 2.0.0-experimental.fc4e943 → 2.0.0-experimental.fcff844
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 +120 -13
- package/dist/index.browser.cjs +25 -41
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +25 -43
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +192 -220
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +27 -41
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +25 -41
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +25 -41
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +13 -13
- package/dist/types/address.d.ts +8 -14
- package/dist/types/address.d.ts.map +1 -1
- package/dist/types/program-derived-address.d.ts +6 -6
- package/dist/types/program-derived-address.d.ts.map +1 -1
- package/dist/types/public-key.d.ts +2 -2
- package/dist/types/public-key.d.ts.map +1 -1
- package/package.json +11 -11
package/dist/index.node.js.map
CHANGED
|
@@ -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,4BA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D;AAAA,EACrE;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;AAEE,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":["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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AC7G;AAEE,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;AAC9B;;;AGjKA,SAAS,oCAAoC;AAI7C,eAAsB,wBAAwB,WAAwC;AAClF,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,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';\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"]}
|
|
@@ -2,20 +2,20 @@ this.globalThis = this.globalThis || {};
|
|
|
2
2
|
this.globalThis.solanaWeb3 = (function (exports) {
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
-
function
|
|
5
|
+
function E(e,r,t=0){if(r.length-t<=0)throw new Error(`Codec [${e}] cannot decode empty byte arrays.`)}function x(e,r,t,n=0){let o=t.length-n;if(o<r)throw new Error(`Codec [${e}] expected ${r} bytes, got ${o}.`)}var re=(e,r)=>{if(e.length>=r)return e;let t=new Uint8Array(r).fill(0);return t.set(e),t},te=(e,r)=>re(e.length<=r?e:e.slice(0,r),r);function w(e,r){return "fixedSize"in r?r.fixedSize:r.getSizeFromValue(e)}function p(e){return Object.freeze({...e,encode:r=>{let t=new Uint8Array(w(r,e));return e.write(r,t,0),t}})}function b(e){return Object.freeze({...e,decode:(r,t=0)=>e.read(r,t)[0]})}function y(e){return "fixedSize"in e&&typeof e.fixedSize=="number"}function ne(e){return !y(e)}function A(e,r){if(y(e)!==y(r))throw new Error("Encoder and decoder must either both be fixed-size or variable-size.");if(y(e)&&y(r)&&e.fixedSize!==r.fixedSize)throw new Error(`Encoder and decoder must have the same fixed size, got [${e.fixedSize}] and [${r.fixedSize}].`);if(!y(e)&&!y(r)&&e.maxSize!==r.maxSize)throw new Error(`Encoder and decoder must have the same max size, got [${e.maxSize}] and [${r.maxSize}].`);return {...r,...e,decode:r.decode,encode:e.encode,read:r.read,write:e.write}}function T(e,r){return p({fixedSize:r,write:(t,n,o)=>{let s=e.encode(t),i=s.length>r?s.slice(0,r):s;return n.set(i,o),o+r}})}function N(e,r){return b({fixedSize:r,read:(t,n)=>{x("fixCodec",r,t,n),(n>0||t.length>r)&&(t=t.slice(n,n+r)),y(e)&&(t=te(t,e.fixedSize));let[o]=e.read(t,0);return [o,n+r]}})}function P(e,r){return p({...ne(e)?{...e,getSizeFromValue:t=>e.getSizeFromValue(r(t))}:e,write:(t,n,o)=>e.write(r(t),n,o)})}function oe(e,r,t,n){if(n<r||n>t)throw new Error(`Codec [${e}] expected number to be in the range [${r}, ${t}], got ${n}.`)}function $(e){return (e==null?void 0:e.endian)!==1}function se(e){return p({fixedSize:e.size,write(r,t,n){e.range&&oe(e.name,e.range[0],e.range[1],r);let o=new ArrayBuffer(e.size);return e.set(new DataView(o),r,$(e.config)),t.set(new Uint8Array(o),n),n+e.size}})}function ie(e){return b({fixedSize:e.size,read(r,t=0){E(e.name,r,t),x(e.name,e.size,r,t);let n=new DataView(ae(r,t,e.size));return [e.get(n,$(e.config)),t+e.size]}})}function ae(e,r,t){let n=e.byteOffset+(r!=null?r:0),o=t!=null?t:e.byteLength;return e.buffer.slice(n,n+o)}var O=(e={})=>se({config:e,name:"u32",range:[0,+"0xffffffff"],set:(r,t,n)=>r.setUint32(0,t,n),size:4}),_=(e={})=>ie({config:e,get:(r,t)=>r.getUint32(0,t),name:"u32",size:4});function de(e,r,t=r){if(!r.match(new RegExp(`^[${e}]*$`)))throw new Error(`Expected a string of base ${e.length}, got [${t}].`)}var ce=e=>p({getSizeFromValue:r=>{let[t,n]=L(r,e[0]);if(n==="")return r.length;let o=V(n,e);return t.length+Math.ceil(o.toString(16).length/2)},write(r,t,n){if(de(e,r),r==="")return n;let[o,s]=L(r,e[0]);if(s==="")return t.set(new Uint8Array(o.length).fill(0),n),n+o.length;let i=V(s,e),d=[];for(;i>0n;)d.unshift(Number(i%256n)),i/=256n;let a=[...Array(o.length).fill(0),...d];return t.set(a,n),n+a.length}}),fe=e=>b({read(r,t){let n=t===0?r:r.slice(t);if(n.length===0)return ["",0];let o=n.findIndex(a=>a!==0);o=o===-1?n.length:o;let s=e[0].repeat(o);if(o===n.length)return [s,r.length];let i=n.slice(o).reduce((a,c)=>a*256n+BigInt(c),0n),d=ge(i,e);return [s+d,r.length]}});function L(e,r){let t=[...e].findIndex(n=>n!==r);return t===-1?[e,""]:[e.slice(0,t),e.slice(t)]}function V(e,r){let t=BigInt(r.length);return [...e].reduce((n,o)=>n*t+BigInt(r.indexOf(o)),0n)}function ge(e,r){let t=BigInt(r.length),n=[];for(;e>0n;)n.unshift(r[Number(e%t)]),e/=t;return n.join("")}var k="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",M=()=>ce(k),K=()=>fe(k);var ue=e=>e.replace(/\u0000/g,"");var le=globalThis.TextDecoder,R=globalThis.TextEncoder,me=()=>{let e;return p({getSizeFromValue:r=>(e||(e=new R)).encode(r).length,write:(r,t,n)=>{let o=(e||(e=new R)).encode(r);return t.set(o,n),n+o.length}})},he=()=>{let e;return b({read(r,t){let n=(e||(e=new le)).decode(r.slice(t));return [ue(n),r.length]}})};function X(e={}){var n,o;let r=(n=e.size)!=null?n:O(),t=(o=e.encoding)!=null?o:me();return r==="variable"?t:typeof r=="number"?T(t,r):p({getSizeFromValue:s=>{let i=w(s,t);return w(i,r)+i},write:(s,i,d)=>{let a=w(s,t);return d=r.write(a,i,d),t.write(s,i,d)}})}function H(e={}){var n,o;let r=(n=e.size)!=null?n:_(),t=(o=e.encoding)!=null?o:he();return r==="variable"?t:typeof r=="number"?N(t,r):b({read:(s,i=0)=>{E("string",s,i);let[d,a]=r.read(s,i),c=Number(d);i=a;let u=s.slice(i,i+c);x("string",c,u);let[h,F]=t.read(u,0);return i+=F,[h,i]}})}var B,I;function S(){return B||(B=M()),B}function ye(){return I||(I=K()),I}function j(e){return !(e.length<32||e.length>44||S().encode(e).byteLength!==32)}function C(e){try{if(e.length<32||e.length>44)throw new Error("Expected input string to decode to a byte array of length 32.");let n=S().encode(e).byteLength;if(n!==32)throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${n}`)}catch(r){throw new Error(`\`${e}\` is not a base-58 encoded address`,{cause:r})}}function pe(e){return C(e),e}function be(){return P(X({encoding:S(),size:32}),e=>pe(e))}function D(){return H({encoding:ye(),size:32})}function U(){return A(be(),D())}function Me(){return new Intl.Collator("en",{caseFirst:"lower",ignorePunctuation:!1,localeMatcher:"best fit",numeric:!1,sensitivity:"variant",usage:"sort"}).compare}function G(){if(!globalThis.isSecureContext)throw new Error("Cryptographic operations are only allowed in secure browser contexts. Read more here: https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts")}async function Z(){var e;if(G(),typeof globalThis.crypto>"u"||typeof((e=globalThis.crypto.subtle)==null?void 0:e.digest)!="function")throw new Error("No digest implementation could be found")}async function W(){var e;if(G(),typeof globalThis.crypto>"u"||typeof((e=globalThis.crypto.subtle)==null?void 0:e.exportKey)!="function")throw new Error("No key export implementation could be found")}var xe=37095705934669439343138083508754565189542113879843219016388785533085940283555n,f=57896044618658097711785492504343953926634992332820282019728792003956564819949n,q=19681161376707505956807079304988542015446066515923890162744021073123829784752n;function g(e){let r=e%f;return r>=0n?r:f+r}function l(e,r){let t=e;for(;r-- >0n;)t*=t,t%=f;return t}function we(e){let t=e*e%f*e%f,n=l(t,2n)*t%f,o=l(n,1n)*e%f,s=l(o,5n)*o%f,i=l(s,10n)*s%f,d=l(i,20n)*i%f,a=l(d,40n)*d%f,c=l(a,80n)*a%f,u=l(c,80n)*a%f,h=l(u,10n)*s%f;return l(h,2n)*e%f}function Ee(e,r){let t=g(r*r*r),n=g(t*t*r),o=we(e*n),s=g(e*t*o),i=g(r*s*s),d=s,a=g(s*q),c=i===e,u=i===g(-e),h=i===g(-e*q);return c&&(s=d),(u||h)&&(s=a),(g(s)&1n)===1n&&(s=g(-s)),!c&&!u?null:s}function Y(e,r){let t=g(e*e),n=g(t-1n),o=g(xe*t+1n),s=Ee(n,o);if(s===null)return !1;let i=(r&128)!==0;return !(s===0n&&i)}function Ae(e){let r=e.toString(16);return r.length===1?`0${r}`:r}function ve(e){let t=`0x${e.reduce((n,o,s)=>`${Ae(s===31?o&-129:o)}${n}`,"")}`;return BigInt(t)}async function J(e){if(e.byteLength!==32)return !1;let r=ve(e);return Y(r,e[31])}function tr(e){return Array.isArray(e)&&e.length===2&&typeof e[0]=="string"&&typeof e[1]=="number"&&e[1]>=0&&e[1]<=255&&j(e[0])}function nr(e){if(!(Array.isArray(e)&&e.length===2&&typeof e[0]=="string"&&typeof e[1]=="number"))throw new Error("Expected given program derived address to have the following format: [Address, ProgramDerivedAddressBump].");if(e[1]<0||e[1]>255)throw new Error(`Expected program derived address bump to be in the range [0, 255], got: ${e[1]}.`);C(e[0]);}var ee=32,Q=16,v=[80,114,111,103,114,97,109,68,101,114,105,118,101,100,65,100,100,114,101,115,115],z=class extends Error{};async function ze({programAddress:e,seeds:r}){if(await Z(),r.length>Q)throw new Error(`A maximum of ${Q} seeds may be supplied when creating an address`);let t,n=r.reduce((a,c,u)=>{let h=typeof c=="string"?(t||(t=new TextEncoder)).encode(c):c;if(h.byteLength>ee)throw new Error(`The seed at index ${u} exceeds the maximum length of 32 bytes`);return a.push(...h),a},[]),o=U(),s=o.encode(e),i=await crypto.subtle.digest("SHA-256",new Uint8Array([...n,...s,...v])),d=new Uint8Array(i);if(await J(d))throw new z("Invalid seeds; point must fall off the Ed25519 curve");return o.decode(d)}async function or({programAddress:e,seeds:r}){let t=255;for(;t>0;)try{return [await ze({programAddress:e,seeds:[...r,new Uint8Array([t])]}),t]}catch(n){if(n instanceof z)t--;else throw n}throw new Error("Unable to find a viable program address bump seed")}async function sr({baseAddress:e,programAddress:r,seed:t}){let{encode:n,decode:o}=U(),s=typeof t=="string"?new TextEncoder().encode(t):t;if(s.byteLength>ee)throw new Error("The seed exceeds the maximum length of 32 bytes");let i=n(r);if(i.length>=v.length&&i.slice(-v.length).every((c,u)=>c===v[u]))throw new Error("programAddress cannot end with the PDA marker");let d=await crypto.subtle.digest("SHA-256",new Uint8Array([...n(e),...s,...i])),a=new Uint8Array(d);return o(a)}async function gr(e){if(await W(),e.type!=="public"||e.algorithm.name!=="Ed25519")throw new Error("The `CryptoKey` must be an `Ed25519` public key");let r=await crypto.subtle.exportKey("raw",e);return D().decode(new Uint8Array(r))}
|
|
6
6
|
|
|
7
|
-
exports.address =
|
|
8
|
-
exports.assertIsAddress =
|
|
9
|
-
exports.assertIsProgramDerivedAddress =
|
|
10
|
-
exports.createAddressWithSeed =
|
|
11
|
-
exports.getAddressCodec =
|
|
12
|
-
exports.getAddressComparator =
|
|
13
|
-
exports.getAddressDecoder =
|
|
14
|
-
exports.getAddressEncoder =
|
|
15
|
-
exports.getAddressFromPublicKey =
|
|
16
|
-
exports.getProgramDerivedAddress =
|
|
17
|
-
exports.isAddress =
|
|
18
|
-
exports.isProgramDerivedAddress =
|
|
7
|
+
exports.address = pe;
|
|
8
|
+
exports.assertIsAddress = C;
|
|
9
|
+
exports.assertIsProgramDerivedAddress = nr;
|
|
10
|
+
exports.createAddressWithSeed = sr;
|
|
11
|
+
exports.getAddressCodec = U;
|
|
12
|
+
exports.getAddressComparator = Me;
|
|
13
|
+
exports.getAddressDecoder = D;
|
|
14
|
+
exports.getAddressEncoder = be;
|
|
15
|
+
exports.getAddressFromPublicKey = gr;
|
|
16
|
+
exports.getProgramDerivedAddress = or;
|
|
17
|
+
exports.isAddress = j;
|
|
18
|
+
exports.isProgramDerivedAddress = tr;
|
|
19
19
|
|
|
20
20
|
return exports;
|
|
21
21
|
|
package/dist/types/address.d.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type
|
|
1
|
+
import { FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from '@solana/codecs-core';
|
|
2
|
+
export type Address<TAddress extends string = string> = TAddress & {
|
|
3
3
|
readonly __brand: unique symbol;
|
|
4
4
|
};
|
|
5
|
-
export declare function isAddress(
|
|
6
|
-
export declare function assertIsAddress(
|
|
7
|
-
export declare function address<TAddress extends string = string>(
|
|
8
|
-
export declare function getAddressEncoder(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export declare function getAddressDecoder(config?: Readonly<{
|
|
12
|
-
description: string;
|
|
13
|
-
}>): Decoder<Base58EncodedAddress>;
|
|
14
|
-
export declare function getAddressCodec(config?: Readonly<{
|
|
15
|
-
description: string;
|
|
16
|
-
}>): Codec<Base58EncodedAddress>;
|
|
5
|
+
export declare function isAddress(putativeAddress: string): putativeAddress is Address<typeof putativeAddress>;
|
|
6
|
+
export declare function assertIsAddress(putativeAddress: string): asserts putativeAddress is Address<typeof putativeAddress>;
|
|
7
|
+
export declare function address<TAddress extends string = string>(putativeAddress: TAddress): Address<TAddress>;
|
|
8
|
+
export declare function getAddressEncoder(): FixedSizeEncoder<Address, 32>;
|
|
9
|
+
export declare function getAddressDecoder(): FixedSizeDecoder<Address, 32>;
|
|
10
|
+
export declare function getAddressCodec(): FixedSizeCodec<Address, Address, 32>;
|
|
17
11
|
export declare function getAddressComparator(): (x: string, y: string) => number;
|
|
18
12
|
//# sourceMappingURL=address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAIH,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAEnB,MAAM,qBAAqB,CAAC;AAG7B,MAAM,MAAM,OAAO,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,IAAI,QAAQ,GAAG;IAC/D,QAAQ,CAAC,OAAO,EAAE,OAAO,MAAM,CAAC;CACnC,CAAC;AAeF,wBAAgB,SAAS,CAAC,eAAe,EAAE,MAAM,GAAG,eAAe,IAAI,OAAO,CAAC,OAAO,eAAe,CAAC,CAkBrG;AAED,wBAAgB,eAAe,CAAC,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,IAAI,OAAO,CAAC,OAAO,eAAe,CAAC,CAuBnH;AAED,wBAAgB,OAAO,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAAE,eAAe,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAGtG;AAED,wBAAgB,iBAAiB,IAAI,gBAAgB,CAAC,OAAO,EAAE,EAAE,CAAC,CAIjE;AAED,wBAAgB,iBAAiB,IAAI,gBAAgB,CAAC,OAAO,EAAE,EAAE,CAAC,CAEjE;AAED,wBAAgB,eAAe,IAAI,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,CAEtE;AAED,wBAAgB,oBAAoB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CASvE"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Address } from './address';
|
|
2
2
|
/**
|
|
3
3
|
* An address derived from a program address and a set of seeds.
|
|
4
4
|
* It includes the bump seed used to derive the address and
|
|
5
5
|
* ensure the address is not on the Ed25519 curve.
|
|
6
6
|
*/
|
|
7
7
|
export type ProgramDerivedAddress<TAddress extends string = string> = Readonly<[
|
|
8
|
-
|
|
8
|
+
Address<TAddress>,
|
|
9
9
|
ProgramDerivedAddressBump
|
|
10
10
|
]>;
|
|
11
11
|
/**
|
|
@@ -23,16 +23,16 @@ export declare function isProgramDerivedAddress<TAddress extends string = string
|
|
|
23
23
|
*/
|
|
24
24
|
export declare function assertIsProgramDerivedAddress<TAddress extends string = string>(value: unknown): asserts value is ProgramDerivedAddress<TAddress>;
|
|
25
25
|
type ProgramDerivedAddressInput = Readonly<{
|
|
26
|
-
programAddress:
|
|
26
|
+
programAddress: Address;
|
|
27
27
|
seeds: Seed[];
|
|
28
28
|
}>;
|
|
29
29
|
type SeedInput = Readonly<{
|
|
30
|
-
baseAddress:
|
|
31
|
-
programAddress:
|
|
30
|
+
baseAddress: Address;
|
|
31
|
+
programAddress: Address;
|
|
32
32
|
seed: Seed;
|
|
33
33
|
}>;
|
|
34
34
|
type Seed = string | Uint8Array;
|
|
35
35
|
export declare function getProgramDerivedAddress({ programAddress, seeds, }: ProgramDerivedAddressInput): Promise<ProgramDerivedAddress>;
|
|
36
|
-
export declare function createAddressWithSeed({ baseAddress, programAddress, seed
|
|
36
|
+
export declare function createAddressWithSeed({ baseAddress, programAddress, seed }: SeedInput): Promise<Address>;
|
|
37
37
|
export {};
|
|
38
38
|
//# sourceMappingURL=program-derived-address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"program-derived-address.d.ts","sourceRoot":"","sources":["../../src/program-derived-address.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"program-derived-address.d.ts","sourceRoot":"","sources":["../../src/program-derived-address.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAA+C,MAAM,WAAW,CAAC;AAGjF;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,IAAI,QAAQ,CAC1E;IAAC,OAAO,CAAC,QAAQ,CAAC;IAAE,yBAAyB;CAAC,CACjD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,MAAM,GAAG;IAC7C,QAAQ,CAAC,OAAO,EAAE,OAAO,MAAM,CAAC;CACnC,CAAC;AAEF;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EACpE,KAAK,EAAE,OAAO,GACf,KAAK,IAAI,qBAAqB,CAAC,QAAQ,CAAC,CAU1C;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EAC1E,KAAK,EAAE,OAAO,GACf,OAAO,CAAC,KAAK,IAAI,qBAAqB,CAAC,QAAQ,CAAC,CAclD;AAED,KAAK,0BAA0B,GAAG,QAAQ,CAAC;IACvC,cAAc,EAAE,OAAO,CAAC;IACxB,KAAK,EAAE,IAAI,EAAE,CAAC;CACjB,CAAC,CAAC;AAEH,KAAK,SAAS,GAAG,QAAQ,CAAC;IACtB,WAAW,EAAE,OAAO,CAAC;IACrB,cAAc,EAAE,OAAO,CAAC;IACxB,IAAI,EAAE,IAAI,CAAC;CACd,CAAC,CAAC;AAEH,KAAK,IAAI,GAAG,MAAM,GAAG,UAAU,CAAC;AA0ChC,wBAAsB,wBAAwB,CAAC,EAC3C,cAAc,EACd,KAAK,GACR,EAAE,0BAA0B,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAmB7D;AAED,wBAAsB,qBAAqB,CAAC,EAAE,WAAW,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,CAyB9G"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function getAddressFromPublicKey(publicKey: CryptoKey): Promise<
|
|
1
|
+
import { Address } from './address';
|
|
2
|
+
export declare function getAddressFromPublicKey(publicKey: CryptoKey): Promise<Address>;
|
|
3
3
|
//# sourceMappingURL=public-key.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public-key.d.ts","sourceRoot":"","sources":["../../src/public-key.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"public-key.d.ts","sourceRoot":"","sources":["../../src/public-key.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAqB,MAAM,WAAW,CAAC;AAEvD,wBAAsB,uBAAuB,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,CAQpF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/addresses",
|
|
3
|
-
"version": "2.0.0-experimental.
|
|
3
|
+
"version": "2.0.0-experimental.fcff844",
|
|
4
4
|
"description": "Helpers for generating account addresses",
|
|
5
5
|
"exports": {
|
|
6
6
|
"browser": {
|
|
@@ -49,26 +49,26 @@
|
|
|
49
49
|
"node": ">=17.4"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@solana/assertions": "2.0.0-experimental.
|
|
53
|
-
"@solana/codecs-core": "2.0.0-experimental.
|
|
54
|
-
"@solana/codecs-strings": "2.0.0-experimental.
|
|
52
|
+
"@solana/assertions": "2.0.0-experimental.fcff844",
|
|
53
|
+
"@solana/codecs-core": "2.0.0-experimental.fcff844",
|
|
54
|
+
"@solana/codecs-strings": "2.0.0-experimental.fcff844"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@solana/eslint-config-solana": "^1.0.2",
|
|
58
|
-
"@swc/jest": "^0.2.
|
|
59
|
-
"@types/jest": "^29.5.
|
|
58
|
+
"@swc/jest": "^0.2.29",
|
|
59
|
+
"@types/jest": "^29.5.6",
|
|
60
60
|
"@typescript-eslint/eslint-plugin": "^6.7.0",
|
|
61
61
|
"@typescript-eslint/parser": "^6.3.0",
|
|
62
62
|
"agadoo": "^3.0.0",
|
|
63
63
|
"eslint": "^8.45.0",
|
|
64
|
-
"eslint-plugin-jest": "^27.2
|
|
64
|
+
"eslint-plugin-jest": "^27.4.2",
|
|
65
65
|
"eslint-plugin-sort-keys-fix": "^1.1.2",
|
|
66
66
|
"jest": "^29.7.0",
|
|
67
|
-
"jest-environment-jsdom": "^29.
|
|
68
|
-
"jest-runner-eslint": "^2.1.
|
|
67
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
68
|
+
"jest-runner-eslint": "^2.1.2",
|
|
69
69
|
"jest-runner-prettier": "^1.0.0",
|
|
70
|
-
"prettier": "^
|
|
71
|
-
"tsup": "
|
|
70
|
+
"prettier": "^3.1",
|
|
71
|
+
"tsup": "^8.0.1",
|
|
72
72
|
"typescript": "^5.2.2",
|
|
73
73
|
"version-from-git": "^1.1.1",
|
|
74
74
|
"build-scripts": "0.0.0",
|