Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@polkadot/util

Package Overview
Dependencies
Maintainers
2
Versions
1411
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polkadot/util - npm Package Compare versions

Comparing version 9.4.2-15 to 9.4.2-16

cjs/u8a/toNumber.js

4

array/zip.d.ts

@@ -0,1 +1,5 @@

/**
* @name arrayZip
* @description Combines 2 distinct key/value arrays into a single [K, V] array
*/
export declare function arrayZip<K, V>(keys: K[], values: V[]): [K, V][];
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/**
* @name arrayZip
* @description Combines 2 distinct key/value arrays into a single [K, V] array
*/
export function arrayZip(keys, values) {

@@ -4,0 +9,0 @@ const result = new Array(keys.length);

@@ -10,2 +10,7 @@ "use strict";

// SPDX-License-Identifier: Apache-2.0
/**
* @name arrayZip
* @description Combines 2 distinct key/value arrays into a single [K, V] array
*/
function arrayZip(keys, values) {

@@ -12,0 +17,0 @@ const result = new Array(keys.length);

83

cjs/compact/fromU8a.js

@@ -39,30 +39,34 @@ "use strict";

case 0b01:
return [2, new _bn.BN(u8a[0] + u8a[1] * 0x100 >>> 2)];
return [2, new _bn.BN(u8a[0] + (u8a[1] << 8) >>> 2)];
case 0b10:
return [4, new _bn.BN(u8a[0] + u8a[1] * 0x100 + u8a[2] * 0x10000 + u8a[3] * 0x1000000 >>> 2)];
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, new _bn.BN(u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + u8a[3] * 0x1000000 >>> 2)];
// 0b11
default:
break;
} // add 5 to shifted (4 for base length, 1 for this byte)
{
// add 5 to shifted (4 for base length, 1 for this byte)
const offset = (u8a[0] >>> 2) + 5; // we unroll the loop
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [5, new _bn.BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + u8a[4] * 0x1000000)];
const offset = (u8a[0] >>> 2) + 5; // we unroll the loop
case 6:
return [6, new _bn.BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8)) * 0x1000000)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, new _bn.BN(u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000)];
case 7:
return [7, new _bn.BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1000000)];
// for anything else, use the non-unrolled version
case 6:
return [6, new _bn.BN(u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, new _bn.BN(u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000 + u8a[6] * 0x10000000000)];
// for anything else, use the non-unrolled version
default:
return [offset, (0, _u8a.u8aToBn)(u8a.subarray(1, offset))];
default:
return [offset, (0, _u8a.u8aToBn)(u8a.subarray(1, offset))];
}
}
}

@@ -84,31 +88,32 @@ }

case 0b01:
return [2, u8a[0] + u8a[1] * 0x100 >>> 2];
return [2, u8a[0] + (u8a[1] << 8) >>> 2];
case 0b10:
return [4, u8a[0] + u8a[1] * 0x100 + u8a[2] * 0x10000 + u8a[3] * 0x1000000 >>> 2];
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + u8a[3] * 0x1000000 >>> 2];
// 0b11
default:
break;
} // add 5 to shifted (4 for base length, 1 for this byte)
{
// add 5 to shifted (4 for base length, 1 for this byte)
// we unroll the loop
switch ((u8a[0] >>> 2) + 5) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + u8a[4] * 0x1000000];
case 6:
return [6, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8)) * 0x1000000];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
const offset = (u8a[0] >>> 2) + 5; // we unroll the loop
case 7:
return [7, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1000000];
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000];
case 6:
return [6, u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000 + u8a[6] * 0x10000000000];
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
}
}
}
}

@@ -14,4 +14,4 @@ "use strict";

type: 'cjs',
version: '9.4.2-15'
version: '9.4.2-16'
};
exports.packageInfo = packageInfo;

