Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5bfa8c3c4 | ||
|
|
359981d2ef | ||
|
|
b0ee79f90c | ||
|
|
c2d595fb3f | ||
|
|
f837200a73 | ||
|
|
ea7dd553a2 | ||
|
|
7c1ca3a5c9 | ||
|
|
75cd547b49 | ||
|
|
91c4c407a9 | ||
|
|
20b46a65fa | ||
|
|
c60459e303 | ||
|
|
92ab4699b3 | ||
|
|
932f4c25f5 | ||
|
|
aaa35a8198 |
+1
-1
@@ -1 +1 @@
|
||||
4
|
||||
5
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
# 2.5.1 Feb 22, 2020
|
||||
|
||||
- Add `isBigInt(value)` to allow `BigInt` checks. Also add support in `bnToBn` as well as number conversion utils
|
||||
- Add `isChildClass(Parent, Child?)` to check if a class extends the parent, asserting Child is Parent
|
||||
- (internal) Use `#<varname>` insead of `private _<varname>` for private class variables
|
||||
|
||||
# 2.4.1 Feb 18, 2020
|
||||
|
||||
- Ensure that `formatBalance` does not apply unit overrides apply when no SI is applicable
|
||||
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
# checkout latest
|
||||
git fetch
|
||||
git checkout gh-pages
|
||||
git pull
|
||||
git checkout --orphan gh-pages-temp
|
||||
|
||||
# cleanup
|
||||
rm -rf node_modules
|
||||
rm -rf coverage
|
||||
rm -rf packages
|
||||
rm -rf test
|
||||
|
||||
# add
|
||||
git add -A
|
||||
git commit -am "refresh history"
|
||||
|
||||
# danger, force new
|
||||
git branch -D gh-pages
|
||||
git branch -m gh-pages
|
||||
git push -f origin gh-pages
|
||||
+1
-1
@@ -9,5 +9,5 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "2.4.1"
|
||||
"version": "2.5.1"
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "2.4.1",
|
||||
"version": "2.5.1",
|
||||
"private": true,
|
||||
"engines": {
|
||||
"yarn": "^1.10.1"
|
||||
@@ -8,7 +8,7 @@
|
||||
"packages/*"
|
||||
],
|
||||
"resolutions": {
|
||||
"typescript": "^3.7.5"
|
||||
"typescript": "^3.8.2"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-ts",
|
||||
@@ -21,7 +21,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.8.4",
|
||||
"@polkadot/dev": "^0.40.18",
|
||||
"@polkadot/dev": "^0.41.2",
|
||||
"@polkadot/ts": "^0.2.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/keyring",
|
||||
"version": "2.4.1",
|
||||
"version": "2.5.1",
|
||||
"description": "Keyring management",
|
||||
"main": "index.js",
|
||||
"publishConfig": {
|
||||
@@ -28,7 +28,7 @@
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.8.4",
|
||||
"@polkadot/util": "^2.4.1",
|
||||
"@polkadot/util-crypto": "^2.4.1"
|
||||
"@polkadot/util": "2.5.1",
|
||||
"@polkadot/util-crypto": "2.5.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@ import Pairs from './pairs';
|
||||
* an account may be obtained using `toJson` accompanied by the account passphrase.
|
||||
*/
|
||||
export default class Keyring implements KeyringInstance {
|
||||
private _pairs: Pairs;
|
||||
readonly #pairs: Pairs;
|
||||
|
||||
private _ss58?: number;
|
||||
readonly #type: KeypairType;
|
||||
|
||||
private _type: KeypairType;
|
||||
#ss58?: number;
|
||||
|
||||
public decodeAddress = decodeAddress;
|
||||
|
||||
@@ -42,9 +42,9 @@ export default class Keyring implements KeyringInstance {
|
||||
|
||||
assert(options && ['ed25519', 'sr25519'].includes(options.type || 'undefined'), `Expected a keyring type of either 'ed25519' or 'sr25519', found '${options.type}`);
|
||||
|
||||
this._pairs = new Pairs();
|
||||
this._ss58 = options.ss58Format;
|
||||
this._type = options.type;
|
||||
this.#pairs = new Pairs();
|
||||
this.#ss58 = options.ss58Format;
|
||||
this.#type = options.type;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +65,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* @description Returns the type of the keyring, either ed25519 of sr25519
|
||||
*/
|
||||
public get type (): KeypairType {
|
||||
return this._type;
|
||||
return this.#type;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +73,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
||||
*/
|
||||
public addPair (pair: KeyringPair): KeyringPair {
|
||||
return this._pairs.add(pair);
|
||||
return this.#pairs.add(pair);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +188,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* @description Encodes the input into an ss58 representation
|
||||
*/
|
||||
public encodeAddress = (key: Uint8Array | string, ss58Format?: number): string => {
|
||||
return encodeAddress(key, ss58Format || this._ss58);
|
||||
return encodeAddress(key, ss58Format || this.#ss58);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,7 +198,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* a key lookup using the provided account address or public key (after decoding it).
|
||||
*/
|
||||
public getPair (address: string | Uint8Array): KeyringPair {
|
||||
return this._pairs.get(address);
|
||||
return this.#pairs.get(address);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,7 +207,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
|
||||
*/
|
||||
public getPairs (): KeyringPair[] {
|
||||
return this._pairs.all();
|
||||
return this.#pairs.all();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -216,7 +216,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* @description Returns an array list of all the public keys associated with each of the keyring pair values that are stored in the keyring pair dictionary.
|
||||
*/
|
||||
public getPublicKeys (): Uint8Array[] {
|
||||
return this._pairs
|
||||
return this.#pairs
|
||||
.all()
|
||||
.map(({ publicKey }): Uint8Array =>
|
||||
publicKey
|
||||
@@ -228,7 +228,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
|
||||
*/
|
||||
public removePair (address: string | Uint8Array): void {
|
||||
this._pairs.remove(address);
|
||||
this.#pairs.remove(address);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -236,7 +236,7 @@ export default class Keyring implements KeyringInstance {
|
||||
* @description Sets the ss58 format for the keyring
|
||||
*/
|
||||
public setSS58Format (ss58: number): void {
|
||||
this._ss58 = ss58;
|
||||
this.#ss58 = ss58;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,6 +248,6 @@ export default class Keyring implements KeyringInstance {
|
||||
* may backup their account to a JSON file that contains this information.
|
||||
*/
|
||||
public toJson (address: string | Uint8Array, passphrase?: string): KeyringPair$Json {
|
||||
return this._pairs.get(address).toJson(passphrase);
|
||||
return this.#pairs.get(address).toJson(passphrase);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,24 +10,20 @@ import { decodeAddress } from '@polkadot/util-crypto';
|
||||
type KeyringPairMap = Record<string, KeyringPair>;
|
||||
|
||||
export default class Pairs implements KeyringPairs {
|
||||
private _map: KeyringPairMap;
|
||||
|
||||
constructor () {
|
||||
this._map = {};
|
||||
}
|
||||
readonly #map: KeyringPairMap = {};
|
||||
|
||||
public add (pair: KeyringPair): KeyringPair {
|
||||
this._map[pair.publicKey.toString()] = pair;
|
||||
this.#map[pair.publicKey.toString()] = pair;
|
||||
|
||||
return pair;
|
||||
}
|
||||
|
||||
public all (): KeyringPair[] {
|
||||
return Object.values(this._map);
|
||||
return Object.values(this.#map);
|
||||
}
|
||||
|
||||
public get (address: string | Uint8Array): KeyringPair {
|
||||
const pair = this._map[decodeAddress(address).toString()];
|
||||
const pair = this.#map[decodeAddress(address).toString()];
|
||||
|
||||
assert(pair, (): string => {
|
||||
const formatted: string = isU8a(address) || isHex(address)
|
||||
@@ -41,6 +37,6 @@ export default class Pairs implements KeyringPairs {
|
||||
}
|
||||
|
||||
public remove (address: string | Uint8Array): void {
|
||||
delete this._map[decodeAddress(address).toString()];
|
||||
delete this.#map[decodeAddress(address).toString()];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-crypto",
|
||||
"version": "2.4.1",
|
||||
"version": "2.5.1",
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -26,7 +26,7 @@
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.8.4",
|
||||
"@polkadot/util": "^2.4.1",
|
||||
"@polkadot/util": "2.5.1",
|
||||
"@polkadot/wasm-crypto": "^1.0.1",
|
||||
"base-x": "^3.0.7",
|
||||
"bip39": "^3.0.2",
|
||||
|
||||
@@ -16,9 +16,9 @@ const BN_OPTIONS = {
|
||||
};
|
||||
|
||||
export default class DeriveJunction {
|
||||
private _chainCode: Uint8Array = new Uint8Array(32);
|
||||
readonly #chainCode: Uint8Array = new Uint8Array(32);
|
||||
|
||||
private _isHard = false;
|
||||
#isHard = false;
|
||||
|
||||
public static from (value: string): DeriveJunction {
|
||||
const [code, isHard] = value.startsWith('/')
|
||||
@@ -38,15 +38,15 @@ export default class DeriveJunction {
|
||||
}
|
||||
|
||||
public get chainCode (): Uint8Array {
|
||||
return this._chainCode;
|
||||
return this.#chainCode;
|
||||
}
|
||||
|
||||
public get isHard (): boolean {
|
||||
return this._isHard;
|
||||
return this.#isHard;
|
||||
}
|
||||
|
||||
public get isSoft (): boolean {
|
||||
return !this._isHard;
|
||||
return !this.#isHard;
|
||||
}
|
||||
|
||||
public hard (value: number | BN | string | Uint8Array): DeriveJunction {
|
||||
@@ -54,7 +54,7 @@ export default class DeriveJunction {
|
||||
}
|
||||
|
||||
public harden (): DeriveJunction {
|
||||
this._isHard = true;
|
||||
this.#isHard = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -72,14 +72,14 @@ export default class DeriveJunction {
|
||||
return this.soft(blake2AsU8a(value));
|
||||
}
|
||||
|
||||
this._chainCode.fill(0);
|
||||
this._chainCode.set(value, 0);
|
||||
this.#chainCode.fill(0);
|
||||
this.#chainCode.set(value, 0);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public soften (): DeriveJunction {
|
||||
this._isHard = false;
|
||||
this.#isHard = false;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -6,37 +6,32 @@ import keyExtractPath, { ExtractResult } from './extractPath';
|
||||
|
||||
describe('keyExtractPath', (): void => {
|
||||
it('extracts properly from soft', (): void => {
|
||||
expect(
|
||||
keyExtractPath('/1')
|
||||
).toEqual({
|
||||
parts: ['/1'],
|
||||
path: [
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
]
|
||||
});
|
||||
const test = keyExtractPath('/1');
|
||||
|
||||
expect(test.parts).toEqual(['/1']);
|
||||
expect(test.path.length).toEqual(1);
|
||||
expect(test.path[0].isHard).toEqual(false);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('extracts properly from hard', (): void => {
|
||||
expect(
|
||||
keyExtractPath('//1')
|
||||
).toEqual({
|
||||
parts: ['//1'],
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
]
|
||||
});
|
||||
const test = keyExtractPath('//1');
|
||||
|
||||
expect(test.parts).toEqual(['//1']);
|
||||
expect(test.path.length).toEqual(1);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('extracts properly from hard/soft', (): void => {
|
||||
expect(
|
||||
keyExtractPath('//1/2')
|
||||
).toEqual({
|
||||
parts: ['//1', '/2'],
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
]
|
||||
});
|
||||
const test = keyExtractPath('//1/2');
|
||||
|
||||
expect(test.parts).toEqual(['//1', '/2']);
|
||||
expect(test.path.length).toEqual(2);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
expect(test.path[1].isHard).toEqual(false);
|
||||
expect(test.path[1].chainCode).toEqual(Uint8Array.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('does not extract from invalid paths', (): void => {
|
||||
|
||||
@@ -12,126 +12,115 @@ describe('keyExtractSuri', (): void => {
|
||||
});
|
||||
|
||||
it('derives on "hello world"', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world')
|
||||
).toEqual({
|
||||
path: [],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world');
|
||||
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(0);
|
||||
});
|
||||
|
||||
it('derives on "hello world/1', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world/1')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world/1');
|
||||
|
||||
expect(test.password).not.toBeDefined();
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(1);
|
||||
expect(test.path[0].isHard).toEqual(false);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on "hello world/DOT', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world/DOT')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world/DOT');
|
||||
|
||||
expect(test.password).not.toBeDefined();
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(1);
|
||||
expect(test.path[0].isHard).toEqual(false);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on "hello world//1', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world//1')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world//1');
|
||||
|
||||
expect(test.password).not.toBeDefined();
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(1);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on "hello world//DOT', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world//DOT')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world//DOT');
|
||||
|
||||
expect(test.password).not.toBeDefined();
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(1);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on "hello world//1/DOT', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world//1/DOT')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world//1/DOT');
|
||||
|
||||
expect(test.password).not.toBeDefined();
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(2);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
expect(test.path[1].isHard).toEqual(false);
|
||||
expect(test.path[1].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on "hello world//DOT/1', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world//DOT/1')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world//DOT/1');
|
||||
|
||||
expect(test.password).not.toBeDefined();
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(2);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
expect(test.path[1].isHard).toEqual(false);
|
||||
expect(test.path[1].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on "hello world///password"', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world///password')
|
||||
).toEqual({
|
||||
password: 'password',
|
||||
path: [],
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world///password');
|
||||
|
||||
expect(test.password).toEqual('password');
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(0);
|
||||
});
|
||||
|
||||
it('derives on "hello world//1/DOT///password"', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world//1/DOT///password')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
password: 'password',
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world//1/DOT///password');
|
||||
|
||||
expect(test.password).toEqual('password');
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(2);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
expect(test.path[1].isHard).toEqual(false);
|
||||
expect(test.path[1].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on "hello world/1//DOT///password"', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('hello world/1//DOT///password')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
password: 'password',
|
||||
phrase: 'hello world'
|
||||
});
|
||||
const test = keyExtractSuri('hello world/1//DOT///password');
|
||||
|
||||
expect(test.password).toEqual('password');
|
||||
expect(test.phrase).toEqual('hello world');
|
||||
expect(test.path.length).toEqual(2);
|
||||
expect(test.path[0].isHard).toEqual(false);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
expect(test.path[1].isHard).toEqual(true);
|
||||
expect(test.path[1].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('derives on actual Alice', (): void => {
|
||||
expect(
|
||||
keyExtractSuri('bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice')
|
||||
).toEqual({
|
||||
path: [
|
||||
{ _isHard: true, _chainCode: Uint8Array.from([20, 65, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
|
||||
],
|
||||
phrase: 'bottom drive obey lake curtain smoke basket hold race lonely fit walk'
|
||||
});
|
||||
const test = keyExtractSuri('bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice');
|
||||
|
||||
expect(test.password).not.toBeDefined();
|
||||
expect(test.phrase).toEqual('bottom drive obey lake curtain smoke basket hold race lonely fit walk');
|
||||
expect(test.path.length).toEqual(1);
|
||||
expect(test.path[0].isHard).toEqual(true);
|
||||
expect(test.path[0].chainCode).toEqual(Uint8Array.from([20, 65, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "2.4.1",
|
||||
"version": "2.5.1",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
|
||||
@@ -19,6 +19,12 @@ describe('bnToBn', (): void => {
|
||||
).toEqual(128);
|
||||
});
|
||||
|
||||
it('converts BigInt values to BN', (): void => {
|
||||
expect(
|
||||
bnToBn(128_821n).toNumber()
|
||||
).toEqual(128821);
|
||||
});
|
||||
|
||||
it('converts number values to BN', (): void => {
|
||||
expect(
|
||||
bnToBn(128).toNumber()
|
||||
|
||||
@@ -6,6 +6,7 @@ import { ToBn } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import isBigInt from '../is/bigInt';
|
||||
import isToBn from '../is/toBn';
|
||||
import isHex from '../is/hex';
|
||||
import hexToBn from '../hex/toBn';
|
||||
@@ -26,11 +27,13 @@ import hexToBn from '../hex/toBn';
|
||||
* bnToBn(new BN(0x1234)); // => BN(0x1234)
|
||||
* ```
|
||||
*/
|
||||
export default function bnToBn <ExtToBn extends ToBn> (value?: ExtToBn | BN | string | number | null): BN {
|
||||
export default function bnToBn <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | string | number | null): BN {
|
||||
if (!value) {
|
||||
return new BN(0);
|
||||
} else if (isHex(value)) {
|
||||
return hexToBn(value.toString());
|
||||
} else if (isBigInt(value)) {
|
||||
return new BN(value.toString());
|
||||
}
|
||||
|
||||
return BN.isBN(value)
|
||||
|
||||
@@ -30,10 +30,7 @@ interface Options extends ToBnOptions {
|
||||
* bnToHex(new BN(0x123456)); // => '0x123456'
|
||||
* ```
|
||||
*/
|
||||
export default function bnToHex (
|
||||
value?: BN | number | null,
|
||||
options: number | Options = { bitLength: -1, isLe: false, isNegative: false }
|
||||
): string {
|
||||
export default function bnToHex (value?: BN | BigInt | number | null, options: number | Options = { bitLength: -1, isLe: false, isNegative: false }): string {
|
||||
if (!value) {
|
||||
return ZERO_STR;
|
||||
}
|
||||
|
||||
@@ -26,13 +26,9 @@ interface Options extends ToBnOptions {
|
||||
* bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
|
||||
* ```
|
||||
*/
|
||||
function bnToU8a (value: BN | number | null, options?: Options): Uint8Array;
|
||||
function bnToU8a (value: BN | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
|
||||
function bnToU8a (
|
||||
value: BN | number | null,
|
||||
arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false },
|
||||
arg2?: boolean
|
||||
): Uint8Array {
|
||||
function bnToU8a (value: BN | BigInt | number | null, options?: Options): Uint8Array;
|
||||
function bnToU8a (value: BN | BigInt | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
|
||||
function bnToU8a (value: BN | BigInt | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array {
|
||||
const _options: Options = {
|
||||
isLe: true,
|
||||
isNegative: false,
|
||||
|
||||
@@ -24,7 +24,7 @@ const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
|
||||
* console.log(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
|
||||
* ```
|
||||
*/
|
||||
export default function compactToU8a (_value: BN | number): Uint8Array {
|
||||
export default function compactToU8a (_value: BN | BigInt | number): Uint8Array {
|
||||
const value = bnToBn(_value);
|
||||
|
||||
if (value.lte(MAX_U8)) {
|
||||
|
||||
@@ -21,6 +21,12 @@ describe('formatBalance', (): void => {
|
||||
).toEqual('123.456µ Unit');
|
||||
});
|
||||
|
||||
it('formats BigInt numbers', (): void => {
|
||||
expect(
|
||||
formatBalance(123_456_789_000n, { withSi: true, decimals: 15 })
|
||||
).toEqual('123.456µ Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15, old style)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { withSi: true }, 15)
|
||||
|
||||
@@ -26,7 +26,7 @@ interface Options {
|
||||
}
|
||||
|
||||
interface BalanceFormatter {
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, options?: Options, decimals?: number): string;
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | BigInt | ExtToBn, options?: Options, decimals?: number): string;
|
||||
calcSi (text: string, decimals?: number): SiDef;
|
||||
findSi (type: string): SiDef;
|
||||
getDefaults (): Defaults;
|
||||
@@ -41,7 +41,7 @@ let defaultDecimals = DEFAULT_DECIMALS;
|
||||
let defaultUnit = DEFAULT_UNIT;
|
||||
|
||||
// Formats a string/number with <prefix>.<postfix><type> notation
|
||||
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, options: Options | boolean = true, optDecimals: number = defaultDecimals): string {
|
||||
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | BigInt | ExtToBn, options: Options | boolean = true, optDecimals: number = defaultDecimals): string {
|
||||
let text = bnToBn(input).toString();
|
||||
|
||||
if (text.length === 0 || text === '0') {
|
||||
|
||||
@@ -25,6 +25,12 @@ describe('formatNumber', (): void => {
|
||||
).toEqual('12,345');
|
||||
});
|
||||
|
||||
it('formats BigInt numbers', (): void => {
|
||||
expect(
|
||||
formatNumber(123_456_789n)
|
||||
).toEqual('123,456,789');
|
||||
});
|
||||
|
||||
it('formats Compact<BN>', (): void => {
|
||||
expect(
|
||||
formatNumber({
|
||||
|
||||
@@ -9,6 +9,6 @@ import BN from 'bn.js';
|
||||
import bnToBn from '../bn/toBn';
|
||||
import formatDecimal from './formatDecimal';
|
||||
|
||||
export default function formatNumber <ExtToBn extends ToBn> (value?: ExtToBn | BN | number | null): string {
|
||||
export default function formatNumber <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null): string {
|
||||
return formatDecimal(bnToBn(value).toString());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { isBigInt } from '.';
|
||||
|
||||
describe('isBigInt', (): void => {
|
||||
it('returns true when a BigInt value', (): void => {
|
||||
expect(
|
||||
isBigInt(123_456n)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true when a BigInt value', (): void => {
|
||||
expect(
|
||||
isBigInt(BigInt(123_456))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-BigInt values', (): void => {
|
||||
expect(
|
||||
isBigInt(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
/**
|
||||
* @name isBigInt
|
||||
* @summary Tests for a `BigInt` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `BigInt`
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isBigInt } from '@polkadot/util';
|
||||
*
|
||||
* console.log('isBigInt', isBigInt(123_456n)); // => true
|
||||
* ```
|
||||
*/
|
||||
export default function isBigInt (value: BigInt | object | string | number | undefined | null): value is BigInt {
|
||||
return typeof value === 'bigint';
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import { isChildClass } from '.';
|
||||
|
||||
describe('isChildClass', (): void => {
|
||||
it('returns true when a Child value', (): void => {
|
||||
expect(
|
||||
isChildClass(BN, class Test extends BN {})
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Child values', (): void => {
|
||||
expect(
|
||||
isChildClass(BN, Uint8Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on undefined', (): void => {
|
||||
expect(
|
||||
isChildClass(BN)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('does TS magic, determining type', (): void => {
|
||||
const T = (class extends BN {}) as unknown;
|
||||
|
||||
if (isChildClass(BN, T)) {
|
||||
expect(new T(12345).toNumber()).toEqual(12345);
|
||||
} else {
|
||||
throw new Error('Not a child class');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '../types';
|
||||
|
||||
/**
|
||||
* @name isChildClass
|
||||
* @summary Tests if the child extends the parent Class
|
||||
* @description
|
||||
* Checks to see if the child Class extends the parent Class
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { isChildClass } from '@polkadot/util';
|
||||
*
|
||||
* console.log('isChildClass', isChildClass(BN, BN); // => true
|
||||
* console.log('isChildClass', isChildClass(BN, Uint8Array); // => false
|
||||
* ```
|
||||
*/
|
||||
export default function isChildClass <P extends Constructor> (Parent: P, Child?: Constructor | null | unknown): Child is P {
|
||||
// https://stackoverflow.com/questions/30993434/check-if-a-constructor-inherits-another-in-es6/30993664
|
||||
return Child
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
? Parent === Child || Parent.isPrototypeOf(Child as Constructor)
|
||||
: false;
|
||||
}
|
||||
@@ -5,10 +5,11 @@
|
||||
/**
|
||||
* @summary Type checking utilities
|
||||
*/
|
||||
|
||||
export { default as isBigInt } from './bigInt';
|
||||
export { default as isBn } from './bn';
|
||||
export { default as isBuffer } from './buffer';
|
||||
export { default as isBoolean } from './boolean';
|
||||
export { default as isChildClass } from './childClass';
|
||||
export { default as isError } from './error';
|
||||
export { default as isFunction } from './function';
|
||||
export { default as isHex } from './hex';
|
||||
|
||||
@@ -4,6 +4,12 @@
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export interface Constructor<T = any> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
new(...value: any[]): T;
|
||||
}
|
||||
|
||||
export interface ToBn {
|
||||
toBn: () => BN;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user