Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39937bed29 | ||
|
|
3519d21d8e | ||
|
|
8b4dc01ae4 | ||
|
|
bb73f7ae41 | ||
|
|
e6a0f1e2fd | ||
|
|
e8af9b5a1e | ||
|
|
1606dfd9ca | ||
|
|
2fcf59eeb0 | ||
|
|
a7606b6514 | ||
|
|
871b20c90a | ||
|
|
5d93642093 | ||
|
|
2b2fefb2bb | ||
|
|
1ede65db38 | ||
|
|
7090c3daec | ||
|
|
c03f53e097 | ||
|
|
2ec02e2580 | ||
|
|
6b4f9b3d2f | ||
|
|
d20c37ddd0 | ||
|
|
23d46b063c | ||
|
|
085862ce79 | ||
|
|
88580b98a6 | ||
|
|
fb8b80afd7 | ||
|
|
d3b5b4ad38 | ||
|
|
049e8dfde8 | ||
|
|
8230e3ceac | ||
|
|
37358d4d05 | ||
|
|
2cf6ffa3b1 | ||
|
|
d7f2a70071 | ||
|
|
eb96e2a7aa | ||
|
|
6965709351 | ||
|
|
1677d3eace | ||
|
|
4d77bf3c5b | ||
|
|
222bb74a81 | ||
|
|
90b4b61852 | ||
|
|
b1e209dfeb | ||
|
|
88beb4d81c | ||
|
|
3b2ecb87e9 | ||
|
|
79a2cd0e8f | ||
|
|
0c2d105f9d | ||
|
|
944d77c5f2 | ||
|
|
0fdf83effe | ||
|
|
66866e1a8e | ||
|
|
9abcc72143 | ||
|
|
6f0aa35098 | ||
|
|
93fb633ea6 | ||
|
|
5524e50baf | ||
|
|
e1354746b1 | ||
|
|
309722f285 | ||
|
|
4efd88869d | ||
|
|
ee0436a612 | ||
|
|
b4ca8219d9 | ||
|
|
d30a3ddedb | ||
|
|
fa5b5bca7e | ||
|
|
87afdb1d86 | ||
|
|
23f68c63f4 | ||
|
|
184dbccf39 | ||
|
|
06c06a8b73 | ||
|
|
82f5fec822 | ||
|
|
a953b3a78a | ||
|
|
19283b3c28 | ||
|
|
ac06f38118 | ||
|
|
f9e5c033ab | ||
|
|
de7817e6b5 | ||
|
|
5972551313 | ||
|
|
cf0aab479a | ||
|
|
5ab0b173b6 | ||
|
|
cf990dd4ec | ||
|
|
8a6fd0812a | ||
|
|
e93f8d7418 | ||
|
|
36f6407796 | ||
|
|
8ede488aec | ||
|
|
eb9d5b9585 | ||
|
|
20821aad40 | ||
|
|
23fa2b90a8 | ||
|
|
f94a1ea730 | ||
|
|
fc98cd9b45 | ||
|
|
5fdd967666 | ||
|
|
9043f335d4 | ||
|
|
d9231d26aa | ||
|
|
445e0f7ea6 | ||
|
|
ba0a183bd9 | ||
|
|
46beaa1151 | ||
|
|
31d55b218a | ||
|
|
9b30b4b09e | ||
|
|
cc0dd2a2b5 | ||
|
|
31a0a9b9d1 | ||
|
|
9e1e49a1b8 | ||
|
|
313b44388a | ||
|
|
30dd8169bc | ||
|
|
67450bc225 | ||
|
|
e9285e9ccd | ||
|
|
2a45652ce0 | ||
|
|
6d923da266 | ||
|
|
d55b48566b | ||
|
|
3ac567e1c4 | ||
|
|
d19713f548 | ||
|
|
53249f51b8 | ||
|
|
4883106c3d | ||
|
|
b3f60da550 | ||
|
|
644f32dfaa | ||
|
|
bf4c6b497f | ||
|
|
a74ac0a0f9 | ||
|
|
6701f7420f | ||
|
|
8e43a2d105 | ||
|
|
9560301883 | ||
|
|
889bd36432 | ||
|
|
e310a63059 | ||
|
|
98082774f7 | ||
|
|
4d680f4b0c | ||
|
|
ff7c086b5b | ||
|
|
63b38c56ff | ||
|
|
8be38e40a1 | ||
|
|
9eb2f14dee | ||
|
|
733d7c1948 | ||
|
|
08ac55d7cd | ||
|
|
e2f5a85d14 | ||
|
|
77289d0ec0 | ||
|
|
29406a0010 | ||
|
|
006d55f5c1 | ||
|
|
650a3ca4a8 | ||
|
|
d3c8112f66 | ||
|
|
8880e8fa8c | ||
|
|
481f7f6ee0 | ||
|
|
a4cefa4bfc | ||
|
|
7ef169585f | ||
|
|
134b8c3b9f | ||
|
|
1158551b9e | ||
|
|
4c2521bc21 | ||
|
|
993e45d148 | ||
|
|
14a0c3f18b | ||
|
|
9fa71647c1 | ||
|
|
cac98aac03 | ||
|
|
48158927e1 | ||
|
|
51082373e6 | ||
|
|
bf80f76173 | ||
|
|
474e98b6d1 | ||
|
|
07317b8840 | ||
|
|
2e46b688cf | ||
|
|
c87cf8c655 | ||
|
|
9eabf95fc0 |
+3
-1
@@ -17,4 +17,6 @@
|
||||
|
||||
7.0.2
|
||||
7.0.3
|
||||
7.1.2
|
||||
7.1.2
|
||||
7.8.2
|
||||
7.9.2
|
||||
@@ -1,5 +1,72 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 7.9.2 Nov 22, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
- Fix TS compiled definitions for `base*` types
|
||||
- Provide JS/Wasm interfaces for `hmacShaAsU8a`
|
||||
|
||||
|
||||
## 7.9.1 Nov 21, 2021
|
||||
|
||||
Contributed:
|
||||
|
||||
- Updated Polymesh genesisHash (Thanks to https://github.com/adamdossa)
|
||||
|
||||
Changes:
|
||||
|
||||
- Add strongly typed `object{Entries, Keys, Values}` utilities
|
||||
- Add `{hex, u8a}ToBigInt` utilities
|
||||
- Add `bi*` utilities for `bigint` operations (mirror of `bn*`)
|
||||
- Add `stringPascalCase` utility
|
||||
- Add `isCodec` to check for Codec-like structures
|
||||
- Add `has*` detection shortcuts (e.g. `hasWasm` or `hasBuffer`)
|
||||
- Add `sha256AsU8a` util (& replace use internally)
|
||||
- Optimize for `hexToU8a` and `u8aToHex` functions
|
||||
- Optimize `u8aEq` to use `DataView` for compares
|
||||
- Align `base{32, 58, 64}*` around (audited) `micro-base`
|
||||
- Space optimization for bip39 wordlists
|
||||
- Sync with upstream Substrate ss58 registry
|
||||
- Expand tests for uncovered functions
|
||||
|
||||
|
||||
## 7.8.2 Nov 7, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
- Expose internal `isArray` check
|
||||
- Move `object{Property, Properties}` from api repo
|
||||
|
||||
|
||||
## 7.8.1 Nov 6, 2021
|
||||
|
||||
Contributed:
|
||||
|
||||
- Added Acala genesisHash (Thanks to https://github.com/qwer951123)
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `objectClear` and `objectSpread` utility functions
|
||||
- Add `lazyMethod` & `lazyMethods` utility functions
|
||||
- Sync with upstream Substrate ss58 registry
|
||||
|
||||
|
||||
## 7.7.1 Oct 31, 2021
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Genshiro support (Thanks to https://github.com/Overseven)
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `arrayZip` to combine a key + value array into entries-like
|
||||
- Assert `chainCode` inputs to `derive*`, protecting against misuse
|
||||
- Adjust testnet detection in `@polkadot/networks`
|
||||
- Adjust base library usage of `for` vs `reduce/map`
|
||||
- Bump Ledger libraries to latest versions
|
||||
|
||||
|
||||
## 7.6.1 Oct 23, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
+18
-13
@@ -1,34 +1,39 @@
|
||||
{
|
||||
"repository": "https://github.com/polkadot-js/common",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"homepage": "https://github.com/polkadot-js/common#readme",
|
||||
"license": "Apache-2",
|
||||
"packageManager": "yarn@3.0.1",
|
||||
"private": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"version": "7.9.2",
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
],
|
||||
"resolutions": {
|
||||
"typescript": "^4.4.4"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-ts",
|
||||
"build:release": "polkadot-ci-ghact-build",
|
||||
"build:rollup": "polkadot-exec-rollup --config",
|
||||
"lint": "polkadot-dev-run-lint",
|
||||
"clean": "polkadot-dev-clean-build",
|
||||
"lint": "polkadot-dev-run-lint",
|
||||
"networks:crosscheck": "polkadot-dev-run-test packages/networks/src/crosscheck",
|
||||
"networks:sync": "node packages/networks/scripts/fromSubstrate.mjs",
|
||||
"networks:sync": "yarn build && node packages/networks/scripts/fromSubstrate.mjs",
|
||||
"postinstall": "polkadot-dev-yarn-only",
|
||||
"test": "polkadot-dev-run-test --coverage --runInBand --testPathIgnorePatterns crosscheck --testPathIgnorePatterns toMiniSecretCmp",
|
||||
"test:mnemonicCmp": "polkadot-dev-run-test packages/util-crypto/src/mnemonic/toMiniSecretCmp.spec.ts",
|
||||
"test:one": "polkadot-dev-run-test --runInBand"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.15.8",
|
||||
"@polkadot/dev": "^0.63.6",
|
||||
"@polkadot/ts": "^0.4.10",
|
||||
"@types/ed2curve": "^0.2.2",
|
||||
"@types/jest": "^27.0.2"
|
||||
"@babel/core": "^7.16.0",
|
||||
"@polkadot/dev": "^0.63.29",
|
||||
"@polkadot/ts": "^0.4.14",
|
||||
"@types/jest": "^27.0.3"
|
||||
},
|
||||
"version": "7.6.1",
|
||||
"packageManager": "yarn@3.0.1"
|
||||
"resolutions": {
|
||||
"typescript": "^4.5.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,32 @@
|
||||
{
|
||||
"name": "@polkadot/hw-ledger-transports",
|
||||
"version": "7.6.1",
|
||||
"type": "module",
|
||||
"repository": "github:polkadot-js/common",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"maintainers": [],
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"contributors": [],
|
||||
"license": "Apache-2.0",
|
||||
"main": "node.js",
|
||||
"browser": "browser.js",
|
||||
"react-native": "react-native.js",
|
||||
"sideEffects": false,
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/hw-ledger-transports#readme",
|
||||
"license": "Apache-2.0",
|
||||
"maintainers": [],
|
||||
"name": "@polkadot/hw-ledger-transports",
|
||||
"repository": {
|
||||
"directory": "packages/hw-ledger-transports",
|
||||
"type": "git",
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "7.9.2",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.15.4",
|
||||
"@ledgerhq/hw-transport": "^6.11.0",
|
||||
"@ledgerhq/hw-transport-webhid": "^6.11.0",
|
||||
"@ledgerhq/hw-transport-webusb": "^6.11.0"
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@ledgerhq/hw-transport": "^6.11.2",
|
||||
"@ledgerhq/hw-transport-webhid": "^6.11.2",
|
||||
"@ledgerhq/hw-transport-webusb": "^6.11.2"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@ledgerhq/hw-transport-node-hid-singleton": "^6.11.0"
|
||||
"@ledgerhq/hw-transport-node-hid-singleton": "^6.11.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,13 +10,6 @@ import LedgerWebUsb from '@ledgerhq/hw-transport-webusb';
|
||||
export { packageInfo } from './packageInfo';
|
||||
|
||||
export const transports: TransportDef[] = [
|
||||
// deprecated
|
||||
// import LedgerU2F from '@ledgerhq/hw-transport-u2f';
|
||||
// {
|
||||
// create: (): Promise<Transport> =>
|
||||
// LedgerU2F.create(),
|
||||
// type: 'u2f'
|
||||
// },
|
||||
{
|
||||
create: (): Promise<Transport> =>
|
||||
LedgerWebUsb.create(),
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger-transports', version: '7.6.1' };
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger-transports', version: '7.9.2' };
|
||||
|
||||
@@ -1,24 +1,30 @@
|
||||
{
|
||||
"name": "@polkadot/hw-ledger",
|
||||
"version": "7.6.1",
|
||||
"type": "module",
|
||||
"repository": "github:polkadot-js/common",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"maintainers": [],
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"contributors": [],
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/hw-ledger#readme",
|
||||
"license": "Apache-2.0",
|
||||
"main": "index.js",
|
||||
"maintainers": [],
|
||||
"name": "@polkadot/hw-ledger",
|
||||
"repository": {
|
||||
"directory": "packages/hw-ledger",
|
||||
"type": "git",
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": [
|
||||
"./detectPackage.js",
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"type": "module",
|
||||
"version": "7.9.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.15.4",
|
||||
"@polkadot/hw-ledger-transports": "7.6.1",
|
||||
"@polkadot/util": "7.6.1",
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@polkadot/hw-ledger-transports": "7.9.2",
|
||||
"@polkadot/util": "7.9.2",
|
||||
"@zondax/ledger-substrate": "^0.18.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type Transport from '@ledgerhq/hw-transport';
|
||||
import type { SubstrateApp } from '@zondax/ledger-substrate';
|
||||
|
||||
import { newCentrifugeApp, newDockApp, newEdgewareApp, newEquilibriumApp, newKusamaApp, newNodleApp, newPolkadotApp, newPolymeshApp, newStatemineApp } from '@zondax/ledger-substrate';
|
||||
import { newCentrifugeApp, newDockApp, newEdgewareApp, newEquilibriumApp, newGenshiroApp, newKusamaApp, newNodleApp, newPolkadotApp, newPolymeshApp, newStatemineApp } from '@zondax/ledger-substrate';
|
||||
|
||||
// These match up with the keys of the knownLedger object in the @polkadot/networks/defaults.ts
|
||||
export const ledgerApps: Record<string, (transport: Transport) => SubstrateApp> = {
|
||||
@@ -12,6 +12,7 @@ export const ledgerApps: Record<string, (transport: Transport) => SubstrateApp>
|
||||
'dock-mainnet': newDockApp,
|
||||
edgeware: newEdgewareApp,
|
||||
equilibrium: newEquilibriumApp,
|
||||
genshiro: newGenshiroApp,
|
||||
kusama: newKusamaApp,
|
||||
'nodle-chain': newNodleApp,
|
||||
polkadot: newPolkadotApp,
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger', version: '7.6.1' };
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger', version: '7.9.2' };
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/hw-ledger authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
export type LedgerTypes = 'hid' | 'u2f' | 'webusb';
|
||||
|
||||
export interface AccountOptions {
|
||||
@@ -15,7 +17,7 @@ export interface LedgerAddress {
|
||||
}
|
||||
|
||||
export interface LedgerSignature {
|
||||
signature: string;
|
||||
signature: HexString;
|
||||
}
|
||||
|
||||
export interface LedgerVersion {
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
{
|
||||
"name": "@polkadot/keyring",
|
||||
"version": "7.6.1",
|
||||
"type": "module",
|
||||
"description": "Keyring management",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"maintainers": [],
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"contributors": [],
|
||||
"description": "Keyring management",
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
||||
"license": "Apache-2.0",
|
||||
"main": "index.js",
|
||||
"maintainers": [],
|
||||
"name": "@polkadot/keyring",
|
||||
"repository": {
|
||||
"directory": "packages/keyring",
|
||||
"type": "git",
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": [
|
||||
"./detectPackage.js",
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"repository": "github:polkadot-js/common",
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
||||
"type": "module",
|
||||
"version": "7.9.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.15.4",
|
||||
"@polkadot/util": "7.6.1",
|
||||
"@polkadot/util-crypto": "7.6.1"
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@polkadot/util": "7.9.2",
|
||||
"@polkadot/util-crypto": "7.9.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "7.6.1",
|
||||
"@polkadot/util-crypto": "7.6.1"
|
||||
"@polkadot/util": "7.9.2",
|
||||
"@polkadot/util-crypto": "7.9.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,10 @@ const keypairFromSeed = {
|
||||
sr25519: (seed: Uint8Array): Keypair => schnorrkelFromSeed(seed)
|
||||
};
|
||||
|
||||
function pairToPublic ({ publicKey }: KeyringPair): Uint8Array {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* # @polkadot/keyring
|
||||
*
|
||||
@@ -262,11 +266,7 @@ export class Keyring implements KeyringInstance {
|
||||
* @description Returns an array list of all the public keys associated with each of the keyring pair values that are stored in the keyring pair dictionary.
|
||||
*/
|
||||
public getPublicKeys (): Uint8Array[] {
|
||||
return this.#pairs
|
||||
.all()
|
||||
.map(({ publicKey }): Uint8Array =>
|
||||
publicKey
|
||||
);
|
||||
return this.#pairs.all().map(pairToPublic);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/keyring', version: '7.6.1' };
|
||||
export const packageInfo = { name: '@polkadot/keyring', version: '7.9.2' };
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { EncryptedJsonEncoding, Keypair, KeypairType } from '@polkadot/util
|
||||
import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } from '../types';
|
||||
import type { PairInfo } from './types';
|
||||
|
||||
import { assert, u8aConcat, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, objectSpread, u8aConcat, u8aEmpty, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { blake2AsU8a, convertPublicKeyToCurve25519, convertSecretKeyToCurve25519, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclOpen, naclSeal, naclSign, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVrfSign, schnorrkelVrfVerify, secp256k1Compress, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify } from '@polkadot/util-crypto';
|
||||
|
||||
import { decodePair } from './decode';
|
||||
@@ -48,9 +48,8 @@ const TYPE_ADDRESS = {
|
||||
sr25519: (p: Uint8Array) => p
|
||||
};
|
||||
|
||||
// Not 100% correct, since it can be a Uint8Array, but an invalid one - just say "undefined" is anything non-valid
|
||||
function isLocked (secretKey?: Uint8Array): secretKey is undefined {
|
||||
return !secretKey || secretKey.length === 0 || secretKey.every((b) => b === 0);
|
||||
return !secretKey || u8aEmpty(secretKey);
|
||||
}
|
||||
|
||||
function vrfHash (proof: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array {
|
||||
@@ -182,7 +181,7 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P
|
||||
secretKey = new Uint8Array();
|
||||
},
|
||||
setMeta: (additional: KeyringPair$Meta): void => {
|
||||
meta = { ...meta, ...additional };
|
||||
meta = objectSpread({}, meta, additional);
|
||||
},
|
||||
sign: (message: HexString | string | Uint8Array, options: SignOptions = {}): Uint8Array => {
|
||||
assert(!isLocked(secretKey), 'Cannot sign with a locked key pair');
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import type { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import type { KeyringPair$Json, KeyringPair$Meta } from '../types';
|
||||
|
||||
import { objectSpread } from '@polkadot/util';
|
||||
import { jsonEncryptFormat } from '@polkadot/util-crypto';
|
||||
|
||||
interface PairStateJson {
|
||||
@@ -12,9 +13,8 @@ interface PairStateJson {
|
||||
}
|
||||
|
||||
export function pairToJson (type: KeypairType, { address, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
|
||||
return {
|
||||
...jsonEncryptFormat(encoded, ['pkcs8', type], isEncrypted),
|
||||
return objectSpread(jsonEncryptFormat(encoded, ['pkcs8', type], isEncrypted), {
|
||||
address,
|
||||
meta
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ export function createTestKeyring (options: KeyringOptions = {}, isDerived = tru
|
||||
const keyring = new Keyring(options);
|
||||
const pairs: PairDef[] = (options.type && options.type === 'ethereum') ? PAIRSETHEREUM : PAIRSSR25519;
|
||||
|
||||
pairs.forEach(({ name, publicKey, secretKey, seed, type }): void => {
|
||||
for (const { name, publicKey, secretKey, seed, type } of pairs) {
|
||||
if (!name && !seed) {
|
||||
throw new Error('Testing pair should have either a name or a seed');
|
||||
}
|
||||
@@ -139,7 +139,7 @@ export function createTestKeyring (options: KeyringOptions = {}, isDerived = tru
|
||||
pair.lock = (): void => {
|
||||
// we don't have lock/unlock functionality here
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return keyring;
|
||||
}
|
||||
|
||||
@@ -15,11 +15,9 @@ export function createTestPairs (options?: KeyringOptions, isDerived = true): Te
|
||||
const pairs = keyring.getPairs();
|
||||
const map: TestKeyringMap = { nobody: nobody() };
|
||||
|
||||
return pairs.reduce((result, pair): TestKeyringMap => {
|
||||
const { name } = pair.meta;
|
||||
for (const p of pairs) {
|
||||
map[p.meta.name as string] = p;
|
||||
}
|
||||
|
||||
result[name as string] = pair;
|
||||
|
||||
return result;
|
||||
}, map);
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,29 @@
|
||||
{
|
||||
"name": "@polkadot/networks",
|
||||
"version": "7.6.1",
|
||||
"type": "module",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"contributors": [],
|
||||
"description": "A list of all available Substrate networks and their applicable prefixes",
|
||||
"main": "index.js",
|
||||
"sideEffects": false,
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"repository": "github:polkadot-js/common",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"maintainers": [],
|
||||
"contributors": [],
|
||||
"license": "Apache-2.0",
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/networks#readme",
|
||||
"license": "Apache-2.0",
|
||||
"maintainers": [],
|
||||
"name": "@polkadot/networks",
|
||||
"repository": {
|
||||
"directory": "packages/networks",
|
||||
"type": "git",
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "7.9.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.15.4"
|
||||
"@babel/runtime": "^7.16.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "7.6.1",
|
||||
"@polkadot/x-fetch": "7.6.1"
|
||||
"@polkadot/util": "7.9.2",
|
||||
"@polkadot/x-fetch": "7.9.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// Copyright 2017-2021 @polkadot/networks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { KnownSubstrate, Ss58Registry } from './types';
|
||||
import type { Ss58Registry } from './types';
|
||||
|
||||
import fs from 'fs';
|
||||
|
||||
import { stringify } from '@polkadot/util';
|
||||
import { objectKeys, stringify } from '@polkadot/util';
|
||||
import { fetch } from '@polkadot/x-fetch';
|
||||
|
||||
import { substrateRegistry } from './defaults';
|
||||
@@ -52,7 +52,7 @@ describe('check latest Substrate ss58 registry', (): void => {
|
||||
});
|
||||
|
||||
it('has the same values as the original', (): void => {
|
||||
const fields = Object.keys(original.schema) as (keyof KnownSubstrate)[];
|
||||
const fields = objectKeys(original.schema);
|
||||
const errors = original.registry
|
||||
.map((n): [string, string[]] => {
|
||||
const other = allNetworks.find(({ prefix }) => prefix === n.prefix);
|
||||
|
||||
@@ -10,6 +10,9 @@ export const substrateRegistry = 'https://raw.githubusercontent.com/paritytech/s
|
||||
// last. This make lookups for the current a simple genesisHash[0]
|
||||
// (See Kusama as an example)
|
||||
export const knownGenesis: KnownGenesis = {
|
||||
acala: [
|
||||
'0xfc41b9bd8ef8fe53d58c7ea67c794c7ec9a73daf05e6d54b14ff6342c99ba64c'
|
||||
],
|
||||
bifrost: [
|
||||
'0x9f28c6a68e0fc9646eff64935684f6eeeece527e37bbe1f213d22caa1d9d6bed'
|
||||
],
|
||||
@@ -25,6 +28,9 @@ export const knownGenesis: KnownGenesis = {
|
||||
equilibrium: [
|
||||
'0x6f1a800de3daff7f5e037ddf66ab22ce03ab91874debeddb1086f5f7dbd48925'
|
||||
],
|
||||
genshiro: [
|
||||
'0x9b8cefc0eb5c568b527998bdd76c184e2b76ae561be76e4667072230217ea243'
|
||||
],
|
||||
hydradx: [
|
||||
'0xd2a620c27ec5cbc5621ff9a522689895074f7cca0d08e7134a7804e1a3ba86fc', // Snakenet Gen3-1
|
||||
'0x10af6e84234477d84dc572bac0789813b254aa490767ed06fb9591191d1073f9', // Snakenet Gen3
|
||||
@@ -52,7 +58,7 @@ export const knownGenesis: KnownGenesis = {
|
||||
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'
|
||||
],
|
||||
polymesh: [
|
||||
'0x9deeb940c92ae02111c3bd5baca89970384f4c9849f02a1b2e53e66414d30f9f'
|
||||
'0x6fbd74e5e1d0a61d52ccfe9d4adaed16dd3a7caa37c6bc4d0c2fa12e8b2f4063'
|
||||
],
|
||||
stafi: [
|
||||
'0x290a4149f09ea0e402c74c1c7e96ae4239588577fe78932f94f5404c68243d80'
|
||||
@@ -83,6 +89,7 @@ export const knownLedger: KnownLedger = {
|
||||
'dock-mainnet': 0x00000252,
|
||||
edgeware: 0x0000020b,
|
||||
equilibrium: 0x05f5e0fd,
|
||||
genshiro: 0x05f5e0fc,
|
||||
kusama: 0x000001b2,
|
||||
'nodle-chain': 0x000003eb,
|
||||
polkadot: 0x00000162,
|
||||
@@ -97,5 +104,6 @@ export const knownTestnet: KnownTestnet = {
|
||||
'dock-testnet': true,
|
||||
jupiter: true,
|
||||
'mathchain-testnet': true,
|
||||
subspace_testnet: true,
|
||||
'zero-alphaville': true
|
||||
};
|
||||
|
||||
@@ -1,43 +1,64 @@
|
||||
// Copyright 2017-2021 @polkadot/networks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Network, SubstrateNetwork } from './types';
|
||||
import type { KnownSubstrate, Network, SubstrateNetwork } from './types';
|
||||
|
||||
import { knownGenesis, knownIcon, knownLedger, knownTestnet } from './defaults';
|
||||
import { knownSubstrate } from './substrate';
|
||||
|
||||
// These are known prefixes that are not sorted
|
||||
const UNSORTED = [0, 2, 42];
|
||||
const TESTNETS = ['testnet'];
|
||||
|
||||
export const allNetworks = knownSubstrate.map((o): SubstrateNetwork => {
|
||||
function toExpanded (o: KnownSubstrate): SubstrateNetwork {
|
||||
const network = o.network || '';
|
||||
const nameParts = network.replace(/_/g, '-').split('-');
|
||||
const n = o as SubstrateNetwork;
|
||||
|
||||
// ledger additions
|
||||
n.slip44 = knownLedger[network];
|
||||
n.hasLedgerSupport = !!n.slip44;
|
||||
|
||||
// general items
|
||||
n.genesisHash = knownGenesis[network] || [];
|
||||
n.icon = knownIcon[network] || 'substrate';
|
||||
n.isIgnored = !!knownTestnet[network] || (!(o.standardAccount && o.decimals && o.symbols) && o.prefix !== 42);
|
||||
|
||||
// filtering
|
||||
n.isTestnet = !!knownTestnet[network] || TESTNETS.includes(nameParts[nameParts.length - 1]);
|
||||
n.isIgnored = n.isTestnet || (!(o.standardAccount && o.decimals && o.symbols) && o.prefix !== 42);
|
||||
|
||||
return n;
|
||||
});
|
||||
}
|
||||
|
||||
function filterSelectable ({ genesisHash, prefix }: Network): boolean {
|
||||
return !!genesisHash.length || prefix === 42;
|
||||
}
|
||||
|
||||
function filterAvailable (n: SubstrateNetwork): n is Network {
|
||||
return !n.isIgnored && !!n.network;
|
||||
}
|
||||
|
||||
function sortNetworks (a: Network, b: Network): number {
|
||||
const isUnSortedA = UNSORTED.includes(a.prefix);
|
||||
const isUnSortedB = UNSORTED.includes(b.prefix);
|
||||
|
||||
return isUnSortedA === isUnSortedB
|
||||
? 0
|
||||
: isUnSortedA
|
||||
? -1
|
||||
: isUnSortedB
|
||||
? 1
|
||||
: a.displayName.localeCompare(b.displayName);
|
||||
}
|
||||
|
||||
// This is all the Substrate networks with our additional information
|
||||
export const allNetworks = knownSubstrate.map(toExpanded);
|
||||
|
||||
// The list of available/claimed prefixes
|
||||
// - no testnets
|
||||
// - we only include those where we have a standardAccount
|
||||
// - sort by name, however we keep 0, 2, 42 first in the list
|
||||
export const availableNetworks = allNetworks
|
||||
.filter((n): n is Network => !n.isIgnored && !!n.network)
|
||||
.sort((a, b) =>
|
||||
UNSORTED.includes(a.prefix) === UNSORTED.includes(b.prefix)
|
||||
? 0
|
||||
: UNSORTED.includes(a.prefix)
|
||||
? -1
|
||||
: UNSORTED.includes(b.prefix)
|
||||
? 1
|
||||
: a.displayName.localeCompare(b.displayName)
|
||||
);
|
||||
export const availableNetworks = allNetworks.filter(filterAvailable).sort(sortNetworks);
|
||||
|
||||
// A filtered list of those chains we have details about (genesisHashes)
|
||||
export const selectableNetworks = availableNetworks.filter((n) => n.genesisHash.length || n.prefix === 42);
|
||||
export const selectableNetworks = availableNetworks.filter(filterSelectable);
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/networks', version: '7.6.1' };
|
||||
export const packageInfo = { name: '@polkadot/networks', version: '7.9.2' };
|
||||
|
||||
@@ -149,7 +149,7 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
network: 'totem',
|
||||
prefix: 14,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['XTX'],
|
||||
symbols: ['TOTEM'],
|
||||
website: 'https://totemaccounting.com'
|
||||
},
|
||||
{
|
||||
@@ -270,13 +270,13 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
website: null
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Dhiway CORD Network',
|
||||
decimals: [12, 12],
|
||||
displayName: 'CORD Network',
|
||||
network: 'cord',
|
||||
prefix: 29,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['DCU'],
|
||||
website: 'https://dhiway.com/'
|
||||
symbols: ['DHI', 'WAY'],
|
||||
website: 'https://cord.network/'
|
||||
},
|
||||
{
|
||||
decimals: [12],
|
||||
@@ -467,6 +467,15 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
symbols: ['LAYR'],
|
||||
website: 'https://composable.finance'
|
||||
},
|
||||
{
|
||||
decimals: [9],
|
||||
displayName: 'xx network',
|
||||
network: 'xxnetwork',
|
||||
prefix: 55,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['XX'],
|
||||
website: 'https://xx.network'
|
||||
},
|
||||
{
|
||||
decimals: [12],
|
||||
displayName: 'HydraDX',
|
||||
@@ -575,6 +584,15 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
symbols: ['TRAC'],
|
||||
website: 'https://origintrail.io'
|
||||
},
|
||||
{
|
||||
decimals: [10],
|
||||
displayName: 'Pontem Network',
|
||||
network: 'pontem-network',
|
||||
prefix: 105,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['PONT'],
|
||||
website: 'https://pontem.network'
|
||||
},
|
||||
{
|
||||
decimals: [12],
|
||||
displayName: 'Heiko',
|
||||
@@ -638,6 +656,15 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
symbols: ['QTZ'],
|
||||
website: 'https://unique.network'
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Pioneer Network by Bit.Country',
|
||||
network: 'pioneer_network',
|
||||
prefix: 268,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['NEER'],
|
||||
website: 'https://bit.country'
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Moonbeam',
|
||||
@@ -656,6 +683,33 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
symbols: ['MOVR'],
|
||||
website: 'https://moonbeam.network'
|
||||
},
|
||||
{
|
||||
decimals: [12],
|
||||
displayName: 'Kapex',
|
||||
network: 'kapex',
|
||||
prefix: 2007,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['KAPEX'],
|
||||
website: 'https://totemaccounting.com'
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Subspace testnet',
|
||||
network: 'subspace_testnet',
|
||||
prefix: 2254,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['tSSC'],
|
||||
website: 'https://subspace.network'
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Subspace',
|
||||
network: 'subspace',
|
||||
prefix: 6094,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['SSC'],
|
||||
website: 'https://subspace.network'
|
||||
},
|
||||
{
|
||||
decimals: [12],
|
||||
displayName: 'Basilisk',
|
||||
|
||||
@@ -26,6 +26,7 @@ export interface SubstrateNetwork extends KnownSubstrate {
|
||||
hasLedgerSupport: boolean;
|
||||
icon: Icon;
|
||||
isIgnored: boolean;
|
||||
isTestnet: boolean;
|
||||
slip44?: number | null;
|
||||
}
|
||||
|
||||
@@ -36,5 +37,5 @@ export interface Network extends SubstrateNetwork {
|
||||
export interface Ss58Registry {
|
||||
registry: KnownSubstrate[];
|
||||
specification: string;
|
||||
schema: Record<string, string>;
|
||||
schema: Record<keyof KnownSubstrate, string>;
|
||||
}
|
||||
|
||||
@@ -1,33 +1,35 @@
|
||||
{
|
||||
"name": "@polkadot/util-crypto",
|
||||
"version": "7.6.1",
|
||||
"type": "module",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"contributors": [],
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||
"license": "Apache-2.0",
|
||||
"maintainers": [],
|
||||
"name": "@polkadot/util-crypto",
|
||||
"repository": {
|
||||
"directory": "packages/util-crypto",
|
||||
"type": "git",
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": [
|
||||
"./cryptoInit.js",
|
||||
"./cryptoInit.cjs",
|
||||
"./detectPackage.js",
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"maintainers": [],
|
||||
"contributors": [],
|
||||
"license": "Apache-2.0",
|
||||
"repository": "github:polkadot-js/common",
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||
"type": "module",
|
||||
"version": "7.9.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.15.4",
|
||||
"@polkadot/networks": "7.6.1",
|
||||
"@polkadot/util": "7.6.1",
|
||||
"@polkadot/wasm-crypto": "^4.2.1",
|
||||
"@polkadot/x-randomvalues": "7.6.1",
|
||||
"base-x": "^3.0.9",
|
||||
"base64-js": "^1.5.1",
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@polkadot/networks": "7.9.2",
|
||||
"@polkadot/util": "7.9.2",
|
||||
"@polkadot/wasm-crypto": "^4.4.1",
|
||||
"@polkadot/x-randomvalues": "7.9.2",
|
||||
"blakejs": "^1.1.1",
|
||||
"bn.js": "^4.12.0",
|
||||
"create-hash": "^1.2.0",
|
||||
@@ -35,6 +37,7 @@
|
||||
"elliptic": "^6.5.4",
|
||||
"hash.js": "^1.1.7",
|
||||
"js-sha3": "^0.8.0",
|
||||
"micro-base": "^0.9.0",
|
||||
"scryptsy": "^2.1.0",
|
||||
"tweetnacl": "^1.0.3",
|
||||
"xxhashjs": "^0.2.2"
|
||||
@@ -42,11 +45,12 @@
|
||||
"devDependencies": {
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"@types/create-hash": "^1.2.2",
|
||||
"@types/elliptic": "^6.4.13",
|
||||
"@types/ed2curve": "^0.2.2",
|
||||
"@types/elliptic": "^6.4.14",
|
||||
"@types/scryptsy": "^2.0.0",
|
||||
"@types/xxhashjs": "^0.2.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "7.6.1"
|
||||
"@polkadot/util": "7.9.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Prefix } from './types';
|
||||
|
||||
import { base58Decode } from '../base58/decode';
|
||||
import { base58Decode } from '../base58';
|
||||
import { checkAddressChecksum } from './checksum';
|
||||
import { defaults } from './defaults';
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base58Decode } from '../base58/decode';
|
||||
import { base58Decode } from '../base58';
|
||||
import { checkAddressChecksum } from './checksum';
|
||||
|
||||
describe('checkAddressChecksum', (): void => {
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { Prefix } from './types';
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6
|
||||
import { assert, isHex, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { base58Decode } from '../base58/decode';
|
||||
import { base58Decode } from '../base58';
|
||||
import { checkAddressChecksum } from './checksum';
|
||||
import { defaults } from './defaults';
|
||||
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Network } from '@polkadot/networks/types';
|
||||
|
||||
import { availableNetworks } from '../networks';
|
||||
|
||||
function networkToPrefix ({ prefix }: Network): number {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
export const defaults = {
|
||||
allowedDecodedLengths: [1, 2, 4, 8, 32, 33],
|
||||
// publicKey has prefix + 2 checksum bytes, short only prefix + 1 checksum byte
|
||||
allowedEncodedLengths: [3, 4, 6, 10, 35, 36, 37, 38],
|
||||
allowedPrefix: availableNetworks.map(({ prefix }) => prefix),
|
||||
allowedPrefix: availableNetworks.map(networkToPrefix),
|
||||
prefix: 42
|
||||
};
|
||||
|
||||
@@ -2,16 +2,20 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { DeriveJunction } from '../key/DeriveJunction';
|
||||
import type { Prefix } from './types';
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { keyExtractPath } from '../key';
|
||||
import { DeriveJunction } from '../key/DeriveJunction';
|
||||
import { schnorrkelDerivePublic } from '../schnorrkel';
|
||||
import { decodeAddress } from './decode';
|
||||
import { encodeAddress } from './encode';
|
||||
|
||||
function filterHard ({ isHard }: DeriveJunction): boolean {
|
||||
return isHard;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name deriveAddress
|
||||
* @summary Creates a sr25519 derived address from the supplied and path.
|
||||
@@ -21,9 +25,13 @@ import { encodeAddress } from './encode';
|
||||
export function deriveAddress (who: HexString | Uint8Array | string, suri: string, ss58Format?: Prefix): string {
|
||||
const { path } = keyExtractPath(suri);
|
||||
|
||||
assert(path.length && !path.some((path) => path.isHard), 'Expected suri to contain a combination of non-hard paths');
|
||||
assert(path.length && !path.every(filterHard), 'Expected suri to contain a combination of non-hard paths');
|
||||
|
||||
return encodeAddress(path.reduce((publicKey: Uint8Array, path: DeriveJunction): Uint8Array => {
|
||||
return schnorrkelDerivePublic(publicKey, path.chainCode);
|
||||
}, decodeAddress(who)), ss58Format);
|
||||
let publicKey = decodeAddress(who);
|
||||
|
||||
for (const { chainCode } of path) {
|
||||
publicKey = schnorrkelDerivePublic(publicKey, chainCode);
|
||||
}
|
||||
|
||||
return encodeAddress(publicKey, ss58Format);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { Prefix } from './types';
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
|
||||
import { assert, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { base58Encode } from '../base58/encode';
|
||||
import { base58Encode } from '../base58';
|
||||
import { decodeAddress } from './decode';
|
||||
import { defaults } from './defaults';
|
||||
import { sshash } from './sshash';
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isAddress } from '.';
|
||||
|
||||
describe('isAddress', (): void => {
|
||||
it('decodes an address', (): void => {
|
||||
expect(
|
||||
isAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes the council address', (): void => {
|
||||
expect(
|
||||
isAddress('F3opxRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('converts a publicKey (hex) as-is', (): void => {
|
||||
expect(
|
||||
isAddress('0x01020304')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a short address', (): void => {
|
||||
expect(
|
||||
isAddress('F7NZ')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 1-byte accountId (with prefix)', (): void => {
|
||||
expect(
|
||||
isAddress('g4b', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte accountId', (): void => {
|
||||
expect(
|
||||
isAddress('3xygo', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('encodes a 4-byte address', (): void => {
|
||||
expect(
|
||||
isAddress('zswfoZa', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 8-byte address', (): void => {
|
||||
expect(
|
||||
isAddress('848Gh2GcGaZia', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 33-byte address', (): void => {
|
||||
expect(
|
||||
isAddress('KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (65)', (): void => {
|
||||
expect(
|
||||
isAddress('cLtA6nCDyvwKcEHH4QkZDSHMhS9s78BvUJUsKUbUAn1Jc2SCF')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (69)', (): void => {
|
||||
expect(
|
||||
isAddress('cnUaoo5wodnTVA4bnr4woSweto8hWZADUvLFXkR9Q6U7BRsbF')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (252)', (): void => {
|
||||
expect(
|
||||
isAddress('xw9Hca4RJTmBRgzJT4ieJBh7XCK9gE3NXBDSEmgGHd4TCrbnG')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (255)', (): void => {
|
||||
expect(
|
||||
isAddress('yGHU8YKprxHbHdEv7oUK4rzMZXtsdhcXVG2CAMyC9WhzhjH2k')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (ecdsa, from Substrate)', (): void => {
|
||||
expect(
|
||||
isAddress('4pbsSkWcBaYoFHrKJZp5fDVUKbqSYD9dhZZGvpp3vQ5ysVs5ybV')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('fails when length is invalid', (): void => {
|
||||
expect(
|
||||
isAddress('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('fails when the checksum does not match', (): void => {
|
||||
expect(
|
||||
isAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMa9cj')
|
||||
).toEqual(false);
|
||||
expect(
|
||||
isAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('fails when invalid base58 encoded address is found', (): void => {
|
||||
expect(
|
||||
isAddress('F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from '../blake2/asU8a';
|
||||
import { BN_LE_16_OPTS } from '../bn';
|
||||
import { decodeAddress } from './decode';
|
||||
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
@@ -16,7 +17,7 @@ export function createKeyDerived (who: HexString | Uint8Array | string, index: b
|
||||
u8aConcat(
|
||||
PREFIX,
|
||||
decodeAddress(who),
|
||||
bnToU8a(index, { bitLength: 16, isLe: true })
|
||||
bnToU8a(index, BN_LE_16_OPTS)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from '../blake2/asU8a';
|
||||
import { decodeAddress } from './decode';
|
||||
import { BN_LE_16_OPTS } from '../bn';
|
||||
import { addressToU8a } from './util';
|
||||
|
||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||
|
||||
@@ -16,8 +17,8 @@ export function createKeyMulti (who: (HexString | Uint8Array | string)[], thresh
|
||||
u8aConcat(
|
||||
PREFIX,
|
||||
compactToU8a(who.length),
|
||||
...u8aSorted(who.map((who) => decodeAddress(who))),
|
||||
bnToU8a(threshold, { bitLength: 16, isLe: true })
|
||||
...u8aSorted(who.map(addressToU8a)),
|
||||
bnToU8a(threshold, BN_LE_16_OPTS)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,11 +6,13 @@ import type { Prefix } from './types';
|
||||
|
||||
import { u8aSorted } from '@polkadot/util';
|
||||
|
||||
import { decodeAddress } from './decode';
|
||||
import { encodeAddress } from './encode';
|
||||
import { addressToU8a } from './util';
|
||||
|
||||
export function sortAddresses (addresses: (HexString | Uint8Array | string)[], ss58Format?: Prefix): string[] {
|
||||
const u8aToAddress = (u8a: Uint8Array) => encodeAddress(u8a, ss58Format);
|
||||
|
||||
return u8aSorted(
|
||||
addresses.map((who) => decodeAddress(who))
|
||||
).map((u8a) => encodeAddress(u8a, ss58Format));
|
||||
addresses.map(addressToU8a)
|
||||
).map(u8aToAddress);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { decodeAddress } from './decode';
|
||||
|
||||
export function addressToU8a (who: string | Uint8Array): Uint8Array {
|
||||
return decodeAddress(who);
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { validateAddress } from '.';
|
||||
|
||||
describe('validateAddress', (): void => {
|
||||
it('decodes an address', (): void => {
|
||||
expect(
|
||||
validateAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes the council address', (): void => {
|
||||
expect(
|
||||
validateAddress('F3opxRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('converts a publicKey (hex) as-is', (): void => {
|
||||
expect(
|
||||
validateAddress('0x01020304')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a short address', (): void => {
|
||||
expect(
|
||||
validateAddress('F7NZ')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 1-byte accountId (with prefix)', (): void => {
|
||||
expect(
|
||||
validateAddress('g4b', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte accountId', (): void => {
|
||||
expect(
|
||||
validateAddress('3xygo', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('encodes a 4-byte address', (): void => {
|
||||
expect(
|
||||
validateAddress('zswfoZa', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 8-byte address', (): void => {
|
||||
expect(
|
||||
validateAddress('848Gh2GcGaZia', false, 2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 33-byte address', (): void => {
|
||||
expect(
|
||||
validateAddress('KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (65)', (): void => {
|
||||
expect(
|
||||
validateAddress('cLtA6nCDyvwKcEHH4QkZDSHMhS9s78BvUJUsKUbUAn1Jc2SCF')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (69)', (): void => {
|
||||
expect(
|
||||
validateAddress('cnUaoo5wodnTVA4bnr4woSweto8hWZADUvLFXkR9Q6U7BRsbF')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (252)', (): void => {
|
||||
expect(
|
||||
validateAddress('xw9Hca4RJTmBRgzJT4ieJBh7XCK9gE3NXBDSEmgGHd4TCrbnG')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (255)', (): void => {
|
||||
expect(
|
||||
validateAddress('yGHU8YKprxHbHdEv7oUK4rzMZXtsdhcXVG2CAMyC9WhzhjH2k')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('decodes a 2-byte prefix (ecdsa, from Substrate)', (): void => {
|
||||
expect(
|
||||
validateAddress('4pbsSkWcBaYoFHrKJZp5fDVUKbqSYD9dhZZGvpp3vQ5ysVs5ybV')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('fails when length is invalid', (): void => {
|
||||
expect(
|
||||
() => validateAddress('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b')
|
||||
).toThrow(/address length/);
|
||||
});
|
||||
|
||||
it('fails when the checksum does not match', (): void => {
|
||||
expect(
|
||||
() => validateAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMa9cj')
|
||||
).toThrow(/address checksum/);
|
||||
expect(
|
||||
() => validateAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU')
|
||||
).toThrow(/address checksum/);
|
||||
});
|
||||
|
||||
it('fails when invalid base58 encoded address is found', (): void => {
|
||||
expect(
|
||||
() => validateAddress('F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29')
|
||||
).toThrow(/Decoding F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29: Invalid base58 character "I" \(0x49\) at index 4/);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,53 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// https://github.com/multiformats/js-multibase/blob/master/src/constants.js
|
||||
export const BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567';
|
||||
export const BITS_PER_CHAR = 5;
|
||||
import { utils } from 'micro-base';
|
||||
|
||||
import { createDecode, createEncode, createIs, createValidate } from './helpers';
|
||||
|
||||
const BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567';
|
||||
|
||||
const BASE32_CONFIG = {
|
||||
alphabet: BASE32_ALPHABET,
|
||||
coder: utils.chain(
|
||||
// We define our own chain, the default base32 has padding
|
||||
utils.radix2(5),
|
||||
utils.alphabet(BASE32_ALPHABET),
|
||||
{
|
||||
decode: (input: string) => input.split(''),
|
||||
encode: (input: string[]) => input.join('')
|
||||
}
|
||||
),
|
||||
ipfsChar: 'b',
|
||||
type: 'base32'
|
||||
};
|
||||
|
||||
/**
|
||||
* @name base32Validate
|
||||
* @summary Validates a base32 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base32, throwing exceptions if not
|
||||
*/
|
||||
export const base32Validate = createValidate(BASE32_CONFIG);
|
||||
|
||||
/**
|
||||
* @name isBase32
|
||||
* @description Checks if the input is in base32, returning true/false
|
||||
*/
|
||||
export const isBase32 = createIs(base32Validate);
|
||||
|
||||
/**
|
||||
* @name base32Decode
|
||||
* @summary Delookup a base32 value.
|
||||
* @description
|
||||
* From the provided input, decode the base32 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export const base32Decode = createDecode(BASE32_CONFIG, base32Validate);
|
||||
|
||||
/**
|
||||
* @name base32Encode
|
||||
* @summary Creates a base32 value.
|
||||
* @description
|
||||
* From the provided input, create the base32 and return the result as a string.
|
||||
*/
|
||||
export const base32Encode = createEncode(BASE32_CONFIG);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { u8aToString } from '@polkadot/util';
|
||||
|
||||
import { base32Decode } from './';
|
||||
import { base32Decode } from '.';
|
||||
|
||||
describe('base32Decode', (): void => {
|
||||
it('decodes a base32', (): void => {
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// adapted from https://github.com/multiformats/js-multibase/blob/424709195b46ffb1d6f2f69a7707598ebe751e5e/src/rfc4648.js
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { BASE32_ALPHABET, BITS_PER_CHAR } from './bs32';
|
||||
import { base32Validate } from './validate';
|
||||
|
||||
// Build the character lookup table:
|
||||
const LOOKUP = BASE32_ALPHABET
|
||||
.split('')
|
||||
.reduce((lookup: Record<string, number>, char: string, index: number): Record<string, number> => {
|
||||
lookup[char] = index;
|
||||
|
||||
return lookup;
|
||||
}, {});
|
||||
|
||||
/** @internal */
|
||||
function decode (output: Uint8Array, input: string, offset: number): [Uint8Array, number, number] {
|
||||
let bits = 0;
|
||||
let buffer = 0;
|
||||
let written = 0;
|
||||
|
||||
for (let i = offset; i < input.length; i++) {
|
||||
buffer = (buffer << BITS_PER_CHAR) | LOOKUP[input[i]];
|
||||
bits += BITS_PER_CHAR;
|
||||
|
||||
if (bits >= 8) {
|
||||
bits -= 8;
|
||||
output[written++] = 0xff & (buffer >> bits);
|
||||
}
|
||||
}
|
||||
|
||||
return [output, bits, buffer];
|
||||
}
|
||||
|
||||
/**
|
||||
* @name base32Decode
|
||||
* @summary Delookup a base32 value.
|
||||
* @description
|
||||
* From the provided input, decode the base32 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export function base32Decode (value: string, ipfsCompat = false): Uint8Array {
|
||||
base32Validate(value, ipfsCompat);
|
||||
|
||||
const offset = (ipfsCompat ? 1 : 0);
|
||||
const [output, bits, buffer] = decode(
|
||||
new Uint8Array(((value.length - offset) * BITS_PER_CHAR / 8) | 0),
|
||||
value,
|
||||
offset
|
||||
);
|
||||
|
||||
assert(!(bits >= BITS_PER_CHAR || 0xff & (buffer << (8 - bits))), 'Unexpected end of data');
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base58Decode } from '../base58';
|
||||
import { base32Encode } from './';
|
||||
import { base32Encode } from '.';
|
||||
|
||||
describe('base32Encode', (): void => {
|
||||
it('encodes to a base32', (): void => {
|
||||
@@ -11,6 +11,12 @@ describe('base32Encode', (): void => {
|
||||
).toEqual('irswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb');
|
||||
});
|
||||
|
||||
it('encodes to a base32 (ipfs-compat)', (): void => {
|
||||
expect(
|
||||
base32Encode('Decentralize everything!!', true)
|
||||
).toEqual('birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb');
|
||||
});
|
||||
|
||||
it('encodes a base58 to a base32', (): void => {
|
||||
expect(
|
||||
base32Encode(
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// adapted from https://github.com/multiformats/js-multibase/blob/424709195b46ffb1d6f2f69a7707598ebe751e5e/src/rfc4648.js
|
||||
|
||||
import type { U8aLike } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { BASE32_ALPHABET, BITS_PER_CHAR } from './bs32';
|
||||
|
||||
const MASK = (1 << BITS_PER_CHAR) - 1;
|
||||
|
||||
/**
|
||||
* @name base32Encode
|
||||
* @summary Creates a base32 value.
|
||||
* @description
|
||||
* From the provided input, create the base32 and return the result as a string.
|
||||
*/
|
||||
export function base32Encode (value: U8aLike, ipfsCompat = false): string {
|
||||
const u8a = u8aToU8a(value);
|
||||
let out = '';
|
||||
let bits = 0;
|
||||
let buffer = 0;
|
||||
|
||||
for (let i = 0; i < u8a.length; ++i) {
|
||||
buffer = (buffer << 8) | u8a[i];
|
||||
bits += 8;
|
||||
|
||||
while (bits > BITS_PER_CHAR) {
|
||||
bits -= BITS_PER_CHAR;
|
||||
out += BASE32_ALPHABET[MASK & (buffer >> bits)];
|
||||
}
|
||||
}
|
||||
|
||||
if (bits) {
|
||||
out += BASE32_ALPHABET[MASK & (buffer << (BITS_PER_CHAR - bits))];
|
||||
}
|
||||
|
||||
return ipfsCompat
|
||||
? `b${out}`
|
||||
: out;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
// NOTE: Work around a TS issue. These are re-defined like in @polkadot/util/types
|
||||
// since without it the import
|
||||
//
|
||||
// import type { U8aLike } from '@polkadot/util/types';
|
||||
//
|
||||
// ends up in the build-generated code as
|
||||
//
|
||||
// import("../../../util/src/types").U8aLike
|
||||
//
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
type U8aLike = number[] | Buffer | Uint8Array | string | String;
|
||||
|
||||
interface Coder {
|
||||
decode: (value: string) => Uint8Array;
|
||||
encode: (value: Uint8Array) => string;
|
||||
}
|
||||
|
||||
interface Config {
|
||||
alphabet: string;
|
||||
coder: Coder;
|
||||
ipfsChar?: string;
|
||||
regex?: RegExp;
|
||||
type: string;
|
||||
}
|
||||
|
||||
type DecodeFn = (value: string, ipfsCompat?: boolean) => Uint8Array;
|
||||
|
||||
type EncodeFn = (value: U8aLike, ipfsCompat?: boolean) => string;
|
||||
|
||||
type ValidateFn = (value?: unknown, ipfsCompat?: boolean) => value is string;
|
||||
|
||||
export function createDecode ({ coder, ipfsChar }: Config, validate: ValidateFn): DecodeFn {
|
||||
return (value: string, ipfsCompat?: boolean): Uint8Array => {
|
||||
validate(value, ipfsCompat);
|
||||
|
||||
return coder.decode(
|
||||
ipfsChar && ipfsCompat
|
||||
? value.substr(1)
|
||||
: value
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export function createEncode ({ coder, ipfsChar }: Config): EncodeFn {
|
||||
return (value: U8aLike, ipfsCompat?: boolean): string => {
|
||||
const out = coder.encode(u8aToU8a(value));
|
||||
|
||||
return ipfsChar && ipfsCompat
|
||||
? `${ipfsChar}${out}`
|
||||
: out;
|
||||
};
|
||||
}
|
||||
|
||||
export function createIs (validate: ValidateFn): ValidateFn {
|
||||
return (value?: unknown, ipfsCompat?: boolean): value is string => {
|
||||
try {
|
||||
return validate(value, ipfsCompat);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function createValidate ({ alphabet, ipfsChar, regex, type }: Config): ValidateFn {
|
||||
return (value?: unknown, ipfsCompat?: boolean): value is string => {
|
||||
assert(value && typeof value === 'string', () => `Expected non-null, non-empty ${type} string input`);
|
||||
|
||||
if (ipfsChar && ipfsCompat) {
|
||||
assert(value[0] === ipfsChar, () => `Expected ipfs-compatible ${type} to start with '${ipfsChar}'`);
|
||||
}
|
||||
|
||||
if (regex) {
|
||||
assert(regex.test(value), `Invalid ${type} encoding`);
|
||||
} else {
|
||||
for (let i = (ipfsCompat ? 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;
|
||||
};
|
||||
}
|
||||
@@ -5,7 +5,4 @@
|
||||
* @summary Encode and decode base32 values
|
||||
*/
|
||||
|
||||
export { base32Decode } from './decode';
|
||||
export { base32Encode } from './encode';
|
||||
export { base32Validate } from './validate';
|
||||
export { isBase32 } from './is';
|
||||
export { base32Decode, base32Encode, base32Validate, isBase32 } from './bs32';
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isBase32 } from '.';
|
||||
|
||||
describe('isBase32', (): void => {
|
||||
it('validates encoded', (): void => {
|
||||
expect(
|
||||
isBase32('afkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', false)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('validates ipfs-compat encoded', (): void => {
|
||||
expect(
|
||||
isBase32('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', false)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('fails on invalid', (): void => {
|
||||
expect(
|
||||
isBase32('not in base32')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('fails on non-ipfs', (): void => {
|
||||
expect(
|
||||
isBase32('afkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -1,16 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base32Validate } from './validate';
|
||||
|
||||
export function testValidator (validate: (value?: string | null, ipfsCompat?: boolean) => boolean, value?: string | null, ipfsCompat?: boolean): boolean {
|
||||
try {
|
||||
return validate(value, ipfsCompat);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isBase32 (value?: string | null, ipfsCompat?: boolean): value is string {
|
||||
return testValidator(base32Validate, value, ipfsCompat);
|
||||
}
|
||||
@@ -4,9 +4,39 @@
|
||||
import { base32Validate } from '.';
|
||||
|
||||
describe('base32Validate', (): void => {
|
||||
it('validates encoded', (): void => {
|
||||
expect(
|
||||
base32Validate('afkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', false)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('validates ipfs-compat encoded', (): void => {
|
||||
expect(
|
||||
() => base32Validate('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
|
||||
).not.toThrow();
|
||||
base32Validate('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('fails on empty', (): void => {
|
||||
expect(
|
||||
() => base32Validate('')
|
||||
).toThrow(/Expected/);
|
||||
});
|
||||
|
||||
it('fails on non-string', (): void => {
|
||||
expect(
|
||||
() => base32Validate(true)
|
||||
).toThrow(/Expected/);
|
||||
});
|
||||
|
||||
it('fails on invalid', (): void => {
|
||||
expect(
|
||||
() => base32Validate('not in base32')
|
||||
).toThrow(/Invalid/);
|
||||
});
|
||||
|
||||
it('fails on non-ipfs', (): void => {
|
||||
expect(
|
||||
() => base32Validate('afkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
|
||||
).toThrow(/Expected/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { createValidateFn } from '../base58/validate';
|
||||
import { BASE32_ALPHABET } from './bs32';
|
||||
|
||||
/**
|
||||
* @name base32Validate
|
||||
* @summary Validates a base32 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base32
|
||||
*/
|
||||
export const base32Validate = createValidateFn({ alphabet: BASE32_ALPHABET, ipfsChar: 'b', type: 'base32' });
|
||||
@@ -1,9 +1,43 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import baseX from 'base-x';
|
||||
import { base58 } from 'micro-base';
|
||||
|
||||
// https://github.com/cryptocoinjs/base-x#alphabets
|
||||
export const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
||||
import { createDecode, createEncode, createIs, createValidate } from '../base32/helpers';
|
||||
|
||||
export const bs58 = baseX(BASE58_ALPHABET);
|
||||
const BASE58_CONFIG = {
|
||||
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
||||
coder: base58,
|
||||
ipfsChar: 'z',
|
||||
type: 'base58'
|
||||
};
|
||||
|
||||
/**
|
||||
* @name base58Validate
|
||||
* @summary Validates a base58 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base58, throwing exceptions if not
|
||||
*/
|
||||
export const base58Validate = createValidate(BASE58_CONFIG);
|
||||
|
||||
/**
|
||||
* @name base58Decode
|
||||
* @summary Decodes a base58 value.
|
||||
* @description
|
||||
* From the provided input, decode the base58 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export const base58Decode = createDecode(BASE58_CONFIG, base58Validate);
|
||||
|
||||
/**
|
||||
* @name base58Encode
|
||||
* @summary Creates a base58 value.
|
||||
* @description
|
||||
* From the provided input, create the base58 and return the result as a string.
|
||||
*/
|
||||
export const base58Encode = createEncode(BASE58_CONFIG);
|
||||
|
||||
/**
|
||||
* @name isBase58
|
||||
* @description Checks if the input is in base58, returning true/false
|
||||
*/
|
||||
export const isBase58 = createIs(base58Validate);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base32Decode } from '../base32';
|
||||
import { base58Decode } from './';
|
||||
|
||||
describe('base58Encode', (): void => {
|
||||
it('encodes a base58 to a base32', (): void => {
|
||||
expect(
|
||||
base58Decode('b2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV')
|
||||
).toEqual(
|
||||
base32Decode('afkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
|
||||
);
|
||||
});
|
||||
|
||||
it('encodes a base58 to a base32 (ipfs-compat)', (): void => {
|
||||
expect(
|
||||
base58Decode('zb2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV', true)
|
||||
).toEqual(
|
||||
base32Decode('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,19 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
|
||||
import { bs58 } from './bs58';
|
||||
import { base58Validate } from './validate';
|
||||
|
||||
/**
|
||||
* @name base58Decode
|
||||
* @summary Decodes a base58 value.
|
||||
* @description
|
||||
* From the provided input, decode the base58 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export function base58Decode (value: string, ipfsCompat?: boolean): Uint8Array {
|
||||
base58Validate(value, ipfsCompat);
|
||||
|
||||
return bufferToU8a(bs58.decode(value.substr(ipfsCompat ? 1 : 0)));
|
||||
}
|
||||
@@ -5,7 +5,15 @@ import { base32Decode } from '../base32';
|
||||
import { base58Encode } from './';
|
||||
|
||||
describe('base58Encode', (): void => {
|
||||
it('encodes a base32 to a base38', (): void => {
|
||||
it('encodes a base32 to a base58', (): void => {
|
||||
expect(
|
||||
base58Encode(
|
||||
base32Decode('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
|
||||
)
|
||||
).toEqual('b2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV');
|
||||
});
|
||||
|
||||
it('encodes a base32 to a base58 (ipfs-compat)', (): void => {
|
||||
expect(
|
||||
base58Encode(
|
||||
base32Decode('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true),
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { U8aLike } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { bs58 } from './bs58';
|
||||
|
||||
/**
|
||||
* @name base58Encode
|
||||
* @summary Creates a base58 value.
|
||||
* @description
|
||||
* From the provided input, create the base58 and return the result as a string.
|
||||
*/
|
||||
export function base58Encode (value: U8aLike, ipfsCompat?: boolean): string {
|
||||
const out = bs58.encode(u8aToU8a(value));
|
||||
|
||||
return ipfsCompat
|
||||
? `z${out}`
|
||||
: out;
|
||||
}
|
||||
@@ -5,7 +5,4 @@
|
||||
* @summary Encode and decode base58 values
|
||||
*/
|
||||
|
||||
export { base58Decode } from './decode';
|
||||
export { base58Encode } from './encode';
|
||||
export { base58Validate } from './validate';
|
||||
export { isBase58 } from './is';
|
||||
export { base58Decode, base58Encode, base58Validate, isBase58 } from './bs58';
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { testValidator } from '../base32/is';
|
||||
import { base58Validate } from './validate';
|
||||
|
||||
export function isBase58 (value?: string | null, ipfsCompat?: boolean): value is string {
|
||||
return testValidator(base58Validate, value, ipfsCompat);
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { BASE58_ALPHABET } from './bs58';
|
||||
|
||||
interface CheckConfig {
|
||||
alphabet: string;
|
||||
ipfsChar: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export function createValidateFn ({ alphabet, ipfsChar, type }: CheckConfig): (value?: unknown, ipfsCompat?: boolean) => value is string {
|
||||
return (value?: unknown, ipfsCompat?: boolean): value is string => {
|
||||
assert(value && typeof value === 'string', () => `Expected non-null, non-empty ${type} string input`);
|
||||
assert(!ipfsCompat || value[0] === ipfsChar, () => `Expected ${type} to start with '${ipfsChar}'`);
|
||||
|
||||
for (let i = (ipfsCompat ? 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;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @name base58Validate
|
||||
* @summary Validates a base58 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base58
|
||||
*/
|
||||
export const base58Validate = createValidateFn({ alphabet: BASE58_ALPHABET, ipfsChar: 'z', type: 'base58' });
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base64 } from 'micro-base';
|
||||
|
||||
import { createDecode, createEncode, createIs, createValidate } from '../base32/helpers';
|
||||
|
||||
const BASE64_CONFIG = {
|
||||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
||||
coder: base64,
|
||||
regex: /^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,
|
||||
type: 'base64'
|
||||
};
|
||||
|
||||
/**
|
||||
* @name base64Validate
|
||||
* @summary Validates a base64 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base64
|
||||
*/
|
||||
export const base64Validate = createValidate(BASE64_CONFIG);
|
||||
|
||||
/**
|
||||
* @name isBase64
|
||||
* @description Checks if the input is in base64, returning true/false
|
||||
*/
|
||||
export const isBase64 = createIs(base64Validate);
|
||||
|
||||
/**
|
||||
* @name base64Decode
|
||||
* @summary Decodes a base64 value.
|
||||
* @description
|
||||
* From the provided input, decode the base64 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export const base64Decode = createDecode(BASE64_CONFIG, base64Validate);
|
||||
|
||||
/**
|
||||
* @name base64Encode
|
||||
* @summary Creates a base64 value.
|
||||
* @description
|
||||
* From the provided input, create the base64 and return the result as a string.
|
||||
*/
|
||||
export const base64Encode = createEncode(BASE64_CONFIG);
|
||||
@@ -1,18 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import base64 from 'base64-js';
|
||||
|
||||
import { base64Validate } from './validate';
|
||||
|
||||
/**
|
||||
* @name base64Decode
|
||||
* @summary Decodes a base64 value.
|
||||
* @description
|
||||
* From the provided input, decode the base64 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export function base64Decode (value: string): Uint8Array {
|
||||
base64Validate(value);
|
||||
|
||||
return base64.toByteArray(value);
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { U8aLike } from '@polkadot/util/types';
|
||||
|
||||
import base64 from 'base64-js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
|
||||
/**
|
||||
* @name base64Encode
|
||||
* @summary Creates a base64 value.
|
||||
* @description
|
||||
* From the provided input, create the base64 and return the result as a string.
|
||||
*/
|
||||
export function base64Encode (value: U8aLike): string {
|
||||
return base64.fromByteArray(u8aToU8a(value));
|
||||
}
|
||||
@@ -5,9 +5,6 @@
|
||||
* @summary Encode and decode base64 values
|
||||
*/
|
||||
|
||||
export { base64Decode } from './decode';
|
||||
export { base64Encode } from './encode';
|
||||
export { base64Decode, base64Encode, base64Validate, isBase64 } from './bs64';
|
||||
export { base64Pad } from './pad';
|
||||
export { base64Trim } from './trim';
|
||||
export { base64Validate } from './validate';
|
||||
export { isBase64 } from './is';
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { base64Validate } from './validate';
|
||||
|
||||
export function isBase64 (value?: string | null): value is string {
|
||||
try {
|
||||
return base64Validate(value);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name base64Pad
|
||||
* @description Adds padding characters for correct length
|
||||
*/
|
||||
export function base64Pad (value: string): string {
|
||||
return value.padEnd(value.length + (value.length % 4), '=');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name base64Trim
|
||||
* @description Trims padding characters
|
||||
*/
|
||||
export function base64Trim (value: string): string {
|
||||
while (value.length && value[value.length - 1] === '=') {
|
||||
value = value.slice(0, -1);
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
/**
|
||||
* @name base64Validate
|
||||
* @summary Validates a base64 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base64
|
||||
*/
|
||||
export function base64Validate (value?: string | null): value is string {
|
||||
assert(value && typeof value === 'string', 'Expected non-null, non-empty base64 string 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,28 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from './asU8a';
|
||||
|
||||
/**
|
||||
* @name blake2AsHex
|
||||
* @summary Creates a blake2b hex from the input.
|
||||
* @description
|
||||
* From a `Uint8Array` input, create the blake2b and return the result as a hex string with the specified `bitLength`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { blake2AsHex } from '@polkadot/util-crypto';
|
||||
*
|
||||
* blake2AsHex('abc'); // => 0xba80a53f981c4d0d
|
||||
* ```
|
||||
*/
|
||||
export function blake2AsHex (data: HexString | Uint8Array | string, bitLength = 256, key?: Uint8Array | null, onlyJs = false): HexString {
|
||||
return u8aToHex(
|
||||
blake2AsU8a(data, bitLength, key, onlyJs)
|
||||
);
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceTest } from '../../test/performance';
|
||||
import { blake2AsU8a } from '.';
|
||||
|
||||
describe('blake2AsU8a', (): void => {
|
||||
@@ -58,4 +59,10 @@ describe('blake2AsU8a', (): void => {
|
||||
|
||||
expect(a).toEqual(b);
|
||||
});
|
||||
|
||||
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
|
||||
performanceTest(`blake2AsU8a, bitLength=${bitLength}`, 64000, (input, onlyJs) =>
|
||||
blake2AsU8a(input, bitLength, null, onlyJs)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,9 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import js from 'blakejs';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { blake2b, isReady } from '@polkadot/wasm-crypto';
|
||||
import { blake2b } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { createAsHex, isWasmOnly } from '../helpers';
|
||||
|
||||
/**
|
||||
* @name blake2AsU8a
|
||||
@@ -22,10 +24,17 @@ import { blake2b, isReady } from '@polkadot/wasm-crypto';
|
||||
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
|
||||
* ```
|
||||
*/
|
||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength = 256, key?: Uint8Array | null, onlyJs = false): Uint8Array {
|
||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
const u8a = u8aToU8a(data);
|
||||
|
||||
return isReady() && !onlyJs
|
||||
? blake2b(u8aToU8a(data), u8aToU8a(key), byteLength)
|
||||
: js.blake2b(u8aToU8a(data), key || undefined, byteLength);
|
||||
return isWasmOnly(onlyJs)
|
||||
? blake2b(u8a, u8aToU8a(key), byteLength)
|
||||
: js.blake2b(u8a, key || undefined, byteLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name blake2AsHex
|
||||
* @description Creates a blake2b hex from the input.
|
||||
*/
|
||||
export const blake2AsHex = createAsHex(blake2AsU8a);
|
||||
|
||||
@@ -5,5 +5,4 @@
|
||||
* @summary Create blake2b values with specified bitlengths
|
||||
*/
|
||||
|
||||
export { blake2AsHex } from './asHex';
|
||||
export { blake2AsU8a } from './asU8a';
|
||||
export { blake2AsHex, blake2AsU8a } from './asU8a';
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const BN_LE_OPTS = { isLe: true };
|
||||
|
||||
export const BN_LE_16_OPTS = { bitLength: 16, isLe: true };
|
||||
|
||||
export const BN_BE_32_OPTS = { bitLength: 32, isLe: false };
|
||||
export const BN_LE_32_OPTS = { bitLength: 32, isLe: true };
|
||||
|
||||
export const BN_BE_256_OPTS = { bitLength: 256, isLe: false };
|
||||
export const BN_LE_256_OPTS = { bitLength: 256, isLe: true };
|
||||
|
||||
export const BN_LE_512_OPTS = { bitLength: 512, isLe: true };
|
||||
@@ -26,6 +26,6 @@ export * from './random';
|
||||
export * from './schnorrkel';
|
||||
export * from './scrypt';
|
||||
export * from './secp256k1';
|
||||
export * from './sha512';
|
||||
export * from './sha';
|
||||
export * from './signature';
|
||||
export * from './xxhash';
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
|
||||
import { isReady, waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
export function cryptoIsReady (): boolean {
|
||||
return isReady();
|
||||
}
|
||||
export const cryptoIsReady = isReady;
|
||||
|
||||
export function cryptoWaitReady (): Promise<boolean> {
|
||||
return waitReady()
|
||||
|
||||
@@ -29,8 +29,8 @@ export function ethereumEncode (addressOrPublic?: HexString | string | Uint8Arra
|
||||
const hash = u8aToHex(keccakAsU8a(address), -1, false);
|
||||
let result = '';
|
||||
|
||||
for (let index = 0; index < 40; index++) {
|
||||
result = `${result}${parseInt(hash[index], 16) > 7 ? address[index].toUpperCase() : address[index]}`;
|
||||
for (let i = 0; i < 40; i++) {
|
||||
result = `${result}${parseInt(hash[i], 16) > 7 ? address[i].toUpperCase() : address[i]}`;
|
||||
}
|
||||
|
||||
return `0x${result}`;
|
||||
|
||||
@@ -17,8 +17,8 @@ export function isEthereumChecksum (_address: string): boolean {
|
||||
const address = _address.replace('0x', '');
|
||||
const hash = u8aToHex(keccakAsU8a(address.toLowerCase()), -1, false);
|
||||
|
||||
for (let index = 0; index < 40; index++) {
|
||||
if (isInvalidChar(address[index], parseInt(hash[index], 16))) {
|
||||
for (let i = 0; i < 40; i++) {
|
||||
if (isInvalidChar(address[i], parseInt(hash[i], 16))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ import type { Keypair } from '../../types';
|
||||
|
||||
import { assert, bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { hmacSha512 } from '../../hmac';
|
||||
import { BN_BE_32_OPTS } from '../../bn';
|
||||
import { hmacShaAsU8a } from '../../hmac';
|
||||
import { secp256k1KeypairFromSeed, secp256k1PrivateKeyTweakAdd } from '../../secp256k1';
|
||||
import { HARDENED, hdValidatePath } from '../validatePath';
|
||||
|
||||
@@ -24,13 +25,13 @@ function createCoded (secretKey: Uint8Array, chainCode: Uint8Array): CodedKeypai
|
||||
}
|
||||
|
||||
function deriveChild (hd: CodedKeypair, index: number): CodedKeypair {
|
||||
const indexBuffer = bnToU8a(index, { bitLength: 32, isLe: false });
|
||||
const indexBuffer = bnToU8a(index, BN_BE_32_OPTS);
|
||||
const data = index >= HARDENED
|
||||
? u8aConcat(new Uint8Array(1), hd.secretKey, indexBuffer)
|
||||
: u8aConcat(hd.publicKey, indexBuffer);
|
||||
|
||||
try {
|
||||
const I = hmacSha512(hd.chainCode, data);
|
||||
const I = hmacShaAsU8a(hd.chainCode, data, 512);
|
||||
|
||||
return createCoded(
|
||||
secp256k1PrivateKeyTweakAdd(hd.secretKey, I.slice(0, 32)),
|
||||
@@ -43,8 +44,8 @@ function deriveChild (hd: CodedKeypair, index: number): CodedKeypair {
|
||||
}
|
||||
|
||||
export function hdEthereum (seed: Uint8Array, path = ''): Keypair {
|
||||
const I = hmacSha512(MASTER_SECRET, seed);
|
||||
const hd = createCoded(I.slice(0, 32), I.slice(32));
|
||||
const I = hmacShaAsU8a(MASTER_SECRET, seed, 512);
|
||||
let hd = createCoded(I.slice(0, 32), I.slice(32));
|
||||
|
||||
if (!path || path === 'm' || path === 'M' || path === "m'" || path === "M'") {
|
||||
return hd;
|
||||
@@ -52,16 +53,15 @@ export function hdEthereum (seed: Uint8Array, path = ''): Keypair {
|
||||
|
||||
assert(hdValidatePath(path), 'Invalid derivation path');
|
||||
|
||||
return path
|
||||
.split('/')
|
||||
.slice(1)
|
||||
.reduce((hd: CodedKeypair, c): CodedKeypair =>
|
||||
deriveChild(
|
||||
hd, parseInt(c, 10) + (
|
||||
(c.length > 1) && c.endsWith("'")
|
||||
? HARDENED
|
||||
: 0
|
||||
)
|
||||
), hd
|
||||
);
|
||||
const parts = path.split('/').slice(1);
|
||||
|
||||
for (const p of parts) {
|
||||
hd = deriveChild(hd, parseInt(p, 10) + (
|
||||
(p.length > 1) && p.endsWith("'")
|
||||
? HARDENED
|
||||
: 0
|
||||
));
|
||||
}
|
||||
|
||||
return hd;
|
||||
}
|
||||
|
||||
@@ -3,27 +3,32 @@
|
||||
|
||||
import { BN_EIGHT, bnToU8a, u8aConcat, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import { hmacSha512 } from '../../hmac';
|
||||
import { BN_LE_32_OPTS, BN_LE_512_OPTS, BN_LE_OPTS } from '../../bn';
|
||||
import { hmacShaAsU8a } from '../../hmac';
|
||||
|
||||
// performs hard-only derivation on the xprv
|
||||
export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Array {
|
||||
const kl = xprv.subarray(0, 32);
|
||||
const kr = xprv.subarray(32, 64);
|
||||
const cc = xprv.subarray(64, 96);
|
||||
const data = u8aConcat([0], kl, kr, bnToU8a(index, { bitLength: 32, isLe: true }));
|
||||
const z = hmacSha512(cc, data);
|
||||
const data = u8aConcat([0], kl, kr, bnToU8a(index, BN_LE_32_OPTS));
|
||||
const z = hmacShaAsU8a(cc, data, 512);
|
||||
|
||||
data[0] = 0x01;
|
||||
|
||||
return u8aConcat(
|
||||
bnToU8a(
|
||||
u8aToBn(kl, { isLe: true }).iadd(u8aToBn(z.subarray(0, 28), { isLe: true }).imul(BN_EIGHT)),
|
||||
{ bitLength: 512, isLe: true }
|
||||
u8aToBn(kl, BN_LE_OPTS).iadd(
|
||||
u8aToBn(z.subarray(0, 28), BN_LE_OPTS).imul(BN_EIGHT)
|
||||
),
|
||||
BN_LE_512_OPTS
|
||||
).subarray(0, 32),
|
||||
bnToU8a(
|
||||
u8aToBn(kr, { isLe: true }).iadd(u8aToBn(z.subarray(32, 64), { isLe: true })),
|
||||
{ bitLength: 512, isLe: true }
|
||||
u8aToBn(kr, BN_LE_OPTS).iadd(
|
||||
u8aToBn(z.subarray(32, 64), BN_LE_OPTS)
|
||||
),
|
||||
BN_LE_512_OPTS
|
||||
).subarray(0, 32),
|
||||
hmacSha512(cc, data).subarray(32, 64)
|
||||
hmacShaAsU8a(cc, data, 512).subarray(32, 64)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12,27 +12,28 @@ import { ledgerDerivePrivate } from './derivePrivate';
|
||||
import { ledgerMaster } from './master';
|
||||
|
||||
export function hdLedger (_mnemonic: string, path: string): Keypair {
|
||||
const parts = _mnemonic
|
||||
const words = _mnemonic
|
||||
.split(' ')
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s);
|
||||
|
||||
assert([12, 24, 25].includes(parts.length), 'Expected a mnemonic with 24 words (or 25 including a password)');
|
||||
assert([12, 24, 25].includes(words.length), 'Expected a mnemonic with 24 words (or 25 including a password)');
|
||||
|
||||
const [mnemonic, password] = parts.length === 25
|
||||
? [parts.slice(0, 24).join(' '), parts[24]]
|
||||
: [parts.join(' '), ''];
|
||||
const [mnemonic, password] = words.length === 25
|
||||
? [words.slice(0, 24).join(' '), words[24]]
|
||||
: [words.join(' '), ''];
|
||||
|
||||
assert(mnemonicValidate(mnemonic), 'Invalid mnemonic passed to ledger derivation');
|
||||
assert(hdValidatePath(path), 'Invalid derivation path');
|
||||
|
||||
return naclKeypairFromSeed(
|
||||
path
|
||||
.split('/')
|
||||
.slice(1)
|
||||
.map((n) => parseInt(n.replace(/'$/, ''), 10))
|
||||
.map((n) => (n < HARDENED) ? (n + HARDENED) : n)
|
||||
.reduce((x, n) => ledgerDerivePrivate(x, n), ledgerMaster(mnemonic, password))
|
||||
.slice(0, 32)
|
||||
);
|
||||
const parts = path.split('/').slice(1);
|
||||
let seed = ledgerMaster(mnemonic, password);
|
||||
|
||||
for (const p of parts) {
|
||||
const n = parseInt(p.replace(/'$/, ''), 10);
|
||||
|
||||
seed = ledgerDerivePrivate(seed, (n < HARDENED) ? (n + HARDENED) : n);
|
||||
}
|
||||
|
||||
return naclKeypairFromSeed(seed.slice(0, 32));
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { hmacSha256, hmacSha512 } from '../../hmac';
|
||||
import { hmacShaAsU8a } from '../../hmac';
|
||||
import { mnemonicToSeedSync } from '../../mnemonic/bip39';
|
||||
|
||||
const ED25519_CRYPTO = 'ed25519 seed';
|
||||
@@ -11,11 +11,11 @@ const ED25519_CRYPTO = 'ed25519 seed';
|
||||
// gets an xprv from a mnemonic
|
||||
export function ledgerMaster (mnemonic: string, password?: string): Uint8Array {
|
||||
const seed = mnemonicToSeedSync(mnemonic, password);
|
||||
const chainCode = hmacSha256(ED25519_CRYPTO, new Uint8Array([1, ...seed]));
|
||||
const chainCode = hmacShaAsU8a(ED25519_CRYPTO, new Uint8Array([1, ...seed]), 256);
|
||||
let priv;
|
||||
|
||||
while (!priv || (priv[31] & 0b0010_0000)) {
|
||||
priv = hmacSha512(ED25519_CRYPTO, priv || seed);
|
||||
priv = hmacShaAsU8a(ED25519_CRYPTO, priv || seed, 512);
|
||||
}
|
||||
|
||||
priv[0] &= 0b1111_1000;
|
||||
|
||||
@@ -10,9 +10,15 @@ export function hdValidatePath (path: string): boolean {
|
||||
|
||||
const parts = path.split('/').slice(1);
|
||||
|
||||
return parts.every((n) => /^\d+'?$/.test(n)) && (
|
||||
!parts
|
||||
.map((n) => parseInt(n.replace(/'$/, ''), 10))
|
||||
.some((n) => isNaN(n) || (n >= HARDENED) || (n < 0))
|
||||
);
|
||||
for (const p of parts) {
|
||||
const n = /^\d+'?$/.test(p)
|
||||
? parseInt(p.replace(/'$/, ''), 10)
|
||||
: Number.NaN;
|
||||
|
||||
if (isNaN(n) || (n >= HARDENED) || (n < 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function createAsHex <T extends (...args: any[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
|
||||
return (...args: Parameters<T>): HexString =>
|
||||
u8aToHex(fn(...args));
|
||||
}
|
||||
|
||||
export function createBitHasher <T> (bitLength: T, fn: (data: HexString | Buffer | Uint8Array | string, bitLength: T, onlyJs?: boolean) => Uint8Array): (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean) => Uint8Array {
|
||||
return (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||
fn(data, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
export function isWasmOnly (onlyJs?: boolean): boolean {
|
||||
return isReady() && !onlyJs;
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { hmacSha256 } from './sha256';
|
||||
export { hmacSha512 } from './sha512';
|
||||
export { hmacShaAsU8a, hmacSha256AsU8a, hmacSha512AsU8a } from './shaAsU8a';
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
export function hmacSha (key: Uint8Array | string, data: Uint8Array, is512: boolean): Uint8Array {
|
||||
return Uint8Array.from(
|
||||
hash
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.hmac(is512 ? hash.sha512 : hash.sha256, key)
|
||||
.update(data)
|
||||
.digest());
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hmacSha } from './sha';
|
||||
|
||||
export function hmacSha256 (key: Uint8Array | string, data: Uint8Array): Uint8Array {
|
||||
return hmacSha(key, data, false);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hmacSha } from './sha';
|
||||
|
||||
export function hmacSha512 (key: Uint8Array | string, data: Uint8Array): Uint8Array {
|
||||
return hmacSha(key, data, true);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceTest } from '../../test/performance';
|
||||
import { hmacShaAsU8a } from '.';
|
||||
|
||||
describe('hmacShaAsU8a', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
const key = stringToU8a('secret');
|
||||
const data = stringToU8a('some message');
|
||||
const output = {
|
||||
256: hexToU8a(
|
||||
'0xf28a70b41263840e5c059a0a733336e0957efba87902aa8cca11441d4b0c96d7'
|
||||
),
|
||||
512: hexToU8a(
|
||||
'0x295832e97ed77be75a9fa98029497e4a722c4b9a2f21b39d34f1befa931a39ec520fd24711d6f5c03501384ea66b83066a01a82c57a0460f8cd1f471fcce5841'
|
||||
)
|
||||
};
|
||||
|
||||
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('returns an hex representation (string)', (): void => {
|
||||
expect(
|
||||
hmacShaAsU8a(key, data, bitLength, onlyJs)
|
||||
).toEqual(output[bitLength]);
|
||||
});
|
||||
});
|
||||
|
||||
performanceTest(`hmacShaAsU8a, bitLength=${bitLength}`, 16000, (input, onlyJs) =>
|
||||
hmacShaAsU8a(input, input, bitLength, onlyJs)
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { hmacSha256, hmacSha512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { isWasmOnly } from '../helpers';
|
||||
|
||||
type HashFn = (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array;
|
||||
|
||||
function createSha (bitLength: 256 | 512 = 256): HashFn {
|
||||
return (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean): Uint8Array =>
|
||||
hmacShaAsU8a(key, data, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8aKey = u8aToU8a(key);
|
||||
|
||||
return isWasmOnly(onlyJs)
|
||||
? is256
|
||||
? hmacSha256(u8aKey, data)
|
||||
: hmacSha512(u8aKey, data)
|
||||
: new Uint8Array(
|
||||
is256
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
? hash.hmac(hash.sha256, u8aKey).update(data).digest()
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
: hash.hmac(hash.sha512, u8aKey).update(data).digest()
|
||||
);
|
||||
}
|
||||
|
||||
export const hmacSha256AsU8a = createSha(256);
|
||||
export const hmacSha512AsU8a = createSha(512);
|
||||
@@ -1,28 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { keccakAsU8a } from './asU8a';
|
||||
|
||||
/**
|
||||
* @name keccakAsHex
|
||||
* @summary Creates a keccak hex string from the input.
|
||||
* @description
|
||||
* From either a `string` or a `Buffer` input, create the keccak and return the result as a `0x` prefixed hex string.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { keccakAsHex } from '@polkadot/util-crypto';
|
||||
*
|
||||
* keccakAsHex('123'); // => 0x...
|
||||
* ```
|
||||
*/
|
||||
export function keccakAsHex (value: HexString | Buffer | Uint8Array | string, bitLength?: 256 | 512): HexString {
|
||||
return u8aToHex(
|
||||
keccakAsU8a(value, bitLength)
|
||||
);
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceTest } from '../../test/performance';
|
||||
import { keccakAsU8a } from '.';
|
||||
|
||||
describe('keccakAsU8a', (): void => {
|
||||
@@ -21,8 +22,8 @@ describe('keccakAsU8a', (): void => {
|
||||
)
|
||||
};
|
||||
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
|
||||
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('returns an hex representation (string)', (): void => {
|
||||
expect(
|
||||
keccakAsU8a(input, bitLength, onlyJs)
|
||||
@@ -41,5 +42,9 @@ describe('keccakAsU8a', (): void => {
|
||||
).toEqual(output[bitLength]);
|
||||
});
|
||||
});
|
||||
|
||||
performanceTest(`keccakAsU8a, bitLength=${bitLength}`, 128000, (input, onlyJs) =>
|
||||
keccakAsU8a(input, bitLength, onlyJs)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,9 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import js from 'js-sha3';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, keccak256 } from '@polkadot/wasm-crypto';
|
||||
import { keccak256, keccak512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { createAsHex, createBitHasher, isWasmOnly } from '../helpers';
|
||||
|
||||
/**
|
||||
* @name keccakAsU8a
|
||||
@@ -22,12 +24,35 @@ import { isReady, keccak256 } from '@polkadot/wasm-crypto';
|
||||
* keccakAsU8a('123'); // => Uint8Array
|
||||
* ```
|
||||
*/
|
||||
export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs = false): Uint8Array {
|
||||
export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return isReady() && is256 && !onlyJs
|
||||
? keccak256(u8aToU8a(value))
|
||||
return isWasmOnly(onlyJs)
|
||||
? is256
|
||||
? keccak256(u8a)
|
||||
: keccak512(u8a)
|
||||
: new Uint8Array(
|
||||
(is256 ? js.keccak256 : js.keccak512).update(u8aToU8a(value)).arrayBuffer()
|
||||
is256
|
||||
? js.keccak256.update(u8a).arrayBuffer()
|
||||
: js.keccak512.update(u8a).arrayBuffer()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name keccak256AsU8a
|
||||
* @description Creates a keccak256 Uint8Array from the input.
|
||||
*/
|
||||
export const keccak256AsU8a = createBitHasher(256, keccakAsU8a);
|
||||
|
||||
/**
|
||||
* @name keccak512AsU8a
|
||||
* @description Creates a keccak512 Uint8Array from the input.
|
||||
*/
|
||||
export const keccak512AsU8a = createBitHasher(512, keccakAsU8a);
|
||||
|
||||
/**
|
||||
* @name keccakAsHex
|
||||
* @description Creates a keccak hex string from the input.
|
||||
*/
|
||||
export const keccakAsHex = createAsHex(keccakAsU8a);
|
||||
|
||||
@@ -5,5 +5,4 @@
|
||||
* @summary Create Keccak256/512 values as hex & Uint8Array output
|
||||
*/
|
||||
|
||||
export { keccakAsHex } from './asHex';
|
||||
export { keccakAsU8a } from './asU8a';
|
||||
export { keccakAsHex, keccakAsU8a, keccak256AsU8a, keccak512AsU8a } from './asU8a';
|
||||
|
||||
@@ -6,14 +6,11 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import { BN, bnToU8a, compactAddLength, hexToU8a, isBigInt, isBn, isHex, isNumber, isString, stringToU8a } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from '../blake2/asU8a';
|
||||
import { BN_LE_256_OPTS } from '../bn';
|
||||
|
||||
const RE_NUMBER = /^\d+$/;
|
||||
|
||||
const JUNCTION_ID_LEN = 32;
|
||||
const BN_OPTIONS = {
|
||||
bitLength: 256,
|
||||
isLe: true
|
||||
};
|
||||
|
||||
export class DeriveJunction {
|
||||
readonly #chainCode: Uint8Array = new Uint8Array(32);
|
||||
@@ -61,7 +58,7 @@ export class DeriveJunction {
|
||||
|
||||
public soft (value: HexString | number | string | bigint | BN | Uint8Array): DeriveJunction {
|
||||
if (isNumber(value) || isBn(value) || isBigInt(value)) {
|
||||
return this.soft(bnToU8a(value, BN_OPTIONS));
|
||||
return this.soft(bnToU8a(value, BN_LE_256_OPTS));
|
||||
} else if (isHex(value)) {
|
||||
return this.soft(hexToU8a(value));
|
||||
} else if (isString(value)) {
|
||||
|
||||
@@ -23,9 +23,9 @@ export function keyExtractPath (derivePath: string): ExtractResult {
|
||||
if (parts) {
|
||||
constructed = parts.join('');
|
||||
|
||||
parts.forEach((value: string): void => {
|
||||
path.push(DeriveJunction.from(value.substr(1)));
|
||||
});
|
||||
for (const p of parts) {
|
||||
path.push(DeriveJunction.from(p.substr(1)));
|
||||
}
|
||||
}
|
||||
|
||||
assert(constructed === derivePath, () => `Re-constructed path "${constructed}" does not match input`);
|
||||
|
||||
@@ -18,8 +18,11 @@ const generators = {
|
||||
|
||||
export function keyFromPath (pair: Keypair, path: DeriveJunction[], type: KeypairType): Keypair {
|
||||
const keyHdkd = generators[type];
|
||||
let result = pair;
|
||||
|
||||
return path.reduce((pair, junction): Keypair => {
|
||||
return keyHdkd(pair, junction);
|
||||
}, pair);
|
||||
for (const junction of path) {
|
||||
result = keyHdkd(result, junction);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -13,12 +13,11 @@
|
||||
// - Remove setting of wordlist passing of wordlist in functions
|
||||
// - Remove mnemonicToSeed (we only use the sync variant)
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { assert, stringToU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { pbkdf2Encode } from '../pbkdf2';
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { randomAsU8a } from '../random';
|
||||
import { sha256AsU8a } from '../sha';
|
||||
import DEFAULT_WORDLIST from './bip39-en';
|
||||
|
||||
const INVALID_MNEMONIC = 'Invalid mnemonic';
|
||||
@@ -38,18 +37,16 @@ function bytesToBinary (bytes: number[]): string {
|
||||
}
|
||||
|
||||
function deriveChecksumBits (entropyBuffer: Uint8Array): string {
|
||||
const ENT = entropyBuffer.length * 8;
|
||||
const CS = ENT / 32;
|
||||
const result = hash.sha256().update(entropyBuffer).digest();
|
||||
|
||||
return bytesToBinary(Array.from(result)).slice(0, CS);
|
||||
return bytesToBinary(
|
||||
Array.from(sha256AsU8a(entropyBuffer))
|
||||
).slice(0, (entropyBuffer.length * 8) / 32);
|
||||
}
|
||||
|
||||
export function mnemonicToSeedSync (mnemonic: string, password?: string): Uint8Array {
|
||||
const mnemonicBuffer = stringToU8a(normalize(mnemonic));
|
||||
const saltBuffer = stringToU8a(`mnemonic${normalize(password)}`);
|
||||
|
||||
return pbkdf2Encode(mnemonicBuffer, saltBuffer).password;
|
||||
return pbkdf2Encode(
|
||||
stringToU8a(normalize(mnemonic)),
|
||||
stringToU8a(`mnemonic${normalize(password)}`)
|
||||
).password;
|
||||
}
|
||||
|
||||
export function mnemonicToEntropy (mnemonic: string): Uint8Array {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { bip39Generate, isReady } from '@polkadot/wasm-crypto';
|
||||
import { bip39Generate } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { isWasmOnly } from '../helpers';
|
||||
import { generateMnemonic } from './bip39';
|
||||
|
||||
export type WordCount = 12 | 15 | 18 | 21 | 24;
|
||||
@@ -28,8 +29,8 @@ const STRENGTH_MAP = {
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicGenerate (numWords: WordCount = 12, onlyJs = false): string {
|
||||
return isReady() && !onlyJs
|
||||
export function mnemonicGenerate (numWords: WordCount = 12, onlyJs?: boolean): string {
|
||||
return isWasmOnly(onlyJs)
|
||||
? bip39Generate(numWords)
|
||||
: generateMnemonic(STRENGTH_MAP[numWords]);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { bip39ToEntropy, isReady } from '@polkadot/wasm-crypto';
|
||||
import { bip39ToEntropy } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { isWasmOnly } from '../helpers';
|
||||
import { mnemonicToEntropy as jsToEntropy } from './bip39';
|
||||
|
||||
export function mnemonicToEntropy (mnemonic: string, onlyJs = false): Uint8Array {
|
||||
return isReady() && !onlyJs
|
||||
export function mnemonicToEntropy (mnemonic: string, onlyJs?: boolean): Uint8Array {
|
||||
return isWasmOnly(onlyJs)
|
||||
? bip39ToEntropy(mnemonic)
|
||||
: jsToEntropy(mnemonic);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
import { bip39ToSeed, isReady } from '@polkadot/wasm-crypto';
|
||||
import { bip39ToSeed } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { isWasmOnly } from '../helpers';
|
||||
import { mnemonicToSeedSync } from './bip39';
|
||||
import { mnemonicValidate } from './validate';
|
||||
|
||||
@@ -24,12 +25,12 @@ import { mnemonicValidate } from './validate';
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs = false, byteLength: 32 | 64 = 32): Uint8Array {
|
||||
export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs?: boolean, byteLength: 32 | 64 = 32): Uint8Array {
|
||||
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
|
||||
assert([32, 64].includes(byteLength), () => `Invalid seed length ${byteLength}, expected 32 or 64`);
|
||||
|
||||
if (byteLength === 32) {
|
||||
return isReady() && !onlyJs
|
||||
return isWasmOnly(onlyJs)
|
||||
? bip39ToSeed(mnemonic, password)
|
||||
: mnemonicToSeedSync(mnemonic, password).subarray(0, 32);
|
||||
} else if (byteLength === 64) {
|
||||
|
||||
@@ -2,16 +2,17 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert, stringToU8a } from '@polkadot/util';
|
||||
import { bip39ToMiniSecret, isReady } from '@polkadot/wasm-crypto';
|
||||
import { bip39ToMiniSecret } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { isWasmOnly } from '../helpers';
|
||||
import { pbkdf2Encode } from '../pbkdf2';
|
||||
import { mnemonicToEntropy } from './toEntropy';
|
||||
import { mnemonicValidate } from './validate';
|
||||
|
||||
export function mnemonicToMiniSecret (mnemonic: string, password = '', onlyJs = false): Uint8Array {
|
||||
export function mnemonicToMiniSecret (mnemonic: string, password = '', onlyJs?: boolean): Uint8Array {
|
||||
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
|
||||
|
||||
if (isReady() && !onlyJs) {
|
||||
if (isWasmOnly(onlyJs)) {
|
||||
return bip39ToMiniSecret(mnemonic, password);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { bip39Validate, isReady } from '@polkadot/wasm-crypto';
|
||||
import { bip39Validate } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { isWasmOnly } from '../helpers';
|
||||
import { validateMnemonic } from './bip39';
|
||||
|
||||
/**
|
||||
@@ -18,8 +19,8 @@ import { validateMnemonic } from './bip39';
|
||||
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicValidate (mnemonic: string, onlyJs = false): boolean {
|
||||
return isReady() && !onlyJs
|
||||
export function mnemonicValidate (mnemonic: string, onlyJs?: boolean): boolean {
|
||||
return isWasmOnly(onlyJs)
|
||||
? bip39Validate(mnemonic)
|
||||
: validateMnemonic(mnemonic);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { convertPublicKeyToCurve25519, convertSecretKeyToCurve25519 } from '.';
|
||||
|
||||
describe('convertPublicKeyToCurve25519', (): void => {
|
||||
it('converts a known key correctly', (): void => {
|
||||
expect(
|
||||
u8aToHex(convertPublicKeyToCurve25519(
|
||||
new Uint8Array([
|
||||
180, 114, 93, 155, 165, 255, 217, 82,
|
||||
16, 250, 209, 11, 193, 10, 88, 218,
|
||||
190, 190, 41, 193, 236, 252, 1, 152,
|
||||
216, 214, 0, 41, 45, 138, 13, 53
|
||||
])
|
||||
))
|
||||
).toEqual('0x5e05848ceeef4f306d6e285a26faf1a8070cf0c89f8d28ab966002fe4ad17746');
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertSecretKeyToCurve25519', (): void => {
|
||||
it('converts a known key correctly', (): void => {
|
||||
expect(
|
||||
u8aToHex(convertSecretKeyToCurve25519(
|
||||
new Uint8Array([
|
||||
1, 2, 3, 4, 5, 6, 7, 8,
|
||||
1, 2, 3, 4, 5, 6, 7, 8,
|
||||
1, 2, 3, 4, 5, 6, 7, 8,
|
||||
1, 2, 3, 4, 5, 6, 7, 8
|
||||
])
|
||||
))
|
||||
).toEqual('0xd875a82940b9174dabb1c0c895485810cc2870f5808565b59d4ef708bdcf526c');
|
||||
});
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user