@@ -96,2 +96,8 @@ "use strict";

});
Object.defineProperty(exports, "u8aToNumber", {
enumerable: true,
get: function () {
return _toNumber.u8aToNumber;
}
});
Object.defineProperty(exports, "u8aToString", {

@@ -142,2 +148,4 @@ enumerable: true,

var _toNumber = require("./toNumber");
var _toString = require("./toString");

@@ -144,0 +152,0 @@

@@ -8,2 +8,4 @@ "use strict";

var _assert = require("../assert");
var _bn = require("../bn/bn");

@@ -16,3 +18,3 @@

/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
/** @deprecated Use u8aToBn (value?: string | null, options?: ToBnOptions) */
function u8aToBn(value) {

@@ -28,20 +30,98 @@ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

} : options;
const count = value.length;
let bn; // shortcut for <= u48 values - in this case the manual conversion
const count = value.length; // shortcut for <= u48 values - in this case the manual conversion
// here seems to be more efficient than passing the full array
// (and yes, it can be u48, e.g. in compacts with variable lengths)
if (count <= 6) {
let result = 0;
if (isNegative) {
let result = 0;
for (let i = 0; i < count; i++) {
result = result * 0x100 + (isLe ? value[count - 1 - i] : value[i]);
if (isLe) {
// Most common case i{8, 16, 32} default LE SCALE-encoded
// For <= 32, we also optimize the xor to a single op
// (see the comments around unrolling in the next section)
switch (count) {
case 0:
return new _bn.BN(0);
case 1:
result = value[0] ^ 0x000000ff;
break;
case 2:
result = value[0] + (value[1] << 8) ^ 0x0000ffff;
break;
case 3:
result = value[0] + (value[1] << 8) + (value[2] << 16) ^ 0x00ffffff;
break;
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
result = value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000 ^ 0xffffffff;
break;
case 5:
result = (value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000 ^ 0xffffffff) + (value[4] ^ 0xff) * 0x100000000;
break;
case 6:
result = (value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000 ^ 0xffffffff) + (value[4] + (value[5] << 8) ^ 0x0000ffff) * 0x100000000;
break;
default:
(0, _assert.assertUnreachable)(count);
}
} else {
for (let i = 0; i < count; i++) {
result = result * 0x100 + (value[i] ^ 0xff);
}
}
return count ? new _bn.BN(result * -1 - 1) : new _bn.BN(0);
} else if (isLe) {
// Most common case - u{8, 16, 32} default LE SCALE-encoded
//
// There are some slight benefits in unrolling this specific loop,
// however it comes with diminishing returns since here the actual
// `new BN` does seem to take up the bulk of the time
switch (count) {
case 0:
return new _bn.BN(0);
case 1:
return new _bn.BN(value[0]);
case 2:
return new _bn.BN(value[0] + (value[1] << 8));
case 3:
return new _bn.BN(value[0] + (value[1] << 8) + (value[2] << 16));
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return new _bn.BN(value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000);
case 5:
return new _bn.BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] + (value[4] << 8)) * 0x1000000);
case 6:
return new _bn.BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1000000);
default:
(0, _assert.assertUnreachable)(count);
}
} else {
let result = 0;
for (let i = 0; i < count; i++) {
result = result * 0x100 + value[i];
}
return new _bn.BN(result);
}
bn = new _bn.BN(result);
} else {
bn = new _bn.BN(value, isLe ? 'le' : 'be');
}
return isNegative && value.length ? bn.fromTwos(value.length * 8) : bn;
return isNegative ? new _bn.BN(value, isLe ? 'le' : 'be').fromTwos(value.length * 8) : new _bn.BN(value, isLe ? 'le' : 'be');
}

@@ -29,30 +29,34 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

