Use ES6 import/export (#139)

* Use ES6 import/export

* Fix tests
This commit is contained in:
Jaco Greeff
2018-06-20 16:36:07 +02:00
committed by GitHub
parent 7a694c075c
commit 99b86d9849
263 changed files with 841 additions and 814 deletions
+1 -1
View File
@@ -10,5 +10,5 @@
"packages": [
"packages/*"
],
"version": "0.22.11"
"version": "0.23.0"
}
+1 -1
View File
@@ -16,6 +16,6 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.19.25"
"@polkadot/dev": "^0.19.28"
}
}
+2 -2
View File
@@ -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",
+3 -3
View File
@@ -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);
};
}
@@ -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)', () => {
+3 -3
View File
@@ -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);
};
}
@@ -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', () => {
@@ -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)
);
};
}
@@ -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', () => {
@@ -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);
};
}
@@ -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)', () => {
@@ -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
};
+3 -3
View File
@@ -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
};
+6 -13
View File
@@ -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';
+4 -4
View File
@@ -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)
);
};
}
@@ -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';
+3 -3
View File
@@ -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)
);
};
}
@@ -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';
+3 -3
View File
@@ -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
};
+3 -3
View File
@@ -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);
};
}
@@ -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', () => {
+6 -6
View File
@@ -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
};
};
}
@@ -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', () => {
+9 -9
View File
@@ -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,
@@ -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();
};
}
@@ -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;
@@ -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);
};
}
@@ -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([
@@ -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);
};
}
@@ -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"
@@ -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
)
);
};
}
@@ -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', () => {
+3 -3
View File
@@ -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);
};
}
+2 -2
View File
@@ -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;
+3 -3
View File
@@ -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);
};
}
+2 -2
View File
@@ -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;
+4 -4
View File
@@ -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)
);
};
}
@@ -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', () => {
+5 -5
View File
@@ -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() // => <random number>
*/
module.exports = function randomAsNumber (): number {
export default function randomAsNumber (): number {
return hexToBn(
randomAsHex(8)
).and(BN_53).toNumber();
};
}
@@ -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', () => {
+3 -3
View File
@@ -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);
};
}
@@ -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', () => {
+4 -4
View File
@@ -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
+3 -3
View File
@@ -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);
};
}
@@ -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', () => {
+2 -2
View File
@@ -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
};
+4 -4
View File
@@ -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)
);
};
}
@@ -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', () => {
+3 -3
View File
@@ -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;
};
}
@@ -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', () => {
+3 -3
View File
@@ -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
};
@@ -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
);
};
}
@@ -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', () => {
@@ -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)
);
};
}
@@ -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', () => {
@@ -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);
};
}
@@ -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', () => {
@@ -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
);
};
}
@@ -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)', () => {
@@ -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
+3 -3
View File
@@ -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"
}
}
+10 -9
View File
@@ -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);
};
}
@@ -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', () => {
+9 -8
View File
@@ -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))
)
);
};
}
@@ -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', () => {
+3 -3
View File
@@ -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
};
+7 -7
View File
@@ -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)
};
};
}
+3 -3
View File
@@ -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]);
+9 -9
View File
@@ -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
};
};
}
@@ -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', () => {
+1 -1
View File
@@ -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
};
+7 -7
View File
@@ -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);
};
}
@@ -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(
+2 -2
View File
@@ -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;
};
}
+10 -10
View File
@@ -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<KeypairType>, meta?: KeyringPair$Meta = {}, defaultEncoded?: Uint8Array): KeyringPair {
export default function pair ({ publicKey, secretKey }: $Shape<KeypairType>, 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<KeypairType>, m
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
naclVerify(message, signature, publicKey)
};
};
}
+3 -1
View File
@@ -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', () => {
+3 -3
View File
@@ -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
};
};
}
+2 -2
View File
@@ -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
};
};
}
+3 -3
View File
@@ -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
}
};
};
}
@@ -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', () => {
+10 -10
View File
@@ -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)];
}
};
};
}
+5 -5
View File
@@ -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;
};
}
+6 -6
View File
@@ -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() });
}
+2 -2
View File
@@ -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"
+2 -2
View File
@@ -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;
+6 -4
View File
@@ -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', () => {
+7 -9
View File
@@ -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<Decoder> = [
{ max: 0x7f, fn: decodeSingle },
{ max: 0xb7, fn: decodeString },
@@ -24,11 +24,9 @@ const decoders: Array<Decoder> = [
{ 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;
+4 -4
View File
@@ -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<any> {
export default function decoder (input?: Uint8Array): Uint8Array | Array<any> {
if (!input || input.length === 0) {
return new Uint8Array([]);
}
@@ -18,4 +18,4 @@ module.exports = function decoder (input?: Uint8Array): Uint8Array | Array<any>
assert(remainder.length === 0, 'invalid remainder');
return decoded;
};
}
+4 -4
View File
@@ -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)
};
};
}
+2 -2
View File
@@ -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)
};
};
}
+4 -4
View File
@@ -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)
};
};
}
@@ -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);
};
}
@@ -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', () => {
+2 -2
View File
@@ -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)
};
};
}
+3 -3
View File
@@ -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)
};
};
}
+2 -2
View File
@@ -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;
+5 -3
View File
@@ -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 }) => {
+4 -4
View File
@@ -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
);
};
}
+4 -6
View File
@@ -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;
+4 -4
View File
@@ -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
);
};
}

Some files were not shown because too many files have changed in this diff Show More