diff --git a/types/secp256k1/index.d.ts b/types/secp256k1/index.d.ts new file mode 100644 index 0000000000..686171565b --- /dev/null +++ b/types/secp256k1/index.d.ts @@ -0,0 +1,144 @@ +// Type definitions for secp256k1 3.5 +// Project: https://github.com/cryptocoinjs/secp256k1-node +// Definitions by: Anler +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +/** Options for the `sign` function */ +export interface SignOptions { + /** Nonce generator. By default it is rfc6979 */ + noncefn?: (message: Buffer, privateKey: Buffer, algo: Buffer | null, + data: Buffer | null, attempt: number) => Buffer; + + /** + * Additional data for noncefn (RFC 6979 3.6) (32 bytes). + * + * By default is `null`. + */ + data?: Buffer; +} + +/** + * Verify an ECDSA privateKey. + */ +export function privateKeyVerify(privateKey: Buffer): boolean; + +/** + * Export a privateKey in DER format. + */ +export function privateKeyExport(privateKey: Buffer, compressed?: boolean): Buffer; + +/** + * Import a privateKey in DER format. + */ +export function privateKeyImport(privateKey: Buffer): Buffer; + +/** + * Negate a privateKey by subtracting it from the order of the curve's base point. + */ +export function privateKeyNegate(privateKey: Buffer): Buffer; + +/** + * Compute the inverse of a privateKey (modulo the order of the curve's base point). + */ +export function privateKeyModInverse(privateKey: Buffer): Buffer; + +/** + * Tweak a privateKey by adding tweak to it. + */ +export function privateKeyTweakAdd(privateKey: Buffer, tweak: Buffer): Buffer; + +/** + * Tweak a privateKey by multiplying it by a tweak. + */ +export function privateKeyTweakMul(privateKey: Buffer, tweak: Buffer): Buffer; + +/** + * Compute the public key for a privateKey. + */ +export function publicKeyCreate(privateKey: Buffer, compressed?: boolean): Buffer; + +/** + * Convert a publicKey to compressed or uncompressed form. + */ +export function publicKeyConvert(publicKey: Buffer, compressed?: boolean): Buffer; + +/** + * Verify an ECDSA publicKey. + */ +export function publicKeyVerify(publicKey: Buffer): boolean; + +/** + * Tweak a publicKey by adding tweak times the generator to it. + */ +export function publicKeyTweakAdd(publicKey: Buffer, tweak: Buffer, compressed?: boolean): Buffer; + +/** + * Tweak a publicKey by multiplying it by a tweak value. + */ +export function publicKeyTweakMul(publicKey: Buffer, tweak: Buffer, compressed?: boolean): Buffer; + +/** + * Add a given publicKeys together. + */ +export function publicKeyCombine(publicKeys: Buffer[], compressed?: boolean): Buffer; + +/** + * Convert a signature to a normalized lower-S form. + */ +export function signatureNormalize(signature: Buffer): Buffer; + +/** + * Serialize an ECDSA signature in DER format. + */ +export function signatureExport(signature: Buffer): Buffer; + +/** + * Parse a DER ECDSA signature (follow by BIP66). + */ +export function signatureImport(signature: Buffer): Buffer; + +/** + * Same as `signatureImport` but not follow by BIP66. + */ +export function signatureImportLax(signature: Buffer): Buffer; + +/** + * Create an ECDSA signature. Always return low-S signature. + * + * Inputs: 32-byte message `m`, 32-byte scalar key `d`, 32-byte scalar nonce `k`. + * - Compute point `R = k * G`. Reject nonce if R's `x` coordinate is zero. + * - Compute 32-byte scalar `r`, the serialization of R's `x` coordinate. + * - Compose 32-byte scalar `s = k^-1 * (r * d + m)`. Reject nonce if `s` is zero. + * - The signature is `(r, s)`. + */ +export function sign(message: Buffer, privateKey: Buffer, options?: SignOptions): {signature: Buffer, recovery: number}; + +/** + * Verify an ECDSA signature. + * + * Note: return false for high signatures! + * + * Inputs: 32-byte message `m`, public key point `Q`, signature: (32-byte `r`, scalar `s`). + * - Signature is invalid if `r` is zero. + * - Signature is invalid if `s` is zero. + * - Compute point `R = (s^-1 * m * G + s^-1 * r * Q)`. Reject if `R` is infinity. + * - Signature is valid if R's `x` coordinate equals to `r`. + */ +export function verify(message: Buffer, signature: Buffer, publicKey: Buffer): boolean; + +/** + * Recover an ECDSA public key from a signature. + */ +export function recover(message: Buffer, signature: Buffer, recovery: number, compressed?: boolean): Buffer; + +/** + * Compute an EC Diffie-Hellman secret and applied sha256 to compressed public key. + */ +export function ecdh(publicKey: Buffer, privateKey: Buffer): Buffer; + +/** + * Compute an EC Diffie-Hellman secret and return public key as result. + */ +export function ecdhUnsafe(publicKey: Buffer, privateKey: Buffer, compressed?: boolean): Buffer; diff --git a/types/secp256k1/secp256k1-tests.ts b/types/secp256k1/secp256k1-tests.ts new file mode 100644 index 0000000000..03754354b1 --- /dev/null +++ b/types/secp256k1/secp256k1-tests.ts @@ -0,0 +1,11 @@ +import { SignOptions, sign } from "secp256k1"; + +const opts: SignOptions = { + data: Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]) +}; + +const message = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + +const prvKey = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + +sign(message, prvKey, opts); diff --git a/types/secp256k1/tsconfig.json b/types/secp256k1/tsconfig.json new file mode 100644 index 0000000000..8d15f80548 --- /dev/null +++ b/types/secp256k1/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "secp256k1-tests.ts" + ] +} diff --git a/types/secp256k1/tslint.json b/types/secp256k1/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/secp256k1/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }