diff --git a/types/elgamal/elgamal-tests.ts b/types/elgamal/elgamal-tests.ts new file mode 100644 index 0000000000..46703b2a8a --- /dev/null +++ b/types/elgamal/elgamal-tests.ts @@ -0,0 +1,11 @@ +import ElGamal from 'elgamal'; + +(async () => { + const eg = await ElGamal.generateAsync(); + + const secret = 'The quick brown fox jumps over the lazy dog'; + const encrypted = await eg.encryptAsync(secret); // $ExpectType EncryptedValue + const decrypted = await eg.decryptAsync(encrypted); // $ExpectType DecryptedValue + + decrypted.toString(); // $ExpectType string +})(); diff --git a/types/elgamal/index.d.ts b/types/elgamal/index.d.ts new file mode 100644 index 0000000000..39b3de597a --- /dev/null +++ b/types/elgamal/index.d.ts @@ -0,0 +1,42 @@ +// Type definitions for elgamal 0.3 +// Project: https://github.com/kripod/elgamal.js +// Definitions by: Yu-Hsi Chiang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { BigInteger as BigInt } from 'jsbn'; + +export default class ElGamal { + p: BigInt; + g: BigInt; + y: BigInt; + x: BigInt; + static generateAsync(primeBits?: number): Promise; + constructor(p: BigInt | string | number, g: BigInt | string | number, y: BigInt | string | number, x: BigInt | string | number); + encryptAsync(m: BigInt | string | number, k?: BigInt | string | number): Promise; + decryptAsync(m: EncryptedValue): Promise; +} + +export { BigInt }; + +export class DecryptedValue { + bi: BigInt; + constructor(m: BigInt | string | number); + toString(): string; +} + +export class EncryptedValue { + a: BigInt; + b: BigInt; + constructor(a: BigInt, b: BigInt); + multiply(encryptedValue: EncryptedValue): EncryptedValue; +} + +export namespace Utils { + const BIG_TWO: BigInt; + function getRandomNbitBigIntAsync(bits: number): Promise; + function getRandomBigIntAsync(min: BigInt, max: BigInt): Promise; + function getBigPrimeAsync(bits: number): Promise; + function parseBigInt(obj: BigInt | string | number): BigInt | null; +} + +export class MissingPrivateKeyError extends Error { } diff --git a/types/elgamal/tsconfig.json b/types/elgamal/tsconfig.json new file mode 100644 index 0000000000..18590eb518 --- /dev/null +++ b/types/elgamal/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "elgamal-tests.ts" + ] +} diff --git a/types/elgamal/tslint.json b/types/elgamal/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/elgamal/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }