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/src/@types/ethjs-util/index.d.ts b/src/@types/ethjs-util/index.d.ts new file mode 100644 index 00000000..1b243730 --- /dev/null +++ b/src/@types/ethjs-util/index.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 +} 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/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/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/index.ts b/src/index.ts index 73e913d3..37831e28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,6 +29,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/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' 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..e80502d7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,10 @@ { "extends": "@ethereumjs/config-tsc", + "compilerOptions": { + "baseUrl": ".", + "paths": { + "*": ["src/@types/*"] + } + }, "include": ["src/**/*.ts", "test/**/*.ts"] } diff --git a/tsconfig.prod.json b/tsconfig.prod.json index 184d95b5..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"] + } }