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

Commit

Permalink
Merge pull request #1 from ethereumjs/master
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
nebojsa94 committed Jun 23, 2020
2 parents 58c2476 + d485939 commit 021add9
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 21 deletions.
1 change: 1 addition & 0 deletions 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'],
Expand Down
69 changes: 69 additions & 0 deletions 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
}
2 changes: 1 addition & 1 deletion 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'
Expand Down
2 changes: 1 addition & 1 deletion 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'

Expand Down
7 changes: 0 additions & 7 deletions 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)
*/
Expand Down
2 changes: 1 addition & 1 deletion 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'
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
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
@@ -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
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
@@ -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
@@ -1,7 +1,6 @@
{
"extends": "@ethereumjs/config-tsc",
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src/**/*.ts"]
}
}

0 comments on commit 021add9

Please sign in to comment.