Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b46308fa8f | ||
|
|
c366e637e9 | ||
|
|
81acc81ef7 | ||
|
|
eb7270ff3f | ||
|
|
30198d1a6d | ||
|
|
2fc078d994 | ||
|
|
bd1735cafb | ||
|
|
ca73e5bc4d | ||
|
|
aff78c2e30 | ||
|
|
fbd454eb5a | ||
|
|
ce964d2f12 | ||
|
|
2c26bc61da | ||
|
|
4111122cba | ||
|
|
e1e95dc6b3 | ||
|
|
e7c665e5da | ||
|
|
301eb587cb | ||
|
|
5c886b0f81 | ||
|
|
56a79924a0 | ||
|
|
beef1b38ed | ||
|
|
ce46a68187 | ||
|
|
94dc2d76b3 | ||
|
|
dcdde1cf4b | ||
|
|
e3a9099b58 | ||
|
|
fb03ff3994 | ||
|
|
1d42fbf2bf | ||
|
|
c0e4b1b93e | ||
|
|
139f3f90b0 | ||
|
|
cf6fae8e1d | ||
|
|
f8d69736c1 | ||
|
|
a4322cb99a | ||
|
|
9445006127 | ||
|
|
aeeaa91752 | ||
|
|
52fb4ac863 | ||
|
|
2a000a2a14 | ||
|
|
97fe3b2ea3 | ||
|
|
9b8a782d58 | ||
|
|
b56e40ea5b | ||
|
|
37d1bcb6eb | ||
|
|
f2b8c1fb4e | ||
|
|
92cc8fc4e3 | ||
|
|
701bd9335d | ||
|
|
d176c74715 | ||
|
|
0bf7c799f3 | ||
|
|
2f7d5cd4e1 | ||
|
|
c42b517293 | ||
|
|
e5dd55e419 | ||
|
|
83fdd11c59 | ||
|
|
08de8ce25a | ||
|
|
800bafc00c | ||
|
|
3b383b97ca | ||
|
|
b381c09ecf | ||
|
|
a53008fd67 | ||
|
|
899056c5a2 | ||
|
|
8554d47030 |
@@ -1 +1,4 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
module.exports = require('./babel.config.js');
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const base = require('@polkadot/dev/config/eslint');
|
||||
|
||||
module.exports = {
|
||||
@@ -7,5 +10,10 @@ module.exports = {
|
||||
project: [
|
||||
'./tsconfig.json'
|
||||
]
|
||||
},
|
||||
rules: {
|
||||
...base.rules,
|
||||
// this seems very broken atm, false positives
|
||||
'@typescript-eslint/unbound-method': 'off'
|
||||
}
|
||||
};
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Vendored
-86
File diff suppressed because one or more lines are too long
+55
File diff suppressed because one or more lines are too long
+2
-2
@@ -5,7 +5,7 @@ enableProgressBars: false
|
||||
nodeLinker: node-modules
|
||||
|
||||
plugins:
|
||||
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.js
|
||||
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
|
||||
spec: "@yarnpkg/plugin-interactive-tools"
|
||||
|
||||
yarnPath: .yarn/releases/yarn-2.1.1.js
|
||||
yarnPath: .yarn/releases/yarn-2.2.2.cjs
|
||||
|
||||
+240
-48
@@ -1,192 +1,338 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 3.5.1 Sep 27, 2020
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Dock network identifiers (Thanks to https://github.com/lovesh)
|
||||
|
||||
Changes:
|
||||
|
||||
- Use cross-environment TextDecoder & TextEncoder from the polkadot-js polyfills
|
||||
- Add `isBase{32, 58, 64}` to `@polkadot/util-crypto`
|
||||
- Add `base64{Pad, Trim}` to add/remove padding characters
|
||||
- Add `createFromJson(json)` to keyring (create pair without adding)
|
||||
- Balance formatter now outputs 4 numbers after the decimal point
|
||||
|
||||
|
||||
## 3.4.1 Aug 31, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Allow for WASM-as-available `mnemonicToBip39` call (used for Ethereum-compatible keypairs)
|
||||
- Add `isWasm` utility to check for valid WASM headers
|
||||
- `@polkadot/wasm-crypto` 1.4.1
|
||||
|
||||
|
||||
## 3.3.1 Aug 24, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `addressRaw` to Keyring interface (if address encoding is different to publicKey)
|
||||
- Allow for Ethereum pair signing and verification
|
||||
|
||||
|
||||
## 3.2.1 Aug 17, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Allow for (optional, default blake2) hasher to secp256k1 sign/verify functions
|
||||
- Utilities for secp256k1 publicKey expansion and compression
|
||||
- Add basic Ethereum address utils (encode with checksum & checksum verification)
|
||||
- Add Ethereum-compatible `mnemonicToBip39` seed creation function
|
||||
|
||||
|
||||
## 3.1.1 Aug 10, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `deriveAddress(<address>, '/<soft>/<soft-b>')` via sr25519 soft
|
||||
- Use `scrypt` from WASM as available (JS fallback in place)
|
||||
- Add `base32{Encode, Decode}` utilities (IPFS-compatible alphabet)
|
||||
- Support IPFS compatibility flag in base58 encoding/decoding
|
||||
- Update to `@polkadot/wasm-crypto` 1.3.1
|
||||
|
||||
|
||||
## 3.0.1 Jul 27, 2020
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Katal ss58Format (Thanks to https://github.com/retotrinkler)
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust JSON pair output with scrypt kdf (version 3 output)
|
||||
|
||||
|
||||
## 2.18.1 Jul 13, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `base64{Decode, Encode, Validate}` as crypto utils
|
||||
- Extract `base58Validate` from base58 decode checks
|
||||
- Add `isAscii` to allow detection of printable ASCII sequences (including tab, newline)
|
||||
- Add `isUtf8` to allow detection of valid Utf8 sequences
|
||||
|
||||
|
||||
## 2.17.1 Jul 6, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `encode{Derived, Multi}Address` to encode derived/multi addresses
|
||||
- Correctly handle 0 inputs in `numberToU8a`
|
||||
- Simplify and flatten base58 dependencies
|
||||
|
||||
|
||||
## 2.16.1 Jun 29, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust `addressCheck` and `encodeAddress` function to check for valid base58 alphabets (better error reporting)
|
||||
- Rename `createKeySub` to `createKeyDerived` based on changed in Substrate (sub keys now named derived)
|
||||
|
||||
|
||||
## 2.15.1 Jun 22, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust keyring to no override 0-prefix (Polkadot live) settings with keychain default
|
||||
|
||||
|
||||
## 2.14.1 Jun 16, 2020
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add `extractTime` utility conversion (Thanks to https://github.com/kwingram25)
|
||||
|
||||
|
||||
## 2.13.1 Jun 8, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Fix JS blake2 fallback (non-wasm) to correctly deal with hex inputs
|
||||
- Align `bnToHex` signature with `bnToU8a` (with old/new style)
|
||||
- Allow `bnTo{Hex,U8a}` to take any value with `.toBn()` signatures
|
||||
|
||||
|
||||
## 2.12.2 Jun 1, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Fix `isFunction` signature to assert `Function`
|
||||
|
||||
|
||||
## 2.12.1 Jun 1, 2020
|
||||
|
||||
- **Important** Removed `mnemonicToSeed`, it is only for bip39, use `mnemonicToMiniSecret` for the Substrate variant
|
||||
|
||||
Changes:
|
||||
|
||||
- Added `u8aEq` to compare two Uint8Array or hex values for an exact match
|
||||
- Added `addressEq` to compare two addresses (ss58, Uint8array or hex) for an exact match
|
||||
- Throw error on `{nacl,schnorrkel}Verify` on invalid input lengths
|
||||
- Performance optimizations in `u8aToHex` & `u8aConcat`
|
||||
|
||||
|
||||
## 2.11.1 May 26, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Internal cleanups on typings
|
||||
- Support for latest TypeScript, eslint & typescript-eslint
|
||||
|
||||
|
||||
## 2.10.1 May 14, 2020
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Kulupu ss58 prefix as an allowed value (Thanks to https://github.com/sorpaas)
|
||||
|
||||
Changes:
|
||||
|
||||
- Make the `TextEncoder` polyfill handle non-compliant Buffer implementations (newer versions of Jest)
|
||||
- `DeriveJunction` now also allows for BigInt values (aligning with number types elsewhere)
|
||||
- Small code-flow optimizations
|
||||
|
||||
|
||||
## 2.9.1 Apr 30, 2020
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add support for ECDSA keypairs (Thanks to https://github.com/akru)
|
||||
|
||||
Changes:
|
||||
|
||||
- Added `createKeyMulti` & `createKeySub` to create utility sub & multi keys
|
||||
- Added `u8aSorted` to sort `Uint8Arrays`
|
||||
- Added `sortAddresses` to sort addresses based on their internal representation
|
||||
|
||||
|
||||
## 2.8.1 Apr 9, 2020
|
||||
|
||||
- Keypair will now throw an error when attempting to sign/derive using a locked pair (Thanks to https://github.com/h4x3rotab)
|
||||
|
||||
Changes:
|
||||
|
||||
- Cleanup `isDevelopment` regex to cater for chains such as `Westend Development`
|
||||
|
||||
|
||||
## 2.7.1 Mar 31, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `bnSqrt` to calculate the integer sqrt via Newton iteration
|
||||
- Allow for optional wasm-crypto, specifically via `@polkadot/util-crypto/noWasm`
|
||||
- Misc. CI infrastructure cleanups
|
||||
|
||||
|
||||
## 2.6.1 Feb 29, 2020
|
||||
|
||||
- **Breaking change** Following on the 2.0 release, `@polkadot/wasm` was updated to 1.1+. sr25510 now only verifies 0.8+ signatures, closing the loop on dropping Substrate 1.x-era support
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `signatureVerify (message: Uint8Array | string, signature: Uint8Array | string, addressOrPublicKey: Uint8Array | string): VerifyResult` to verify any signature, regardless of type. Returns validity & detected crypto type.
|
||||
- Add `cryptoIsReady (): boolean` function to check status of initialization
|
||||
- Add `addressCheckChecksum (decoded: Uint8Array)` as an helper to extract and verify the ss58 checksum
|
||||
- Swap to yarn 2, allow use in yarn 2 projects
|
||||
|
||||
|
||||
## 2.5.1 Feb 22, 2020
|
||||
|
||||
- **Breaking change** (TypeScript only) The `*.d.ts` files now contain TypeScript 3.8 features, `#private`, which is not usable in older versions
|
||||
|
||||
Changes:
|
||||
|
||||
- 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 `#<field>` instead of `private _<field>` for private class variables
|
||||
|
||||
|
||||
## 2.4.1 Feb 18, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Ensure that `formatBalance` does not apply unit overrides apply when no SI is applicable
|
||||
- Adjust `formatBalance(<balance>, <options>)` to take expanded `withUnit: string | boolean` option where string is an actual unit, e.g. `KSM`
|
||||
- The `decimals` option to `formatBalance` should now be passed as part of the `options` (previous last-param deprecated)
|
||||
- The `.setSS58Format` on keyring will now just operate on pairs created on the keyring, not globally. The global `setSS58Format` in `@polkadot/util-crypto` will be deprecated.
|
||||
- The deprecated `addressPrefix` option to keyring has been belatedly removed (Use `ss58Format` rather)
|
||||
|
||||
|
||||
## 2.3.1 Feb 15, 2020
|
||||
|
||||
Contributed:
|
||||
|
||||
- Remove dependency on moment.js with `formatDate` function (Thanks to https://github.com/AndreasGassmann)
|
||||
- Move TypeScript `@types/*` to dev deps (Thanks to https://github.com/AndreasGassmann)
|
||||
|
||||
Changes:
|
||||
|
||||
- Update to `@polkadot/wasm-crypto` 1.0
|
||||
- Cleanup dependencies to pave the way for using yarn 2 (Explicit instead of implicit)
|
||||
|
||||
|
||||
## 2.2.1 Feb 06, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Add resolver for jest requires (tests from src)
|
||||
|
||||
|
||||
## 2.1.1 Feb 04, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Update `detectPackage(<package.json>, __dirname?)` with calling folder
|
||||
|
||||
|
||||
## 2.0.1 Jan 30, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
- Update w3f/schnorrkel to 0.8.5 (Full Substrate 2.x support, no 1.x support)
|
||||
- Remove Alice session (ed2551) account from testing keyring (not applicable to Substrate 2.x)
|
||||
- Remove `chainspec`, `db`, `trie-codec`, `trie-db` and `trie-hash` packages (moved to client)
|
||||
- Renamed `assertSingletonPackage` to `detectPackage` with inclusion of version listing
|
||||
- Swap to elliptic library for secp2561 recovery (No node bindings)
|
||||
|
||||
|
||||
## 1.8.1 Jan 09, 2020
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add cross-client encryption/decryption support (Thanks to https://github.com/hskang9)
|
||||
|
||||
|
||||
## 1.7.1 Nov 27, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- `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 Oct 24, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- `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 Sep 25, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- 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 Sep 12, 2019
|
||||
|
||||
- **Breaking change** To set the `ss58Format`, you should now use `setSS58Format` as opposed to the old `setAddressPrefix`
|
||||
|
||||
Changes:
|
||||
|
||||
- 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 Sep 10, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- 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 Sep 09, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Keyring `addFromAddress` can now be used to store external accounts, for example those provided by external signers
|
||||
- Maintainability updates
|
||||
|
||||
|
||||
## 1.1.1 Aug 09, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- `assertSingletonPackage` now warns via console.warn, but does not throw. This does allow the info to come accross while still allowing the app developers using multiple libraries time to fix.
|
||||
|
||||
|
||||
## 1.0.1 Aug 02, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- `formatBalance` now allows for `Compact` inputs
|
||||
- `formaBalance` now allows correct formatting of very large decimals
|
||||
|
||||
### 0.94.1 Jul 20, 2019
|
||||
|
||||
## 0.94.1 Jul 20, 2019
|
||||
Changes:
|
||||
|
||||
- Add `checkAddress(address, prefix)` to `@polkadot/util-crypto` that validates an ss-58 address
|
||||
- Add support for the Kusama network (as a valid checked prefix)
|
||||
@@ -194,7 +340,10 @@
|
||||
- The ed25519 key for Alice (representing the session), now appears in the test keyrings
|
||||
- Fix missing dependencies for keyring (after address moves in 0.93.1)
|
||||
|
||||
### 0.93.1 Jun 14, 2019
|
||||
|
||||
## 0.93.1 Jun 14, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Breaking: External pair interface for keyring has been changed. Instead of
|
||||
- `getMeta` use the `meta` getter, i.e. `console.log(pair.meta.name)`
|
||||
@@ -202,133 +351,176 @@
|
||||
- `publicKey` use the `publicKey` getter, i.e. `console.log(pair.publicKey)`
|
||||
- `Move decodeAddress`, `encodeAddress` & `setAddressPrefix` functions into `@polkadot/util-crypto` from `@polkadot/keyring`. External interfaces should not be affected at this point since it is also (still) exported and exposed on keyring
|
||||
|
||||
### 0.92.1 Jun 04, 2019
|
||||
|
||||
## 0.92.1 Jun 04, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Only allow integer input values into `formatBalance`
|
||||
- Allow `formatBalance`, `formatDecimal` & `formatNumber` to cater for negative numbers
|
||||
- Add 'Bob//stash' to testing keyring, joining the existing 'Alice//stash'
|
||||
|
||||
### 0.91.1 May 22, 2019
|
||||
|
||||
## 0.91.1 May 22, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- ed25519/sr25519 sign & verify functions can now take the message input as Uint8Array/string/hex and verify allows for the signature/publicKey to be specified as Uint8Array/hex
|
||||
- Update `@polkadot/wasm` to include a maintenance bump for the `w3f/schnorrkel` libraries
|
||||
|
||||
### 0.90.1 May 08, 2019
|
||||
|
||||
## 0.90.1 May 08, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Moving towards 1.0
|
||||
- Added bnMin & bnMax functions
|
||||
|
||||
### 0.76.1 Apr 03, 2019
|
||||
|
||||
## 0.76.1 Apr 03, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Fix `addFromMnemonic` to generate using new-style derivation.
|
||||
- Pull in `formatBalance`, `formatDecimal`, `formatElapsed`, `formatNumber`, `calcSi`, `findSi` & `isTestChain` utility functions from the originals in `@polkadot/ui-util`
|
||||
- Swap out `wasm-schnorrkel` & `wasm-ed25519` for combined version with `wasm-crypto`
|
||||
- Swap DB interfaces to optional LRU, removing `transactionAsync` (client-only changes)
|
||||
|
||||
### 0.75.1 Mar 29, 2019
|
||||
|
||||
## 0.75.1 Mar 29, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Start journey to 1.0
|
||||
|
||||
### 0.42.1 Mar 28, 2019
|
||||
|
||||
## 0.42.1 Mar 28, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `assertSingletonPackage` to util
|
||||
- Ensure that only a single keyring instance is loaded (uses above)
|
||||
|
||||
### 0.41.1 Mar 25, 2019
|
||||
|
||||
## 0.41.1 Mar 25, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Extensions to the client-specific database interfaces (non-breaking for API and util users)
|
||||
|
||||
### 0.40.1 Mar 23, 2019
|
||||
|
||||
## 0.40.1 Mar 23, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Add additional derivation path extraction mechanisms to util-crypto
|
||||
- Add createFromUri function to keypair (creates keypair, does not add it)
|
||||
- Allow all crypto hashing & ed25519 to use WASM (as available, JS fallbacks)
|
||||
|
||||
### 0.39.1 Mar 20, 2019
|
||||
|
||||
## 0.39.1 Mar 20, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Pull in bip39 generation functions from `@poladkot/wasm-crypto` with a fallback to the pure JS versions
|
||||
|
||||
### 0.38.1 Mar 18, 2019
|
||||
|
||||
## 0.38.1 Mar 18, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Align with current substrate master for sr25519 keys and hard/soft derivation
|
||||
- Swap default dev keys to derived (sr25519)
|
||||
|
||||
### 0.37.1 Mar 14, 2019
|
||||
|
||||
## 0.37.1 Mar 14, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Swap to publishing -beta.x on merge (non-breaking testing)
|
||||
|
||||
### 0.36.1 Mar 13, 2019
|
||||
|
||||
## 0.36.1 Mar 13, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `fromUri` to keyring along with hard & soft key derivation.
|
||||
|
||||
### 0.35.1 Mar 04, 2019
|
||||
|
||||
## 0.35.1 Mar 04, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Remove NodeJs-only `syncify` function, not exported by the package (with binary dependency for Node)
|
||||
|
||||
### 0.34.1 Feb 13, 2019
|
||||
|
||||
## 0.34.1 Feb 13, 2019
|
||||
|
||||
Changes:
|
||||
|
||||
- Keyring now allows for ed25519 and sr25519 instances. Upon creation, you need to pass the `ed25519`or `sr25519` string to denote the type, i.e. `new Keyring({ type: 'sr25519' })`
|
||||
|
||||
### 0.33.1 Nov 22, 2018
|
||||
|
||||
### 0.32.1 Oct 18, 2018
|
||||
## 0.33.1 Nov 22, 2018
|
||||
|
||||
### 0.31.1 Oct 12, 2018
|
||||
## 0.32.1 Oct 18, 2018
|
||||
|
||||
### 0.30.1 Sep 11, 2018
|
||||
## 0.31.1 Oct 12, 2018
|
||||
|
||||
### 0.29.1 Sep 01, 2018
|
||||
## 0.30.1 Sep 11, 2018
|
||||
|
||||
### 0.28.1 Aug 11, 2018
|
||||
## 0.29.1 Sep 01, 2018
|
||||
|
||||
### 0.27.1 Aug 06, 2018
|
||||
## 0.28.1 Aug 11, 2018
|
||||
|
||||
### 0.26.1 Jun 27, 2018
|
||||
## 0.27.1 Aug 06, 2018
|
||||
|
||||
### 0.25.1 Jun 26, 2018
|
||||
## 0.26.1 Jun 27, 2018
|
||||
|
||||
### 0.24.1 Jun 22, 2018
|
||||
## 0.25.1 Jun 26, 2018
|
||||
|
||||
### 0.23.1 Jun 21, 2018
|
||||
## 0.24.1 Jun 22, 2018
|
||||
|
||||
### 0.22.1 May 24, 2018
|
||||
## 0.23.1 Jun 21, 2018
|
||||
|
||||
### 0.21.1 May 17, 2018
|
||||
## 0.22.1 May 24, 2018
|
||||
|
||||
### 0.20.1 May 16, 2018
|
||||
## 0.21.1 May 17, 2018
|
||||
|
||||
### 0.19.1 Apr 19, 2018
|
||||
## 0.20.1 May 16, 2018
|
||||
|
||||
### 0.18.1 Mar 20, 2018
|
||||
## 0.19.1 Apr 19, 2018
|
||||
|
||||
### 0.17.1 Mar0 8, 2018
|
||||
## 0.18.1 Mar 20, 2018
|
||||
|
||||
### 0.16.1 Feb 23, 2018
|
||||
## 0.17.1 Mar0 8, 2018
|
||||
|
||||
### 0.15.1 Feb 15, 2018
|
||||
## 0.16.1 Feb 23, 2018
|
||||
|
||||
### 0.14.1 Feb 07, 2018
|
||||
## 0.15.1 Feb 15, 2018
|
||||
|
||||
### 0.13.1 Jan 25, 2018
|
||||
## 0.14.1 Feb 07, 2018
|
||||
|
||||
### 0.12.1 Jan 11, 2018
|
||||
## 0.13.1 Jan 25, 2018
|
||||
|
||||
### 0.11.1 Dec 30, 2017
|
||||
## 0.12.1 Jan 11, 2018
|
||||
|
||||
### 0.10.1 Dec 17, 2017
|
||||
## 0.11.1 Dec 30, 2017
|
||||
|
||||
### 0.9.1 Dec 14, 2017
|
||||
## 0.10.1 Dec 17, 2017
|
||||
|
||||
### 0.8.1 Dec 12, 2017
|
||||
## 0.9.1 Dec 14, 2017
|
||||
|
||||
### 0.7.1 Dec 11, 2017
|
||||
## 0.8.1 Dec 12, 2017
|
||||
|
||||
### 0.6.1 Nov 30, 2017
|
||||
## 0.7.1 Dec 11, 2017
|
||||
|
||||
### 0.5.1 Nov 20, 2017
|
||||
## 0.6.1 Nov 30, 2017
|
||||
|
||||
### 0.4.1 Nov 19, 2017
|
||||
## 0.5.1 Nov 20, 2017
|
||||
|
||||
### 0.3.1 Nov 19, 2017
|
||||
## 0.4.1 Nov 19, 2017
|
||||
|
||||
### 0.2.1 Nov 18, 2017
|
||||
## 0.3.1 Nov 19, 2017
|
||||
|
||||
## 0.2.1 Nov 18, 2017
|
||||
|
||||
+1
-2
@@ -1,5 +1,4 @@
|
||||
// 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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
module.exports = require('@polkadot/dev/config/babel');
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#!/bin/bash
|
||||
# Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
function copy_folder () {
|
||||
SRC="packages/$1/build"
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
// 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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const config = require('@polkadot/dev/config/jest');
|
||||
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
// 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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const path = require('path');
|
||||
|
||||
|
||||
+1
-1
@@ -9,5 +9,5 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "3.2.1"
|
||||
"version": "3.5.1"
|
||||
}
|
||||
|
||||
+10
-6
@@ -7,7 +7,8 @@
|
||||
"packages/*"
|
||||
],
|
||||
"resolutions": {
|
||||
"babel-core": "^7.0.0-bridge.0"
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"typescript": "^4.0.3"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-ts",
|
||||
@@ -21,10 +22,13 @@
|
||||
"test:one": "polkadot-dev-run-test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.11.1",
|
||||
"@polkadot/dev": "^0.55.38",
|
||||
"@polkadot/ts": "^0.3.38",
|
||||
"@types/jest": "^26.0.9"
|
||||
"@babel/core": "^7.11.6",
|
||||
"@polkadot/dev": "^0.55.54",
|
||||
"@polkadot/ts": "^0.3.47",
|
||||
"@types/jest": "^26.0.14",
|
||||
"typedoc": "^0.19.2",
|
||||
"typedoc-plugin-markdown": "^3.0.2",
|
||||
"typedoc-plugin-no-inherit": "^1.2.0"
|
||||
},
|
||||
"version": "3.2.1"
|
||||
"version": "3.5.1"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/keyring",
|
||||
"version": "3.2.1",
|
||||
"version": "3.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.11.2",
|
||||
"@polkadot/util": "3.2.1",
|
||||
"@polkadot/util-crypto": "3.2.1"
|
||||
"@polkadot/util": "3.5.1",
|
||||
"@polkadot/util-crypto": "3.5.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// default substrate dev phrase
|
||||
const DEV_PHRASE = 'bottom drive obey lake curtain smoke basket hold race lonely fit walk';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { cryptoWaitReady, setSS58Format, encodeAddress } from '@polkadot/util-crypto';
|
||||
@@ -31,6 +30,12 @@ describe('keypair', (): void => {
|
||||
).toEqual(publicKeyTwo);
|
||||
});
|
||||
|
||||
it('creates via a dev seed', (): void => {
|
||||
expect(
|
||||
keyring.addFromUri('//Alice').address
|
||||
).toEqual('5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu');
|
||||
});
|
||||
|
||||
it('creates a ed25519 pair via mnemonicToSeed', (): void => {
|
||||
expect(
|
||||
keyring.addFromUri(
|
||||
@@ -204,6 +209,36 @@ describe('keypair', (): void => {
|
||||
).toEqual('5DzMsaYFhmpRdErWrP6K6PD7UXzYoeETToSBUrZSvxasqWRz');
|
||||
});
|
||||
|
||||
it('allows creation from JSON', (): void => {
|
||||
keyring.setSS58Format(2);
|
||||
const pair = keyring.createFromJson(
|
||||
JSON.parse('{"address":"0x02fde629668eb2bcc7d748f40a7e597f7c7b363498ff3db31f03ce4854937883ad","encoded":"qIhAhKqtf2iyEoWEr8nmBdksSI8EHHCpgJHToqd6Pl8AgAAAAQAAAAgAAADDZ//fj/BRRj+0+bl1KAlYgoPJp6nEUwiw0fVqO2BW4mjEgQ+iWwJEgDf1JUtecbzOlfhTXBzqX/dIYzLgUADrF4EFEPpboCWiU1iN7W/3DM1cOTRVvTGcbdIqW//z3axhz961qzeJVUIFgllwGe/euLUPIlKbIkiN/CsRYdQ=","encoding":{"content":["pkcs8","ecdsa"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{"genesisHash":"0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe","name":"ecdsa","tags":[],"whenCreated":1600925898271}}')
|
||||
);
|
||||
|
||||
expect(pair.address).toEqual('DHL8HKFuTTR55JzzLmkJRCAfPBbuevKaT9cXikxbEV97Ko8');
|
||||
expect(pair.publicKey).toEqual(hexToU8a('0x02fde629668eb2bcc7d748f40a7e597f7c7b363498ff3db31f03ce4854937883ad'));
|
||||
});
|
||||
|
||||
it('fails toJson() when password is incorrect', (): void => {
|
||||
const pair = keyring.createFromJson(
|
||||
JSON.parse('{"address":"0x02fde629668eb2bcc7d748f40a7e597f7c7b363498ff3db31f03ce4854937883ad","encoded":"qIhAhKqtf2iyEoWEr8nmBdksSI8EHHCpgJHToqd6Pl8AgAAAAQAAAAgAAADDZ//fj/BRRj+0+bl1KAlYgoPJp6nEUwiw0fVqO2BW4mjEgQ+iWwJEgDf1JUtecbzOlfhTXBzqX/dIYzLgUADrF4EFEPpboCWiU1iN7W/3DM1cOTRVvTGcbdIqW//z3axhz961qzeJVUIFgllwGe/euLUPIlKbIkiN/CsRYdQ=","encoding":{"content":["pkcs8","ecdsa"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{"genesisHash":"0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe","name":"ecdsa","tags":[],"whenCreated":1600925898271}}')
|
||||
);
|
||||
|
||||
expect(
|
||||
() => pair.toJson('invalid')
|
||||
).toThrow(/Unable to decode using the supplied passphrase/);
|
||||
});
|
||||
|
||||
it('pass toJson() when password is correct', (): void => {
|
||||
const pair = keyring.createFromJson(
|
||||
JSON.parse('{"address":"0x02fde629668eb2bcc7d748f40a7e597f7c7b363498ff3db31f03ce4854937883ad","encoded":"qIhAhKqtf2iyEoWEr8nmBdksSI8EHHCpgJHToqd6Pl8AgAAAAQAAAAgAAADDZ//fj/BRRj+0+bl1KAlYgoPJp6nEUwiw0fVqO2BW4mjEgQ+iWwJEgDf1JUtecbzOlfhTXBzqX/dIYzLgUADrF4EFEPpboCWiU1iN7W/3DM1cOTRVvTGcbdIqW//z3axhz961qzeJVUIFgllwGe/euLUPIlKbIkiN/CsRYdQ=","encoding":{"content":["pkcs8","ecdsa"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{"genesisHash":"0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe","name":"ecdsa","tags":[],"whenCreated":1600925898271}}')
|
||||
);
|
||||
|
||||
expect(
|
||||
() => pair.toJson('testing')
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it('signs and verifies', (): void => {
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
const pair = keyring.getPair(addressKeyOne);
|
||||
@@ -228,13 +263,43 @@ describe('keypair', (): void => {
|
||||
expect(pair.address).toEqual('0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
|
||||
});
|
||||
|
||||
it('encodes a pair toJSON', (): void => {
|
||||
const pair = keyring.createFromUri(PHRASE);
|
||||
const json = pair.toJson('password');
|
||||
|
||||
expect(json.address).toEqual('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077');
|
||||
expect(json.encoding).toEqual({
|
||||
content: ['pkcs8', 'ethereum'],
|
||||
type: ['scrypt', 'xsalsa20-poly1305'],
|
||||
version: '3'
|
||||
});
|
||||
});
|
||||
|
||||
it('allows adding from JSON', (): void => {
|
||||
const pair = keyring.addFromJson(
|
||||
JSON.parse('{"address":"34wtVv7QQ5NsJQDcyWYnz9J5XrNgYE3VcRfKQ6scSt7o1ixi","encoded":"U8qFEaghhmNV2PgFhjqzmhyUy37Ok7abfFU2MNsBd0sAgAAAAQAAAAgAAAA3+NniKogzNphiMNueB1X0sGA07B6CaXWfpXPx45iSXoTTprwzU5mOoSqUWO0GKHROI72LN+uJ8Yfv6Ll6JOOV3VPKfoVoFmYm+zDrrMPa0gk5E5kUuSijxADcE6zUrliPVr0Ix/qaghu5SJ7RtWDQLBf4Hp86SJ8Gg6gTSSk=","encoding":{"content":["pkcs8","ethereum"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{}}')
|
||||
JSON.parse('{"address":"KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou","encoded":"U8qFEaghhmNV2PgFhjqzmhyUy37Ok7abfFU2MNsBd0sAgAAAAQAAAAgAAAA3+NniKogzNphiMNueB1X0sGA07B6CaXWfpXPx45iSXoTTprwzU5mOoSqUWO0GKHROI72LN+uJ8Yfv6Ll6JOOV3VPKfoVoFmYm+zDrrMPa0gk5E5kUuSijxADcE6zUrliPVr0Ix/qaghu5SJ7RtWDQLBf4Hp86SJ8Gg6gTSSk=","encoding":{"content":["pkcs8","ethereum"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{}}')
|
||||
);
|
||||
|
||||
expect(pair.publicKey).toEqual(hexToU8a('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077'));
|
||||
expect(pair.publicKey).toEqual(hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077'));
|
||||
expect(pair.address).toEqual('0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
|
||||
|
||||
pair.decodePkcs8('password');
|
||||
|
||||
expect(pair.isLocked).toBe(false);
|
||||
expect(pair.publicKey).toEqual(hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077'));
|
||||
expect(pair.address).toBe('0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
|
||||
});
|
||||
|
||||
it('allows for signing/verification', (): void => {
|
||||
const MESSAGE = stringToU8a('just some test message');
|
||||
const signer = keyring.createFromUri(PHRASE);
|
||||
const verifier = keyring.addFromJson(
|
||||
JSON.parse('{"address":"KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou","encoded":"U8qFEaghhmNV2PgFhjqzmhyUy37Ok7abfFU2MNsBd0sAgAAAAQAAAAgAAAA3+NniKogzNphiMNueB1X0sGA07B6CaXWfpXPx45iSXoTTprwzU5mOoSqUWO0GKHROI72LN+uJ8Yfv6Ll6JOOV3VPKfoVoFmYm+zDrrMPa0gk5E5kUuSijxADcE6zUrliPVr0Ix/qaghu5SJ7RtWDQLBf4Hp86SJ8Gg6gTSSk=","encoding":{"content":["pkcs8","ethereum"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{}}')
|
||||
);
|
||||
|
||||
const signature = signer.sign(MESSAGE);
|
||||
|
||||
expect(verifier.verify(MESSAGE, signature)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { detectPackage } from '@polkadot/util';
|
||||
import { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeypairType, Keypair } from '@polkadot/util-crypto/types';
|
||||
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta, KeyringOptions } from './types';
|
||||
@@ -104,15 +103,8 @@ export default class Keyring implements KeyringInstance {
|
||||
* of an account backup), and then generates a keyring pair from it that it passes to
|
||||
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
||||
*/
|
||||
public addFromJson ({ address, encoded, encoding: { content, type, version }, meta }: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair {
|
||||
const cryptoType = version === '0' || !Array.isArray(content)
|
||||
? this.type
|
||||
: content[1];
|
||||
const encType = !Array.isArray(type)
|
||||
? [type]
|
||||
: type;
|
||||
|
||||
return this.addFromAddress(address, meta, isHex(encoded) ? hexToU8a(encoded) : base64Decode(encoded), cryptoType, ignoreChecksum, encType);
|
||||
public addFromJson (json: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair {
|
||||
return this.addPair(this.createFromJson(json, ignoreChecksum));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,6 +141,27 @@ export default class Keyring implements KeyringInstance {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name createFromJson
|
||||
* @description Creates a pair from a JSON keyfile
|
||||
*/
|
||||
public createFromJson ({ address, encoded, encoding: { content, type, version }, meta }: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair {
|
||||
const cryptoType = version === '0' || !Array.isArray(content)
|
||||
? this.type
|
||||
: content[1];
|
||||
const encType = !Array.isArray(type)
|
||||
? [type]
|
||||
: type;
|
||||
const publicKey = isHex(address)
|
||||
? hexToU8a(address)
|
||||
: this.decodeAddress(address, ignoreChecksum);
|
||||
const decoded = isHex(encoded)
|
||||
? hexToU8a(encoded)
|
||||
: base64Decode(encoded);
|
||||
|
||||
return createPair({ toSS58: this.encodeAddress, type: cryptoType }, { publicKey, secretKey: new Uint8Array() }, meta, decoded, encType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name createFromUri
|
||||
* @summary Creates a Keypair from an suri
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import testingPairs from '../testingPairs';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeyringPair$JsonEncodingTypes } from '../types';
|
||||
import { PairInfo } from './types';
|
||||
@@ -8,7 +7,7 @@ import { PairInfo } from './types';
|
||||
import { assert, stringToU8a, u8aFixLength } from '@polkadot/util';
|
||||
import { naclDecrypt, scryptEncode, scryptFromU8a } from '@polkadot/util-crypto';
|
||||
|
||||
import { NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH, SCRYPT_LENGTH } from './defaults';
|
||||
import { ENCODING, NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH, SCRYPT_LENGTH } from './defaults';
|
||||
|
||||
const SEED_OFFSET = PKCS8_HEADER.length;
|
||||
|
||||
@@ -43,7 +42,7 @@ function decodePkcs8 (encoded: Uint8Array): DecodeResult {
|
||||
};
|
||||
}
|
||||
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null, encType: KeyringPair$JsonEncodingTypes[] = ['scrypt', 'xsalsa20-poly1305']): DecodeResult {
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null, encType: KeyringPair$JsonEncodingTypes[] = ENCODING): DecodeResult {
|
||||
assert(encrypted, 'No encrypted data available to decode');
|
||||
assert(passphrase || !encType.includes('xsalsa20-poly1305'), 'Password required to decode encypted data');
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeyringPair$JsonEncodingTypes } from '../types';
|
||||
|
||||
const ENCODING: KeyringPair$JsonEncodingTypes[] = ['scrypt', 'xsalsa20-poly1305'];
|
||||
const NONCE_LENGTH = 24;
|
||||
const PKCS8_DIVIDER = new Uint8Array([161, 35, 3, 33, 0]);
|
||||
const PKCS8_HEADER = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
|
||||
@@ -12,6 +14,7 @@ const SEED_LENGTH = 32;
|
||||
const SCRYPT_LENGTH = SALT_LENGTH + (3 * 4);
|
||||
|
||||
export {
|
||||
ENCODING,
|
||||
NONCE_LENGTH,
|
||||
PKCS8_DIVIDER,
|
||||
PKCS8_HEADER,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import testingPairs from '../testingPairs';
|
||||
import { PKCS8_DIVIDER, PKCS8_HEADER, NONCE_LENGTH, PUB_LENGTH, SCRYPT_LENGTH, SEC_LENGTH } from './defaults';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { PairInfo } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { cryptoWaitReady, encodeAddress as toSS58, setSS58Format } from '@polkadot/util-crypto';
|
||||
|
||||
@@ -25,6 +24,11 @@ describe('pair', (): void => {
|
||||
).toEqual(
|
||||
new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79])
|
||||
);
|
||||
expect(
|
||||
keyring.alice.addressRaw
|
||||
).toEqual(
|
||||
new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79])
|
||||
);
|
||||
});
|
||||
|
||||
it('allows signing', (): void => {
|
||||
@@ -111,6 +115,7 @@ describe('pair', (): void => {
|
||||
const pair = createPair({ toSS58, type: 'ethereum' }, { publicKey: hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077') });
|
||||
|
||||
expect(pair.address).toEqual('0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
|
||||
expect(pair.addressRaw).toEqual(hexToU8a('0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Keypair, KeypairType } from '@polkadot/util-crypto/types';
|
||||
import { KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta, SignOptions } from '../types';
|
||||
import { PairInfo } from './types';
|
||||
|
||||
import { assert, u8aConcat } from '@polkadot/util';
|
||||
import { blake2AsU8a, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify, secp256k1Compress, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, secp256k1Verify } from '@polkadot/util-crypto';
|
||||
import { assert, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
import { blake2AsU8a, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, secp256k1Verify, secp256k1Compress } from '@polkadot/util-crypto';
|
||||
|
||||
import decode from './decode';
|
||||
import encode from './encode';
|
||||
@@ -35,54 +34,28 @@ const TYPE_PREFIX = {
|
||||
};
|
||||
|
||||
const TYPE_SIGNATURE = {
|
||||
ecdsa: (message: Uint8Array, pair: Partial<Keypair>) =>
|
||||
secp256k1Sign(message, pair, 'blake2'),
|
||||
ecdsa: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'blake2'),
|
||||
ed25519: naclSign,
|
||||
ethereum: (message: Uint8Array, pair: Partial<Keypair>) =>
|
||||
secp256k1Sign(message, pair, 'keccak'),
|
||||
ethereum: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'keccak'),
|
||||
sr25519: schnorrkelSign
|
||||
};
|
||||
|
||||
const TYPE_ADDRESS = {
|
||||
ecdsa: (p: Uint8Array) => p.length > 32 ? blake2AsU8a(p) : p,
|
||||
ed25519: (p: Uint8Array) => p,
|
||||
ethereum: (p: Uint8Array) => keccakAsU8a(secp256k1Expand(p)),
|
||||
sr25519: (p: Uint8Array) => p
|
||||
};
|
||||
|
||||
const TYPE_VERIFY = {
|
||||
ecdsa: (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) =>
|
||||
secp256k1Verify(message, signature, blake2AsU8a(publicKey), 'blake2'),
|
||||
ecdsa: (m: Uint8Array, s: Uint8Array, p: Uint8Array) => secp256k1Verify(m, s, TYPE_ADDRESS.ecdsa(p), 'blake2'),
|
||||
ed25519: naclVerify,
|
||||
ethereum: (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) =>
|
||||
secp256k1Verify(message, signature, keccakAsU8a(publicKey), 'keccak'),
|
||||
ethereum: (m: Uint8Array, s: Uint8Array, p: Uint8Array) => secp256k1Verify(m, s, TYPE_ADDRESS.ethereum(p), 'keccak', true),
|
||||
sr25519: schnorrkelVerify
|
||||
};
|
||||
|
||||
function isEmpty (u8a: Uint8Array): boolean {
|
||||
return u8a.reduce((count, u8): number => count + u8, 0) === 0;
|
||||
}
|
||||
|
||||
function fromSeed (type: KeypairType, seed: Uint8Array): Keypair {
|
||||
return TYPE_FROM_SEED[type](seed);
|
||||
}
|
||||
|
||||
function sign (type: KeypairType, message: Uint8Array, pair: Partial<Keypair>, { withType = false }: SignOptions = {}): Uint8Array {
|
||||
return u8aConcat(
|
||||
// for multi-signatures, i.e. with indicator, append the signature type as per
|
||||
// the MultiSignature enum
|
||||
withType
|
||||
? TYPE_PREFIX[type]
|
||||
: SIG_TYPE_NONE,
|
||||
TYPE_SIGNATURE[type](message, pair)
|
||||
);
|
||||
}
|
||||
|
||||
function verify (type: KeypairType, message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean {
|
||||
return TYPE_VERIFY[type](message, signature, publicKey);
|
||||
}
|
||||
|
||||
function getAddress (type: KeypairType, publicKey: Uint8Array, isCompressed = false): Uint8Array {
|
||||
if (type === 'ecdsa' && publicKey.length > 32) {
|
||||
return blake2AsU8a(publicKey);
|
||||
} else if (type === 'ethereum' && isCompressed) {
|
||||
return secp256k1Compress(publicKey).slice(-32);
|
||||
}
|
||||
|
||||
return publicKey;
|
||||
return u8a.reduce((count, u8) => count + u8, 0) === 0;
|
||||
}
|
||||
|
||||
// Not 100% correct, since it can be a Uint8Array, but an invalid one - just say "undefined" is anything non-valid
|
||||
@@ -122,18 +95,47 @@ function isLocked (secretKey?: Uint8Array): secretKey is undefined {
|
||||
* property that indicates whether the public key value of the `encoded` property is encoded or not.
|
||||
*/
|
||||
export default function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null, encTypes?: KeyringPair$JsonEncodingTypes[]): KeyringPair {
|
||||
const decodePkcs8 = (passphrase?: string, userEncoded?: Uint8Array | null): void => {
|
||||
const decoded = decode(passphrase, userEncoded || encoded, encTypes);
|
||||
|
||||
if (decoded.secretKey.length === 64) {
|
||||
publicKey = decoded.publicKey;
|
||||
secretKey = decoded.secretKey;
|
||||
} else {
|
||||
const pair = TYPE_FROM_SEED[type](decoded.secretKey);
|
||||
|
||||
publicKey = pair.publicKey;
|
||||
secretKey = pair.secretKey;
|
||||
}
|
||||
};
|
||||
|
||||
const recode = (passphrase?: string): Uint8Array => {
|
||||
isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
|
||||
|
||||
encoded = encode({ publicKey, secretKey }, passphrase); // re-encode, latest version
|
||||
encTypes = undefined; // swap to defaults, latest version follows
|
||||
|
||||
return encoded;
|
||||
};
|
||||
|
||||
const encodeAddress = (): string => {
|
||||
const raw = TYPE_ADDRESS[type](publicKey);
|
||||
|
||||
return type === 'ethereum'
|
||||
? ethereumEncode(raw)
|
||||
: toSS58(raw);
|
||||
};
|
||||
|
||||
return {
|
||||
get address (): string {
|
||||
return encodeAddress();
|
||||
},
|
||||
get addressRaw (): Uint8Array {
|
||||
const raw = TYPE_ADDRESS[type](publicKey);
|
||||
|
||||
return type === 'ethereum'
|
||||
? ethereumEncode(getAddress(type, publicKey))
|
||||
: toSS58(getAddress(type, publicKey));
|
||||
? raw.slice(-20)
|
||||
: raw;
|
||||
},
|
||||
get isLocked (): boolean {
|
||||
return isLocked(secretKey);
|
||||
@@ -148,19 +150,7 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
|
||||
return type;
|
||||
},
|
||||
// eslint-disable-next-line sort-keys
|
||||
decodePkcs8: (passphrase?: string, userEncoded?: Uint8Array | null): void => {
|
||||
const decoded = decode(passphrase, userEncoded || encoded, encTypes);
|
||||
|
||||
if (decoded.secretKey.length === 64) {
|
||||
publicKey = decoded.publicKey;
|
||||
secretKey = decoded.secretKey;
|
||||
} else {
|
||||
const pair = fromSeed(type, decoded.secretKey);
|
||||
|
||||
publicKey = pair.publicKey;
|
||||
secretKey = pair.secretKey;
|
||||
}
|
||||
},
|
||||
decodePkcs8,
|
||||
derive: (suri: string, meta?: KeyringPair$Meta): KeyringPair => {
|
||||
assert(!isLocked(secretKey), 'Cannot derive on a locked keypair');
|
||||
|
||||
@@ -177,14 +167,24 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
|
||||
setMeta: (additional: KeyringPair$Meta): void => {
|
||||
meta = { ...meta, ...additional };
|
||||
},
|
||||
sign: (message: Uint8Array, options?: SignOptions): Uint8Array => {
|
||||
sign: (message: Uint8Array, options: SignOptions = {}): Uint8Array => {
|
||||
assert(!isLocked(secretKey), 'Cannot sign with a locked key pair');
|
||||
|
||||
return sign(type, message, { publicKey, secretKey }, options);
|
||||
return u8aConcat(
|
||||
options.withType
|
||||
? TYPE_PREFIX[type]
|
||||
: SIG_TYPE_NONE,
|
||||
TYPE_SIGNATURE[type](message, { publicKey, secretKey })
|
||||
);
|
||||
},
|
||||
toJson: (passphrase?: string): KeyringPair$Json => {
|
||||
const address = ['ecdsa', 'ethereum'].includes(type)
|
||||
? u8aToHex(secp256k1Compress(publicKey))
|
||||
: encodeAddress();
|
||||
|
||||
return toJson(type, { address, meta }, recode(passphrase), !!passphrase);
|
||||
},
|
||||
toJson: (passphrase?: string): KeyringPair$Json =>
|
||||
toJson(type, { address: toSS58(getAddress(type, publicKey, true)), meta }, recode(passphrase), !!passphrase),
|
||||
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
|
||||
verify(type, message, signature, publicKey)
|
||||
TYPE_VERIFY[type](message, signature, publicKey)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '../types';
|
||||
|
||||
@@ -26,6 +25,7 @@ const json: KeyringPair$Json = {
|
||||
export default function everybody (): KeyringPair {
|
||||
const pair: KeyringPair = {
|
||||
address,
|
||||
addressRaw: publicKey,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
decodePkcs8: (passphrase?: string, encoded?: Uint8Array): void =>
|
||||
undefined,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import testingPairs from '../testingPairs';
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import { KeyringPair$Json, KeyringPair$Meta } from '../types';
|
||||
import { KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta } from '../types';
|
||||
|
||||
import { base64Encode } from '@polkadot/util-crypto';
|
||||
|
||||
import { ENCODING } from './defaults';
|
||||
|
||||
interface PairStateJson {
|
||||
address: string;
|
||||
meta: KeyringPair$Meta;
|
||||
@@ -14,6 +15,9 @@ interface PairStateJson {
|
||||
|
||||
// version 2 - nonce, encoded (previous)
|
||||
// version 3 - salt, nonce, encoded
|
||||
const VERSION = '3';
|
||||
|
||||
const ENC_NONE: KeyringPair$JsonEncodingTypes[] = ['none'];
|
||||
|
||||
export default function toJson (type: KeypairType, { address, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
|
||||
return {
|
||||
@@ -22,9 +26,9 @@ export default function toJson (type: KeypairType, { address, meta }: PairStateJ
|
||||
encoding: {
|
||||
content: ['pkcs8', type],
|
||||
type: isEncrypted
|
||||
? ['scrypt', 'xsalsa20-poly1305']
|
||||
: ['none'],
|
||||
version: '3'
|
||||
? ENCODING
|
||||
: ENC_NONE,
|
||||
version: VERSION
|
||||
},
|
||||
meta
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export interface PairInfo {
|
||||
publicKey: Uint8Array;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeyringPairs, KeyringPair } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// From https://github.com/paritytech/substrate/wiki/Secret-URI-Test-Vectors
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeyringInstance, KeyringOptions } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { cryptoWaitReady } from '@polkadot/util-crypto';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { KeyringPair, KeyringOptions } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @polkadot/keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from '@polkadot/util-crypto/address/types';
|
||||
import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
@@ -35,6 +34,7 @@ export interface SignOptions {
|
||||
|
||||
export interface KeyringPair {
|
||||
readonly address: string;
|
||||
readonly addressRaw: Uint8Array;
|
||||
readonly meta: KeyringPair$Meta;
|
||||
readonly isLocked: boolean;
|
||||
readonly publicKey: Uint8Array;
|
||||
@@ -72,6 +72,7 @@ export interface KeyringInstance {
|
||||
addFromMnemonic (mnemonic: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
|
||||
addFromSeed (seed: Uint8Array, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
|
||||
addFromUri (suri: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
|
||||
createFromJson (json: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair;
|
||||
createFromUri (suri: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
|
||||
getPair (address: string | Uint8Array): KeyringPair;
|
||||
getPairs (): KeyringPair[];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-crypto",
|
||||
"version": "3.2.1",
|
||||
"version": "3.5.1",
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -26,12 +26,12 @@
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/util": "3.2.1",
|
||||
"@polkadot/wasm-crypto": "^1.3.1",
|
||||
"@polkadot/util": "3.5.1",
|
||||
"@polkadot/wasm-crypto": "^1.4.1",
|
||||
"base-x": "^3.0.8",
|
||||
"bip39": "^3.0.2",
|
||||
"blakejs": "^1.1.0",
|
||||
"bn.js": "^5.1.2",
|
||||
"bn.js": "^5.1.3",
|
||||
"elliptic": "^6.5.3",
|
||||
"js-sha3": "^0.8.0",
|
||||
"pbkdf2": "^3.1.1",
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import checkAddress from './check';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import sshash from './sshash';
|
||||
|
||||
export default function checkChecksum (decoded: Uint8Array): [boolean, number] {
|
||||
const isPublicKey = decoded.length === 35;
|
||||
const isPublicKey = [35, 36].includes(decoded.length);
|
||||
|
||||
// non-publicKeys has 1 byte checksums, else default to 2
|
||||
const length = decoded.length - (isPublicKey ? 2 : 1);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
import { stringToU8a, u8aToHex, hexToU8a } from '@polkadot/util';
|
||||
|
||||
import testingPairs, { TestKeyringMap } from '../../../keyring/src/testingPairs';
|
||||
import decode from './decode';
|
||||
@@ -74,6 +73,14 @@ describe('decodeAddress', (): void => {
|
||||
).toEqual(new Uint8Array([42, 44, 10, 0, 0, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('decodes a 33-byte address', (): void => {
|
||||
expect(
|
||||
decode('KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou')
|
||||
).toEqual(
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
);
|
||||
});
|
||||
|
||||
it.skip('allows invalid prefix (in list)', (): void => {
|
||||
expect(
|
||||
(): Uint8Array => decode('6GfvWUvHvU8otbZ7sFhXH4eYeMcKdUkL61P3nFy52efEPVUx')
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { allowedSS58 } from './ss58';
|
||||
|
||||
const defaults = {
|
||||
allowedDecodedLengths: [1, 2, 4, 8, 32],
|
||||
allowedDecodedLengths: [1, 2, 4, 8, 32, 33],
|
||||
// publicKey has prefix + 2 checksum bytes, short only prefix + 1 checksum byte
|
||||
allowedEncodedLengths: [3, 4, 6, 10, 35],
|
||||
allowedEncodedLengths: [3, 4, 6, 10, 35, 36],
|
||||
allowedPrefix: allowedSS58,
|
||||
prefix: 42
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
|
||||
@@ -67,11 +66,17 @@ describe('encode', (): void => {
|
||||
).toEqual('as7QnGMf');
|
||||
});
|
||||
|
||||
it('enodes a 8-byte address', (): void => {
|
||||
it('encodes a 8-byte address', (): void => {
|
||||
expect(
|
||||
encode(
|
||||
new Uint8Array([42, 44, 10, 0, 0, 0, 0, 0]), 68
|
||||
)
|
||||
).toEqual('4q7qY5RBG7Z4wv');
|
||||
});
|
||||
|
||||
it('encodes an 33-byte address', (): void => {
|
||||
expect(
|
||||
encode('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
).toEqual('KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
@@ -19,7 +18,7 @@ export default function encode (_key: Uint8Array | string, ss58Format: Prefix =
|
||||
|
||||
assert(defaults.allowedDecodedLengths.includes(key.length), `Expected a valid key to convert, with length ${defaults.allowedDecodedLengths.join(', ')}`);
|
||||
|
||||
const isPublicKey = key.length === 32;
|
||||
const isPublicKey = [32, 33].includes(key.length);
|
||||
const input = u8aConcat(new Uint8Array([ss58Format]), key);
|
||||
const hash = sshash(input);
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import encodeDerivedAddress from './encodeDerived';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import encodeMultiAddress from './encodeMulti';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
import { addressEq } from '.';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aEq } from '@polkadot/util';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import checkAddress from './check';
|
||||
import checkAddressChecksum from './checkChecksum';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import createKeyDerived from './keyDerived';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { createKeyMulti } from '.';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@polkadot/util';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import encode from './encode';
|
||||
import setSS58Format from './setSS58Format';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { sortAddresses } from '.';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// 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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Prefix } from './types';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const allowedSS58 = [
|
||||
// Polkadot
|
||||
@@ -13,6 +12,8 @@ export const allowedSS58 = [
|
||||
16, 17,
|
||||
// Dothereum
|
||||
20,
|
||||
// Dock testnet and mainnet
|
||||
21, 22,
|
||||
// Substrate default
|
||||
42, 43,
|
||||
// Substrate BBQ (deprecated)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// https://github.com/multiformats/js-multibase/blob/master/src/constants.js
|
||||
const BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aToString } from '@polkadot/util';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// adapted from https://github.com/multiformats/js-multibase/blob/424709195b46ffb1d6f2f69a7707598ebe751e5e/src/rfc4648.js
|
||||
|
||||
@@ -25,20 +24,14 @@ const LOOKUP = BASE32_ALPHABET
|
||||
* From the provided input, decode the base32 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export default function base32Decode (value: string, ipfsCompat = false): Uint8Array {
|
||||
if (ipfsCompat) {
|
||||
assert(value[0] === 'b', "Expected IPFS base32 identifier 'b' at string start");
|
||||
validate(value, ipfsCompat);
|
||||
|
||||
value = value.substr(1);
|
||||
}
|
||||
|
||||
validate(value);
|
||||
|
||||
const out = new Uint8Array((value.length * BITS_PER_CHAR / 8) | 0);
|
||||
const out = new Uint8Array(((value.length - (ipfsCompat ? 1 : 0)) * BITS_PER_CHAR / 8) | 0);
|
||||
let bits = 0;
|
||||
let buffer = 0;
|
||||
let written = 0;
|
||||
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
for (let i = (ipfsCompat ? 1 : 0); i < value.length; ++i) {
|
||||
buffer = (buffer << BITS_PER_CHAR) | LOOKUP[value[i]];
|
||||
bits += BITS_PER_CHAR;
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base58Decode } from '../base58';
|
||||
import { base32Encode } from './';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// adapted from https://github.com/multiformats/js-multibase/blob/424709195b46ffb1d6f2f69a7707598ebe751e5e/src/rfc4648.js
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Encode and decode base32 values
|
||||
@@ -9,3 +8,4 @@
|
||||
export { default as base32Decode } from './decode';
|
||||
export { default as base32Encode } from './encode';
|
||||
export { default as base32Validate } from './validate';
|
||||
export { default as isBase32 } from './is';
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import validate from './validate';
|
||||
|
||||
export default function isBase32 (value?: string | null): boolean {
|
||||
try {
|
||||
return validate(value);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base32Validate } from '.';
|
||||
|
||||
describe('base32Validate', (): void => {
|
||||
it('validates ipfs-compat encoded', (): void => {
|
||||
expect(
|
||||
() => base32Validate('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
|
||||
).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { validateChars } from '../base58/validate';
|
||||
import { BASE32_ALPHABET } from './bs32';
|
||||
@@ -11,6 +10,6 @@ import { BASE32_ALPHABET } from './bs32';
|
||||
* @description
|
||||
* Validates the the supplied value is valid base32
|
||||
*/
|
||||
export default function base32Validate (value: string): true {
|
||||
return validateChars('base32', BASE32_ALPHABET, value);
|
||||
export default function base32Validate (value?: string | null, ipfsCompat = false): true {
|
||||
return validateChars('base32', BASE32_ALPHABET, value, ipfsCompat ? 'b' : undefined);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import baseX from 'base-x';
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert, bufferToU8a } from '@polkadot/util';
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
|
||||
import { bs58 } from './bs58';
|
||||
import validate from './validate';
|
||||
@@ -13,14 +12,8 @@ import validate from './validate';
|
||||
* @description
|
||||
* From the provided input, decode the base58 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export default function base58Decode (value: string, ipfsCompat = false): Uint8Array {
|
||||
if (ipfsCompat) {
|
||||
assert(value[0] === 'z', "Expected IPFS base32 identifier 'z' at string start");
|
||||
export default function base58Decode (value: string, ipfsCompat?: boolean): Uint8Array {
|
||||
validate(value, ipfsCompat);
|
||||
|
||||
value = value.substr(1);
|
||||
}
|
||||
|
||||
validate(value);
|
||||
|
||||
return bufferToU8a(bs58.decode(value));
|
||||
return bufferToU8a(bs58.decode(value.substr(ipfsCompat ? 1 : 0)));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base32Decode } from '../base32';
|
||||
import { base58Encode } from './';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aToBuffer, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -12,7 +11,7 @@ import { bs58 } from './bs58';
|
||||
* @description
|
||||
* From the provided input, create the base58 and return the result as a string.
|
||||
*/
|
||||
export default function base58Encode (value: Uint8Array | string | Buffer | number[], ipfsCompat = false): string {
|
||||
export default function base58Encode (value: Uint8Array | string | Buffer | number[], ipfsCompat?: boolean): string {
|
||||
const out = bs58.encode(
|
||||
u8aToBuffer(u8aToU8a(value))
|
||||
);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Encode and decode base58 values
|
||||
@@ -9,3 +8,4 @@
|
||||
export { default as base58Decode } from './decode';
|
||||
export { default as base58Encode } from './encode';
|
||||
export { default as base58Validate } from './validate';
|
||||
export { default as isBase58 } from './is';
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import validate from './validate';
|
||||
|
||||
export default function isBase58 (value?: string | null, ipfsCompat?: boolean): boolean {
|
||||
try {
|
||||
return validate(value, ipfsCompat);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { BASE58_ALPHABET } from './bs58';
|
||||
|
||||
export function validateChars (type: string, alphabet: string, value: string): true {
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (!alphabet.includes(value[i])) {
|
||||
throw new TypeError(`Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
|
||||
}
|
||||
export function validateChars (type: string, alphabet: string, value?: string | null, startChar?: string): true {
|
||||
assert(value, `Expected non-null, non-empty ${type} input`);
|
||||
assert(!startChar || value[0] === startChar, `Expected ${type} to start with '${startChar || ''}'`);
|
||||
|
||||
for (let i = (startChar ? 1 : 0); i < value.length; i++) {
|
||||
assert(alphabet.includes(value[i]), `Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -20,6 +22,6 @@ export function validateChars (type: string, alphabet: string, value: string): t
|
||||
* @description
|
||||
* Validates the the supplied value is valid base58
|
||||
*/
|
||||
export default function base58Validate (value: string): true {
|
||||
return validateChars('base58', BASE58_ALPHABET, value);
|
||||
export default function base58Validate (value?: string | null, ipfsCompat = false): true {
|
||||
return validateChars('base58', BASE58_ALPHABET, value, ipfsCompat ? 'z' : undefined);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base64Encode } from '.';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aToBuffer, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Encode and decode base64 values
|
||||
@@ -8,4 +7,7 @@
|
||||
|
||||
export { default as base64Decode } from './decode';
|
||||
export { default as base64Encode } from './encode';
|
||||
export { default as base64Pad } from './pad';
|
||||
export { default as base64Trim } from './trim';
|
||||
export { default as base64Validate } from './validate';
|
||||
export { default as isBase64 } from './is';
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import validate from './validate';
|
||||
|
||||
export default function isBase64 (value?: string | null): boolean {
|
||||
try {
|
||||
return validate(value);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base64Pad } from '.';
|
||||
|
||||
describe('base64Pad', (): void => {
|
||||
it('pads a utf-8 string', (): void => {
|
||||
expect(
|
||||
base64Pad('YWJjZA')
|
||||
).toEqual('YWJjZA==');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export default function base64Trim (value: string): string {
|
||||
return value.padEnd(value.length + (value.length % 4), '=');
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base64Trim } from '.';
|
||||
|
||||
describe('base64Trim', (): void => {
|
||||
it('trims a utf-8 string', (): void => {
|
||||
expect(
|
||||
base64Trim('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
|
||||
).toEqual('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export default function base64Trim (value: string): string {
|
||||
while (value.length && value[value.length - 1] === '=') {
|
||||
value = value.slice(0, -1);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base64Validate } from '.';
|
||||
|
||||
describe('base64Validate', (): void => {
|
||||
it('decodes a mixed base64 utf8 string', (): void => {
|
||||
it('validates a mixed base64 utf8 string', (): void => {
|
||||
expect(
|
||||
() => base64Validate('aGVsbG8gd29ybGQg0J/RgNC40LLQtd^GC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
|
||||
).toThrow(/Invalid base64 encoding/);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
/**
|
||||
* @name base64Validate
|
||||
@@ -8,10 +9,9 @@
|
||||
* @description
|
||||
* Validates the the supplied value is valid base64
|
||||
*/
|
||||
export default function base64Validate (value: string): true {
|
||||
if (!(/^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value))) {
|
||||
throw new TypeError('Invalid base64 encoding');
|
||||
}
|
||||
export default function base64Validate (value?: string | null): true {
|
||||
assert(value, 'Expected non-null, non-empty base64 input');
|
||||
assert(/^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value), 'Invalid base64 encoding');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import blakejs from 'blakejs';
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Create blake2b values with specified bitlengths
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { keccakAsU8a } from '../keccak';
|
||||
import { ethereumEncode } from './';
|
||||
|
||||
describe('formatAddress', () => {
|
||||
@@ -35,12 +35,6 @@ describe('formatAddress', () => {
|
||||
const ADDRESS = '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887';
|
||||
|
||||
it('encodes a compressed publicKey', (): void => {
|
||||
expect(
|
||||
ethereumEncode('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
).toEqual(ADDRESS);
|
||||
});
|
||||
|
||||
it('encodes a compressed publicKey (indicator)', (): void => {
|
||||
expect(
|
||||
ethereumEncode('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
).toEqual(ADDRESS);
|
||||
@@ -48,13 +42,15 @@ describe('formatAddress', () => {
|
||||
|
||||
it('encodes an expanded publicKey', (): void => {
|
||||
expect(
|
||||
ethereumEncode('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
ethereumEncode('0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
).toEqual(ADDRESS);
|
||||
});
|
||||
|
||||
it('encodes an expanded publicKey (indicator + x/y)', (): void => {
|
||||
it('encodes a pre-hashed key', (): void => {
|
||||
expect(
|
||||
ethereumEncode('0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
ethereumEncode(
|
||||
keccakAsU8a('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
)
|
||||
).toEqual(ADDRESS);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
// Copyright 2017-2020 @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.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { keccakAsU8a } from '../keccak';
|
||||
import { secp256k1Expand } from '../secp256k1';
|
||||
|
||||
function getH160 (u8a: Uint8Array): Uint8Array {
|
||||
if ([33, 65].includes(u8a.length)) {
|
||||
u8a = keccakAsU8a(secp256k1Expand(u8a));
|
||||
}
|
||||
|
||||
return u8a.slice(-20);
|
||||
}
|
||||
|
||||
export default function ethereumEncode (addressOrPublic?: string | Uint8Array): string {
|
||||
if (!addressOrPublic) {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
let u8aAddress = u8aToU8a(addressOrPublic);
|
||||
const u8aAddress = u8aToU8a(addressOrPublic);
|
||||
|
||||
assert([20, 32, 33, 64, 65].includes(u8aAddress.length), 'Invalid address or publicKey passed');
|
||||
assert([20, 32, 33, 65].includes(u8aAddress.length), 'Invalid address or publicKey passed');
|
||||
|
||||
if (u8aAddress.length > 20) {
|
||||
u8aAddress = keccakAsU8a(secp256k1Expand(u8aAddress)).slice(-20);
|
||||
}
|
||||
|
||||
const address = u8aToHex(u8aAddress, -1, false);
|
||||
const address = u8aToHex(getH160(u8aAddress), -1, false);
|
||||
const hash = u8aToHex(keccakAsU8a(address), -1, false);
|
||||
let result = '';
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user