Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efa6661817 | ||
|
|
c988d5011c | ||
|
|
f173bb3d2a | ||
|
|
1f0368061f | ||
|
|
45ef924862 | ||
|
|
dec88b6ab7 | ||
|
|
ed402341dc | ||
|
|
76a9f2787c | ||
|
|
5b1c9b8ccb | ||
|
|
af751c6c89 | ||
|
|
4ab8dd70bd | ||
|
|
a5f59ca926 | ||
|
|
71f26aac8d | ||
|
|
cc7d6a2e01 | ||
|
|
e29f0a3920 | ||
|
|
f38975d126 | ||
|
|
dc4b59bb0c | ||
|
|
2f5df085b6 | ||
|
|
225ca1de00 | ||
|
|
aeb500e804 | ||
|
|
e9dd6f1efc | ||
|
|
563dbbf7e4 | ||
|
|
5a5bc7da0b | ||
|
|
fb2c51a49b | ||
|
|
568e3b7840 | ||
|
|
87d69d7098 | ||
|
|
255cd93bf4 | ||
|
|
ef5780996e | ||
|
|
5155ae8d2b | ||
|
|
f1447ffcbc | ||
|
|
64e9ec5044 | ||
|
|
05986f6d64 | ||
|
|
caca69525a | ||
|
|
845398846c | ||
|
|
412ca6ccd7 | ||
|
|
9232fd5a91 | ||
|
|
49174d181e | ||
|
|
c5bc09fde7 | ||
|
|
1a436906f5 | ||
|
|
d7bb55c003 | ||
|
|
50f015a713 | ||
|
|
1c94508d61 | ||
|
|
ddb007de7b | ||
|
|
8bee7d5d6b | ||
|
|
253eea9249 | ||
|
|
502fdc1e32 | ||
|
|
f3aaaede25 | ||
|
|
309d691938 | ||
|
|
4d6438c3e7 | ||
|
|
19817b213b | ||
|
|
20d4457483 | ||
|
|
a2d0f2b1a3 | ||
|
|
a512766bfb | ||
|
|
b11b42e2b6 | ||
|
|
c13c2022ca | ||
|
|
bf1c187969 | ||
|
|
16d077c87d | ||
|
|
00374effc5 | ||
|
|
891a855fb8 | ||
|
|
854cb56621 | ||
|
|
1018a11548 | ||
|
|
dc55f2170f | ||
|
|
36cd5cd504 | ||
|
|
a5d23696e6 | ||
|
|
5adc753f81 | ||
|
|
5e494b7163 |
+1
-1
@@ -1 +1 @@
|
||||
1
|
||||
2
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
version: 2
|
||||
jobs:
|
||||
build:
|
||||
working_directory: ~/polkadot-js
|
||||
docker:
|
||||
- image: circleci/node:10
|
||||
steps:
|
||||
- checkout
|
||||
- restore_cache:
|
||||
key: dependency-cache-{{ checksum "yarn.lock" }}
|
||||
- run:
|
||||
name: install-deps
|
||||
command: yarn install --frozen-lockfile
|
||||
- save_cache:
|
||||
key: dependency-cache-{{ checksum "yarn.lock" }}
|
||||
paths:
|
||||
- ./node_modules
|
||||
- ~/.cache/yarn
|
||||
- run:
|
||||
name: build
|
||||
command: yarn polkadot-dev-circleci-build
|
||||
+1
-1
@@ -5,7 +5,7 @@ module.exports = {
|
||||
parserOptions: {
|
||||
...base.parserOptions,
|
||||
project: [
|
||||
'./tsconfig.eslint.json'
|
||||
'./tsconfig.json'
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
name: PR
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
name: Linting
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: lint
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn lint
|
||||
|
||||
test:
|
||||
name: Testing
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: test
|
||||
env:
|
||||
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn test
|
||||
|
||||
build_code:
|
||||
name: Build Code
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: build
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn build
|
||||
|
||||
build_docs:
|
||||
name: Build Docs
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: build
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn docs
|
||||
@@ -0,0 +1,67 @@
|
||||
name: Master
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build_code:
|
||||
name: Build Code
|
||||
if: "! contains(github.event.head_commit.message, '[CI Skip]')"
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
with:
|
||||
token: ${{ secrets.GH_PAT }}
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: build
|
||||
env:
|
||||
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
||||
GH_PAT: ${{ secrets.GH_PAT }}
|
||||
GH_TOKEN: ${{ secrets.GH_PAT }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn polkadot-dev-ghact-build
|
||||
|
||||
build_docs:
|
||||
name: Build Docs
|
||||
if: "! contains(github.event.head_commit.message, '[CI Skip]')"
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: build
|
||||
env:
|
||||
GH_PAT: ${{ secrets.GH_PAT }}
|
||||
GH_TOKEN: ${{ secrets.GH_PAT }}
|
||||
GH_PAGES_SRC: build-docs
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn polkadot-dev-ghact-docs
|
||||
|
||||
dummy:
|
||||
name: Dummy
|
||||
if: "contains(github.event.head_commit.message, '[CI Skip]')"
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12.x]
|
||||
steps:
|
||||
- name: dummy
|
||||
run: |
|
||||
echo "Dummy skip step"
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "10"
|
||||
env:
|
||||
global:
|
||||
- CC_TEST_REPORTER_ID=27a299582eb7fd9580e27897a6ecda1c82a1cd24bcfe2f2ebda1ca70238509b4
|
||||
- GH_PAGES_SRC="build-docs"
|
||||
cache:
|
||||
yarn: true
|
||||
directories:
|
||||
- node_modules
|
||||
before_install:
|
||||
- curl -o- -L https://yarnpkg.com/install.sh | bash
|
||||
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
||||
- chmod +x ./cc-test-reporter
|
||||
- export PATH=$HOME/.yarn/bin:$(yarn global bin):$PATH
|
||||
before_script:
|
||||
- ./cc-test-reporter before-build
|
||||
script:
|
||||
- yarn polkadot-dev-travis-build
|
||||
after_script:
|
||||
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
||||
deploy:
|
||||
skip_cleanup: true
|
||||
provider: script
|
||||
script: yarn polkadot-dev-travis-deploy
|
||||
on:
|
||||
branch: master
|
||||
@@ -1,3 +1,31 @@
|
||||
# 1.7.1
|
||||
|
||||
- `formatBalance` signature now allows to force unit displays
|
||||
- Keypair now allows for derive on a pair, with `pair.derive(...)`
|
||||
- `assert` now acts like a real asserts, with no return - signature adapted for new TS 3.7
|
||||
|
||||
# 1.6.1
|
||||
|
||||
- `pair.sign(message)` now takes an optional second `options?: SignOptions` parameter, where `SignOptions` is currently defined as `{ withType: boolean }`. Here `withType` instructs the signing to prepend the type of signature (ed2551, sr25519 or ecdsa). This is used for the new Polkadot/Substrate MultiSignature format.
|
||||
|
||||
# 1.5.1
|
||||
|
||||
- Fix typings on the Keyring-exposed encode & decodeAddress
|
||||
- Allow ss58Format = 0 as a valid value
|
||||
- Loosen type definitions for allowed ss58format values (still checked against allowed array)
|
||||
|
||||
# 1.4.1
|
||||
|
||||
- **Breaking change** To set the `ss58Format`, you should now use `setSS58Format` as opposed to the old `setAddressPrefix`
|
||||
- Renamed `keyring.setAddressPrefix` to `keyring.setSS58Format`
|
||||
- Deprecated `addressPrefix` on the keyring options, added the `ss58Format` as a replacement (aligning with chain properties - the old version is still accepted)
|
||||
- Added `stringToHex` and `hexToString` conversion utilities to `@polkadot/util`
|
||||
- Swap to [Babel 7.6.0](https://babeljs.io/) for all compilation, for latest improvements in code generation
|
||||
|
||||
# 1.3.1
|
||||
|
||||
- Remove the `ExtError` class, always prefer the standard JS `Error` object for errors. This would bre a breaking change for any applications using `ExtError`
|
||||
|
||||
# 1.2.1
|
||||
|
||||
- Keyring `addFromAddress` can now be used to store external accounts, for example those provided by external signers
|
||||
|
||||
+1
-1
@@ -9,5 +9,5 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "1.2.1"
|
||||
"version": "1.7.1"
|
||||
}
|
||||
|
||||
+7
-7
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"private": true,
|
||||
"engines": {
|
||||
"yarn": "^1.10.1"
|
||||
@@ -9,11 +9,11 @@
|
||||
],
|
||||
"resolutions": {
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"typescript": "^3.6.2"
|
||||
"typescript": "^3.7.2"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-ts && polkadot-dev-build-docs",
|
||||
"check": "yarn lint",
|
||||
"build": "polkadot-dev-build-ts",
|
||||
"docs": "polkadot-dev-build-docs",
|
||||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx . && tsc --noEmit --pretty",
|
||||
"clean": "polkadot-dev-clean-build",
|
||||
"postinstall": "polkadot-dev-yarn-only",
|
||||
@@ -21,9 +21,9 @@
|
||||
"test:one": "jest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.5.5",
|
||||
"@polkadot/dev": "^0.31.0-beta.7",
|
||||
"@polkadot/ts": "^0.1.71",
|
||||
"@babel/core": "^7.7.4",
|
||||
"@polkadot/dev": "^0.32.0-beta.15",
|
||||
"@polkadot/ts": "^0.1.86",
|
||||
"gh-pages": "^2.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/chainspec",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "Contains chain specifications for known chains",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,7 +25,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/chainspec#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/util": "^1.2.1"
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/util": "^1.7.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/db",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "Implementation of a basic sync in-memory and on-disk databases with overlays",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,9 +25,9 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/db#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/trie-hash": "^1.2.1",
|
||||
"@polkadot/util": "^1.2.1",
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/trie-hash": "^1.7.1",
|
||||
"@polkadot/util": "^1.7.1",
|
||||
"lru_map": "^0.3.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ import TransactionDb from './engines/TransactionDb';
|
||||
* ```
|
||||
*/
|
||||
export default class Memory extends TransactionDb {
|
||||
public constructor () {
|
||||
constructor () {
|
||||
super(new MemoryDb());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ export default class LruDb implements BaseDb {
|
||||
|
||||
private lru: LRUMap<string, CachedValue>;
|
||||
|
||||
public constructor (backing: BaseDb, itemCount: number = DEFAULT_ITEM_COUNT) {
|
||||
constructor (backing: BaseDb, itemCount: number = DEFAULT_ITEM_COUNT) {
|
||||
this.backing = backing;
|
||||
this.lru = new LRUMap(itemCount);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ export default class TransactionDb implements TxDb {
|
||||
|
||||
private txStarted: boolean;
|
||||
|
||||
public constructor (backing: BaseDb) {
|
||||
constructor (backing: BaseDb) {
|
||||
this.backing = backing;
|
||||
this.txStarted = false;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/keyring",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "Keyring management",
|
||||
"main": "index.js",
|
||||
"publishConfig": {
|
||||
@@ -27,8 +27,8 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/util": "^1.2.1",
|
||||
"@polkadot/util-crypto": "^1.2.1"
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/util": "^1.7.1",
|
||||
"@polkadot/util-crypto": "^1.7.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { cryptoWaitReady, setAddressPrefix } from '@polkadot/util-crypto';
|
||||
import { cryptoWaitReady, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
import Keyring from '.';
|
||||
|
||||
@@ -25,6 +25,10 @@ describe('keypair', (): void => {
|
||||
keypair.addFromSeed(seedOne, {});
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
setSS58Format(42);
|
||||
});
|
||||
|
||||
it('adds the pair', (): void => {
|
||||
expect(
|
||||
keypair.addFromSeed(seedTwo, {}).publicKey
|
||||
@@ -40,7 +44,7 @@ describe('keypair', (): void => {
|
||||
});
|
||||
|
||||
it('adds from a mnemonic', (): void => {
|
||||
setAddressPrefix(68);
|
||||
setSS58Format(68);
|
||||
|
||||
expect(
|
||||
keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address
|
||||
@@ -93,6 +97,10 @@ describe('keypair', (): void => {
|
||||
keypair.addFromSeed(seedOne, {});
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
setSS58Format(42);
|
||||
});
|
||||
|
||||
it('creates with dev phrase when only path specified', (): void => {
|
||||
expect(
|
||||
keypair.createFromUri('//Alice').address
|
||||
@@ -106,7 +114,7 @@ describe('keypair', (): void => {
|
||||
});
|
||||
|
||||
it('adds from a mnemonic', (): void => {
|
||||
setAddressPrefix(68);
|
||||
setSS58Format(68);
|
||||
|
||||
expect(
|
||||
keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { assertSingletonPackage } from '@polkadot/util';
|
||||
import { decodeAddress, encodeAddress, setAddressPrefix } from '@polkadot/util-crypto';
|
||||
import { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
import Keyring from './keyring';
|
||||
|
||||
@@ -15,5 +15,5 @@ export {
|
||||
Keyring,
|
||||
decodeAddress,
|
||||
encodeAddress,
|
||||
setAddressPrefix
|
||||
setSS58Format
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringOptions } from './types';
|
||||
|
||||
import { assert, hexToU8a, isNumber, isHex, stringToU8a } from '@polkadot/util';
|
||||
import { decodeAddress, encodeAddress, keyExtractSuri, keyFromPath, naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, mnemonicToMiniSecret, setAddressPrefix } from '@polkadot/util-crypto';
|
||||
import { decodeAddress, encodeAddress, keyExtractSuri, keyFromPath, naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, mnemonicToMiniSecret, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
import { DEV_PHRASE } from './defaults';
|
||||
import createPair from './pair';
|
||||
@@ -37,17 +37,22 @@ export default class Keyring implements KeyringInstance {
|
||||
|
||||
public encodeAddress = encodeAddress;
|
||||
|
||||
public setAddressPrefix = setAddressPrefix;
|
||||
public setSS58Format = setSS58Format;
|
||||
|
||||
public constructor (options: KeyringOptions = {}) {
|
||||
constructor (options: KeyringOptions = {}) {
|
||||
options.type = options.type || 'ed25519';
|
||||
options.ss58Format = isNumber(options.ss58Format)
|
||||
? options.addressPrefix
|
||||
: options.ss58Format;
|
||||
|
||||
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._type = options.type;
|
||||
|
||||
setAddressPrefix(isNumber(options.addressPrefix) ? options.addressPrefix : 42);
|
||||
if (isNumber(options.ss58Format)) {
|
||||
setSS58Format(options.ss58Format);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,10 +14,9 @@ type DecodeResult = PairInfo & {
|
||||
secretKey: Uint8Array;
|
||||
};
|
||||
|
||||
export default function decode (passphrase?: string, _encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(_encrypted, 'No encrypted data available to decode');
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(encrypted, 'No encrypted data available to decode');
|
||||
|
||||
const encrypted = (_encrypted as Uint8Array);
|
||||
const encoded = passphrase
|
||||
? (naclDecrypt(
|
||||
encrypted.subarray(NONCE_LENGTH),
|
||||
|
||||
@@ -14,7 +14,7 @@ export default function encode ({ publicKey, secretKey }: PairInfo, passphrase?:
|
||||
|
||||
const encoded = u8aConcat(
|
||||
PKCS8_HEADER,
|
||||
secretKey as Uint8Array,
|
||||
secretKey,
|
||||
PKCS8_DIVIDER,
|
||||
publicKey
|
||||
);
|
||||
|
||||
@@ -2,14 +2,19 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { setAddressPrefix } from '@polkadot/util-crypto';
|
||||
import { cryptoWaitReady, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
import { PAIRS } from '../testing';
|
||||
import testingPairs from '../testingPairs';
|
||||
import createPair from '.';
|
||||
|
||||
const keyring = testingPairs({ type: 'ed25519' }, false);
|
||||
|
||||
describe('pair', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await cryptoWaitReady();
|
||||
});
|
||||
|
||||
const SIGNATURE = new Uint8Array([80, 191, 198, 147, 225, 207, 75, 88, 126, 39, 129, 109, 191, 38, 72, 181, 75, 254, 81, 143, 244, 79, 237, 38, 236, 141, 28, 252, 134, 26, 169, 234, 79, 33, 153, 158, 151, 34, 175, 188, 235, 20, 35, 135, 83, 120, 139, 211, 233, 130, 1, 208, 201, 215, 73, 80, 56, 98, 185, 196, 11, 8, 193, 14]);
|
||||
|
||||
it('has a publicKey', (): void => {
|
||||
@@ -61,11 +66,13 @@ describe('pair', (): void => {
|
||||
'5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua'
|
||||
);
|
||||
|
||||
setAddressPrefix(68);
|
||||
setSS58Format(68);
|
||||
|
||||
expect(keyring.alice.address).toEqual(
|
||||
'7sGUeMak588SPY2YMmmuKUuLz7u2WQpf74F9dCFtSLB2td9d'
|
||||
);
|
||||
|
||||
setSS58Format(42);
|
||||
});
|
||||
|
||||
it('allows getting public key after decoding', (): void => {
|
||||
@@ -77,4 +84,13 @@ describe('pair', (): void => {
|
||||
|
||||
expect(pair.isLocked).toEqual(false);
|
||||
});
|
||||
|
||||
it('allows derivation on the pair', (): void => {
|
||||
const alice = createPair('sr25519', { publicKey: PAIRS[0].publicKey, secretKey: PAIRS[0].secretKey }, {});
|
||||
const stash = alice.derive('//stash');
|
||||
const soft = alice.derive('//funding/0');
|
||||
|
||||
expect(stash.publicKey).toEqual(PAIRS[1].publicKey);
|
||||
expect(soft.address).toEqual('5ECQNn7UueWHPFda5qUi4fTmTtyCnPvGnuoyVVSj5CboJh9J');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,15 +3,21 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Keypair, KeypairType } from '@polkadot/util-crypto/types';
|
||||
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '../types';
|
||||
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } from '../types';
|
||||
import { PairInfo } from './types';
|
||||
|
||||
import { encodeAddress, naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify } from '@polkadot/util-crypto';
|
||||
import { u8aConcat, assert } from '@polkadot/util';
|
||||
import { encodeAddress, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify } from '@polkadot/util-crypto';
|
||||
|
||||
import decode from './decode';
|
||||
import encode from './encode';
|
||||
import toJson from './toJson';
|
||||
|
||||
const SIG_TYPE_NONE = new Uint8Array();
|
||||
const SIG_TYPE_ED25519 = new Uint8Array([0]);
|
||||
const SIG_TYPE_SR25519 = new Uint8Array([1]);
|
||||
// const SIG_TYPE_ECDSA = new Uint8Array([2]);
|
||||
|
||||
function isEmpty (u8a: Uint8Array): boolean {
|
||||
return u8a.reduce((count, u8): number => count + u8, 0) === 0;
|
||||
}
|
||||
@@ -24,10 +30,24 @@ const fromSeed = (type: KeypairType, seed: Uint8Array): Keypair =>
|
||||
? schnorrkelFromSeed(seed)
|
||||
: naclFromSeed(seed);
|
||||
|
||||
const sign = (type: KeypairType, message: Uint8Array, pair: Partial<Keypair>): Uint8Array =>
|
||||
isSr25519(type)
|
||||
? schnorrkelSign(message, pair)
|
||||
: naclSign(message, pair);
|
||||
const sign = (type: KeypairType, message: Uint8Array, pair: Partial<Keypair>, { withType = false }: SignOptions = {}): Uint8Array => {
|
||||
const typeSr25519 = isSr25519(type);
|
||||
|
||||
return u8aConcat(
|
||||
// for multi-signatures, i.e. with indicator, append the signature type as per
|
||||
// the MultiSignature enum
|
||||
withType
|
||||
? (
|
||||
typeSr25519
|
||||
? SIG_TYPE_SR25519
|
||||
: SIG_TYPE_ED25519
|
||||
)
|
||||
: SIG_TYPE_NONE,
|
||||
typeSr25519
|
||||
? schnorrkelSign(message, pair)
|
||||
: naclSign(message, pair)
|
||||
);
|
||||
};
|
||||
|
||||
const verify = (type: KeypairType, message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean =>
|
||||
isSr25519(type)
|
||||
@@ -67,10 +87,21 @@ const verify = (type: KeypairType, message: Uint8Array, signature: Uint8Array, p
|
||||
*/
|
||||
export default function createPair (type: KeypairType, { publicKey, secretKey }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null): KeyringPair {
|
||||
return {
|
||||
type,
|
||||
get address (): string {
|
||||
return encodeAddress(publicKey);
|
||||
},
|
||||
get meta (): KeyringPair$Meta {
|
||||
return meta;
|
||||
},
|
||||
get isLocked (): boolean {
|
||||
return !secretKey || secretKey.length === 0 || isEmpty(secretKey);
|
||||
},
|
||||
get publicKey (): Uint8Array {
|
||||
return publicKey;
|
||||
},
|
||||
get type (): KeypairType {
|
||||
return type;
|
||||
},
|
||||
decodePkcs8: (passphrase?: string, _encoded?: Uint8Array | null): void => {
|
||||
const decoded = decode(passphrase, _encoded || encoded);
|
||||
|
||||
@@ -84,25 +115,24 @@ export default function createPair (type: KeypairType, { publicKey, secretKey }:
|
||||
secretKey = pair.secretKey;
|
||||
}
|
||||
},
|
||||
derive: (suri: string, meta?: KeyringPair$Meta): KeyringPair => {
|
||||
assert(secretKey, 'Cannot derive on a locked keypair');
|
||||
|
||||
const { path } = keyExtractPath(suri);
|
||||
const derived = keyFromPath({ publicKey, secretKey: secretKey }, path, type);
|
||||
|
||||
return createPair(type, derived, meta, null);
|
||||
},
|
||||
encodePkcs8: (passphrase?: string): Uint8Array =>
|
||||
encode({ publicKey, secretKey }, passphrase),
|
||||
get meta (): KeyringPair$Meta {
|
||||
return meta;
|
||||
},
|
||||
get isLocked (): boolean {
|
||||
return !secretKey || secretKey.length === 0 || isEmpty(secretKey);
|
||||
},
|
||||
lock: (): void => {
|
||||
secretKey = new Uint8Array(0);
|
||||
},
|
||||
get publicKey (): Uint8Array {
|
||||
return publicKey;
|
||||
},
|
||||
setMeta: (additional: KeyringPair$Meta): void => {
|
||||
meta = { ...meta, ...additional };
|
||||
},
|
||||
sign: (message: Uint8Array): Uint8Array =>
|
||||
sign(type, message, { publicKey, secretKey }),
|
||||
sign: (message: Uint8Array, options?: SignOptions): Uint8Array =>
|
||||
sign(type, message, { publicKey, secretKey }, options),
|
||||
toJson: (passphrase?: string): KeyringPair$Json =>
|
||||
toJson(type, { meta, publicKey }, encode({ publicKey, secretKey }, passphrase), !!passphrase),
|
||||
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
|
||||
|
||||
@@ -24,13 +24,16 @@ const json: KeyringPair$Json = {
|
||||
};
|
||||
|
||||
export default function everybody (): KeyringPair {
|
||||
return {
|
||||
const pair: KeyringPair = {
|
||||
type: 'ed25519',
|
||||
address,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
decodePkcs8: (passphrase?: string, encoded?: Uint8Array): void =>
|
||||
undefined,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
derive: (suri: string, meta?: KeyringPair$Meta): KeyringPair =>
|
||||
pair,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
encodePkcs8: (passphrase?: string): Uint8Array =>
|
||||
new Uint8Array(0),
|
||||
meta,
|
||||
@@ -52,4 +55,6 @@ export default function everybody (): KeyringPair {
|
||||
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
|
||||
false
|
||||
};
|
||||
|
||||
return pair;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ type KeyringPairMap = Record<string, KeyringPair>;
|
||||
export default class Pairs implements KeyringPairs {
|
||||
private _map: KeyringPairMap;
|
||||
|
||||
public constructor () {
|
||||
constructor () {
|
||||
this._map = {};
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ interface PairDef {
|
||||
// keyring is for testing - what happens is that in most cases the keyring is initialises
|
||||
// before anything else. Since the sr25519 crypto is async, this creates problems with
|
||||
// adding the keys when only the keyring is used.
|
||||
const PAIRS: PairDef[] = [
|
||||
export const PAIRS: PairDef[] = [
|
||||
{
|
||||
seed: 'Alice',
|
||||
secretKey: hexToU8a('0x98319d4ff8a9508c4bb0cf0b5a78d760a0b2082c02775e6e82370816fedfff48925a225d97aa00682d6a59b95b18780c10d7032336e88f3442b42361f4a66011'),
|
||||
|
||||
@@ -7,7 +7,9 @@ import { Prefix } from '@polkadot/util-crypto/address/types';
|
||||
import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
|
||||
export interface KeyringOptions {
|
||||
// @deprecated
|
||||
addressPrefix?: Prefix;
|
||||
ss58Format?: Prefix;
|
||||
type?: KeypairType;
|
||||
}
|
||||
|
||||
@@ -31,18 +33,23 @@ export interface KeyringPair$Json {
|
||||
meta: KeyringPair$Meta;
|
||||
}
|
||||
|
||||
export interface SignOptions {
|
||||
withType?: boolean;
|
||||
}
|
||||
|
||||
export interface KeyringPair {
|
||||
readonly address: string;
|
||||
readonly meta: KeyringPair$Meta;
|
||||
readonly isLocked: boolean;
|
||||
readonly publicKey: Uint8Array;
|
||||
readonly type: KeypairType;
|
||||
|
||||
address: string;
|
||||
decodePkcs8: (passphrase?: string, encoded?: Uint8Array) => void;
|
||||
derive: (suri: string, meta?: KeyringPair$Meta) => KeyringPair;
|
||||
encodePkcs8: (passphrase?: string) => Uint8Array;
|
||||
meta: KeyringPair$Meta;
|
||||
isLocked: boolean;
|
||||
lock: () => void;
|
||||
publicKey: Uint8Array;
|
||||
setMeta: (meta: KeyringPair$Meta) => void;
|
||||
sign (message: Uint8Array): Uint8Array;
|
||||
sign (message: Uint8Array, options?: SignOptions): Uint8Array;
|
||||
toJson (passphrase?: string): KeyringPair$Json;
|
||||
verify (message: Uint8Array, signature: Uint8Array): boolean;
|
||||
}
|
||||
@@ -59,9 +66,9 @@ export interface KeyringInstance {
|
||||
readonly publicKeys: Uint8Array[];
|
||||
readonly type: KeypairType;
|
||||
|
||||
decodeAddress (encoded: string | Uint8Array, ignoreChecksum?: boolean): Uint8Array;
|
||||
encodeAddress (key: Uint8Array | string): string;
|
||||
setAddressPrefix (prefix: Prefix): void;
|
||||
decodeAddress (encoded: string | Uint8Array, ignoreChecksum?: boolean, ss58Format?: Prefix): Uint8Array;
|
||||
encodeAddress (key: Uint8Array | string, ss58Format?: Prefix): string;
|
||||
setSS58Format (ss58Format: Prefix): void;
|
||||
|
||||
addPair (pair: KeyringPair): KeyringPair;
|
||||
addFromAddress (address: string | Uint8Array, meta?: KeyringPair$Meta, encoded?: Uint8Array | null, type?: KeypairType, ignoreChecksum?: boolean): KeyringPair;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/trie-codec",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "This is an implementation of the codec for Substrate Trie",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,7 +25,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-codec#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/util": "^1.2.1"
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/util": "^1.7.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export default class NodeHeader {
|
||||
|
||||
private _value: Null | BranchHeader | ExtensionHeader | LeafHeader;
|
||||
|
||||
public constructor (input?: null | Uint8Array | (null | Uint8Array)[]) {
|
||||
constructor (input?: null | Uint8Array | (null | Uint8Array)[]) {
|
||||
const [nodeType, value] = Array.isArray(input)
|
||||
? NodeHeader.decodeNodeHeaderArray(input)
|
||||
: NodeHeader.decodeNodeHeaderU8a(input);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/trie-db",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "This is an implementation of the modified merkle patricia tree as specified in Ethereum's yellow paper, adapted for Polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,16 +25,16 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-db#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/db": "^1.2.1",
|
||||
"@polkadot/trie-codec": "^1.2.1",
|
||||
"@polkadot/trie-hash": "^1.2.1",
|
||||
"@polkadot/util": "^1.2.1",
|
||||
"@polkadot/util-crypto": "^1.2.1"
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/db": "^1.7.1",
|
||||
"@polkadot/trie-codec": "^1.7.1",
|
||||
"@polkadot/trie-hash": "^1.7.1",
|
||||
"@polkadot/util": "^1.7.1",
|
||||
"@polkadot/util-crypto": "^1.7.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/chainspec": "^1.2.1",
|
||||
"@polkadot/util-rlp": "^1.2.1",
|
||||
"@polkadot/chainspec": "^1.7.1",
|
||||
"@polkadot/util-rlp": "^1.7.1",
|
||||
"ethereumjs-testing": "1.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ export default class Checkpoint {
|
||||
|
||||
protected rootHash: Uint8Array;
|
||||
|
||||
public constructor (rootHash: Uint8Array) {
|
||||
constructor (rootHash: Uint8Array) {
|
||||
this.rootHash = rootHash;
|
||||
this.txRoot = rootHash;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ export default class Impl extends Checkpoint {
|
||||
|
||||
protected constants: Constants;
|
||||
|
||||
public constructor (db: TxDb, rootHash?: Uint8Array, codec: Codec = substrateCodec) {
|
||||
constructor (db: TxDb, rootHash?: Uint8Array, codec: Codec = substrateCodec) {
|
||||
const _constants = constants(codec);
|
||||
|
||||
super(rootHash || _constants.EMPTY_HASH);
|
||||
|
||||
@@ -25,7 +25,7 @@ const l = logger('trie/db');
|
||||
* @example See [Polkadot-JS Common Trie-DB Examples](https://polkadot.js.org/api/common/examples/trie-db/)
|
||||
*/
|
||||
export default class Trie extends Impl implements TrieDb {
|
||||
public constructor (db: TxDb = new MemoryDb(), rootHash?: Uint8Array, codec?: Codec) {
|
||||
constructor (db: TxDb = new MemoryDb(), rootHash?: Uint8Array, codec?: Codec) {
|
||||
super(db, rootHash, codec);
|
||||
|
||||
l.debug((): string => `Created with ${this.codec.type} codec, root ${u8aToHex(this.rootHash, 64)}`);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/trie-hash",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "Generates trie roots from key-value pairs",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,10 +25,10 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/util": "^1.2.1"
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/util": "^1.7.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/chainspec": "^1.2.1"
|
||||
"@polkadot/chainspec": "^1.7.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-crypto",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,8 +25,8 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/util": "^1.2.1",
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/util": "^1.7.1",
|
||||
"@polkadot/wasm-crypto": "^0.14.1",
|
||||
"@types/bip39": "^2.4.2",
|
||||
"@types/bs58": "^4.0.0",
|
||||
|
||||
@@ -12,7 +12,7 @@ import sshash from './sshash';
|
||||
export default function check (address: string, prefix: Prefix): [boolean, string | null] {
|
||||
const decoded = bs58.decode(address);
|
||||
|
||||
if (decoded[0] as Prefix !== prefix) {
|
||||
if (decoded[0] !== prefix) {
|
||||
return [false, `Prefix mismatch, expected ${prefix}, found ${decoded[0]}`];
|
||||
} else if (!defaults.allowedEncodedLengths.includes(decoded.length)) {
|
||||
return [false, 'Invalid decoded address length'];
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import testingPairs, { TestKeyringMap } from '../../../keyring/src/testingPairs';
|
||||
import decode from './decode';
|
||||
|
||||
@@ -20,6 +22,12 @@ describe('decode', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes the council address', (): void => {
|
||||
expect(
|
||||
u8aToHex(decode('F3opxRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29'))
|
||||
).toEqual(u8aToHex(stringToU8a('modlpy/trsry'.padEnd(32, '\0'))));
|
||||
});
|
||||
|
||||
it('converts a publicKey (u8a) as-is', (): void => {
|
||||
expect(
|
||||
decode(new Uint8Array([1, 2, 3]))
|
||||
|
||||
@@ -13,7 +13,7 @@ import defaults from './defaults';
|
||||
import sshash from './sshash';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export default function decode (encoded: string | Uint8Array, ignoreChecksum?: boolean, prefix: Prefix = defaults.prefix): Uint8Array {
|
||||
export default function decode (encoded: string | Uint8Array, ignoreChecksum?: boolean, ss58Format: Prefix = 99): Uint8Array {
|
||||
if (isU8a(encoded) || isHex(encoded)) {
|
||||
return u8aToU8a(encoded);
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Prefix } from './types';
|
||||
import { allowedSS58 } from './ss58';
|
||||
|
||||
const defaults = {
|
||||
allowedDecodedLengths: [1, 2, 4, 8, 32],
|
||||
// publicKey has prefix + 2 checksum bytes, short only prefix + 1 checksum byte
|
||||
allowedEncodedLengths: [3, 4, 6, 10, 35],
|
||||
allowedPrefix: [0, 1, 3, 42, 43, 68, 69] as Prefix[],
|
||||
prefix: 42 as Prefix
|
||||
allowedPrefix: allowedSS58,
|
||||
prefix: 42
|
||||
};
|
||||
|
||||
export default defaults;
|
||||
|
||||
@@ -14,6 +14,12 @@ describe('encode', (): void => {
|
||||
).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua');
|
||||
});
|
||||
|
||||
it('can re-encode an address', (): void => {
|
||||
expect(
|
||||
encode('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 68)
|
||||
).toEqual('7sL6eNJj5ZGV5cn3hhV2deRUsivXfBfMH76wCALCqWj1EKzv');
|
||||
});
|
||||
|
||||
it('fails when non-valid publicKey provided', (): void => {
|
||||
expect(
|
||||
(): string => encode(
|
||||
|
||||
@@ -7,19 +7,21 @@ import { Prefix } from './types';
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
|
||||
|
||||
import bs58 from 'bs58';
|
||||
import { assert, u8aConcat, u8aToBuffer, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, u8aConcat, u8aToBuffer } from '@polkadot/util';
|
||||
|
||||
import decode from './decode';
|
||||
import defaults from './defaults';
|
||||
import sshash from './sshash';
|
||||
|
||||
export default function encode (_key: Uint8Array | string, prefix: Prefix = defaults.prefix): string {
|
||||
const key = u8aToU8a(_key);
|
||||
export default function encode (_key: Uint8Array | string, ss58Format: Prefix = defaults.prefix): string {
|
||||
// decode it, this means we can re-encode an address
|
||||
const key = decode(_key);
|
||||
|
||||
assert(defaults.allowedDecodedLengths.includes(key.length), `Expected a valid key to convert, with length ${defaults.allowedDecodedLengths}`);
|
||||
|
||||
const isPublicKey = key.length === 32;
|
||||
|
||||
const input = u8aConcat(new Uint8Array([prefix]), key);
|
||||
const input = u8aConcat(new Uint8Array([ss58Format]), key);
|
||||
const hash = sshash(input);
|
||||
|
||||
return bs58.encode(
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
import checkAddress from './check';
|
||||
import decodeAddress from './decode';
|
||||
import encodeAddress from './encode';
|
||||
import setAddressPrefix from './setPrefix';
|
||||
import setSS58Format from './setSS58Format';
|
||||
|
||||
export {
|
||||
checkAddress,
|
||||
decodeAddress,
|
||||
encodeAddress,
|
||||
setAddressPrefix
|
||||
setSS58Format
|
||||
};
|
||||
|
||||
+3
-3
@@ -3,11 +3,11 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import encode from './encode';
|
||||
import setPrefix from './setPrefix';
|
||||
import setSS58Format from './setSS58Format';
|
||||
|
||||
describe('setPrefix', (): void => {
|
||||
describe('setSS58Format', (): void => {
|
||||
beforeEach((): void => {
|
||||
setPrefix(68);
|
||||
setSS58Format(68);
|
||||
});
|
||||
|
||||
it('sets and allows encoding using', (): void => {
|
||||
+1
-1
@@ -6,6 +6,6 @@ import { Prefix } from './types';
|
||||
|
||||
import defaults from './defaults';
|
||||
|
||||
export default function setPrefix (prefix: Prefix): void {
|
||||
export default function setSS58Format (prefix: Prefix): void {
|
||||
defaults.prefix = prefix;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export const allowedSS58 = [
|
||||
// Polkadot
|
||||
0, 1,
|
||||
// Kusama
|
||||
2, 3,
|
||||
// Dothereum
|
||||
20,
|
||||
// Substrate default
|
||||
42, 43,
|
||||
// Substrate BBQ (deprecated)
|
||||
68, 69
|
||||
];
|
||||
@@ -2,9 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
type PrefixPolkadot = 0;
|
||||
type PrefixKusama = 2;
|
||||
type PrefixSubstrate = 42;
|
||||
type DeprecatedPrefixBBQ = 68;
|
||||
|
||||
export type Prefix = PrefixPolkadot | PrefixKusama | PrefixSubstrate | DeprecatedPrefixBBQ;
|
||||
// FIXME we really want this to map with what is in the allowedSS58 array... i.e. the
|
||||
// values there. As of now, we just map to number.
|
||||
export type Prefix = number;
|
||||
|
||||
@@ -28,6 +28,6 @@ export default function naclSign (message: Uint8Array | string, { publicKey, sec
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
return isReady()
|
||||
? ed25519Sign(publicKey as Uint8Array, (secretKey as Uint8Array).subarray(0, 32), messageU8a)
|
||||
: nacl.sign.detached(messageU8a, secretKey as Uint8Array);
|
||||
? ed25519Sign(publicKey as Uint8Array, secretKey.subarray(0, 32), messageU8a)
|
||||
: nacl.sign.detached(messageU8a, secretKey);
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ import { sr25519Sign } from '@polkadot/wasm-crypto';
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export default function schnorrkelSign (message: Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
assert(publicKey && publicKey.length === 32, 'Expected valid publicKey, 32-bytes');
|
||||
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 64-bytes');
|
||||
assert(publicKey?.length === 32, 'Expected valid publicKey, 32-bytes');
|
||||
assert(secretKey?.length === 64, 'Expected valid secretKey, 64-bytes');
|
||||
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
return sr25519Sign(publicKey as Uint8Array, secretKey as Uint8Array, messageU8a);
|
||||
return sr25519Sign(publicKey, secretKey, messageU8a);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-rlp",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "RLP encoding and decoding",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -27,8 +27,8 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-rlp#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/util": "^1.2.1"
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@polkadot/util": "^1.7.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ethereumjs-testing": "1.0.4"
|
||||
|
||||
@@ -30,5 +30,5 @@ export default function decode (input: Uint8Array): DecodeOutput {
|
||||
|
||||
assert(decoder, 'Unable to find decoder for input type');
|
||||
|
||||
return (decoder as Decoder).fn(decode, input);
|
||||
return decoder.fn(decode, input);
|
||||
}
|
||||
|
||||
@@ -45,5 +45,5 @@ export default function toU8a (value?: any): Uint8Array {
|
||||
|
||||
assert(encoder, 'invalid type');
|
||||
|
||||
return (encoder as Encoder).fn(value);
|
||||
return encoder.fn(value);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "1.2.1",
|
||||
"version": "1.7.1",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,11 +25,11 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"@types/bn.js": "^4.11.5",
|
||||
"bn.js": "^4.11.8",
|
||||
"camelcase": "^5.3.1",
|
||||
"chalk": "^2.4.2",
|
||||
"chalk": "^3.0.0",
|
||||
"ip-regex": "^4.1.0",
|
||||
"moment": "^2.24.0"
|
||||
}
|
||||
|
||||
@@ -6,30 +6,18 @@ import { assert } from '.';
|
||||
|
||||
describe('assert', (): void => {
|
||||
it('should not throw an error when test is true', (): void => {
|
||||
expect(
|
||||
assert(true, 'nothing should be thrown')
|
||||
).toEqual(true);
|
||||
assert(true, 'nothing should be thrown');
|
||||
});
|
||||
|
||||
it('should throw an error when test is not true', (): void => {
|
||||
expect(
|
||||
(): boolean => assert(false, 'error thrown')
|
||||
(): void => assert(false, 'error thrown')
|
||||
).toThrow(/error thrown/);
|
||||
});
|
||||
|
||||
it('should throw an error when message: () => string', (): void => {
|
||||
expect(
|
||||
(): boolean => assert(false, (): string => 'message from function')
|
||||
(): void => assert(false, (): string => 'message from function')
|
||||
).toThrow(/message from function/);
|
||||
});
|
||||
|
||||
it('throws a valid constructed ExtError', (): void => {
|
||||
try {
|
||||
assert(false, 'error', -666, 'data');
|
||||
} catch (error) {
|
||||
expect(error.code).toEqual(-666);
|
||||
expect(error.data).toEqual('data');
|
||||
expect(error.message).toEqual('error');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
+13
-17
@@ -2,37 +2,33 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import ExtError from './ext/error';
|
||||
import isFunction from './is/function';
|
||||
|
||||
type MessageFn = () => string;
|
||||
|
||||
type Falsy = null | undefined | false | 0 | ''; // No NaN type
|
||||
|
||||
/**
|
||||
* @name assert
|
||||
* @summary Checks for a valid test, if not ExtError is thrown.
|
||||
* @summary Checks for a valid test, if not Error is thrown.
|
||||
* @description
|
||||
* Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned.
|
||||
* Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an Error with the supplied `message`. When `test` passes, `true` is returned.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* const { assert } from '@polkadot/util';
|
||||
*
|
||||
* assert(true, 'True should be true'); // true returned
|
||||
* assert(false, 'False should not be true'); // ExtError thrown
|
||||
* assert(false, () => 'message'); // ExtError with 'message'
|
||||
* assert(true, 'True should be true'); // passes
|
||||
* assert(false, 'False should not be true'); // Error thrown
|
||||
* assert(false, () => 'message'); // Error with 'message'
|
||||
* ```
|
||||
*/
|
||||
export default function assert<T> (test: Falsy | T, message: string | MessageFn, code: number = ExtError.CODES.ASSERT, data?: string | number): test is T {
|
||||
if (test) {
|
||||
return true;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export default function assert (condition: any, message: string | MessageFn): asserts condition {
|
||||
if (!condition) {
|
||||
throw new Error(
|
||||
isFunction(message)
|
||||
? message()
|
||||
: message
|
||||
);
|
||||
}
|
||||
|
||||
if (isFunction(message)) {
|
||||
message = message();
|
||||
}
|
||||
|
||||
throw new ExtError(message, code, data);
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
// Copyright 2017-2019 @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 isError from '../is/error';
|
||||
import { ExtError } from '.';
|
||||
|
||||
describe('ExtError', (): void => {
|
||||
describe('constructor', (): void => {
|
||||
it('constructs an Error that is still an Error', (): void => {
|
||||
expect(
|
||||
isError(
|
||||
new ExtError()
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('static', (): void => {
|
||||
it('exposes the .CODES as a static', (): void => {
|
||||
expect(
|
||||
Object.keys(ExtError.CODES)
|
||||
).not.toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('constructor properties', (): void => {
|
||||
it('sets the .message property', (): void => {
|
||||
expect(
|
||||
new ExtError('test message').message
|
||||
).toEqual('test message');
|
||||
});
|
||||
|
||||
it("sets the .message to '' when not set", (): void => {
|
||||
expect(
|
||||
new ExtError().message
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('sets the .code property', (): void => {
|
||||
expect(
|
||||
new ExtError('test message', 1234).code
|
||||
).toEqual(1234);
|
||||
});
|
||||
|
||||
it('sets the .code to UKNOWN when not set', (): void => {
|
||||
expect(
|
||||
new ExtError('test message').code
|
||||
).toEqual(ExtError.CODES.UNKNOWN);
|
||||
});
|
||||
|
||||
it('sets the .data property', (): void => {
|
||||
const data = 'here';
|
||||
|
||||
expect(
|
||||
new ExtError('test message', 1234, data).data
|
||||
).toEqual(data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stack traces', (): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let captureStackTrace: (targetObject: Record<string, any>, constructorOpt?: Function | undefined) => void;
|
||||
|
||||
beforeEach((): void => {
|
||||
captureStackTrace = Error.captureStackTrace;
|
||||
|
||||
Error.captureStackTrace = function (error): void {
|
||||
Object.defineProperty(error, 'stack', {
|
||||
configurable: true,
|
||||
get: function getStack (): string {
|
||||
const value = 'some stack returned';
|
||||
|
||||
Object.defineProperty(this, 'stack', { value });
|
||||
|
||||
return value;
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
Error.captureStackTrace = captureStackTrace;
|
||||
});
|
||||
|
||||
it('captures via captureStackTrace when available', (): void => {
|
||||
expect(
|
||||
new ExtError().stack
|
||||
).toEqual('some stack returned');
|
||||
});
|
||||
|
||||
it('captures via stack when captureStackTrace not available', (): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
Error.captureStackTrace = null as any;
|
||||
|
||||
expect(
|
||||
new ExtError().stack.length
|
||||
).not.toEqual(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,73 +0,0 @@
|
||||
// Copyright 2017-2019 @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 { ExtErrorInterface } from '../types';
|
||||
|
||||
import isFunction from '../is/function';
|
||||
|
||||
const UNKNOWN = -99999;
|
||||
|
||||
function extend (that: ExtError, name: string, value?: string | number): void {
|
||||
Object.defineProperty(that, name, {
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
value
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ExtError
|
||||
* @summary Extension to the basic JS Error.
|
||||
* @description
|
||||
* The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (any types) parameters are available on the object.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* const { ExtError } from '@polkadot/util');
|
||||
*
|
||||
* throw new ExtError('some message', ExtError.CODES.METHOD_NOT_FOUND); // => error.code = -32601
|
||||
* ```
|
||||
*/
|
||||
export default class ExtError extends Error implements ExtErrorInterface {
|
||||
public code: number;
|
||||
|
||||
public data?: number | string;
|
||||
|
||||
public message: string;
|
||||
|
||||
public name: string;
|
||||
|
||||
public stack: string;
|
||||
|
||||
public constructor (message = '', code: number = UNKNOWN, data?: number | string) {
|
||||
super();
|
||||
|
||||
// These are actually done via extend below to clean them up
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
this.name = name;
|
||||
this.message = message;
|
||||
this.stack = '';
|
||||
|
||||
extend(this, 'message', String(message));
|
||||
extend(this, 'name', this.constructor.name);
|
||||
extend(this, 'data', data);
|
||||
extend(this, 'code', code);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
if (isFunction(Error.captureStackTrace)) {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
} else {
|
||||
extend(this, 'stack', (new Error(message)).stack);
|
||||
}
|
||||
}
|
||||
|
||||
public static CODES = {
|
||||
ASSERT: -90009,
|
||||
UNKNOWN,
|
||||
INVALID_JSONRPC: -99998,
|
||||
METHOD_NOT_FOUND: -32601 // Rust client
|
||||
};
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
// Copyright 2017-2019 @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.
|
||||
|
||||
/**
|
||||
* @summary Extensions to basic classes
|
||||
*/
|
||||
|
||||
export { default as ExtError } from './error';
|
||||
@@ -9,80 +9,108 @@ import formatBalance from './formatBalance';
|
||||
describe('formatBalance', (): void => {
|
||||
const TESTVAL = new BN('123456789000');
|
||||
|
||||
it('formats empty to 0', (): void => {
|
||||
expect(formatBalance()).toEqual('0');
|
||||
expect(formatBalance('0')).toEqual('0');
|
||||
describe('SI formatting', (): void => {
|
||||
it('formats empty to 0', (): void => {
|
||||
expect(formatBalance()).toEqual('0');
|
||||
expect(formatBalance('0')).toEqual('0');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 15)
|
||||
).toEqual('123.456µ Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=36)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 36)
|
||||
).toEqual('0.123y Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15, Compact)', (): void => {
|
||||
const compact = {
|
||||
toBn: (): BN => TESTVAL,
|
||||
unwrap: (): BN => TESTVAL,
|
||||
something: 'else'
|
||||
};
|
||||
expect(
|
||||
formatBalance(compact, true, 15)
|
||||
).toEqual('123.456µ Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 12)
|
||||
).toEqual('123.456m Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=12, no SI)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, false, 12)
|
||||
).toEqual('123.456');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=9)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 9)
|
||||
).toEqual('123.456 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=6)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 6)
|
||||
).toEqual('123.456k Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 10 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(10), true, 12)
|
||||
).toEqual('1.234 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 100 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(100), true, 12)
|
||||
).toEqual('12.345 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 1000 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(1000), true, 12)
|
||||
).toEqual('123.456 Unit');
|
||||
});
|
||||
|
||||
it('formats -123,456,789,000 (decimals=15)', (): void => {
|
||||
expect(
|
||||
formatBalance(new BN('-123456789000'), true, 15)
|
||||
).toEqual('-123.456µ Unit');
|
||||
});
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 15)
|
||||
).toEqual('123.456µ Unit');
|
||||
});
|
||||
describe('Forced formatting', (): void => {
|
||||
it('formats 123,456,789,000 (decimals=12, forceUnit=base)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: '-' }, 12)
|
||||
).toEqual('0.123 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=36)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 36)
|
||||
).toEqual('0.123y Unit');
|
||||
});
|
||||
it('formats 123,456,789,000 (decimals=9, forceUnit=base)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: '-' }, 9)
|
||||
).toEqual('123.456 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=15, Compact)', (): void => {
|
||||
const compact = {
|
||||
toBn: (): BN => TESTVAL,
|
||||
unwrap: (): BN => TESTVAL,
|
||||
something: 'else'
|
||||
};
|
||||
expect(
|
||||
formatBalance(compact, true, 15)
|
||||
).toEqual('123.456µ Unit');
|
||||
});
|
||||
it('formats 123,456,789,000 (decimals=7, forceUnit=base)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: '-' }, 7)
|
||||
).toEqual('12,345.678 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 12)
|
||||
).toEqual('123.456m Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=12, no SI)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, false, 12)
|
||||
).toEqual('123.456');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=9)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 9)
|
||||
).toEqual('123.456 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 (decimals=6)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, true, 6)
|
||||
).toEqual('123.456k Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 10 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(10), true, 12)
|
||||
).toEqual('1.234 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 100 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(100), true, 12)
|
||||
).toEqual('12.345 Unit');
|
||||
});
|
||||
|
||||
it('formats 123,456,789,000 * 1000 (decimals=12)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL.muln(1000), true, 12)
|
||||
).toEqual('123.456 Unit');
|
||||
});
|
||||
|
||||
it('formats -123,456,789,000 (decimals=15)', (): void => {
|
||||
expect(
|
||||
formatBalance(new BN('-123456789000'), true, 15)
|
||||
).toEqual('-123.456µ Unit');
|
||||
it('formats 123,456,789,000 (decimals=15, forceUnit=µ)', (): void => {
|
||||
expect(
|
||||
formatBalance(TESTVAL, { forceUnit: 'µ' }, 15)
|
||||
).toEqual('123.456µ Unit');
|
||||
});
|
||||
});
|
||||
|
||||
describe('calcSi', (): void => {
|
||||
|
||||
@@ -2,22 +2,25 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ToBn } from '../types';
|
||||
import { SiDef, ToBn } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import bnToBn from '../bn/toBn';
|
||||
import isBoolean from '../is/boolean';
|
||||
import isUndefined from '../is/undefined';
|
||||
import formatDecimal from './formatDecimal';
|
||||
import { SI, SI_MID, SiDef, calcSi, findSi } from './si';
|
||||
import { SI, SI_MID, calcSi, findSi } from './si';
|
||||
|
||||
interface Defaults {
|
||||
decimals: number;
|
||||
unit: string;
|
||||
}
|
||||
|
||||
type Options = boolean | { forceUnit?: string; withSi?: boolean };
|
||||
|
||||
interface BalanceFormatter {
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, withSi?: boolean, decimals?: number): string;
|
||||
<ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, options?: Options, decimals?: number): string;
|
||||
calcSi (text: string, decimals?: number): SiDef;
|
||||
findSi (type: string): SiDef;
|
||||
getDefaults (): Defaults;
|
||||
@@ -32,7 +35,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, withSi = true, decimals: number = defaultDecimals): string {
|
||||
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | ExtToBn, options: Options = true, decimals: number = defaultDecimals): string {
|
||||
let text = bnToBn(input).toString();
|
||||
|
||||
if (text.length === 0 || text === '0') {
|
||||
@@ -47,10 +50,15 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | E
|
||||
text = text.substr(1);
|
||||
}
|
||||
|
||||
// extract options - the boolean case is for backwards-compat
|
||||
const { forceUnit = undefined, withSi = true } = isBoolean(options)
|
||||
? { withSi: options }
|
||||
: options;
|
||||
|
||||
// NOTE We start at midpoint (8) minus 1 - this means that values display as
|
||||
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we us ceil
|
||||
// so there are at most 3 decimal before the decimal seperator
|
||||
const si = calcSi(text, decimals);
|
||||
const si = calcSi(text, decimals, forceUnit);
|
||||
const mid = text.length - (decimals + si.power);
|
||||
const prefix = text.substr(0, mid);
|
||||
const padding = mid < 0 ? 0 - mid : 0;
|
||||
|
||||
@@ -2,11 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export interface SiDef {
|
||||
power: number;
|
||||
text: string;
|
||||
value: string;
|
||||
}
|
||||
import { SiDef } from '../types';
|
||||
|
||||
export const SI_MID = 8;
|
||||
|
||||
@@ -30,11 +26,6 @@ export const SI: SiDef[] = [
|
||||
{ power: 24, value: 'Y', text: 'Yotta' }
|
||||
];
|
||||
|
||||
export function calcSi (text: string, decimals: number): SiDef {
|
||||
const siDefIndex = (SI_MID - 1) + Math.ceil((text.length - decimals) / 3);
|
||||
return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1];
|
||||
}
|
||||
|
||||
// Given a SI type (e.g. k, m, Y) find the SI definition
|
||||
export function findSi (type: string): SiDef {
|
||||
// use a loop here, better RN support (which doesn't have [].find)
|
||||
@@ -46,3 +37,13 @@ export function findSi (type: string): SiDef {
|
||||
|
||||
return SI[SI_MID];
|
||||
}
|
||||
|
||||
export function calcSi (text: string, decimals: number, forceUnit?: string): SiDef {
|
||||
if (forceUnit) {
|
||||
return findSi(forceUnit);
|
||||
}
|
||||
|
||||
const siDefIndex = (SI_MID - 1) + Math.ceil((text.length - decimals) / 3);
|
||||
|
||||
return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1];
|
||||
}
|
||||
|
||||
@@ -12,4 +12,5 @@ export { default as hexHasPrefix } from './hasPrefix';
|
||||
export { default as hexStripPrefix } from './stripPrefix';
|
||||
export { default as hexToBn } from './toBn';
|
||||
export { default as hexToNumber } from './toNumber';
|
||||
export { default as hexToString } from './toString';
|
||||
export { default as hexToU8a } from './toU8a';
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2019 @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 { hexToString } from '.';
|
||||
|
||||
describe('hexToString', (): void => {
|
||||
it('converts an empty to ""', (): void => {
|
||||
expect(
|
||||
hexToString()
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('converts to a string from hex', (): void => {
|
||||
expect(
|
||||
hexToString('0x68656c6c6f')
|
||||
).toEqual('hello');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2019 @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 u8aToString from '../u8a/toString';
|
||||
import hexToU8a from './toU8a';
|
||||
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* Hex input values return the actual bytes value converted to a string. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { hexToString } from '@polkadot/util';
|
||||
*
|
||||
* hexToU8a('0x68656c6c6f'); // hello
|
||||
* ```
|
||||
*/
|
||||
export default function hexToString (_value?: string | null): string {
|
||||
return u8aToString(
|
||||
hexToU8a(_value)
|
||||
);
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import hexStripPrefix from './stripPrefix';
|
||||
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @summary Creates a Buffer object from a hex string.
|
||||
* @summary Creates a Uint8Array object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @example
|
||||
|
||||
@@ -17,7 +17,6 @@ export * from './array';
|
||||
export * from './bn';
|
||||
export * from './buffer';
|
||||
export * from './compact';
|
||||
export * from './ext';
|
||||
export * from './format';
|
||||
export * from './hex';
|
||||
export * from './is';
|
||||
|
||||
@@ -9,5 +9,6 @@
|
||||
export { default as stringCamelCase } from './camelCase';
|
||||
export { default as stringLowerFirst } from './lowerFirst';
|
||||
export { default as stringShorten } from './shorten';
|
||||
export { default as stringToHex } from './toHex';
|
||||
export { default as stringToU8a } from './toU8a';
|
||||
export { default as stringUpperFirst } from './upperFirst';
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2019 @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 { stringToHex } from '.';
|
||||
|
||||
describe('hexToString', (): void => {
|
||||
it('converts an empty to ""', (): void => {
|
||||
expect(
|
||||
stringToHex()
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts to a hex from string', (): void => {
|
||||
expect(
|
||||
stringToHex('hello')
|
||||
).toEqual('0x68656c6c6f');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2019 @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 u8aToHex from '../u8a/toHex';
|
||||
import stringToU8a from './toU8a';
|
||||
|
||||
/**
|
||||
* @name stringToHex
|
||||
* @summary Creates a hex string from a utf-8 string
|
||||
* @description
|
||||
* String input values return the actual encoded hex value.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { stringToHex } from '@polkadot/util';
|
||||
*
|
||||
* stringToU8a('hello'); // 0x68656c6c6f
|
||||
* ```
|
||||
*/
|
||||
export default function stringToHex (value?: string): string {
|
||||
return u8aToHex(
|
||||
stringToU8a(value)
|
||||
);
|
||||
}
|
||||
@@ -8,11 +8,10 @@ export interface ToBn {
|
||||
toBn: () => BN;
|
||||
}
|
||||
|
||||
export interface ExtErrorInterface {
|
||||
code: number;
|
||||
data?: string | number;
|
||||
message: string;
|
||||
stack: string;
|
||||
export interface SiDef {
|
||||
power: number;
|
||||
text: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": [
|
||||
"build",
|
||||
"packages/*/build"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"checkJs": true
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"extends": "./node_modules/@polkadot/dev/config/tsconfig",
|
||||
"exclude": [
|
||||
"build/**/*",
|
||||
"**/build/**/*"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
@@ -22,6 +26,7 @@
|
||||
"@polkadot/util": [ "packages/util/src" ],
|
||||
"@polkadot/util/*": [ "packages/util/src/*" ]
|
||||
},
|
||||
"skipLibCheck": true,
|
||||
"typeRoots": [
|
||||
"./node_modules/@polkadot/ts",
|
||||
"./node_modules/@types"
|
||||
|
||||
Reference in New Issue
Block a user