Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Add typescript definitions for ethjs-util methods #248

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down
69 changes: 69 additions & 0 deletions src/@types/ethjs-util/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion src/account.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/bytes.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
7 changes: 0 additions & 7 deletions src/externals.ts
Original file line number Diff line number Diff line change
@@ -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)
*/
Expand Down
2 changes: 1 addition & 1 deletion src/hash.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ethjsUtil = require('ethjs-util')
import * as ethjsUtil from 'ethjs-util'

/**
* Throws if a string is not hex prefixed
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion src/object.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
10 changes: 5 additions & 5 deletions test/externals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([], (<unknown>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)
})
})
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"extends": "@ethereumjs/config-tsc",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": ["src/@types/*"]
}
},
"include": ["src/**/*.ts", "test/**/*.ts"]
}
5 changes: 2 additions & 3 deletions tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "@ethereumjs/config-tsc",
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src/**/*.ts"]
}
}