Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bff05aad32 | ||
|
|
0f98c1ac22 | ||
|
|
f72d9a2a26 | ||
|
|
1d0558a537 | ||
|
|
94e7c05573 | ||
|
|
12601805b1 | ||
|
|
9194f417fe | ||
|
|
6a3118e66c | ||
|
|
4ccad202c5 | ||
|
|
96a019f6eb | ||
|
|
bad6a1b991 | ||
|
|
d83e50ee19 | ||
|
|
feae08fc0f | ||
|
|
fdd32df12f | ||
|
|
17a9b653ac | ||
|
|
6154454c38 | ||
|
|
ac0ec5be55 | ||
|
|
3802426046 | ||
|
|
d91b27b180 | ||
|
|
1f9a1a5f8a | ||
|
|
6b3edffe55 | ||
|
|
7d46eb8423 | ||
|
|
a7eb0ed319 | ||
|
|
227a34d55d | ||
|
|
8a29dc2b57 | ||
|
|
88d1a71df8 | ||
|
|
6f53068297 | ||
|
|
5bf71dab41 | ||
|
|
b6f3dfb2ca | ||
|
|
4d3a2d9610 | ||
|
|
7eaf013572 | ||
|
|
c673b96963 | ||
|
|
c57eee5027 | ||
|
|
9efd2db377 | ||
|
|
c90246ec0b | ||
|
|
fbe1532f62 | ||
|
|
f514402b15 | ||
|
|
5ea6992704 | ||
|
|
7955022ce5 | ||
|
|
000d097de2 | ||
|
|
8a6c8b64df | ||
|
|
306e503d23 | ||
|
|
648019f9d4 | ||
|
|
5e48cfbbaa | ||
|
|
57825fc73b | ||
|
|
19ae016723 | ||
|
|
8d7bed5865 | ||
|
|
e019a3043a | ||
|
|
9dfd013755 | ||
|
|
a5df4ed764 | ||
|
|
a938a9f998 | ||
|
|
1a6f81e2a7 | ||
|
|
6ab85a42bb | ||
|
|
21c9e1aefd | ||
|
|
58f740f3de | ||
|
|
ddc6b83e96 | ||
|
|
285e595930 | ||
|
|
dce16d59e6 | ||
|
|
b8edcb0e65 | ||
|
|
b63607c4a0 | ||
|
|
c56c86432a | ||
|
|
e52fe9a838 | ||
|
|
06dca3d955 | ||
|
|
0908dbb861 | ||
|
|
e97410df52 | ||
|
|
faa6734329 | ||
|
|
f2f25815c1 | ||
|
|
80785babb4 | ||
|
|
59761086d0 | ||
|
|
05c82721e8 | ||
|
|
1b13cf0eea | ||
|
|
e6777ece62 | ||
|
|
7436976598 | ||
|
|
699ef952c3 | ||
|
|
cd9dc35475 | ||
|
|
ee7923c457 | ||
|
|
bd3f51844c | ||
|
|
986dfe5006 | ||
|
|
1eba41b2e6 | ||
|
|
88c2d16951 | ||
|
|
303a57c4b5 | ||
|
|
7bfca0d40f | ||
|
|
916187dbf2 | ||
|
|
cfc598b30f | ||
|
|
67bfe3a52e | ||
|
|
0299cba18e | ||
|
|
182c6770d1 | ||
|
|
c89ec8a964 | ||
|
|
dc63304735 | ||
|
|
9ce825de94 | ||
|
|
3cb41c3b03 | ||
|
|
b1f9c37f6a | ||
|
|
4a69486993 | ||
|
|
3a7ab0454f | ||
|
|
ecfc262bcd | ||
|
|
776a653f42 | ||
|
|
ee99ffdbb4 | ||
|
|
bda848f484 | ||
|
|
82a63887a6 | ||
|
|
50317d7dc3 | ||
|
|
39e6a3c048 | ||
|
|
db93c3076b | ||
|
|
c8f0ad55c4 | ||
|
|
2104c50bd0 | ||
|
|
7e9b2770b6 | ||
|
|
8bf24c2f85 | ||
|
|
60337a3b55 | ||
|
|
f527c80b5c | ||
|
|
bdd2bcf236 | ||
|
|
cf370febf0 | ||
|
|
715ef6e7db | ||
|
|
cc644e85bf | ||
|
|
c7b8d95097 | ||
|
|
42de7cebe1 | ||
|
|
2e520c933a | ||
|
|
5d1dd0dd27 | ||
|
|
154f9f22f1 | ||
|
|
98e398954e | ||
|
|
3643c2e306 | ||
|
|
80195f6d11 | ||
|
|
6bc648bfe0 | ||
|
|
c29b12d3b5 | ||
|
|
8a5b514944 | ||
|
|
64fd66bd2a | ||
|
|
75aa7ba2c7 | ||
|
|
25196c3367 | ||
|
|
eeb68c7e0f |
+7
-1
@@ -19,4 +19,10 @@
|
||||
7.0.3
|
||||
7.1.2
|
||||
7.8.2
|
||||
7.9.2
|
||||
7.9.2
|
||||
8.0.2
|
||||
8.0.3
|
||||
8.0.4
|
||||
8.0.5
|
||||
8.1.2
|
||||
8.2.2
|
||||
@@ -1,5 +1,21 @@
|
||||
checks:
|
||||
argument-count:
|
||||
config:
|
||||
threshold: 5
|
||||
method-complexity:
|
||||
config:
|
||||
threshold: 7
|
||||
method-count:
|
||||
config:
|
||||
threshold: 25
|
||||
method-lines:
|
||||
config:
|
||||
threshold: 30
|
||||
|
||||
exclude_patterns:
|
||||
- "**/*.spec.js"
|
||||
- "**/*.spec.ts"
|
||||
- "packages/networks/src/substrate.ts"
|
||||
- "packages/util/src/is/utf8.ts"
|
||||
- "packages/x-noble-hashes/src/**.ts"
|
||||
- "packages/x-noble-secp256k1/src/**.ts"
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
packages/x-noble-hashes/**/*.ts
|
||||
packages/x-noble-secp256k1/**/*.ts
|
||||
+2
-8
@@ -6,18 +6,12 @@ const base = require('@polkadot/dev/config/eslint.cjs');
|
||||
module.exports = {
|
||||
...base,
|
||||
ignorePatterns: [
|
||||
'.eslintrc.js',
|
||||
'.github/**',
|
||||
'.vscode/**',
|
||||
'.yarn/**',
|
||||
'**/build/*',
|
||||
'**/coverage/*',
|
||||
'**/node_modules/*'
|
||||
...base.ignorePatterns
|
||||
],
|
||||
parserOptions: {
|
||||
...base.parserOptions,
|
||||
project: [
|
||||
'./tsconfig.json'
|
||||
'./tsconfig.eslint.json'
|
||||
]
|
||||
},
|
||||
rules: {
|
||||
|
||||
@@ -2,7 +2,7 @@ name: 'Lock Threads'
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '15 1 * * *'
|
||||
- cron: '15 1/3 * * *'
|
||||
|
||||
jobs:
|
||||
lock:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
name: 'Close stale issues and PRs'
|
||||
on:
|
||||
schedule:
|
||||
- cron: '50 1 * * *'
|
||||
- cron: '50 1/3 * * *'
|
||||
|
||||
jobs:
|
||||
stale:
|
||||
|
||||
+3
-2
@@ -11,7 +11,9 @@ tmp/
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.idea
|
||||
.npmrc
|
||||
.vscode
|
||||
.yarn/*
|
||||
!.yarn/releases
|
||||
!.yarn/plugins
|
||||
@@ -24,5 +26,4 @@ yarn-debug.log*
|
||||
yarn-error.log*
|
||||
store.db
|
||||
store.db.compacted
|
||||
.idea
|
||||
.vscode
|
||||
tsconfig.*buildinfo
|
||||
|
||||
@@ -1,5 +1,79 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 8.2.2 Dec 19, 2021
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Bifrost Ledger capabilities (Thanks to https://github.com/awesomepan)
|
||||
- Add Sora genesisHash & Ledger capabilities (Thanks to https://github.com/NoodleSploder)
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `isPromise` check utility function
|
||||
- Add `assertUnreachable` for codepaths that should not be followed
|
||||
- Preserve all-caps word splits in `stringCamelCase`
|
||||
- Adjust `x-*` fallbacks via `x-global/extractGlobal`
|
||||
- Sync with upstream Substrate ss58 registry
|
||||
- Additional tests for `bip39` wordlists
|
||||
- Maintainability cleanups
|
||||
- Update to latest Ledger dependencies
|
||||
- Swap to upstream `@noble` packages
|
||||
- Adjust tsconfig with references
|
||||
- 8.2.2 (8.2.1 failed CI due to incorrect CHANGELOG)
|
||||
|
||||
|
||||
## 8.1.2 Dec 5, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjustment to non-full-word consecutive uppercase via `string{Camel, Pascal}Case`
|
||||
|
||||
|
||||
## 8.1.1 Dec 5, 2021
|
||||
|
||||
Contributed:
|
||||
|
||||
- Add Picasso genesisHash (Thanks to https://github.com/hussein-aitlahcen)
|
||||
|
||||
Changes:
|
||||
|
||||
- Allow for use of `secp256k1` from `@polkadot/wasm-crypto`
|
||||
- Adjust `isBigInt` to not do `instanceof` check
|
||||
- Ensure consequtive capitals in `stringCamelCase` are lowercased
|
||||
- Use `@polkadot/x-bigint` for `BigInt` constructor
|
||||
- Split `@noble/hashes` fork into `x-noble-hashes`
|
||||
- Add `@noble/secp256k1` fork into `x-noble-secp256k1`, replaces `elliptic`
|
||||
|
||||
|
||||
## 8.0.5 Dec 1, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust sha3 expectation of `BigInt` availability
|
||||
|
||||
|
||||
## 8.0.4 Nov 30, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
- Adjust `stringCamelCase` with leading `_`
|
||||
|
||||
|
||||
## 8.0.3 Nov 30, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
- Remove `Buffer` usage in `x-randomvalues`
|
||||
- Adjust `stringCamelCase` for all-caps parts
|
||||
|
||||
|
||||
## 8.0.2 Nov 26, 2021
|
||||
|
||||
Changes:
|
||||
|
||||
- Explicitly include BigInt patched versions
|
||||
|
||||
|
||||
## 8.0.1 Nov 26, 2021
|
||||
|
||||
**Important** In the 8.0 version the hashing utilities have been consolidated around an optimized and audited version from `@noble/hashes`. As in the past the WASM hashing will be used by default if the environment is initialized.
|
||||
|
||||
+2
-3
@@ -13,8 +13,7 @@ module.exports = Object.assign({}, config, {
|
||||
'@polkadot/util-(crypto)(.*)$': '<rootDir>/packages/util-$1/src/$2',
|
||||
// eslint-disable-next-line sort-keys
|
||||
'@polkadot/util(.*)$': '<rootDir>/packages/util/src/$1',
|
||||
'@polkadot/x-(fetch|randomvalues|textdecoder|textencoder|ws)(.*)$': '<rootDir>/packages/x-$1/src/node',
|
||||
'@polkadot/x-global(.*)$': '<rootDir>/packages/x-global/src/$1',
|
||||
'@polkadot/x-rxjs(.*)$': '<rootDir>/packages/x-rxjs/src/$1'
|
||||
'@polkadot/x-(bigint|global)(.*)$': '<rootDir>/packages/x-$1/src/$2',
|
||||
'@polkadot/x-(fetch|randomvalues|textdecoder|textencoder|ws)(.*)$': '<rootDir>/packages/x-$1/src/node'
|
||||
}
|
||||
});
|
||||
|
||||
+7
-8
@@ -10,7 +10,7 @@
|
||||
"url": "https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"version": "8.0.1",
|
||||
"version": "8.2.2",
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
],
|
||||
@@ -22,19 +22,18 @@
|
||||
"lint": "polkadot-dev-run-lint",
|
||||
"networks:crosscheck": "polkadot-dev-run-test packages/networks/src/crosscheck",
|
||||
"networks:sync": "yarn build && node packages/networks/scripts/fromSubstrate.mjs",
|
||||
"postinstall": "polkadot-dev-yarn-only && patch-package --error-on-fail --patch-dir packages/util-crypto/src/patches",
|
||||
"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.16.0",
|
||||
"@polkadot/dev": "^0.63.33",
|
||||
"@polkadot/ts": "^0.4.15",
|
||||
"@types/jest": "^27.0.3",
|
||||
"patch-package": "^6.4.7"
|
||||
"@babel/core": "^7.16.5",
|
||||
"@polkadot/dev": "^0.64.5",
|
||||
"@polkadot/ts": "^0.4.20",
|
||||
"@types/jest": "^27.0.3"
|
||||
},
|
||||
"resolutions": {
|
||||
"typescript": "^4.5.2"
|
||||
"typescript": "^4.5.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,17 +16,17 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "8.0.1",
|
||||
"version": "8.2.2",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@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"
|
||||
"@babel/runtime": "^7.16.5",
|
||||
"@ledgerhq/hw-transport": "^6.20.0",
|
||||
"@ledgerhq/hw-transport-webhid": "^6.20.0",
|
||||
"@ledgerhq/hw-transport-webusb": "^6.20.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@ledgerhq/hw-transport-node-hid-singleton": "^6.11.2"
|
||||
"@ledgerhq/hw-transport-node-hid-singleton": "^6.20.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger-transports', version: '8.0.1' };
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger-transports', version: '8.2.2' };
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "..",
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"references": []
|
||||
}
|
||||
@@ -19,12 +19,12 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "8.0.1",
|
||||
"version": "8.2.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@polkadot/hw-ledger-transports": "8.0.1",
|
||||
"@polkadot/util": "8.0.1",
|
||||
"@zondax/ledger-substrate": "^0.18.0"
|
||||
"@babel/runtime": "^7.16.5",
|
||||
"@polkadot/hw-ledger-transports": "8.2.2",
|
||||
"@polkadot/util": "8.2.2",
|
||||
"@zondax/ledger-substrate": "^0.23.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/hw-ledger authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { knownLedger } from '@polkadot/networks/defaults';
|
||||
|
||||
import { ledgerApps } from './defaults';
|
||||
|
||||
describe('defaults', (): void => {
|
||||
it('has entries for each of the apps (hwledger -> networks)', (): void => {
|
||||
expect(
|
||||
Object.keys(knownLedger).filter((network) =>
|
||||
!ledgerApps[network]
|
||||
)
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('has entries for each of the apps (networks -> hwledger)', (): void => {
|
||||
expect(
|
||||
Object.keys(ledgerApps).filter((network) =>
|
||||
!knownLedger[network]
|
||||
)
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -4,10 +4,11 @@
|
||||
import type Transport from '@ledgerhq/hw-transport';
|
||||
import type { SubstrateApp } from '@zondax/ledger-substrate';
|
||||
|
||||
import { newCentrifugeApp, newDockApp, newEdgewareApp, newEquilibriumApp, newGenshiroApp, newKusamaApp, newNodleApp, newPolkadotApp, newPolymeshApp, newStatemineApp } from '@zondax/ledger-substrate';
|
||||
import { newBifrostApp, newCentrifugeApp, newDockApp, newEdgewareApp, newEquilibriumApp, newGenshiroApp, newKusamaApp, newNodleApp, newPolkadotApp, newPolymeshApp, newSoraApp, 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> = {
|
||||
bifrost: newBifrostApp,
|
||||
centrifuge: newCentrifugeApp,
|
||||
'dock-mainnet': newDockApp,
|
||||
edgeware: newEdgewareApp,
|
||||
@@ -17,5 +18,6 @@ export const ledgerApps: Record<string, (transport: Transport) => SubstrateApp>
|
||||
'nodle-chain': newNodleApp,
|
||||
polkadot: newPolkadotApp,
|
||||
polymesh: newPolymeshApp,
|
||||
sora: newSoraApp,
|
||||
statemine: newStatemineApp
|
||||
};
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger', version: '8.0.1' };
|
||||
export const packageInfo = { name: '@polkadot/hw-ledger', version: '8.2.2' };
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "..",
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"references": [
|
||||
{ "path": "../hw-ledger-transports" },
|
||||
{ "path": "../util" }
|
||||
]
|
||||
}
|
||||
@@ -20,15 +20,15 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "8.0.1",
|
||||
"version": "8.2.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@polkadot/util": "8.0.1",
|
||||
"@polkadot/util-crypto": "8.0.1"
|
||||
"@babel/runtime": "^7.16.5",
|
||||
"@polkadot/util": "8.2.2",
|
||||
"@polkadot/util-crypto": "8.2.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "8.0.1",
|
||||
"@polkadot/util-crypto": "8.0.1"
|
||||
"@polkadot/util": "8.2.2",
|
||||
"@polkadot/util-crypto": "8.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,6 +369,14 @@ describe('keypair', (): void => {
|
||||
).toEqual(ETH_ADDRESS_ONE);
|
||||
});
|
||||
|
||||
it('creates with dev phrase with derivation path specified - addFromUri with type', (): void => {
|
||||
const keyringUntyped = new Keyring();
|
||||
|
||||
expect(
|
||||
keyringUntyped.addFromUri(PHRASE, {}, 'ethereum').address
|
||||
).toEqual(ETH_ADDRESS_ONE);
|
||||
});
|
||||
|
||||
it('encodes a pair toJSON (and decodes)', (): void => {
|
||||
const pair = keyring.createFromUri(PHRASE);
|
||||
const json = pair.toJson('password');
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/keyring', version: '8.0.1' };
|
||||
export const packageInfo = { name: '@polkadot/keyring', version: '8.2.2' };
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { KeyringInstance, KeyringOptions } from './types';
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { assert, hexToU8a } from '@polkadot/util';
|
||||
import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
|
||||
import { Keyring } from './keyring';
|
||||
@@ -111,6 +111,15 @@ export const PAIRSETHEREUM: PairDef[] = [
|
||||
}
|
||||
];
|
||||
|
||||
function createMeta (name?: string, seed?: string) {
|
||||
assert(name || seed, 'Testing pair should have either a name or a seed');
|
||||
|
||||
return {
|
||||
isTesting: true,
|
||||
name: name || (seed && seed.replace('//', '_').toLowerCase())
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @name testKeyring
|
||||
* @summary Create an instance of Keyring pre-populated with locked test accounts
|
||||
@@ -119,17 +128,12 @@ export const PAIRSETHEREUM: PairDef[] = [
|
||||
*/
|
||||
export function createTestKeyring (options: KeyringOptions = {}, isDerived = true): KeyringInstance {
|
||||
const keyring = new Keyring(options);
|
||||
const pairs: PairDef[] = (options.type && options.type === 'ethereum') ? PAIRSETHEREUM : PAIRSSR25519;
|
||||
const pairs = (options.type && options.type === 'ethereum')
|
||||
? PAIRSETHEREUM
|
||||
: PAIRSSR25519;
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
const meta = {
|
||||
isTesting: true,
|
||||
name: name || (seed && seed.replace('//', '_').toLowerCase())
|
||||
};
|
||||
const meta = createMeta(name, seed);
|
||||
const pair = !isDerived && !name && seed
|
||||
? keyring.addFromUri(seed, meta, options.type)
|
||||
: keyring.addPair(
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "..",
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"references": [
|
||||
{ "path": "../util" },
|
||||
{ "path": "../util-crypto" }
|
||||
]
|
||||
}
|
||||
@@ -17,13 +17,14 @@
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "8.0.1",
|
||||
"version": "8.2.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.16.3"
|
||||
"@babel/runtime": "^7.16.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "8.0.1",
|
||||
"@polkadot/x-fetch": "8.0.1"
|
||||
"@polkadot/hw-ledger": "8.2.2",
|
||||
"@polkadot/util": "8.2.2",
|
||||
"@polkadot/x-fetch": "8.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,46 @@
|
||||
// Copyright 2017-2021 @polkadot/networks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ledgerApps } from '@polkadot/hw-ledger/defaults';
|
||||
import { isHex } from '@polkadot/util';
|
||||
|
||||
import { knownGenesis, knownLedger } from './defaults';
|
||||
|
||||
describe('defaults', (): void => {
|
||||
it('has hex values for all genesis chains', (): void => {
|
||||
expect(
|
||||
Object.keys(knownGenesis).filter((network) =>
|
||||
!knownGenesis[network].length ||
|
||||
knownGenesis[network].some((g) => !isHex(g, 256))
|
||||
)
|
||||
).toEqual([]);
|
||||
describe('genesis', (): void => {
|
||||
it('has hex values for all genesis chains', (): void => {
|
||||
expect(
|
||||
Object.keys(knownGenesis).filter((network) =>
|
||||
!knownGenesis[network].length ||
|
||||
knownGenesis[network].some((g) => !isHex(g, 256))
|
||||
)
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('has genesis for all Ledger chains', (): void => {
|
||||
expect(
|
||||
Object.keys(knownLedger).filter((network) =>
|
||||
!knownGenesis[network]
|
||||
)
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
it('has genesis for all Ledger chains', (): void => {
|
||||
expect(
|
||||
Object.keys(knownLedger).filter((network) =>
|
||||
!knownGenesis[network]
|
||||
)
|
||||
).toEqual([]);
|
||||
describe('Ledger', (): void => {
|
||||
it('has entries for each of the apps (hwledger -> networks)', (): void => {
|
||||
expect(
|
||||
Object.keys(knownLedger).filter((network) =>
|
||||
!ledgerApps[network]
|
||||
)
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it('has entries for each of the apps (networks -> hwledger)', (): void => {
|
||||
expect(
|
||||
Object.keys(ledgerApps).filter((network) =>
|
||||
!knownLedger[network]
|
||||
)
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,6 +51,9 @@ export const knownGenesis: KnownGenesis = {
|
||||
'nodle-chain': [
|
||||
'0xa3d114c2b8d0627c1aa9b134eafcf7d05ca561fdc19fb388bb9457f81809fb23'
|
||||
],
|
||||
picasso: [
|
||||
'0xe8e7f0f4c4f5a00720b4821dbfddefea7490bcf0b19009961cc46957984e2c1c'
|
||||
],
|
||||
plasm: [
|
||||
'0x3e86364d4b4894021cb2a0390bcf2feb5517d5292f2de2bb9404227e908b0b8b'
|
||||
],
|
||||
@@ -60,6 +63,9 @@ export const knownGenesis: KnownGenesis = {
|
||||
polymesh: [
|
||||
'0x6fbd74e5e1d0a61d52ccfe9d4adaed16dd3a7caa37c6bc4d0c2fa12e8b2f4063'
|
||||
],
|
||||
sora: [
|
||||
'0x7e4e32d0feafd4f9c9414b0be86373f9a1efa904809b683453a9af6856d38ad5'
|
||||
],
|
||||
stafi: [
|
||||
'0x290a4149f09ea0e402c74c1c7e96ae4239588577fe78932f94f5404c68243d80'
|
||||
],
|
||||
@@ -76,6 +82,7 @@ export const knownIcon: KnownIcon = {
|
||||
centrifuge: 'polkadot',
|
||||
kusama: 'polkadot',
|
||||
polkadot: 'polkadot',
|
||||
sora: 'polkadot',
|
||||
statemine: 'polkadot',
|
||||
statemint: 'polkadot',
|
||||
westmint: 'polkadot'
|
||||
@@ -85,6 +92,7 @@ export const knownIcon: KnownIcon = {
|
||||
// and maps to the known slip44 (minus the `0x8` hard derivation flag)
|
||||
// NOTE: Any network here needs to have a genesisHash attached in the config above
|
||||
export const knownLedger: KnownLedger = {
|
||||
bifrost: 0x00000314,
|
||||
centrifuge: 0x000002eb,
|
||||
'dock-mainnet': 0x00000252,
|
||||
edgeware: 0x0000020b,
|
||||
@@ -94,6 +102,7 @@ export const knownLedger: KnownLedger = {
|
||||
'nodle-chain': 0x000003eb,
|
||||
polkadot: 0x00000162,
|
||||
polymesh: 0x00000253,
|
||||
sora: 0x00000269,
|
||||
statemine: 0x000001b2 // common-good on Kusama, shares derivation
|
||||
};
|
||||
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/networks', version: '8.0.1' };
|
||||
export const packageInfo = { name: '@polkadot/networks', version: '8.2.2' };
|
||||
|
||||
@@ -504,12 +504,21 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
website: 'https://crust.network'
|
||||
},
|
||||
{
|
||||
decimals: [0, 9, 9, 9, 9, 9, 9, 9],
|
||||
displayName: 'Equilibrium Network',
|
||||
network: 'equilibrium',
|
||||
decimals: [9, 9, 9],
|
||||
displayName: 'Genshiro Network',
|
||||
network: 'genshiro',
|
||||
prefix: 67,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['Unknown', 'USD', 'EQ', 'ETH', 'BTC', 'EOS', 'DOT', 'CRV'],
|
||||
symbols: ['GENS', 'EQD', 'LPT0'],
|
||||
website: 'https://genshiro.equilibrium.io'
|
||||
},
|
||||
{
|
||||
decimals: [9],
|
||||
displayName: 'Equilibrium Network',
|
||||
network: 'equilibrium',
|
||||
prefix: 68,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['EQ'],
|
||||
website: 'https://equilibrium.io'
|
||||
},
|
||||
{
|
||||
@@ -531,12 +540,12 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
website: 'https://zeitgeist.pm'
|
||||
},
|
||||
{
|
||||
decimals: [12],
|
||||
decimals: [18],
|
||||
displayName: 'Manta network',
|
||||
network: 'manta',
|
||||
prefix: 77,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['MA'],
|
||||
symbols: ['MANTA'],
|
||||
website: 'https://manta.network'
|
||||
},
|
||||
{
|
||||
@@ -665,6 +674,15 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
symbols: ['NEER'],
|
||||
website: 'https://bit.country'
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Efinity',
|
||||
network: 'efinity',
|
||||
prefix: 1110,
|
||||
standardAccount: 'Sr25519',
|
||||
symbols: ['EFI'],
|
||||
website: 'https://efinity.io/'
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Moonbeam',
|
||||
@@ -692,6 +710,24 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
symbols: ['KAPEX'],
|
||||
website: 'https://totemaccounting.com'
|
||||
},
|
||||
{
|
||||
decimals: [10],
|
||||
displayName: 'Interlay',
|
||||
network: 'interlay',
|
||||
prefix: 2032,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['INTR'],
|
||||
website: 'https://interlay.io/'
|
||||
},
|
||||
{
|
||||
decimals: [12],
|
||||
displayName: 'Kintsugi',
|
||||
network: 'kintsugi',
|
||||
prefix: 2092,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['KINT'],
|
||||
website: 'https://interlay.io/'
|
||||
},
|
||||
{
|
||||
decimals: [18],
|
||||
displayName: 'Subspace testnet',
|
||||
@@ -723,7 +759,7 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
decimals: [12],
|
||||
displayName: 'CESS Testnet',
|
||||
network: 'cess-testnet',
|
||||
prefix: 10042,
|
||||
prefix: 11330,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['TCESS'],
|
||||
website: 'https://cess.cloud'
|
||||
@@ -732,7 +768,7 @@ export const knownSubstrate: KnownSubstrate[] = [
|
||||
decimals: [12],
|
||||
displayName: 'CESS',
|
||||
network: 'cess',
|
||||
prefix: 10043,
|
||||
prefix: 11331,
|
||||
standardAccount: '*25519',
|
||||
symbols: ['CESS'],
|
||||
website: 'https://cess.cloud'
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "..",
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"references": [
|
||||
{ "path": "../hw-ledger" },
|
||||
{ "path": "../util" },
|
||||
{ "path": "../x-fetch" }
|
||||
]
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
{
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"bugs": "https://github.com/polkadot-js/common/issues",
|
||||
"bundledDependencies": [
|
||||
"@noble/hashes"
|
||||
],
|
||||
"contributors": [],
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"engines": {
|
||||
@@ -25,27 +22,29 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "8.0.1",
|
||||
"version": "8.2.2",
|
||||
"browser": {
|
||||
"crypto": false,
|
||||
"stream": false
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@noble/hashes": "0.4.1",
|
||||
"@polkadot/networks": "8.0.1",
|
||||
"@polkadot/util": "8.0.1",
|
||||
"@polkadot/wasm-crypto": "^4.4.1",
|
||||
"@polkadot/x-randomvalues": "8.0.1",
|
||||
"bn.js": "^4.12.0",
|
||||
"@babel/runtime": "^7.16.5",
|
||||
"@noble/hashes": "^0.4.5",
|
||||
"@noble/secp256k1": "^1.3.4",
|
||||
"@polkadot/networks": "8.2.2",
|
||||
"@polkadot/util": "8.2.2",
|
||||
"@polkadot/wasm-crypto": "^4.5.1",
|
||||
"@polkadot/x-bigint": "8.2.2",
|
||||
"@polkadot/x-randomvalues": "8.2.2",
|
||||
"ed2curve": "^0.3.0",
|
||||
"elliptic": "^6.5.4",
|
||||
"micro-base": "^0.9.0",
|
||||
"micro-base": "^0.10.0",
|
||||
"tweetnacl": "^1.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"@types/ed2curve": "^0.2.2",
|
||||
"@types/elliptic": "^6.4.14"
|
||||
"@types/ed2curve": "^0.2.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/util": "8.0.1"
|
||||
"@polkadot/util": "8.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,21 +3,15 @@
|
||||
|
||||
import { hexToU8a, stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { createTestPairs, TestKeyringMap } from '../../../keyring/src/testingPairs';
|
||||
import { ALICE_PUBLIC_SR } from './encode.spec';
|
||||
import { decodeAddress } from '.';
|
||||
|
||||
describe('decodeAddress', (): void => {
|
||||
let keyring: TestKeyringMap;
|
||||
|
||||
beforeAll((): void => {
|
||||
keyring = createTestPairs({ type: 'sr25519' });
|
||||
});
|
||||
|
||||
it('decodes an address', (): void => {
|
||||
expect(
|
||||
decodeAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')
|
||||
).toEqual(
|
||||
keyring.alice.publicKey
|
||||
ALICE_PUBLIC_SR
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { createTestPairs } from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import { encodeAddress } from '.';
|
||||
|
||||
const keyring = createTestPairs({ type: 'ed25519' }, false);
|
||||
export const ALICE_PUBLIC_SR = new Uint8Array([212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125]);
|
||||
|
||||
export const ALICE_PUBLIC_ED = new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79]);
|
||||
|
||||
const SUBKEY = [
|
||||
{
|
||||
@@ -69,7 +69,7 @@ const SUBKEY = [
|
||||
describe('encode', (): void => {
|
||||
it('encodes an address to a valid value', (): void => {
|
||||
expect(
|
||||
keyring.alice.address
|
||||
encodeAddress(ALICE_PUBLIC_ED)
|
||||
).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua');
|
||||
});
|
||||
|
||||
@@ -88,7 +88,7 @@ describe('encode', (): void => {
|
||||
it('fails when non-valid publicKey provided', (): void => {
|
||||
expect(
|
||||
(): string => encodeAddress(
|
||||
keyring.alice.publicKey.slice(0, 30)
|
||||
ALICE_PUBLIC_ED.slice(0, 30)
|
||||
)
|
||||
).toThrow(/Expected a valid key/);
|
||||
});
|
||||
@@ -141,7 +141,7 @@ describe('encode', (): void => {
|
||||
|
||||
it('encodes with 2 byte prefix', (): void => {
|
||||
expect(
|
||||
encodeAddress(keyring.alice.publicKey, 255)
|
||||
encodeAddress(ALICE_PUBLIC_ED, 255)
|
||||
).toEqual('yGHU8YKprxHbHdEv7oUK4rzMZXtsdhcXVG2CAMyC9WhzhjH2k');
|
||||
});
|
||||
|
||||
@@ -155,19 +155,19 @@ describe('encode', (): void => {
|
||||
|
||||
it('does not encode for > 16,383, < 0', (): void => {
|
||||
expect(
|
||||
() => encodeAddress(keyring.alice.publicKey, -1)
|
||||
() => encodeAddress(ALICE_PUBLIC_ED, -1)
|
||||
).toThrow(/range ss58Format specified/);
|
||||
expect(
|
||||
() => encodeAddress(keyring.alice.publicKey, 16384)
|
||||
() => encodeAddress(ALICE_PUBLIC_ED, 16384)
|
||||
).toThrow(/range ss58Format specified/);
|
||||
});
|
||||
|
||||
it('does not encode reserved', (): void => {
|
||||
expect(
|
||||
() => encodeAddress(keyring.alice.publicKey, 46)
|
||||
() => encodeAddress(ALICE_PUBLIC_ED, 46)
|
||||
).toThrow(/range ss58Format specified/);
|
||||
expect(
|
||||
() => encodeAddress(keyring.alice.publicKey, 47)
|
||||
() => encodeAddress(ALICE_PUBLIC_ED, 47)
|
||||
).toThrow(/range ss58Format specified/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { createTestPairs } from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import { ALICE_PUBLIC_SR } from './encode.spec';
|
||||
import { addressEq } from '.';
|
||||
|
||||
describe('addressEq', (): void => {
|
||||
@@ -34,12 +33,10 @@ describe('addressEq', (): void => {
|
||||
});
|
||||
|
||||
it('returns true for equal, address vs publicKey', (): void => {
|
||||
const keyring = createTestPairs({ type: 'sr25519' });
|
||||
|
||||
expect(
|
||||
addressEq(
|
||||
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
|
||||
keyring.alice.publicKey
|
||||
ALICE_PUBLIC_SR
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
@@ -5,9 +5,9 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import type { HashType } from '../secp256k1/types';
|
||||
import type { Prefix } from './types';
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
import { assert, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { secp256k1Hasher } from '../secp256k1/hasher';
|
||||
import { hasher } from '../secp256k1/hasher';
|
||||
import { encodeAddress } from './encode';
|
||||
|
||||
/**
|
||||
@@ -15,15 +15,9 @@ import { encodeAddress } from './encode';
|
||||
* @summary Converts an EVM address to its corresponding SS58 address.
|
||||
*/
|
||||
export function evmToAddress (evmAddress: HexString | string | Uint8Array, ss58Format?: Prefix, hashType: HashType = 'blake2'): string {
|
||||
const wrapError = (message: string) => `Converting ${evmAddress as string}: ${message}`;
|
||||
|
||||
const message = u8aConcat('evm:', evmAddress);
|
||||
|
||||
if (message.length !== 24) {
|
||||
throw new Error(wrapError('Invalid evm address length'));
|
||||
}
|
||||
assert(message.length === 24, () => `Converting ${evmAddress as string}: Invalid evm address length`);
|
||||
|
||||
const address = secp256k1Hasher(hashType, message);
|
||||
|
||||
return encodeAddress(address, ss58Format);
|
||||
return encodeAddress(hasher(hashType, message), ss58Format);
|
||||
}
|
||||
|
||||
@@ -5,20 +5,20 @@ import { utils } from 'micro-base';
|
||||
|
||||
import { createDecode, createEncode, createIs, createValidate } from './helpers';
|
||||
|
||||
const BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567';
|
||||
const chars = 'abcdefghijklmnopqrstuvwxyz234567';
|
||||
|
||||
const BASE32_CONFIG = {
|
||||
alphabet: BASE32_ALPHABET,
|
||||
const config = {
|
||||
chars,
|
||||
coder: utils.chain(
|
||||
// We define our own chain, the default base32 has padding
|
||||
utils.radix2(5),
|
||||
utils.alphabet(BASE32_ALPHABET),
|
||||
utils.alphabet(chars),
|
||||
{
|
||||
decode: (input: string) => input.split(''),
|
||||
encode: (input: string[]) => input.join('')
|
||||
}
|
||||
),
|
||||
ipfsChar: 'b',
|
||||
ipfs: 'b',
|
||||
type: 'base32'
|
||||
};
|
||||
|
||||
@@ -28,7 +28,7 @@ const BASE32_CONFIG = {
|
||||
* @description
|
||||
* Validates that the supplied value is valid base32, throwing exceptions if not
|
||||
*/
|
||||
export const base32Validate = createValidate(BASE32_CONFIG);
|
||||
export const base32Validate = createValidate(config);
|
||||
|
||||
/**
|
||||
* @name isBase32
|
||||
@@ -42,7 +42,7 @@ export const isBase32 = createIs(base32Validate);
|
||||
* @description
|
||||
* From the provided input, decode the base32 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export const base32Decode = createDecode(BASE32_CONFIG, base32Validate);
|
||||
export const base32Decode = createDecode(config, base32Validate);
|
||||
|
||||
/**
|
||||
* @name base32Encode
|
||||
@@ -50,4 +50,4 @@ export const base32Decode = createDecode(BASE32_CONFIG, base32Validate);
|
||||
* @description
|
||||
* From the provided input, create the base32 and return the result as a string.
|
||||
*/
|
||||
export const base32Encode = createEncode(BASE32_CONFIG);
|
||||
export const base32Encode = createEncode(config);
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { U8aLike } from '@polkadot/util/types';
|
||||
|
||||
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
|
||||
//
|
||||
// impor("../../../util/src/types").U8aLike
|
||||
//
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
type U8aLike = number[] | Buffer | Uint8Array | string | String;
|
||||
// re-export the type so *.d.ts files don't have ../src imports
|
||||
export type { U8aLike } from '@polkadot/util/types';
|
||||
|
||||
interface Coder {
|
||||
decode: (value: string) => Uint8Array;
|
||||
@@ -21,9 +14,9 @@ interface Coder {
|
||||
}
|
||||
|
||||
interface Config {
|
||||
alphabet: string;
|
||||
chars: string;
|
||||
coder: Coder;
|
||||
ipfsChar?: string;
|
||||
ipfs?: string;
|
||||
regex?: RegExp;
|
||||
type: string;
|
||||
}
|
||||
@@ -34,24 +27,24 @@ 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 {
|
||||
export function createDecode ({ coder, ipfs }: Config, validate: ValidateFn): DecodeFn {
|
||||
return (value: string, ipfsCompat?: boolean): Uint8Array => {
|
||||
validate(value, ipfsCompat);
|
||||
|
||||
return coder.decode(
|
||||
ipfsChar && ipfsCompat
|
||||
ipfs && ipfsCompat
|
||||
? value.substr(1)
|
||||
: value
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export function createEncode ({ coder, ipfsChar }: Config): EncodeFn {
|
||||
export function createEncode ({ coder, ipfs }: Config): EncodeFn {
|
||||
return (value: U8aLike, ipfsCompat?: boolean): string => {
|
||||
const out = coder.encode(u8aToU8a(value));
|
||||
|
||||
return ipfsChar && ipfsCompat
|
||||
? `${ipfsChar}${out}`
|
||||
return ipfs && ipfsCompat
|
||||
? `${ipfs}${out}`
|
||||
: out;
|
||||
};
|
||||
}
|
||||
@@ -66,20 +59,24 @@ export function createIs (validate: ValidateFn): ValidateFn {
|
||||
};
|
||||
}
|
||||
|
||||
export function createValidate ({ alphabet, ipfsChar, regex, type }: Config): ValidateFn {
|
||||
export function createValidate ({ chars, ipfs, 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 (ipfs && ipfsCompat) {
|
||||
assert(value[0] === ipfs, () => `Expected ipfs-compatible ${type} to start with '${ipfs}'`);
|
||||
}
|
||||
|
||||
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}`);
|
||||
}
|
||||
for (let i = (ipfsCompat ? 1 : 0); i < value.length; i++) {
|
||||
assert(
|
||||
chars.includes(value[i]) || (
|
||||
value[i] === '=' && (
|
||||
(i === value.length - 1) ||
|
||||
!chars.includes(value[i + 1])
|
||||
)
|
||||
),
|
||||
() => `Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -5,10 +5,10 @@ import { base58 } from 'micro-base';
|
||||
|
||||
import { createDecode, createEncode, createIs, createValidate } from '../base32/helpers';
|
||||
|
||||
const BASE58_CONFIG = {
|
||||
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
||||
const config = {
|
||||
chars: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
|
||||
coder: base58,
|
||||
ipfsChar: 'z',
|
||||
ipfs: 'z',
|
||||
type: 'base58'
|
||||
};
|
||||
|
||||
@@ -18,7 +18,7 @@ const BASE58_CONFIG = {
|
||||
* @description
|
||||
* Validates that the supplied value is valid base58, throwing exceptions if not
|
||||
*/
|
||||
export const base58Validate = createValidate(BASE58_CONFIG);
|
||||
export const base58Validate = createValidate(config);
|
||||
|
||||
/**
|
||||
* @name base58Decode
|
||||
@@ -26,7 +26,7 @@ export const base58Validate = createValidate(BASE58_CONFIG);
|
||||
* @description
|
||||
* From the provided input, decode the base58 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export const base58Decode = createDecode(BASE58_CONFIG, base58Validate);
|
||||
export const base58Decode = createDecode(config, base58Validate);
|
||||
|
||||
/**
|
||||
* @name base58Encode
|
||||
@@ -34,7 +34,7 @@ export const base58Decode = createDecode(BASE58_CONFIG, base58Validate);
|
||||
* @description
|
||||
* From the provided input, create the base58 and return the result as a string.
|
||||
*/
|
||||
export const base58Encode = createEncode(BASE58_CONFIG);
|
||||
export const base58Encode = createEncode(config);
|
||||
|
||||
/**
|
||||
* @name isBase58
|
||||
|
||||
@@ -5,10 +5,9 @@ import { base64 } from 'micro-base';
|
||||
|
||||
import { createDecode, createEncode, createIs, createValidate } from '../base32/helpers';
|
||||
|
||||
const BASE64_CONFIG = {
|
||||
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
||||
const config = {
|
||||
chars: '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'
|
||||
};
|
||||
|
||||
@@ -18,7 +17,7 @@ const BASE64_CONFIG = {
|
||||
* @description
|
||||
* Validates that the supplied value is valid base64
|
||||
*/
|
||||
export const base64Validate = createValidate(BASE64_CONFIG);
|
||||
export const base64Validate = createValidate(config);
|
||||
|
||||
/**
|
||||
* @name isBase64
|
||||
@@ -32,7 +31,7 @@ export const isBase64 = createIs(base64Validate);
|
||||
* @description
|
||||
* From the provided input, decode the base64 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export const base64Decode = createDecode(BASE64_CONFIG, base64Validate);
|
||||
export const base64Decode = createDecode(config, base64Validate);
|
||||
|
||||
/**
|
||||
* @name base64Encode
|
||||
@@ -40,4 +39,4 @@ export const base64Decode = createDecode(BASE64_CONFIG, base64Validate);
|
||||
* @description
|
||||
* From the provided input, create the base64 and return the result as a string.
|
||||
*/
|
||||
export const base64Encode = createEncode(BASE64_CONFIG);
|
||||
export const base64Encode = createEncode(config);
|
||||
|
||||
@@ -6,11 +6,27 @@ import { stringToU8a } from '@polkadot/util';
|
||||
import { base64Decode } from '.';
|
||||
|
||||
describe('base64Decode', (): void => {
|
||||
it('decodes a mixed base64 utf8 string', (): void => {
|
||||
it('decodes a mixed base64 utf8 string (1)', (): void => {
|
||||
expect(
|
||||
base64Decode('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
|
||||
).toEqual(
|
||||
stringToU8a('hello world Приветствую ми 你好')
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes a mixed base64 utf8 string (2)', (): void => {
|
||||
expect(
|
||||
base64Decode('4pyTIMOgIGxhIG1vZGU=')
|
||||
).toEqual(
|
||||
stringToU8a('✓ à la mode')
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes a mixed base64 utf8 string (3)', (): void => {
|
||||
expect(
|
||||
base64Decode('SGVsbG8gV29ybGQh')
|
||||
).toEqual(
|
||||
stringToU8a('Hello World!')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,6 @@ describe('base64Validate', (): void => {
|
||||
it('validates a mixed base64 utf8 string', (): void => {
|
||||
expect(
|
||||
() => base64Validate('aGVsbG8gd29ybGQg0J/RgNC40LLQtd^GC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
|
||||
).toThrow(/Invalid base64 encoding/);
|
||||
).toThrow(/Invalid base64 character "\^" \(0x5e\) at index 30/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { blake2AsU8a } from '.';
|
||||
|
||||
describe('blake2AsU8a', (): void => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const BN_BE_OPTS = { isLe: false };
|
||||
export const BN_LE_OPTS = { isLe: true };
|
||||
|
||||
export const BN_LE_16_OPTS = { bitLength: 16, isLe: true };
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '@polkadot/x-bigint/shim';
|
||||
|
||||
import { utils as utilsNobleSecp256k1 } from '@noble/secp256k1';
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { cryptoWaitReady } from './crypto';
|
||||
import { hmacSha256AsU8a } from './hmac';
|
||||
|
||||
// Set overrides on the secp256k1 utils
|
||||
// - hmacShaSync - This needs to be set, unset by default
|
||||
utilsNobleSecp256k1.hmacSha256Sync = (key: Uint8Array, ...messages: Uint8Array[]) =>
|
||||
hmacSha256AsU8a(key, u8aConcat(...messages));
|
||||
|
||||
// start init process immediately
|
||||
cryptoWaitReady().catch((): void => {
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
import type { Keypair } from '../../types';
|
||||
|
||||
import nacl from 'tweetnacl';
|
||||
import { randomAsU8a } from '../../random';
|
||||
import { ed25519PairFromSeed } from './fromSeed';
|
||||
|
||||
/**
|
||||
* @name ed25519PairFromRandom
|
||||
@@ -20,5 +21,5 @@ import nacl from 'tweetnacl';
|
||||
* ```
|
||||
*/
|
||||
export function ed25519PairFromRandom (): Keypair {
|
||||
return nacl.sign.keyPair();
|
||||
return ed25519PairFromSeed(randomAsU8a());
|
||||
}
|
||||
|
||||
@@ -25,8 +25,7 @@ import { ed25519PairFromSeed } from './fromSeed';
|
||||
export function ed25519PairFromString (value: string): Keypair {
|
||||
return ed25519PairFromSeed(
|
||||
blake2AsU8a(
|
||||
stringToU8a(value),
|
||||
256
|
||||
stringToU8a(value)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { ed25519PairFromSeed, ed25519Sign } from '.';
|
||||
|
||||
const PAIR = ed25519PairFromSeed(
|
||||
|
||||
@@ -3,7 +3,16 @@
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { hasBigInt, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
// re-export so TS *.d.ts generation is correct
|
||||
export type { HexString } from '@polkadot/util/types';
|
||||
|
||||
interface DualHash {
|
||||
256: (u8a: Uint8Array) => Uint8Array;
|
||||
512: (u8a: Uint8Array) => Uint8Array;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function createAsHex <T extends (...args: any[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
|
||||
@@ -15,3 +24,13 @@ export function createBitHasher <T> (bitLength: T, fn: (data: HexString | Buffer
|
||||
return (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||
fn(data, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
export function createDualHasher (wa: DualHash, js: DualHash): (value: HexString | Buffer | Uint8Array | string, bitLength?: 256 | 512, onlyJs?: boolean) => Uint8Array {
|
||||
return (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array => {
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? wa[bitLength](u8a)
|
||||
: js[bitLength](u8a);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { hmacShaAsU8a } from '.';
|
||||
|
||||
describe('hmacShaAsU8a', (): void => {
|
||||
|
||||
@@ -10,6 +10,16 @@ import { hmacSha256, hmacSha512, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
type BitLength = 256 | 512;
|
||||
|
||||
const JS_HASH = {
|
||||
256: sha256,
|
||||
512: sha512
|
||||
};
|
||||
|
||||
const WA_MHAC = {
|
||||
256: hmacSha256,
|
||||
512: hmacSha512
|
||||
};
|
||||
|
||||
function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array {
|
||||
return (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean): Uint8Array =>
|
||||
hmacShaAsU8a(key, data, bitLength, onlyJs);
|
||||
@@ -20,16 +30,11 @@ function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint
|
||||
* @description creates a Hmac Sha (256/512) Uint8Array from the key & data
|
||||
*/
|
||||
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8aKey = u8aToU8a(key);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? is256
|
||||
? hmacSha256(u8aKey, data)
|
||||
: hmacSha512(u8aKey, data)
|
||||
: is256
|
||||
? hmac(sha256, u8aKey, data)
|
||||
: hmac(sha512, u8aKey, data);
|
||||
? WA_MHAC[bitLength](u8aKey, data)
|
||||
: hmac(JS_HASH[bitLength], u8aKey, data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { keccakAsU8a } from '.';
|
||||
|
||||
describe('keccakAsU8a', (): void => {
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { keccak_256 as keccak256Js, keccak_512 as keccak512Js } from '@noble/hashes/lib/sha3';
|
||||
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, keccak256, keccak512 } from '@polkadot/wasm-crypto';
|
||||
import { keccak256, keccak512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { createAsHex, createBitHasher } from '../helpers';
|
||||
|
||||
type BitLength = 256 | 512;
|
||||
import { createAsHex, createBitHasher, createDualHasher } from '../helpers';
|
||||
|
||||
/**
|
||||
* @name keccakAsU8a
|
||||
@@ -26,18 +21,11 @@ type BitLength = 256 | 512;
|
||||
* keccakAsU8a('123'); // => Uint8Array
|
||||
* ```
|
||||
*/
|
||||
export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? is256
|
||||
? keccak256(u8a)
|
||||
: keccak512(u8a)
|
||||
: is256
|
||||
? keccak256Js(u8a)
|
||||
: keccak512Js(u8a);
|
||||
}
|
||||
export const keccakAsU8a = createDualHasher(
|
||||
{ 256: keccak256, 512: keccak512 },
|
||||
{ 256: keccak256Js, 512: keccak512Js }
|
||||
);
|
||||
|
||||
/**
|
||||
* @name keccak256AsU8a
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import DEFAULT_WORDLIST from './bip39-en';
|
||||
|
||||
describe('wordlist', (): void => {
|
||||
it('has the correct number of words', (): void => {
|
||||
expect(DEFAULT_WORDLIST).toHaveLength(2048);
|
||||
});
|
||||
|
||||
it('has no empty words', (): void => {
|
||||
expect(DEFAULT_WORDLIST.some((n) => !n.length || n.trim() !== n)).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -3,4 +3,4 @@
|
||||
|
||||
// Auto-generated by @polkadot/dev, do not edit
|
||||
|
||||
export const packageInfo = { name: '@polkadot/util-crypto', version: '8.0.1' };
|
||||
export const packageInfo = { name: '@polkadot/util-crypto', version: '8.2.2' };
|
||||
|
||||
@@ -1,268 +0,0 @@
|
||||
diff --git a/node_modules/@noble/hashes/lib/_sha2.js b/node_modules/@noble/hashes/lib/_sha2.js
|
||||
index 3a7e7d7..0517727 100644
|
||||
--- a/node_modules/@noble/hashes/lib/_sha2.js
|
||||
+++ b/node_modules/@noble/hashes/lib/_sha2.js
|
||||
@@ -2,12 +2,14 @@
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.SHA2 = void 0;
|
||||
const utils_1 = require("./utils");
|
||||
+const _32n = (0, utils_1._n)(32);
|
||||
+const _u32_max = (0, utils_1._n)(0xffffffff);
|
||||
// Polyfill for Safari 14
|
||||
function setBigUint64(view, byteOffset, value, isLE) {
|
||||
if (typeof view.setBigUint64 === 'function')
|
||||
return view.setBigUint64(byteOffset, value, isLE);
|
||||
- const wh = Number((value >> 32n) & 0xffffffffn);
|
||||
- const wl = Number(value & 0xffffffffn);
|
||||
+ const wh = Number((value >> _32n) & _u32_max);
|
||||
+ const wl = Number(value & _u32_max);
|
||||
const [h, l] = isLE ? [4, 0] : [0, 4];
|
||||
view.setUint32(byteOffset + h, wh, isLE);
|
||||
view.setUint32(byteOffset + l, wl, isLE);
|
||||
diff --git a/node_modules/@noble/hashes/lib/_u64.js b/node_modules/@noble/hashes/lib/_u64.js
|
||||
index 396889a..f958bd5 100644
|
||||
--- a/node_modules/@noble/hashes/lib/_u64.js
|
||||
+++ b/node_modules/@noble/hashes/lib/_u64.js
|
||||
@@ -1,11 +1,13 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.add5H = exports.add5L = exports.add4H = exports.add4L = exports.add3H = exports.add3L = exports.add = exports.rotlBL = exports.rotlBH = exports.rotlSL = exports.rotlSH = exports.rotr32L = exports.rotr32H = exports.rotrBL = exports.rotrBH = exports.rotrSL = exports.rotrSH = exports.shrSL = exports.shrSH = exports.toBig = exports.split = exports.fromBig = void 0;
|
||||
-const U32_MASK64 = 2n ** 32n - 1n;
|
||||
+const utils_1 = require("./utils");
|
||||
+const U32_MASK64 = (0, utils_1._n)(2 ** 32 - 1);
|
||||
+const _32n = (0, utils_1._n)(32);
|
||||
function fromBig(n, le = false) {
|
||||
if (le)
|
||||
- return { h: Number(n & U32_MASK64), l: Number((n >> 32n) & U32_MASK64) };
|
||||
- return { h: Number((n >> 32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
||||
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
||||
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
||||
}
|
||||
exports.fromBig = fromBig;
|
||||
function split(lst, le = false) {
|
||||
@@ -17,7 +19,7 @@ function split(lst, le = false) {
|
||||
return [Ah, Al];
|
||||
}
|
||||
exports.split = split;
|
||||
-const toBig = (h, l) => (BigInt(h >>> 0) << 32n) | BigInt(l >>> 0);
|
||||
+const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
|
||||
exports.toBig = toBig;
|
||||
// for Shift in [0, 32)
|
||||
const shrSH = (h, l, s) => h >>> s;
|
||||
diff --git a/node_modules/@noble/hashes/lib/blake3.d.ts b/node_modules/@noble/hashes/lib/blake3.d.ts
|
||||
index 80f8856..3ab02fc 100644
|
||||
--- a/node_modules/@noble/hashes/lib/blake3.d.ts
|
||||
+++ b/node_modules/@noble/hashes/lib/blake3.d.ts
|
||||
@@ -27,8 +27,8 @@ declare class BLAKE3 extends blake2.BLAKE2<BLAKE3> implements HashXOF<BLAKE3> {
|
||||
private b2CompressOut;
|
||||
protected finish(): void;
|
||||
private writeInto;
|
||||
- XOFInto(out: Uint8Array): Uint8Array;
|
||||
- XOF(bytes: number): Uint8Array;
|
||||
+ xofInto(out: Uint8Array): Uint8Array;
|
||||
+ xof(bytes: number): Uint8Array;
|
||||
digestInto(out: Uint8Array): Uint8Array;
|
||||
digest(): Uint8Array;
|
||||
}
|
||||
diff --git a/node_modules/@noble/hashes/lib/blake3.js b/node_modules/@noble/hashes/lib/blake3.js
|
||||
index bfc7a19..52b28f0 100644
|
||||
--- a/node_modules/@noble/hashes/lib/blake3.js
|
||||
+++ b/node_modules/@noble/hashes/lib/blake3.js
|
||||
@@ -229,14 +229,14 @@ class BLAKE3 extends blake2.BLAKE2 {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
- XOFInto(out) {
|
||||
+ xofInto(out) {
|
||||
if (!this.enableXOF)
|
||||
throw new Error('XOF impossible after digest call');
|
||||
return this.writeInto(out);
|
||||
}
|
||||
- XOF(bytes) {
|
||||
+ xof(bytes) {
|
||||
(0, utils_1.assertNumber)(bytes);
|
||||
- return this.XOFInto(new Uint8Array(bytes));
|
||||
+ return this.xofInto(new Uint8Array(bytes));
|
||||
}
|
||||
digestInto(out) {
|
||||
if (out.length < this.outputLen)
|
||||
diff --git a/node_modules/@noble/hashes/lib/sha3-addons.js b/node_modules/@noble/hashes/lib/sha3-addons.js
|
||||
index 51793e2..6a9eef7 100644
|
||||
--- a/node_modules/@noble/hashes/lib/sha3-addons.js
|
||||
+++ b/node_modules/@noble/hashes/lib/sha3-addons.js
|
||||
@@ -321,7 +321,7 @@ class KeccakPRG extends sha3_1.Keccak {
|
||||
throw new Error('KeccakPRG: digest is not allowed, please use .fetch instead.');
|
||||
}
|
||||
fetch(bytes) {
|
||||
- return this.XOF(bytes);
|
||||
+ return this.xof(bytes);
|
||||
}
|
||||
// Ensure irreversibility (even if state leaked previous outputs cannot be computed)
|
||||
forget() {
|
||||
diff --git a/node_modules/@noble/hashes/lib/sha3.d.ts b/node_modules/@noble/hashes/lib/sha3.d.ts
|
||||
index 6f0a062..1105727 100644
|
||||
--- a/node_modules/@noble/hashes/lib/sha3.d.ts
|
||||
+++ b/node_modules/@noble/hashes/lib/sha3.d.ts
|
||||
@@ -17,8 +17,8 @@ export declare class Keccak extends Hash<Keccak> implements HashXOF<Keccak> {
|
||||
update(data: Input): this;
|
||||
protected finish(): void;
|
||||
protected writeInto(out: Uint8Array): Uint8Array;
|
||||
- XOFInto(out: Uint8Array): Uint8Array;
|
||||
- XOF(bytes: number): Uint8Array;
|
||||
+ xofInto(out: Uint8Array): Uint8Array;
|
||||
+ xof(bytes: number): Uint8Array;
|
||||
digestInto(out: Uint8Array): Uint8Array;
|
||||
digest(): Uint8Array;
|
||||
destroy(): void;
|
||||
diff --git a/node_modules/@noble/hashes/lib/sha3.js b/node_modules/@noble/hashes/lib/sha3.js
|
||||
index 91adaf4..41a84b4 100644
|
||||
--- a/node_modules/@noble/hashes/lib/sha3.js
|
||||
+++ b/node_modules/@noble/hashes/lib/sha3.js
|
||||
@@ -22,20 +22,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.shake256 = exports.shake128 = exports.keccak_512 = exports.keccak_384 = exports.keccak_256 = exports.keccak_224 = exports.sha3_512 = exports.sha3_384 = exports.sha3_256 = exports.sha3_224 = exports.Keccak = exports.keccakP = void 0;
|
||||
const u64 = __importStar(require("./_u64"));
|
||||
const utils_1 = require("./utils");
|
||||
+const _0n = (0, utils_1._n)(0);
|
||||
+const _1n = (0, utils_1._n)(1);
|
||||
+const _2n = (0, utils_1._n)(2);
|
||||
+const _7n = (0, utils_1._n)(7);
|
||||
+const _256n = (0, utils_1._n)(256);
|
||||
+const _0x71n = (0, utils_1._n)(0x71);
|
||||
// Various per round constants calculations
|
||||
const [SHA3_PI, SHA3_ROTL, _SHA3_IOTA] = [[], [], []];
|
||||
-for (let round = 0, R = 1n, x = 1, y = 0; round < 24; round++) {
|
||||
+for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
||||
// Pi
|
||||
[x, y] = [y, (2 * x + 3 * y) % 5];
|
||||
SHA3_PI.push(2 * (5 * y + x));
|
||||
// Rotational
|
||||
SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
|
||||
// Iota
|
||||
- let t = 0n;
|
||||
+ let t = _0n;
|
||||
for (let j = 0; j < 7; j++) {
|
||||
- R = ((R << 1n) ^ ((R >> 7n) * 0x71n)) % 256n;
|
||||
- if (R & 2n)
|
||||
- t ^= 1n << ((1n << BigInt(j)) - 1n);
|
||||
+ R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
|
||||
+ if (R & _2n)
|
||||
+ t ^= _1n << ((_1n << BigInt(j)) - _1n);
|
||||
}
|
||||
_SHA3_IOTA.push(t);
|
||||
}
|
||||
@@ -161,15 +167,15 @@ class Keccak extends utils_1.Hash {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
- XOFInto(out) {
|
||||
+ xofInto(out) {
|
||||
// Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
|
||||
if (!this.enableXOF)
|
||||
throw new Error('XOF is not possible for this instance');
|
||||
return this.writeInto(out);
|
||||
}
|
||||
- XOF(bytes) {
|
||||
+ xof(bytes) {
|
||||
(0, utils_1.assertNumber)(bytes);
|
||||
- return this.XOFInto(new Uint8Array(bytes));
|
||||
+ return this.xofInto(new Uint8Array(bytes));
|
||||
}
|
||||
digestInto(out) {
|
||||
if (out.length < this.outputLen)
|
||||
diff --git a/node_modules/@noble/hashes/lib/sha512.js b/node_modules/@noble/hashes/lib/sha512.js
|
||||
index 567bd80..0a733ab 100644
|
||||
--- a/node_modules/@noble/hashes/lib/sha512.js
|
||||
+++ b/node_modules/@noble/hashes/lib/sha512.js
|
||||
@@ -26,26 +26,26 @@ const utils_1 = require("./utils");
|
||||
// Round contants (first 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409):
|
||||
// prettier-ignore
|
||||
const [SHA512_Kh, SHA512_Kl] = u64.split([
|
||||
- 0x428a2f98d728ae22n, 0x7137449123ef65cdn, 0xb5c0fbcfec4d3b2fn, 0xe9b5dba58189dbbcn,
|
||||
- 0x3956c25bf348b538n, 0x59f111f1b605d019n, 0x923f82a4af194f9bn, 0xab1c5ed5da6d8118n,
|
||||
- 0xd807aa98a3030242n, 0x12835b0145706fben, 0x243185be4ee4b28cn, 0x550c7dc3d5ffb4e2n,
|
||||
- 0x72be5d74f27b896fn, 0x80deb1fe3b1696b1n, 0x9bdc06a725c71235n, 0xc19bf174cf692694n,
|
||||
- 0xe49b69c19ef14ad2n, 0xefbe4786384f25e3n, 0x0fc19dc68b8cd5b5n, 0x240ca1cc77ac9c65n,
|
||||
- 0x2de92c6f592b0275n, 0x4a7484aa6ea6e483n, 0x5cb0a9dcbd41fbd4n, 0x76f988da831153b5n,
|
||||
- 0x983e5152ee66dfabn, 0xa831c66d2db43210n, 0xb00327c898fb213fn, 0xbf597fc7beef0ee4n,
|
||||
- 0xc6e00bf33da88fc2n, 0xd5a79147930aa725n, 0x06ca6351e003826fn, 0x142929670a0e6e70n,
|
||||
- 0x27b70a8546d22ffcn, 0x2e1b21385c26c926n, 0x4d2c6dfc5ac42aedn, 0x53380d139d95b3dfn,
|
||||
- 0x650a73548baf63den, 0x766a0abb3c77b2a8n, 0x81c2c92e47edaee6n, 0x92722c851482353bn,
|
||||
- 0xa2bfe8a14cf10364n, 0xa81a664bbc423001n, 0xc24b8b70d0f89791n, 0xc76c51a30654be30n,
|
||||
- 0xd192e819d6ef5218n, 0xd69906245565a910n, 0xf40e35855771202an, 0x106aa07032bbd1b8n,
|
||||
- 0x19a4c116b8d2d0c8n, 0x1e376c085141ab53n, 0x2748774cdf8eeb99n, 0x34b0bcb5e19b48a8n,
|
||||
- 0x391c0cb3c5c95a63n, 0x4ed8aa4ae3418acbn, 0x5b9cca4f7763e373n, 0x682e6ff3d6b2b8a3n,
|
||||
- 0x748f82ee5defb2fcn, 0x78a5636f43172f60n, 0x84c87814a1f0ab72n, 0x8cc702081a6439ecn,
|
||||
- 0x90befffa23631e28n, 0xa4506cebde82bde9n, 0xbef9a3f7b2c67915n, 0xc67178f2e372532bn,
|
||||
- 0xca273eceea26619cn, 0xd186b8c721c0c207n, 0xeada7dd6cde0eb1en, 0xf57d4f7fee6ed178n,
|
||||
- 0x06f067aa72176fban, 0x0a637dc5a2c898a6n, 0x113f9804bef90daen, 0x1b710b35131c471bn,
|
||||
- 0x28db77f523047d84n, 0x32caab7b40c72493n, 0x3c9ebe0a15c9bebcn, 0x431d67c49c100d4cn,
|
||||
- 0x4cc5d4becb3e42b6n, 0x597f299cfc657e2an, 0x5fcb6fab3ad6faecn, 0x6c44198c4a475817n
|
||||
+ (0, utils_1._n)('0x428a2f98d728ae22'), (0, utils_1._n)('0x7137449123ef65cd'), (0, utils_1._n)('0xb5c0fbcfec4d3b2f'), (0, utils_1._n)('0xe9b5dba58189dbbc'),
|
||||
+ (0, utils_1._n)('0x3956c25bf348b538'), (0, utils_1._n)('0x59f111f1b605d019'), (0, utils_1._n)('0x923f82a4af194f9b'), (0, utils_1._n)('0xab1c5ed5da6d8118'),
|
||||
+ (0, utils_1._n)('0xd807aa98a3030242'), (0, utils_1._n)('0x12835b0145706fbe'), (0, utils_1._n)('0x243185be4ee4b28c'), (0, utils_1._n)('0x550c7dc3d5ffb4e2'),
|
||||
+ (0, utils_1._n)('0x72be5d74f27b896f'), (0, utils_1._n)('0x80deb1fe3b1696b1'), (0, utils_1._n)('0x9bdc06a725c71235'), (0, utils_1._n)('0xc19bf174cf692694'),
|
||||
+ (0, utils_1._n)('0xe49b69c19ef14ad2'), (0, utils_1._n)('0xefbe4786384f25e3'), (0, utils_1._n)('0x0fc19dc68b8cd5b5'), (0, utils_1._n)('0x240ca1cc77ac9c65'),
|
||||
+ (0, utils_1._n)('0x2de92c6f592b0275'), (0, utils_1._n)('0x4a7484aa6ea6e483'), (0, utils_1._n)('0x5cb0a9dcbd41fbd4'), (0, utils_1._n)('0x76f988da831153b5'),
|
||||
+ (0, utils_1._n)('0x983e5152ee66dfab'), (0, utils_1._n)('0xa831c66d2db43210'), (0, utils_1._n)('0xb00327c898fb213f'), (0, utils_1._n)('0xbf597fc7beef0ee4'),
|
||||
+ (0, utils_1._n)('0xc6e00bf33da88fc2'), (0, utils_1._n)('0xd5a79147930aa725'), (0, utils_1._n)('0x06ca6351e003826f'), (0, utils_1._n)('0x142929670a0e6e70'),
|
||||
+ (0, utils_1._n)('0x27b70a8546d22ffc'), (0, utils_1._n)('0x2e1b21385c26c926'), (0, utils_1._n)('0x4d2c6dfc5ac42aed'), (0, utils_1._n)('0x53380d139d95b3df'),
|
||||
+ (0, utils_1._n)('0x650a73548baf63de'), (0, utils_1._n)('0x766a0abb3c77b2a8'), (0, utils_1._n)('0x81c2c92e47edaee6'), (0, utils_1._n)('0x92722c851482353b'),
|
||||
+ (0, utils_1._n)('0xa2bfe8a14cf10364'), (0, utils_1._n)('0xa81a664bbc423001'), (0, utils_1._n)('0xc24b8b70d0f89791'), (0, utils_1._n)('0xc76c51a30654be30'),
|
||||
+ (0, utils_1._n)('0xd192e819d6ef5218'), (0, utils_1._n)('0xd69906245565a910'), (0, utils_1._n)('0xf40e35855771202a'), (0, utils_1._n)('0x106aa07032bbd1b8'),
|
||||
+ (0, utils_1._n)('0x19a4c116b8d2d0c8'), (0, utils_1._n)('0x1e376c085141ab53'), (0, utils_1._n)('0x2748774cdf8eeb99'), (0, utils_1._n)('0x34b0bcb5e19b48a8'),
|
||||
+ (0, utils_1._n)('0x391c0cb3c5c95a63'), (0, utils_1._n)('0x4ed8aa4ae3418acb'), (0, utils_1._n)('0x5b9cca4f7763e373'), (0, utils_1._n)('0x682e6ff3d6b2b8a3'),
|
||||
+ (0, utils_1._n)('0x748f82ee5defb2fc'), (0, utils_1._n)('0x78a5636f43172f60'), (0, utils_1._n)('0x84c87814a1f0ab72'), (0, utils_1._n)('0x8cc702081a6439ec'),
|
||||
+ (0, utils_1._n)('0x90befffa23631e28'), (0, utils_1._n)('0xa4506cebde82bde9'), (0, utils_1._n)('0xbef9a3f7b2c67915'), (0, utils_1._n)('0xc67178f2e372532b'),
|
||||
+ (0, utils_1._n)('0xca273eceea26619c'), (0, utils_1._n)('0xd186b8c721c0c207'), (0, utils_1._n)('0xeada7dd6cde0eb1e'), (0, utils_1._n)('0xf57d4f7fee6ed178'),
|
||||
+ (0, utils_1._n)('0x06f067aa72176fba'), (0, utils_1._n)('0x0a637dc5a2c898a6'), (0, utils_1._n)('0x113f9804bef90dae'), (0, utils_1._n)('0x1b710b35131c471b'),
|
||||
+ (0, utils_1._n)('0x28db77f523047d84'), (0, utils_1._n)('0x32caab7b40c72493'), (0, utils_1._n)('0x3c9ebe0a15c9bebc'), (0, utils_1._n)('0x431d67c49c100d4c'),
|
||||
+ (0, utils_1._n)('0x4cc5d4becb3e42b6'), (0, utils_1._n)('0x597f299cfc657e2a'), (0, utils_1._n)('0x5fcb6fab3ad6faec'), (0, utils_1._n)('0x6c44198c4a475817')
|
||||
]);
|
||||
// Temporary buffer, not used to store anything between runs
|
||||
const SHA512_W_H = new Uint32Array(80);
|
||||
diff --git a/node_modules/@noble/hashes/lib/utils.d.ts b/node_modules/@noble/hashes/lib/utils.d.ts
|
||||
index 8233e80..67c4246 100644
|
||||
--- a/node_modules/@noble/hashes/lib/utils.d.ts
|
||||
+++ b/node_modules/@noble/hashes/lib/utils.d.ts
|
||||
@@ -1,5 +1,6 @@
|
||||
/*! noble-hashes - MIT License (c) 2021 Paul Miller (paulmillr.com) */
|
||||
export declare type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array | Uint16Array | Int16Array | Uint32Array | Int32Array;
|
||||
+export declare const _n: (value: string | number | bigint | boolean) => bigint;
|
||||
export declare const u8: (arr: TypedArray) => Uint8Array;
|
||||
export declare const u32: (arr: TypedArray) => Uint32Array;
|
||||
export declare const createView: (arr: TypedArray) => DataView;
|
||||
@@ -24,8 +25,8 @@ export declare abstract class Hash<T extends Hash<T>> {
|
||||
clone(): T;
|
||||
}
|
||||
export declare type HashXOF<T extends Hash<T>> = Hash<T> & {
|
||||
- XOF(bytes: number): Uint8Array;
|
||||
- XOFInto(buf: Uint8Array): Uint8Array;
|
||||
+ xof(bytes: number): Uint8Array;
|
||||
+ xofInto(buf: Uint8Array): Uint8Array;
|
||||
};
|
||||
declare type EmptyObj = {};
|
||||
export declare function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(def: T1, _opts?: T2): T1 & T2;
|
||||
@@ -46,7 +47,7 @@ export declare function wrapConstructorWithOpts<H extends Hash<H>, T extends Obj
|
||||
};
|
||||
export declare const crypto: {
|
||||
node?: any;
|
||||
- web?: Crypto;
|
||||
+ web?: any;
|
||||
};
|
||||
export declare function randomBytes(bytesLength?: number): Uint8Array;
|
||||
export {};
|
||||
diff --git a/node_modules/@noble/hashes/lib/utils.js b/node_modules/@noble/hashes/lib/utils.js
|
||||
index 1d45928..3811cf9 100644
|
||||
--- a/node_modules/@noble/hashes/lib/utils.js
|
||||
+++ b/node_modules/@noble/hashes/lib/utils.js
|
||||
@@ -1,7 +1,9 @@
|
||||
"use strict";
|
||||
/*! noble-hashes - MIT License (c) 2021 Paul Miller (paulmillr.com) */
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
-exports.randomBytes = exports.crypto = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.checkOpts = exports.Hash = exports.assertHash = exports.assertBool = exports.assertNumber = exports.toBytes = exports.asyncLoop = exports.nextTick = exports.bytesToHex = exports.isLE = exports.rotr = exports.createView = exports.u32 = exports.u8 = void 0;
|
||||
+exports.randomBytes = exports.crypto = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.checkOpts = exports.Hash = exports.assertHash = exports.assertBool = exports.assertNumber = exports.toBytes = exports.asyncLoop = exports.nextTick = exports.bytesToHex = exports.isLE = exports.rotr = exports.createView = exports.u32 = exports.u8 = exports._n = void 0;
|
||||
+// helper to protect against older bundlers
|
||||
+exports._n = typeof BigInt !== 'undefined' ? BigInt : () => Number.NaN;
|
||||
// Cast array to different type
|
||||
const u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
||||
exports.u8 = u8;
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { _n } from '@noble/hashes/lib/utils';
|
||||
|
||||
describe('@noble/hashes', (): void => {
|
||||
it('is patched', (): void => {
|
||||
expect(
|
||||
_n(123) === 123n
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -4,8 +4,8 @@
|
||||
import { u8aEq, u8aToHex } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { pbkdf2Encode } from '.';
|
||||
|
||||
const KNOWN_SALT = new Uint8Array([
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { DEFAULT_PARAMS } from './defaults';
|
||||
import { scryptEncode } from '.';
|
||||
|
||||
|
||||
@@ -2,17 +2,43 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { secp256k1Compress } from './';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { secp256k1Compress } from '.';
|
||||
|
||||
describe('secp256k1Compress', (): void => {
|
||||
it('compresses a known key', (): void => {
|
||||
expect(
|
||||
secp256k1Compress(
|
||||
hexToU8a('0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
)
|
||||
).toEqual(
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
);
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('returns a compressed key as-is', (): void => {
|
||||
expect(
|
||||
secp256k1Compress(
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077'),
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
);
|
||||
});
|
||||
|
||||
it('compresses a known key', (): void => {
|
||||
expect(
|
||||
secp256k1Compress(
|
||||
hexToU8a('0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af'),
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
performanceWasm('secp256k1Compress', 100000, (input, onlyJs) => secp256k1Compress(input, onlyJs),
|
||||
[[
|
||||
hexToU8a('0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
]]
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
import { Point } from '@noble/secp256k1';
|
||||
|
||||
import { secp256k1 } from './secp256k1';
|
||||
import { assert, hasBigInt } from '@polkadot/util';
|
||||
import { isReady, secp256k1Compress as wasm } from '@polkadot/wasm-crypto';
|
||||
|
||||
export function secp256k1Compress (publicKey: Uint8Array): Uint8Array {
|
||||
assert([33, 65].includes(publicKey.length), 'Invalid publicKey provided');
|
||||
export function secp256k1Compress (publicKey: Uint8Array, onlyJs?: boolean): Uint8Array {
|
||||
if (publicKey.length === 33) {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
return new Uint8Array(
|
||||
secp256k1
|
||||
.keyFromPublic(publicKey)
|
||||
.getPublic()
|
||||
.encodeCompressed()
|
||||
);
|
||||
assert(publicKey.length === 65, 'Invalid publicKey provided');
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? wasm(publicKey)
|
||||
: Point.fromHex(publicKey).toRawBytes(true);
|
||||
}
|
||||
|
||||
@@ -2,27 +2,43 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { secp256k1Expand } from './';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { secp256k1Expand } from '.';
|
||||
|
||||
describe('secp256k1Expand', (): void => {
|
||||
it('expands a known key', (): void => {
|
||||
expect(
|
||||
secp256k1Expand(
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
)
|
||||
).toEqual(
|
||||
hexToU8a('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
);
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('expands a known full key', (): void => {
|
||||
expect(
|
||||
secp256k1Expand(
|
||||
hexToU8a('0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
)
|
||||
).toEqual(
|
||||
hexToU8a('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
);
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('expands a known key', (): void => {
|
||||
expect(
|
||||
secp256k1Expand(
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077'),
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(
|
||||
hexToU8a('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
);
|
||||
});
|
||||
|
||||
it('expands a known full key', (): void => {
|
||||
expect(
|
||||
secp256k1Expand(
|
||||
hexToU8a('0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af'),
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(
|
||||
hexToU8a('0xb9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
performanceWasm('secp256k1Expand', 2000, (input, onlyJs) => secp256k1Expand(input, onlyJs),
|
||||
[[
|
||||
hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077')
|
||||
]]
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,20 +1,28 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert, bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
import { Point } from '@noble/secp256k1';
|
||||
|
||||
import { assert, bnToU8a, hasBigInt, u8aConcat } from '@polkadot/util';
|
||||
import { isReady, secp256k1Expand as wasm } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { BN_BE_256_OPTS } from '../bn';
|
||||
import { secp256k1 } from './secp256k1';
|
||||
|
||||
export function secp256k1Expand (publicKey: Uint8Array): Uint8Array {
|
||||
assert([33, 65].includes(publicKey.length), 'Invalid publicKey provided');
|
||||
export function secp256k1Expand (publicKey: Uint8Array, onlyJs?: boolean): Uint8Array {
|
||||
if (publicKey.length === 65) {
|
||||
return publicKey.subarray(1);
|
||||
}
|
||||
|
||||
const expanded = secp256k1
|
||||
.keyFromPublic(publicKey)
|
||||
.getPublic();
|
||||
assert(publicKey.length === 33, 'Invalid publicKey provided');
|
||||
|
||||
if (!hasBigInt || (!onlyJs && isReady())) {
|
||||
return wasm(publicKey).subarray(1);
|
||||
}
|
||||
|
||||
const { x, y } = Point.fromHex(publicKey);
|
||||
|
||||
return u8aConcat(
|
||||
bnToU8a(expanded.getX(), BN_BE_256_OPTS),
|
||||
bnToU8a(expanded.getY(), BN_BE_256_OPTS)
|
||||
bnToU8a(x, BN_BE_256_OPTS),
|
||||
bnToU8a(y, BN_BE_256_OPTS)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { secp256k1Hasher } from './hasher';
|
||||
|
||||
describe('secp256k1Hasher', (): void => {
|
||||
it('fails with unknown hasher', (): void => {
|
||||
expect(
|
||||
() => secp256k1Hasher('unknown' as 'blake2', 'testing')
|
||||
).toThrow(/Unsupported secp256k1 hasher 'unknown', expected one of blake2, keccak/);
|
||||
});
|
||||
import { hasher } from './hasher';
|
||||
|
||||
describe('hasher', (): void => {
|
||||
it('creates a blake2 hash', (): void => {
|
||||
expect(
|
||||
secp256k1Hasher('blake2', 'abc')
|
||||
hasher('blake2', 'abc')
|
||||
).toEqual(
|
||||
new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25])
|
||||
);
|
||||
@@ -20,7 +14,7 @@ describe('secp256k1Hasher', (): void => {
|
||||
|
||||
it('creates a keccak hash', (): void => {
|
||||
expect(
|
||||
secp256k1Hasher('keccak', 'abc')
|
||||
hasher('keccak', 'abc')
|
||||
).toEqual(
|
||||
new Uint8Array([78, 3, 101, 122, 234, 69, 169, 79, 199, 212, 123, 168, 38, 200, 214, 103, 192, 209, 230, 227, 58, 100, 160, 54, 236, 68, 245, 143, 161, 45, 108, 69])
|
||||
);
|
||||
|
||||
@@ -6,14 +6,8 @@ import type { HashType } from './types';
|
||||
import { blake2AsU8a } from '../blake2';
|
||||
import { keccakAsU8a } from '../keccak';
|
||||
|
||||
const HASH_TYPES = ['blake2', 'keccak'];
|
||||
|
||||
export function secp256k1Hasher (hashType: HashType, data: Uint8Array | string): Uint8Array {
|
||||
if (hashType === 'blake2') {
|
||||
return blake2AsU8a(data);
|
||||
} else if (hashType === 'keccak') {
|
||||
return keccakAsU8a(data);
|
||||
}
|
||||
|
||||
throw new Error(`Unsupported secp256k1 hasher '${hashType as string}', expected one of ${HASH_TYPES.join(', ')}`);
|
||||
export function hasher (hashType: HashType, data: Uint8Array | string, onlyJs?: boolean): Uint8Array {
|
||||
return hashType === 'keccak'
|
||||
? keccakAsU8a(data, undefined, onlyJs)
|
||||
: blake2AsU8a(data, undefined, undefined, onlyJs);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a, u8aToHex } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { mnemonicToMiniSecret } from '../../mnemonic';
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { secp256k1PairFromSeed } from '..';
|
||||
|
||||
// mnemonic, secret, public, account_id
|
||||
@@ -37,23 +39,33 @@ const tests: Test[] = [
|
||||
];
|
||||
|
||||
describe('secp256k1PairFromSeed', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
const TEST = hexToU8a('0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae');
|
||||
const RESULT = {
|
||||
publicKey: hexToU8a('0x03fd8c74f795ced92064b86191cb2772b1e3a0947740aa0a5a6e379592471fd85b'),
|
||||
secretKey: hexToU8a('0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae')
|
||||
};
|
||||
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
|
||||
expect(secp256k1PairFromSeed(TEST)).toEqual(RESULT);
|
||||
});
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
|
||||
expect(secp256k1PairFromSeed(TEST, onlyJs)).toEqual(RESULT);
|
||||
});
|
||||
|
||||
tests.forEach(([mnemonic, secretKey, publicKey], index): void => {
|
||||
it(`creates valid against known (${index})`, (): void => {
|
||||
const seed = mnemonicToMiniSecret(mnemonic);
|
||||
const pair = secp256k1PairFromSeed(seed);
|
||||
tests.forEach(([mnemonic, secretKey, publicKey], index): void => {
|
||||
it(`creates valid against known (${index})`, (): void => {
|
||||
const seed = mnemonicToMiniSecret(mnemonic);
|
||||
const pair = secp256k1PairFromSeed(seed, onlyJs);
|
||||
|
||||
expect(u8aToHex(pair.secretKey)).toEqual(secretKey);
|
||||
expect(u8aToHex(pair.publicKey)).toEqual(publicKey);
|
||||
expect(u8aToHex(pair.secretKey)).toEqual(secretKey);
|
||||
expect(u8aToHex(pair.publicKey)).toEqual(publicKey);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
performanceWasm('secp256k1PairFromSeed', 500, (input, onlyJs) =>
|
||||
secp256k1PairFromSeed(input, onlyJs)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -3,22 +3,29 @@
|
||||
|
||||
import type { Keypair } from '../../types';
|
||||
|
||||
import { assert, bnToU8a } from '@polkadot/util';
|
||||
import { getPublicKey } from '@noble/secp256k1';
|
||||
|
||||
import { BN_BE_256_OPTS } from '../../bn';
|
||||
import { secp256k1 } from '../secp256k1';
|
||||
import { assert, hasBigInt } from '@polkadot/util';
|
||||
import { isReady, secp256k1FromSeed } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name secp256k1PairFromSeed
|
||||
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
*/
|
||||
export function secp256k1PairFromSeed (seed: Uint8Array): Keypair {
|
||||
export function secp256k1PairFromSeed (seed: Uint8Array, onlyJs?: boolean): Keypair {
|
||||
assert(seed.length === 32, 'Expected valid 32-byte private key as a seed');
|
||||
|
||||
const key = secp256k1.keyFromPrivate(seed);
|
||||
if (!hasBigInt || (!onlyJs && isReady())) {
|
||||
const full = secp256k1FromSeed(seed);
|
||||
|
||||
return {
|
||||
publicKey: full.slice(32),
|
||||
secretKey: full.slice(0, 32)
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
publicKey: new Uint8Array(key.getPublic().encodeCompressed()),
|
||||
secretKey: bnToU8a(key.getPrivate(), BN_BE_256_OPTS)
|
||||
publicKey: getPublicKey(seed, true),
|
||||
secretKey: seed
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,17 +2,30 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { keccakAsU8a } from '../keccak';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { secp256k1Recover } from '.';
|
||||
|
||||
describe('secp256k1Recover', (): void => {
|
||||
it('recovers a publicKey', (): void => {
|
||||
const pubKey = '0x93a9fc7154c6da3c826415df01eb0e37fb4da4b0';
|
||||
const sig = '0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff';
|
||||
const msg = '0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9';
|
||||
const res = keccakAsU8a(secp256k1Recover(u8aToU8a(msg), u8aToU8a(sig), 0));
|
||||
const sig = u8aToU8a('0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff');
|
||||
const msg = u8aToU8a('0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9');
|
||||
|
||||
expect(u8aToHex(res.subarray(12))).toEqual(pubKey);
|
||||
describe('secp256k1Recover', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('recovers a publicKey', (): void => {
|
||||
const pubKey = '0x93a9fc7154c6da3c826415df01eb0e37fb4da4b0';
|
||||
const res = keccakAsU8a(secp256k1Recover(msg, sig, 0, undefined, onlyJs));
|
||||
|
||||
expect(u8aToHex(res.subarray(-20))).toEqual(pubKey);
|
||||
});
|
||||
});
|
||||
|
||||
performanceWasm('secp256k1Recover', 200, (_, onlyJs) =>
|
||||
secp256k1Recover(msg, sig, 0, undefined, onlyJs)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,17 +1,31 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { secp256k1 } from './secp256k1';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { HashType } from './types';
|
||||
|
||||
import { recoverPublicKey, Signature } from '@noble/secp256k1';
|
||||
|
||||
import { assert, hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, secp256k1Recover as wasm } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { secp256k1Compress } from './compress';
|
||||
import { secp256k1Expand } from './expand';
|
||||
|
||||
/**
|
||||
* @name secp256k1Recover
|
||||
* @description Recovers a publicKey from the supplied signature
|
||||
*/
|
||||
export function secp256k1Recover (message: Uint8Array, signature: Uint8Array, recovery: number): Uint8Array {
|
||||
return new Uint8Array(
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument
|
||||
secp256k1
|
||||
.recoverPubKey(message, { r: signature.slice(0, 32), s: signature.slice(32, 64) }, recovery)
|
||||
.encode(null, true)
|
||||
);
|
||||
export function secp256k1Recover (msgHash: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, recovery: number, hashType: HashType = 'blake2', onlyJs?: boolean): Uint8Array {
|
||||
const sig = u8aToU8a(signature).subarray(0, 64);
|
||||
const msg = u8aToU8a(msgHash);
|
||||
const publicKey = !hasBigInt || (!onlyJs && isReady())
|
||||
? wasm(msg, sig, recovery)
|
||||
: recoverPublicKey(msg, Signature.fromCompact(sig).toRawBytes(), recovery);
|
||||
|
||||
assert(publicKey, 'Unable to recover publicKey from signature');
|
||||
|
||||
return hashType === 'keccak'
|
||||
? secp256k1Expand(publicKey, onlyJs)
|
||||
: secp256k1Compress(publicKey, onlyJs);
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import elliptic from 'elliptic';
|
||||
|
||||
// eslint-disable-next-line new-cap
|
||||
export const secp256k1 = new elliptic.ec('secp256k1');
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { secp256k1PairFromSeed, secp256k1Sign } from '.';
|
||||
|
||||
const pair = secp256k1PairFromSeed(hexToU8a('0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae'));
|
||||
const msg = hexToU8a('0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9');
|
||||
|
||||
describe('sign', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('generates a known signature', (): void => {
|
||||
expect(
|
||||
secp256k1Sign(msg, pair, undefined, onlyJs)
|
||||
).toEqual(hexToU8a(
|
||||
// from elliptic, this is - 0xdf92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba8c4efa521361678d9b8582744d77aa4b8d886d7380b7808a683174afad9c4700300
|
||||
// libsecp256k1 & @noble/hashes do agree here...
|
||||
'0xdf92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba83b105adec9e9872647a7d8bb28855b45e22805aea3d097953cbb1391f671d13e01'
|
||||
));
|
||||
});
|
||||
});
|
||||
|
||||
// since the libsecp256k1 signatures don't match (but can be verified), we
|
||||
// do both signing and verification here (checking the extracted key)
|
||||
performanceWasm('secp256k1Sign', 1000, (_, onlyJs) =>
|
||||
secp256k1Sign(msg, pair, undefined, onlyJs)
|
||||
);
|
||||
});
|
||||
@@ -4,25 +4,33 @@
|
||||
import type { Keypair } from '../types';
|
||||
import type { HashType } from './types';
|
||||
|
||||
import { assert, bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
import { Signature, signSync } from '@noble/secp256k1';
|
||||
|
||||
import { assert, bnToU8a, hasBigInt, u8aConcat } from '@polkadot/util';
|
||||
import { isReady, secp256k1Sign as wasm } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { BN_BE_256_OPTS } from '../bn';
|
||||
import { secp256k1Hasher } from './hasher';
|
||||
import { secp256k1 } from './secp256k1';
|
||||
import { hasher } from './hasher';
|
||||
|
||||
/**
|
||||
* @name secp256k1Sign
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function secp256k1Sign (message: Uint8Array | string, { secretKey }: Partial<Keypair>, hashType: HashType = 'blake2'): Uint8Array {
|
||||
export function secp256k1Sign (message: Uint8Array | string, { secretKey }: Partial<Keypair>, hashType: HashType = 'blake2', onlyJs?: boolean): Uint8Array {
|
||||
assert(secretKey?.length === 32, 'Expected valid secp256k1 secretKey, 32-bytes');
|
||||
|
||||
const key = secp256k1.keyFromPrivate(secretKey);
|
||||
const ecsig = key.sign(secp256k1Hasher(hashType, message));
|
||||
const data = hasher(hashType, message, onlyJs);
|
||||
|
||||
if (!hasBigInt || (!onlyJs && isReady())) {
|
||||
return wasm(data, secretKey);
|
||||
}
|
||||
|
||||
const [sigBytes, recoveryParam] = signSync(data, secretKey, { canonical: true, recovered: true });
|
||||
const { r, s } = Signature.fromHex(sigBytes);
|
||||
|
||||
return u8aConcat(
|
||||
bnToU8a(ecsig.r, BN_BE_256_OPTS),
|
||||
bnToU8a(ecsig.s, BN_BE_256_OPTS),
|
||||
new Uint8Array([ecsig.recoveryParam || 0])
|
||||
bnToU8a(r, BN_BE_256_OPTS),
|
||||
bnToU8a(s, BN_BE_256_OPTS),
|
||||
new Uint8Array([recoveryParam || 0])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../cryptoInit';
|
||||
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { secp256k1Hasher } from './hasher';
|
||||
import { hasher } from './hasher';
|
||||
import { secp256k1Expand, secp256k1PairFromSeed, secp256k1Sign, secp256k1Verify } from '.';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
@@ -15,7 +17,7 @@ describe('sign and verify', (): void => {
|
||||
const sig = '0x92fcacf0946bbd10b31dfe16d567ed1d3014e81007dd9e5256e19c0f07eacc1643b151ca29e449a765e16a7ce59b88d800467d6b3412d30ea8ad22307a59664b00';
|
||||
const msg = stringToU8a('secp256k1');
|
||||
|
||||
expect(secp256k1Verify(msg, sig, address)).toBe(true);
|
||||
expect(secp256k1Verify(msg, sig, address)).toEqual(true);
|
||||
});
|
||||
|
||||
it('has 65-byte signatures', (): void => {
|
||||
@@ -27,25 +29,25 @@ describe('sign and verify', (): void => {
|
||||
it('signs/verifies a message by random key (blake2)', (): void => {
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
const signature = secp256k1Sign(MESSAGE, pair);
|
||||
const address = secp256k1Hasher('blake2', pair.publicKey);
|
||||
const address = hasher('blake2', pair.publicKey);
|
||||
|
||||
expect(secp256k1Verify(MESSAGE, signature, address)).toBe(true);
|
||||
expect(secp256k1Verify(MESSAGE, signature, address)).toEqual(true);
|
||||
});
|
||||
|
||||
it('signs/verifies a message by random key (keccak)', (): void => {
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
const signature = secp256k1Sign(MESSAGE, pair, 'keccak');
|
||||
const address = secp256k1Hasher('keccak', secp256k1Expand(pair.publicKey));
|
||||
const address = hasher('keccak', secp256k1Expand(pair.publicKey));
|
||||
|
||||
expect(secp256k1Verify(MESSAGE, signature, address, 'keccak')).toBe(true);
|
||||
expect(secp256k1Verify(MESSAGE, signature, address, 'keccak')).toEqual(true);
|
||||
});
|
||||
|
||||
it('fails verification on hasher mismatches', (): void => {
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
const signature = secp256k1Sign(MESSAGE, pair, 'keccak');
|
||||
const address = secp256k1Hasher('keccak', secp256k1Expand(pair.publicKey));
|
||||
const address = hasher('keccak', secp256k1Expand(pair.publicKey));
|
||||
|
||||
expect(secp256k1Verify(MESSAGE, signature, address, 'blake2')).toBe(false);
|
||||
expect(secp256k1Verify(MESSAGE, signature, address, 'blake2')).toEqual(false);
|
||||
});
|
||||
|
||||
it('works over a range of random keys (blake2)', (): void => {
|
||||
@@ -57,10 +59,10 @@ describe('sign and verify', (): void => {
|
||||
secp256k1Verify(
|
||||
MESSAGE,
|
||||
secp256k1Sign(MESSAGE, pair, 'blake2'),
|
||||
secp256k1Hasher('blake2', pair.publicKey),
|
||||
hasher('blake2', pair.publicKey),
|
||||
'blake2'
|
||||
)
|
||||
).toBe(true);
|
||||
).toEqual(true);
|
||||
} catch (error) {
|
||||
console.error(`blake2 failed on #${i}`);
|
||||
throw error;
|
||||
@@ -77,10 +79,10 @@ describe('sign and verify', (): void => {
|
||||
secp256k1Verify(
|
||||
MESSAGE,
|
||||
secp256k1Sign(MESSAGE, pair, 'keccak'),
|
||||
secp256k1Hasher('keccak', secp256k1Expand(pair.publicKey)),
|
||||
hasher('keccak', secp256k1Expand(pair.publicKey)),
|
||||
'keccak'
|
||||
)
|
||||
).toBe(true);
|
||||
).toEqual(true);
|
||||
} catch (error) {
|
||||
console.error(`keccak failed on #${i}`);
|
||||
throw error;
|
||||
|
||||
@@ -4,13 +4,22 @@
|
||||
import { secp256k1PrivateKeyTweakAdd } from './tweakAdd';
|
||||
|
||||
describe('TweakAdd', (): void => {
|
||||
describe('PrivateKey TweakAdd', (): void => {
|
||||
it('fails for wrong array length', (): void => {
|
||||
const A = new Uint8Array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]);
|
||||
const B = new Uint8Array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]);
|
||||
|
||||
expect(
|
||||
() => secp256k1PrivateKeyTweakAdd(A, B)
|
||||
).toThrow(/Expected tweak to be an Uint8Array/);
|
||||
});
|
||||
|
||||
describe.each([false, true])('onlyBn=%p', (onlyBn): void => {
|
||||
it('succeeds for a simple case', (): void => {
|
||||
const A = new Uint8Array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]);
|
||||
const B = new Uint8Array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]);
|
||||
|
||||
expect(
|
||||
secp256k1PrivateKeyTweakAdd(A, B)
|
||||
secp256k1PrivateKeyTweakAdd(A, B, onlyBn)
|
||||
).toEqual(new Uint8Array([
|
||||
3, 4, 3, 4, 3, 4, 3, 4, 3,
|
||||
4, 3, 4, 3, 4, 3, 4, 3, 4,
|
||||
@@ -18,14 +27,5 @@ describe('TweakAdd', (): void => {
|
||||
4, 3, 4, 3, 4
|
||||
]));
|
||||
});
|
||||
|
||||
it('fails for wrong array length', (): void => {
|
||||
const A = new Uint8Array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]);
|
||||
const B = new Uint8Array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]);
|
||||
|
||||
expect(
|
||||
() => secp256k1PrivateKeyTweakAdd(A, B)
|
||||
).toThrow(/Expected tweak to be an Uint8Array/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,32 +1,54 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert, BN, bnToU8a, isU8a } from '@polkadot/util';
|
||||
import { _0n, assert, BN, bnToU8a, hasBigInt, isU8a, nToU8a, u8aToBigInt } from '@polkadot/util';
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { BN_BE_256_OPTS } from '../bn';
|
||||
import { BN_BE_256_OPTS, BN_BE_OPTS } from '../bn';
|
||||
|
||||
// pre-defined curve param as lifted form elliptic
|
||||
// https://github.com/indutny/elliptic/blob/e71b2d9359c5fe9437fbf46f1f05096de447de57/lib/elliptic/curves.js#L182
|
||||
const N = new BN(
|
||||
'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141'.replace(/ /g, ''),
|
||||
'hex'
|
||||
);
|
||||
const N = 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141'.replace(/ /g, '');
|
||||
const N_BI = BigInt(`0x${N}`);
|
||||
const N_BN = new BN(N, 'hex');
|
||||
|
||||
export function secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Array): Uint8Array {
|
||||
function addBi (seckey: Uint8Array, tweak: Uint8Array): Uint8Array {
|
||||
let res = u8aToBigInt(tweak, BN_BE_OPTS);
|
||||
|
||||
assert(res < N_BI, 'Tweak parameter is out of range');
|
||||
|
||||
res += u8aToBigInt(seckey, BN_BE_OPTS);
|
||||
|
||||
if (res >= N_BI) {
|
||||
res -= N_BI;
|
||||
}
|
||||
|
||||
assert(res !== _0n, 'Invalid resulting private key');
|
||||
|
||||
return nToU8a(res, BN_BE_256_OPTS);
|
||||
}
|
||||
|
||||
function addBn (seckey: Uint8Array, tweak: Uint8Array): Uint8Array {
|
||||
const res = new BN(tweak);
|
||||
|
||||
assert(res.cmp(N_BN) < 0, 'Tweak parameter is out of range');
|
||||
|
||||
res.iadd(new BN(seckey));
|
||||
|
||||
if (res.cmp(N_BN) >= 0) {
|
||||
res.isub(N_BN);
|
||||
}
|
||||
|
||||
assert(!res.isZero(), 'Invalid resulting private key');
|
||||
|
||||
return bnToU8a(res, BN_BE_256_OPTS);
|
||||
}
|
||||
|
||||
export function secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Array, onlyBn?: boolean): Uint8Array {
|
||||
assert(isU8a(seckey) && seckey.length === 32, 'Expected seckey to be an Uint8Array with length 32');
|
||||
assert(isU8a(tweak) && tweak.length === 32, 'Expected tweak to be an Uint8Array with length 32');
|
||||
|
||||
const bn = new BN(tweak);
|
||||
|
||||
assert(bn.cmp(N) < 0, 'Tweak parameter is out of range');
|
||||
|
||||
bn.iadd(new BN(seckey));
|
||||
|
||||
if (bn.cmp(N) >= 0) {
|
||||
bn.isub(N);
|
||||
}
|
||||
|
||||
assert(!bn.isZero(), 'Invalid resulting private key');
|
||||
|
||||
return bnToU8a(bn, BN_BE_256_OPTS);
|
||||
return !hasBigInt || onlyBn
|
||||
? addBn(seckey, tweak)
|
||||
: addBi(seckey, tweak);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,75 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { secp256k1Verify } from '.';
|
||||
import '../cryptoInit';
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { hasher } from './hasher';
|
||||
import { secp256k1PairFromSeed, secp256k1Verify } from '.';
|
||||
|
||||
const message = 'Pay KSMs to the Kusama account:88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee';
|
||||
|
||||
describe('secp256k1Verify', (): void => {
|
||||
it('validates known ETH against address', (): void => {
|
||||
const message = 'Pay KSMs to the Kusama account:88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee';
|
||||
|
||||
expect(
|
||||
secp256k1Verify(
|
||||
`\x19Ethereum Signed Message:\n${message.length.toString()}${message}`,
|
||||
'0x55bd020bdbbdc02de34e915effc9b18a99002f4c29f64e22e8dcbb69e722ea6c28e1bb53b9484063fbbfd205e49dcc1f620929f520c9c4c3695150f05a28f52a01',
|
||||
'0x002309df96687e44280bb72c3818358faeeb699c',
|
||||
'keccak'
|
||||
)
|
||||
).toBe(true);
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('validates known ETH against address', (): void => {
|
||||
expect(
|
||||
secp256k1Verify(
|
||||
`\x19Ethereum Signed Message:\n${message.length.toString()}${message}`,
|
||||
'0x55bd020bdbbdc02de34e915effc9b18a99002f4c29f64e22e8dcbb69e722ea6c28e1bb53b9484063fbbfd205e49dcc1f620929f520c9c4c3695150f05a28f52a01',
|
||||
'0x002309df96687e44280bb72c3818358faeeb699c',
|
||||
'keccak',
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
describe.each([false, true])('validation against known, publicKey=%p', (isPublic): void => {
|
||||
const pair = secp256k1PairFromSeed(hexToU8a('0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae'));
|
||||
const msg = hexToU8a('0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9');
|
||||
const addr = isPublic
|
||||
? pair.publicKey
|
||||
: hasher('blake2', pair.publicKey, onlyJs);
|
||||
|
||||
it('signature from JS', (): void => {
|
||||
expect(
|
||||
secp256k1Verify(
|
||||
msg,
|
||||
'0xdf92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba8c4efa521361678d9b8582744d77aa4b8d886d7380b7808a683174afad9c4700300',
|
||||
addr,
|
||||
'blake2',
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('signature from wasm', (): void => {
|
||||
expect(
|
||||
secp256k1Verify(
|
||||
msg,
|
||||
'0xdf92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba83b105adec9e9872647a7d8bb28855b45e22805aea3d097953cbb1391f671d13e01',
|
||||
addr,
|
||||
'blake2',
|
||||
onlyJs
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
performanceWasm('secp256k1Verify', 100, (_, onlyJs) =>
|
||||
secp256k1Verify(
|
||||
`\x19Ethereum Signed Message:\n${message.length.toString()}${message}`,
|
||||
'0x55bd020bdbbdc02de34e915effc9b18a99002f4c29f64e22e8dcbb69e722ea6c28e1bb53b9484063fbbfd205e49dcc1f620929f520c9c4c3695150f05a28f52a01',
|
||||
'0x002309df96687e44280bb72c3818358faeeb699c',
|
||||
'keccak',
|
||||
onlyJs
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -6,36 +6,26 @@ import type { HashType } from './types';
|
||||
|
||||
import { assert, u8aEq, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { secp256k1Expand } from './expand';
|
||||
import { secp256k1Hasher } from './hasher';
|
||||
import { secp256k1 } from './secp256k1';
|
||||
import { hasher } from './hasher';
|
||||
import { secp256k1Recover } from './recover';
|
||||
|
||||
/**
|
||||
* @name secp256k1Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function secp256k1Verify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, address: HexString | Uint8Array | string, hashType: HashType = 'blake2'): boolean {
|
||||
const isEthereum = hashType === 'keccak';
|
||||
const u8a = u8aToU8a(signature);
|
||||
export function secp256k1Verify (msgHash: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, address: HexString | Uint8Array | string, hashType: HashType = 'blake2', onlyJs?: boolean): boolean {
|
||||
const sig = u8aToU8a(signature);
|
||||
|
||||
assert(u8a.length === 65, `Expected signature with 65 bytes, ${u8a.length} found instead`);
|
||||
assert(sig.length === 65, `Expected signature with 65 bytes, ${sig.length} found instead`);
|
||||
|
||||
const publicKey = new Uint8Array(
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument
|
||||
secp256k1
|
||||
.recoverPubKey(
|
||||
secp256k1Hasher(hashType, message),
|
||||
{ r: u8a.slice(0, 32), s: u8a.slice(32, 64) },
|
||||
u8a[64]
|
||||
)
|
||||
.encodeCompressed()
|
||||
);
|
||||
|
||||
const signingAddress = secp256k1Hasher(hashType, isEthereum ? secp256k1Expand(publicKey) : publicKey);
|
||||
const inputAddress = u8aToU8a(address);
|
||||
const publicKey = secp256k1Recover(hasher(hashType, msgHash), sig, sig[64], hashType, onlyJs);
|
||||
const signerAddr = hasher(hashType, publicKey, onlyJs);
|
||||
const inputAddr = u8aToU8a(address);
|
||||
|
||||
// for Ethereum (keccak) the last 20 bytes is the address
|
||||
return isEthereum
|
||||
? u8aEq(signingAddress.slice(-20), inputAddress.slice(-20))
|
||||
: u8aEq(signingAddress, inputAddress);
|
||||
return u8aEq(publicKey, inputAddr) || (
|
||||
hashType === 'keccak'
|
||||
? u8aEq(signerAddr.slice(-20), inputAddr.slice(-20))
|
||||
: u8aEq(signerAddr, inputAddr)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,34 +1,21 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { sha256 as sha256Js } from '@noble/hashes/lib/sha256';
|
||||
import { sha512 as sha512Js } from '@noble/hashes/lib/sha512';
|
||||
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, sha256, sha512 } from '@polkadot/wasm-crypto';
|
||||
import { sha256, sha512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { createBitHasher } from '../helpers';
|
||||
|
||||
type BitLength = 256 | 512;
|
||||
import { createBitHasher, createDualHasher } from '../helpers';
|
||||
|
||||
/**
|
||||
* @name shaAsU8a
|
||||
* @summary Creates a sha Uint8Array from the input.
|
||||
*/
|
||||
export function shaAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? is256
|
||||
? sha256(u8a)
|
||||
: sha512(u8a)
|
||||
: is256
|
||||
? sha256Js(u8a)
|
||||
: sha512Js(u8a);
|
||||
}
|
||||
export const shaAsU8a = createDualHasher(
|
||||
{ 256: sha256, 512: sha512 },
|
||||
{ 256: sha256Js, 512: sha512Js }
|
||||
);
|
||||
|
||||
/**
|
||||
* @name sha256AsU8a
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { sha256AsU8a } from '.';
|
||||
|
||||
const TESTS = [
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { sha512AsU8a } from '.';
|
||||
|
||||
describe('sha512AsU8a', (): void => {
|
||||
|
||||
+4
-4
@@ -2,16 +2,16 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { arrayRange } from '@polkadot/util';
|
||||
import { randomAsU8a } from '@polkadot/util-crypto';
|
||||
import { performance, performanceCmp } from '@polkadot/util/test/performance';
|
||||
|
||||
import { performance, performanceCmp } from '../../util/test/performance';
|
||||
import { randomAsU8a } from '..';
|
||||
|
||||
type ExecFn = (input: Uint8Array, onlyJs: boolean) => unknown;
|
||||
|
||||
const GENERATED = arrayRange(256).map(() => [randomAsU8a()]);
|
||||
|
||||
export function performanceWasm (name: string, count: number, exec: ExecFn): void {
|
||||
performanceCmp(name, ['WebAssembly', 'JavaScript'], count, GENERATED, exec);
|
||||
export function performanceWasm (name: string, count: number, exec: ExecFn, inputs = GENERATED): void {
|
||||
performanceCmp(name, ['WebAssembly', 'JavaScript'], count, inputs, exec);
|
||||
}
|
||||
|
||||
export { performance, performanceCmp };
|
||||
@@ -4,7 +4,7 @@
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { performanceWasm } from '../test/performance';
|
||||
import { xxhashAsU8a } from '.';
|
||||
|
||||
describe('xxhashAsU8a', (): void => {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { _0n, _1n, _n } from '@polkadot/util';
|
||||
import { _0n, _1n } from '@polkadot/util';
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
// Adapted from https://github.com/pierrec/js-xxhash/blob/0504e76f3d31a21ae8528a7f590c7289c9e431d2/lib/xxhash64.js
|
||||
//
|
||||
@@ -26,29 +27,29 @@ interface State {
|
||||
v4: bigint;
|
||||
}
|
||||
|
||||
const P64_1 = _n('11400714785074694791');
|
||||
const P64_2 = _n('14029467366897019727');
|
||||
const P64_3 = _n('1609587929392839161');
|
||||
const P64_4 = _n('9650029242287828579');
|
||||
const P64_5 = _n('2870177450012600261');
|
||||
const P64_1 = BigInt('11400714785074694791');
|
||||
const P64_2 = BigInt('14029467366897019727');
|
||||
const P64_3 = BigInt('1609587929392839161');
|
||||
const P64_4 = BigInt('9650029242287828579');
|
||||
const P64_5 = BigInt('2870177450012600261');
|
||||
|
||||
// mask for a u64, all bits set
|
||||
const U64 = _n('0xffffffffffffffff');
|
||||
const U64 = BigInt('0xffffffffffffffff');
|
||||
|
||||
// various constants
|
||||
const _7n = _n(7);
|
||||
const _11n = _n(11);
|
||||
const _12n = _n(12);
|
||||
const _16n = _n(16);
|
||||
const _18n = _n(18);
|
||||
const _23n = _n(23);
|
||||
const _27n = _n(27);
|
||||
const _29n = _n(29);
|
||||
const _31n = _n(31);
|
||||
const _32n = _n(32);
|
||||
const _33n = _n(33);
|
||||
const _64n = _n(64);
|
||||
const _256n = _n(256);
|
||||
const _7n = BigInt(7);
|
||||
const _11n = BigInt(11);
|
||||
const _12n = BigInt(12);
|
||||
const _16n = BigInt(16);
|
||||
const _18n = BigInt(18);
|
||||
const _23n = BigInt(23);
|
||||
const _27n = BigInt(27);
|
||||
const _29n = BigInt(29);
|
||||
const _31n = BigInt(31);
|
||||
const _32n = BigInt(32);
|
||||
const _33n = BigInt(33);
|
||||
const _64n = BigInt(64);
|
||||
const _256n = BigInt(256);
|
||||
|
||||
function rotl (a: bigint, b: bigint): bigint {
|
||||
const c = a & U64;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "..",
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"references": [
|
||||
{ "path": "../networks" },
|
||||
{ "path": "../util" },
|
||||
{ "path": "../x-bigint" },
|
||||
{ "path": "../x-randomvalues" }
|
||||
]
|
||||
}
|
||||
@@ -34,14 +34,19 @@
|
||||
"./detectPackage.cjs"
|
||||
],
|
||||
"type": "module",
|
||||
"version": "8.0.1",
|
||||
"version": "8.2.2",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@polkadot/x-textdecoder": "8.0.1",
|
||||
"@polkadot/x-textencoder": "8.0.1",
|
||||
"@babel/runtime": "^7.16.5",
|
||||
"@polkadot/x-bigint": "8.2.2",
|
||||
"@polkadot/x-global": "8.2.2",
|
||||
"@polkadot/x-textdecoder": "8.2.2",
|
||||
"@polkadot/x-textencoder": "8.2.2",
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"bn.js": "^4.12.0",
|
||||
"ip-regex": "^4.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/x-randomvalues": "8.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,3 +42,11 @@ export function assertReturn <T> (value: T | undefined | null, message: string |
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name assertUnreachable
|
||||
* @description An assertion helper that ensures all codepaths are followed
|
||||
*/
|
||||
export function assertUnreachable (x: never): never {
|
||||
throw new Error(`This codepath should be unreachable. Unhandled input: ${x as unknown as string}`);
|
||||
}
|
||||
|
||||
@@ -1,39 +1,31 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
type NewBigInt = (value: string | number | bigint | boolean) => bigint;
|
||||
|
||||
/**
|
||||
* @name _n
|
||||
* @description Creates a new instance of BigInt in environments that do support it
|
||||
*/
|
||||
export const _n: NewBigInt = typeof BigInt !== 'undefined'
|
||||
? BigInt
|
||||
: () => Number.NaN as unknown as bigint;
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
/**
|
||||
* @name _0n
|
||||
* @summary BigInt constant for 0.
|
||||
*/
|
||||
export const _0n = _n(0);
|
||||
export const _0n = BigInt(0);
|
||||
|
||||
/**
|
||||
* @name _1n
|
||||
* @summary BigInt constant for 1.
|
||||
*/
|
||||
export const _1n = _n(1);
|
||||
export const _1n = BigInt(1);
|
||||
|
||||
/**
|
||||
* @name _1Mn
|
||||
* @summary BigInt constant for 1,000,000.
|
||||
*/
|
||||
export const _1Mn = _n(1_000_000);
|
||||
export const _1Mn = BigInt(1_000_000);
|
||||
|
||||
/**
|
||||
* @name _1Bn
|
||||
* @summary BigInt constant for 1,000,000,000.
|
||||
*/
|
||||
export const _1Bn = _n(1_000_000_000);
|
||||
export const _1Bn = BigInt(1_000_000_000);
|
||||
|
||||
/**
|
||||
* @name _1Qn
|
||||
@@ -45,4 +37,4 @@ export const _1Qn = _1Bn * _1Bn;
|
||||
* @name _2pow53n
|
||||
* @summary BigInt constant for MAX_SAFE_INTEGER
|
||||
*/
|
||||
export const _2pow53n = _n(Number.MAX_SAFE_INTEGER);
|
||||
export const _2pow53n = BigInt(Number.MAX_SAFE_INTEGER);
|
||||
|
||||
@@ -9,7 +9,6 @@ export * from './consts';
|
||||
|
||||
export { nSqrt } from './sqrt';
|
||||
export { nToBigInt } from './toBigInt';
|
||||
export { nMax } from './max';
|
||||
export { nMin } from './min';
|
||||
export { nMax, nMin } from './min';
|
||||
export { nToHex } from './toHex';
|
||||
export { nToU8a } from './toU8a';
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export function nMax (...items: bigint[]): bigint {
|
||||
assert(items.length >= 1, 'Must provide one or more bigint arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
if (items[i] > result) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -3,20 +3,40 @@
|
||||
|
||||
import { assert } from '../assert';
|
||||
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export function nMin (...items: bigint[]): bigint {
|
||||
function gt (a: bigint, b: bigint): boolean {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
function lt (a: bigint, b: bigint): boolean {
|
||||
return a < b;
|
||||
}
|
||||
|
||||
function find (items: bigint[], cmp: (a: bigint, b: bigint) => boolean): bigint {
|
||||
assert(items.length >= 1, 'Must provide one or more bigint arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
if (items[i] < result) {
|
||||
if (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export function nMax (...items: bigint[]): bigint {
|
||||
return find(items, gt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export function nMin (...items: bigint[]): bigint {
|
||||
return find(items, lt);
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
import type { BN } from '../bn';
|
||||
import type { ToBigInt, ToBn } from '../types';
|
||||
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { _0n, _1n, _2pow53n, _n } from './consts';
|
||||
import { _0n, _1n, _2pow53n } from './consts';
|
||||
import { nToBigInt } from './toBigInt';
|
||||
|
||||
const _sqrt2pow53n = _n(94906265);
|
||||
const _sqrt2pow53n = BigInt(94906265);
|
||||
|
||||
/**
|
||||
* @name nSqrt
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
import type { BN } from '../bn/bn';
|
||||
import type { HexString, ToBigInt, ToBn } from '../types';
|
||||
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { hexToBigInt } from '../hex/toBigInt';
|
||||
import { isBn } from '../is/bn';
|
||||
import { isHex } from '../is/hex';
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../../test/performance';
|
||||
import { arrayRange } from '../array';
|
||||
import { performance } from '../test/performance';
|
||||
import { nToU8a } from '.';
|
||||
|
||||
const ptest = arrayRange(65536).map((v) => [v]);
|
||||
|
||||
@@ -4,16 +4,18 @@
|
||||
import type { BN } from '../bn/bn';
|
||||
import type { ToBigInt, ToBn, ToBnOptions } from '../types';
|
||||
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { _0n, _1n, _n } from './consts';
|
||||
import { _0n, _1n } from './consts';
|
||||
import { nToBigInt } from './toBigInt';
|
||||
|
||||
interface Options extends ToBnOptions {
|
||||
bitLength?: number;
|
||||
}
|
||||
|
||||
const DIV = _n(256);
|
||||
const NEG_MASK = _n(0xff);
|
||||
const DIV = BigInt(256);
|
||||
const NEG_MASK = BigInt(0xff);
|
||||
|
||||
function createEmpty ({ bitLength = 0 }: Options): Uint8Array {
|
||||
return bitLength === -1
|
||||
|
||||
@@ -9,8 +9,7 @@ export * from './consts';
|
||||
|
||||
export { BN } from './bn';
|
||||
export { bnFromHex } from './fromHex';
|
||||
export { bnMax } from './max';
|
||||
export { bnMin } from './min';
|
||||
export { bnMax, bnMin } from './min';
|
||||
export { bnSqrt } from './sqrt';
|
||||
export { bnToBn } from './toBn';
|
||||
export { bnToHex } from './toHex';
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = BN[type](result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMax
|
||||
* @summary Finds and returns the highest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMax } from '@polkadot/util';
|
||||
*
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export function bnMax (...items: BN[]): BN {
|
||||
return checkMaxMin('max', items);
|
||||
}
|
||||
@@ -1,9 +1,37 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from './bn';
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
import { checkMaxMin } from './max';
|
||||
export function find (items: BN[], cmp: (a: BN, b: BN) => BN): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = cmp(result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMax
|
||||
* @summary Finds and returns the highest value in an array of BNs.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import BN from 'bn.js';
|
||||
* import { bnMax } from '@polkadot/util';
|
||||
*
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export function bnMax (...items: BN[]): BN {
|
||||
return find(items, BN.max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnMin
|
||||
@@ -19,5 +47,5 @@ import { checkMaxMin } from './max';
|
||||
* ```
|
||||
*/
|
||||
export function bnMin (...items: BN[]): BN {
|
||||
return checkMaxMin('min', items);
|
||||
return find(items, BN.min);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../../test/performance';
|
||||
import { arrayRange } from '../array';
|
||||
import { performance } from '../test/performance';
|
||||
import { BN, bnToU8a } from '.';
|
||||
|
||||
const ptest = arrayRange(65536).map((v) => [v]);
|
||||
|
||||
@@ -43,6 +43,33 @@ const DEFAULT_UNIT = SI[SI_MID].text;
|
||||
let defaultDecimals = DEFAULT_DECIMALS;
|
||||
let defaultUnit = DEFAULT_UNIT;
|
||||
|
||||
function getUnits (si: SiDef, withSi: boolean, withSiFull: boolean, withUnit: boolean | string): string {
|
||||
const unit = isBoolean(withUnit)
|
||||
? SI[SI_MID].text
|
||||
: withUnit;
|
||||
|
||||
return withSi || withSiFull
|
||||
? si.value === '-'
|
||||
? withUnit
|
||||
? ` ${unit}`
|
||||
: ''
|
||||
: ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}`
|
||||
: '';
|
||||
}
|
||||
|
||||
function getPrePost (text: string, decimals: number, forceUnit?: string): [SiDef, string, string] {
|
||||
// NOTE We start at midpoint (8) minus 1 - this means that values display as
|
||||
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we use ceil
|
||||
// so there are at most 3 decimal before the decimal separator
|
||||
const si = calcSi(text, decimals, forceUnit);
|
||||
const mid = text.length - (decimals + si.power);
|
||||
const prefix = text.substring(0, mid);
|
||||
const padding = mid < 0 ? 0 - mid : 0;
|
||||
const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substring(mid < 0 ? 0 : mid)}0000`.substring(0, 4);
|
||||
|
||||
return [si, prefix || '0', postfix];
|
||||
}
|
||||
|
||||
// Formats a string/number with <prefix>.<postfix><type> notation
|
||||
function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | bigint | ExtToBn, options: Options | boolean = true, optDecimals: number = defaultDecimals): string {
|
||||
let text = bnToBn(input).toString();
|
||||
@@ -51,39 +78,27 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | b
|
||||
return '0';
|
||||
}
|
||||
|
||||
// strip the negative sign so we can work with clean groupings, re-add this in the
|
||||
// end when we return the result (from here on we work with positive numbers)
|
||||
const isNegative = text[0].startsWith('-');
|
||||
|
||||
if (isNegative) {
|
||||
text = text.substr(1);
|
||||
}
|
||||
|
||||
// extract options - the boolean case is for backwards-compat
|
||||
const { decimals = optDecimals, forceUnit = undefined, withSi = true, withSiFull = false, withUnit = true } = isBoolean(options)
|
||||
? { withSi: options }
|
||||
: options;
|
||||
|
||||
// NOTE We start at midpoint (8) minus 1 - this means that values display as
|
||||
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we use ceil
|
||||
// so there are at most 3 decimal before the decimal separator
|
||||
const si = calcSi(text, decimals, forceUnit);
|
||||
const mid = text.length - (decimals + si.power);
|
||||
const prefix = text.substr(0, mid);
|
||||
const padding = mid < 0 ? 0 - mid : 0;
|
||||
const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substr(mid < 0 ? 0 : mid)}0000`.substr(0, 4);
|
||||
const units = withSi || withSiFull
|
||||
? si.value === '-'
|
||||
? withUnit
|
||||
? ` ${isBoolean(withUnit) ? si.text : withUnit}`
|
||||
: ''
|
||||
: ` ${withSiFull ? si.text : si.value}${withUnit ? `${withSiFull ? ' ' : ''}${(isBoolean(withUnit) ? SI[SI_MID].text : withUnit)}` : ''}`
|
||||
: '';
|
||||
// strip the negative sign so we can work with clean groupings, re-add this in the
|
||||
// end when we return the result (from here on we work with positive numbers)
|
||||
let sign = '';
|
||||
|
||||
return `${isNegative ? '-' : ''}${formatDecimal(prefix || '0')}.${postfix}${units}`;
|
||||
if (text[0].startsWith('-')) {
|
||||
sign = '-';
|
||||
text = text.substring(1);
|
||||
}
|
||||
|
||||
const [si, prefix, postfix] = getPrePost(text, decimals, forceUnit);
|
||||
const units = getUnits(si, withSi, withSiFull, withUnit);
|
||||
|
||||
return `${sign}${formatDecimal(prefix)}.${postfix}${units}`;
|
||||
}
|
||||
|
||||
const formatBalance = _formatBalance as BalanceFormatter;
|
||||
export const formatBalance = _formatBalance as BalanceFormatter;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
formatBalance.calcSi = (text: string, decimals: number = defaultDecimals): SiDef =>
|
||||
@@ -126,5 +141,3 @@ formatBalance.setDefaults = ({ decimals, unit }: SetDefaults): void => {
|
||||
|
||||
SI[SI_MID].text = defaultUnit;
|
||||
};
|
||||
|
||||
export { formatBalance };
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export const hasBigInt = typeof BigInt !== 'undefined';
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
export const hasBigInt = typeof BigInt === 'function' && typeof BigInt.asIntN === 'function';
|
||||
export const hasBuffer = typeof Buffer !== 'undefined';
|
||||
export const hasDirname = typeof __dirname !== 'undefined';
|
||||
export const hasProcess = typeof process === 'object';
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
import type { ToBnOptions } from '../types';
|
||||
|
||||
import { BigInt } from '@polkadot/x-bigint';
|
||||
|
||||
import { objectSpread } from '../object/spread';
|
||||
import { u8aToBigInt } from '../u8a/toBigInt';
|
||||
import { hexToU8a } from './toU8a';
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user