necjs
Version:
NECJS SDK for NCOG Earth Chain RPC
112 lines (111 loc) • 5.49 kB
TypeScript
export declare const DEFAULT_DECIMALS = 18;
export declare const NEC_DECIMALS = 18;
export declare const WEI_FACTOR: bigint;
/**
* Convert a hex string to a decimal (string or number).
* Assumes input is already in base units.
*/
export declare function hexToDecimalString(hex: string): string | number;
/**
* Convert a hex string to a decimal-string (no extra multiplication).
* Use for normalizing RPC response fields already in base units.
*/
export declare function normalizeHexField(key: string, hex: string): string;
/**
* Serialize a decimal (number, numeric-string, or bigint) to hex-with-0x.
* Assumes input is already in base units.
*/
export declare function decimalToHex(value: number | string | bigint): string;
/**
* Generic: parse whole- or fractional-unit amount into base-unit hex.
* Accepts number|string|bigint, handles fractional up to `decimals`.
*/
export declare function parseUnits(value: number | string | bigint, decimals?: number): string;
/**
* Convert an Ether value (number|string|bigint), including fractional,
* → Wei → hex-with-0x.
*/
export declare function etherToWeiHex(value: number | string | bigint): string;
/**
* Convert a Wei-hex (or bigint or numeric string) into an Ether decimal string.
*/
export declare function hexToEther(value: string | number | bigint): string;
/**
* Generic: format a base-unit amount (hex, number, or bigint)
* into a human-readable decimal string.
*/
export declare function formatUnits(value: string | number | bigint, decimals?: number): string;
/**
* Convert a NEC base-unit amount (hex, number, or bigint) into a NEC decimal string.
*/
export declare function hexToNec(value: string | number | bigint): string;
/**
* Convert a whole-NEC amount (number|string|bigint) into base-unit hex.
*/
export declare function necToHex(value: number | string | bigint): string;
/**
* Convert a Wei (number, bigint, or hex string) directly into a NEC decimal string.
* Useful when NEC is pegged 1:1 with Ether base units.
*/
export declare function weiToNec(value: string | number | bigint): string;
/**
* Walk and serialize all fields in TxParams for JSON-RPC.
*/
export declare function serializeForRpc(payload: Record<string, any>): Record<string, any>;
/**
* Walk and normalize JSON-RPC response (hex → decimal string or number).
*/
export declare function normalizeResponse(resp: Record<string, any> | any): Record<string, any> | any;
/**
* Checks if a string is a valid Ethereum/EVM address.
*/
export declare function isValidAddress(address: string): boolean;
/**
* Convert a decimal Ether value (number|string|bigint) to a Wei value as a string (base 10, not hex).
* E.g., 1.23 -> '1230000000000000000'
*/
export declare function decimalToWei(value: number | string | bigint, decimals?: number): string;
/**
* Extract a Kyber public key (ek) from a secret key (dk) hex string.
* If input already appears to be a public key (800/1184/1568 bytes), it is returned as-is.
* Returns a lowercase hex string without 0x prefix.
*/
export declare function kyberPrivateKeyToEncryptedPublicKeyAddress(skHex: string): string;
export declare function kyberPrivateKeyToPublicKeyAddress(skHex: string): string;
export declare function mldsaPublicKeyToAddress(publicKey: string): string;
/**
* Sign a message using MLDSA with a private key.
* @param message - The message to sign
* @param privateKey - The private key to sign with (hex string)
* @param algorithm - The MLDSA algorithm to use ('ml_dsa44', 'ml_dsa65', or 'ml_dsa87')
* @returns Promise<string> - The signature as a hex string
*/
export declare function signMessageMLDSA(message: string, privateKey: string, algorithm?: 'ml_dsa44' | 'ml_dsa65' | 'ml_dsa87'): Promise<string>;
/**
* Verify an MLDSA signature against a message and public key.
* @param message - The original message that was signed
* @param signature - The MLDSA signature (hex string)
* @param publicKey - The public key to verify against (hex string)
* @param algorithm - The MLDSA algorithm to use ('ml_dsa44', 'ml_dsa65', or 'ml_dsa87')
* @returns Promise<boolean> - True if signature is valid, false otherwise
*/
export declare function verifyMLDSASignature(message: string, signature: string, publicKey: string, algorithm?: 'ml_dsa44' | 'ml_dsa65' | 'ml_dsa87'): Promise<boolean>;
/**
* Generate a new MLDSA key pair.
* @param algorithm - The MLDSA algorithm to use ('ml_dsa44', 'ml_dsa65', or 'ml_dsa87')
* @returns Promise<{publicKey: string, privateKey: string}> - The key pair as hex strings
*/
export declare function generateMLDSAKeyPair(algorithm?: 'ml_dsa44' | 'ml_dsa65' | 'ml_dsa87'): Promise<{
publicKey: string;
privateKey: string;
}>;
/**
* Derive or validate an MLDSA public key from a provided key hex.
* - If the provided hex already looks like a public key for the given algorithm, it is returned (lowercase, no 0x).
* - If it looks like a secret key for the given algorithm, deriving the public key is not supported by the
* current bundled API. In this case, an error is thrown with guidance to persist the public key at keygen time.
*
* Note: MLDSA secret keys (Dilithium) in this bundle do not embed the public key; reconstructing requires internal
* primitives that are not exported. Persist the `publicKey` from `generateMLDSAKeyPair` alongside the `privateKey`.
*/
export declare function mldsaPrivateKeyToPublicKey(keyHex: string, algorithm?: 'ml_dsa44' | 'ml_dsa65' | 'ml_dsa87'): Promise<string>;