case 0b01:
return [2, new BN(u8a[0] + u8a[1] * 0x100 >>> 2)];
return [2, new BN(u8a[0] + (u8a[1] << 8) >>> 2)];
case 0b10:
return [4, new BN(u8a[0] + u8a[1] * 0x100 + u8a[2] * 0x10000 + u8a[3] * 0x1000000 >>> 2)];
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, new BN(u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + u8a[3] * 0x1000000 >>> 2)];
// 0b11
default:
break;
} // add 5 to shifted (4 for base length, 1 for this byte)
{
// add 5 to shifted (4 for base length, 1 for this byte)
const offset = (u8a[0] >>> 2) + 5; // we unroll the loop
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [5, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + u8a[4] * 0x1000000)];
const offset = (u8a[0] >>> 2) + 5; // we unroll the loop
case 6:
return [6, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8)) * 0x1000000)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, new BN(u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000)];
case 7:
return [7, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1000000)];
// for anything else, use the non-unrolled version
case 6:
return [6, new BN(u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, new BN(u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000 + u8a[6] * 0x10000000000)];
// for anything else, use the non-unrolled version
default:
return [offset, u8aToBn(u8a.subarray(1, offset))];
default:
return [offset, u8aToBn(u8a.subarray(1, offset))];
}
}
}

@@ -73,31 +77,32 @@ }

case 0b01:
return [2, u8a[0] + u8a[1] * 0x100 >>> 2];
return [2, u8a[0] + (u8a[1] << 8) >>> 2];
case 0b10:
return [4, u8a[0] + u8a[1] * 0x100 + u8a[2] * 0x10000 + u8a[3] * 0x1000000 >>> 2];
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + u8a[3] * 0x1000000 >>> 2];
// 0b11
default:
break;
} // add 5 to shifted (4 for base length, 1 for this byte)
{
// add 5 to shifted (4 for base length, 1 for this byte)
// we unroll the loop
switch ((u8a[0] >>> 2) + 5) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + u8a[4] * 0x1000000];
case 6:
return [6, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8)) * 0x1000000];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
const offset = (u8a[0] >>> 2) + 5; // we unroll the loop
case 7:
return [7, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1000000];
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000];
case 6:
return [6, u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, u8a[1] + u8a[2] * 0x100 + u8a[3] * 0x10000 + u8a[4] * 0x1000000 + u8a[5] * 0x100000000 + u8a[6] * 0x10000000000];
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
}
}
}
}

@@ -23,3 +23,3 @@ {

"type": "module",
"version": "9.4.2-15",
"version": "9.4.2-16",
"main": "./cjs/index.js",

@@ -680,2 +680,7 @@ "module": "./index.js",

},
"./u8a/toNumber": {
"types": "./u8a/toNumber.d.ts",
"require": "./cjs/u8a/toNumber.js",
"default": "./u8a/toNumber.js"
},
"./u8a/toString": {

@@ -704,6 +709,6 @@ "types": "./u8a/toString.d.ts",

"@babel/runtime": "^7.18.3",
"@polkadot/x-bigint": "9.4.2-15",
"@polkadot/x-global": "9.4.2-15",
"@polkadot/x-textdecoder": "9.4.2-15",
"@polkadot/x-textencoder": "9.4.2-15",
"@polkadot/x-bigint": "9.4.2-16",
"@polkadot/x-global": "9.4.2-16",
"@polkadot/x-textdecoder": "9.4.2-16",
"@polkadot/x-textencoder": "9.4.2-16",
"@types/bn.js": "^5.1.0",

@@ -710,0 +715,0 @@ "bn.js": "^5.2.1",

@@ -8,3 +8,3 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

type: 'esm',
version: '9.4.2-15'
version: '9.4.2-16'
};

@@ -33,3 +33,3 @@ /// <reference types="bn.js" />

/**
* @description Number is negative, apply two's complement
* @description Number is signed, apply two's complement
*/

@@ -36,0 +36,0 @@ isNegative?: boolean;

