diff --git a/packages/type-extrinsics/src/codec/encode/call.ts b/packages/type-extrinsics/src/codec/encode/call.ts index 61d396f1e..ef22f0bcc 100644 --- a/packages/type-extrinsics/src/codec/encode/call.ts +++ b/packages/type-extrinsics/src/codec/encode/call.ts @@ -7,13 +7,14 @@ import { EncodingVersions } from '@polkadot/params/types'; import { ExtrinsicWithAccount, ExtrinsicWithIndex } from '../types'; import sizes from '@polkadot/params/sizes'; +import defaultSizes from '@polkadot/primitives/sizes'; import bnToU8a from '@polkadot/util/bn/toU8a'; import u8aConcat from '@polkadot/util/u8a/concat'; export default function encodeCall (publicKey: Uint8Array, nonce: number | BN, data: ExtrinsicWithIndex, version: EncodingVersions): ExtrinsicWithAccount { return u8aConcat( publicKey, - bnToU8a(nonce, sizes.Index.get(version), true), + bnToU8a(nonce, sizes.Index.get(version) || defaultSizes.Index, true), data ); } diff --git a/packages/type-params/src/encode/type/index.ts b/packages/type-params/src/encode/type/index.ts index 80d3d9fbf..a3404064b 100644 --- a/packages/type-params/src/encode/type/index.ts +++ b/packages/type-params/src/encode/type/index.ts @@ -10,6 +10,7 @@ import u8aConcat from '@polkadot/util/u8a/concat'; import u8aFromString from '@polkadot/util/u8a/fromString'; import u8aToU8a from '@polkadot/util/u8a/toU8a'; import addressDecode from '@polkadot/util-keyring/address/decode'; +import defaultSizes from '@polkadot/primitives/sizes'; import sizes from '../../sizes'; import keyValue from './keyValue'; @@ -26,7 +27,7 @@ export default function encodeType (type: Param$Type, value: any, version: Encod ); case 'Balance': - return bnToU8a(value, sizes.Balance.get(version), true); + return bnToU8a(value, sizes.Balance.get(version) || defaultSizes.Balance, true); case 'BlockNumber': case 'SessionKey': @@ -55,7 +56,7 @@ export default function encodeType (type: Param$Type, value: any, version: Encod return u8aToU8a(value); case 'Index': - return bnToU8a(value, sizes.Index.get(version), true); + return bnToU8a(value, sizes.Index.get(version) || defaultSizes.Balance, true); case 'KeyValue': case 'KeyValueStorage': diff --git a/packages/type-params/src/method.ts b/packages/type-params/src/method.ts index b63ad2711..ea93b8455 100644 --- a/packages/type-params/src/method.ts +++ b/packages/type-params/src/method.ts @@ -11,7 +11,7 @@ export default function createMethod (section: keyof T, sectionIndex: Uint8Ar return (name: string, index: number = 0): CreateItem => ({ description, key = '', params, type, isDeprecated = false, isHidden = false, isSigned = false, isSubscription = false, isUnhashed = false }: CreateItemOptions): SectionItem => ({ description, - index: u8aConcat(sectionIndex, bnToU8a(index, 8)), + index: u8aConcat(sectionIndex, bnToU8a(index, 8, true)), isDeprecated, isHidden, isSigned, diff --git a/packages/type-params/src/section.ts b/packages/type-params/src/section.ts index d68491184..2a4d9f436 100644 --- a/packages/type-params/src/section.ts +++ b/packages/type-params/src/section.ts @@ -9,7 +9,7 @@ import bnToU8a from '@polkadot/util/bn/toU8a'; import createMethod from './method'; export default function createSection (name: keyof T, _index: number = 0): CreateSection { - const index = bnToU8a(_index, 8); + const index = bnToU8a(_index, 8, true); const creator = (optOrFn: CreateSectionOptions): Section => { if (typeof optOrFn === 'function') { return creator( diff --git a/packages/util-rlp/src/encoder/toU8a.ts b/packages/util-rlp/src/encoder/toU8a.ts index c4e0ca509..fa25c8b96 100644 --- a/packages/util-rlp/src/encoder/toU8a.ts +++ b/packages/util-rlp/src/encoder/toU8a.ts @@ -44,7 +44,7 @@ const encoders: Array = [ // NOTE: Buffer before U8a { check: isBuffer, fn: bufferToU8a }, { check: isU8a, fn: convertU8a }, - { check: isBn, fn: bnToU8a }, + { check: isBn, fn: (value: any) => bnToU8a(value, -1, false) }, { check: isNumber, fn: numberToU8a }, { check: isString, fn: convertString } ]; diff --git a/packages/util-rlp/test/getTests.ts b/packages/util-rlp/test/getTests.ts index 3e5d6ca47..705946100 100644 --- a/packages/util-rlp/test/getTests.ts +++ b/packages/util-rlp/test/getTests.ts @@ -14,7 +14,7 @@ export default function getTests (file: string) { name, input: tests[name].in[0] !== '#' ? tests[name].in - : bnToU8a(new BN(tests[name].in.slice(1))), + : bnToU8a(new BN(tests[name].in.slice(1)), -1, true), output: `0x${tests[name].out.toLowerCase()}` })); } diff --git a/packages/util/src/bn/toU8a.spec.js b/packages/util/src/bn/toU8a.spec.js index bdf0eca2a..26047b1c2 100644 --- a/packages/util/src/bn/toU8a.spec.js +++ b/packages/util/src/bn/toU8a.spec.js @@ -9,25 +9,25 @@ import { bnToU8a } from './index'; describe('bnToU8a', () => { it('converts null values to 0x00', () => { expect( - bnToU8a(null) + bnToU8a(null, -1, false) ).toEqual(new Uint8Array([])); }); it('converts null values to 0x00000000 (bitLength)', () => { expect( - bnToU8a(null, 32) + bnToU8a(null, 32, false) ).toEqual(new Uint8Array([0, 0, 0, 0])); }); it('converts BN values to a prefixed hex representation', () => { expect( - bnToU8a(new BN(0x123456)) + bnToU8a(new BN(0x123456), -1, false) ).toEqual(new Uint8Array([0x12, 0x34, 0x56])); }); it('converts BN values to a prefixed hex representation (bitLength)', () => { expect( - bnToU8a(new BN(0x123456), 32) + bnToU8a(new BN(0x123456), 32, false) ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56])); }); diff --git a/packages/util/src/bn/toU8a.ts b/packages/util/src/bn/toU8a.ts index 80edbd41e..524b98e97 100644 --- a/packages/util/src/bn/toU8a.ts +++ b/packages/util/src/bn/toU8a.ts @@ -19,7 +19,7 @@ import bnToHex from './toHex'; * * bnToU8a(new BN(0x1234)); // => [0x12, 0x34] */ -export default function bnToU8a (value?: BN | number, bitLength: number = -1, isLe: boolean = false): Uint8Array { +export default function bnToU8a (value: BN | number, bitLength: number, isLe: boolean): Uint8Array { const byteLength = Math.ceil(bitLength / 8); if (!value) { diff --git a/packages/util/src/logger.spec.js b/packages/util/src/logger.spec.js index 89fe086e6..de2ee5151 100644 --- a/packages/util/src/logger.spec.js +++ b/packages/util/src/logger.spec.js @@ -29,7 +29,9 @@ describe('logger', () => { }; global.console = spy; - dateMatch = expect.stringMatching(/20[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/); + + // dateMatch = expect.stringMatching(/20[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/); + dateMatch = expect.stringMatching(/20[0-9]{2}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}/); prefixMatch = expect.stringMatching(/TEST:/); }); diff --git a/packages/util/src/logger.ts b/packages/util/src/logger.ts index 009a43206..5fadf3cc2 100644 --- a/packages/util/src/logger.ts +++ b/packages/util/src/logger.ts @@ -38,7 +38,7 @@ function apply (log: LogType, type: string, values: Logger$Data): void { // @ts-ignore Not sure how to coax TS here... console[logTo[log]].apply( console, [ - chalk(new Date().toString()), chalk(type) + chalk(new Date().toISOString()), chalk(type) ].concat(values) ); } diff --git a/packages/util/src/u8a/toBn.spec.js b/packages/util/src/u8a/toBn.spec.js index 6cf050897..056dfc1ad 100644 --- a/packages/util/src/u8a/toBn.spec.js +++ b/packages/util/src/u8a/toBn.spec.js @@ -5,10 +5,11 @@ import { u8aToBn } from './index'; describe('u8aToBn', () => { - it('converts values', () => { + it('converts values (big-endian)', () => { expect( u8aToBn( - new Uint8Array([0x12, 0x34]) + new Uint8Array([0x12, 0x34]), + false ).toString(16) ).toEqual('1234'); }); @@ -25,12 +26,13 @@ describe('u8aToBn', () => { it('converts empty', () => { expect( u8aToBn( - new Uint8Array([]) + new Uint8Array([]), + true ).toString(16) ).toEqual('0'); }); - it('handled overflows correctly', () => { + it('handled overflows correctly (little-endian)', () => { expect( u8aToBn( new Uint8Array([ 0, 1, 0, 0, 0, 0, 0, 0 ]), diff --git a/packages/util/src/u8a/toBn.ts b/packages/util/src/u8a/toBn.ts index 70884afd4..626402017 100644 --- a/packages/util/src/u8a/toBn.ts +++ b/packages/util/src/u8a/toBn.ts @@ -18,7 +18,7 @@ import u8aToHex from './toHex'; * * u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f */ -export default function u8aToBn (value?: Uint8Array, isLe: boolean = false): BN { +export default function u8aToBn (value: Uint8Array, isLe: boolean): BN { return hexToBn( u8aToHex(value), isLe