diff --git a/types/ed25519/ed25519-tests.ts b/types/ed25519/ed25519-tests.ts new file mode 100644 index 0000000000..b8bbcb5d31 --- /dev/null +++ b/types/ed25519/ed25519-tests.ts @@ -0,0 +1,15 @@ +import * as crypto from 'crypto'; +import * as ed25519 from 'ed25519'; + +function signAndValidatePlaintext() { + const seed: Buffer = crypto.randomBytes(32); + const keyPair: ed25519.CurveKeyPair = ed25519.MakeKeypair(seed); + + const plaintext = Buffer.from('plaintext'); + const signature: Buffer = ed25519.Sign(plaintext, keyPair.privateKey); + + const matches: boolean = ed25519.Verify(plaintext, signature, keyPair.publicKey); + if (!matches) { + throw new Error("Sign and verify should work for the same plaintext"); + } +} diff --git a/types/ed25519/index.d.ts b/types/ed25519/index.d.ts new file mode 100644 index 0000000000..bc280afe0c --- /dev/null +++ b/types/ed25519/index.d.ts @@ -0,0 +1,46 @@ +// Type definitions for ed25519 0.0 +// Project: https://github.com/dazoe/ed25519 +// Definitions by: Erik Mavrinac +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// Main site: https://ed25519.cr.yp.to/ +// Manually generated and maintained because the package is a +// veneer on an underlying C library; auto-generation won't work. +// The JavaScript-C interface is well described at +// https://github.com/dazoe/ed25519/blob/master/src/ed25519.cc + +/// + +/** The key material returned from a call to MakeKeypair(). */ +export interface CurveKeyPair { + /** + * A Buffer containing the public portion of the Curve25519 key. + */ + publicKey: Buffer; + + /** + * A Buffer containing the private, secret portion of the Curve25519 key. + */ + privateKey: Buffer; +} + +/** + * Uses the crytpographically strong random seed to generate a + * Curve25519 key pair. + * @return The public and private key pair. + */ +export function MakeKeypair(seed: Buffer): CurveKeyPair; + +/** + * Signs a plaintext message buffer using the private key generated using + * MakeKeypair(). + * @return The signature calculated on the plaintext. + */ +export function Sign(message: Buffer, privateKeyOrKeyPair: Buffer | CurveKeyPair): Buffer; + +/** + * Verifies a signature for a message buffer using a + * public key generated using MakeKeypair(). + * @return True if the signature validates correctly, false otherwise. + */ +export function Verify(message: Buffer, signature: Buffer, publicKey: Buffer): boolean; diff --git a/types/ed25519/tsconfig.json b/types/ed25519/tsconfig.json new file mode 100644 index 0000000000..98fa33a4b1 --- /dev/null +++ b/types/ed25519/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", + "ed25519-tests.ts" + ] +} diff --git a/types/ed25519/tslint.json b/types/ed25519/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/ed25519/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}