From 1968358c88a97c20bd09d6aa9c6af8209acff4e8 Mon Sep 17 00:00:00 2001 From: Bryson Thill Date: Wed, 26 Jul 2017 07:07:41 -0700 Subject: [PATCH] [ethjs-signer] introduce typings (#18338) * [ethjs-signer] introduce typings * split function into two overloads in ethjs-signer package --- types/ethjs-signer/ethjs-signer-tests.ts | 16 ++++++++++++++++ types/ethjs-signer/index.d.ts | 18 ++++++++++++++++++ types/ethjs-signer/tsconfig.json | 22 ++++++++++++++++++++++ types/ethjs-signer/tslint.json | 3 +++ 4 files changed, 59 insertions(+) create mode 100644 types/ethjs-signer/ethjs-signer-tests.ts create mode 100644 types/ethjs-signer/index.d.ts create mode 100644 types/ethjs-signer/tsconfig.json create mode 100644 types/ethjs-signer/tslint.json diff --git a/types/ethjs-signer/ethjs-signer-tests.ts b/types/ethjs-signer/ethjs-signer-tests.ts new file mode 100644 index 0000000000..7f6939fc3b --- /dev/null +++ b/types/ethjs-signer/ethjs-signer-tests.ts @@ -0,0 +1,16 @@ +import { sign, recover } from "ethjs-signer"; + +const nonce = 0; +const privateKey = '0xecbcd9838f7f2afa6e809df8d7cdae69aa5dfc14d563ee98e97effd3f6a652f2'; + +const transaction = { + to: '0xce31a19193d4b23f4e9d6163d7247243bAF801c3', + value: 300000, + gasPrice: 2000000, + nonce +}; + +const signedTransactionString = sign(transaction, privateKey) as string; +const signedTransaction = sign(transaction, privateKey, true) as any[]; + +const publicKey = recover(signedTransactionString, -1, signedTransaction[7], signedTransaction[8]); diff --git a/types/ethjs-signer/index.d.ts b/types/ethjs-signer/index.d.ts new file mode 100644 index 0000000000..969ed9e9ea --- /dev/null +++ b/types/ethjs-signer/index.d.ts @@ -0,0 +1,18 @@ +// Type definitions for ethjs-signer 0.1 +// Project: https://github.com/ethjs/ethjs-signer/ +// Definitions by: Bryson Thill +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +/** + * Signs a raw transaction and return it either as a serialized hex string or raw tx object. + */ +export function sign(transaction: object, privateKey: string, toObject?: false): string; +export function sign(transaction: object, privateKey: string, toObject: true): any[]; + +/** + * Recovers a ECDSA public key from a raw transaction. + */ +export function recover(rawTx: string | Buffer, v: number, r: Buffer, s: Buffer): Buffer; diff --git a/types/ethjs-signer/tsconfig.json b/types/ethjs-signer/tsconfig.json new file mode 100644 index 0000000000..202b9ca11a --- /dev/null +++ b/types/ethjs-signer/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [ ], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ethjs-signer-tests.ts" + ] +} \ No newline at end of file diff --git a/types/ethjs-signer/tslint.json b/types/ethjs-signer/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/ethjs-signer/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file