@@ -14,4 +14,5 @@ /**

export { u8aToHex } from './toHex';
export { u8aToNumber } from './toNumber';
export { u8aToString } from './toString';
export { u8aToU8a } from './toU8a';
export { U8A_WRAP_ETHEREUM, U8A_WRAP_POSTFIX, U8A_WRAP_PREFIX, u8aIsWrapped, u8aWrapBytes, u8aUnwrapBytes } from './wrap';

@@ -17,4 +17,5 @@ // Copyright 2017-2022 @polkadot/util authors & contributors

export { u8aToHex } from "./toHex.js";
export { u8aToNumber } from "./toNumber.js";
export { u8aToString } from "./toString.js";
export { u8aToU8a } from "./toU8a.js";
export { U8A_WRAP_ETHEREUM, U8A_WRAP_POSTFIX, U8A_WRAP_PREFIX, u8aIsWrapped, u8aWrapBytes, u8aUnwrapBytes } from "./wrap.js";
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assertUnreachable } from "../assert.js";
import { BN } from "../bn/bn.js";

@@ -24,3 +25,3 @@ import { isBoolean } from "../is/boolean.js";

/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
/** @deprecated Use u8aToBn (value?: string | null, options?: ToBnOptions) */
function u8aToBn(value, options = {}) {

@@ -35,22 +36,100 @@ // NOTE: This is the same process as followed in the hexToBn conversion

} : options;
const count = value.length;
let bn; // shortcut for <= u48 values - in this case the manual conversion
const count = value.length; // shortcut for <= u48 values - in this case the manual conversion
// here seems to be more efficient than passing the full array
// (and yes, it can be u48, e.g. in compacts with variable lengths)
if (count <= 6) {
let result = 0;
if (isNegative) {
let result = 0;
for (let i = 0; i < count; i++) {
result = result * 0x100 + (isLe ? value[count - 1 - i] : value[i]);
if (isLe) {
// Most common case i{8, 16, 32} default LE SCALE-encoded
// For <= 32, we also optimize the xor to a single op
// (see the comments around unrolling in the next section)
switch (count) {
case 0:
return new BN(0);
case 1:
result = value[0] ^ 0x000000ff;
break;
case 2:
result = value[0] + (value[1] << 8) ^ 0x0000ffff;
break;
case 3:
result = value[0] + (value[1] << 8) + (value[2] << 16) ^ 0x00ffffff;
break;
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
result = value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000 ^ 0xffffffff;
break;
case 5:
result = (value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000 ^ 0xffffffff) + (value[4] ^ 0xff) * 0x100000000;
break;
case 6:
result = (value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000 ^ 0xffffffff) + (value[4] + (value[5] << 8) ^ 0x0000ffff) * 0x100000000;
break;
default:
assertUnreachable(count);
}
} else {
for (let i = 0; i < count; i++) {
result = result * 0x100 + (value[i] ^ 0xff);
}
}
return count ? new BN(result * -1 - 1) : new BN(0);
} else if (isLe) {
// Most common case - u{8, 16, 32} default LE SCALE-encoded
//
// There are some slight benefits in unrolling this specific loop,
// however it comes with diminishing returns since here the actual
// `new BN` does seem to take up the bulk of the time
switch (count) {
case 0:
return new BN(0);
case 1:
return new BN(value[0]);
case 2:
return new BN(value[0] + (value[1] << 8));
case 3:
return new BN(value[0] + (value[1] << 8) + (value[2] << 16));
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + value[3] * 0x1000000);
case 5:
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] + (value[4] << 8)) * 0x1000000);
case 6:
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1000000);
default:
assertUnreachable(count);
}
} else {
let result = 0;
for (let i = 0; i < count; i++) {
result = result * 0x100 + value[i];
}
return new BN(result);
}
bn = new BN(result);
} else {
bn = new BN(value, isLe ? 'le' : 'be');
}
return isNegative && value.length ? bn.fromTwos(value.length * 8) : bn;
return isNegative ? new BN(value, isLe ? 'le' : 'be').fromTwos(value.length * 8) : new BN(value, isLe ? 'le' : 'be');
}
export { u8aToBn };

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc