diff --git a/package.json b/package.json index 72b12147a..61f159b53 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.16.11", + "@polkadot/dev": "^0.16.12", "lerna": "^2.5.1" } } diff --git a/packages/util/docs/hex.md b/packages/util/docs/hex.md index 590e8db70..cb9f1407b 100644 --- a/packages/util/docs/hex.md +++ b/packages/util/docs/hex.md @@ -111,7 +111,7 @@ console.log('stripped', hexStripPrefix('0x1234')); // => 1234 Creates a BN.js bignumber object from a hex string. ```js -hexToBn (value?: string): BN +hexToBn (value?: string, isLe: boolean = false): BN ``` diff --git a/packages/util/docs/u8a.md b/packages/util/docs/u8a.md index 6b03049f4..88bbbd771 100644 --- a/packages/util/docs/u8a.md +++ b/packages/util/docs/u8a.md @@ -129,7 +129,7 @@ u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f] Creates a BN from a Uint8Array object. ```js -u8aToHex (value?: Uint8Array): BN +u8aToHex (value?: Uint8Array, isLe: boolean = false): BN ``` diff --git a/packages/util/src/hex/toBn.js b/packages/util/src/hex/toBn.js index c8e5ddb08..f4e0441eb 100644 --- a/packages/util/src/hex/toBn.js +++ b/packages/util/src/hex/toBn.js @@ -9,7 +9,7 @@ const hexStripPrefix = require('./stripPrefix'); /** @name hexToBn - @signature hexToBn (value?: string): BN + @signature hexToBn (value?: string, isLe: boolean = false): BN @summary Creates a BN.js bignumber object from a hex string. @description `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error. @@ -18,11 +18,11 @@ const hexStripPrefix = require('./stripPrefix'); hexToBn('0x123480001f'); // => BN(0x123480001f) */ -module.exports = function hexToBn (value?: string): BN { +module.exports = function hexToBn (value?: string, isLe: boolean = false): BN { // flowlint-next-line sketchy-null-string:off if (!value) { return new BN(0); } - return new BN(hexStripPrefix(value) || '0', 16); + return new BN(hexStripPrefix(value) || '0', 16, isLe ? 'le' : 'be'); }; diff --git a/packages/util/src/hex/toBn.spec.js b/packages/util/src/hex/toBn.spec.js index 5ffa9ddc2..3669e6c80 100644 --- a/packages/util/src/hex/toBn.spec.js +++ b/packages/util/src/hex/toBn.spec.js @@ -30,4 +30,12 @@ describe('hexToBn', () => { ) ).toEqual(true); }); + + it('converts little-endian', () => { + expect( + hexToBn('0x4500000000000000', true).eq( + new BN(69) + ) + ).toEqual(true); + }); }); diff --git a/packages/util/src/u8a/toBn.js b/packages/util/src/u8a/toBn.js index 9c246e890..6c78d9eaa 100644 --- a/packages/util/src/u8a/toBn.js +++ b/packages/util/src/u8a/toBn.js @@ -10,7 +10,7 @@ const u8aToHex = require('./toHex'); /** @name u8aToBn - @signature u8aToHex (value?: Uint8Array): BN + @signature u8aToHex (value?: Uint8Array, isLe: boolean = false): BN @summary Creates a BN from a Uint8Array object. @description `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value. @@ -19,8 +19,9 @@ const u8aToHex = require('./toHex'); u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f */ -module.exports = function u8aToBn (value?: Uint8Array): BN { +module.exports = function u8aToBn (value?: Uint8Array, isLe: boolean = false): BN { return hexToBn( - u8aToHex(value) + u8aToHex(value), + isLe ); }; diff --git a/packages/util/src/u8a/toBn.spec.js b/packages/util/src/u8a/toBn.spec.js index ca0916cb8..635662764 100644 --- a/packages/util/src/u8a/toBn.spec.js +++ b/packages/util/src/u8a/toBn.spec.js @@ -13,6 +13,15 @@ describe('u8aToBn', () => { ).toEqual('1234'); }); + it('converts values (little-endian)', () => { + expect( + u8aToBn( + new Uint8Array([0x12, 0x34]), + true + ).toString(16) + ).toEqual('3412'); + }); + it('converts empty', () => { expect( u8aToBn( diff --git a/yarn.lock b/yarn.lock index 397053482..59cb2f3d6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -69,9 +69,9 @@ dependencies: babel-runtime "^6.26.0" -"@polkadot/dev@^0.16.11": - version "0.16.11" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.16.11.tgz#86dfbf1f60bef4a6ce0f8e60520fc6bf8adc7d59" +"@polkadot/dev@^0.16.12": + version "0.16.12" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.16.12.tgz#0d46d2b713510375381c273ebb787b040c9907ef" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0"