Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a14bec79f8 | ||
|
|
c0de5ac3c8 | ||
|
|
8d7f810556 | ||
|
|
5c6526a6bd | ||
|
|
e9635ab75b | ||
|
|
e946243b6f | ||
|
|
ca6a4177b5 | ||
|
|
7817311f84 | ||
|
|
77659aae2a | ||
|
|
7cc619fbb0 | ||
|
|
887654049e | ||
|
|
d07b79f578 | ||
|
|
cefeb0bb51 | ||
|
|
1d675ec803 | ||
|
|
7596dfa65c | ||
|
|
508e997c64 | ||
|
|
89563ee05c | ||
|
|
8b72584d53 | ||
|
|
4ae62c114b | ||
|
|
823fb51d18 | ||
|
|
a840b815bd | ||
|
|
fbf973dc2a | ||
|
|
2698f6779f | ||
|
|
3b5de80c54 | ||
|
|
041f5a7c2b | ||
|
|
cb8f1130af | ||
|
|
f480df7504 | ||
|
|
51763d3bf9 | ||
|
|
d2b7e88060 | ||
|
|
b20df563ce | ||
|
|
5ada180d66 | ||
|
|
a4256de444 | ||
|
|
bce5553a4b | ||
|
|
670743cbcf | ||
|
|
ffed9ceb6d | ||
|
|
bbacb1b80a | ||
|
|
5ea38c9898 | ||
|
|
7177722a49 | ||
|
|
6083923604 | ||
|
|
7a3e1c23aa | ||
|
|
cab50dbf7a | ||
|
|
9964b308ce | ||
|
|
247a0e7227 | ||
|
|
f2dba8aff8 | ||
|
|
2ef18335e3 | ||
|
|
fa0818d088 | ||
|
|
2106626f22 | ||
|
|
fceab0becc | ||
|
|
bdf42368f7 | ||
|
|
36ab5ea5e2 | ||
|
|
c5c8469038 | ||
|
|
83d0121a28 | ||
|
|
aff74ebde3 | ||
|
|
be4619ba3d | ||
|
|
17bc2e9fa0 | ||
|
|
66e2382f26 | ||
|
|
6c299f81c6 | ||
|
|
a4fcba6ff6 | ||
|
|
1a3507366e | ||
|
|
65939dbe9d | ||
|
|
6914411232 | ||
|
|
c79f46aaa2 | ||
|
|
cea09b2c8b | ||
|
|
bc23ed9fc9 | ||
|
|
a4f286e89a | ||
|
|
f112f9eb5e | ||
|
|
010552ca44 | ||
|
|
9ae4b1066e | ||
|
|
1a57abde6e | ||
|
|
cf4b44940e | ||
|
|
81e19aace4 | ||
|
|
7341c32df5 | ||
|
|
bb1ed6cfcd | ||
|
|
55dcfb5e18 |
@@ -1,5 +1,47 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 9.4.1 Jun 4, 2022
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `nextTick` to `@polkadot/util` to defer execution to the queue
|
||||
- Upgrade dependencies to latest stable versions
|
||||
|
||||
|
||||
## 9.3.1 May 29, 2022
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Ledger support for Parallel (Thanks to https://github.com/carlosala)
|
||||
- Add Ledger support for Astar & Polkadex (Thanks to https://github.com/carlosala)
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust `u8aToU8a` checks with better `isU8a` shortcut
|
||||
- Adjust compact conversion with unrolled loops
|
||||
- Adjust `string{Camel, Pascal}Case` with loop (no map)
|
||||
- Add `compactFromU8aLim` variant with Uint8Array-only inputs
|
||||
- Add `u8aConcatStrict` variant with Uint8Array-only inputs
|
||||
- Internal code maintainability enhancements
|
||||
- Upgrade dependencies to latest stable versions
|
||||
|
||||
|
||||
## 9.2.1 May 13, 2022
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Ledger support for Acala (Thanks to https://github.com/ntduan)
|
||||
|
||||
Changes:
|
||||
|
||||
- Optimize `u8aToHex` with direct (non-hex) conversion
|
||||
- Change all occurences of `.substr(...)` to `.substring(...)`
|
||||
- Sync with upstream Substrate ss58 registry
|
||||
- Mark deprecated function signatures (no removals)
|
||||
- Re-enable auto init of `cryptoWaitReady()`
|
||||
- Upgrade to `@polkadot/wasm-crypto` 6.1.1
|
||||
|
||||
|
||||
## 9.1.1 Apr 30, 2022
|
||||
|
||||
Contributed:
|
||||
|
||||
+3
-2
@@ -1,9 +1,10 @@
|
||||
1304 Jaco 9.1.1 (#1473)
|
||||
1338 Jaco 9.4.1 (#1514)
|
||||
9 Antoine Estienne Add eth uri keyring test (#1291)
|
||||
6 Amaury Martiny Add tests from Rust (#283)
|
||||
5 kwingram25 Add BN consts and timeToString (#610)
|
||||
4 Adam Dossa Update Polymesh Genesis Hash (#1193)
|
||||
4 Jakub Pánik Add basilisk 🐍 (#1396)
|
||||
3 Carlo Sala some changes to KnownLedger (#1497)
|
||||
3 Luke Schoen docs: Fix example in readme since we use default imports (#243)
|
||||
2 AndreasGassmann feat(dependencies): remove moment.js (#530)
|
||||
2 Gav Wood Fix stake:unstake (#182)
|
||||
@@ -20,7 +21,6 @@
|
||||
1 amphineko chore: fix typo in versionDetect messages (#941)
|
||||
1 Axel Chalon Simplify @polkadot/keyring KeyringPair (#419)
|
||||
1 Brad Larson [FIX] Adjust error message (#646)
|
||||
1 Carlo Sala feat(networks): add statemint to KnownLedger (#1467)
|
||||
1 Eliott Teissonniere add nodle ledger specs (#1089)
|
||||
1 h4x3rotab Throws an error when signing with a locked keypair (#578)
|
||||
1 Harry Liu Update README.md (#446)
|
||||
@@ -34,6 +34,7 @@
|
||||
1 Mario Neises remove redudant export (#257)
|
||||
1 Miguel Hervas Add CFG Ledger Support (#1057)
|
||||
1 nahuseyoum Reserving 65 for Aventus based on the new extended range (#871)
|
||||
1 Nantian add acala ledger config (#1485)
|
||||
1 qwer951123 Add acala genesis (#1161)
|
||||
1 Raoul Millais Fix yarn add instructions in x-global README (#873)
|
||||
1 Raymond Zhong Add Edgeware ledger app (#1006)
|
||||
|
||||
+5
-5
@@ -10,7 +10,7 @@
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
],
|
||||
@@ -27,11 +27,11 @@
|
||||
"test:one": "polkadot-dev-run-test --runInBand"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.17.10",
|
||||
"@polkadot/dev": "^0.66.16",
|
||||
"@types/jest": "^27.4.1"
|
||||
"@babel/core": "^7.18.2",
|
||||
"@polkadot/dev": "^0.66.34",
|
||||
"@types/jest": "^28.1.0"
|
||||
},
|
||||
"resolutions": {
|
||||
"typescript": "^4.6.4"
|
||||
"typescript": "^4.7.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,16 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@ledgerhq/hw-transport": "^6.27.1",
|
||||
"@ledgerhq/hw-transport-webhid": "^6.27.1",
|
||||
"@ledgerhq/hw-transport-webusb": "^6.27.1",
|
||||
"@polkadot/util": "9.1.1"
|
||||
"@polkadot/util": "9.4.1"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@ledgerhq/hw-transport-node-hid-singleton": "^6.27.1"
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger-transports', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger-transports', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -19,13 +19,13 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@ledgerhq/hw-transport": "^6.27.1",
|
||||
"@polkadot/hw-ledger-transports": "9.1.1",
|
||||
"@polkadot/util": "9.1.1",
|
||||
"@zondax/ledger-substrate": "^0.26.0"
|
||||
"@polkadot/hw-ledger-transports": "9.4.1",
|
||||
"@polkadot/util": "9.4.1",
|
||||
"@zondax/ledger-substrate": "^0.30.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,18 +4,23 @@
|
||||
import type Transport from '@ledgerhq/hw-transport';
|
||||
import type { SubstrateApp } from '@zondax/ledger-substrate';
|
||||
|
||||
import { newBifrostApp, newCentrifugeApp, newDockApp, newEdgewareApp, newEquilibriumApp, newGenshiroApp, newKusamaApp, newNodleApp, newPolkadotApp, newPolymeshApp, newSoraApp, newStatemineApp, newStatemintApp, newXXNetworkApp } from '@zondax/ledger-substrate';
|
||||
import { newAcalaApp, newAstarApp, newBifrostApp, newCentrifugeApp, newDockApp, newEdgewareApp, newEquilibriumApp, newGenshiroApp, newKaruraApp, newKusamaApp, newNodleApp, newParallelApp, newPolkadexApp, newPolkadotApp, newPolymeshApp, newSoraApp, newStatemineApp, newStatemintApp, newXXNetworkApp } from '@zondax/ledger-substrate';
|
||||
|
||||
// These match up with the keys of the knownLedger object in the @polkadot/networks/defaults/ledger.ts
|
||||
export const ledgerApps: Record<string, (transport: Transport) => SubstrateApp> = {
|
||||
acala: newAcalaApp,
|
||||
astar: newAstarApp,
|
||||
bifrost: newBifrostApp,
|
||||
centrifuge: newCentrifugeApp,
|
||||
'dock-mainnet': newDockApp,
|
||||
edgeware: newEdgewareApp,
|
||||
equilibrium: newEquilibriumApp,
|
||||
genshiro: newGenshiroApp,
|
||||
karura: newKaruraApp,
|
||||
kusama: newKusamaApp,
|
||||
'nodle-chain': newNodleApp,
|
||||
'nodle-para': newNodleApp,
|
||||
parallel: newParallelApp,
|
||||
polkadex: newPolkadexApp,
|
||||
polkadot: newPolkadotApp,
|
||||
polymesh: newPolymeshApp,
|
||||
sora: newSoraApp,
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -20,15 +20,15 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/util": "9.1.1",
|
||||
"@polkadot/util-crypto": "9.1.1"
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/util": "9.4.1",
|
||||
"@polkadot/util-crypto": "9.4.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "9.1.1",
|
||||
"@polkadot/util-crypto": "9.1.1"
|
||||
"@polkadot/util": "9.4.1",
|
||||
"@polkadot/util-crypto": "9.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/keyring', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/keyring', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/util": "9.1.1",
|
||||
"@substrate/ss58-registry": "^1.17.0"
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/util": "9.4.1",
|
||||
"@substrate/ss58-registry": "^1.22.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/hw-ledger": "9.1.1",
|
||||
"@polkadot/x-fetch": "9.1.1"
|
||||
"@polkadot/hw-ledger": "9.4.1",
|
||||
"@polkadot/x-fetch": "9.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,12 +53,18 @@ export const knownGenesis: KnownGenesis = {
|
||||
'0xe3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f636', // Kusama CC2
|
||||
'0x3fd7b9eb6a00376e5be61f01abb429ffb0b104be05eaff4d458da48fcd425baf' // Kusama CC1
|
||||
],
|
||||
'nodle-chain': [
|
||||
'0xa3d114c2b8d0627c1aa9b134eafcf7d05ca561fdc19fb388bb9457f81809fb23'
|
||||
'nodle-para': [
|
||||
'0x97da7ede98d7bad4e36b4d734b6055425a3be036da2a332ea5a7037656427a21'
|
||||
],
|
||||
parallel: [
|
||||
'0xe61a41c53f5dcd0beb09df93b34402aada44cb05117b71059cce40a2723a4e97'
|
||||
],
|
||||
picasso: [
|
||||
'0xe8e7f0f4c4f5a00720b4821dbfddefea7490bcf0b19009961cc46957984e2c1c'
|
||||
],
|
||||
polkadex: [
|
||||
'0x3920bcb4960a1eef5580cd5367ff3f430eef052774f78468852f7b9cb39f8a3c'
|
||||
],
|
||||
polkadot: [
|
||||
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'
|
||||
],
|
||||
|
||||
@@ -8,14 +8,19 @@ import type { KnownLedger } from '../types';
|
||||
//
|
||||
// NOTE: Any network here needs to have a genesisHash attached in the ./genesis.ts config
|
||||
export const knownLedger: KnownLedger = {
|
||||
acala: 0x00000313,
|
||||
astar: 0x0000032a,
|
||||
bifrost: 0x00000314,
|
||||
centrifuge: 0x000002eb,
|
||||
'dock-mainnet': 0x00000252,
|
||||
edgeware: 0x0000020b,
|
||||
equilibrium: 0x05f5e0fd,
|
||||
genshiro: 0x05f5e0fc,
|
||||
karura: 0x000002ae,
|
||||
kusama: 0x000001b2,
|
||||
'nodle-chain': 0x000003eb,
|
||||
'nodle-para': 0x000003eb,
|
||||
parallel: 0x00000162,
|
||||
polkadex: 0x0000031f,
|
||||
polkadot: 0x00000162,
|
||||
polymesh: 0x00000253,
|
||||
sora: 0x00000269,
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/networks', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/networks', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -245,22 +245,9 @@
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://stafi.io"
|
||||
},
|
||||
{
|
||||
"prefix": 21,
|
||||
"network": "dock-testnet",
|
||||
"displayName": "Dock Testnet",
|
||||
"symbols": [
|
||||
"DCK"
|
||||
],
|
||||
"decimals": [
|
||||
6
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://dock.io"
|
||||
},
|
||||
{
|
||||
"prefix": 22,
|
||||
"network": "dock-mainnet",
|
||||
"network": "dock-pos-mainnet",
|
||||
"displayName": "Dock Mainnet",
|
||||
"symbols": [
|
||||
"DCK"
|
||||
@@ -459,12 +446,12 @@
|
||||
{
|
||||
"prefix": 38,
|
||||
"network": "kilt",
|
||||
"displayName": "KILT Chain",
|
||||
"displayName": "KILT Spiritnet",
|
||||
"symbols": [
|
||||
"KILT"
|
||||
],
|
||||
"decimals": [
|
||||
18
|
||||
15
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://kilt.io/"
|
||||
@@ -646,6 +633,19 @@
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://dico.io"
|
||||
},
|
||||
{
|
||||
"prefix": 54,
|
||||
"network": "cere",
|
||||
"displayName": "Cere Network",
|
||||
"symbols": [
|
||||
"CERE"
|
||||
],
|
||||
"decimals": [
|
||||
10
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://cere.network"
|
||||
},
|
||||
{
|
||||
"prefix": 55,
|
||||
"network": "xxnetwork",
|
||||
@@ -793,6 +793,32 @@
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://polkadex.trade"
|
||||
},
|
||||
{
|
||||
"prefix": 89,
|
||||
"network": "polkadexparachain",
|
||||
"displayName": "Polkadex Parachain",
|
||||
"symbols": [
|
||||
"PDEX"
|
||||
],
|
||||
"decimals": [
|
||||
12
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://polkadex.trade"
|
||||
},
|
||||
{
|
||||
"prefix": 93,
|
||||
"network": "fragnova",
|
||||
"displayName": "Fragnova Network",
|
||||
"symbols": [
|
||||
"NOVA"
|
||||
],
|
||||
"decimals": [
|
||||
12
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://fragnova.com"
|
||||
},
|
||||
{
|
||||
"prefix": 98,
|
||||
"network": "polkasmith",
|
||||
@@ -824,13 +850,13 @@
|
||||
"network": "origintrail-parachain",
|
||||
"displayName": "OriginTrail Parachain",
|
||||
"symbols": [
|
||||
"TRAC"
|
||||
"OTP"
|
||||
],
|
||||
"decimals": [
|
||||
18
|
||||
12
|
||||
],
|
||||
"standardAccount": "secp256k1",
|
||||
"website": "https://origintrail.io"
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://parachain.origintrail.io/"
|
||||
},
|
||||
{
|
||||
"prefix": 105,
|
||||
@@ -867,6 +893,19 @@
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://integritee.network"
|
||||
},
|
||||
{
|
||||
"prefix": 117,
|
||||
"network": "tinker",
|
||||
"displayName": "Tinker",
|
||||
"symbols": [
|
||||
"TNKR"
|
||||
],
|
||||
"decimals": [
|
||||
12
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://invarch.network"
|
||||
},
|
||||
{
|
||||
"prefix": 128,
|
||||
"network": "clover",
|
||||
@@ -1024,7 +1063,7 @@
|
||||
"website": "https://moonbeam.network"
|
||||
},
|
||||
{
|
||||
"prefix": 1337,
|
||||
"prefix": 1328,
|
||||
"network": "ajuna",
|
||||
"displayName": "Ajuna Network",
|
||||
"symbols": [
|
||||
@@ -1033,7 +1072,20 @@
|
||||
"decimals": [
|
||||
12
|
||||
],
|
||||
"standardAccount": "Sr25519",
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://ajuna.io"
|
||||
},
|
||||
{
|
||||
"prefix": 1337,
|
||||
"network": "bajun",
|
||||
"displayName": "Bajun Network",
|
||||
"symbols": [
|
||||
"BAJU"
|
||||
],
|
||||
"decimals": [
|
||||
12
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://ajuna.io"
|
||||
},
|
||||
{
|
||||
@@ -1101,6 +1153,19 @@
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://subspace.network"
|
||||
},
|
||||
{
|
||||
"prefix": 7007,
|
||||
"network": "tidefi",
|
||||
"displayName": "Tidefi",
|
||||
"symbols": [
|
||||
"TIFI"
|
||||
],
|
||||
"decimals": [
|
||||
12
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://tidefi.com"
|
||||
},
|
||||
{
|
||||
"prefix": 7391,
|
||||
"network": "unique_mainnet",
|
||||
@@ -1114,6 +1179,19 @@
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://unique.network"
|
||||
},
|
||||
{
|
||||
"prefix": 9807,
|
||||
"network": "dentnet",
|
||||
"displayName": "DENTNet",
|
||||
"symbols": [
|
||||
"DENTX"
|
||||
],
|
||||
"decimals": [
|
||||
18
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://www.dentnet.io"
|
||||
},
|
||||
{
|
||||
"prefix": 10041,
|
||||
"network": "basilisk",
|
||||
@@ -1165,5 +1243,18 @@
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://ata.network"
|
||||
},
|
||||
{
|
||||
"prefix": 12191,
|
||||
"network": "nftmart",
|
||||
"displayName": "NFTMart",
|
||||
"symbols": [
|
||||
"NMT"
|
||||
],
|
||||
"decimals": [
|
||||
12
|
||||
],
|
||||
"standardAccount": "*25519",
|
||||
"website": "https://nftmart.io"
|
||||
}
|
||||
]
|
||||
@@ -22,21 +22,21 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"browser": {
|
||||
"crypto": false,
|
||||
"stream": false
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@noble/hashes": "1.0.0",
|
||||
"@noble/secp256k1": "1.5.5",
|
||||
"@polkadot/networks": "9.1.1",
|
||||
"@polkadot/util": "9.1.1",
|
||||
"@polkadot/wasm-crypto": "^6.0.1",
|
||||
"@polkadot/x-bigint": "9.1.1",
|
||||
"@polkadot/x-randomvalues": "9.1.1",
|
||||
"@polkadot/networks": "9.4.1",
|
||||
"@polkadot/util": "9.4.1",
|
||||
"@polkadot/wasm-crypto": "^6.1.1",
|
||||
"@polkadot/x-bigint": "9.4.1",
|
||||
"@polkadot/x-randomvalues": "9.4.1",
|
||||
"@scure/base": "1.0.0",
|
||||
"ed2curve": "^0.3.0",
|
||||
"tweetnacl": "^1.0.3"
|
||||
@@ -45,6 +45,6 @@
|
||||
"@types/ed2curve": "^0.2.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "9.1.1"
|
||||
"@polkadot/util": "9.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,5 @@ import { decodeAddress } from './decode';
|
||||
* @summary Converts an SS58 address to its corresponding EVM address.
|
||||
*/
|
||||
export function addressToEvm (address: HexString | string | Uint8Array, ignoreChecksum?: boolean): Uint8Array {
|
||||
const decoded = decodeAddress(address, ignoreChecksum);
|
||||
|
||||
return decoded.subarray(0, 20);
|
||||
return decodeAddress(address, ignoreChecksum).subarray(0, 20);
|
||||
}
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Network } from '@polkadot/networks/types';
|
||||
|
||||
import { availableNetworks } from '../networks';
|
||||
|
||||
function networkToPrefix ({ prefix }: Network): number {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
export const defaults = {
|
||||
allowedDecodedLengths: [1, 2, 4, 8, 32, 33],
|
||||
// publicKey has prefix + 2 checksum bytes, short only prefix + 1 checksum byte
|
||||
allowedEncodedLengths: [3, 4, 6, 10, 35, 36, 37, 38],
|
||||
allowedPrefix: availableNetworks.map(networkToPrefix),
|
||||
allowedPrefix: availableNetworks.map(({ prefix }) => prefix),
|
||||
prefix: 42
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ const l = logger('setSS58Format');
|
||||
* @deprecated Use keyring.setSS58Format
|
||||
*/
|
||||
export function setSS58Format (prefix: Prefix): void {
|
||||
l.warn('Global setting of the ss58Format is deprecated and not recommended. Set format on the keyring (if used) or as pat of the address encode function');
|
||||
l.warn('Global setting of the ss58Format is deprecated and not recommended. Set format on the keyring (if used) or as part of the address encode function');
|
||||
|
||||
defaults.prefix = prefix;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ export function createDecode ({ coder, ipfs }: Config, validate: ValidateFn): De
|
||||
|
||||
return coder.decode(
|
||||
ipfs && ipfsCompat
|
||||
? value.substr(1)
|
||||
? value.substring(1)
|
||||
: value
|
||||
);
|
||||
};
|
||||
|
||||
@@ -10,8 +10,6 @@ import { blake2b, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { createAsHex } from '../helpers';
|
||||
|
||||
type BitLength = 64 | 128 | 256 | 384 | 512;
|
||||
|
||||
/**
|
||||
* @name blake2AsU8a
|
||||
* @summary Creates a blake2b u8a from the input.
|
||||
@@ -26,7 +24,7 @@ type BitLength = 64 | 128 | 256 | 384 | 512;
|
||||
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
|
||||
* ```
|
||||
*/
|
||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength: BitLength = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength: 64 | 128 | 256 | 384 | 512 = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
const u8a = u8aToU8a(data);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { utils as utilsNobleSecp256k1 } from '@noble/secp256k1';
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
// import { cryptoWaitReady } from './crypto';
|
||||
import { cryptoWaitReady } from './crypto';
|
||||
import { hmacSha256AsU8a } from './hmac';
|
||||
|
||||
// Set overrides on the secp256k1 utils
|
||||
@@ -15,7 +15,7 @@ import { hmacSha256AsU8a } from './hmac';
|
||||
utilsNobleSecp256k1.hmacSha256Sync = (key: Uint8Array, ...messages: Uint8Array[]) =>
|
||||
hmacSha256AsU8a(key, u8aConcat(...messages));
|
||||
|
||||
// // start init process immediately
|
||||
// cryptoWaitReady().catch((): void => {
|
||||
// // shouldn't happen, logged above
|
||||
// });
|
||||
// start init process immediately
|
||||
cryptoWaitReady().catch((): void => {
|
||||
// shouldn't happen, logged and caught inside cryptoWaitReady
|
||||
});
|
||||
|
||||
@@ -14,13 +14,12 @@ interface DualHash {
|
||||
512: (u8a: Uint8Array) => Uint8Array;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function createAsHex <T extends (...args: any[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
|
||||
export function createAsHex <T extends (...args: never[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
|
||||
return (...args: Parameters<T>): HexString =>
|
||||
u8aToHex(fn(...args));
|
||||
}
|
||||
|
||||
export function createBitHasher <T> (bitLength: T, fn: (data: HexString | Buffer | Uint8Array | string, bitLength: T, onlyJs?: boolean) => Uint8Array): (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean) => Uint8Array {
|
||||
export function createBitHasher (bitLength: 256 | 512, fn: (data: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512, onlyJs?: boolean) => Uint8Array): (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean) => Uint8Array {
|
||||
return (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||
fn(data, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ import { sha512 } from '@noble/hashes/sha512';
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
import { hmacSha256, hmacSha512, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
type BitLength = 256 | 512;
|
||||
|
||||
const JS_HASH = {
|
||||
256: sha256,
|
||||
512: sha512
|
||||
@@ -20,7 +18,7 @@ const WA_MHAC = {
|
||||
512: hmacSha512
|
||||
};
|
||||
|
||||
function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array {
|
||||
function createSha (bitLength: 256 | 512): (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array {
|
||||
return (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean): Uint8Array =>
|
||||
hmacShaAsU8a(key, data, bitLength, onlyJs);
|
||||
}
|
||||
@@ -29,7 +27,7 @@ function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint
|
||||
* @name hmacShaAsU8a
|
||||
* @description creates a Hmac Sha (256/512) Uint8Array from the key & data
|
||||
*/
|
||||
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
||||
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array {
|
||||
const u8aKey = u8aToU8a(key);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
|
||||
@@ -20,7 +20,7 @@ export class DeriveJunction {
|
||||
public static from (value: string): DeriveJunction {
|
||||
const result = new DeriveJunction();
|
||||
const [code, isHard] = value.startsWith('/')
|
||||
? [value.substr(1), true]
|
||||
? [value.substring(1), true]
|
||||
: [value, false];
|
||||
|
||||
result.soft(
|
||||
|
||||
@@ -24,7 +24,7 @@ export function keyExtractPath (derivePath: string): ExtractResult {
|
||||
constructed = parts.join('');
|
||||
|
||||
for (const p of parts) {
|
||||
path.push(DeriveJunction.from(p.substr(1)));
|
||||
path.push(DeriveJunction.from(p.substring(1)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
// - Use util-crypto randomAsU8a (instead of randombytes)
|
||||
// - Remove setting of wordlist passing of wordlist in functions
|
||||
// - Remove mnemonicToSeed (we only use the sync variant)
|
||||
// - generateMnemonic takes number of words (instead of strength)
|
||||
|
||||
import { assert, stringToU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -73,7 +74,7 @@ export function mnemonicToEntropy (mnemonic: string): Uint8Array {
|
||||
// calculate the checksum and compare
|
||||
const entropyBytes = entropyBits.match(/(.{1,8})/g)?.map(binaryToByte);
|
||||
|
||||
assert(entropyBytes && entropyBytes.length % 4 === 0 && entropyBytes.length >= 16 && entropyBytes.length <= 32, INVALID_ENTROPY);
|
||||
assert(entropyBytes && (entropyBytes.length % 4 === 0) && (entropyBytes.length >= 16) && (entropyBytes.length <= 32), INVALID_ENTROPY);
|
||||
|
||||
const entropy = u8aToU8a(entropyBytes);
|
||||
const newChecksum = deriveChecksumBits(entropy);
|
||||
@@ -85,7 +86,7 @@ export function mnemonicToEntropy (mnemonic: string): Uint8Array {
|
||||
|
||||
export function entropyToMnemonic (entropy: Uint8Array): string {
|
||||
// 128 <= ENT <= 256
|
||||
assert(entropy.length % 4 === 0 && entropy.length >= 16 && entropy.length <= 32, INVALID_ENTROPY);
|
||||
assert((entropy.length % 4 === 0) && (entropy.length >= 16) && (entropy.length <= 32), INVALID_ENTROPY);
|
||||
|
||||
const entropyBits = bytesToBinary(Array.from(entropy));
|
||||
const checksumBits = deriveChecksumBits(entropy);
|
||||
@@ -97,12 +98,8 @@ export function entropyToMnemonic (entropy: Uint8Array): string {
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
export function generateMnemonic (strength?: number): string {
|
||||
strength = strength || 128;
|
||||
|
||||
assert(strength % 32 === 0, INVALID_ENTROPY);
|
||||
|
||||
return entropyToMnemonic(randomAsU8a(strength / 8));
|
||||
export function generateMnemonic (numWords: 12 | 15 | 18 | 21 | 24): string {
|
||||
return entropyToMnemonic(randomAsU8a((numWords / 3) * 4));
|
||||
}
|
||||
|
||||
export function validateMnemonic (mnemonic: string): boolean {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { cryptoWaitReady } from '..';
|
||||
import { mnemonicGenerate, WordCount } from './generate';
|
||||
import { mnemonicGenerate } from './generate';
|
||||
import { mnemonicValidate } from './validate';
|
||||
|
||||
describe('mnemonicGenerate', (): void => {
|
||||
@@ -17,7 +17,7 @@ describe('mnemonicGenerate', (): void => {
|
||||
});
|
||||
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it.each([12, 15, 18, 21, 24] as WordCount[])('generates a valid mnemonic (%p words)', (num): void => {
|
||||
it.each([12, 15, 18, 21, 24] as 12[])('generates a valid mnemonic (%p words)', (num): void => {
|
||||
const mnemonic = mnemonicGenerate(num, onlyJs);
|
||||
const isValid = mnemonicValidate(mnemonic);
|
||||
|
||||
|
||||
@@ -6,17 +6,6 @@ import { bip39Generate, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { generateMnemonic } from './bip39';
|
||||
|
||||
export type WordCount = 12 | 15 | 18 | 21 | 24;
|
||||
|
||||
// mapping of words to the actual strength (as expected)
|
||||
const STRENGTH_MAP = {
|
||||
12: 16 * 8,
|
||||
15: 20 * 8,
|
||||
18: 24 * 8,
|
||||
21: 28 * 8,
|
||||
24: 32 * 8
|
||||
};
|
||||
|
||||
/**
|
||||
* @name mnemonicGenerate
|
||||
* @summary Creates a valid mnemonic string using using [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
|
||||
@@ -29,8 +18,8 @@ const STRENGTH_MAP = {
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicGenerate (numWords: WordCount = 12, onlyJs?: boolean): string {
|
||||
export function mnemonicGenerate (numWords: 12 | 15 | 18 | 21 | 24 = 12, onlyJs?: boolean): string {
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? bip39Generate(numWords)
|
||||
: generateMnemonic(STRENGTH_MAP[numWords]);
|
||||
: generateMnemonic(numWords);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ describe('mnemonicToMiniSecret', (): void => {
|
||||
u8aToHex(mnemonicToMiniSecret(mnemonic, 'Substrate', onlyJs))
|
||||
).toEqual(
|
||||
// mini returned here, only check first 32-bytes (64 hex + 2 prefix)
|
||||
seed.substr(0, 66)
|
||||
seed.substring(0, 66)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/util-crypto', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/util-crypto', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -9,8 +9,6 @@ import { isReady, twox } from '@polkadot/wasm-crypto';
|
||||
import { createAsHex } from '../helpers';
|
||||
import { xxhash64 } from './xxhash64';
|
||||
|
||||
type BitLength = 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512;
|
||||
|
||||
/**
|
||||
* @name xxhashAsU8a
|
||||
* @summary Creates a xxhash64 u8a from the input.
|
||||
@@ -25,7 +23,7 @@ type BitLength = 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512;
|
||||
* xxhashAsU8a('abc'); // => 0x44bc2cf5ad770999
|
||||
* ```
|
||||
*/
|
||||
export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bitLength: BitLength = 64, onlyJs?: boolean): Uint8Array {
|
||||
export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bitLength: 64 | 128 | 192 | 256 | 320 | 384 | 448 | 512 = 64, onlyJs?: boolean): Uint8Array {
|
||||
const rounds = Math.ceil(bitLength / 64);
|
||||
const u8a = u8aToU8a(data);
|
||||
|
||||
|
||||
+10
-10
@@ -9,13 +9,13 @@
|
||||
"exports": {
|
||||
"./hex/toU8a": {
|
||||
"node": {
|
||||
"require": "./hex/toU8aBuffer.cjs",
|
||||
"require": "./cjs/hex/toU8aBuffer.js",
|
||||
"default": "./hex/toU8aBuffer.js"
|
||||
}
|
||||
},
|
||||
"./u8a/toHex": {
|
||||
"node": {
|
||||
"require": "./u8a/toHexBuffer.cjs",
|
||||
"require": "./cjs/u8a/toHexBuffer.js",
|
||||
"default": "./u8a/toHexBuffer.js"
|
||||
}
|
||||
}
|
||||
@@ -34,19 +34,19 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/x-bigint": "9.1.1",
|
||||
"@polkadot/x-global": "9.1.1",
|
||||
"@polkadot/x-textdecoder": "9.1.1",
|
||||
"@polkadot/x-textencoder": "9.1.1",
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/x-bigint": "9.4.1",
|
||||
"@polkadot/x-global": "9.4.1",
|
||||
"@polkadot/x-textdecoder": "9.4.1",
|
||||
"@polkadot/x-textencoder": "9.4.1",
|
||||
"@types/bn.js": "^5.1.0",
|
||||
"bn.js": "^5.2.0",
|
||||
"bn.js": "^5.2.1",
|
||||
"ip-regex": "^4.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/x-randomvalues": "9.1.1"
|
||||
"@polkadot/x-randomvalues": "9.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { arrayChunk } from '.';
|
||||
import { performance } from '../test/performance';
|
||||
import { arrayChunk, arrayRange } from '.';
|
||||
|
||||
describe('arrayChunk', (): void => {
|
||||
it('chunks arrays', (): void => {
|
||||
@@ -9,4 +10,6 @@ describe('arrayChunk', (): void => {
|
||||
arrayChunk([1, 2, 3, 4, 5, 6, 7], 3)
|
||||
).toEqual([[1, 2, 3], [4, 5, 6], [7]]);
|
||||
});
|
||||
|
||||
performance('arrayChunk', 200_000, [[arrayRange(500), 50]], arrayChunk);
|
||||
});
|
||||
|
||||
@@ -13,12 +13,6 @@ import { nToBigInt } from './toBigInt';
|
||||
const DIV = BigInt(256);
|
||||
const NEG_MASK = BigInt(0xff);
|
||||
|
||||
function createEmpty ({ bitLength = 0 }: NumberOptions): Uint8Array {
|
||||
return bitLength === -1
|
||||
? new Uint8Array()
|
||||
: new Uint8Array(Math.ceil(bitLength / 8));
|
||||
}
|
||||
|
||||
function toU8a (value: bigint, { isLe, isNegative }: NumberOptions): Uint8Array {
|
||||
const arr: number[] = [];
|
||||
|
||||
@@ -59,7 +53,9 @@ export function nToU8a <ExtToBn extends ToBn | ToBigInt> (value?: ExtToBn | BN |
|
||||
const valueBi = nToBigInt(value);
|
||||
|
||||
if (valueBi === _0n) {
|
||||
return createEmpty(opts);
|
||||
return opts.bitLength === -1
|
||||
? new Uint8Array()
|
||||
: new Uint8Array(Math.ceil((opts.bitLength || 0) / 8));
|
||||
}
|
||||
|
||||
const u8a = toU8a(valueBi, opts);
|
||||
|
||||
@@ -27,10 +27,9 @@ import { BN } from './bn';
|
||||
* ```
|
||||
*/
|
||||
export function bnToBn <ExtToBn extends ToBigInt | ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): BN {
|
||||
return BN.isBN(value)
|
||||
? value
|
||||
: !value
|
||||
? new BN(0)
|
||||
return value
|
||||
? BN.isBN(value)
|
||||
? value
|
||||
: isHex(value)
|
||||
? hexToBn(value.toString())
|
||||
: isBigInt(value)
|
||||
@@ -39,5 +38,6 @@ export function bnToBn <ExtToBn extends ToBigInt | ToBn> (value?: HexString | Ex
|
||||
? value.toBn()
|
||||
: isToBigInt(value)
|
||||
? new BN(value.toBigInt().toString())
|
||||
: new BN(value);
|
||||
: new BN(value)
|
||||
: new BN(0);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ describe('bnToHex', (): void => {
|
||||
).toBe('0xfffffb2e');
|
||||
});
|
||||
|
||||
it('handles backwards compatibility', (): void => {
|
||||
it('handles legacy compatibility', (): void => {
|
||||
expect(
|
||||
bnToHex(new BN(1234), 32)
|
||||
).toBe(
|
||||
|
||||
@@ -28,7 +28,9 @@ const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: false, isNegative: fa
|
||||
* ```
|
||||
*/
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions): HexString;
|
||||
/** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString;
|
||||
/** @deprecated Use bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2?: boolean): HexString {
|
||||
return !value
|
||||
? ZERO_STR
|
||||
|
||||
@@ -10,23 +10,6 @@ import { bnToBn } from './toBn';
|
||||
|
||||
const DEFAULT_OPTS: NumberOptions = { bitLength: -1, isLe: true, isNegative: false };
|
||||
|
||||
function createEmpty (byteLength: number, options: NumberOptions): Uint8Array {
|
||||
return options.bitLength === -1
|
||||
? new Uint8Array()
|
||||
: new Uint8Array(byteLength);
|
||||
}
|
||||
|
||||
function createValue (valueBn: BN, byteLength: number, { isLe, isNegative }: NumberOptions): Uint8Array {
|
||||
const output = new Uint8Array(byteLength);
|
||||
const bn = isNegative
|
||||
? valueBn.toTwos(byteLength * 8)
|
||||
: valueBn;
|
||||
|
||||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnToU8a
|
||||
* @summary Creates a Uint8Array object from a BN.
|
||||
@@ -42,9 +25,11 @@ function createValue (valueBn: BN, byteLength: number, { isLe, isNegative }: Num
|
||||
* ```
|
||||
*/
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions): Uint8Array;
|
||||
/** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
|
||||
/** @deprecated Use bnToU8a(value?: ExtToBn | BN | bigint | number | null, options?: NumberOptions) */
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | NumberOptions = DEFAULT_OPTS, arg2?: boolean): Uint8Array {
|
||||
const options: NumberOptions = objectSpread(
|
||||
const { bitLength, isLe, isNegative }: NumberOptions = objectSpread(
|
||||
{ bitLength: -1, isLe: true, isNegative: false },
|
||||
isNumber(arg1)
|
||||
? { bitLength: arg1, isLe: arg2 }
|
||||
@@ -52,13 +37,24 @@ function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number
|
||||
);
|
||||
|
||||
const valueBn = bnToBn(value);
|
||||
const byteLength = options.bitLength === -1
|
||||
const byteLength = bitLength === -1
|
||||
? Math.ceil(valueBn.bitLength() / 8)
|
||||
: Math.ceil((options.bitLength || 0) / 8);
|
||||
: Math.ceil((bitLength || 0) / 8);
|
||||
|
||||
return value
|
||||
? createValue(valueBn, byteLength, options)
|
||||
: createEmpty(byteLength, options);
|
||||
if (!value) {
|
||||
return bitLength === -1
|
||||
? new Uint8Array()
|
||||
: new Uint8Array(byteLength);
|
||||
}
|
||||
|
||||
const output = new Uint8Array(byteLength);
|
||||
const bn = isNegative
|
||||
? valueBn.toTwos(byteLength * 8)
|
||||
: valueBn;
|
||||
|
||||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export { bnToU8a };
|
||||
|
||||
@@ -20,6 +20,7 @@ export * from './is';
|
||||
export * from './lazy';
|
||||
export * from './logger';
|
||||
export * from './memoize';
|
||||
export * from './nextTick';
|
||||
export * from './number';
|
||||
export * from './object';
|
||||
export * from './promisify';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aConcat } from '../u8a';
|
||||
import { u8aConcatStrict } from '../u8a';
|
||||
import { compactToU8a } from './toU8a';
|
||||
|
||||
/**
|
||||
@@ -17,8 +17,8 @@ import { compactToU8a } from './toU8a';
|
||||
* ```
|
||||
*/
|
||||
export function compactAddLength (input: Uint8Array): Uint8Array {
|
||||
return u8aConcat(
|
||||
return u8aConcatStrict([
|
||||
compactToU8a(input.length),
|
||||
input
|
||||
);
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
import { BN } from '../bn/bn';
|
||||
import { hexToU8a } from '../hex/toU8a';
|
||||
import { compactFromU8a } from '.';
|
||||
import { performance } from '../test/performance';
|
||||
import { compactFromU8a, compactFromU8aLim } from '.';
|
||||
|
||||
describe('compactFromU8a', (): void => {
|
||||
it('decoded u8 value', (): void => {
|
||||
@@ -14,16 +15,25 @@ describe('compactFromU8a', (): void => {
|
||||
|
||||
it('decodes from same u16 encoded value', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([0b11111101, 0b00000111]))
|
||||
).toEqual([2, new BN(511)]);
|
||||
compactFromU8a(new Uint8Array([0b11111101, 0b00000111])).toString()
|
||||
).toEqual(
|
||||
[2, new BN(511)].toString()
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes from same u32 encoded value (short)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([254, 255, 3, 0]))
|
||||
compactFromU8a(new Uint8Array([254, 255, 3, 0])).toString()
|
||||
).toEqual(
|
||||
// since we use in-place, the words are different... HACK it
|
||||
[4, new BN(0xffff).ishln(16).ishrn(16)]
|
||||
[4, new BN(0xffff)].toString()
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes from same u32 encoded value (short)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(new Uint8Array([254, 255, 255, 255])).toString()
|
||||
).toEqual(
|
||||
[4, new BN(1073741823)].toString()
|
||||
);
|
||||
});
|
||||
|
||||
@@ -47,6 +57,14 @@ describe('compactFromU8a', (): void => {
|
||||
).toEqual([7, new BN('5af3107a4000', 16)]);
|
||||
});
|
||||
|
||||
it('decodes an actual value (Buffer)', (): void => {
|
||||
expect(
|
||||
compactFromU8a(
|
||||
Buffer.from('0b00407a10f35a', 'hex')
|
||||
)
|
||||
).toEqual([7, new BN('5af3107a4000', 16)]);
|
||||
});
|
||||
|
||||
it('decodes an actual value', (): void => {
|
||||
expect(
|
||||
compactFromU8a(
|
||||
@@ -54,4 +72,11 @@ describe('compactFromU8a', (): void => {
|
||||
)[1].toString()
|
||||
).toEqual('100000000');
|
||||
});
|
||||
|
||||
performance('compactFromU8a (u8)', 1_000_000, [[new Uint8Array([63 << 2])]], compactFromU8a);
|
||||
performance('compactFromU8a (u16)', 1_000_000, [[new Uint8Array([0b11111101, 0b00000111])]], compactFromU8a);
|
||||
performance('compactFromU8a (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8a);
|
||||
performance('compactFromU8aLim (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8aLim);
|
||||
performance('compactFromU8a (u48)', 1_000_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a);
|
||||
performance('compactFromU8a (u96)', 250_000, [[new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18])]], compactFromU8a);
|
||||
});
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
|
||||
import type { U8aLike } from '../types';
|
||||
|
||||
import { BN, BN_FOUR } from '../bn';
|
||||
import { BN } from '../bn';
|
||||
import { u8aToBn, u8aToU8a } from '../u8a';
|
||||
|
||||
/**
|
||||
* @name compactFromU8a
|
||||
* @description Retrievs the offset and encoded length from a compact-prefixed value
|
||||
* @description Retrives the offset and encoded length from a compact-prefixed value
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
@@ -22,22 +22,86 @@ import { u8aToBn, u8aToU8a } from '../u8a';
|
||||
*/
|
||||
export function compactFromU8a (input: U8aLike): [number, BN] {
|
||||
const u8a = u8aToU8a(input);
|
||||
const flag = u8a[0] & 0b11;
|
||||
|
||||
if (flag === 0b00) {
|
||||
return [1, new BN(u8a[0]).ishrn(2)];
|
||||
} else if (flag === 0b01) {
|
||||
return [2, u8aToBn(u8a.subarray(0, 2), true).ishrn(2)];
|
||||
} else if (flag === 0b10) {
|
||||
return [4, u8aToBn(u8a.subarray(0, 4), true).ishrn(2)];
|
||||
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
|
||||
// than doing an additional call to u8aToBn (as with variable length)
|
||||
switch (u8a[0] & 0b11) {
|
||||
case 0b00:
|
||||
return [1, new BN(u8a[0] >>> 2)];
|
||||
|
||||
case 0b01:
|
||||
return [2, new BN((u8a[0] + (u8a[1] * 0x1_00)) >>> 2)];
|
||||
|
||||
case 0b10:
|
||||
return [4, new BN((u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
|
||||
|
||||
// 0b11
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const offset = 1 + (
|
||||
new BN(u8a[0])
|
||||
.ishrn(2) // clear flag
|
||||
.iadd(BN_FOUR) // add 4 for base length
|
||||
.toNumber()
|
||||
);
|
||||
// add 5 to shifted (4 for base length, 1 for this byte)
|
||||
const offset = (u8a[0] >>> 2) + 5;
|
||||
|
||||
return [offset, u8aToBn(u8a.subarray(1, offset), true)];
|
||||
// we unroll the loop
|
||||
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] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00))];
|
||||
|
||||
case 6:
|
||||
return [6, new BN(u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00))];
|
||||
|
||||
// 6 bytes data is the maximum, 48 bits (56 would overflow)
|
||||
case 7:
|
||||
return [7, new BN(u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00) + (u8a[6] * 0x1_00_00_00_00_00))];
|
||||
|
||||
// for anything else, use the non-unrolled version
|
||||
default:
|
||||
return [offset, u8aToBn(u8a.subarray(1, offset))];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name compactFromU8aLim
|
||||
* @description A limited version of [[compactFromU8a]], accepting only Uint8Array inputs for values <= 48 bits
|
||||
*/
|
||||
export function compactFromU8aLim (u8a: Uint8Array): [number, number] {
|
||||
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
|
||||
// than doing an additional call to u8aToBn (as with variable length)
|
||||
switch (u8a[0] & 0b11) {
|
||||
case 0b00:
|
||||
return [1, u8a[0] >>> 2];
|
||||
|
||||
case 0b01:
|
||||
return [2, (u8a[0] + (u8a[1] * 0x1_00)) >>> 2];
|
||||
|
||||
case 0b10:
|
||||
return [4, (u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2];
|
||||
|
||||
// 0b11
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// 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:
|
||||
return [5, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00)];
|
||||
|
||||
case 6:
|
||||
return [6, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00)];
|
||||
|
||||
// 6 bytes data is the maximum, 48 bits (56 would overflow)
|
||||
case 7:
|
||||
return [7, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00) + (u8a[6] * 0x1_00_00_00_00_00)];
|
||||
|
||||
// for anything else, use the non-unrolled version
|
||||
default:
|
||||
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,5 +24,5 @@
|
||||
|
||||
export { compactAddLength } from './addLength';
|
||||
export { compactStripLength } from './stripLength';
|
||||
export { compactFromU8a } from './fromU8a';
|
||||
export { compactFromU8a, compactFromU8aLim } from './fromU8a';
|
||||
export { compactToU8a } from './toU8a';
|
||||
|
||||
@@ -61,7 +61,7 @@ describe('encode', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('encodes a large value', (): void => {
|
||||
it('encodes a large value (6 bytes)', (): void => {
|
||||
expect(
|
||||
compactToU8a(
|
||||
new BN('00005af3107a4000', 16)
|
||||
@@ -71,6 +71,16 @@ describe('encode', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('encodes a large value (9 bytes)', (): void => {
|
||||
expect(
|
||||
compactToU8a(
|
||||
new BN('1200005af3107a4034', 16)
|
||||
)
|
||||
).toEqual(
|
||||
new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18])
|
||||
);
|
||||
});
|
||||
|
||||
it('does not modify the original', (): void => {
|
||||
const original = new BN(123456);
|
||||
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { BN, BN_ONE, BN_TWO, bnToBn, bnToU8a } from '../bn';
|
||||
import { u8aConcat } from '../u8a';
|
||||
import { u8aConcatStrict } from '../u8a';
|
||||
|
||||
const MAX_U8 = BN_TWO.pow(new BN(8 - 2)).isub(BN_ONE);
|
||||
const MAX_U16 = BN_TWO.pow(new BN(16 - 2)).isub(BN_ONE);
|
||||
const MAX_U32 = BN_TWO.pow(new BN(32 - 2)).isub(BN_ONE);
|
||||
const BL_16 = { bitLength: 16 };
|
||||
const BL_32 = { bitLength: 32 };
|
||||
|
||||
/**
|
||||
* @name compactToU8a
|
||||
@@ -27,9 +29,9 @@ export function compactToU8a (value: BN | bigint | number): Uint8Array {
|
||||
if (bn.lte(MAX_U8)) {
|
||||
return new Uint8Array([bn.toNumber() << 2]);
|
||||
} else if (bn.lte(MAX_U16)) {
|
||||
return bnToU8a(bn.shln(2).iadd(BN_ONE), 16, true);
|
||||
return bnToU8a(bn.shln(2).iadd(BN_ONE), BL_16);
|
||||
} else if (bn.lte(MAX_U32)) {
|
||||
return bnToU8a(bn.shln(2).iadd(BN_TWO), 32, true);
|
||||
return bnToU8a(bn.shln(2).iadd(BN_TWO), BL_32);
|
||||
}
|
||||
|
||||
const u8a = bnToU8a(bn);
|
||||
@@ -42,9 +44,9 @@ export function compactToU8a (value: BN | bigint | number): Uint8Array {
|
||||
|
||||
assert(length >= 4, 'Invalid length, previous checks match anything less than 2^30');
|
||||
|
||||
return u8aConcat(
|
||||
return u8aConcatStrict([
|
||||
// subtract 4 as minimum (also catered for in decoding)
|
||||
[((length - 4) << 2) + 0b11],
|
||||
new Uint8Array([((length - 4) << 2) + 0b11]),
|
||||
u8a.subarray(0, length)
|
||||
);
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -44,6 +44,10 @@ interface Options {
|
||||
}
|
||||
|
||||
interface BalanceFormatter {
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options?: Options): string;
|
||||
/** @deprecated Use balanceFormat(input?: number | string | BN | bigint | ExtToBn, options?: Options) */
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options?: boolean, decimals?: number): string;
|
||||
/** @deprecated Use balanceFormat(input?: number | string | BN | bigint | ExtToBn, options?: Options) */
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options?: Options, decimals?: number): string;
|
||||
calcSi (text: string, decimals?: number): SiDef;
|
||||
findSi (type: string): SiDef;
|
||||
|
||||
@@ -9,7 +9,7 @@ export function formatDecimal (value: string): string {
|
||||
// we rather strip and re-add the negative sign in the output
|
||||
const isNegative = value[0].startsWith('-');
|
||||
const matched = isNegative
|
||||
? value.substr(1).match(NUMBER_REGEX)
|
||||
? value.substring(1).match(NUMBER_REGEX)
|
||||
: value.match(NUMBER_REGEX);
|
||||
|
||||
return matched
|
||||
|
||||
@@ -21,7 +21,7 @@ export function hexStripPrefix (value?: string | null): string {
|
||||
if (!value || value === '0x') {
|
||||
return '';
|
||||
} else if (REGEX_HEX_PREFIXED.test(value)) {
|
||||
return value.substr(2);
|
||||
return value.substring(2);
|
||||
} else if (REGEX_HEX_NOPREFIX.test(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ describe('hexToBn', (): void => {
|
||||
).toBe(256);
|
||||
});
|
||||
|
||||
it('handles backwards compatibility', (): void => {
|
||||
it('handles legacy compatibility', (): void => {
|
||||
expect(
|
||||
hexToBn('0x0001000000000000', false).eq(
|
||||
hexToBn('0x0001000000000000', { isLe: false })
|
||||
|
||||
@@ -8,8 +8,6 @@ import { isBoolean } from '../is/boolean';
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { hexStripPrefix } from './stripPrefix';
|
||||
|
||||
const DEFAULT_OPTS: ToBnOptions = { isLe: false, isNegative: false };
|
||||
|
||||
/**
|
||||
* @name hexToBn
|
||||
* @summary Creates a BN.js object from a hex string.
|
||||
@@ -28,12 +26,17 @@ const DEFAULT_OPTS: ToBnOptions = { isLe: false, isNegative: false };
|
||||
* hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
* ```
|
||||
*/
|
||||
export function hexToBn (value?: string | null, options: ToBnOptions | boolean = DEFAULT_OPTS): BN {
|
||||
function hexToBn (value?: string | null, options?: ToBnOptions): BN;
|
||||
/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
|
||||
function hexToBn (value?: string | null, options?: boolean): BN;
|
||||
/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
|
||||
function hexToBn (value?: string | null, options: ToBnOptions | boolean = {}): BN {
|
||||
if (!value || value === '0x') {
|
||||
return new BN(0);
|
||||
}
|
||||
|
||||
const { isLe, isNegative }: ToBnOptions = objectSpread(
|
||||
// For hex, default to BE
|
||||
const { isLe, isNegative } = objectSpread<ToBnOptions>(
|
||||
{ isLe: false, isNegative: false },
|
||||
isBoolean(options)
|
||||
? { isLe: options }
|
||||
@@ -43,8 +46,10 @@ export function hexToBn (value?: string | null, options: ToBnOptions | boolean =
|
||||
const bn = new BN(stripped, 16, isLe ? 'le' : 'be');
|
||||
|
||||
// fromTwos takes as parameter the number of bits, which is the hex length
|
||||
// multiplied by 4.
|
||||
// multiplied by 4 (2 bytes being 8 bits)
|
||||
return isNegative
|
||||
? bn.fromTwos(stripped.length * 4)
|
||||
: bn;
|
||||
}
|
||||
|
||||
export { hexToBn };
|
||||
|
||||
@@ -42,11 +42,13 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U
|
||||
const length = endLength - offset - mod;
|
||||
|
||||
for (let i = 0; i < length; i += 2) {
|
||||
dv.setUint16(i, HEX_TO_U16[value.substr(i * 2, 4)]);
|
||||
const idx = i * 2;
|
||||
|
||||
dv.setUint16(i, HEX_TO_U16[value.substring(idx, idx + 4)]);
|
||||
}
|
||||
|
||||
if (mod) {
|
||||
dv.setUint8(length, HEX_TO_U8[value.substr(value.length - 2, 2)]);
|
||||
dv.setUint8(length, HEX_TO_U8[value.substring(value.length - 2)]);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -10,9 +10,15 @@ describe('isBuffer', (): void => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Buffer values', (): void => {
|
||||
it('returns false on non-Buffer values (number)', (): void => {
|
||||
expect(
|
||||
isBuffer(0)
|
||||
isBuffer(123)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on non-Buffer values (null)', (): void => {
|
||||
expect(
|
||||
isBuffer(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hasBuffer } from '../has';
|
||||
import { isFunction } from './function';
|
||||
|
||||
/**
|
||||
* @name isBuffer
|
||||
@@ -18,5 +19,6 @@ import { hasBuffer } from '../has';
|
||||
* ```
|
||||
*/
|
||||
export function isBuffer (value: unknown): value is Buffer {
|
||||
return hasBuffer && Buffer.isBuffer(value);
|
||||
// we do check a function first, since it is slightly faster than isBuffer itself
|
||||
return hasBuffer && isFunction(value && (value as Buffer).readDoubleLE) && Buffer.isBuffer(value);
|
||||
}
|
||||
|
||||
@@ -12,12 +12,8 @@ interface Compact<T> {
|
||||
unwrap (): T;
|
||||
}
|
||||
|
||||
const checker = isOnObject('toBigInt', 'toBn', 'toNumber', 'unwrap');
|
||||
|
||||
/**
|
||||
* @name isCompact
|
||||
* @summary Tests for SCALE-Compact-like object instance.
|
||||
*/
|
||||
export function isCompact <T> (value?: unknown): value is Compact<T> {
|
||||
return checker(value);
|
||||
}
|
||||
export const isCompact: <T> (value?: unknown) => value is Compact<T> = isOnObject('toBigInt', 'toBn', 'toNumber', 'unwrap');
|
||||
|
||||
@@ -16,5 +16,8 @@
|
||||
* ```
|
||||
*/
|
||||
export function isError (value: unknown): value is Error {
|
||||
return value instanceof Error;
|
||||
return (
|
||||
((value && (value as Error).constructor) === Error) ||
|
||||
value instanceof Error
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,16 @@ describe('isInstanceOf', (): void => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on real instances (inherited)', (): void => {
|
||||
class Test extends Array {
|
||||
// default
|
||||
}
|
||||
|
||||
expect(
|
||||
isInstanceOf(new Test(2), Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-allocated instances', (): void => {
|
||||
expect(
|
||||
isInstanceOf([], Array)
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function isInstanceOf (value: unknown, clazz: Function): boolean {
|
||||
return value instanceof clazz;
|
||||
export function isInstanceOf (value: unknown, Clazz: Function): boolean {
|
||||
return (
|
||||
((value && (value as Record<string, unknown>).constructor) === Clazz) ||
|
||||
value instanceof Clazz
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { isU8a } from '.';
|
||||
|
||||
describe('isU8a', (): void => {
|
||||
@@ -10,15 +11,30 @@ describe('isU8a', (): void => {
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on null values', (): void => {
|
||||
expect(
|
||||
isU8a(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on Array values', (): void => {
|
||||
expect(
|
||||
isU8a([])
|
||||
isU8a([1, 2, 3])
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on Buffer values', (): void => {
|
||||
// under Node, Buffer implements Uint8Array
|
||||
expect(
|
||||
isU8a(Buffer.from([1, 2, 3]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on Uint8Array values', (): void => {
|
||||
expect(
|
||||
isU8a(new Uint8Array())
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
performance('isU8a', 2_000_000, [[new Uint8Array()]], isU8a);
|
||||
});
|
||||
|
||||
@@ -16,5 +16,10 @@
|
||||
* ```
|
||||
*/
|
||||
export function isU8a (value?: unknown): value is Uint8Array {
|
||||
return value instanceof Uint8Array;
|
||||
// here we defer the instanceof check which is actually slightly
|
||||
// slower than just checking the constrctor (direct instances)
|
||||
return (
|
||||
((value && (value as Uint8Array).constructor) === Uint8Array) ||
|
||||
value instanceof Uint8Array
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ function formatWithLength (maxLength: number): (v: unknown) => unknown {
|
||||
|
||||
return r.length < maxLength
|
||||
? v
|
||||
: `${r.substr(0, maxLength)} ...`;
|
||||
: `${r.substring(0, maxLength)} ...`;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,10 @@ function defaultGetId (): string {
|
||||
return 'none';
|
||||
}
|
||||
|
||||
/**
|
||||
* @name memoize
|
||||
* @description Memomize the function with a specific instanceId
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function memoize <T, F extends (...args: any[]) => T> (fn: F, { getInstanceId = defaultGetId }: Options = {}): Memoized<F> {
|
||||
const cache: Record<string, Record<string, T>> = {};
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name nextTick
|
||||
* @description Defer the operation to the queue for evaluation on the next tick
|
||||
*/
|
||||
export function nextTick (onExec: () => unknown, onError?: (error: Error) => unknown): void {
|
||||
Promise
|
||||
.resolve()
|
||||
.then((): void => {
|
||||
onExec();
|
||||
})
|
||||
.catch((error): void => {
|
||||
if (onError) {
|
||||
onError(error as Error);
|
||||
} else {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/util', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/util', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -109,5 +109,5 @@ describe('stringCamelCase', (): void => {
|
||||
).toEqual('aBCDef');
|
||||
});
|
||||
|
||||
performance('stringCamelCase', 64000, randomWords, stringCamelCase);
|
||||
performance('stringCamelCase', 100_000, randomWords, stringCamelCase);
|
||||
});
|
||||
|
||||
@@ -7,50 +7,59 @@ import type { AnyString } from '../types';
|
||||
//
|
||||
// this is not as optimal as the original answer (we split into multiple),
|
||||
// however it does pass the tests (which the original doesn't) and it is still
|
||||
// a 10+x improvement over the original camelcase npm package (at running)
|
||||
// a major improvement over the original camelcase npm package (at running)
|
||||
//
|
||||
// original: 20.88 μs/op
|
||||
// this: 2.86 μs/op
|
||||
// this: 1.20 μs/op
|
||||
//
|
||||
// Caveat of this: only Ascii, but acceptable for the intended usecase
|
||||
function converter (fn: (w: string, i: number) => string): (value: AnyString) => string {
|
||||
const format = (w: string, i: number) => fn(w[0], i) + w.slice(1);
|
||||
function converter (format: (w: string, i: number) => string): (value: AnyString) => string {
|
||||
const formatRuns = (w: string) => w.slice(0, w.length - 1).toLowerCase() + w.slice(-1).toUpperCase();
|
||||
|
||||
return (value: AnyString): string =>
|
||||
value
|
||||
return (value: AnyString): string => {
|
||||
const parts = value
|
||||
.toString()
|
||||
// replace all seperators (including consequtive) with spaces
|
||||
.replace(/[-_., ]+/g, ' ')
|
||||
// we don't want leading or trailing spaces
|
||||
.trim()
|
||||
// split into words
|
||||
.split(' ')
|
||||
.split(' ');
|
||||
let result = '';
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const w = parts[i];
|
||||
|
||||
// apply the formatting
|
||||
.map((w, i) =>
|
||||
format(
|
||||
w.toUpperCase() === w
|
||||
// all full uppercase + letters are changed to lowercase
|
||||
? w.toLowerCase()
|
||||
// all consecutive capitals + letters are changed to lowercase
|
||||
// e.g. UUID64 -> uuid64, while preserving splits, eg. NFTOrder -> nftOrder
|
||||
: w.replace(/^[A-Z0-9]{2,}[^a-z]/, (w) =>
|
||||
w.slice(0, w.length - 1).toLowerCase() + w.slice(-1).toUpperCase()
|
||||
),
|
||||
i
|
||||
)
|
||||
)
|
||||
// combine into a single word
|
||||
.join('');
|
||||
result += format(
|
||||
w.toUpperCase() === w
|
||||
// all full uppercase + letters are changed to lowercase
|
||||
? w.toLowerCase()
|
||||
// all consecutive capitals + letters are changed to lowercase
|
||||
// e.g. UUID64 -> uuid64, while preserving splits, eg. NFTOrder -> nftOrder
|
||||
: w.replace(/^[A-Z0-9]{2,}[^a-z]/, formatRuns),
|
||||
i
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @name stringCamelCase
|
||||
* @summary Convert a dash/dot/underscore/space separated Ascii string/String to camelCase
|
||||
*/
|
||||
export const stringCamelCase = converter((w, i) => i ? w.toUpperCase() : w.toLowerCase());
|
||||
export const stringCamelCase = converter((w, i) =>
|
||||
// lowercase for first letter/first word, else uppercase first, rest unchanged
|
||||
(i ? w[0].toUpperCase() : w[0].toLowerCase()) + w.slice(1)
|
||||
);
|
||||
|
||||
/**
|
||||
* @name stringPascalCase
|
||||
* @summary Convert a dash/dot/underscore/space separated Ascii string/String to PascalCase
|
||||
*/
|
||||
export const stringPascalCase = converter((w) => w.toUpperCase());
|
||||
export const stringPascalCase = converter((w) =>
|
||||
// uppercase the first character, leave the rest unchanged
|
||||
w[0].toUpperCase() + w.slice(1)
|
||||
);
|
||||
|
||||
@@ -15,4 +15,10 @@ describe('stringPascalCase', (): void => {
|
||||
stringPascalCase(String('foo_bar-- __baz Bob'))
|
||||
).toBe('FooBarBazBob');
|
||||
});
|
||||
|
||||
it('adjusts all-uppercase + digits', (): void => {
|
||||
expect(
|
||||
stringPascalCase('UUID64')
|
||||
).toEqual('Uuid64');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,5 +20,5 @@ import type { AnyString } from '../types';
|
||||
export function stringShorten (value: AnyString, prefixLength = 6): string {
|
||||
return value.length <= 2 + 2 * prefixLength
|
||||
? value.toString()
|
||||
: `${value.substr(0, prefixLength)}…${value.slice(-prefixLength)}`;
|
||||
: `${value.substring(0, prefixLength)}…${value.slice(-prefixLength)}`;
|
||||
}
|
||||
|
||||
@@ -6,10 +6,13 @@ import { stringToU8a } from '..';
|
||||
const RUSS_HELLO = new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33]);
|
||||
|
||||
describe('stringToU8a', (): void => {
|
||||
it('decodes to an empty string for undefined', (): void => {
|
||||
it('decodes to an empty string for undefined/null', (): void => {
|
||||
expect(
|
||||
stringToU8a()
|
||||
).toEqual(new Uint8Array());
|
||||
expect(
|
||||
stringToU8a(null)
|
||||
).toEqual(new Uint8Array());
|
||||
});
|
||||
|
||||
it('encodes the string correctly', (): void => {
|
||||
|
||||
@@ -21,8 +21,7 @@ const encoder = new TextEncoder();
|
||||
* stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function stringToU8a (value?: AnyString): Uint8Array {
|
||||
export function stringToU8a (value?: AnyString | null): Uint8Array {
|
||||
return value
|
||||
? encoder.encode(value.toString())
|
||||
: new Uint8Array();
|
||||
|
||||
@@ -6,6 +6,9 @@ import { formatDecimal, formatNumber } from '..';
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type ExecFn = (...params: any[]) => unknown;
|
||||
|
||||
const NUM_PAD = 16;
|
||||
const PRE_PAD = 32;
|
||||
|
||||
function loop (count: number, inputs: unknown[][], exec: ExecFn): [number, unknown[]] {
|
||||
const start = Date.now();
|
||||
const results = new Array<unknown>(inputs.length);
|
||||
@@ -32,8 +35,8 @@ export function formatOps (count: number, time: number): string {
|
||||
const ops = 1_000_000 / micro;
|
||||
|
||||
return `
|
||||
${formatFixed(ops).padStart(15 + 20)} ops/s
|
||||
${formatFixed(micro).padStart(15 + 20)} μs/op`;
|
||||
${formatFixed(ops).padStart(NUM_PAD + PRE_PAD + 1)} ops/s
|
||||
${formatFixed(micro).padStart(NUM_PAD + PRE_PAD + 1)} μs/op`;
|
||||
}
|
||||
|
||||
export function performance (name: string, count: number, inputs: unknown[][], exec: ExecFn): void {
|
||||
@@ -43,7 +46,7 @@ export function performance (name: string, count: number, inputs: unknown[][], e
|
||||
console.log(`
|
||||
performance run for ${name} completed with ${formatNumber(count)} iterations.
|
||||
|
||||
${`${name}:`.padStart(19)} ${time.toString().padStart(15)} ms${formatOps(count, time)}
|
||||
${`${name}:`.padStart(PRE_PAD)} ${time.toString().padStart(NUM_PAD)} ms${formatOps(count, time)}
|
||||
`);
|
||||
});
|
||||
}
|
||||
@@ -58,9 +61,9 @@ export function performanceCmp (name: string, [first, second]: [string, string],
|
||||
console.log(`
|
||||
performance run for ${name} completed with ${formatNumber(count)} iterations.
|
||||
|
||||
${`${first}:`.padStart(19)} ${ta.toString().padStart(15)} ms ${ta < tb ? '(fastest)' : `(slowest, ${(ta / tb).toFixed(2)}x)`}${formatOps(count, ta)}
|
||||
${`${first}:`.padStart(PRE_PAD)} ${ta.toString().padStart(NUM_PAD)} ms ${ta < tb ? '(fastest)' : `(slowest, ${(ta / tb).toFixed(2)}x)`}${formatOps(count, ta)}
|
||||
|
||||
${`${second}:`.padStart(19)} ${tb.toString().padStart(15)} ms ${ta > tb ? '(fastest)' : `(slowest, ${(tb / ta).toFixed(2)}x)`}${formatOps(count, tb)}
|
||||
${`${second}:`.padStart(PRE_PAD)} ${tb.toString().padStart(NUM_PAD)} ms ${ta > tb ? '(fastest)' : `(slowest, ${(tb / ta).toFixed(2)}x)`}${formatOps(count, tb)}
|
||||
`);
|
||||
|
||||
const unmatched = ra.filter((r, i) =>
|
||||
|
||||
@@ -5,7 +5,7 @@ import { getRandomValues } from '@polkadot/x-randomvalues';
|
||||
|
||||
import { arrayRange } from '../array';
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aConcat } from '.';
|
||||
import { u8aConcat, u8aConcatStrict } from '.';
|
||||
|
||||
const ptest = arrayRange(10).map(() => getRandomValues(new Uint8Array(32)));
|
||||
|
||||
@@ -34,5 +34,7 @@ describe('u8aConcat', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
performance('u8aConcat', 50000, [[ptest]], u8aConcat);
|
||||
performance('u8aConcat', 10000, [[ptest]], u8aConcat);
|
||||
performance('u8aConcatStrict', 10000, [[[ptest]]], u8aConcatStrict);
|
||||
performance('u8aConcatStrict (len)', 10000, [[[ptest], 320]], u8aConcatStrict);
|
||||
});
|
||||
|
||||
@@ -23,15 +23,30 @@ import { u8aToU8a } from './toU8a';
|
||||
* ```
|
||||
*/
|
||||
export function u8aConcat (...list: U8aLike[]): Uint8Array {
|
||||
let length = 0;
|
||||
let offset = 0;
|
||||
const u8as = new Array<Uint8Array>(list.length);
|
||||
let length = 0;
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
u8as[i] = u8aToU8a(list[i]);
|
||||
length += u8as[i].length;
|
||||
}
|
||||
|
||||
return u8aConcatStrict(u8as, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aConcatStrict
|
||||
* @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
|
||||
*/
|
||||
export function u8aConcatStrict (u8as: Uint8Array[], length = 0): Uint8Array {
|
||||
let offset = 0;
|
||||
|
||||
if (!length) {
|
||||
for (let i = 0; i < u8as.length; i++) {
|
||||
length += u8as[i].length;
|
||||
}
|
||||
}
|
||||
|
||||
const result = new Uint8Array(length);
|
||||
|
||||
for (let i = 0; i < u8as.length; i++) {
|
||||
|
||||
@@ -4,10 +4,20 @@
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aEq } from '.';
|
||||
|
||||
const ptest = new Uint8Array(32768);
|
||||
const ltest = new Uint8Array(32768);
|
||||
const stest = new Uint8Array(256);
|
||||
const ztest = new Uint8Array(64);
|
||||
|
||||
for (let i = 0; i < ptest.length; i++) {
|
||||
ptest[i] = i % 256;
|
||||
for (let i = 0; i < ltest.length; i++) {
|
||||
if (i < ztest.length) {
|
||||
ztest[i] = i % 256;
|
||||
}
|
||||
|
||||
if (i < stest.length) {
|
||||
stest[i] = i % 256;
|
||||
}
|
||||
|
||||
ltest[i] = i % 256;
|
||||
}
|
||||
|
||||
describe('u8aEq', (): void => {
|
||||
@@ -95,5 +105,7 @@ describe('u8aEq', (): void => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
performance('u8aEq (32k cmp)', 50000, [[ptest, ptest]], u8aEq);
|
||||
performance('u8aEq (64 cmp)', 200_000, [[ztest, ztest]], u8aEq);
|
||||
performance('u8aEq (256 cmp)', 200_000, [[stest, stest]], u8aEq);
|
||||
performance('u8aEq (32k cmp)', 10_000, [[ltest, ltest]], u8aEq);
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
export { u8aCmp } from './cmp';
|
||||
export { u8aConcat } from './concat';
|
||||
export { u8aConcat, u8aConcatStrict } from './concat';
|
||||
export { u8aEmpty } from './empty';
|
||||
export { u8aEq } from './eq';
|
||||
export { u8aFixLength } from './fixLength';
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aToBigInt } from '.';
|
||||
|
||||
describe('u8aToBigInt', (): void => {
|
||||
@@ -56,4 +57,6 @@ describe('u8aToBigInt', (): void => {
|
||||
)
|
||||
).toEqual(-1234n);
|
||||
});
|
||||
|
||||
performance('u8aToBigInt', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBigInt);
|
||||
});
|
||||
|
||||
@@ -55,7 +55,7 @@ export function u8aToBigInt (value: Uint8Array, options: ToBnOptions = {}): bigi
|
||||
return BigInt(0);
|
||||
}
|
||||
|
||||
const { isLe, isNegative }: ToBnOptions = objectSpread({ isLe: true, isNegative: false }, options);
|
||||
const { isLe, isNegative } = objectSpread<ToBnOptions>({ isLe: true, isNegative: false }, options);
|
||||
const u8a = isLe
|
||||
? value.reverse()
|
||||
: value;
|
||||
|
||||
@@ -1,9 +1,25 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aToBn } from '.';
|
||||
|
||||
describe('u8aToBn', (): void => {
|
||||
it('supports legacy compatibility', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34]),
|
||||
true
|
||||
).toString(16)
|
||||
).toBe('3412');
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34]),
|
||||
false
|
||||
).toString(16)
|
||||
).toBe('1234');
|
||||
});
|
||||
|
||||
it('converts little-endian by default', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
@@ -11,6 +27,7 @@ describe('u8aToBn', (): void => {
|
||||
).toString(16)
|
||||
).toBe('3412');
|
||||
});
|
||||
|
||||
it('converts values (big-endian)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
@@ -20,7 +37,7 @@ describe('u8aToBn', (): void => {
|
||||
).toBe('1234');
|
||||
});
|
||||
|
||||
it('converts values (little-endian)', (): void => {
|
||||
it('converts values (little-endian, u16)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34]),
|
||||
@@ -29,13 +46,69 @@ describe('u8aToBn', (): void => {
|
||||
).toBe('3412');
|
||||
});
|
||||
|
||||
it('converts empty', (): void => {
|
||||
it('converts values (little-endian, u32)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(),
|
||||
new Uint8Array([0x12, 0x34, 0x56, 0x78]),
|
||||
{ isLe: true }
|
||||
).toString(16)
|
||||
).toBe('0');
|
||||
).toBe('78563412');
|
||||
});
|
||||
|
||||
it('converts values (big-endian, u16)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34]),
|
||||
{ isLe: false }
|
||||
).toString(16)
|
||||
).toBe('1234');
|
||||
});
|
||||
|
||||
it('converts values (big-endian, u32)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34, 0x56, 0x78]),
|
||||
{ isLe: false }
|
||||
).toString(16)
|
||||
).toBe('12345678');
|
||||
});
|
||||
|
||||
describe('empty creation', (): void => {
|
||||
it('handles unsigned (le)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(),
|
||||
{ isLe: true }
|
||||
).toString(16)
|
||||
).toBe('0');
|
||||
});
|
||||
|
||||
it('handles signed (le)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(),
|
||||
{ isLe: true, isNegative: true }
|
||||
).toString(16)
|
||||
).toBe('0');
|
||||
});
|
||||
|
||||
it('handles unsigned (be)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(),
|
||||
{ isLe: false }
|
||||
).toString(16)
|
||||
).toBe('0');
|
||||
});
|
||||
|
||||
it('handles signed (be)', (): void => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array(),
|
||||
{ isLe: false, isNegative: true }
|
||||
).toString(16)
|
||||
).toBe('0');
|
||||
});
|
||||
});
|
||||
|
||||
it('handles negative numbers (little-endian)', (): void => {
|
||||
@@ -89,4 +162,7 @@ describe('u8aToBn', (): void => {
|
||||
)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
performance('u8aToBn (u32)', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
||||
performance('u8aToBn (u64)', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
|
||||
});
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '../bn/bn';
|
||||
import type { ToBnOptions } from '../types';
|
||||
|
||||
import { hexToBn } from '../hex/toBn';
|
||||
import { u8aToHex } from './toHex';
|
||||
|
||||
const DEFAULT_OPTS: ToBnOptions = { isLe: true, isNegative: false };
|
||||
import { BN } from '../bn/bn';
|
||||
import { isBoolean } from '../is/boolean';
|
||||
import { objectSpread } from '../object/spread';
|
||||
|
||||
/**
|
||||
* @name u8aToBn
|
||||
@@ -16,7 +14,7 @@ const DEFAULT_OPTS: ToBnOptions = { isLe: true, isNegative: false };
|
||||
* `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
|
||||
* @param value The value to convert
|
||||
* @param options Options to pass while converting
|
||||
* @param options.isLe Convert using Little Endian
|
||||
* @param options.isLe Convert using Little Endian (default)
|
||||
* @param options.isNegative Convert using two's complement
|
||||
* @example
|
||||
* <BR>
|
||||
@@ -27,9 +25,44 @@ const DEFAULT_OPTS: ToBnOptions = { isLe: true, isNegative: false };
|
||||
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
* ```
|
||||
*/
|
||||
export function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = DEFAULT_OPTS): BN {
|
||||
return hexToBn(
|
||||
u8aToHex(value),
|
||||
options
|
||||
function u8aToBn (value: Uint8Array, options?: ToBnOptions): BN;
|
||||
/** @deprecated Use u8aToBn(value: Uint8Array, options?: ToBnOptions) */
|
||||
function u8aToBn (value: Uint8Array, options?: boolean): BN;
|
||||
/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
|
||||
function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = {}): BN {
|
||||
// NOTE: This is the same process as followed in the hexToBn conversion
|
||||
// For Uint8Array, default to LE
|
||||
const { isLe, isNegative } = objectSpread<ToBnOptions>(
|
||||
{ isLe: true, isNegative: false },
|
||||
isBoolean(options)
|
||||
? { isLe: options }
|
||||
: options
|
||||
);
|
||||
const count = value.length;
|
||||
let bn: BN;
|
||||
|
||||
// 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;
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
result = (result * 0x1_00) + (
|
||||
isLe
|
||||
? value[count - 1 - i]
|
||||
: value[i]
|
||||
);
|
||||
}
|
||||
|
||||
bn = new BN(result);
|
||||
} else {
|
||||
bn = new BN(value, isLe ? 'le' : 'be');
|
||||
}
|
||||
|
||||
return isNegative && value.length
|
||||
? bn.fromTwos(value.length * 8)
|
||||
: bn;
|
||||
}
|
||||
|
||||
export { u8aToBn };
|
||||
|
||||
@@ -5,10 +5,15 @@ import { performanceCmp } from '../test/performance';
|
||||
import { u8aToHex as u8aToHexBuffer } from './toHexBuffer';
|
||||
import { u8aToHex } from '.';
|
||||
|
||||
const ptest = new Uint8Array(32768);
|
||||
const ptest32k = new Uint8Array(32768);
|
||||
const ptest256 = new Uint8Array(256);
|
||||
|
||||
for (let i = 0; i < ptest.length; i++) {
|
||||
ptest[i] = i % 256;
|
||||
for (let i = 0; i < ptest32k.length; i++) {
|
||||
if (i < ptest256.length) {
|
||||
ptest256[1] = i % 256;
|
||||
}
|
||||
|
||||
ptest32k[i] = i % 256;
|
||||
}
|
||||
|
||||
describe('u8aToHex', (): void => {
|
||||
@@ -66,7 +71,19 @@ describe('u8aToHex', (): void => {
|
||||
).toEqual('0x8000…0c0d');
|
||||
});
|
||||
|
||||
performanceCmp('u8aToHex', ['u8aToHexBuffer', 'u8aToHex'], 2000, [[ptest]], (s: Uint8Array, isSecond) =>
|
||||
it('converts known bytes to their correct values', (): void => {
|
||||
expect(
|
||||
// hello world
|
||||
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]))
|
||||
).toEqual('0x68656c6c6f20776f726c64');
|
||||
});
|
||||
|
||||
performanceCmp('u8aToHex (32k)', ['u8aToHexBuffer', 'u8aToHex'], 2000, [[ptest32k]], (s: Uint8Array, isSecond) =>
|
||||
isSecond
|
||||
? u8aToHex(s)
|
||||
: u8aToHexBuffer(s)
|
||||
);
|
||||
performanceCmp('u8aToHex (128)', ['u8aToHexBuffer', 'u8aToHex'], 100000, [[ptest256]], (s: Uint8Array, isSecond) =>
|
||||
isSecond
|
||||
? u8aToHex(s)
|
||||
: u8aToHexBuffer(s)
|
||||
|
||||
@@ -13,6 +13,8 @@ function hex (value: Uint8Array): string {
|
||||
let result = '';
|
||||
|
||||
for (let i = 0; i < length; i += 2) {
|
||||
// we only use getUint16 here instead of getUint32 - at least in our
|
||||
// tests this is faster to execute (both long & short strings tested)
|
||||
result += U16_TO_HEX[dv.getUint16(i)];
|
||||
}
|
||||
|
||||
@@ -43,7 +45,7 @@ export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed
|
||||
return `${isPrefixed ? '0x' : ''}${
|
||||
!value || !value.length
|
||||
? ''
|
||||
: (length > 0 && value.length > length)
|
||||
: (bitLength > 0 && value.length > length)
|
||||
? `${hex(value.subarray(0, length / 2))}…${hex(value.subarray(value.length - length / 2))}`
|
||||
: hex(value)
|
||||
}` as HexString;
|
||||
|
||||
@@ -23,7 +23,7 @@ export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed
|
||||
return `${isPrefixed ? '0x' : ''}${
|
||||
!value || !value.length
|
||||
? ''
|
||||
: (length > 0 && value.length > length)
|
||||
: (bitLength > 0 && value.length > length)
|
||||
? `${Buffer.from(value.subarray(0, length / 2)).toString('hex')}…${Buffer.from(value.subarray(value.length - length / 2)).toString('hex')}`
|
||||
: Buffer.from(value).toString('hex')
|
||||
}` as HexString;
|
||||
|
||||
@@ -20,7 +20,7 @@ const decoder = new TextDecoder('utf-8');
|
||||
* ```
|
||||
*/
|
||||
export function u8aToString (value?: Uint8Array | null): string {
|
||||
return !value?.length
|
||||
? ''
|
||||
: decoder.decode(value);
|
||||
return value
|
||||
? decoder.decode(value)
|
||||
: '';
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aToU8a } from '.';
|
||||
|
||||
describe('u8aToU8a', (): void => {
|
||||
@@ -40,14 +41,17 @@ describe('u8aToU8a', (): void => {
|
||||
|
||||
it('creates from a Buffer', (): void => {
|
||||
expect(
|
||||
u8aToU8a(Buffer.from([1, 2, 3, 128, 0, 10, 11, 12]))
|
||||
u8aToU8a(Buffer.from([1, 2, 3, 128, 0, 10, 11, 12])).buffer
|
||||
).toEqual(
|
||||
new Uint8Array([1, 2, 3, 128, 0, 10, 11, 12])
|
||||
new Uint8Array([1, 2, 3, 128, 0, 10, 11, 12]).buffer
|
||||
);
|
||||
});
|
||||
|
||||
it('creates from a Buffer (hex)', (): void => {
|
||||
expect(
|
||||
u8aToU8a(Buffer.from('80000a', 'hex'))
|
||||
u8aToU8a(Buffer.from('80000a', 'hex')).buffer
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0, 10])
|
||||
new Uint8Array([128, 0, 10]).buffer
|
||||
);
|
||||
});
|
||||
|
||||
@@ -65,4 +69,8 @@ describe('u8aToU8a', (): void => {
|
||||
u8aToU8a(123 as unknown as Uint8Array)
|
||||
).toEqual(new Uint8Array([49, 50, 51]));
|
||||
});
|
||||
|
||||
performance('u8aToU8a (hex)', 250_000, [['0x1234']], u8aToU8a);
|
||||
performance('u8aToU8a (str)', 250_000, [['test']], u8aToU8a);
|
||||
performance('u8aToU8a (u8a)', 1_000_000, [[new Uint8Array()]], u8aToU8a);
|
||||
});
|
||||
|
||||
@@ -25,13 +25,11 @@ import { stringToU8a } from '../string/toU8a';
|
||||
* ```
|
||||
*/
|
||||
export function u8aToU8a (value?: U8aLike | null): Uint8Array {
|
||||
return !value
|
||||
? new Uint8Array()
|
||||
: Array.isArray(value) || isBuffer(value)
|
||||
? new Uint8Array(value)
|
||||
: isU8a(value)
|
||||
? value
|
||||
: isHex(value)
|
||||
? hexToU8a(value)
|
||||
: stringToU8a(value);
|
||||
return isU8a(value)
|
||||
? value
|
||||
: isHex(value)
|
||||
? hexToU8a(value)
|
||||
: isBuffer(value) || Array.isArray(value)
|
||||
? new Uint8Array(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type { U8aLike } from '../types';
|
||||
|
||||
import { u8aConcat } from './concat';
|
||||
import { u8aConcatStrict } from './concat';
|
||||
import { u8aEq } from './eq';
|
||||
import { u8aToU8a } from './toU8a';
|
||||
|
||||
@@ -45,5 +45,5 @@ export function u8aWrapBytes (bytes: U8aLike): Uint8Array {
|
||||
// if Ethereum-wrapping, we don't add our wrapping bytes
|
||||
return u8aIsWrapped(u8a, true)
|
||||
? u8a
|
||||
: u8aConcat(U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX);
|
||||
: u8aConcatStrict([U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX]);
|
||||
}
|
||||
|
||||
@@ -20,9 +20,9 @@
|
||||
"./shim.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/x-global": "9.1.1"
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/x-global": "9.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/x-bigint', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/x-bigint', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/util": "9.1.1",
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/util": "9.4.1",
|
||||
"buffer-es6": "^4.9.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/x-bundle', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/x-bundle', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -17,13 +17,13 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/x-global": "9.1.1",
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/x-global": "9.4.1",
|
||||
"@types/node-fetch": "^2.6.1",
|
||||
"node-fetch": "^2.6.7"
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/x-fetch', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/x-fetch', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9"
|
||||
"@babel/runtime": "^7.18.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ export const xglobal = (
|
||||
);
|
||||
|
||||
export function extractGlobal <N extends GlobalNames, T extends GlobalType<N>> (name: N, fallback: unknown): T {
|
||||
// Not quite sure why this is here - snuck in with TS 4.7.2 with no real idea
|
||||
// (as of now) as to why this looks like an "any" when we do cast it to a T
|
||||
//
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return typeof xglobal[name as 'undefined'] === 'undefined'
|
||||
? fallback as T
|
||||
: xglobal[name as 'undefined'] as unknown as T;
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/x-global', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/x-global', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/x-global": "9.1.1"
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/x-global": "9.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react-native": "^0.67.6"
|
||||
"@types/react-native": "^0.67.8"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/x-randomvalues', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/x-randomvalues', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/x-global": "9.1.1"
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/x-global": "9.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Do not edit, auto-generated by @polkadot/dev
|
||||
|
||||
export const packageInfo = { name: '@polkadot/x-textdecoder', path: 'auto', type: 'auto', version: '9.1.1' };
|
||||
export const packageInfo = { name: '@polkadot/x-textdecoder', path: 'auto', type: 'auto', version: '9.4.1' };
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "9.1.1",
|
||||
"version": "9.4.1",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.17.9",
|
||||
"@polkadot/x-global": "9.1.1"
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"@polkadot/x-global": "9.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user