From ae4082631b17799a2fb083c447cb98df80e5ffb6 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Tue, 28 Apr 2020 14:46:26 -0700 Subject: [PATCH 1/3] Define ethjs-util types --- src/externals.ts | 7 ----- src/index.ts | 10 +++++- test/externals.spec.ts | 10 +++--- tsconfig.json | 2 +- tsconfig.prod.json | 2 +- typings/ethjs-util.d.ts | 69 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 typings/ethjs-util.d.ts diff --git a/src/externals.ts b/src/externals.ts index 2e233bdc..01aea919 100644 --- a/src/externals.ts +++ b/src/externals.ts @@ -1,19 +1,12 @@ /** * Re-exports commonly used modules: - * * Adds [`ethjs-util`](https://github.com/ethjs/ethjs-util) methods. * * Exports [`BN`](https://github.com/indutny/bn.js), [`rlp`](https://github.com/ethereumjs/rlp). * @packageDocumentation */ -const ethjsUtil = require('ethjs-util') import * as BN from 'bn.js' import * as rlp from 'rlp' -/** - * [`ethjsUtil`](https://github.com/ethjs/ethjs-util) - */ -Object.assign(exports, ethjsUtil) - /** * [`BN`](https://github.com/indutny/bn.js) */ diff --git a/src/index.ts b/src/index.ts index 73e913d3..a87fd2ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,6 @@ +/// +import * as ethjsUtil from 'ethjs-util' + /** * Constants */ @@ -29,6 +32,11 @@ export * from './bytes' export * from './object' /** - * External exports (ethjsUtil, BN, rlp, secp256k1) + * External exports (BN, rlp, secp256k1) */ export * from './externals' + +/** + * Export ethjs-util methods + */ +export * from 'ethjs-util' diff --git a/test/externals.spec.ts b/test/externals.spec.ts index 6d8402ba..1d837c92 100644 --- a/test/externals.spec.ts +++ b/test/externals.spec.ts @@ -101,24 +101,24 @@ describe('External ethjsUtil export', () => { it('should use ethjsUtil functions correctly', () => { // should convert intToHex - assert.equal((src as any).intToHex(new src.BN(0)), '0x0') + assert.equal(src.intToHex(new src.BN(0).toNumber()), '0x0') // should convert intToHex const i = 6003400 - const hex = (src as any).intToHex(i) + const hex = src.intToHex(i) assert.equal(hex, '0x5b9ac8') // should convert a int to a buffer const j = 6003400 - const buf = (src as any).intToBuffer(j) + const buf = src.intToBuffer(j) assert.equal(buf.toString('hex'), '5b9ac8') }) it('should handle exceptions and invalid inputs', () => { // should throw when invalid abi - assert.throws(() => (src as any).getKeys([], 3289), Error) + assert.throws(() => src.getKeys([], (3289) as string), Error) // should detect invalid length hex string - assert.equal((src as any).isHexString('0x0', 2), false) + assert.equal(src.isHexString('0x0', 2), false) }) }) diff --git a/tsconfig.json b/tsconfig.json index ddbd8a97..983313f2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "@ethereumjs/config-tsc", - "include": ["src/**/*.ts", "test/**/*.ts"] + "include": ["src/**/*.ts", "test/**/*.ts", "typings/**/*.js"] } diff --git a/tsconfig.prod.json b/tsconfig.prod.json index 184d95b5..7b74b45d 100644 --- a/tsconfig.prod.json +++ b/tsconfig.prod.json @@ -3,5 +3,5 @@ "compilerOptions": { "outDir": "./dist" }, - "include": ["src/**/*.ts"] + "include": ["src/**/*.ts", "typings/**/*.ts"] } diff --git a/typings/ethjs-util.d.ts b/typings/ethjs-util.d.ts new file mode 100644 index 00000000..1b243730 --- /dev/null +++ b/typings/ethjs-util.d.ts @@ -0,0 +1,69 @@ +declare module 'ethjs-util' { + /** + * @description Returns a `Boolean` on whether or not the a `String` starts with '0x' + */ + export function isHexPrefixed(str: string): boolean + + /** + * @description Removes '0x' from a given `String` if present + */ + export function stripHexPrefix(str: string): string + + /** + * @description Pads a `String` to have an even length + */ + export function padToEven(value: string): string + + /** + * @description Converts a `Number` into a hex `String` + */ + export function intToHex(i: number): string + + /** + * @description Converts an `Number` to a `Buffer` + */ + export function intToBuffer(i: number): Buffer + + /** + * @description Get the binary size of a string + */ + export function getBinarySize(str: string): number + + /** + * @description Returns TRUE if the first specified array contains all elements + * from the second one. FALSE otherwise. If `some` is true, will + * return true if first specified array contain some elements of + * the second. + */ + export function arrayContainsArray(superset: any[], subset: any[], some?: boolean): boolean + + /** + * @description Should be called to get utf8 from it's hex representation + */ + export function toUtf8(hex: string): string + + /** + * @description Should be called to get ascii from it's hex representation + */ + export function toAscii(hex: string): string + + /** + * @description Should be called to get hex representation (prefixed by 0x) of utf8 string + */ + export function fromUtf8(stringValue: string): string + + /** + * @description Should be called to get hex representation (prefixed by 0x) of ascii string + */ + export function fromAscii(stringValue: string): string + + /** + * @description getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3] + */ + export function getKeys(params: any[], key: string, allowEmpty?: boolean): any[] + + /** + * @description check if string is hex string of specific length + */ + export function isHexString(value: string, length?: number): boolean +} From 8620487ee128b31fc3c6d9665dde67cac04306fa Mon Sep 17 00:00:00 2001 From: cgewecke Date: Tue, 28 Apr 2020 14:48:31 -0700 Subject: [PATCH 2/3] Use import syntax for ethjs-util internally --- src/account.ts | 2 +- src/bytes.ts | 2 +- src/hash.ts | 2 +- src/helpers.ts | 2 +- src/object.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/account.ts b/src/account.ts index 0d1fb63e..00f7bbc9 100644 --- a/src/account.ts +++ b/src/account.ts @@ -1,4 +1,4 @@ -const ethjsUtil = require('ethjs-util') +import * as ethjsUtil from 'ethjs-util' import * as assert from 'assert' import * as secp256k1 from 'secp256k1' import * as BN from 'bn.js' diff --git a/src/bytes.ts b/src/bytes.ts index 39634853..94492aa4 100644 --- a/src/bytes.ts +++ b/src/bytes.ts @@ -1,4 +1,4 @@ -const ethjsUtil = require('ethjs-util') +import * as ethjsUtil from 'ethjs-util' import * as BN from 'bn.js' import { assertIsBuffer, assertIsArray, assertIsHexString } from './helpers' diff --git a/src/hash.ts b/src/hash.ts index e9e40ca7..57e06d81 100644 --- a/src/hash.ts +++ b/src/hash.ts @@ -1,6 +1,6 @@ const createKeccakHash = require('keccak') const createHash = require('create-hash') -const ethjsUtil = require('ethjs-util') +import * as ethjsUtil from 'ethjs-util' import * as rlp from 'rlp' import { toBuffer, setLengthLeft } from './bytes' import { assertIsString, assertIsBuffer, assertIsArray, assertIsHexString } from './helpers' diff --git a/src/helpers.ts b/src/helpers.ts index c6959678..e1403366 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,4 +1,4 @@ -const ethjsUtil = require('ethjs-util') +import * as ethjsUtil from 'ethjs-util' /** * Throws if a string is not hex prefixed diff --git a/src/object.ts b/src/object.ts index fb3cb314..e24991eb 100644 --- a/src/object.ts +++ b/src/object.ts @@ -1,4 +1,4 @@ -const ethjsUtil = require('ethjs-util') +import * as ethjsUtil from 'ethjs-util' import * as assert from 'assert' import * as rlp from 'rlp' import { toBuffer, baToJSON, unpadBuffer } from './bytes' From 40ecfe9098ac8f71c805894d2b3c3dbded21cb2e Mon Sep 17 00:00:00 2001 From: tomonari_t Date: Thu, 4 Jun 2020 08:27:18 -0700 Subject: [PATCH 3/3] Fix ethjs-util types resolution --- karma.conf.js | 1 + .../ethjs-util.d.ts => src/@types/ethjs-util/index.d.ts | 0 src/index.ts | 3 --- tsconfig.json | 8 +++++++- tsconfig.prod.json | 5 ++--- 5 files changed, 10 insertions(+), 7 deletions(-) rename typings/ethjs-util.d.ts => src/@types/ethjs-util/index.d.ts (100%) diff --git a/karma.conf.js b/karma.conf.js index 82a17b5f..f166a9e5 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,6 +1,7 @@ module.exports = function(config) { config.set({ frameworks: ['mocha', 'karma-typescript'], + exclude: ["src/@types/**"], // ref: https://github.com/monounity/karma-typescript/issues/254 files: ['src/**/*.ts', 'test/**/*.ts'], preprocessors: { '**/*.ts': ['karma-typescript'], diff --git a/typings/ethjs-util.d.ts b/src/@types/ethjs-util/index.d.ts similarity index 100% rename from typings/ethjs-util.d.ts rename to src/@types/ethjs-util/index.d.ts diff --git a/src/index.ts b/src/index.ts index a87fd2ff..37831e28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,3 @@ -/// -import * as ethjsUtil from 'ethjs-util' - /** * Constants */ diff --git a/tsconfig.json b/tsconfig.json index 983313f2..e80502d7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,10 @@ { "extends": "@ethereumjs/config-tsc", - "include": ["src/**/*.ts", "test/**/*.ts", "typings/**/*.js"] + "compilerOptions": { + "baseUrl": ".", + "paths": { + "*": ["src/@types/*"] + } + }, + "include": ["src/**/*.ts", "test/**/*.ts"] } diff --git a/tsconfig.prod.json b/tsconfig.prod.json index 7b74b45d..f93323e1 100644 --- a/tsconfig.prod.json +++ b/tsconfig.prod.json @@ -1,7 +1,6 @@ { - "extends": "@ethereumjs/config-tsc", + "extends": "./tsconfig.json", "compilerOptions": { "outDir": "./dist" - }, - "include": ["src/**/*.ts", "typings/**/*.ts"] + } }