diff --git a/lerna.json b/lerna.json index 0b60c92ef..46a90aa76 100644 --- a/lerna.json +++ b/lerna.json @@ -10,5 +10,5 @@ "packages": [ "packages/*" ], - "version": "0.22.11" + "version": "0.23.0" } diff --git a/package.json b/package.json index 1bb12fbcd..318a1d053 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,6 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.19.25" + "@polkadot/dev": "^0.19.28" } } diff --git a/packages/util-crypto/package.json b/packages/util-crypto/package.json index 55088bae4..24814bd2b 100644 --- a/packages/util-crypto/package.json +++ b/packages/util-crypto/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-crypto", - "version": "0.22.11", + "version": "0.23.0", "description": "A collection of useful crypto utilities for @polkadot", "main": "index.js", "keywords": [ @@ -34,7 +34,7 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.22.11", + "@polkadot/util": "^0.23.0", "blakejs": "^1.1.0", "js-sha3": "^0.7.0", "tweetnacl": "^1.0.0", diff --git a/packages/util-crypto/src/blake2/asHex.js b/packages/util-crypto/src/blake2/asHex.js index 73dd38aa6..24e3c2c61 100644 --- a/packages/util-crypto/src/blake2/asHex.js +++ b/packages/util-crypto/src/blake2/asHex.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const blake2bAsHex = require('./blake2b/asHex'); +import blake2bAsHex from './blake2b/asHex'; /** @name blake2AsHex @@ -16,6 +16,6 @@ const blake2bAsHex = require('./blake2b/asHex'); blake2AsHex('abc') // => 0xba80a53f981c4d0d */ -module.exports = function blake2AsHex (data: Uint8Array, bitLength: number = 256): string { +export default function blake2AsHex (data: Uint8Array, bitLength: number = 256): string { return blake2bAsHex(data, bitLength); -}; +} diff --git a/packages/util-crypto/src/blake2/asHex.spec.js b/packages/util-crypto/src/blake2/asHex.spec.js index edf41a354..59e6797c1 100644 --- a/packages/util-crypto/src/blake2/asHex.spec.js +++ b/packages/util-crypto/src/blake2/asHex.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import hexToU8a from '@polkadot/util/hex/toU8a'; -const { blake2AsHex } = require('./index'); +import { blake2AsHex } from './index'; describe('blake2AsHex', () => { it('returns a 64-bit value (specified)', () => { diff --git a/packages/util-crypto/src/blake2/asU8a.js b/packages/util-crypto/src/blake2/asU8a.js index 7c10ed289..8f89ae63b 100644 --- a/packages/util-crypto/src/blake2/asU8a.js +++ b/packages/util-crypto/src/blake2/asU8a.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const blake2bAsU8a = require('./blake2b/asU8a'); +import blake2bAsU8a from './blake2b/asU8a'; /** @name blake2AsU8a @@ -16,6 +16,6 @@ const blake2bAsU8a = require('./blake2b/asU8a'); blake2AsU8a('abc') // => [0xba, 0x80, 0xa53, 0xf98, 0x1c, 0x4d, 0x0d] */ -module.exports = function blake2AsU8a (data: Uint8Array, bitLength: number = 256, key?: Uint8Array): Uint8Array { +export default function blake2AsU8a (data: Uint8Array, bitLength: number = 256, key?: Uint8Array): Uint8Array { return blake2bAsU8a(data, bitLength, key); -}; +} diff --git a/packages/util-crypto/src/blake2/asU8a.spec.js b/packages/util-crypto/src/blake2/asU8a.spec.js index c45919f7f..78e2c9d1a 100644 --- a/packages/util-crypto/src/blake2/asU8a.spec.js +++ b/packages/util-crypto/src/blake2/asU8a.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import hexToU8a from '@polkadot/util/hex/toU8a'; -const { blake2AsU8a } = require('./index'); +import { blake2AsU8a } from './index'; describe('blake2AsU8a', () => { it('returns a 64-bit value by default', () => { diff --git a/packages/util-crypto/src/blake2/blake2b/asHex.js b/packages/util-crypto/src/blake2/blake2b/asHex.js index 3a3817456..b74f167eb 100644 --- a/packages/util-crypto/src/blake2/blake2b/asHex.js +++ b/packages/util-crypto/src/blake2/blake2b/asHex.js @@ -3,8 +3,8 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aToHex = require('@polkadot/util/u8a/toHex'); -const blake2bAsU8a = require('./asU8a'); +import u8aToHex from '@polkadot/util/u8a/toHex'; +import blake2bAsU8a from './asU8a'; /** @name blake2bAsHex @@ -17,8 +17,8 @@ const blake2bAsU8a = require('./asU8a'); blake2bAsHex('abc') // => '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923' */ -module.exports = function blake2bAsHex (data: Uint8Array, bitLength: number = 512): string { +export default function blake2bAsHex (data: Uint8Array, bitLength: number = 512): string { return u8aToHex( blake2bAsU8a(data, bitLength) ); -}; +} diff --git a/packages/util-crypto/src/blake2/blake2b/asHex.spec.js b/packages/util-crypto/src/blake2/blake2b/asHex.spec.js index 21123e21b..5bfb90793 100644 --- a/packages/util-crypto/src/blake2/blake2b/asHex.spec.js +++ b/packages/util-crypto/src/blake2/blake2b/asHex.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import hexToU8a from '@polkadot/util/hex/toU8a'; -const { blake2bAsHex } = require('./index'); +import { blake2bAsHex } from './index'; describe('blake2bAsHex', () => { it('creates the correct hash', () => { diff --git a/packages/util-crypto/src/blake2/blake2b/asU8a.js b/packages/util-crypto/src/blake2/blake2b/asU8a.js index db597c788..61cef941a 100644 --- a/packages/util-crypto/src/blake2/blake2b/asU8a.js +++ b/packages/util-crypto/src/blake2/blake2b/asU8a.js @@ -4,7 +4,7 @@ // @flow // TODO: For Node we can also use node-blake2 (npm blake2) -const blakejs = require('blakejs'); +import blakejs from 'blakejs'; /** @name blake2bAsU8a @@ -17,6 +17,6 @@ const blakejs = require('blakejs'); blake2bAsU8a('abc') // => Uint8Array('508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982') */ -module.exports = function blake2bAsU8a (data: Uint8Array, bitLength: number = 512, key: ?Uint8Array = null): Uint8Array { +export default function blake2bAsU8a (data: Uint8Array, bitLength: number = 512, key: ?Uint8Array = null): Uint8Array { return blakejs.blake2b(data, key, bitLength / 8); -}; +} diff --git a/packages/util-crypto/src/blake2/blake2b/asU8a.spec.js b/packages/util-crypto/src/blake2/blake2b/asU8a.spec.js index b2b754a5d..7ad706626 100644 --- a/packages/util-crypto/src/blake2/blake2b/asU8a.spec.js +++ b/packages/util-crypto/src/blake2/blake2b/asU8a.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { blake2bAsU8a } = require('./index'); +import { blake2bAsU8a } from './index'; describe('blake2bAsU8a', () => { it('creates the correct hash (default length)', () => { diff --git a/packages/util-crypto/src/blake2/blake2b/index.js b/packages/util-crypto/src/blake2/blake2b/index.js index 90476ffc3..4de819f05 100644 --- a/packages/util-crypto/src/blake2/blake2b/index.js +++ b/packages/util-crypto/src/blake2/blake2b/index.js @@ -3,13 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -const blake2bAsHex = require('./asHex'); -const blake2bAsU8a = require('./asU8a'); +import blake2bAsHex from './asHex'; +import blake2bAsU8a from './asU8a'; /** @summary Create [Blake2b](https://blake2.net/) values as hex & Uint8Array output */ -module.exports = { +export { blake2bAsHex, blake2bAsU8a }; diff --git a/packages/util-crypto/src/blake2/index.js b/packages/util-crypto/src/blake2/index.js index bedaa0422..891a651b4 100644 --- a/packages/util-crypto/src/blake2/index.js +++ b/packages/util-crypto/src/blake2/index.js @@ -3,13 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -const blake2AsHex = require('./asHex'); -const blake2AsU8a = require('./asU8a'); +import blake2AsHex from './asHex'; +import blake2AsU8a from './asU8a'; /** @summary Create blake2b values with specified bitlengths */ -module.exports = { +export { blake2AsHex, blake2AsU8a }; diff --git a/packages/util-crypto/src/index.js b/packages/util-crypto/src/index.js index 1bb415346..ba11b1fc4 100644 --- a/packages/util-crypto/src/index.js +++ b/packages/util-crypto/src/index.js @@ -3,16 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const blake2 = require('./blake2'); -const keccak = require('./keccak'); -const nacl = require('./nacl'); -const random = require('./random'); -const sha512 = require('./sha512'); -const xxhash = require('./xxhash'); - -/** - @summary Utility methods for this package are split into groups -*/ -module.exports = Object.assign( - {}, blake2, keccak, nacl, random, sha512, xxhash -); +export * from './blake2'; +export * from './keccak'; +export * from './nacl'; +export * from './random'; +export * from './sha512'; +export * from './xxhash'; diff --git a/packages/util-crypto/src/keccak/asHex.js b/packages/util-crypto/src/keccak/asHex.js index 61c04e898..dc9546c2a 100644 --- a/packages/util-crypto/src/keccak/asHex.js +++ b/packages/util-crypto/src/keccak/asHex.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aToHex = require('@polkadot/util/u8a/toHex'); +import u8aToHex from '@polkadot/util/u8a/toHex'; -const keccakAsU8a = require('./asU8a'); +import keccakAsU8a from './asU8a'; /** @name keccakAsHex @@ -18,8 +18,8 @@ const keccakAsU8a = require('./asU8a'); keccakAsHex('123') // => 0x... */ -module.exports = function keccakAsHex (value: Buffer | Uint8Array | string): string { +export default function keccakAsHex (value: Buffer | Uint8Array | string): string { return u8aToHex( keccakAsU8a(value) ); -}; +} diff --git a/packages/util-crypto/src/keccak/asHex.spec.js b/packages/util-crypto/src/keccak/asHex.spec.js index 370aab0f9..301f579c4 100644 --- a/packages/util-crypto/src/keccak/asHex.spec.js +++ b/packages/util-crypto/src/keccak/asHex.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { keccakAsHex } = require('./index'); +import { keccakAsHex } from './index'; const value = 'test value'; const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e'; diff --git a/packages/util-crypto/src/keccak/asU8a.js b/packages/util-crypto/src/keccak/asU8a.js index da2ffd706..f5b0cc192 100644 --- a/packages/util-crypto/src/keccak/asU8a.js +++ b/packages/util-crypto/src/keccak/asU8a.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const { keccak256 } = require('js-sha3'); +import { keccak256 } from 'js-sha3'; /** @name keccakAsU8a @@ -16,8 +16,8 @@ const { keccak256 } = require('js-sha3'); keccakAsU8a('123') // => Uint8Array */ -module.exports = function keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array { +export default function keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array { return new Uint8Array( keccak256.arrayBuffer(value) ); -}; +} diff --git a/packages/util-crypto/src/keccak/asU8a.spec.js b/packages/util-crypto/src/keccak/asU8a.spec.js index e2d450553..87b871166 100644 --- a/packages/util-crypto/src/keccak/asU8a.spec.js +++ b/packages/util-crypto/src/keccak/asU8a.spec.js @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import hexToU8a from '@polkadot/util/hex/toU8a'; +import u8aFromString from '@polkadot/util/u8a/fromString'; -const { keccakAsU8a } = require('./index'); +import { keccakAsU8a } from './index'; describe('keccakAsU8a', () => { const input = 'test value'; diff --git a/packages/util-crypto/src/keccak/index.js b/packages/util-crypto/src/keccak/index.js index 2c3f4b2c2..5f1744c93 100644 --- a/packages/util-crypto/src/keccak/index.js +++ b/packages/util-crypto/src/keccak/index.js @@ -3,13 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -const keccakAsHex = require('./asHex'); -const keccakAsU8a = require('./asU8a'); +import keccakAsHex from './asHex'; +import keccakAsU8a from './asU8a'; /** @summary Create Keccak256 values as hex & Uint8Array output */ -module.exports = { +export { keccakAsHex, keccakAsU8a }; diff --git a/packages/util-crypto/src/nacl/decrypt.js b/packages/util-crypto/src/nacl/decrypt.js index 748dbfc98..ec544f03b 100644 --- a/packages/util-crypto/src/nacl/decrypt.js +++ b/packages/util-crypto/src/nacl/decrypt.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name naclDecrypt @@ -16,6 +16,6 @@ const nacl = require('tweetnacl'); naclDecrypt([...], [...], [...]) // => [...] */ -module.exports = function naclDecrypt (encrypted: Uint8Array, nonce: Uint8Array, secret: Uint8Array): ?Uint8Array { +export default function naclDecrypt (encrypted: Uint8Array, nonce: Uint8Array, secret: Uint8Array): ?Uint8Array { return nacl.secretbox.open(encrypted, nonce, secret); -}; +} diff --git a/packages/util-crypto/src/nacl/decrypt.spec.js b/packages/util-crypto/src/nacl/decrypt.spec.js index 49715e508..af8e2801f 100644 --- a/packages/util-crypto/src/nacl/decrypt.spec.js +++ b/packages/util-crypto/src/nacl/decrypt.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { naclDecrypt, naclEncrypt } = require('./index'); +import { naclDecrypt, naclEncrypt } from './index'; describe('naclDecrypt', () => { it('decrypts a encrypted message', () => { diff --git a/packages/util-crypto/src/nacl/encrypt.js b/packages/util-crypto/src/nacl/encrypt.js index 4a25c5067..577d51951 100644 --- a/packages/util-crypto/src/nacl/encrypt.js +++ b/packages/util-crypto/src/nacl/encrypt.js @@ -3,15 +3,15 @@ // of the ISC license. See the LICENSE file for details. // @flow +import nacl from 'tweetnacl'; + +import randomAsU8a from '../random/asU8a'; + type Encrypted = { encrypted: Uint8Array, nonce: Uint8Array }; -const nacl = require('tweetnacl'); - -const randomAsU8a = require('../random/asU8a'); - /** @name naclEncrypt @signature naclEncrypt (message: Uint8Array, secret: Uint8Array, nonce?: Uint8Array): { encrypted: Uint8Array, nonce: Uint8Array } @@ -23,9 +23,9 @@ const randomAsU8a = require('../random/asU8a'); naclSign([...], [...]) // => [...] */ -module.exports = function naclEncrypt (message: Uint8Array, secret: Uint8Array, nonce: Uint8Array = randomAsU8a(24)): Encrypted { +export default function naclEncrypt (message: Uint8Array, secret: Uint8Array, nonce: Uint8Array = randomAsU8a(24)): Encrypted { return { encrypted: nacl.secretbox(message, nonce, secret), nonce }; -}; +} diff --git a/packages/util-crypto/src/nacl/encrypt.spec.js b/packages/util-crypto/src/nacl/encrypt.spec.js index 28acb4f4f..d62ddfda9 100644 --- a/packages/util-crypto/src/nacl/encrypt.spec.js +++ b/packages/util-crypto/src/nacl/encrypt.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { naclEncrypt } = require('./index'); +import { naclEncrypt } from './index'; describe('naclEncrypt', () => { it('encrypts a message', () => { diff --git a/packages/util-crypto/src/nacl/index.js b/packages/util-crypto/src/nacl/index.js index da8f23aaa..aeaac062b 100644 --- a/packages/util-crypto/src/nacl/index.js +++ b/packages/util-crypto/src/nacl/index.js @@ -3,19 +3,19 @@ // of the ISC license. See the LICENSE file for details. // @flow -const naclDecrypt = require('./decrypt'); -const naclEncrypt = require('./encrypt'); -const naclKeypairFromRandom = require('./keypair/fromRandom'); -const naclKeypairFromSecret = require('./keypair/fromSecret'); -const naclKeypairFromSeed = require('./keypair/fromSeed'); -const naclKeypairFromString = require('./keypair/fromString'); -const naclSign = require('./sign'); -const naclVerify = require('./verify'); +import naclDecrypt from './decrypt'; +import naclEncrypt from './encrypt'; +import naclKeypairFromRandom from './keypair/fromRandom'; +import naclKeypairFromSecret from './keypair/fromSecret'; +import naclKeypairFromSeed from './keypair/fromSeed'; +import naclKeypairFromString from './keypair/fromString'; +import naclSign from './sign'; +import naclVerify from './verify'; /** @summary Implements [NaCl](http://nacl.cr.yp.to/) secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures */ -module.exports = { +export { naclDecrypt, naclEncrypt, naclKeypairFromRandom, diff --git a/packages/util-crypto/src/nacl/keypair/fromRandom.js b/packages/util-crypto/src/nacl/keypair/fromRandom.js index 84be602c5..714d80931 100644 --- a/packages/util-crypto/src/nacl/keypair/fromRandom.js +++ b/packages/util-crypto/src/nacl/keypair/fromRandom.js @@ -5,7 +5,7 @@ import type { KeypairType } from '../../types'; -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name naclKeypair @@ -18,6 +18,6 @@ const nacl = require('tweetnacl'); naclKeypair() // => { secretKey: [...], publicKey: [...] } */ -module.exports = function naclKeypairFromRandom (): KeypairType { +export default function naclKeypairFromRandom (): KeypairType { return nacl.sign.keyPair(); -}; +} diff --git a/packages/util-crypto/src/nacl/keypair/fromRandom.spec.js b/packages/util-crypto/src/nacl/keypair/fromRandom.spec.js index 22b37d724..bf0814837 100644 --- a/packages/util-crypto/src/nacl/keypair/fromRandom.spec.js +++ b/packages/util-crypto/src/nacl/keypair/fromRandom.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { naclKeypairFromRandom } = require('../index'); +import { naclKeypairFromRandom } from '../index'; describe('naclKeypairFromRandom', () => { let keypair; diff --git a/packages/util-crypto/src/nacl/keypair/fromSecret.js b/packages/util-crypto/src/nacl/keypair/fromSecret.js index 470f1ba17..795b968a2 100644 --- a/packages/util-crypto/src/nacl/keypair/fromSecret.js +++ b/packages/util-crypto/src/nacl/keypair/fromSecret.js @@ -5,7 +5,7 @@ import type { KeypairType } from '../../types'; -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name naclKeypairFromSecret @@ -18,6 +18,6 @@ const nacl = require('tweetnacl'); naclKeypairFromSecret(...) // => { secretKey: [...], publicKey: [...] } */ -module.exports = function naclKeypairFromSecret (secret: Uint8Array): KeypairType { +export default function naclKeypairFromSecret (secret: Uint8Array): KeypairType { return nacl.sign.keyPair.fromSecretKey(secret); -}; +} diff --git a/packages/util-crypto/src/nacl/keypair/fromSecret.spec.js b/packages/util-crypto/src/nacl/keypair/fromSecret.spec.js index c6a539ca5..a5c7b8e47 100644 --- a/packages/util-crypto/src/nacl/keypair/fromSecret.spec.js +++ b/packages/util-crypto/src/nacl/keypair/fromSecret.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { naclKeypairFromSecret } = require('../index'); +import { naclKeypairFromSecret } from '../index'; describe('naclKeypairFromSecret', () => { const secretKey = new Uint8Array([ diff --git a/packages/util-crypto/src/nacl/keypair/fromSeed.js b/packages/util-crypto/src/nacl/keypair/fromSeed.js index d8f67e179..92c61ef6c 100644 --- a/packages/util-crypto/src/nacl/keypair/fromSeed.js +++ b/packages/util-crypto/src/nacl/keypair/fromSeed.js @@ -5,7 +5,7 @@ import type { KeypairType } from '../../types'; -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name naclKeypairFromSeed @@ -18,6 +18,6 @@ const nacl = require('tweetnacl'); naclKeypairFromSeed(...) // => { secretKey: [...], publicKey: [...] } */ -module.exports = function naclKeypairFromSeed (seed: Uint8Array): KeypairType { +export default function naclKeypairFromSeed (seed: Uint8Array): KeypairType { return nacl.sign.keyPair.fromSeed(seed); -}; +} diff --git a/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js b/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js index 87b8826d8..1e9fceec4 100644 --- a/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js +++ b/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import u8aFromString from '@polkadot/util/u8a/fromString'; -const { naclKeypairFromSeed } = require('../index'); +import { naclKeypairFromSeed } from '../index'; describe('naclKeypairFromSeed', () => { // NOTE: Aligned with Rust test, b"12345678901234567890123456789012" diff --git a/packages/util-crypto/src/nacl/keypair/fromString.js b/packages/util-crypto/src/nacl/keypair/fromString.js index 150ee5529..7927eca57 100644 --- a/packages/util-crypto/src/nacl/keypair/fromString.js +++ b/packages/util-crypto/src/nacl/keypair/fromString.js @@ -5,10 +5,10 @@ import type { KeypairType } from '../../types'; -const u8aFromUtf8 = require('@polkadot/util/u8a/fromUtf8'); +import u8aFromUtf8 from '@polkadot/util/u8a/fromUtf8'; -const blake2AsU8a = require('../../blake2/asU8a'); -const fromSeed = require('./fromSeed'); +import blake2AsU8a from '../../blake2/asU8a'; +import fromSeed from './fromSeed'; /** @name naclKeypairFromString @@ -21,11 +21,11 @@ const fromSeed = require('./fromSeed'); naclKeypairFromString('test') // => { secretKey: [...], publicKey: [...] } */ -module.exports = function naclKeypairFromString (value: string): KeypairType { +export default function naclKeypairFromString (value: string): KeypairType { return fromSeed( blake2AsU8a( u8aFromUtf8(value), 256 ) ); -}; +} diff --git a/packages/util-crypto/src/nacl/keypair/fromString.spec.js b/packages/util-crypto/src/nacl/keypair/fromString.spec.js index d8fcb7a13..5c8a030d2 100644 --- a/packages/util-crypto/src/nacl/keypair/fromString.spec.js +++ b/packages/util-crypto/src/nacl/keypair/fromString.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { naclKeypairFromString } = require('../index'); +import { naclKeypairFromString } from '../index'; describe('naclKeypairFromSeed', () => { it('generates a valid publicKey/secretKey pair', () => { diff --git a/packages/util-crypto/src/nacl/sign.js b/packages/util-crypto/src/nacl/sign.js index 2b35f9afe..d80b9b8fd 100644 --- a/packages/util-crypto/src/nacl/sign.js +++ b/packages/util-crypto/src/nacl/sign.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name naclSign @@ -16,6 +16,6 @@ const nacl = require('tweetnacl'); naclSign([...], [...]) // => [...] */ -module.exports = function naclSign (message: Uint8Array, secretKey: Uint8Array): Uint8Array { +export default function naclSign (message: Uint8Array, secretKey: Uint8Array): Uint8Array { return nacl.sign.detached(message, secretKey); -}; +} diff --git a/packages/util-crypto/src/nacl/sign.spec.js b/packages/util-crypto/src/nacl/sign.spec.js index 4738a935c..64e922d2a 100644 --- a/packages/util-crypto/src/nacl/sign.spec.js +++ b/packages/util-crypto/src/nacl/sign.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import u8aFromString from '@polkadot/util/u8a/fromString'; -const { naclSign, naclKeypairFromSeed } = require('./index'); +import { naclSign, naclKeypairFromSeed } from './index'; describe('naclSign', () => { let secretKey; diff --git a/packages/util-crypto/src/nacl/verify.js b/packages/util-crypto/src/nacl/verify.js index aa2bb979c..4ef985a71 100644 --- a/packages/util-crypto/src/nacl/verify.js +++ b/packages/util-crypto/src/nacl/verify.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name naclSign @@ -16,6 +16,6 @@ const nacl = require('tweetnacl'); naclVerify([...], [...], [...]) // => true/false */ -module.exports = function naclVerify (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean { +export default function naclVerify (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean { return nacl.sign.detached.verify(message, signature, publicKey); -}; +} diff --git a/packages/util-crypto/src/nacl/verify.spec.js b/packages/util-crypto/src/nacl/verify.spec.js index 1abfb617c..6362fbcda 100644 --- a/packages/util-crypto/src/nacl/verify.spec.js +++ b/packages/util-crypto/src/nacl/verify.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import u8aFromString from '@polkadot/util/u8a/fromString'; -const { naclVerify, naclKeypairFromSeed } = require('./index'); +import { naclVerify, naclKeypairFromSeed } from './index'; describe('naclSign', () => { let publicKey; diff --git a/packages/util-crypto/src/random/asHex.js b/packages/util-crypto/src/random/asHex.js index 437e3591d..d6a667c92 100644 --- a/packages/util-crypto/src/random/asHex.js +++ b/packages/util-crypto/src/random/asHex.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aToHex = require('@polkadot/util/u8a/toHex'); +import u8aToHex from '@polkadot/util/u8a/toHex'; -const randomAsU8a = require('./asU8a'); +import randomAsU8a from './asU8a'; /** @name randomAsHex @@ -18,8 +18,8 @@ const randomAsU8a = require('./asU8a'); randomAsHex() // => 0x... */ -module.exports = function randomAsHex (length?: number = 32): string { +export default function randomAsHex (length?: number = 32): string { return u8aToHex( randomAsU8a(length) ); -}; +} diff --git a/packages/util-crypto/src/random/asHex.spec.js b/packages/util-crypto/src/random/asHex.spec.js index 976aeeeb5..306110869 100644 --- a/packages/util-crypto/src/random/asHex.spec.js +++ b/packages/util-crypto/src/random/asHex.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isHex = require('@polkadot/util/is/hex'); +import isHex from '@polkadot/util/is/hex'; -const { randomAsHex } = require('./index'); +import { randomAsHex } from './index'; describe('randomAsBuffer', () => { it('generated results does not match', () => { diff --git a/packages/util-crypto/src/random/asNumber.js b/packages/util-crypto/src/random/asNumber.js index 5ac12f576..ebaff60a4 100644 --- a/packages/util-crypto/src/random/asNumber.js +++ b/packages/util-crypto/src/random/asNumber.js @@ -3,10 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexToBn = require('@polkadot/util/hex/toBn'); -const BN = require('bn.js'); +import BN from 'bn.js'; +import hexToBn from '@polkadot/util/hex/toBn'; -const randomAsHex = require('./asHex'); +import randomAsHex from './asHex'; const BN_53 = new BN(0b11111111111111111111111111111111111111111111111111111); @@ -21,8 +21,8 @@ const BN_53 = new BN(0b11111111111111111111111111111111111111111111111111111); randomAsNumber() // => */ -module.exports = function randomAsNumber (): number { +export default function randomAsNumber (): number { return hexToBn( randomAsHex(8) ).and(BN_53).toNumber(); -}; +} diff --git a/packages/util-crypto/src/random/asNumber.spec.js b/packages/util-crypto/src/random/asNumber.spec.js index e7a677307..d7a7a9c2e 100644 --- a/packages/util-crypto/src/random/asNumber.spec.js +++ b/packages/util-crypto/src/random/asNumber.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { randomAsNumber } = require('./index'); +import { randomAsNumber } from './index'; describe('randomAsNumber', () => { it('generates subsequent non-matching numbers', () => { diff --git a/packages/util-crypto/src/random/asU8a.js b/packages/util-crypto/src/random/asU8a.js index cf7106641..c93325681 100644 --- a/packages/util-crypto/src/random/asU8a.js +++ b/packages/util-crypto/src/random/asU8a.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name randomAsU8a @@ -16,6 +16,6 @@ const nacl = require('tweetnacl'); randomAsU8s() // => Uint8Array([...]) */ -module.exports = function randomAsU8a (length?: number = 32): Uint8Array { +export default function randomAsU8a (length?: number = 32): Uint8Array { return nacl.randomBytes(length); -}; +} diff --git a/packages/util-crypto/src/random/asU8a.spec.js b/packages/util-crypto/src/random/asU8a.spec.js index b2ee07bf5..88d9ffb62 100644 --- a/packages/util-crypto/src/random/asU8a.spec.js +++ b/packages/util-crypto/src/random/asU8a.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isU8a = require('@polkadot/util/is/u8a'); +import isU8a from '@polkadot/util/is/u8a'; -const { randomAsU8a } = require('./index'); +import { randomAsU8a } from './index'; describe('randomAsU8a', () => { it('generates a Uint8Array', () => { diff --git a/packages/util-crypto/src/random/index.js b/packages/util-crypto/src/random/index.js index 493431fb2..1e91c4e5a 100644 --- a/packages/util-crypto/src/random/index.js +++ b/packages/util-crypto/src/random/index.js @@ -3,14 +3,14 @@ // of the ISC license. See the LICENSE file for details. // @flow -const randomAsHex = require('./asHex'); -const randomAsNumber = require('./asNumber'); -const randomAsU8a = require('./asU8a'); +import randomAsHex from './asHex'; +import randomAsNumber from './asNumber'; +import randomAsU8a from './asU8a'; /** @summary Returns a sequence of secure random bytes in a variety of formats */ -module.exports = { +export { randomAsHex, randomAsNumber, randomAsU8a diff --git a/packages/util-crypto/src/sha512/asU8a.js b/packages/util-crypto/src/sha512/asU8a.js index a16d9d551..c13d04692 100644 --- a/packages/util-crypto/src/sha512/asU8a.js +++ b/packages/util-crypto/src/sha512/asU8a.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const nacl = require('tweetnacl'); +import nacl from 'tweetnacl'; /** @name sha512AsU8a @@ -16,6 +16,6 @@ const nacl = require('tweetnacl'); sha512AsU8a(Uint8Array.from([...])) // => Uint8Array([...]) */ -module.exports = function sha512AsU8a (data: Uint8Array): Uint8Array { +export default function sha512AsU8a (data: Uint8Array): Uint8Array { return nacl.hash(data); -}; +} diff --git a/packages/util-crypto/src/sha512/asU8a.spec.js b/packages/util-crypto/src/sha512/asU8a.spec.js index f9d4a8878..6bf9b5ba1 100644 --- a/packages/util-crypto/src/sha512/asU8a.spec.js +++ b/packages/util-crypto/src/sha512/asU8a.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { sha512AsU8a } = require('./index'); +import { sha512AsU8a } from './index'; describe('sha512AsU8a', () => { it('creates a sha-512 hash', () => { diff --git a/packages/util-crypto/src/sha512/index.js b/packages/util-crypto/src/sha512/index.js index 3318c9037..b3ac7917f 100644 --- a/packages/util-crypto/src/sha512/index.js +++ b/packages/util-crypto/src/sha512/index.js @@ -3,11 +3,11 @@ // of the ISC license. See the LICENSE file for details. // @flow -const sha512AsU8a = require('./asU8a'); +import sha512AsU8a from './asU8a'; /** @summary Implements Sha-512 hashing functions for a variety of input and outputs */ -module.exports = { +export { sha512AsU8a }; diff --git a/packages/util-crypto/src/xxhash/asHex.js b/packages/util-crypto/src/xxhash/asHex.js index e42e4dc24..0b0bb99e1 100644 --- a/packages/util-crypto/src/xxhash/asHex.js +++ b/packages/util-crypto/src/xxhash/asHex.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aToHex = require('@polkadot/util/u8a/toHex'); +import u8aToHex from '@polkadot/util/u8a/toHex'; -const xxhashAsU8a = require('./asU8a'); +import xxhashAsU8a from './asU8a'; /** @name xxhashAsHex @@ -18,8 +18,8 @@ const xxhashAsU8a = require('./asU8a'); xxhashAsHex('abc') // => 0x44bc2cf5ad770999 */ -module.exports = function xxhashAsHex (data: Buffer | Uint8Array | string, bitLength: number = 64): string { +export default function xxhashAsHex (data: Buffer | Uint8Array | string, bitLength: number = 64): string { return u8aToHex( xxhashAsU8a(data, bitLength) ); -}; +} diff --git a/packages/util-crypto/src/xxhash/asHex.spec.js b/packages/util-crypto/src/xxhash/asHex.spec.js index 42fc19cfc..f7661294b 100644 --- a/packages/util-crypto/src/xxhash/asHex.spec.js +++ b/packages/util-crypto/src/xxhash/asHex.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { xxhashAsHex } = require('./index'); +import { xxhashAsHex } from './index'; describe('xxhashAsHex', () => { it('returns a 64-bit value by default', () => { diff --git a/packages/util-crypto/src/xxhash/asU8a.js b/packages/util-crypto/src/xxhash/asU8a.js index ebddad78c..9b5e14434 100644 --- a/packages/util-crypto/src/xxhash/asU8a.js +++ b/packages/util-crypto/src/xxhash/asU8a.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const xxhash64AsBn = require('./xxhash64/asBn'); +import xxhash64AsBn from './xxhash64/asBn'; /** @name xxhashAsU8a @@ -16,7 +16,7 @@ const xxhash64AsBn = require('./xxhash64/asBn'); xxhashAsU8a('abc') // => 0x44bc2cf5ad770999 */ -module.exports = function xxhashAsU8a (data: Buffer | Uint8Array | string, bitLength: number = 64): Uint8Array { +export default function xxhashAsU8a (data: Buffer | Uint8Array | string, bitLength: number = 64): Uint8Array { const byteLength = Math.ceil(bitLength / 8); const iterations = Math.ceil(bitLength / 64); const u8a = new Uint8Array(byteLength); @@ -26,4 +26,4 @@ module.exports = function xxhashAsU8a (data: Buffer | Uint8Array | string, bitLe } return u8a; -}; +} diff --git a/packages/util-crypto/src/xxhash/asU8a.spec.js b/packages/util-crypto/src/xxhash/asU8a.spec.js index 2850bd609..4a7e1d6ec 100644 --- a/packages/util-crypto/src/xxhash/asU8a.spec.js +++ b/packages/util-crypto/src/xxhash/asU8a.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import hexToU8a from '@polkadot/util/hex/toU8a'; -const { xxhashAsU8a } = require('./index'); +import { xxhashAsU8a } from './index'; describe('xxhashAsU8a', () => { it('returns a 64-bit value by default', () => { diff --git a/packages/util-crypto/src/xxhash/index.js b/packages/util-crypto/src/xxhash/index.js index ecf64ea01..7216701d1 100644 --- a/packages/util-crypto/src/xxhash/index.js +++ b/packages/util-crypto/src/xxhash/index.js @@ -3,13 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -const xxhashAsHex = require('./asHex'); -const xxhashAsU8a = require('./asU8a'); +import xxhashAsHex from './asHex'; +import xxhashAsU8a from './asU8a'; /** @summary Create xxhash64 values with specified bitlengths */ -module.exports = { +export { xxhashAsHex, xxhashAsU8a }; diff --git a/packages/util-crypto/src/xxhash/xxhash64/asBn.js b/packages/util-crypto/src/xxhash/xxhash64/asBn.js index 91f65df33..78e87bbe7 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asBn.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asBn.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const BN = require('bn.js'); +import BN from 'bn.js'; -const xxhash64AsRaw = require('./asRaw'); +import xxhash64AsRaw from './asRaw'; /** @name xxhash64AsBn @@ -18,9 +18,9 @@ const xxhash64AsRaw = require('./asRaw'); xxhash64AsBn('abcd', 0xabcd)) // => new BN(0xe29f70f8b8c96df7) */ -module.exports = function xxhash64AsBn (data: Buffer | Uint8Array | string, seed: number): BN { +export default function xxhash64AsBn (data: Buffer | Uint8Array | string, seed: number): BN { return new BN( xxhash64AsRaw(data, seed), 16 ); -}; +} diff --git a/packages/util-crypto/src/xxhash/xxhash64/asBn.spec.js b/packages/util-crypto/src/xxhash/xxhash64/asBn.spec.js index 92154cc50..82b6bb407 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asBn.spec.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asBn.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { xxhash64AsBn } = require('./index'); +import { xxhash64AsBn } from './index'; describe('xxhash64AsBn', () => { it('creates the correct BN output', () => { diff --git a/packages/util-crypto/src/xxhash/xxhash64/asHex.js b/packages/util-crypto/src/xxhash/xxhash64/asHex.js index 7e5785bd1..c4ab3377f 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asHex.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asHex.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexAddPrefix = require('@polkadot/util/hex/addPrefix'); +import hexAddPrefix from '@polkadot/util/hex/addPrefix'; -const xxhash64AsRaw = require('./asRaw'); +import xxhash64AsRaw from './asRaw'; /** @name xxhash64AsHex @@ -18,8 +18,8 @@ const xxhash64AsRaw = require('./asRaw'); xxhash64AsHex('abcd', 0xabcd)) // => 0xe29f70f8b8c96df7 */ -module.exports = function xxhash64AsHex (data: Buffer | Uint8Array | string, seed: number): string { +export default function xxhash64AsHex (data: Buffer | Uint8Array | string, seed: number): string { return hexAddPrefix( xxhash64AsRaw(data, seed) ); -}; +} diff --git a/packages/util-crypto/src/xxhash/xxhash64/asHex.spec.js b/packages/util-crypto/src/xxhash/xxhash64/asHex.spec.js index 48c1f81b3..203189a32 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asHex.spec.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asHex.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { xxhash64AsHex } = require('./index'); +import { xxhash64AsHex } from './index'; describe('xxhash64AsHex', () => { it('creates the correct hex output', () => { diff --git a/packages/util-crypto/src/xxhash/xxhash64/asRaw.js b/packages/util-crypto/src/xxhash/xxhash64/asRaw.js index 3570faf43..0ebcea008 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asRaw.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asRaw.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const xxhash64AsValue = require('./asValue'); +import xxhash64AsValue from './asValue'; /** @name xxhash64AsRaw @@ -16,6 +16,6 @@ const xxhash64AsValue = require('./asValue'); xxhash64AsRaw('abcd', 0xabcd)) // => e29f70f8b8c96df7 */ -module.exports = function xxhash64AsRaw (data: Buffer | Uint8Array | string, seed: number): string { +export default function xxhash64AsRaw (data: Buffer | Uint8Array | string, seed: number): string { return xxhash64AsValue(data, seed).toString(16); -}; +} diff --git a/packages/util-crypto/src/xxhash/xxhash64/asRaw.spec.js b/packages/util-crypto/src/xxhash/xxhash64/asRaw.spec.js index 41982a820..382b3a52b 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asRaw.spec.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asRaw.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { xxhash64AsRaw } = require('./index'); +import { xxhash64AsRaw } from './index'; describe('xxhash64AsRaw', () => { it('creates the correct non-prefixed hex output', () => { diff --git a/packages/util-crypto/src/xxhash/xxhash64/asValue.js b/packages/util-crypto/src/xxhash/xxhash64/asValue.js index 815a7daa3..7d74637b8 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asValue.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asValue.js @@ -5,21 +5,20 @@ import type { xxhashjs$Result } from 'xxhashjs'; -const isBuffer = require('@polkadot/util/is/buffer'); -const isString = require('@polkadot/util/is/string'); -const u8aToBuffer = require('@polkadot/util/u8a/toBuffer'); -const xxhashjs = require('xxhashjs'); +import isBuffer from '@polkadot/util/is/buffer'; +import isString from '@polkadot/util/is/string'; +import u8aToBuffer from '@polkadot/util/u8a/toBuffer'; +import xxhashjs from 'xxhashjs'; -module.exports = function xxhash64AsValue (data: Buffer | Uint8Array | string, seed: number): xxhashjs$Result { +export default function xxhash64AsValue (data: Buffer | Uint8Array | string, seed: number): xxhashjs$Result { if (isBuffer(data) || isString(data)) { // $FlowFixMe we have determined the type return xxhashjs.h64(data, seed); } return xxhashjs.h64( - // NOTE: We can't use data.buffer, it has the incorrect length // $FlowFixMe we have determined the type u8aToBuffer(data), seed ); -}; +} diff --git a/packages/util-crypto/src/xxhash/xxhash64/asValue.spec.js b/packages/util-crypto/src/xxhash/xxhash64/asValue.spec.js index 6beba115b..84c82f44d 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/asValue.spec.js +++ b/packages/util-crypto/src/xxhash/xxhash64/asValue.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const xxhash64AsValue = require('./asValue'); +import xxhash64AsValue from './asValue'; describe('xxhash64AsValue', () => { it('creates the correct output (string input)', () => { diff --git a/packages/util-crypto/src/xxhash/xxhash64/index.js b/packages/util-crypto/src/xxhash/xxhash64/index.js index 425bda44e..41b8c3eec 100644 --- a/packages/util-crypto/src/xxhash/xxhash64/index.js +++ b/packages/util-crypto/src/xxhash/xxhash64/index.js @@ -3,14 +3,14 @@ // of the ISC license. See the LICENSE file for details. // @flow -const xxhash64AsBn = require('./asBn'); -const xxhash64AsHex = require('./asHex'); -const xxhash64AsRaw = require('./asRaw'); +import xxhash64AsBn from './asBn'; +import xxhash64AsHex from './asHex'; +import xxhash64AsRaw from './asRaw'; /** @summary Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output */ -module.exports = { +export { xxhash64AsBn, xxhash64AsHex, xxhash64AsRaw diff --git a/packages/util-keyring/package.json b/packages/util-keyring/package.json index 16020561b..933fe4616 100644 --- a/packages/util-keyring/package.json +++ b/packages/util-keyring/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-keyring", - "version": "0.22.11", + "version": "0.23.0", "description": "Keyring management", "main": "index.js", "engines": { @@ -36,8 +36,8 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.22.11", - "@polkadot/util-crypto": "^0.22.11", + "@polkadot/util": "^0.23.0", + "@polkadot/util-crypto": "^0.23.0", "bs58": "^4.0.1" } } diff --git a/packages/util-keyring/src/address/decode.js b/packages/util-keyring/src/address/decode.js index 4e78f528d..c9e0ec7f0 100644 --- a/packages/util-keyring/src/address/decode.js +++ b/packages/util-keyring/src/address/decode.js @@ -5,15 +5,16 @@ // Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6 -const bs58 = require('bs58'); -const assert = require('@polkadot/util/assert'); -const bufferToU8a = require('@polkadot/util/buffer/toU8a'); -const isHex = require('@polkadot/util/is/hex'); -const isU8a = require('@polkadot/util/is/u8a'); -const u8aToU8a = require('@polkadot/util/u8a/toU8a'); -const blake2b = require('@polkadot/util-crypto/blake2/asU8a'); +import bs58 from 'bs58'; -module.exports = function decode (encoded: string | Uint8Array): Uint8Array { +import assert from '@polkadot/util/assert'; +import bufferToU8a from '@polkadot/util/buffer/toU8a'; +import isHex from '@polkadot/util/is/hex'; +import isU8a from '@polkadot/util/is/u8a'; +import u8aToU8a from '@polkadot/util/u8a/toU8a'; +import blake2b from '@polkadot/util-crypto/blake2/asU8a'; + +export default function decode (encoded: string | Uint8Array): Uint8Array { if (isU8a(encoded) || isHex(encoded)) { return u8aToU8a(encoded); } @@ -32,4 +33,4 @@ module.exports = function decode (encoded: string | Uint8Array): Uint8Array { assert(decoded[33] === hash[0] && decoded[34] === hash[1], error(' Invalid decoded address checksum')); return decoded.slice(1, 33); -}; +} diff --git a/packages/util-keyring/src/address/decode.spec.js b/packages/util-keyring/src/address/decode.spec.js index d36129be8..3875810e2 100644 --- a/packages/util-keyring/src/address/decode.spec.js +++ b/packages/util-keyring/src/address/decode.spec.js @@ -3,8 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const keyring = require('../testingPairs')(); -const decode = require('./decode'); +import testingPairs from '../testingPairs'; +import decode from './decode'; + +const keyring = testingPairs(); describe('decode', () => { it('decodes an address', () => { diff --git a/packages/util-keyring/src/address/encode.js b/packages/util-keyring/src/address/encode.js index f0467fb68..06cd68b14 100644 --- a/packages/util-keyring/src/address/encode.js +++ b/packages/util-keyring/src/address/encode.js @@ -5,16 +5,17 @@ // Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34 -const bs58 = require('bs58'); -const assert = require('@polkadot/util/assert'); -const u8aConcat = require('@polkadot/util/u8a/concat'); -const u8aToBuffer = require('@polkadot/util/u8a/toBuffer'); -const u8aToU8a = require('@polkadot/util/u8a/toU8a'); -const blake2b = require('@polkadot/util-crypto/blake2/asU8a'); +import bs58 from 'bs58'; + +import assert from '@polkadot/util/assert'; +import u8aConcat from '@polkadot/util/u8a/concat'; +import u8aToBuffer from '@polkadot/util/u8a/toBuffer'; +import u8aToU8a from '@polkadot/util/u8a/toU8a'; +import blake2b from '@polkadot/util-crypto/blake2/asU8a'; const PREFIX = new Uint8Array([42]); -module.exports = function encode (_publicKey: Uint8Array | string): string { +export default function encode (_publicKey: Uint8Array | string): string { const publicKey = u8aToU8a(_publicKey); assert(publicKey.length === 32, `Expected a valid publicKey to convert`); @@ -27,4 +28,4 @@ module.exports = function encode (_publicKey: Uint8Array | string): string { u8aConcat(input, hash.subarray(0, 2)) ) ); -}; +} diff --git a/packages/util-keyring/src/address/encode.spec.js b/packages/util-keyring/src/address/encode.spec.js index 764a8ffd0..0a98aade6 100644 --- a/packages/util-keyring/src/address/encode.spec.js +++ b/packages/util-keyring/src/address/encode.spec.js @@ -3,8 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const keyring = require('../testingPairs')(); -const encode = require('./encode'); +import testingPairs from '../testingPairs'; +import encode from './encode'; + +const keyring = testingPairs(); describe('encode', () => { it('encodes an address to a valid value', () => { diff --git a/packages/util-keyring/src/address/index.js b/packages/util-keyring/src/address/index.js index 5659f3f28..5b18118fc 100644 --- a/packages/util-keyring/src/address/index.js +++ b/packages/util-keyring/src/address/index.js @@ -3,10 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const decodeAddress = require('./decode'); -const encodeAddress = require('./encode'); +import decodeAddress from './decode'; +import encodeAddress from './encode'; -module.exports = { +export { decodeAddress, encodeAddress }; diff --git a/packages/util-keyring/src/index.js b/packages/util-keyring/src/index.js index a57715463..efdd1a2e4 100644 --- a/packages/util-keyring/src/index.js +++ b/packages/util-keyring/src/index.js @@ -5,14 +5,14 @@ import type { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta } from './types'; -const naclKeypairFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed'); -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import naclKeypairFromSeed from '@polkadot/util-crypto/nacl/keypair/fromSeed'; +import hexToU8a from '@polkadot/util/hex/toU8a'; -const addressDecode = require('./address/decode'); -const createPair = require('./pair'); -const createPairs = require('./pairs'); +import addressDecode from './address/decode'; +import createPair from './pair'; +import createPairs from './pairs'; -module.exports = function keyring (): KeyringInstance { +export default function keyring (): KeyringInstance { const pairs = createPairs(); const addFromAddress = (address: string | Uint8Array, meta?: KeyringPair$Meta, defaultEncoded?: Uint8Array) => pairs.add(createPair({ publicKey: addressDecode(address) }, meta, defaultEncoded)); @@ -33,4 +33,4 @@ module.exports = function keyring (): KeyringInstance { toJson: (address: string | Uint8Array, passphrase?: string): KeyringPair$Json => pairs.get(address).toJson(passphrase) }; -}; +} diff --git a/packages/util-keyring/src/index.spec.js b/packages/util-keyring/src/index.spec.js index 2d4ad37c9..90a984ce2 100644 --- a/packages/util-keyring/src/index.spec.js +++ b/packages/util-keyring/src/index.spec.js @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import hexToU8a from '@polkadot/util/hex/toU8a'; +import u8aFromString from '@polkadot/util/u8a/fromString'; -const index = require('./index'); +import index from './index'; describe('keypair', () => { const publicKeyOne = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]); diff --git a/packages/util-keyring/src/pair/decode.js b/packages/util-keyring/src/pair/decode.js index e6c4c12b2..19b51cad0 100644 --- a/packages/util-keyring/src/pair/decode.js +++ b/packages/util-keyring/src/pair/decode.js @@ -3,21 +3,21 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aConcat = require('@polkadot/util/u8a/concat'); -const u8aFixLength = require('@polkadot/util/u8a/fixLength'); -const u8aFromString = require('@polkadot/util/u8a/fromString'); -const assert = require('@polkadot/util/assert'); -const naclDecrypt = require('@polkadot/util-crypto/nacl/decrypt'); -const naclFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed'); +import u8aConcat from '@polkadot/util/u8a/concat'; +import u8aFixLength from '@polkadot/util/u8a/fixLength'; +import u8aFromString from '@polkadot/util/u8a/fromString'; +import assert from '@polkadot/util/assert'; +import naclDecrypt from '@polkadot/util-crypto/nacl/decrypt'; +import naclFromSeed from '@polkadot/util-crypto/nacl/keypair/fromSeed'; -const { PKCS8_DIVIDER, PKCS8_HEADER } = require('./defaults'); +import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults'; const KEY_LENGTH = 32; const SEED_OFFSET = PKCS8_HEADER.length; const DIV_OFFSET = SEED_OFFSET + KEY_LENGTH; const PUBLIC_OFFSET = SEED_OFFSET + KEY_LENGTH + PKCS8_DIVIDER.length; -module.exports = function decode (passphrase?: string, _encrypted?: Uint8Array) { +export default function decode (passphrase?: string, _encrypted?: Uint8Array) { assert(_encrypted, `No encrypted data available to decode`); // flowlint-next-line unclear-type:off @@ -47,4 +47,4 @@ module.exports = function decode (passphrase?: string, _encrypted?: Uint8Array) publicKey, secretKey }; -}; +} diff --git a/packages/util-keyring/src/pair/decode.spec.js b/packages/util-keyring/src/pair/decode.spec.js index 7c6184823..4ea5a1194 100644 --- a/packages/util-keyring/src/pair/decode.spec.js +++ b/packages/util-keyring/src/pair/decode.spec.js @@ -2,7 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const keyring = require('../testingPairs')(); +import testingPairs from '../testingPairs'; + +const keyring = testingPairs(); describe('decode', () => { it('fails when no data provided', () => { diff --git a/packages/util-keyring/src/pair/defaults.js b/packages/util-keyring/src/pair/defaults.js index 09436ee7d..71d9a8030 100644 --- a/packages/util-keyring/src/pair/defaults.js +++ b/packages/util-keyring/src/pair/defaults.js @@ -6,7 +6,7 @@ const PKCS8_DIVIDER = new Uint8Array([161, 35, 3, 33, 0]); const PKCS8_HEADER = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]); -module.exports = { +export { PKCS8_DIVIDER, PKCS8_HEADER }; diff --git a/packages/util-keyring/src/pair/encode.js b/packages/util-keyring/src/pair/encode.js index b85e6fdb3..c9c95336a 100644 --- a/packages/util-keyring/src/pair/encode.js +++ b/packages/util-keyring/src/pair/encode.js @@ -3,14 +3,14 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aConcat = require('@polkadot/util/u8a/concat'); -const u8aFixLength = require('@polkadot/util/u8a/fixLength'); -const u8aFromString = require('@polkadot/util/u8a/fromString'); -const naclEncrypt = require('@polkadot/util-crypto/nacl/encrypt'); +import u8aConcat from '@polkadot/util/u8a/concat'; +import u8aFixLength from '@polkadot/util/u8a/fixLength'; +import u8aFromString from '@polkadot/util/u8a/fromString'; +import naclEncrypt from '@polkadot/util-crypto/nacl/encrypt'; -const { PKCS8_DIVIDER, PKCS8_HEADER } = require('./defaults'); +import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults'; -module.exports = function encode (secretKey: Uint8Array, passphrase?: string): Uint8Array { +export default function encode (secretKey: Uint8Array, passphrase?: string): Uint8Array { const encoded = u8aConcat( PKCS8_HEADER, secretKey.subarray(0, 32), @@ -26,4 +26,4 @@ module.exports = function encode (secretKey: Uint8Array, passphrase?: string): U const { encrypted, nonce } = naclEncrypt(encoded, u8aFixLength(u8aFromString(passphrase), 256, true)); return u8aConcat(nonce, encrypted); -}; +} diff --git a/packages/util-keyring/src/pair/encode.spec.js b/packages/util-keyring/src/pair/encode.spec.js index 53488cf8a..fee935379 100644 --- a/packages/util-keyring/src/pair/encode.spec.js +++ b/packages/util-keyring/src/pair/encode.spec.js @@ -2,12 +2,14 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const keyring = require('../testingPairs')(); -const { PKCS8_DIVIDER, PKCS8_HEADER } = require('./defaults'); +import testingPairs from '../testingPairs'; +import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults'; const PKCS8_LENGTH = PKCS8_DIVIDER.length + PKCS8_HEADER.length + 64; const ENCODED_LENGTH = 125; +const keyring = testingPairs(); + describe('encode', () => { it('returns PKCS8 when no passphrase supplied', () => { expect( diff --git a/packages/util-keyring/src/pair/getMeta.js b/packages/util-keyring/src/pair/getMeta.js index d3ce6787c..e51e04c62 100644 --- a/packages/util-keyring/src/pair/getMeta.js +++ b/packages/util-keyring/src/pair/getMeta.js @@ -6,6 +6,6 @@ import type { KeyringPair$Meta } from '../types'; import type { PairState } from './types'; -module.exports = function getMeta (state: PairState): KeyringPair$Meta { +export default function getMeta (state: PairState): KeyringPair$Meta { return state.meta; -}; +} diff --git a/packages/util-keyring/src/pair/index.js b/packages/util-keyring/src/pair/index.js index 5af898834..6235cad5e 100644 --- a/packages/util-keyring/src/pair/index.js +++ b/packages/util-keyring/src/pair/index.js @@ -7,17 +7,17 @@ import type { KeypairType } from '@polkadot/util-crypto/types'; import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '../types'; import type { PairState } from './types'; -const naclSign = require('@polkadot/util-crypto/nacl/sign'); -const naclVerify = require('@polkadot/util-crypto/nacl/verify'); +import naclSign from '@polkadot/util-crypto/nacl/sign'; +import naclVerify from '@polkadot/util-crypto/nacl/verify'; -const encodeAddress = require('../address/encode'); -const decode = require('./decode'); -const encode = require('./encode'); -const getMeta = require('./getMeta'); -const setMeta = require('./setMeta'); -const toJson = require('./toJson'); +import encodeAddress from '../address/encode'; +import decode from './decode'; +import encode from './encode'; +import getMeta from './getMeta'; +import setMeta from './setMeta'; +import toJson from './toJson'; -module.exports = function pair ({ publicKey, secretKey }: $Shape, meta?: KeyringPair$Meta = {}, defaultEncoded?: Uint8Array): KeyringPair { +export default function pair ({ publicKey, secretKey }: $Shape, meta?: KeyringPair$Meta = {}, defaultEncoded?: Uint8Array): KeyringPair { const state: PairState = { address: encodeAddress(publicKey), meta: { ...meta } @@ -50,4 +50,4 @@ module.exports = function pair ({ publicKey, secretKey }: $Shape, m verify: (message: Uint8Array, signature: Uint8Array): boolean => naclVerify(message, signature, publicKey) }; -}; +} diff --git a/packages/util-keyring/src/pair/index.spec.js b/packages/util-keyring/src/pair/index.spec.js index 3363ced11..997f585b9 100644 --- a/packages/util-keyring/src/pair/index.spec.js +++ b/packages/util-keyring/src/pair/index.spec.js @@ -2,7 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const keyring = require('../testingPairs')(); +import testingPairs from '../testingPairs'; + +const keyring = testingPairs(); // aligned with @polkadot/util-crypto tests describe('pair', () => { diff --git a/packages/util-keyring/src/pair/nobody.js b/packages/util-keyring/src/pair/nobody.js index 498acb9bb..6c03e0f8e 100644 --- a/packages/util-keyring/src/pair/nobody.js +++ b/packages/util-keyring/src/pair/nobody.js @@ -5,7 +5,7 @@ import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '../types'; -const addressEncode = require('../address/encode'); +import addressEncode from '../address/encode'; const publicKey = new Uint8Array(32); const address = addressEncode(publicKey); @@ -23,7 +23,7 @@ const json = { meta }; -module.exports = function everybody (): KeyringPair { +export default function everybody (): KeyringPair { return { address: (): string => address, @@ -46,4 +46,4 @@ module.exports = function everybody (): KeyringPair { verify: (message: Uint8Array, signature: Uint8Array): boolean => false }; -}; +} diff --git a/packages/util-keyring/src/pair/setMeta.js b/packages/util-keyring/src/pair/setMeta.js index 15f47c4ba..ca6185f2f 100644 --- a/packages/util-keyring/src/pair/setMeta.js +++ b/packages/util-keyring/src/pair/setMeta.js @@ -6,9 +6,9 @@ import type { KeyringPair$Meta } from '../types'; import type { PairState } from './types'; -module.exports = function setMeta (state: PairState, meta: KeyringPair$Meta): void { +export default function setMeta (state: PairState, meta: KeyringPair$Meta): void { state.meta = { ...state.meta, ...meta }; -}; +} diff --git a/packages/util-keyring/src/pair/toJson.js b/packages/util-keyring/src/pair/toJson.js index 8b0c42ecc..d80e4ac7d 100644 --- a/packages/util-keyring/src/pair/toJson.js +++ b/packages/util-keyring/src/pair/toJson.js @@ -6,9 +6,9 @@ import type { KeyringPair$Json } from '../types'; import type { PairState } from './types'; -const u8aToHex = require('@polkadot/util/u8a/toHex'); +import u8aToHex from '@polkadot/util/u8a/toHex'; -module.exports = function toJson ({ address, meta }: PairState, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json { +export default function toJson ({ address, meta }: PairState, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json { return { address, encoded: u8aToHex(encoded), @@ -23,4 +23,4 @@ module.exports = function toJson ({ address, meta }: PairState, encoded: Uint8Ar ...meta } }; -}; +} diff --git a/packages/util-keyring/src/pair/toJson.spec.js b/packages/util-keyring/src/pair/toJson.spec.js index 63d1cb415..bf77b08d2 100644 --- a/packages/util-keyring/src/pair/toJson.spec.js +++ b/packages/util-keyring/src/pair/toJson.spec.js @@ -2,7 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const keyring = require('../testingPairs')(); +import testingPairs from '../testingPairs'; + +const keyring = testingPairs(); describe('toJson', () => { it('creates an unencoded output with no passphrase', () => { diff --git a/packages/util-keyring/src/pairs.js b/packages/util-keyring/src/pairs.js index e665814fb..45cb51691 100644 --- a/packages/util-keyring/src/pairs.js +++ b/packages/util-keyring/src/pairs.js @@ -5,19 +5,19 @@ import type { KeyringPairs, KeyringPair } from './types'; +import assert from '@polkadot/util/assert'; +import isHex from '@polkadot/util/is/hex'; +import isU8a from '@polkadot/util/is/u8a'; +import u8aToHex from '@polkadot/util/u8a/toHex'; +import u8aToU8a from '@polkadot/util/u8a/toU8a'; + +import addressDecode from './address/decode'; + type KeyringPairMap = { [Uint8Array]: KeyringPair }; -const assert = require('@polkadot/util/assert'); -const isHex = require('@polkadot/util/is/hex'); -const isU8a = require('@polkadot/util/is/u8a'); -const u8aToHex = require('@polkadot/util/u8a/toHex'); -const u8aToU8a = require('@polkadot/util/u8a/toU8a'); - -const addressDecode = require('./address/decode'); - -module.exports = function pairs (): KeyringPairs { +export default function pairs (): KeyringPairs { const self: KeyringPairMap = {}; return { @@ -47,4 +47,4 @@ module.exports = function pairs (): KeyringPairs { delete self[addressDecode(address)]; } }; -}; +} diff --git a/packages/util-keyring/src/testing.js b/packages/util-keyring/src/testing.js index 6c1e0caf5..8cc1e8d86 100644 --- a/packages/util-keyring/src/testing.js +++ b/packages/util-keyring/src/testing.js @@ -5,10 +5,10 @@ import type { KeyringInstance } from './types'; -const hexToU8a = require('@polkadot/util/hex/toU8a'); -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import hexToU8a from '@polkadot/util/hex/toU8a'; +import u8aFromString from '@polkadot/util/u8a/fromString'; -const createKeyring = require('./index'); +import createKeyring from './index'; function padSeed (seed: string): Uint8Array { return u8aFromString(seed.padEnd(32, ' ')); @@ -33,7 +33,7 @@ const SEEDS = { hexToU8a('0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60') }; -module.exports = function testKeyring (): KeyringInstance { +export default function testKeyring (): KeyringInstance { const keyring = createKeyring(); Object @@ -46,4 +46,4 @@ module.exports = function testKeyring (): KeyringInstance { }); return keyring; -}; +} diff --git a/packages/util-keyring/src/testingPairs.js b/packages/util-keyring/src/testingPairs.js index 6c3280f5f..da8ca8413 100644 --- a/packages/util-keyring/src/testingPairs.js +++ b/packages/util-keyring/src/testingPairs.js @@ -5,15 +5,15 @@ import type { KeyringPair, KeyringInstance } from './types'; +import createKeyring from './testing'; +import nobody from './pair/nobody'; + type TestKeyringMap = { _keyring: KeyringInstance, [string]: KeyringPair }; -const createKeyring = require('./testing'); -const nobody = require('./pair/nobody')(); - -module.exports = function testKeyringPairs (): TestKeyringMap { +export default function testKeyringPairs (): TestKeyringMap { const keyring = createKeyring(); const pairs = keyring.getPairs(); @@ -23,5 +23,5 @@ module.exports = function testKeyringPairs (): TestKeyringMap { result[(name: string)] = pair; return result; - }, { _keyring: keyring, nobody }); -}; + }, { _keyring: keyring, nobody: nobody() }); +} diff --git a/packages/util-rlp/package.json b/packages/util-rlp/package.json index 0dd7d2802..9ebf8e04a 100644 --- a/packages/util-rlp/package.json +++ b/packages/util-rlp/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-rlp", - "version": "0.22.11", + "version": "0.23.0", "description": "RLP encoding and decoding", "main": "index.js", "keywords": [ @@ -34,7 +34,7 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.22.11" + "@polkadot/util": "^0.23.0" }, "devDependencies": { "ethereumjs-testing": "^1.0.4" diff --git a/packages/util-rlp/src/decode.js b/packages/util-rlp/src/decode.js index 876b344b8..3989837ca 100644 --- a/packages/util-rlp/src/decode.js +++ b/packages/util-rlp/src/decode.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const decoder = require('./decoder'); +import decoder from './decoder'; /** @name decode @@ -16,4 +16,4 @@ const decoder = require('./decoder'); encode(new Uint8Array([0x83, 0x64, 0x6f, 0x67])) // => 'dog' as Uint8Array */ -module.exports = decoder; +export default decoder; diff --git a/packages/util-rlp/src/decode.spec.js b/packages/util-rlp/src/decode.spec.js index 311595aaf..aab13584f 100644 --- a/packages/util-rlp/src/decode.spec.js +++ b/packages/util-rlp/src/decode.spec.js @@ -2,12 +2,14 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); -const u8aToHex = require('@polkadot/util/u8a/toHex'); +import hexToU8a from '@polkadot/util/hex/toU8a'; +import u8aToHex from '@polkadot/util/u8a/toHex'; -const { decode, encode } = require('./index'); +import { decode, encode } from './index'; -const rlptests = require('../test/getTests')('RLPTests/rlptest.json'); +import getTests from '../test/getTests'; + +const rlptests = getTests('RLPTests/rlptest.json'); describe('decode', () => { it('returns empty list for undefined inputs', () => { diff --git a/packages/util-rlp/src/decoder/decode.js b/packages/util-rlp/src/decoder/decode.js index 4d666b1b3..9ef1222d7 100644 --- a/packages/util-rlp/src/decoder/decode.js +++ b/packages/util-rlp/src/decoder/decode.js @@ -5,17 +5,17 @@ import type { DecodeFunc, DecodeOutput } from './types'; +import decodeListLong from './listLong'; +import decodeListShort from './listShort'; +import decodeNumber from './number'; +import decodeSingle from './single'; +import decodeString from './string'; + type Decoder = { max: number, fn: (decode: DecodeFunc, input: Uint8Array) => DecodeOutput }; -const decodeListLong = require('./listLong'); -const decodeListShort = require('./listShort'); -const decodeNumber = require('./number'); -const decodeSingle = require('./single'); -const decodeString = require('./string'); - const decoders: Array = [ { max: 0x7f, fn: decodeSingle }, { max: 0xb7, fn: decodeString }, @@ -24,11 +24,9 @@ const decoders: Array = [ { max: 0xff, fn: decodeListLong } ]; -function decode (input: Uint8Array): DecodeOutput { +export default function decode (input: Uint8Array): DecodeOutput { return decoders .find(({ max }) => input[0] <= max) // $FlowFixMe last entry (listLong) is max uint8 value .fn(decode, input); } - -module.exports = decode; diff --git a/packages/util-rlp/src/decoder/index.js b/packages/util-rlp/src/decoder/index.js index 89d811613..56d2e74b9 100644 --- a/packages/util-rlp/src/decoder/index.js +++ b/packages/util-rlp/src/decoder/index.js @@ -3,12 +3,12 @@ // of the ISC license. See the LICENSE file for details. // @flow -const assert = require('@polkadot/util/assert'); +import assert from '@polkadot/util/assert'; -const internalDecode = require('./decode'); +import internalDecode from './decode'; // flowlint-next-line unclear-type:off -module.exports = function decoder (input?: Uint8Array): Uint8Array | Array { +export default function decoder (input?: Uint8Array): Uint8Array | Array { if (!input || input.length === 0) { return new Uint8Array([]); } @@ -18,4 +18,4 @@ module.exports = function decoder (input?: Uint8Array): Uint8Array | Array assert(remainder.length === 0, 'invalid remainder'); return decoded; -}; +} diff --git a/packages/util-rlp/src/decoder/listLong.js b/packages/util-rlp/src/decoder/listLong.js index 44a350fd8..d1b015d53 100644 --- a/packages/util-rlp/src/decoder/listLong.js +++ b/packages/util-rlp/src/decoder/listLong.js @@ -5,11 +5,11 @@ import type { DecodeFunc, DecodeOutput } from './types'; -const assert = require('@polkadot/util/assert'); +import assert from '@polkadot/util/assert'; -const safeParseInt = require('./safeParseInt'); +import safeParseInt from './safeParseInt'; -module.exports = function decodeListLong (decode: DecodeFunc, input: Uint8Array): DecodeOutput { +export default function decodeListLong (decode: DecodeFunc, input: Uint8Array): DecodeOutput { const llength = input[0] - 0xf6; const length = safeParseInt(input.slice(1, llength)); const totalLength = llength + length; @@ -32,4 +32,4 @@ module.exports = function decodeListLong (decode: DecodeFunc, input: Uint8Array) decoded, remainder: input.slice(totalLength) }; -}; +} diff --git a/packages/util-rlp/src/decoder/listShort.js b/packages/util-rlp/src/decoder/listShort.js index 8ef7f481d..c02f12179 100644 --- a/packages/util-rlp/src/decoder/listShort.js +++ b/packages/util-rlp/src/decoder/listShort.js @@ -5,7 +5,7 @@ import type { DecodeFunc, DecodeOutput } from './types'; -module.exports = function decodeListShort (decode: DecodeFunc, input: Uint8Array): DecodeOutput { +export default function decodeListShort (decode: DecodeFunc, input: Uint8Array): DecodeOutput { const length = input[0] - 0xbf; let innerRemainder = input.slice(1, length); const decoded = []; @@ -21,4 +21,4 @@ module.exports = function decodeListShort (decode: DecodeFunc, input: Uint8Array decoded, remainder: input.slice(length) }; -}; +} diff --git a/packages/util-rlp/src/decoder/number.js b/packages/util-rlp/src/decoder/number.js index 3984d9e62..c1911ca29 100644 --- a/packages/util-rlp/src/decoder/number.js +++ b/packages/util-rlp/src/decoder/number.js @@ -5,11 +5,11 @@ import type { DecodeFunc, DecodeOutput } from './types'; -const assert = require('@polkadot/util/assert'); +import assert from '@polkadot/util/assert'; -const safeParseInt = require('./safeParseInt'); +import safeParseInt from './safeParseInt'; -module.exports = function decodeNumber (decode: DecodeFunc, input: Uint8Array): DecodeOutput { +export default function decodeNumber (decode: DecodeFunc, input: Uint8Array): DecodeOutput { const llength = input[0] - 0xb6; const length = safeParseInt(input.slice(1, llength)); const decoded = input.slice(llength, length + llength); @@ -20,4 +20,4 @@ module.exports = function decodeNumber (decode: DecodeFunc, input: Uint8Array): decoded, remainder: input.slice(length + llength) }; -}; +} diff --git a/packages/util-rlp/src/decoder/safeParseInt.js b/packages/util-rlp/src/decoder/safeParseInt.js index 607b7c5e4..abfb52e70 100644 --- a/packages/util-rlp/src/decoder/safeParseInt.js +++ b/packages/util-rlp/src/decoder/safeParseInt.js @@ -3,10 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const assert = require('@polkadot/util/assert'); +import assert from '@polkadot/util/assert'; -module.exports = function safeParseInt (input: Uint8Array): number { +export default function safeParseInt (input: Uint8Array): number { assert(input[0] > 0, 'invalid RLP: extra zeros'); return input.reduce((result, value) => (result * 256) + value, 0); -}; +} diff --git a/packages/util-rlp/src/decoder/safeParseInt.spec.js b/packages/util-rlp/src/decoder/safeParseInt.spec.js index cfe39f6c9..500bd6585 100644 --- a/packages/util-rlp/src/decoder/safeParseInt.spec.js +++ b/packages/util-rlp/src/decoder/safeParseInt.spec.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const safeParseInt = require('./safeParseInt'); +import safeParseInt from './safeParseInt'; describe('safeParseInt', () => { it('does not allow 0 in first position', () => { diff --git a/packages/util-rlp/src/decoder/single.js b/packages/util-rlp/src/decoder/single.js index 7d73b3f08..ad9bade99 100644 --- a/packages/util-rlp/src/decoder/single.js +++ b/packages/util-rlp/src/decoder/single.js @@ -5,9 +5,9 @@ import type { DecodeFunc, DecodeOutput } from './types'; -module.exports = function decodeSingle (decode: DecodeFunc, input: Uint8Array): DecodeOutput { +export default function decodeSingle (decode: DecodeFunc, input: Uint8Array): DecodeOutput { return { decoded: input.slice(0, 1), remainder: input.slice(1) }; -}; +} diff --git a/packages/util-rlp/src/decoder/string.js b/packages/util-rlp/src/decoder/string.js index 377628aaa..caf1666f1 100644 --- a/packages/util-rlp/src/decoder/string.js +++ b/packages/util-rlp/src/decoder/string.js @@ -5,9 +5,9 @@ import type { DecodeFunc, DecodeOutput } from './types'; -const assert = require('@polkadot/util/assert'); +import assert from '@polkadot/util/assert'; -module.exports = function decodeString (decode: DecodeFunc, input: Uint8Array): DecodeOutput { +export default function decodeString (decode: DecodeFunc, input: Uint8Array): DecodeOutput { const firstByte = input[0]; const length = firstByte - 0x7f; let decoded; @@ -25,4 +25,4 @@ module.exports = function decodeString (decode: DecodeFunc, input: Uint8Array): decoded, remainder: input.slice(length) }; -}; +} diff --git a/packages/util-rlp/src/encode.js b/packages/util-rlp/src/encode.js index 8e5224b75..5fb1b9cf8 100644 --- a/packages/util-rlp/src/encode.js +++ b/packages/util-rlp/src/encode.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const encoder = require('./encoder'); +import encoder from './encoder'; /** @name encode @@ -16,4 +16,4 @@ const encoder = require('./encoder'); encode('dog') // => [0x83, 0x64, 0x6f, 0x67] */ -module.exports = encoder; +export default encoder; diff --git a/packages/util-rlp/src/encode.spec.js b/packages/util-rlp/src/encode.spec.js index 67d858c93..54f12fa97 100644 --- a/packages/util-rlp/src/encode.spec.js +++ b/packages/util-rlp/src/encode.spec.js @@ -2,11 +2,13 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const u8aToHex = require('@polkadot/util/u8a/toHex'); +import u8aToHex from '@polkadot/util/u8a/toHex'; -const { encode } = require('./index'); +import { encode } from './index'; -const rlptests = require('../test/getTests')('RLPTests/rlptest.json'); +import getTests from '../test/getTests'; + +const rlptests = getTests('RLPTests/rlptest.json'); describe('encode', () => { rlptests.forEach(({ name, input, output }) => { diff --git a/packages/util-rlp/src/encoder/array.js b/packages/util-rlp/src/encoder/array.js index 1e8f86355..661e783f4 100644 --- a/packages/util-rlp/src/encoder/array.js +++ b/packages/util-rlp/src/encoder/array.js @@ -5,16 +5,16 @@ import type { EncodeFunc } from './types'; -const u8aConcat = require('@polkadot/util/u8a/concat'); +import u8aConcat from '@polkadot/util/u8a/concat'; -const encodeLength = require('./length'); +import encodeLength from './length'; // flowlint-next-line unclear-type:off -module.exports = function encodeArray (encoder: EncodeFunc, input: any): Uint8Array { +export default function encodeArray (encoder: EncodeFunc, input: any): Uint8Array { const encoded = u8aConcat.apply(null, input.map(encoder)); return u8aConcat( encodeLength(encoded.length, 192), encoded ); -}; +} diff --git a/packages/util-rlp/src/encoder/index.js b/packages/util-rlp/src/encoder/index.js index be94eef5a..7d838ee4f 100644 --- a/packages/util-rlp/src/encoder/index.js +++ b/packages/util-rlp/src/encoder/index.js @@ -3,17 +3,15 @@ // of the ISC license. See the LICENSE file for details. // @flow -const encodeArray = require('./array'); -const toU8a = require('./toU8a'); -const encodeU8a = require('./u8a'); +import encodeArray from './array'; +import toU8a from './toU8a'; +import encodeU8a from './u8a'; // flowlint-next-line unclear-type:off -function encoder (input: any): Uint8Array { +export default function encoder (input: any): Uint8Array { if (input instanceof Array) { return encodeArray(encoder, input); } return encodeU8a(encoder, toU8a(input)); } - -module.exports = encoder; diff --git a/packages/util-rlp/src/encoder/length.js b/packages/util-rlp/src/encoder/length.js index e5d82d995..ae3e1e907 100644 --- a/packages/util-rlp/src/encoder/length.js +++ b/packages/util-rlp/src/encoder/length.js @@ -3,10 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const numberToHex = require('@polkadot/util/number/toHex'); -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import numberToHex from '@polkadot/util/number/toHex'; +import hexToU8a from '@polkadot/util/hex/toU8a'; -module.exports = function encodeLength (length: number, offset: number): Uint8Array { +export default function encodeLength (length: number, offset: number): Uint8Array { if (length < 56) { return new Uint8Array([length + offset]); } @@ -16,4 +16,4 @@ module.exports = function encodeLength (length: number, offset: number): Uint8Ar return hexToU8a( numberToHex(offset + 55 + hexLength.length / 2) + hexLength ); -}; +} diff --git a/packages/util-rlp/src/encoder/length.spec.js b/packages/util-rlp/src/encoder/length.spec.js index 2a4ab2f44..b5ea0b9d8 100644 --- a/packages/util-rlp/src/encoder/length.spec.js +++ b/packages/util-rlp/src/encoder/length.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const encodeLength = require('./length'); +import encodeLength from './length'; describe('encodeLength', () => { it('returns offset + length where <= 55', () => { diff --git a/packages/util-rlp/src/encoder/toU8a.js b/packages/util-rlp/src/encoder/toU8a.js index bffc5b51c..1f5da0c60 100644 --- a/packages/util-rlp/src/encoder/toU8a.js +++ b/packages/util-rlp/src/encoder/toU8a.js @@ -3,6 +3,21 @@ // of the ISC license. See the LICENSE file for details. // @flow +import assert from '@polkadot/util/assert'; +import bnToU8a from '@polkadot/util/bn/toU8a'; +import bufferToU8a from '@polkadot/util/buffer/toU8a'; +import hexHasPrefix from '@polkadot/util/hex/hasPrefix'; +import hexToU8a from '@polkadot/util/hex/toU8a'; +import isBn from '@polkadot/util/is/bn'; +import isBuffer from '@polkadot/util/is/buffer'; +import isNull from '@polkadot/util/is/null'; +import isNumber from '@polkadot/util/is/number'; +import isString from '@polkadot/util/is/string'; +import isU8a from '@polkadot/util/is/u8a'; +import isUndefined from '@polkadot/util/is/undefined'; +import numberToU8a from '@polkadot/util/number/toU8a'; +import u8aFromUtf8 from '@polkadot/util/u8a/fromUtf8'; + type Encoder = { // flowlint-next-line unclear-type:off check: (value: any) => boolean, @@ -10,21 +25,6 @@ type Encoder = { fn: (value: any) => Uint8Array }; -const assert = require('@polkadot/util/assert'); -const bnToU8a = require('@polkadot/util/bn/toU8a'); -const bufferToU8a = require('@polkadot/util/buffer/toU8a'); -const hexHasPrefix = require('@polkadot/util/hex/hasPrefix'); -const hexToU8a = require('@polkadot/util/hex/toU8a'); -const isBn = require('@polkadot/util/is/bn'); -const isBuffer = require('@polkadot/util/is/buffer'); -const isNull = require('@polkadot/util/is/null'); -const isNumber = require('@polkadot/util/is/number'); -const isString = require('@polkadot/util/is/string'); -const isU8a = require('@polkadot/util/is/u8a'); -const isUndefined = require('@polkadot/util/is/undefined'); -const numberToU8a = require('@polkadot/util/number/toU8a'); -const u8aFromUtf8 = require('@polkadot/util/u8a/fromUtf8'); - function newEmpty (): Uint8Array { return new Uint8Array([]); } @@ -53,11 +53,11 @@ const encoders: Array = [ ]; // flowlint-next-line unclear-type:off -module.exports = function toU8a (value: any): Uint8Array { +export default function toU8a (value: any): Uint8Array { const encoder = encoders.find(({ check }) => check(value)); assert(encoder, 'invalid type'); // $FlowFixMe value checked above return encoder.fn(value); -}; +} diff --git a/packages/util-rlp/src/encoder/toU8a.spec.js b/packages/util-rlp/src/encoder/toU8a.spec.js index 52012142b..023b4e82c 100644 --- a/packages/util-rlp/src/encoder/toU8a.spec.js +++ b/packages/util-rlp/src/encoder/toU8a.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const BN = require('bn.js'); +import BN from 'bn.js'; -const toU8a = require('./toU8a'); +import toU8a from './toU8a'; describe('toU8a', () => { it('returns a Uint8Array input as-is', () => { diff --git a/packages/util-rlp/src/encoder/u8a.js b/packages/util-rlp/src/encoder/u8a.js index e09fbd103..88cc302ca 100644 --- a/packages/util-rlp/src/encoder/u8a.js +++ b/packages/util-rlp/src/encoder/u8a.js @@ -5,11 +5,11 @@ import type { EncodeFunc } from './types'; -const u8aConcat = require('@polkadot/util/u8a/concat'); +import u8aConcat from '@polkadot/util/u8a/concat'; -const encodeLength = require('./length'); +import encodeLength from './length'; -module.exports = function encodeU8a (encoder: EncodeFunc, input: Uint8Array): Uint8Array { +export default function encodeU8a (encoder: EncodeFunc, input: Uint8Array): Uint8Array { if (input.length === 1 && input[0] < 128) { return input; } @@ -18,4 +18,4 @@ module.exports = function encodeU8a (encoder: EncodeFunc, input: Uint8Array): Ui encodeLength(input.length, 128), input ); -}; +} diff --git a/packages/util-rlp/src/index.js b/packages/util-rlp/src/index.js index f661193f6..85c654cad 100644 --- a/packages/util-rlp/src/index.js +++ b/packages/util-rlp/src/index.js @@ -3,13 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -const decode = require('./decode'); -const encode = require('./encode'); +import decode from './decode'; +import encode from './encode'; /** @summary Utility methods to encode and decode [Ethereum RLP](https://github.com/ethereum/wiki/wiki/RLP). Adapted from the [EthereumJS implementation](https://github.com/ethereumjs/rlp/blob/0ce09db81fc303fcee593f7cc094ba44015f9b92/index.js) */ -module.exports = { +export { decode, encode }; diff --git a/packages/util-rlp/test/getTests.js b/packages/util-rlp/test/getTests.js index bfa1f0506..7b46f323c 100644 --- a/packages/util-rlp/test/getTests.js +++ b/packages/util-rlp/test/getTests.js @@ -2,12 +2,12 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const BN = require('bn.js'); -const { getSingleFile } = require('ethereumjs-testing'); +import BN from 'bn.js'; +import { getSingleFile } from 'ethereumjs-testing'; -const bnToU8a = require('@polkadot/util/bn/toU8a'); +import bnToU8a from '@polkadot/util/bn/toU8a'; -module.exports = function getTests (file) { +export default function getTests (file) { const tests = getSingleFile(file); return Object.keys(tests).map((name) => ({ @@ -17,4 +17,4 @@ module.exports = function getTests (file) { : bnToU8a(new BN(tests[name].in.slice(1))), output: `0x${tests[name].out.toLowerCase()}` })); -}; +} diff --git a/packages/util-triedb/package.json b/packages/util-triedb/package.json index 30db3166a..85eda7299 100644 --- a/packages/util-triedb/package.json +++ b/packages/util-triedb/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-triedb", - "version": "0.22.11", + "version": "0.23.0", "description": "Creation of triehash values", "main": "index.js", "keywords": [ @@ -34,10 +34,10 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.22.11", - "@polkadot/util-crypto": "^0.22.11", - "@polkadot/util-rlp": "^0.22.11", - "@polkadot/util-triehash": "^0.22.11", + "@polkadot/util": "^0.23.0", + "@polkadot/util-crypto": "^0.23.0", + "@polkadot/util-rlp": "^0.23.0", + "@polkadot/util-triehash": "^0.23.0", "leveldown": "^4.0.1", "levelup": "^3.0.1", "memdown": "^3.0.0" diff --git a/packages/util-triedb/src/clear.js b/packages/util-triedb/src/clear.js index 7189a2da0..0ee80b855 100644 --- a/packages/util-triedb/src/clear.js +++ b/packages/util-triedb/src/clear.js @@ -5,5 +5,5 @@ import type { TrieDbState } from './types'; -module.exports = function clear (self: TrieDbState): void { -}; +export default function clear (self: TrieDbState): void { +} diff --git a/packages/util-triedb/src/commit.js b/packages/util-triedb/src/commit.js index 7aedc341a..d00ea9f64 100644 --- a/packages/util-triedb/src/commit.js +++ b/packages/util-triedb/src/commit.js @@ -5,5 +5,5 @@ import type { TrieDbState } from './types'; -module.exports = function commit (self: TrieDbState): void { -}; +export default function commit (self: TrieDbState): void { +} diff --git a/packages/util-triedb/src/index.js b/packages/util-triedb/src/index.js index c03367b97..8414f5c8c 100644 --- a/packages/util-triedb/src/index.js +++ b/packages/util-triedb/src/index.js @@ -6,11 +6,11 @@ // import type { LevelUp$AbstractStorage } from 'levelup'; // import type { TrieDb } from './types'; -// const clear = require('./clear'); -// const commit = require('./commit'); -// const state = require('./state'); +// const clear from './clear'); +// const commit from './commit'); +// const state from './state'); -// module.exports = function triedb (db: LevelUp$AbstractStorage): TrieDb { +// export default function triedb (db: LevelUp$AbstractStorage): TrieDb { // const self = state(db); // return { diff --git a/packages/util-triedb/src/state.js b/packages/util-triedb/src/state.js index 7eb6eb28a..6322606f2 100644 --- a/packages/util-triedb/src/state.js +++ b/packages/util-triedb/src/state.js @@ -6,16 +6,16 @@ import type { LevelUp$AbstractStorage } from 'levelup'; import type { TrieDbState } from './types'; -const l = require('@polkadot/util/logger')('triedb'); +import logger from '@polkadot/util/logger'; -const createTemp = require('./temp'); +import createTemp from './temp'; -module.exports = function state (db: LevelUp$AbstractStorage): TrieDbState { +export default function state (db: LevelUp$AbstractStorage): TrieDbState { const storage = createTemp(); return { db, - l, + l: logger('triedb'), storage }; -}; +} diff --git a/packages/util-triedb/src/temp/clear.spec.js b/packages/util-triedb/src/temp/clear.spec.js index c98331fc9..e112ab673 100644 --- a/packages/util-triedb/src/temp/clear.spec.js +++ b/packages/util-triedb/src/temp/clear.spec.js @@ -2,9 +2,15 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const memory = require('./index')(); +import factory from './index'; describe('clear', () => { + let memory; + + beforeEach(() => { + memory = factory(); + }); + it('clears all values', () => { memory.set(new Uint8Array([1]), new Uint8Array([2])); memory.clear(); diff --git a/packages/util-triedb/src/temp/commit.js b/packages/util-triedb/src/temp/commit.js index 155f76acd..e6222a063 100644 --- a/packages/util-triedb/src/temp/commit.js +++ b/packages/util-triedb/src/temp/commit.js @@ -6,10 +6,10 @@ import type { Trie$Pairs } from '@polkadot/util-triehash/types'; import type { Temp$Storage } from './types'; -const del = require('./del'); -const set = require('./set'); +import del from './del'; +import set from './set'; -module.exports = function commit (storage: Temp$Storage, values: Trie$Pairs): void { +export default function commit (storage: Temp$Storage, values: Trie$Pairs): void { values.forEach(({ k, v }) => { if (v && v.length) { set(storage, k, v); @@ -17,4 +17,4 @@ module.exports = function commit (storage: Temp$Storage, values: Trie$Pairs): vo del(storage, k); } }); -}; +} diff --git a/packages/util-triedb/src/temp/commit.spec.js b/packages/util-triedb/src/temp/commit.spec.js index 09f61f9b2..957fb6715 100644 --- a/packages/util-triedb/src/temp/commit.spec.js +++ b/packages/util-triedb/src/temp/commit.spec.js @@ -2,15 +2,17 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const memory = require('./index')(); +import factory from './index'; describe('commit', () => { + let memory; const k1 = new Uint8Array([1]); const k2 = new Uint8Array([2]); const v1 = new Uint8Array([9]); const v2 = new Uint8Array([8]); beforeEach(() => { + memory = factory(); memory.commit([ { k: k1, v: v1 } ]); diff --git a/packages/util-triedb/src/temp/del.js b/packages/util-triedb/src/temp/del.js index be2cf98fa..c897a0128 100644 --- a/packages/util-triedb/src/temp/del.js +++ b/packages/util-triedb/src/temp/del.js @@ -5,6 +5,6 @@ import type { Temp$Storage } from './types'; -module.exports = function del (storage: Temp$Storage, k: Uint8Array): void { +export default function del (storage: Temp$Storage, k: Uint8Array): void { delete storage[k]; -}; +} diff --git a/packages/util-triedb/src/temp/del.spec.js b/packages/util-triedb/src/temp/del.spec.js index 9a38f7de6..2e43bc37d 100644 --- a/packages/util-triedb/src/temp/del.spec.js +++ b/packages/util-triedb/src/temp/del.spec.js @@ -2,9 +2,15 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const memory = require('./index')(); +import factory from './index'; describe('del', () => { + let memory; + + beforeEach(() => { + memory = factory(); + }); + it('removes a value', () => { memory.set(new Uint8Array([1]), new Uint8Array([2])); memory.del(new Uint8Array([1])); diff --git a/packages/util-triedb/src/temp/get.js b/packages/util-triedb/src/temp/get.js index 6b8fdd37b..8219addfb 100644 --- a/packages/util-triedb/src/temp/get.js +++ b/packages/util-triedb/src/temp/get.js @@ -5,10 +5,10 @@ import type { Temp$Storage } from './types'; -module.exports = function get (storage: Temp$Storage, k: Uint8Array): Uint8Array | null { +export default function get (storage: Temp$Storage, k: Uint8Array): Uint8Array | null { const value = storage[k]; return value && value.v ? value.v.slice() : null; -}; +} diff --git a/packages/util-triedb/src/temp/index.js b/packages/util-triedb/src/temp/index.js index 708f9fc42..14099903e 100644 --- a/packages/util-triedb/src/temp/index.js +++ b/packages/util-triedb/src/temp/index.js @@ -7,15 +7,15 @@ import type { Trie$Pairs } from '@polkadot/util-triehash/types'; import type { TrieDb } from '../types'; import type { Temp$Storage } from './types'; -const trieRoot = require('@polkadot/util-triehash/root'); +import trieRoot from '@polkadot/util-triehash/root'; -const commit = require('./commit'); -const del = require('./del'); -const get = require('./get'); -const pairs = require('./pairs'); -const set = require('./set'); +import commit from './commit'; +import del from './del'; +import get from './get'; +import pairs from './pairs'; +import set from './set'; -module.exports = function temp (): TrieDb { +export default function temp (): TrieDb { let storage: Temp$Storage = {}; return { @@ -37,4 +37,4 @@ module.exports = function temp (): TrieDb { trieRoot: (): Uint8Array => trieRoot(pairs(storage)) }; -}; +} diff --git a/packages/util-triedb/src/temp/pairs.js b/packages/util-triedb/src/temp/pairs.js index 97f196260..b5b4bc16e 100644 --- a/packages/util-triedb/src/temp/pairs.js +++ b/packages/util-triedb/src/temp/pairs.js @@ -6,9 +6,9 @@ import type { Trie$Pairs } from '@polkadot/util-triehash/types'; import type { Temp$Storage } from './types'; -module.exports = function pairs (storage: Temp$Storage): Trie$Pairs { +export default function pairs (storage: Temp$Storage): Trie$Pairs { // flowlint-next-line unclear-type:off const keys = ((Object.keys(storage): any): Array); return keys.map((k) => storage[k]); -}; +} diff --git a/packages/util-triedb/src/temp/set.js b/packages/util-triedb/src/temp/set.js index cdee90336..bff452ae3 100644 --- a/packages/util-triedb/src/temp/set.js +++ b/packages/util-triedb/src/temp/set.js @@ -6,9 +6,9 @@ import type { Trie$Pair } from '@polkadot/util-triehash/types'; import type { Temp$Storage } from './types'; -module.exports = function set (storage: Temp$Storage, k: Uint8Array, v: Uint8Array): void { +export default function set (storage: Temp$Storage, k: Uint8Array, v: Uint8Array): void { storage[k] = ({ k: k.slice(), v: v.slice() }: Trie$Pair); -}; +} diff --git a/packages/util-triedb/src/temp/trieRoot.spec.js b/packages/util-triedb/src/temp/trieRoot.spec.js index d6aed80c0..671328a63 100644 --- a/packages/util-triedb/src/temp/trieRoot.spec.js +++ b/packages/util-triedb/src/temp/trieRoot.spec.js @@ -2,12 +2,18 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const u8aFromString = require('@polkadot/util/u8a/fromString'); -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import u8aFromString from '@polkadot/util/u8a/fromString'; +import hexToU8a from '@polkadot/util/hex/toU8a'; -const temp = require('./index')(); +import factory from './index'; describe('trieRoot', () => { + let temp; + + beforeEach(() => { + temp = factory(); + }); + it('calculates the correct trieRoot', () => { temp.set(u8aFromString('doe'), u8aFromString('reindeer')); temp.set(u8aFromString('dog'), u8aFromString('puppy')); diff --git a/packages/util-triehash/package.json b/packages/util-triehash/package.json index 64c56fe34..0eb5eb9f5 100644 --- a/packages/util-triehash/package.json +++ b/packages/util-triehash/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-triehash", - "version": "0.22.11", + "version": "0.23.0", "description": "Creation of triehash values", "main": "index.js", "keywords": [ @@ -34,8 +34,8 @@ }, "dependencies": { "@babel/runtime": "^7.0.0-beta.47", - "@polkadot/util": "^0.22.11", - "@polkadot/util-crypto": "^0.22.11", - "@polkadot/util-rlp": "^0.22.11" + "@polkadot/util": "^0.23.0", + "@polkadot/util-crypto": "^0.23.0", + "@polkadot/util-rlp": "^0.23.0" } } diff --git a/packages/util-triehash/src/encode/aux.js b/packages/util-triehash/src/encode/aux.js index 1d56c7235..b46093737 100644 --- a/packages/util-triehash/src/encode/aux.js +++ b/packages/util-triehash/src/encode/aux.js @@ -5,13 +5,13 @@ import type { Trie$Pairs } from '../types'; -const keccakAsU8a = require('@polkadot/util-crypto/keccak/asU8a'); -const rlpEncode = require('@polkadot/util-rlp/encode'); +import keccakAsU8a from '@polkadot/util-crypto/keccak/asU8a'; +import rlpEncode from '@polkadot/util-rlp/encode'; -const encode = require('./index'); +import encode from './index'; // flowlint-next-line unclear-type:off -module.exports = function encodeAux (pairs: Trie$Pairs, preLength: number): any { +export default function encodeAux (pairs: Trie$Pairs, preLength: number): any { const encoded = encode(pairs, preLength); const rlped = rlpEncode(encoded); @@ -20,4 +20,4 @@ module.exports = function encodeAux (pairs: Trie$Pairs, preLength: number): any } return keccakAsU8a(rlped); -}; +} diff --git a/packages/util-triehash/src/encode/hexPrefix.js b/packages/util-triehash/src/encode/hexPrefix.js index f4ac63b98..a216975d9 100644 --- a/packages/util-triehash/src/encode/hexPrefix.js +++ b/packages/util-triehash/src/encode/hexPrefix.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -module.exports = function encodeHexPrefix (nibbles: Uint8Array | Array, isLeaf: boolean): Uint8Array { +export default function encodeHexPrefix (nibbles: Uint8Array | Array, isLeaf: boolean): Uint8Array { const bits = ((nibbles.length & 1) + (2 * (isLeaf ? 1 : 0))) << 4; const result = []; let offset = nibbles.length % 2; @@ -16,4 +16,4 @@ module.exports = function encodeHexPrefix (nibbles: Uint8Array | Array, } return new Uint8Array(result); -}; +} diff --git a/packages/util-triehash/src/encode/hexPrefix.spec.js b/packages/util-triehash/src/encode/hexPrefix.spec.js index 91d92e02c..426380909 100644 --- a/packages/util-triehash/src/encode/hexPrefix.spec.js +++ b/packages/util-triehash/src/encode/hexPrefix.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const encodeHexPrefix = require('./hexPrefix'); +import encodeHexPrefix from './hexPrefix'; describe('encodeHexPrefix', () => { it('encodes [0, 0, 1, 2, 3, 4, 5], false', () => { diff --git a/packages/util-triehash/src/encode/index.js b/packages/util-triehash/src/encode/index.js index d66edb1bf..9ea85fb03 100644 --- a/packages/util-triehash/src/encode/index.js +++ b/packages/util-triehash/src/encode/index.js @@ -5,16 +5,13 @@ import type { Trie$Pairs } from '../types'; -// FIXME: This is horrible, but since aux calls back into encode (and ius used by the signgular encodes below, we set the export to valid before it is used) -module.exports = encode; - -const encodePairs = require('./pairs'); -const encodeShared = require('./shared'); -const encodeSingle = require('./single'); -const sharedPrefixLength = require('../util/sharedPrefixLength'); +import encodePairs from './pairs'; +import encodeShared from './shared'; +import encodeSingle from './single'; +import sharedPrefixLength from '../util/sharedPrefixLength'; // flowlint-next-line unclear-type:off -function encode (pairs: Trie$Pairs, preLength: number): Array { +export default function encode (pairs: Trie$Pairs, preLength: number): Array { if (pairs.length === 0) { return []; } diff --git a/packages/util-triehash/src/encode/pairs.js b/packages/util-triehash/src/encode/pairs.js index 3ebbb9dd5..6674d4454 100644 --- a/packages/util-triehash/src/encode/pairs.js +++ b/packages/util-triehash/src/encode/pairs.js @@ -5,7 +5,7 @@ import type { Trie$Pairs } from '../types'; -const encodeAux = require('./aux'); +import encodeAux from './aux'; function calcLength (pairs: Trie$Pairs, preLength: number, index: number, start: number): number { let length = 0; @@ -22,7 +22,7 @@ function calcLength (pairs: Trie$Pairs, preLength: number, index: number, start: } // flowlint-next-line unclear-type:off -module.exports = function encodePairs (pairs: Trie$Pairs, preLength: number): Array { +export default function encodePairs (pairs: Trie$Pairs, preLength: number): Array { const result = []; let start = preLength === pairs[0].k.length @@ -45,4 +45,4 @@ module.exports = function encodePairs (pairs: Trie$Pairs, preLength: number): Ar } return result; -}; +} diff --git a/packages/util-triehash/src/encode/shared.js b/packages/util-triehash/src/encode/shared.js index 69a2a2bbe..f32510a2e 100644 --- a/packages/util-triehash/src/encode/shared.js +++ b/packages/util-triehash/src/encode/shared.js @@ -5,11 +5,11 @@ import type { Trie$Pairs } from '../types'; -const encodeAux = require('./aux'); -const encodeHexPrefix = require('./hexPrefix'); +import encodeAux from './aux'; +import encodeHexPrefix from './hexPrefix'; // flowlint-next-line unclear-type:off -module.exports = function encodeShared (pairs: Trie$Pairs, preLength: number, sharedLength: number): Array { +export default function encodeShared (pairs: Trie$Pairs, preLength: number, sharedLength: number): Array { return [ encodeHexPrefix( pairs[0].k.slice(preLength, sharedLength), @@ -17,4 +17,4 @@ module.exports = function encodeShared (pairs: Trie$Pairs, preLength: number, sh ), encodeAux(pairs, sharedLength) ]; -}; +} diff --git a/packages/util-triehash/src/encode/single.js b/packages/util-triehash/src/encode/single.js index 0dc607699..181211948 100644 --- a/packages/util-triehash/src/encode/single.js +++ b/packages/util-triehash/src/encode/single.js @@ -5,10 +5,10 @@ import type { Trie$Pair } from '../types'; -const encodeHexPrefix = require('./hexPrefix'); +import encodeHexPrefix from './hexPrefix'; // flowlint-next-line unclear-type:off -module.exports = function encodeSingle (pair: Trie$Pair, preLength: number): Array { +export default function encodeSingle (pair: Trie$Pair, preLength: number): Array { return [ encodeHexPrefix( pair.k.slice(preLength), @@ -16,4 +16,4 @@ module.exports = function encodeSingle (pair: Trie$Pair, preLength: number): Arr ), pair.v ]; -}; +} diff --git a/packages/util-triehash/src/index.js b/packages/util-triehash/src/index.js index 685842a94..e395698c2 100644 --- a/packages/util-triehash/src/index.js +++ b/packages/util-triehash/src/index.js @@ -3,13 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -const trieRoot = require('./root'); -const trieRootOrdered = require('./rootOrdered'); +import trieRoot from './root'; +import trieRootOrdered from './rootOrdered'; /** @summary Utility methods to create [Ethereum Trie hashes](https://github.com/ethereum/wiki/wiki/Patricia-Tree) */ -module.exports = { +export { trieRoot, trieRootOrdered }; diff --git a/packages/util-triehash/src/root.js b/packages/util-triehash/src/root.js index 6eadffdab..83d1e9665 100644 --- a/packages/util-triehash/src/root.js +++ b/packages/util-triehash/src/root.js @@ -5,9 +5,9 @@ import type { Trie$Pairs } from './types'; -const asNibbles = require('./util/asNibbles'); -const genRoot = require('./util/genRoot'); -const pairsUniq = require('./util/pairsUniq'); +import asNibbles from './util/asNibbles'; +import genRoot from './util/genRoot'; +import pairsUniq from './util/pairsUniq'; /** @name trieRoot @@ -24,11 +24,11 @@ const pairsUniq = require('./util/pairsUniq'); v: u8aFromString('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') }]) // => 0xd23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab */ -module.exports = function trieRoot (pairs: Trie$Pairs): Uint8Array { +export default function trieRoot (pairs: Trie$Pairs): Uint8Array { return genRoot( pairsUniq(pairs).map(({ k, v }) => ({ k: asNibbles(k), v })) ); -}; +} diff --git a/packages/util-triehash/src/root.spec.js b/packages/util-triehash/src/root.spec.js index 9b4b58ee0..ad3a8cf78 100644 --- a/packages/util-triehash/src/root.spec.js +++ b/packages/util-triehash/src/root.spec.js @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const u8aFromString = require('@polkadot/util/u8a/fromString'); -const hexToU8a = require('@polkadot/util/hex/toU8a'); +import u8aFromString from '@polkadot/util/u8a/fromString'; +import hexToU8a from '@polkadot/util/hex/toU8a'; -const { trieRoot } = require('./index'); +import { trieRoot } from './index'; describe('trieRoot', () => { it('encodes empty k/v pairs', () => { diff --git a/packages/util-triehash/src/rootOrdered.js b/packages/util-triehash/src/rootOrdered.js index 658fd7520..c93f86616 100644 --- a/packages/util-triehash/src/rootOrdered.js +++ b/packages/util-triehash/src/rootOrdered.js @@ -2,11 +2,11 @@ // of the ISC license. See the LICENSE file for details. // @flow -const rlpEncode = require('@polkadot/util-rlp/encode'); +import rlpEncode from '@polkadot/util-rlp/encode'; -const asNibbles = require('./util/asNibbles'); -const genRoot = require('./util/genRoot'); -const pairsUniq = require('./util/pairsUniq'); +import asNibbles from './util/asNibbles'; +import genRoot from './util/genRoot'; +import pairsUniq from './util/pairsUniq'; /** @name trieRootOrdered @@ -23,7 +23,7 @@ const pairsUniq = require('./util/pairsUniq'); u8aFromString('reindeer') ]) // => 0xe766d5d51b89dc39d981b41bda63248d7abce4f0225eefd023792a540bcffee3 */ -module.exports = function trieRootOrdered (values: Array): Uint8Array { +export default function trieRootOrdered (values: Array): Uint8Array { return genRoot( pairsUniq( values.map((v, index) => ({ @@ -34,4 +34,4 @@ module.exports = function trieRootOrdered (values: Array): Uint8Arra })) ) ); -}; +} diff --git a/packages/util-triehash/src/rootOrdered.spec.js b/packages/util-triehash/src/rootOrdered.spec.js index 018b27342..1ffbb3443 100644 --- a/packages/util-triehash/src/rootOrdered.spec.js +++ b/packages/util-triehash/src/rootOrdered.spec.js @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const hexToU8a = require('@polkadot/util/hex/toU8a'); -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import hexToU8a from '@polkadot/util/hex/toU8a'; +import u8aFromString from '@polkadot/util/u8a/fromString'; -const { trieRootOrdered } = require('./index'); +import { trieRootOrdered } from './index'; describe('trieRootOrdered', () => { // https://github.com/paritytech/parity/blob/e95b09348386d01b71901365785c5fa3aa2f7a6d/util/triehash/src/lib.rs#L42 diff --git a/packages/util-triehash/src/util/asNibbles.js b/packages/util-triehash/src/util/asNibbles.js index f217e2ff3..bfa065f5b 100644 --- a/packages/util-triehash/src/util/asNibbles.js +++ b/packages/util-triehash/src/util/asNibbles.js @@ -14,7 +14,7 @@ asNibbles(new Uint8Array([0x41, 0x20]) // => Uint8Array([4, 1, 2, 0]) */ -module.exports = function asNibbles (bytes: Uint8Array | Array): Uint8Array { +export default function asNibbles (bytes: Uint8Array | Array): Uint8Array { return bytes.reduce((result, byte, index) => { result.set( [byte >> 4, byte & 0b1111], @@ -23,4 +23,4 @@ module.exports = function asNibbles (bytes: Uint8Array | Array): Uint8Ar return result; }, new Uint8Array(bytes.length * 2)); -}; +} diff --git a/packages/util-triehash/src/util/asNibbles.spec.js b/packages/util-triehash/src/util/asNibbles.spec.js index 128bcb8c6..9785fb145 100644 --- a/packages/util-triehash/src/util/asNibbles.spec.js +++ b/packages/util-triehash/src/util/asNibbles.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const u8aFromString = require('@polkadot/util/u8a/fromString'); +import u8aFromString from '@polkadot/util/u8a/fromString'; -const asNibbles = require('./asNibbles'); +import asNibbles from './asNibbles'; describe('asNibbles', () => { it('converts an array', () => { diff --git a/packages/util-triehash/src/util/genRoot.js b/packages/util-triehash/src/util/genRoot.js index 9ee09df61..7c0462d30 100644 --- a/packages/util-triehash/src/util/genRoot.js +++ b/packages/util-triehash/src/util/genRoot.js @@ -5,12 +5,12 @@ import type { Trie$Pairs } from '../types'; -const keccakAsU8a = require('@polkadot/util-crypto/keccak/asU8a'); -const rlpEncode = require('@polkadot/util-rlp/encode'); +import keccakAsU8a from '@polkadot/util-crypto/keccak/asU8a'; +import rlpEncode from '@polkadot/util-rlp/encode'; -const encode = require('../encode'); +import encode from '../encode'; -module.exports = function genRoot (pairs: Trie$Pairs): Uint8Array { +export default function genRoot (pairs: Trie$Pairs): Uint8Array { const encoded = encode(pairs, 0); return keccakAsU8a( @@ -20,4 +20,4 @@ module.exports = function genRoot (pairs: Trie$Pairs): Uint8Array { : new Uint8Array([]) ) ); -}; +} diff --git a/packages/util-triehash/src/util/pairsUniq.js b/packages/util-triehash/src/util/pairsUniq.js index 2a864755b..01cc2c14b 100644 --- a/packages/util-triehash/src/util/pairsUniq.js +++ b/packages/util-triehash/src/util/pairsUniq.js @@ -5,13 +5,13 @@ import type { Trie$Pair, Trie$Pairs } from '../types'; +import u8aToHex from '@polkadot/util/u8a/toHex'; + type PairMap = { [string]: Trie$Pair }; -const u8aToHex = require('@polkadot/util/u8a/toHex'); - -module.exports = function pairsUniq (pairs: Trie$Pairs): Trie$Pairs { +export default function pairsUniq (pairs: Trie$Pairs): Trie$Pairs { const map: PairMap = pairs.reduce((result, pair) => { result[u8aToHex(pair.k)] = pair; @@ -22,4 +22,4 @@ module.exports = function pairsUniq (pairs: Trie$Pairs): Trie$Pairs { .keys(map) .sort() .map((key) => map[key]); -}; +} diff --git a/packages/util-triehash/src/util/pairsUniq.spec.js b/packages/util-triehash/src/util/pairsUniq.spec.js index a7d33982f..26f9eedcb 100644 --- a/packages/util-triehash/src/util/pairsUniq.spec.js +++ b/packages/util-triehash/src/util/pairsUniq.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const pairsUniq = require('./pairsUniq'); +import pairsUniq from './pairsUniq'; describe('pairsUniq', () => { it('returns non-duplicated unchanged', () => { diff --git a/packages/util-triehash/src/util/sharedLength.js b/packages/util-triehash/src/util/sharedLength.js index fdde89679..aa868d073 100644 --- a/packages/util-triehash/src/util/sharedLength.js +++ b/packages/util-triehash/src/util/sharedLength.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -module.exports = function getSharedLength (a: Uint8Array, b: Uint8Array): number { +export default function getSharedLength (a: Uint8Array, b: Uint8Array): number { const count = Math.min(a.length, b.length); for (let index = 0; index < count; index++) { @@ -13,4 +13,4 @@ module.exports = function getSharedLength (a: Uint8Array, b: Uint8Array): number } return count; -}; +} diff --git a/packages/util-triehash/src/util/sharedPrefixLength.js b/packages/util-triehash/src/util/sharedPrefixLength.js index a9e9d5d00..8d294b4bc 100644 --- a/packages/util-triehash/src/util/sharedPrefixLength.js +++ b/packages/util-triehash/src/util/sharedPrefixLength.js @@ -5,9 +5,9 @@ import type { Trie$Pairs } from '../types'; -const getSharedLength = require('./sharedLength'); +import getSharedLength from './sharedLength'; -module.exports = function sharedPrefixLength (pairs: Trie$Pairs): number { +export default function sharedPrefixLength (pairs: Trie$Pairs): number { return pairs.reduce((length, { k }, index) => { if (index === 0) { return k.length; @@ -18,4 +18,4 @@ module.exports = function sharedPrefixLength (pairs: Trie$Pairs): number { length ); }, 0); -}; +} diff --git a/packages/util-triehash/src/util/sharedPrefixLength.spec.js b/packages/util-triehash/src/util/sharedPrefixLength.spec.js index 54f5eb786..6549446ec 100644 --- a/packages/util-triehash/src/util/sharedPrefixLength.spec.js +++ b/packages/util-triehash/src/util/sharedPrefixLength.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const sharedPrefixLength = require('./sharedPrefixLength'); +import sharedPrefixLength from './sharedPrefixLength'; describe('sharedPrefixLength', () => { it('returns 0 where no matches found', () => { diff --git a/packages/util/docs/README.md b/packages/util/docs/README.md index 3d5171a8f..79a0bab02 100644 --- a/packages/util/docs/README.md +++ b/packages/util/docs/README.md @@ -1,6 +1,6 @@ # Available interfaces -Utility methods for this package are split into groups +Utility methods for this package are split into groups - [array](array.md) Utility methods that operates on arrays - [bn](bn.md) Utility methods to convert to and from `BN` objects @@ -16,7 +16,7 @@ Utility methods for this package are split into groups ## assert -Checks for a valid test, if not ExtError is thrown. +Checks for a valid test, if not ExtError is thrown. ```js assert (test: mixed, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: mixed): void @@ -26,7 +26,7 @@ assert (test: mixed, message: string | () => string, code: number = ExtError.COD Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned. ```js -const assert = require('@polkadot/util/assert'); +const assert from '@polkadot/util/assert'); assert(true, 'True should be true'); // true returned assert(false, 'False should not be true'); // ExtError thrown @@ -35,7 +35,7 @@ assert(false, () => 'message'); // ExtError with 'message' ## Logger -Creates a consistent log interface for messages +Creates a consistent log interface for messages ```js logger (type: string): Logger @@ -45,14 +45,14 @@ logger (type: string): Logger Returns a `Logger` that has `.log`, `.error`, `.warn` and `.debug` (constrolled with environment `DEBUG=typeA,typeB`) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console. ```js -const l = require('@polkadot/util/logger')('test'); +const l from '@polkadot/util/logger')('test'); l.log('blah'); // TEST: blah ``` ## promisify -Wraps an async callback into a `Promise` +Wraps an async callback into a `Promise` ```js function promisify (this: ?Object, fn: Function, ...params: Array): Promise @@ -62,8 +62,8 @@ function promisify (this: ?Object, fn: Function, ...params: Array): Promi Wraps the supplied async function `fn` that has a standard JS callback `(error: Error, result: any)` into a `Promise`, passing the supplied parameters. When `error` is set, the Promise is rejected, else the Promise resolves with the `result` value. ```js -const promisify = require('@polkadot/util/promisify'); +const promisify from '@polkadot/util/promisify'); await promisify(null, ((a, cb) => cb(null, a), true); // resolves with `true` await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!` -``` \ No newline at end of file +``` diff --git a/packages/util/docs/ext.md b/packages/util/docs/ext.md index 071da1baa..01e18e05e 100644 --- a/packages/util/docs/ext.md +++ b/packages/util/docs/ext.md @@ -1,12 +1,12 @@ # ext -Extensions to basic classes +Extensions to basic classes - [ExtError](#exterror) Extension to the basic JS Error. ## ExtError -Extension to the basic JS Error. +Extension to the basic JS Error. ```js ExtError (message: string = '', code: number = UNKNOWN_ERROR, value: any) @@ -16,7 +16,7 @@ ExtError (message: string = '', code: number = UNKNOWN_ERROR, value: any) The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (mixed types) parameters are available on the object. ```js -const { ExtError } = require('@polkadot/util'); +const { ExtError } from '@polkadot/util'); throw new ExtError('some message', ExtError.CODES.METHOD_NOT_FOUND); // => error.code = -32601 -``` \ No newline at end of file +``` diff --git a/packages/util/package.json b/packages/util/package.json index 6f7f05f92..1f273d5df 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util", - "version": "0.22.11", + "version": "0.23.0", "description": "A collection of useful utilities for @polkadot", "main": "index.js", "keywords": [ diff --git a/packages/util/src/array/filter.js b/packages/util/src/array/filter.js index 4298ad4d8..aacbef5ee 100644 --- a/packages/util/src/array/filter.js +++ b/packages/util/src/array/filter.js @@ -3,8 +3,8 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isNull = require('../is/null'); -const isUndefined = require('../is/undefined'); +import isNull from '../is/null'; +import isUndefined from '../is/undefined'; /** @name arrayFilter @@ -18,8 +18,8 @@ const isUndefined = require('../is/undefined'); arrayFilter([0, void 0, true, null, false, '']); // [0, true, null, false, ''] arrayFilter([0, void 0, true, null, false, ''], false); // [0, true, false, ''] */ -module.exports = function arrayFilter (array: Array, allowNulls: boolean = true): Array { +export default function arrayFilter (array: Array, allowNulls: boolean = true): Array { return array.filter((value) => { return !isUndefined(value) && (allowNulls || !isNull(value)); }); -}; +} diff --git a/packages/util/src/array/filter.spec.js b/packages/util/src/array/filter.spec.js index afae8a51e..06407fac8 100644 --- a/packages/util/src/array/filter.spec.js +++ b/packages/util/src/array/filter.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { arrayFilter } = require('./index'); +import { arrayFilter } from './index'; describe('filterArray', () => { it('filters arrays, removing undefined', () => { diff --git a/packages/util/src/array/index.js b/packages/util/src/array/index.js index 983c103d5..9c98d091a 100644 --- a/packages/util/src/array/index.js +++ b/packages/util/src/array/index.js @@ -3,11 +3,11 @@ // of the ISC license. See the LICENSE file for details. // @flow -const arrayFilter = require('./filter'); +import arrayFilter from './filter'; /** @summary Utility methods that operates on arrays */ -module.exports = { +export { arrayFilter }; diff --git a/packages/util/src/assert.js b/packages/util/src/assert.js index 06d0e5c84..bea658f96 100644 --- a/packages/util/src/assert.js +++ b/packages/util/src/assert.js @@ -3,8 +3,8 @@ // of the ISC license. See the LICENSE file for details. // @flow -const ExtError = require('./ext/error'); -const isFunction = require('./is/function'); +import ExtError from './ext/error'; +import isFunction from './is/function'; /** @name assert @@ -13,13 +13,13 @@ const isFunction = require('./is/function'); @description Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned. @example - const assert = require('@polkadot/util/assert'); + const assert from '@polkadot/util/assert'); assert(true, 'True should be true'); // true returned assert(false, 'False should not be true'); // ExtError thrown assert(false, () => 'message'); // ExtError with 'message' */ -module.exports = function assert (test: mixed, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: mixed): boolean { +export default function assert (test: mixed, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: mixed): boolean { // flowlint-next-line sketchy-null:off if (test) { return true; @@ -32,4 +32,4 @@ module.exports = function assert (test: mixed, message: string | () => string, c // $FlowFixMe we have a string here throw new ExtError(message, code, data); -}; +} diff --git a/packages/util/src/assert.spec.js b/packages/util/src/assert.spec.js index 56fb2fa3f..47395676c 100644 --- a/packages/util/src/assert.spec.js +++ b/packages/util/src/assert.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { assert } = require('./index'); +import { assert } from './index'; describe('assert', () => { it('should not throw an error when test is true', () => { diff --git a/packages/util/src/bn/fromHex.js b/packages/util/src/bn/fromHex.js index 152b11d83..b6da9e803 100644 --- a/packages/util/src/bn/fromHex.js +++ b/packages/util/src/bn/fromHex.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const bnFromHex = require('../hex/toBn'); +import bnFromHex from '../hex/toBn'; /** @name bnFromHex @@ -11,4 +11,4 @@ const bnFromHex = require('../hex/toBn'); @summary Creates a BN.js bignumber object from a hex string. @alias hex/toBn */ -module.exports = bnFromHex; +export default bnFromHex; diff --git a/packages/util/src/bn/fromHex.spec.js b/packages/util/src/bn/fromHex.spec.js index a5759deec..74e9cb496 100644 --- a/packages/util/src/bn/fromHex.spec.js +++ b/packages/util/src/bn/fromHex.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { bnFromHex } = require('./index'); +import { bnFromHex } from './index'; describe('bnFromHex', () => { it('exists as a function', () => { diff --git a/packages/util/src/bn/index.js b/packages/util/src/bn/index.js index 5be48c819..b96c82422 100644 --- a/packages/util/src/bn/index.js +++ b/packages/util/src/bn/index.js @@ -3,15 +3,15 @@ // of the ISC license. See the LICENSE file for details. // @flow -const bnFromHex = require('./fromHex'); -const bnToBn = require('./toBn'); -const bnToHex = require('./toHex'); -const bnToU8a = require('./toU8a'); +import bnFromHex from './fromHex'; +import bnToBn from './toBn'; +import bnToHex from './toHex'; +import bnToU8a from './toU8a'; /** @summary Utility methods to convert to and from `BN` objects */ -module.exports = { +export { bnFromHex, bnToBn, bnToHex, diff --git a/packages/util/src/bn/toBn.js b/packages/util/src/bn/toBn.js index 6bda5c109..e87f3d3d8 100644 --- a/packages/util/src/bn/toBn.js +++ b/packages/util/src/bn/toBn.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const BN = require('bn.js'); +import BN from 'bn.js'; /** @name bnToBn @@ -18,7 +18,7 @@ const BN = require('bn.js'); bnToBn(0x1234); // => BN(0x1234) bnToBn(new BN(0x1234)); // => BN(0x1234) */ -module.exports = function bnToBn (value?: BN | number): BN { +export default function bnToBn (value?: BN | number): BN { // flowlint-next-line sketchy-null-number:off if (!value) { return new BN(0); @@ -28,4 +28,4 @@ module.exports = function bnToBn (value?: BN | number): BN { // $FlowFixMe type is determined ? value : new BN(value); -}; +} diff --git a/packages/util/src/bn/toBn.spec.js b/packages/util/src/bn/toBn.spec.js index 2b0bc03e5..e2cb90a17 100644 --- a/packages/util/src/bn/toBn.spec.js +++ b/packages/util/src/bn/toBn.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const BN = require('bn.js'); +import BN from 'bn.js'; -const { bnToBn } = require('./index'); +import { bnToBn } from './index'; describe('bnToBn', () => { it('converts null values to 0x00', () => { diff --git a/packages/util/src/bn/toHex.js b/packages/util/src/bn/toHex.js index 43b3729f4..ea1e10de4 100644 --- a/packages/util/src/bn/toHex.js +++ b/packages/util/src/bn/toHex.js @@ -3,10 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const BN = require('bn.js'); +import BN from 'bn.js'; -const hexFixLength = require('../hex/fixLength'); -const bnToBn = require('./toBn'); +import hexFixLength from '../hex/fixLength'; +import bnToBn from './toBn'; const ZERO_STR = '0x00'; @@ -22,11 +22,11 @@ const ZERO_STR = '0x00'; bnToHex(new BN(0x123456)); // => '0x123456' */ -module.exports = function bnToHex (value?: BN | number, bitLength: number = -1): string { +export default function bnToHex (value?: BN | number, bitLength: number = -1): string { // flowlint-next-line sketchy-null-number:off if (!value) { return ZERO_STR; } return hexFixLength(bnToBn(value).toString(16), bitLength, true); -}; +} diff --git a/packages/util/src/bn/toHex.spec.js b/packages/util/src/bn/toHex.spec.js index f2c64e8f5..d6e261d40 100644 --- a/packages/util/src/bn/toHex.spec.js +++ b/packages/util/src/bn/toHex.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const BN = require('bn.js'); +import BN from 'bn.js'; -const { bnToHex } = require('./index'); +import { bnToHex } from './index'; describe('bnToHex', () => { it('converts null values to 0x00', () => { diff --git a/packages/util/src/bn/toU8a.js b/packages/util/src/bn/toU8a.js index 2b4c6acd9..c4879ddc5 100644 --- a/packages/util/src/bn/toU8a.js +++ b/packages/util/src/bn/toU8a.js @@ -5,9 +5,9 @@ import type BN from 'bn.js'; -const hexToU8a = require('../hex/toU8a'); -const bnToBn = require('./toBn'); -const bnToHex = require('./toHex'); +import hexToU8a from '../hex/toU8a'; +import bnToBn from './toBn'; +import bnToHex from './toHex'; /** @name bnToU8a @@ -20,7 +20,7 @@ const bnToHex = require('./toHex'); bnToU8a(new BN(0x1234)); // => [0x12, 0x34] */ -module.exports = function bnToU8a (value?: BN | number, bitLength: number = -1, isLe: boolean = false): Uint8Array { +export default function bnToU8a (value?: BN | number, bitLength: number = -1, isLe: boolean = false): Uint8Array { const byteLength = Math.ceil(bitLength / 8); // flowlint-next-line sketchy-null-number:off @@ -44,4 +44,4 @@ module.exports = function bnToU8a (value?: BN | number, bitLength: number = -1, ); return output; -}; +} diff --git a/packages/util/src/bn/toU8a.spec.js b/packages/util/src/bn/toU8a.spec.js index 49f19bc9f..bdf0eca2a 100644 --- a/packages/util/src/bn/toU8a.spec.js +++ b/packages/util/src/bn/toU8a.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const BN = require('bn.js'); +import BN from 'bn.js'; -const { bnToU8a } = require('./index'); +import { bnToU8a } from './index'; describe('bnToU8a', () => { it('converts null values to 0x00', () => { diff --git a/packages/util/src/buffer/fromU8a.js b/packages/util/src/buffer/fromU8a.js index 02674a8ba..876e2501b 100644 --- a/packages/util/src/buffer/fromU8a.js +++ b/packages/util/src/buffer/fromU8a.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const bufferFromU8a = require('../u8a/toBuffer'); +import bufferFromU8a from '../u8a/toBuffer'; /** @name bufferFromU8a @@ -11,4 +11,4 @@ const bufferFromU8a = require('../u8a/toBuffer'); @summary Creates a Buffer object from a Uint8Array. @alias u8a/toBuffer */ -module.exports = bufferFromU8a; +export default bufferFromU8a; diff --git a/packages/util/src/buffer/fromU8a.spec.js b/packages/util/src/buffer/fromU8a.spec.js index 3a2e15477..afcc8ca00 100644 --- a/packages/util/src/buffer/fromU8a.spec.js +++ b/packages/util/src/buffer/fromU8a.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { bufferFromU8a } = require('./index'); +import { bufferFromU8a } from './index'; describe('bufferFromU8a', () => { it('exists as a function', () => { diff --git a/packages/util/src/buffer/index.js b/packages/util/src/buffer/index.js index b8c0349d8..e73a1a172 100644 --- a/packages/util/src/buffer/index.js +++ b/packages/util/src/buffer/index.js @@ -3,13 +3,13 @@ // of the ISC license. See the LICENSE file for details. // @flow -const bufferFromU8a = require('./fromU8a'); -const bufferToU8a = require('./toU8a'); +import bufferFromU8a from './fromU8a'; +import bufferToU8a from './toU8a'; /** @summary Utility methods to convert to and from `Buffer` objects */ -module.exports = { +export { bufferFromU8a, bufferToU8a }; diff --git a/packages/util/src/buffer/toU8a.js b/packages/util/src/buffer/toU8a.js index 732de07f6..063b141ca 100644 --- a/packages/util/src/buffer/toU8a.js +++ b/packages/util/src/buffer/toU8a.js @@ -14,10 +14,10 @@ bufferToU8a(Buffer.from([1, 2, 3])); */ -module.exports = function bufferToU8a (buffer?: Buffer): Uint8Array { +export default function bufferToU8a (buffer?: Buffer): Uint8Array { if (!buffer) { return new Uint8Array([]); } return new Uint8Array(buffer); -}; +} diff --git a/packages/util/src/buffer/toU8a.spec.js b/packages/util/src/buffer/toU8a.spec.js index 92a1841b5..4411da940 100644 --- a/packages/util/src/buffer/toU8a.spec.js +++ b/packages/util/src/buffer/toU8a.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { bufferToU8a } = require('./index'); +import { bufferToU8a } from './index'; describe('bufferToU8a', () => { it('returns an empty buffer when null provided', () => { diff --git a/packages/util/src/ext/error.js b/packages/util/src/ext/error.js index 28b2ec533..c8c1687a6 100644 --- a/packages/util/src/ext/error.js +++ b/packages/util/src/ext/error.js @@ -5,10 +5,19 @@ import type { ExtErrorInterface } from '../types'; -const isFunction = require('../is/function'); +import isFunction from '../is/function'; const UNKNOWN = -99999; +// flowlint-next-line unclear-type:off +function extend (that: any, name: string, value: mixed): void { + Object.defineProperty(that, name, { + configurable: true, + enumerable: false, + value + }); +} + /** @name ExtError @signature ExtError (message: string = '', code: number = UNKNOWN_ERROR, value: any) @@ -16,11 +25,11 @@ const UNKNOWN = -99999; @description The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (mixed types) parameters are available on the object. @example - const { ExtError } = require('@polkadot/util'); + const { ExtError } from '@polkadot/util'); throw new ExtError('some message', ExtError.CODES.METHOD_NOT_FOUND); // => error.code = -32601 */ -module.exports = class ExtError extends Error implements ExtErrorInterface { +export default class ExtError extends Error implements ExtErrorInterface { code: number; data: mixed; message: string; @@ -30,23 +39,15 @@ module.exports = class ExtError extends Error implements ExtErrorInterface { constructor (message: string = '', code: number = UNKNOWN, data: mixed) { super(); - const extend = (name: string, value: mixed): void => { - Object.defineProperty(this, name, { - configurable: true, - enumerable: false, - value - }); - }; - - extend('message', String(message)); - extend('name', this.constructor.name); - extend('data', data); - extend('code', code); + extend(this, 'message', String(message)); + extend(this, 'name', this.constructor.name); + extend(this, 'data', data); + extend(this, 'code', code); if (isFunction(Error.captureStackTrace)) { Error.captureStackTrace(this, this.constructor); } else { - extend('stack', (new Error(message)).stack); + extend(this, 'stack', (new Error(message)).stack); } } @@ -56,4 +57,4 @@ module.exports = class ExtError extends Error implements ExtErrorInterface { INVALID_JSONRPC: -99998, METHOD_NOT_FOUND: -32601 // Rust client }; -}; +} diff --git a/packages/util/src/ext/error.spec.js b/packages/util/src/ext/error.spec.js index bdfaad81e..cf2b351e5 100644 --- a/packages/util/src/ext/error.spec.js +++ b/packages/util/src/ext/error.spec.js @@ -2,8 +2,8 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isError = require('../is/error'); -const { ExtError } = require('./index'); +import isError from '../is/error'; +import { ExtError } from './index'; describe('ExtError', () => { describe('constructor', () => { diff --git a/packages/util/src/ext/index.js b/packages/util/src/ext/index.js index b233e47e1..8745e86a6 100644 --- a/packages/util/src/ext/index.js +++ b/packages/util/src/ext/index.js @@ -3,11 +3,11 @@ // of the ISC license. See the LICENSE file for details. // @flow -const ExtError = require('./error'); +import ExtError from './error'; /** @summary Extensions to basic classes */ -module.exports = { +export { ExtError }; diff --git a/packages/util/src/hex/addPrefix.js b/packages/util/src/hex/addPrefix.js index da5aa53af..e0f85626c 100644 --- a/packages/util/src/hex/addPrefix.js +++ b/packages/util/src/hex/addPrefix.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hasPrefix = require('./hasPrefix'); +import hasPrefix from './hasPrefix'; /** @name hexAddPrefix @@ -16,7 +16,7 @@ const hasPrefix = require('./hasPrefix'); console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12 */ -module.exports = function hexAddPrefix (value: ?string): string { +export default function hexAddPrefix (value: ?string): string { // flowlint-next-line sketchy-null-string:off if (value && hasPrefix(value)) { return value; @@ -28,4 +28,4 @@ module.exports = function hexAddPrefix (value: ?string): string { : ''; return `0x${prefix}${value || ''}`; -}; +} diff --git a/packages/util/src/hex/addPrefix.spec.js b/packages/util/src/hex/addPrefix.spec.js index 2c54c2da7..d607b9800 100644 --- a/packages/util/src/hex/addPrefix.spec.js +++ b/packages/util/src/hex/addPrefix.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { hexAddPrefix } = require('./index'); +import { hexAddPrefix } from './index'; describe('hexAddPrefix', () => { it('does not add when prefix is available', () => { diff --git a/packages/util/src/hex/fixLength.js b/packages/util/src/hex/fixLength.js index c1a8857e6..69b64d3a9 100644 --- a/packages/util/src/hex/fixLength.js +++ b/packages/util/src/hex/fixLength.js @@ -3,8 +3,8 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexAddPrefix = require('./addPrefix'); -const hexStripPrefix = require('./stripPrefix'); +import hexAddPrefix from './addPrefix'; +import hexStripPrefix from './stripPrefix'; /** @name hexFixLength @@ -19,7 +19,7 @@ const hexStripPrefix = require('./stripPrefix'); console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012 console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12 */ -module.exports = function hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string { +export default function hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string { const strLength = Math.ceil(bitLength / 4); const hexLength = strLength + 2; @@ -38,4 +38,4 @@ module.exports = function hexFixLength (value: string, bitLength: number = -1, w return hexAddPrefix( `${'0'.repeat(strLength)}${hexStripPrefix(value)}`.slice(-1 * strLength) ); -}; +} diff --git a/packages/util/src/hex/fixLength.spec.js b/packages/util/src/hex/fixLength.spec.js index 1c879b44c..fd3d9b7c7 100644 --- a/packages/util/src/hex/fixLength.spec.js +++ b/packages/util/src/hex/fixLength.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { hexFixLength } = require('./index'); +import { hexFixLength } from './index'; describe('hexFixLength', () => { it('returns bitLength === -1 as-is', () => { diff --git a/packages/util/src/hex/fromBn.js b/packages/util/src/hex/fromBn.js index cdf94be46..9665e9ec1 100644 --- a/packages/util/src/hex/fromBn.js +++ b/packages/util/src/hex/fromBn.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexFromBn = require('../bn/toHex'); +import hexFromBn from '../bn/toHex'; /** @name hexFromBn @@ -11,4 +11,4 @@ const hexFromBn = require('../bn/toHex'); @summary Creates a hex value from a BN.js bignumber object. @alias bn/toHex */ -module.exports = hexFromBn; +export default hexFromBn; diff --git a/packages/util/src/hex/fromBn.spec.js b/packages/util/src/hex/fromBn.spec.js index 3dd04438b..402ce03d3 100644 --- a/packages/util/src/hex/fromBn.spec.js +++ b/packages/util/src/hex/fromBn.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { hexFromBn } = require('./index'); +import { hexFromBn } from './index'; describe('hexFromBn', () => { it('exists as a function', () => { diff --git a/packages/util/src/hex/fromNumber.js b/packages/util/src/hex/fromNumber.js index 57ca1428d..6c27dfc1c 100644 --- a/packages/util/src/hex/fromNumber.js +++ b/packages/util/src/hex/fromNumber.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexFromNumber = require('../number/toHex'); +import hexFromNumber from '../number/toHex'; /** @name hexFromNumber @@ -11,4 +11,4 @@ const hexFromNumber = require('../number/toHex'); @summary Creates a hex value from a number. @alias number/toHex */ -module.exports = hexFromNumber; +export default hexFromNumber; diff --git a/packages/util/src/hex/fromNumber.spec.js b/packages/util/src/hex/fromNumber.spec.js index b7d263891..073338409 100644 --- a/packages/util/src/hex/fromNumber.spec.js +++ b/packages/util/src/hex/fromNumber.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { hexFromNumber } = require('./index'); +import { hexFromNumber } from './index'; describe('hexFromNumber', () => { it('exists as a function', () => { diff --git a/packages/util/src/hex/hasPrefix.js b/packages/util/src/hex/hasPrefix.js index 74c3283ec..ae063f4ce 100644 --- a/packages/util/src/hex/hasPrefix.js +++ b/packages/util/src/hex/hasPrefix.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isHex = require('../is/hex'); +import isHex from '../is/hex'; /** @name hexHasPrefix @@ -16,7 +16,7 @@ const isHex = require('../is/hex'); console.log('has prefix', hexHasPrefix('0x1234')); // => true */ -module.exports = function hexHasPrefix (value: ?string): boolean { +export default function hexHasPrefix (value: ?string): boolean { // flowlint-next-line sketchy-null-string:off return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x'); -}; +} diff --git a/packages/util/src/hex/hasPrefix.spec.js b/packages/util/src/hex/hasPrefix.spec.js index ddcb541a6..8bab73399 100644 --- a/packages/util/src/hex/hasPrefix.spec.js +++ b/packages/util/src/hex/hasPrefix.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { hexHasPrefix } = require('./index'); +import { hexHasPrefix } from './index'; describe('hexHasPrefix', () => { it('returns true when hex prefix is found', () => { diff --git a/packages/util/src/hex/index.js b/packages/util/src/hex/index.js index 543c938a9..d6452bc87 100644 --- a/packages/util/src/hex/index.js +++ b/packages/util/src/hex/index.js @@ -3,20 +3,20 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexAddPrefix = require('./addPrefix'); -const hexFixLength = require('./fixLength'); -const hexFromBn = require('./fromBn'); -const hexFromNumber = require('./fromNumber'); -const hexHasPrefix = require('./hasPrefix'); -const hexStripPrefix = require('./stripPrefix'); -const hexToBn = require('./toBn'); -const hexToNumber = require('./toNumber'); -const hexToU8a = require('./toU8a'); +import hexAddPrefix from './addPrefix'; +import hexFixLength from './fixLength'; +import hexFromBn from './fromBn'; +import hexFromNumber from './fromNumber'; +import hexHasPrefix from './hasPrefix'; +import hexStripPrefix from './stripPrefix'; +import hexToBn from './toBn'; +import hexToNumber from './toNumber'; +import hexToU8a from './toU8a'; /** @summary Internal utilities to create and test for hex values */ -module.exports = { +export { hexAddPrefix, hexFixLength, hexFromBn, diff --git a/packages/util/src/hex/stripPrefix.js b/packages/util/src/hex/stripPrefix.js index 6aba30078..86a3fe63c 100644 --- a/packages/util/src/hex/stripPrefix.js +++ b/packages/util/src/hex/stripPrefix.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hasPrefix = require('./hasPrefix'); +import hasPrefix from './hasPrefix'; const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/; @@ -18,7 +18,7 @@ const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/; console.log('stripped', hexStripPrefix('0x1234')); // => 1234 */ -module.exports = function hexStripPrefix (value: ?string): string { +export default function hexStripPrefix (value: ?string): string { // flowlint-next-line sketchy-null-string:off if (!value) { return ''; @@ -33,4 +33,4 @@ module.exports = function hexStripPrefix (value: ?string): string { } throw new Error(`Invalid hex ${value} passed to hexStripPrefix`); -}; +} diff --git a/packages/util/src/hex/stripPrefix.spec.js b/packages/util/src/hex/stripPrefix.spec.js index ff3a86426..e1b33b3f2 100644 --- a/packages/util/src/hex/stripPrefix.spec.js +++ b/packages/util/src/hex/stripPrefix.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { hexStripPrefix } = require('./index'); +import { hexStripPrefix } from './index'; describe('hexStripPrefix', () => { it('returns an empty string when null value supplied', () => { diff --git a/packages/util/src/hex/toBn.js b/packages/util/src/hex/toBn.js index 346d97a69..1054db541 100644 --- a/packages/util/src/hex/toBn.js +++ b/packages/util/src/hex/toBn.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const BN = require('bn.js'); +import BN from 'bn.js'; -const hexStripPrefix = require('./stripPrefix'); +import hexStripPrefix from './stripPrefix'; function reverse (value: string): string { return (value.match(new RegExp('.{1,2}', 'g')) || []) @@ -24,7 +24,7 @@ function reverse (value: string): string { hexToBn('0x123480001f'); // => BN(0x123480001f) */ -module.exports = function hexToBn (_value?: string, isLe: boolean = false): BN { +export default function hexToBn (_value?: string, isLe: boolean = false): BN { // flowlint-next-line sketchy-null-string:off if (!_value) { return new BN(0); @@ -33,4 +33,4 @@ module.exports = function hexToBn (_value?: string, isLe: boolean = false): BN { const value = hexStripPrefix(_value); return new BN((isLe ? reverse(value) : value) || '00', 16); -}; +} diff --git a/packages/util/src/hex/toBn.spec.js b/packages/util/src/hex/toBn.spec.js index 5f3725cc3..e9ce81bee 100644 --- a/packages/util/src/hex/toBn.spec.js +++ b/packages/util/src/hex/toBn.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { hexToBn } = require('./index'); +import { hexToBn } from './index'; describe('hexToBn', () => { it('converts prefixed hex values to BN', () => { diff --git a/packages/util/src/hex/toNumber.js b/packages/util/src/hex/toNumber.js index 07ca09f2f..44b38cd07 100644 --- a/packages/util/src/hex/toNumber.js +++ b/packages/util/src/hex/toNumber.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexToBn = require('./toBn'); +import hexToBn from './toBn'; /** @name hexToNumber @@ -16,11 +16,11 @@ const hexToBn = require('./toBn'); hexToNumber('0x1234'); // => 0x1234 */ -module.exports = function hexToNumber (value?: string): number { +export default function hexToNumber (value?: string): number { // flowlint-next-line sketchy-null-string:off if (!value) { return NaN; } return hexToBn(value).toNumber(); -}; +} diff --git a/packages/util/src/hex/toNumber.spec.js b/packages/util/src/hex/toNumber.spec.js index 3bde50c29..e54018fa5 100644 --- a/packages/util/src/hex/toNumber.spec.js +++ b/packages/util/src/hex/toNumber.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { hexToNumber } = require('./index'); +import { hexToNumber } from './index'; describe('hexToNumber', () => { it('converts an empty to NaN', () => { diff --git a/packages/util/src/hex/toU8a.js b/packages/util/src/hex/toU8a.js index 58fe45f40..c0e837cca 100644 --- a/packages/util/src/hex/toU8a.js +++ b/packages/util/src/hex/toU8a.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const assert = require('../assert'); -const isHex = require('../is/hex'); -const hexStripPrefix = require('./stripPrefix'); +import assert from '../assert'; +import isHex from '../is/hex'; +import hexStripPrefix from './stripPrefix'; /** @name hexToU8a @@ -19,7 +19,7 @@ const hexStripPrefix = require('./stripPrefix'); hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f]) hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f]) */ -module.exports = function hexToU8a (_value?: string | null, bitLength: number = -1): Uint8Array { +export default function hexToU8a (_value?: string | null, bitLength: number = -1): Uint8Array { // flowlint-next-line sketchy-null-string:off if (!_value) { return new Uint8Array([]); @@ -42,4 +42,4 @@ module.exports = function hexToU8a (_value?: string | null, bitLength: number = } return result; -}; +} diff --git a/packages/util/src/hex/toU8a.spec.js b/packages/util/src/hex/toU8a.spec.js index 4e5ac491b..d786f8fc1 100644 --- a/packages/util/src/hex/toU8a.spec.js +++ b/packages/util/src/hex/toU8a.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { hexToU8a } = require('./index'); +import { hexToU8a } from './index'; describe('hexToU8a', () => { it('returns an empty Uint8Array when null provided', () => { diff --git a/packages/util/src/index.js b/packages/util/src/index.js index 513cb4e98..68c466ac9 100644 --- a/packages/util/src/index.js +++ b/packages/util/src/index.js @@ -3,22 +3,25 @@ // of the ISC license. See the LICENSE file for details. // @flow -const array = require('./array'); -const assert = require('./assert'); -const bn = require('./bn'); -const buffer = require('./buffer'); -const ext = require('./ext'); -const hex = require('./hex'); -const is = require('./is'); -const logger = require('./logger'); -const number = require('./number'); -const promisify = require('./promisify'); -const string = require('./string'); -const u8a = require('./u8a'); +import assert from './assert'; +import logger from './logger'; +import promisify from './promisify'; + +export * from './array'; +export * from './bn'; +export * from './buffer'; +export * from './ext'; +export * from './hex'; +export * from './is'; +export * from './number'; +export * from './string'; +export * from './u8a'; /** @summary Utility methods for this package are split into groups */ -module.exports = Object.assign( - {}, array, { assert }, bn, buffer, ext, hex, is, { logger }, number, { promisify }, string, u8a -); +export { + assert, + logger, + promisify +}; diff --git a/packages/util/src/index.spec.js b/packages/util/src/index.spec.js index 1b2d7f36b..d536a7ab6 100644 --- a/packages/util/src/index.spec.js +++ b/packages/util/src/index.spec.js @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const util = require('./index'); +import { assert } from './index'; describe('index', () => { it('exports ok', () => { - expect(util).toBeDefined(); + expect(assert).toBeDefined(); }); }); diff --git a/packages/util/src/is/bn.js b/packages/util/src/is/bn.js index 4350c33ca..145b7ee44 100644 --- a/packages/util/src/is/bn.js +++ b/packages/util/src/is/bn.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const BN = require('bn.js'); +import BN from 'bn.js'; /** @name isBn @@ -17,6 +17,6 @@ const BN = require('bn.js'); console.log('isBn', isBn(new BN(1))); // => true */ -module.exports = function isBn (value: mixed): boolean { +export default function isBn (value: mixed): boolean { return BN.isBN(value); -}; +} diff --git a/packages/util/src/is/bn.spec.js b/packages/util/src/is/bn.spec.js index 094a87b1e..45151afff 100644 --- a/packages/util/src/is/bn.spec.js +++ b/packages/util/src/is/bn.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const BN = require('bn.js'); +import BN from 'bn.js'; -const { isBn } = require('./index'); +import { isBn } from './index'; describe('isBN', () => { it('returns true when a BN value', () => { diff --git a/packages/util/src/is/boolean.js b/packages/util/src/is/boolean.js index d01c1431b..d7bc32b4e 100644 --- a/packages/util/src/is/boolean.js +++ b/packages/util/src/is/boolean.js @@ -14,6 +14,6 @@ isBoolean(false); // => true */ -module.exports = function isBoolean (value: mixed): boolean { +export default function isBoolean (value: mixed): boolean { return typeof value === 'boolean'; -}; +} diff --git a/packages/util/src/is/boolean.spec.js b/packages/util/src/is/boolean.spec.js index 8b40dc24c..9b6af314f 100644 --- a/packages/util/src/is/boolean.spec.js +++ b/packages/util/src/is/boolean.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isBoolean } = require('./index'); +import { isBoolean } from './index'; describe('isBoolean', () => { it('returns true on false', () => { diff --git a/packages/util/src/is/buffer.js b/packages/util/src/is/buffer.js index 3db59ecfc..281a7254e 100644 --- a/packages/util/src/is/buffer.js +++ b/packages/util/src/is/buffer.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isInstanceOf = require('./instanceOf'); +import isInstanceOf from './instanceOf'; /** @name isBuffer @@ -16,6 +16,6 @@ const isInstanceOf = require('./instanceOf'); console.log('isBuffer', isBuffer(Buffer.from([]))); // => true */ -module.exports = function isBuffer (value: mixed): boolean { +export default function isBuffer (value: mixed): boolean { return isInstanceOf(value, Buffer); -}; +} diff --git a/packages/util/src/is/buffer.spec.js b/packages/util/src/is/buffer.spec.js index 527b6d06f..91b07c235 100644 --- a/packages/util/src/is/buffer.spec.js +++ b/packages/util/src/is/buffer.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isBuffer } = require('./index'); +import { isBuffer } from './index'; describe('isBuffer', () => { it('returns true when a Buffer value', () => { diff --git a/packages/util/src/is/error.js b/packages/util/src/is/error.js index a8bb78856..241148f22 100644 --- a/packages/util/src/is/error.js +++ b/packages/util/src/is/error.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isInstanceOf = require('./instanceOf'); +import isInstanceOf from './instanceOf'; /** @name isError @@ -16,6 +16,6 @@ const isInstanceOf = require('./instanceOf'); console.log('isError', isError(new Error('message'))); // => true */ -module.exports = function isError (value: mixed): boolean { +export default function isError (value: mixed): boolean { return isInstanceOf(value, Error); -}; +} diff --git a/packages/util/src/is/error.spec.js b/packages/util/src/is/error.spec.js index 65f4af318..78805c27d 100644 --- a/packages/util/src/is/error.spec.js +++ b/packages/util/src/is/error.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isError } = require('./index'); +import { isError } from './index'; describe('isError', () => { it('returns true when an Error value', () => { diff --git a/packages/util/src/is/function.js b/packages/util/src/is/function.js index 45059e14b..375099f95 100644 --- a/packages/util/src/is/function.js +++ b/packages/util/src/is/function.js @@ -14,6 +14,6 @@ console.log('isFunction', isFunction(() => false)); // => true */ -module.exports = function isFunction (value: mixed): boolean { +export default function isFunction (value: mixed): boolean { return typeof value === 'function'; -}; +} diff --git a/packages/util/src/is/function.spec.js b/packages/util/src/is/function.spec.js index 2e945db2a..3e62da1a6 100644 --- a/packages/util/src/is/function.spec.js +++ b/packages/util/src/is/function.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isFunction } = require('./index'); +import { isFunction } from './index'; describe('isFunction', () => { it('returns true on valid functions', () => { diff --git a/packages/util/src/is/hex.js b/packages/util/src/is/hex.js index 3681b6249..7f89188ee 100644 --- a/packages/util/src/is/hex.js +++ b/packages/util/src/is/hex.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isString = require('./string'); +import isString from './string'; const HEX_REGEX = /^0x[a-fA-F0-9]+$/; @@ -19,9 +19,9 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/; isHex('0x1234'); // => true isHex('0x1234', 8); // => false */ -module.exports = function isHex (_value: mixed, bitLength: number = -1, ignoreLength: boolean = false): boolean { +export default function isHex (_value: mixed, bitLength: number = -1, ignoreLength: boolean = false): boolean { // flowlint-next-line unclear-type:off - const value = ((_value: any): string); + const value: string = (_value: any); const isValidHex = value === '0x' || (isString(value) && HEX_REGEX.test(value)); if (isValidHex && bitLength !== -1) { @@ -29,4 +29,4 @@ module.exports = function isHex (_value: mixed, bitLength: number = -1, ignoreLe } return isValidHex && (ignoreLength || (value.length % 2 === 0)); -}; +} diff --git a/packages/util/src/is/hex.spec.js b/packages/util/src/is/hex.spec.js index 573f009a8..b476ef860 100644 --- a/packages/util/src/is/hex.spec.js +++ b/packages/util/src/is/hex.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isHex } = require('./index'); +import { isHex } from './index'; describe('isHex', () => { const test = '1234abcd'; diff --git a/packages/util/src/is/index.js b/packages/util/src/is/index.js index 6d75afa6c..c3fa5f9af 100644 --- a/packages/util/src/is/index.js +++ b/packages/util/src/is/index.js @@ -3,26 +3,26 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isBn = require('./bn'); -const isBuffer = require('./buffer'); -const isBoolean = require('./boolean'); -const isError = require('./error'); -const isFunction = require('./function'); -const isHex = require('./hex'); -const isInstanceOf = require('./instanceOf'); -const isIp = require('./ip'); -const isNull = require('./null'); -const isNumber = require('./number'); -const isObject = require('./object'); -const isObservable = require('./observable'); -const isString = require('./string'); -const isU8a = require('./u8a'); -const isUndefined = require('./undefined'); +import isBn from './bn'; +import isBuffer from './buffer'; +import isBoolean from './boolean'; +import isError from './error'; +import isFunction from './function'; +import isHex from './hex'; +import isInstanceOf from './instanceOf'; +import isIp from './ip'; +import isNull from './null'; +import isNumber from './number'; +import isObject from './object'; +import isObservable from './observable'; +import isString from './string'; +import isU8a from './u8a'; +import isUndefined from './undefined'; /** @summary Type checking utilities */ -module.exports = { +export { isBn, isBoolean, isBuffer, diff --git a/packages/util/src/is/instanceOf.js b/packages/util/src/is/instanceOf.js index 39c32a882..f397bb9c9 100644 --- a/packages/util/src/is/instanceOf.js +++ b/packages/util/src/is/instanceOf.js @@ -14,6 +14,6 @@ console.log('isInstanceOf', isInstanceOf(new Array(0), Array)); // => true */ -module.exports = function isInstanceOf (value: mixed, clazz: Class): boolean { +export default function isInstanceOf (value: mixed, clazz: Class): boolean { return value instanceof clazz; -}; +} diff --git a/packages/util/src/is/instanceOf.spec.js b/packages/util/src/is/instanceOf.spec.js index 3a59cc210..84ea0f1ff 100644 --- a/packages/util/src/is/instanceOf.spec.js +++ b/packages/util/src/is/instanceOf.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isInstanceOf } = require('./index'); +import { isInstanceOf } from './index'; describe('isInstanceOf', () => { it('returns true on real instances', () => { diff --git a/packages/util/src/is/ip.js b/packages/util/src/is/ip.js index cf8c01447..c652d4ac2 100644 --- a/packages/util/src/is/ip.js +++ b/packages/util/src/is/ip.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -type IpTypes = 'v4' | 'v6'; +import ipRegex from 'ip-regex'; -const ipRegex = require('ip-regex'); +type IpTypes = 'v4' | 'v6'; /** @name isIp @@ -21,7 +21,7 @@ const ipRegex = require('ip-regex'); isIp('192.168.0.1', 'v6')); // => false isIp('1:2:3:4:5:6:7:8', 'v4'); // => false */ -module.exports = function isIp (value: string, type?: IpTypes): boolean { +export default function isIp (value: string, type?: IpTypes): boolean { if (type === 'v4') { return ipRegex.v4({ exact: true }).test(value); } else if (type === 'v6') { @@ -29,4 +29,4 @@ module.exports = function isIp (value: string, type?: IpTypes): boolean { } return ipRegex({ exact: true }).test(value); -}; +} diff --git a/packages/util/src/is/ip.spec.js b/packages/util/src/is/ip.spec.js index 1f0a2e8a8..a83c1452f 100644 --- a/packages/util/src/is/ip.spec.js +++ b/packages/util/src/is/ip.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isIp } = require('./index'); +import { isIp } from './index'; describe('isIp', () => { it('returns false when non-ip found', () => { diff --git a/packages/util/src/is/null.js b/packages/util/src/is/null.js index 0d000ddd3..5289d49a8 100644 --- a/packages/util/src/is/null.js +++ b/packages/util/src/is/null.js @@ -14,6 +14,6 @@ console.log('isNull', isNull(null)); // => true */ -module.exports = function isNull (value: mixed): boolean { +export default function isNull (value: mixed): boolean { return value === null; -}; +} diff --git a/packages/util/src/is/null.spec.js b/packages/util/src/is/null.spec.js index 86cd2e5bb..7baf20cf8 100644 --- a/packages/util/src/is/null.spec.js +++ b/packages/util/src/is/null.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isNull } = require('./index'); +import { isNull } from './index'; describe('isNull', () => { it('returns true when a null value', () => { diff --git a/packages/util/src/is/number.js b/packages/util/src/is/number.js index 35da157f5..8a3a97aef 100644 --- a/packages/util/src/is/number.js +++ b/packages/util/src/is/number.js @@ -14,6 +14,6 @@ console.log('isNumber', isNumber(1234)); // => true */ -module.exports = function isNumber (value: mixed): boolean { +export default function isNumber (value: mixed): boolean { return typeof value === 'number'; -}; +} diff --git a/packages/util/src/is/number.spec.js b/packages/util/src/is/number.spec.js index d19d01545..df38a589b 100644 --- a/packages/util/src/is/number.spec.js +++ b/packages/util/src/is/number.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isNumber } = require('./index'); +import { isNumber } from './index'; describe('isNumber', () => { it('returns true on valid numbers', () => { diff --git a/packages/util/src/is/object.js b/packages/util/src/is/object.js index 9b5126f46..127ee75a5 100644 --- a/packages/util/src/is/object.js +++ b/packages/util/src/is/object.js @@ -15,6 +15,6 @@ isObject({}); // => true isObject('something'); // => false */ -module.exports = function isObject (value: mixed): boolean { +export default function isObject (value: mixed): boolean { return typeof value === 'object'; -}; +} diff --git a/packages/util/src/is/object.spec.js b/packages/util/src/is/object.spec.js index 341cf97af..9424989a8 100644 --- a/packages/util/src/is/object.spec.js +++ b/packages/util/src/is/object.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isObject } = require('./index'); +import { isObject } from './index'; describe('isObject', () => { it('returns true on valid objects', () => { diff --git a/packages/util/src/is/observable.js b/packages/util/src/is/observable.js index 32000bc12..502b96c04 100644 --- a/packages/util/src/is/observable.js +++ b/packages/util/src/is/observable.js @@ -3,8 +3,8 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isFunction = require('./function'); -const isObject = require('./object'); +import isFunction from './function'; +import isObject from './object'; /** @name isBObservable @@ -17,7 +17,7 @@ const isObject = require('./object'); console.log('isObservable', isObservable(...)); */ -module.exports = function isObservable (value: mixed): boolean { +export default function isObservable (value: mixed): boolean { // $FlowFixMe check for possibly non-existent .next return isObject(value) && isFunction(value.next); -}; +} diff --git a/packages/util/src/is/observable.spec.js b/packages/util/src/is/observable.spec.js index b2cf0e4de..cb7ed9ab2 100644 --- a/packages/util/src/is/observable.spec.js +++ b/packages/util/src/is/observable.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isObservable } = require('./index'); +import { isObservable } from './index'; describe('isObservable', () => { it('returns true on valid observables', () => { diff --git a/packages/util/src/is/string.js b/packages/util/src/is/string.js index 0a2afc8d4..1a0bdec50 100644 --- a/packages/util/src/is/string.js +++ b/packages/util/src/is/string.js @@ -14,6 +14,6 @@ console.log('isString', isString('test')); // => true */ -module.exports = function isString (value: mixed): boolean { +export default function isString (value: mixed): boolean { return typeof value === 'string'; -}; +} diff --git a/packages/util/src/is/string.spec.js b/packages/util/src/is/string.spec.js index f4e484dbc..fd4d3a1d5 100644 --- a/packages/util/src/is/string.spec.js +++ b/packages/util/src/is/string.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isString } = require('./index'); +import { isString } from './index'; describe('isString', () => { it('returns true on valid strings', () => { diff --git a/packages/util/src/is/u8a.js b/packages/util/src/is/u8a.js index d6ecd20d4..1a41944db 100644 --- a/packages/util/src/is/u8a.js +++ b/packages/util/src/is/u8a.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isInstanceOf = require('./instanceOf'); +import isInstanceOf from './instanceOf'; /** @name isU8a @@ -16,6 +16,6 @@ const isInstanceOf = require('./instanceOf'); console.log('isU8a', isU8a([])); // => false */ -module.exports = function isU8a (value: mixed): boolean { +export default function isU8a (value: mixed): boolean { return isInstanceOf(value, Uint8Array); -}; +} diff --git a/packages/util/src/is/u8a.spec.js b/packages/util/src/is/u8a.spec.js index d9be1db10..d3c33940e 100644 --- a/packages/util/src/is/u8a.spec.js +++ b/packages/util/src/is/u8a.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isU8a } = require('./index'); +import { isU8a } from './index'; describe('isUint8Array', () => { it('returns false on undefined values', () => { diff --git a/packages/util/src/is/undefined.js b/packages/util/src/is/undefined.js index c84209bf5..2b94587a4 100644 --- a/packages/util/src/is/undefined.js +++ b/packages/util/src/is/undefined.js @@ -14,6 +14,6 @@ console.log('isUndefined', isUndefined(void(0))); // => true */ -module.exports = function isUndefined (value: mixed): boolean { +export default function isUndefined (value: mixed): boolean { return typeof value === 'undefined'; -}; +} diff --git a/packages/util/src/is/undefined.spec.js b/packages/util/src/is/undefined.spec.js index 6104b9889..383fbbede 100644 --- a/packages/util/src/is/undefined.spec.js +++ b/packages/util/src/is/undefined.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { isUndefined } = require('./index'); +import { isUndefined } from './index'; describe('isUndefined', () => { it('returns true on undefined values', () => { diff --git a/packages/util/src/logger.js b/packages/util/src/logger.js index 64f1cf795..703023769 100644 --- a/packages/util/src/logger.js +++ b/packages/util/src/logger.js @@ -5,13 +5,13 @@ import type { Logger, Logger$Data } from './types'; +import chalk from 'chalk'; + +import isFunction from './is/function'; + type ConsoleType = 'error' | 'log' | 'warn'; type LogType = ConsoleType | 'debug'; -const chalk = require('chalk'); - -const isFunction = require('./is/function'); - const logTo: { [LogType]: ConsoleType } = { debug: 'log', error: 'error', @@ -49,11 +49,11 @@ function apply (log: LogType, type: string, values: Logger$Data): void { @description Returns a `Logger` that has `.log`, `.error`, `.warn` and `.debug` (constrolled with environment `DEBUG=typeA,typeB`) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console. @example - const l = require('@polkadot/util/logger')('test'); + const l from '@polkadot/util/logger')('test'); l.log('blah'); // TEST: blah */ -module.exports = function logger (_type: string): Logger { +export default function logger (_type: string): Logger { const type = ` ${_type.toUpperCase()}:`.slice(-16); let isDebug; @@ -74,4 +74,4 @@ module.exports = function logger (_type: string): Logger { log: (...values: Logger$Data): void => apply('log', type, values), warn: (...values: Logger$Data): void => apply('warn', type, values) }; -}; +} diff --git a/packages/util/src/logger.spec.js b/packages/util/src/logger.spec.js index e2260039d..89fe086e6 100644 --- a/packages/util/src/logger.spec.js +++ b/packages/util/src/logger.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { logger } = require('./index'); +import { logger } from './index'; describe('logger', () => { let dateMatch; diff --git a/packages/util/src/number/fromHex.js b/packages/util/src/number/fromHex.js index 7176b1905..7b5dd3aa2 100644 --- a/packages/util/src/number/fromHex.js +++ b/packages/util/src/number/fromHex.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const numberFromHex = require('../hex/toNumber'); +import numberFromHex from '../hex/toNumber'; /** @name numberFromHex @@ -11,4 +11,4 @@ const numberFromHex = require('../hex/toNumber'); @summary Creates a Number value from a Buffer object. @alias hex/toNumber */ -module.exports = numberFromHex; +export default numberFromHex; diff --git a/packages/util/src/number/fromHex.spec.js b/packages/util/src/number/fromHex.spec.js index 3ddf069f5..8e2616bc0 100644 --- a/packages/util/src/number/fromHex.spec.js +++ b/packages/util/src/number/fromHex.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { numberFromHex } = require('./index'); +import { numberFromHex } from './index'; describe('numberFromHex', () => { it('exists as a function', () => { diff --git a/packages/util/src/number/index.js b/packages/util/src/number/index.js index 178e0732e..a88c0b0e1 100644 --- a/packages/util/src/number/index.js +++ b/packages/util/src/number/index.js @@ -3,14 +3,14 @@ // of the ISC license. See the LICENSE file for details. // @flow -const numberFromHex = require('./fromHex'); -const numberToHex = require('./toHex'); -const numberToU8a = require('./toU8a'); +import numberFromHex from './fromHex'; +import numberToHex from './toHex'; +import numberToU8a from './toU8a'; /** @summary Utility methods to convert to and from `number` values */ -module.exports = { +export { numberFromHex, numberToHex, numberToU8a diff --git a/packages/util/src/number/toHex.js b/packages/util/src/number/toHex.js index 7f2e5b04f..8643f13d7 100644 --- a/packages/util/src/number/toHex.js +++ b/packages/util/src/number/toHex.js @@ -3,9 +3,9 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexFixLength = require('../hex/fixLength'); -const isNull = require('../is/null'); -const isUndefined = require('../is/undefined'); +import hexFixLength from '../hex/fixLength'; +import isNull from '../is/null'; +import isUndefined from '../is/undefined'; /** @name numberToHex @@ -19,7 +19,7 @@ const isUndefined = require('../is/undefined'); numberToHex(0x1234); // => '0x1234' numberToHex(0x1234, 32) // => 0x00001234 */ -module.exports = function numberToHex (value?: number, bitLength: number = -1): string { +export default function numberToHex (value?: number, bitLength: number = -1): string { if (isUndefined(value) || isNull(value) || isNaN(value)) { return '0x'; } @@ -29,4 +29,4 @@ module.exports = function numberToHex (value?: number, bitLength: number = -1): bitLength, true ); -}; +} diff --git a/packages/util/src/number/toHex.spec.js b/packages/util/src/number/toHex.spec.js index f22fc3828..49164215c 100644 --- a/packages/util/src/number/toHex.spec.js +++ b/packages/util/src/number/toHex.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { numberToHex } = require('./index'); +import { numberToHex } from './index'; describe('numberToHex', () => { it('converts undefined to 0x', () => { diff --git a/packages/util/src/number/toU8a.js b/packages/util/src/number/toU8a.js index 18babf780..e2716df9b 100644 --- a/packages/util/src/number/toU8a.js +++ b/packages/util/src/number/toU8a.js @@ -3,8 +3,8 @@ // of the ISC license. See the LICENSE file for details. // @flow -const hexToU8a = require('../hex/toU8a'); -const numberToHex = require('./toHex'); +import hexToU8a from '../hex/toU8a'; +import numberToHex from './toHex'; /** @name numberToU8a @@ -17,7 +17,7 @@ const numberToHex = require('./toHex'); numberToU8a(0x1234); // => [0x12, 0x34] */ -module.exports = function numberToU8a (value?: number, bitLength: number = -1): Uint8Array { +export default function numberToU8a (value?: number, bitLength: number = -1): Uint8Array { // flowlint-next-line sketchy-null-number:off if (!value || isNaN(value)) { return new Uint8Array([]); @@ -26,4 +26,4 @@ module.exports = function numberToU8a (value?: number, bitLength: number = -1): return hexToU8a( numberToHex(value, bitLength) ); -}; +} diff --git a/packages/util/src/number/toU8a.spec.js b/packages/util/src/number/toU8a.spec.js index a6ed42d61..0751d9b59 100644 --- a/packages/util/src/number/toU8a.spec.js +++ b/packages/util/src/number/toU8a.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { numberToU8a } = require('./index'); +import { numberToU8a } from './index'; describe('numberToU8a', () => { it('converts 0 to empty', () => { diff --git a/packages/util/src/promisify.js b/packages/util/src/promisify.js index 50d21386f..34d25c9ab 100644 --- a/packages/util/src/promisify.js +++ b/packages/util/src/promisify.js @@ -10,13 +10,13 @@ @description Wraps the supplied async function `fn` that has a standard JS callback `(error: Error, result: any)` into a `Promise`, passing the supplied parameters. When `error` is set, the Promise is rejected, else the Promise resolves with the `result` value. @example - const promisify = require('@polkadot/util/promisify'); + const promisify from '@polkadot/util/promisify'); await promisify(null, ((a, cb) => cb(null, a), true); // resolves with `true` await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!` */ // flowlint-next-line unclear-type:off -module.exports = function promisify (self: ?Object, fn: Function, ...params: Array): Promise { +export default function promisify (self: ?Object, fn: Function, ...params: Array): Promise { return new Promise((resolve, reject) => { fn.apply(self, params.concat([ // flowlint-next-line unclear-type:off @@ -29,4 +29,4 @@ module.exports = function promisify (self: ?Object, fn: Function, ...params: Arr } ])); }); -}; +} diff --git a/packages/util/src/promisify.spec.js b/packages/util/src/promisify.spec.js index 2181d4866..ae74da477 100644 --- a/packages/util/src/promisify.spec.js +++ b/packages/util/src/promisify.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { promisify } = require('./index'); +import { promisify } from './index'; describe('promisify', () => { it('handles functions with no parameters (resolve)', () => { diff --git a/packages/util/src/string/index.js b/packages/util/src/string/index.js index d44d55f5b..f6d08bee7 100644 --- a/packages/util/src/string/index.js +++ b/packages/util/src/string/index.js @@ -3,11 +3,11 @@ // of the ISC license. See the LICENSE file for details. // @flow -const stringShorten = require('./shorten'); +import stringShorten from './shorten'; /** @summary Utility methods to convert to work with `string` values */ -module.exports = { +export { stringShorten }; diff --git a/packages/util/src/string/shorten.js b/packages/util/src/string/shorten.js index ba2634972..be94bc97b 100644 --- a/packages/util/src/string/shorten.js +++ b/packages/util/src/string/shorten.js @@ -14,7 +14,7 @@ stringShorten('1234567890', 2); // => 12..90 */ -module.exports = function stringShorten (_value: mixed, prefixLength: number = 6): string { +export default function stringShorten (_value: mixed, prefixLength: number = 6): string { // $FlowFixMe coerced into string (by any means) const value = `${_value}`; @@ -23,4 +23,4 @@ module.exports = function stringShorten (_value: mixed, prefixLength: number = 6 } return `${value.substr(0, prefixLength)}..${value.slice(-prefixLength)}`; -}; +} diff --git a/packages/util/src/string/shorten.spec.js b/packages/util/src/string/shorten.spec.js index c7e0ae0df..30e2884d8 100644 --- a/packages/util/src/string/shorten.spec.js +++ b/packages/util/src/string/shorten.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { stringShorten } = require('./index'); +import { stringShorten } from './index'; describe('stringShorten', () => { it('returns the value as-is when <= maxLength', () => { diff --git a/packages/util/src/syncify.js b/packages/util/src/syncify.js index 74183e46c..276f9879c 100644 --- a/packages/util/src/syncify.js +++ b/packages/util/src/syncify.js @@ -3,15 +3,15 @@ // of the ISC license. See the LICENSE file for details. // @flow -const deasync = require('deasync'); +import deasync from 'deasync'; -const isError = require('./is/error'); -const isUndefined = require('./is/undefined'); +import isError from './is/error'; +import isUndefined from './is/undefined'; // FIXME: This is currently for Node environments only - for Browser environments deasync is not available, we need a different solution. // flowlint-next-line unclear-type:off -module.exports = function syncify (promise: Promise): any { +export default function syncify (promise: Promise): any { let result; (async () => { @@ -27,4 +27,4 @@ module.exports = function syncify (promise: Promise): any { } return result; -}; +} diff --git a/packages/util/src/syncify.spec.js b/packages/util/src/syncify.spec.js index 1091edd41..0ece2989b 100644 --- a/packages/util/src/syncify.spec.js +++ b/packages/util/src/syncify.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const syncify = require('./syncify'); +import syncify from './syncify'; describe('syncify', () => { it('returns the result of the promise', () => { diff --git a/packages/util/src/u8a/concat.js b/packages/util/src/u8a/concat.js index 5ba6da8bf..8b463db01 100644 --- a/packages/util/src/u8a/concat.js +++ b/packages/util/src/u8a/concat.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aToU8a = require('./toU8a'); +import u8aToU8a from './toU8a'; /** @name u8aConcat @@ -19,7 +19,7 @@ const u8aToU8a = require('./toU8a'); new Uint8Array([4, 5, 6]) ); // [1, 2, 3, 4, 5, 6] */ -module.exports = function u8aConcat (..._list: Array): Uint8Array { +export default function u8aConcat (..._list: Array): Uint8Array { const list: Array = _list.map(u8aToU8a); const length = list.reduce((total, item) => total + item.length, 0); const result = new Uint8Array(length); @@ -31,4 +31,4 @@ module.exports = function u8aConcat (..._list: Array): Uint return result; }, result); -}; +} diff --git a/packages/util/src/u8a/concat.spec.js b/packages/util/src/u8a/concat.spec.js index 3bea1325c..38ebb0df6 100644 --- a/packages/util/src/u8a/concat.spec.js +++ b/packages/util/src/u8a/concat.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aConcat } = require('./index'); +import { u8aConcat } from './index'; describe('u8aConcat', () => { it('concatenates arrays', () => { diff --git a/packages/util/src/u8a/fixLength.js b/packages/util/src/u8a/fixLength.js index c3f0859bd..cd34e1ac0 100644 --- a/packages/util/src/u8a/fixLength.js +++ b/packages/util/src/u8a/fixLength.js @@ -16,7 +16,7 @@ u8aFixLength('0x12', 16) // => 0x0012 u8aFixLength('0x1234', 8) // => 0x12 */ -module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -1, atStart?: boolean = false): Uint8Array { +export default function u8aFixLength (value: Uint8Array, bitLength: number = -1, atStart?: boolean = false): Uint8Array { const byteLength = Math.ceil(bitLength / 8); if (bitLength === -1 || value.length === byteLength) { @@ -36,4 +36,4 @@ module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = - } return result; -}; +} diff --git a/packages/util/src/u8a/fixLength.spec.js b/packages/util/src/u8a/fixLength.spec.js index df61b7a24..b5654da5a 100644 --- a/packages/util/src/u8a/fixLength.spec.js +++ b/packages/util/src/u8a/fixLength.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aFixLength } = require('./index'); +import { u8aFixLength } from './index'; describe('u8aFixLength', () => { it('returns bitLength === -1 as-is', () => { diff --git a/packages/util/src/u8a/fromBn.js b/packages/util/src/u8a/fromBn.js index aa2d65f8c..cffb747e3 100644 --- a/packages/util/src/u8a/fromBn.js +++ b/packages/util/src/u8a/fromBn.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aFromBn = require('../bn/toU8a'); +import u8aFromBn from '../bn/toU8a'; /** @name u8aFromBn @@ -11,4 +11,4 @@ const u8aFromBn = require('../bn/toU8a'); @summary Creates a Uint8Array value from a BN object. @alias bn/toU8a */ -module.exports = u8aFromBn; +export default u8aFromBn; diff --git a/packages/util/src/u8a/fromBn.spec.js b/packages/util/src/u8a/fromBn.spec.js index 3dc866465..f722b14bb 100644 --- a/packages/util/src/u8a/fromBn.spec.js +++ b/packages/util/src/u8a/fromBn.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { u8aFromBn } = require('./index'); +import { u8aFromBn } from './index'; describe('u8aFromBn', () => { it('exists as a function', () => { diff --git a/packages/util/src/u8a/fromBuffer.js b/packages/util/src/u8a/fromBuffer.js index 35a219fbf..c4f0f1e3e 100644 --- a/packages/util/src/u8a/fromBuffer.js +++ b/packages/util/src/u8a/fromBuffer.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aFromBuffer = require('../buffer/toU8a'); +import u8aFromBuffer from '../buffer/toU8a'; /** @name u8aFromBuffer @@ -11,4 +11,4 @@ const u8aFromBuffer = require('../buffer/toU8a'); @summary Creates a Uint8Array value from a Buffer object. @alias buffer/toU8a */ -module.exports = u8aFromBuffer; +export default u8aFromBuffer; diff --git a/packages/util/src/u8a/fromBuffer.spec.js b/packages/util/src/u8a/fromBuffer.spec.js index 192fcfe29..0cf41a5e5 100644 --- a/packages/util/src/u8a/fromBuffer.spec.js +++ b/packages/util/src/u8a/fromBuffer.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { u8aFromBuffer } = require('./index'); +import { u8aFromBuffer } from './index'; describe('u8aFromBuffer', () => { it('exists as a function', () => { diff --git a/packages/util/src/u8a/fromHex.js b/packages/util/src/u8a/fromHex.js index 6a90ef286..c1ffc02a6 100644 --- a/packages/util/src/u8a/fromHex.js +++ b/packages/util/src/u8a/fromHex.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aFromHex = require('../hex/toU8a'); +import u8aFromHex from '../hex/toU8a'; /** @name u8aFromHex @@ -11,4 +11,4 @@ const u8aFromHex = require('../hex/toU8a'); @summary Creates a Uint8Array value from a hex string. @alias hex/toU8a */ -module.exports = u8aFromHex; +export default u8aFromHex; diff --git a/packages/util/src/u8a/fromHex.spec.js b/packages/util/src/u8a/fromHex.spec.js index e716c6b48..1103e199e 100644 --- a/packages/util/src/u8a/fromHex.spec.js +++ b/packages/util/src/u8a/fromHex.spec.js @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const isFunction = require('../is/function'); +import isFunction from '../is/function'; -const { u8aFromHex } = require('./index'); +import { u8aFromHex } from './index'; describe('u8aFromHex', () => { it('exists as a function', () => { diff --git a/packages/util/src/u8a/fromString.js b/packages/util/src/u8a/fromString.js index 4c3893106..483ce1167 100644 --- a/packages/util/src/u8a/fromString.js +++ b/packages/util/src/u8a/fromString.js @@ -14,7 +14,7 @@ u8aFromString('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f] */ -module.exports = function u8aFromString (value: string): Uint8Array { +export default function u8aFromString (value: string): Uint8Array { const stringArray = []; for (let index = 0; index < value.length; index++) { @@ -24,4 +24,4 @@ module.exports = function u8aFromString (value: string): Uint8Array { } return new Uint8Array(stringArray); -}; +} diff --git a/packages/util/src/u8a/fromString.spec.js b/packages/util/src/u8a/fromString.spec.js index 9a9719585..5bef4ffaa 100644 --- a/packages/util/src/u8a/fromString.spec.js +++ b/packages/util/src/u8a/fromString.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aFromString } = require('./index'); +import { u8aFromString } from './index'; describe('u8aFromString', () => { it('encodes a string byte for byte', () => { diff --git a/packages/util/src/u8a/fromUtf8.js b/packages/util/src/u8a/fromUtf8.js index 9feee0ad2..980884e5b 100644 --- a/packages/util/src/u8a/fromUtf8.js +++ b/packages/util/src/u8a/fromUtf8.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -require('./polyfill/textEncoder'); +import './polyfill/textEncoder'; const encoder = new TextEncoder(); @@ -18,11 +18,11 @@ const encoder = new TextEncoder(); u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f] */ -module.exports = function u8aFromUtf8 (value?: string): Uint8Array { +export default function u8aFromUtf8 (value?: string): Uint8Array { // flowlint-next-line sketchy-null-string:off if (!value) { return new Uint8Array([]); } return encoder.encode(value); -}; +} diff --git a/packages/util/src/u8a/fromUtf8.spec.js b/packages/util/src/u8a/fromUtf8.spec.js index 0c9200c7d..80bc599ad 100644 --- a/packages/util/src/u8a/fromUtf8.spec.js +++ b/packages/util/src/u8a/fromUtf8.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aFromUtf8 } = require('./index'); +import { u8aFromUtf8 } from './index'; describe('u8aFromUtf8', () => { it('decodes to an empty string for undefined', () => { diff --git a/packages/util/src/u8a/index.js b/packages/util/src/u8a/index.js index d708ff0d0..88bc8003c 100644 --- a/packages/util/src/u8a/index.js +++ b/packages/util/src/u8a/index.js @@ -3,24 +3,24 @@ // of the ISC license. See the LICENSE file for details. // @flow -const u8aConcat = require('./concat'); -const u8aFixLength = require('./fixLength'); -const u8aFromBn = require('./fromBn'); -const u8aFromBuffer = require('./fromBuffer'); -const u8aFromHex = require('./fromHex'); -const u8aFromUtf8 = require('./fromUtf8'); -const u8aFromString = require('./fromString'); -const u8aToBn = require('./toBn'); -const u8aToBuffer = require('./toBuffer'); -const u8aToHex = require('./toHex'); -const u8aToString = require('./toString'); -const u8aToU8a = require('./toU8a'); -const u8aToUtf8 = require('./toUtf8'); +import u8aConcat from './concat'; +import u8aFixLength from './fixLength'; +import u8aFromBn from './fromBn'; +import u8aFromBuffer from './fromBuffer'; +import u8aFromHex from './fromHex'; +import u8aFromUtf8 from './fromUtf8'; +import u8aFromString from './fromString'; +import u8aToBn from './toBn'; +import u8aToBuffer from './toBuffer'; +import u8aToHex from './toHex'; +import u8aToString from './toString'; +import u8aToU8a from './toU8a'; +import u8aToUtf8 from './toUtf8'; /** @summary Utility methods to convert to and from `Uint8Array` objects */ -module.exports = { +export { u8aConcat, u8aFixLength, u8aFromBn, diff --git a/packages/util/src/u8a/toBn.js b/packages/util/src/u8a/toBn.js index df7af7739..8f98aa073 100644 --- a/packages/util/src/u8a/toBn.js +++ b/packages/util/src/u8a/toBn.js @@ -3,10 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -const BN = require('bn.js'); +import BN from 'bn.js'; -const hexToBn = require('../hex/toBn'); -const u8aToHex = require('./toHex'); +import hexToBn from '../hex/toBn'; +import u8aToHex from './toHex'; /** @name u8aToBn @@ -19,9 +19,9 @@ const u8aToHex = require('./toHex'); u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f */ -module.exports = function u8aToBn (value?: Uint8Array, isLe: boolean = false): BN { +export default function u8aToBn (value?: Uint8Array, isLe: boolean = false): BN { return hexToBn( u8aToHex(value), isLe ); -}; +} diff --git a/packages/util/src/u8a/toBn.spec.js b/packages/util/src/u8a/toBn.spec.js index c0812f1b4..6cf050897 100644 --- a/packages/util/src/u8a/toBn.spec.js +++ b/packages/util/src/u8a/toBn.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aToBn } = require('./index'); +import { u8aToBn } from './index'; describe('u8aToBn', () => { it('converts values', () => { diff --git a/packages/util/src/u8a/toBuffer.js b/packages/util/src/u8a/toBuffer.js index 4d0f17ae9..170bd0a90 100644 --- a/packages/util/src/u8a/toBuffer.js +++ b/packages/util/src/u8a/toBuffer.js @@ -14,10 +14,10 @@ console.log('Buffer', u8aToBuffer('0x123480001f')); */ -module.exports = function u8aToBuffer (value?: Uint8Array): Buffer { +export default function u8aToBuffer (value?: Uint8Array): Buffer { if (!value) { return Buffer.from([]); } return Buffer.from(value); -}; +} diff --git a/packages/util/src/u8a/toBuffer.spec.js b/packages/util/src/u8a/toBuffer.spec.js index 077e3b020..7df1b1187 100644 --- a/packages/util/src/u8a/toBuffer.spec.js +++ b/packages/util/src/u8a/toBuffer.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aToBuffer } = require('./index'); +import { u8aToBuffer } from './index'; describe('u8aToBuffer', () => { it('returns [] when the buffer is null', () => { diff --git a/packages/util/src/u8a/toHex.js b/packages/util/src/u8a/toHex.js index 3ff5853bd..94a422d5f 100644 --- a/packages/util/src/u8a/toHex.js +++ b/packages/util/src/u8a/toHex.js @@ -14,7 +14,7 @@ u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f */ -module.exports = function u8aToHex (value?: Uint8Array | null, bitLength: number = -1, isPrefixed: boolean = true): string { +export default function u8aToHex (value?: Uint8Array | null, bitLength: number = -1, isPrefixed: boolean = true): string { const prefix = isPrefixed ? '0x' : ''; @@ -34,4 +34,4 @@ module.exports = function u8aToHex (value?: Uint8Array | null, bitLength: number return value.reduce((result, item) => { return result + `0${item.toString(16)}`.slice(-2); }, prefix); -}; +} diff --git a/packages/util/src/u8a/toHex.spec.js b/packages/util/src/u8a/toHex.spec.js index 03f72d274..4d4e9cb02 100644 --- a/packages/util/src/u8a/toHex.spec.js +++ b/packages/util/src/u8a/toHex.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aToHex } = require('./index'); +import { u8aToHex } from './index'; describe('u8aToHex', () => { it('returns empty as 0x', () => { diff --git a/packages/util/src/u8a/toString.js b/packages/util/src/u8a/toString.js index 61d04d2f5..970b2bfc4 100644 --- a/packages/util/src/u8a/toString.js +++ b/packages/util/src/u8a/toString.js @@ -14,10 +14,10 @@ u8aToString(new Uint8Array([21,23,45,67])); // 21,23,45,67 */ -module.exports = function u8aToString (value?: Uint8Array): string { +export default function u8aToString (value?: Uint8Array): string { if (!value || !value.length) { return ''; } return value.toString(); -}; +} diff --git a/packages/util/src/u8a/toString.spec.js b/packages/util/src/u8a/toString.spec.js index 9a01283ee..8fae6411b 100644 --- a/packages/util/src/u8a/toString.spec.js +++ b/packages/util/src/u8a/toString.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aToString } = require('./index'); +import { u8aToString } from './index'; describe('u8aToString', () => { it('decodes to an empty string for undefined', () => { diff --git a/packages/util/src/u8a/toU8a.js b/packages/util/src/u8a/toU8a.js index 803c7f3fa..26d0b457a 100644 --- a/packages/util/src/u8a/toU8a.js +++ b/packages/util/src/u8a/toU8a.js @@ -3,12 +3,12 @@ // of the ISC license. See the LICENSE file for details. // @flow -const isBuffer = require('../is/buffer'); -const isHex = require('../is/hex'); -const isString = require('../is/string'); -const bufferToU8a = require('../buffer/toU8a'); -const hexToU8a = require('../hex/toU8a'); -const u8aFromUtf8 = require('./fromUtf8'); +import isBuffer from '../is/buffer'; +import isHex from '../is/hex'; +import isString from '../is/string'; +import bufferToU8a from '../buffer/toU8a'; +import hexToU8a from '../hex/toU8a'; +import u8aFromUtf8 from './fromUtf8'; /** @name u8aToU8a @@ -22,7 +22,7 @@ const u8aFromUtf8 = require('./fromUtf8'); u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34]) u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34]) */ -module.exports = function u8aToU8a (value?: Buffer | Uint8Array | string | null): Uint8Array { +export default function u8aToU8a (value?: Buffer | Uint8Array | string | null): Uint8Array { // flowlint-next-line sketchy-null-string:off if (!value) { return new Uint8Array(0); @@ -45,4 +45,4 @@ module.exports = function u8aToU8a (value?: Buffer | Uint8Array | string | null) // $FlowFixMe type is determined return value; -}; +} diff --git a/packages/util/src/u8a/toU8a.spec.js b/packages/util/src/u8a/toU8a.spec.js index d353acde3..167e0c08a 100644 --- a/packages/util/src/u8a/toU8a.spec.js +++ b/packages/util/src/u8a/toU8a.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aToU8a } = require('./index'); +import { u8aToU8a } from './index'; describe('u8aToU8a', () => { it('returns an empty Uint8Array when null provided', () => { diff --git a/packages/util/src/u8a/toUtf8.js b/packages/util/src/u8a/toUtf8.js index d98b64980..723c4f22b 100644 --- a/packages/util/src/u8a/toUtf8.js +++ b/packages/util/src/u8a/toUtf8.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -require('./polyfill/textDecoder'); +import './polyfill/textDecoder'; const decoder = new TextDecoder('utf-8'); @@ -18,10 +18,10 @@ const decoder = new TextDecoder('utf-8'); u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello */ -module.exports = function u8aToUtf8 (value?: Uint8Array): string { +export default function u8aToUtf8 (value?: Uint8Array): string { if (!value || !value.length) { return ''; } return decoder.decode(value); -}; +} diff --git a/packages/util/src/u8a/toUtf8.spec.js b/packages/util/src/u8a/toUtf8.spec.js index f0e5d4d9f..b5a2fbec4 100644 --- a/packages/util/src/u8a/toUtf8.spec.js +++ b/packages/util/src/u8a/toUtf8.spec.js @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -const { u8aToUtf8 } = require('./index'); +import { u8aToUtf8 } from './index'; describe('u8aToUtf8', () => { it('decodes to an empty string for undefined', () => { diff --git a/yarn.lock b/yarn.lock index 096b4e13f..6dc61f335 100644 --- a/yarn.lock +++ b/yarn.lock @@ -664,9 +664,9 @@ lodash "^4.17.5" to-fast-properties "^2.0.0" -"@polkadot/dev@^0.19.25": - version "0.19.25" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.19.25.tgz#cf63238f59b9cdc015204ff760cf0a4c2892d0e5" +"@polkadot/dev@^0.19.28": + version "0.19.28" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.19.28.tgz#d474cec5cb66bf6d9c96b195de2f83cc4befe3af" dependencies: "@babel/cli" "^7.0.0-beta.47" "@babel/core" "^7.0.0-beta.47"