UNPKG

ox

Version:

Ethereum Standard Library

238 lines 7.24 kB
import { secp256k1 } from '@noble/curves/secp256k1.js'; import * as Address from './Address.js'; import * as Bytes from './Bytes.js'; import * as Hex from './Hex.js'; import { formatPublicKey, formatSignature, normalizePublicKey, normalizeSignature, } from './internal/cryptoIo.js'; import * as Entropy from './internal/entropy.js'; import { fromRecoveredBytes, toCompactBytes, toRecoveredBytes, } from './internal/signature.js'; import * as PublicKey from './PublicKey.js'; /** Re-export of noble/curves secp256k1 utilities. */ export const noble = secp256k1; /** * Creates a new secp256k1 ECDSA key pair consisting of a private key and its corresponding public key. * * @example * ```ts twoslash * import { Secp256k1 } from 'ox' * * const { privateKey, publicKey } = Secp256k1.createKeyPair() * ``` * * @param options - The options to generate the key pair. * @returns The generated key pair containing both private and public keys. */ export function createKeyPair(options = {}) { const { as = 'Hex' } = options; const privateKey = randomPrivateKey({ as }); const publicKey = getPublicKey({ privateKey }); return { privateKey: privateKey, publicKey, }; } /** * Computes the secp256k1 ECDSA public key from a provided private key. * * @example * ```ts twoslash * import { Secp256k1 } from 'ox' * * const publicKey = Secp256k1.getPublicKey({ * privateKey: '0x...' * }) * ``` * * @param options - The options to compute the public key. * @returns The computed public key. */ export function getPublicKey(options) { const { as = 'Object', privateKey } = options; const bytes = secp256k1.getPublicKey(Bytes.from(privateKey), false); const publicKey = PublicKey.fromBytes(bytes); return formatPublicKey(publicKey, as); } /** * Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key. * * @example * ```ts twoslash * import { Secp256k1 } from 'ox' * * const { privateKey: privateKeyA } = * Secp256k1.createKeyPair() * const { publicKey: publicKeyB } = Secp256k1.createKeyPair() * * const sharedSecret = Secp256k1.getSharedSecret({ * privateKey: privateKeyA, * publicKey: publicKeyB * }) * ``` * * @param options - The options to compute the shared secret. * @returns The computed shared secret. */ export function getSharedSecret(options) { const { as = 'Hex', privateKey, publicKey } = options; const sharedSecret = secp256k1.getSharedSecret(Bytes.from(privateKey), PublicKey.toBytes(normalizePublicKey(publicKey)), true); if (as === 'Hex') return Hex.fromBytes(sharedSecret); return sharedSecret; } /** * Generates a random ECDSA private key on the secp256k1 curve. * * @example * ```ts twoslash * import { Secp256k1 } from 'ox' * * const privateKey = Secp256k1.randomPrivateKey() * ``` * * @param options - The options to generate the private key. * @returns The generated private key. */ export function randomPrivateKey(options = {}) { const { as = 'Hex' } = options; const bytes = secp256k1.utils.randomSecretKey(); if (as === 'Hex') return Hex.fromBytes(bytes); return bytes; } /** * Recovers the signing address from the signed payload and signature. * * @example * ```ts twoslash * import { Secp256k1 } from 'ox' * * const signature = Secp256k1.sign({ * payload: '0xdeadbeef', * privateKey: '0x...' * }) * * const address = Secp256k1.recoverAddress({ * // [!code focus] * payload: '0xdeadbeef', // [!code focus] * signature // [!code focus] * }) // [!code focus] * ``` * * @param options - The recovery options. * @returns The recovered address. */ export function recoverAddress(options) { return Address.fromPublicKey(recoverPublicKey(options)); } /** * Recovers the signing public key from the signed payload and signature. * * @example * ```ts twoslash * import { Secp256k1 } from 'ox' * * const signature = Secp256k1.sign({ * payload: '0xdeadbeef', * privateKey: '0x...' * }) * * const publicKey = Secp256k1.recoverPublicKey({ * // [!code focus] * payload: '0xdeadbeef', // [!code focus] * signature // [!code focus] * }) // [!code focus] * ``` * * @param options - The recovery options. * @returns The recovered public key. */ export function recoverPublicKey(options) { const { as = 'Object', payload, signature } = options; const sigBytes = toRecoveredBytes(normalizeSignature(signature)); const point = secp256k1.Signature.fromBytes(sigBytes, 'recovered').recoverPublicKey(Bytes.from(payload)); const publicKey = PublicKey.fromBytes(point.toBytes(false)); return formatPublicKey(publicKey, as); } /** * Signs the payload with the provided private key. * * @example * ```ts twoslash * import { Secp256k1 } from 'ox' * * const signature = Secp256k1.sign({ * // [!code focus] * payload: '0xdeadbeef', // [!code focus] * privateKey: '0x...' // [!code focus] * }) // [!code focus] * ``` * * @param options - The signing options. * @returns The ECDSA {@link ox#Signature.Signature}. */ export function sign(options) { const { as = 'Object', extraEntropy = Entropy.extraEntropy, hash, payload, privateKey, } = options; const sigBytes = secp256k1.sign(Bytes.from(payload), Bytes.from(privateKey), { extraEntropy: typeof extraEntropy === 'boolean' ? extraEntropy : Bytes.from(extraEntropy), lowS: true, prehash: hash === true, format: 'recovered', }); const signature = fromRecoveredBytes(sigBytes); return formatSignature(signature, as); } /** * Verifies a payload was signed by the provided address. * * @example * ### Verify with Ethereum Address * * ```ts twoslash * import { Secp256k1 } from 'ox' * * const signature = Secp256k1.sign({ * payload: '0xdeadbeef', * privateKey: '0x...' * }) * * const verified = Secp256k1.verify({ * // [!code focus] * address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code focus] * payload: '0xdeadbeef', // [!code focus] * signature // [!code focus] * }) // [!code focus] * ``` * * @example * ### Verify with Public Key * * ```ts twoslash * import { Secp256k1 } from 'ox' * * const privateKey = '0x...' * const publicKey = Secp256k1.getPublicKey({ privateKey }) * const signature = Secp256k1.sign({ * payload: '0xdeadbeef', * privateKey * }) * * const verified = Secp256k1.verify({ * // [!code focus] * publicKey, // [!code focus] * payload: '0xdeadbeef', // [!code focus] * signature // [!code focus] * }) // [!code focus] * ``` * * @param options - The verification options. * @returns Whether the payload was signed by the provided address. */ export function verify(options) { const { hash, payload } = options; if (options.address) return Address.isEqual(options.address, recoverAddress({ payload, signature: options.signature })); const sig = normalizeSignature(options.signature); return secp256k1.verify(toCompactBytes(sig), Bytes.from(payload), PublicKey.toBytes(normalizePublicKey(options.publicKey)), { lowS: true, prehash: hash === true }); } //# sourceMappingURL=Secp256k1.js.map