tiny-crypto-suite
Version:
Tiny tools, big crypto — seamless encryption and certificate handling for modern web and Node apps.
285 lines • 11.3 kB
text/typescript
export default TinySecp256k1;
export type ValidationResult = {
/**
* - Indicates whether the address is valid.
*/
valid: boolean;
/**
* - The type of the address if valid (e.g., 'bech32', 'p2pkh').
*/
type: string | null;
};
/**
* @typedef {Object} ValidationResult
* @property {boolean} valid - Indicates whether the address is valid.
* @property {string|null} type - The type of the address if valid (e.g., 'bech32', 'p2pkh').
*/
/**
* A minimal wrapper around the `secp256k1` elliptic curve cryptography using the `elliptic` library.
* Provides functionality for creating and managing an elliptic key pair, signing messages,
* verifying signatures, and recovering public keys from signed messages.
*
* This class is designed to be lightweight, dependency-lazy (loads `elliptic` only when needed),
* and optionally compatible with Bitcoin/Ethereum-style message prefixes and signature formats.
*
* ### Features:
* - Generates or imports a private key with configurable encoding.
* - Uses `secp256k1` curve via `elliptic` library.
* - Supports signing messages (SHA-256 and double SHA-256).
* - Supports recoverable signatures with 65-byte format.
* - Allows recovery of public keys from messages and signatures.
* - Verifies ECDSA signatures.
*
* ### Usage:
* ```js
* const signer = new TinySecp256k1({
* msgPrefix: 'MyApp Signed Message:\n',
* privateKey: 'a1b2c3...',
* privateKeyEncoding: 'hex'
* });
* await signer.init();
* const sig = signer.signMessage('hello');
* const pubKey = signer.recoverMessage('hello', sig);
* ```
*
* ### Internal Notes:
* - Internally uses lazy loading for `elliptic`, allowing the class to be used
* in contexts where `elliptic` may not yet be installed.
* - The message prefix format mimics Bitcoin/Ethereum message signing,
* allowing compatibility with common recovery mechanisms.
* - Signatures are canonical and encoded in DER or recoverable formats (r, s, v).
*
* @class
*/
declare class TinySecp256k1 {
/**
* Computes SHA-256 hash of the input buffer.
*
* @param {Buffer} buf - The buffer to hash.
* @returns {Buffer} The SHA-256 hash of the input.
*/
static sha256(buf: Buffer): Buffer;
/**
* Computes double SHA-256 hash of the input buffer.
*
* @param {Buffer} buf - The buffer to hash.
* @returns {Buffer} The double SHA-256 hash of the input.
*/
static doubleSha256(buf: Buffer): Buffer;
/**
* RIPEMD160(SHA256(x))
* @param {Buffer} buffer
* @returns {Buffer}
*/
static hash160(buffer: Buffer): Buffer;
/**
* Creates an instance of TinySecp256k1.
*
* @param {Object} [options] - Optional parameters for the instance.
* @param {string|null} [options.type=null] - Crypto type used during the get address.
* @param {string|null} [options.prefix=null] - Crypto prefix used during message verification.
* @param {string|null} [options.msgPrefix=null] - Message prefix used during message signing.
* @param {string|null} [options.privateKey=null] - String representation of the private key.
* @param {BufferEncoding} [options.privateKeyEncoding='hex'] - Encoding used for the privateKey string.
*/
constructor({ type, prefix, msgPrefix, privateKey, privateKeyEncoding, }?: {
type?: string | null | undefined;
prefix?: string | null | undefined;
msgPrefix?: string | null | undefined;
privateKey?: string | null | undefined;
privateKeyEncoding?: BufferEncoding | undefined;
});
/** @typedef {import('elliptic')} Elliptic */
/** @typedef {import('elliptic').ec} ec */
/** @typedef {import('elliptic').ec.KeyPair} KeyPair */
msgPrefix: string;
prefix: string;
type: string;
privateKey: Buffer<ArrayBufferLike>;
/** @type {Record<string, string>} */
types: Record<string, string>;
/** @type {Record<string, string>} */
prefixes: Record<string, string>;
/**
* Checks if the given type exists in the supported types list.
*
* @param {string} type
* @returns {boolean}
* @throws {Error} If type is not a string.
*/
isType(type: string): boolean;
/**
* Checks if the given prefix exists in the supported prefixes list.
*
* @param {string} type
* @returns {boolean}
* @throws {Error} If type is not a string.
*/
isPrefix(type: string): boolean;
/**
* Returns the matching prefix type from the supported list if found.
*
* @param {string} address
* @returns {string|null}
* @throws {Error} If address is not a string.
*/
getPrefixType(address: string): string | null;
/**
* Returns the message prefix if it's a string.
*
* @returns {string}
* @throws {Error} If msgPrefix is not a string.
*/
getMsgPrefix(): string;
/**
* Returns the address prefix if it's a string.
*
* @returns {string}
* @throws {Error} If prefix is not a string.
*/
getPrefix(): string;
/**
* Returns the crypto type if it's a string.
*
* @returns {string}
* @throws {Error} If type is not a string.
*/
getType(): string;
/**
* Initializes the internal elliptic key pair using the private key.
*
* @returns {Promise<KeyPair>} The elliptic key pair.
*/
init(): Promise<import("elliptic").ec.KeyPair>;
keyPair: import("elliptic").ec.KeyPair | undefined;
/**
* Returns the elliptic key pair generated from the private key.
*
* @returns {KeyPair} The elliptic key pair.
* @throws {Error} If the key pair is not initialized (init() was not called).
*/
getKeyPair(): import("elliptic").ec.KeyPair;
/**
* Dynamically imports the `elliptic` module and stores it in the instance.
* Ensures the module is loaded only once (lazy singleton).
*
* @returns {Promise<ec>} The loaded `elliptic` module.
*/
fetchElliptic(): Promise<import("elliptic").ec>;
elliptic: typeof import("elliptic") | undefined;
ec: import("elliptic").ec | undefined;
/**
* Returns the initialized `ec` instance from the elliptic module.
*
* @returns {ec} The elliptic curve instance (secp256k1).
* @throws Will throw an error if `ec` is not initialized.
*/
getEc(): import("elliptic").ec;
/**
* Returns the loaded `Elliptic` module.
*
* @returns {Elliptic} The elliptic module.
* @throws Will throw an error if the module is not initialized.
*/
getElliptic(): typeof import("elliptic");
/**
* Returns the private key in hexadecimal format.
*
* @returns {string} Hex representation of the private key.
*/
getPrivateKeyHex(): string;
/**
* Returns the public key as a buffer.
* @param {boolean} [compressed=true] - Whether to return the compressed version of the key.
* @returns {Buffer}
*/
getPublicKeyBuffer(compressed?: boolean): Buffer;
/**
* Returns the public key in hexadecimal format.
*
* @param {boolean} [compressed=true] - Whether to return the compressed version of the key.
* @returns {string} Hex representation of the public key.
*/
getPublicKeyHex(compressed?: boolean): string;
/**
* Returns the public key in vanilla format.
*
* @param {boolean} [compressed=true] - Whether to return the compressed version of the key.
* @returns {Buffer} Hash160 representation of the public key.
*/
getPubVanillaAddress(compressed?: boolean): Buffer;
/**
* Returns the address in hash160 format.
*
* @param {string} address - Whether to return the compressed version of the key.
* @param {string} [type=this.getType()] - The type of address to generate.
* @returns {Buffer} Hash160 representation of the public key.
*/
addressToVanilla(address: string, type?: string): Buffer;
/**
* Returns the public address derived from the public key.
*
* @param {string} [type=this.getType()] - The type of address to generate.
* @param {Buffer} [pubKey=this.getPublicKeyBuffer()] - The pubKey buffer.
* @returns {string} The public address.
*/
getAddress(pubKey?: Buffer, type?: string): string;
/**
* Signs a message using ECDSA and returns the DER-encoded signature.
*
* @param {string|Buffer} message - The message to sign.
* @param {BufferEncoding} [encoding='utf8'] - Encoding if message is a string.
* @returns {Buffer} DER-encoded signature buffer.
*/
signECDSA(message: string | Buffer, encoding?: BufferEncoding): Buffer;
/**
* Verifies an ECDSA signature against a message and a public key.
*
* @param {string|Buffer} message - The original message to verify.
* @param {Buffer} signatureBuffer - The signature buffer to validate.
* @param {string} pubKeyHex - The public key in hex format.
* @param {BufferEncoding} [encoding] - Encoding if the message is a string.
* @returns {boolean} `true` if valid, `false` otherwise.
*/
verifyECDSA(message: string | Buffer, signatureBuffer: Buffer, pubKeyHex: string, encoding?: BufferEncoding): boolean;
/**
* Validates a address.
*
* @param {string} address - The address string to validate.
* @param {string} [type=this.getType()] - The type of address to generate.
* @returns {ValidationResult}
*/
validateAddress(address: string, type?: string): ValidationResult;
/**
* Recovers the public key from a signed message and signature with recovery param.
*
* @param {string|Buffer} message - The original signed message.
* @param {Buffer} signature - A 65-byte compact signature buffer (r + s + v).
* @param {Object} [options] - Options for decoding the message hash.
* @param {BufferEncoding} [options.encoding='hex'] - The encoding of the input message.
* @param {string} [options.prefix=this.getMsgPrefix()] - Optional prefix used before hashing the message.
* @returns {string|null} The recovered compressed public key in hex format, or null if recovery fails.
* @throws {Error} If the encoding type is unsupported or signature is invalid.
*/
recoverMessage(message: string | Buffer, signature: Buffer, options?: {
encoding?: BufferEncoding | undefined;
prefix?: string | undefined;
}): string | null;
/**
* Signs a message using ECDSA and includes the recovery param in the result.
*
* @param {string|Buffer} message - The message to sign.
* @param {Object} [options] - Options for the message hashing process.
* @param {BufferEncoding} [options.encoding='hex'] - The encoding used for string messages.
* @param {string} [options.prefix=this.getMsgPrefix()] - Optional message prefix for the hash.
* @returns {Buffer} A 65-byte recoverable signature (r + s + v).
* @throws {Error} If recovery param is missing or encoding type is unsupported.
*/
signMessage(message: string | Buffer, options?: {
encoding?: BufferEncoding | undefined;
prefix?: string | undefined;
}): Buffer;
#private;
}
import { Buffer } from 'buffer';
//# sourceMappingURL=index.d.mts.map