Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1934060e0 | ||
|
|
6d9da39d8b | ||
|
|
4ed096024e | ||
|
|
ae36d58802 | ||
|
|
c8100dbe43 | ||
|
|
28d2ce38bd | ||
|
|
afce99ed1a | ||
|
|
29d13ab19c | ||
|
|
68a8bcb7db | ||
|
|
d2ecf1730a | ||
|
|
af56c10278 | ||
|
|
42f318fcb5 | ||
|
|
336df0d76d | ||
|
|
d85cedf5a0 | ||
|
|
a59187bce6 | ||
|
|
f9a5b75e10 | ||
|
|
87228149d4 | ||
|
|
67f36f6a13 | ||
|
|
75a9898daf | ||
|
|
0f726ce777 | ||
|
|
765f9baf44 | ||
|
|
2d181df360 | ||
|
|
77ff703a08 | ||
|
|
804ed13d0a | ||
|
|
341a6b0c08 | ||
|
|
614985b8ac |
@@ -1 +1,2 @@
|
||||
5
|
||||
2.6.2
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
${{ runner.os }}-yarn-
|
||||
- name: lint
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
yarn lint
|
||||
|
||||
test:
|
||||
@@ -55,7 +55,7 @@ jobs:
|
||||
env:
|
||||
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
yarn test
|
||||
|
||||
build_code:
|
||||
@@ -82,7 +82,7 @@ jobs:
|
||||
${{ runner.os }}-yarn-
|
||||
- name: build
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
yarn build
|
||||
|
||||
build_docs:
|
||||
@@ -109,5 +109,5 @@ jobs:
|
||||
${{ runner.os }}-yarn-
|
||||
- name: build
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
yarn docs
|
||||
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
yarn polkadot-ci-ghact-build
|
||||
|
||||
build_docs:
|
||||
@@ -71,7 +71,7 @@ jobs:
|
||||
GH_PAGES_SRC: build-docs
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn install --immutable | grep -v 'YN0013'
|
||||
yarn polkadot-ci-ghact-docs
|
||||
|
||||
dummy:
|
||||
|
||||
+3
-3
@@ -12,10 +12,10 @@ tmp/
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.npmrc
|
||||
.yarn/*
|
||||
!.yarn/releases
|
||||
!.yarn/plugins
|
||||
.pnp.*
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
cc-test-reporter
|
||||
lerna-debug.log*
|
||||
npm-debug.log*
|
||||
|
||||
File diff suppressed because one or more lines are too long
+58
File diff suppressed because one or more lines are too long
+10
@@ -0,0 +1,10 @@
|
||||
enableImmutableInstalls: false
|
||||
enableProgressBars: false
|
||||
|
||||
nodeLinker: node-modules
|
||||
|
||||
plugins:
|
||||
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.js
|
||||
spec: "@yarnpkg/plugin-interactive-tools"
|
||||
|
||||
yarnPath: .yarn/releases/yarn-berry.js
|
||||
+10
-1
@@ -1,8 +1,17 @@
|
||||
# 2.6.1 Feb 29, 2020
|
||||
|
||||
- **Breaking change** Following on the 2.0 release, `@polkadot/wasm` was updated to 1.1+. sr25510 now only verifies 0.8+ signatures, closing the loop on dropping Substrate 1.x-era support
|
||||
- Add `signatureVerify (message: Uint8Array | string, signature: Uint8Array | string, addressOrPublicKey: Uint8Array | string): VerifyResult` to verify any signature, regardless of type. Returns validity & detected crypto type.
|
||||
- Add `cryptoIsReady (): boolean` function to check status of initialization
|
||||
- Add `addressCheckChecksum (decoded: Uint8Array)` as an helper to extract and verify the ss58 checksum
|
||||
- Swap to yarn 2, allow use in yan 2 projects
|
||||
|
||||
# 2.5.1 Feb 22, 2020
|
||||
|
||||
- **Breaking change** (TypeScript only) The `*.d.ts` files now contain TypeScript 3.8 features, `#private`, which is not usable in older versions
|
||||
- Add `isBigInt(value)` to allow `BigInt` checks. Also add support in `bnToBn` as well as number conversion utils
|
||||
- Add `isChildClass(Parent, Child?)` to check if a class extends the parent, asserting Child is Parent
|
||||
- (internal) Use `#<varname>` insead of `private _<varname>` for private class variables
|
||||
- (internal) Use `#<field>` instead of `private _<field>` for private class variables
|
||||
|
||||
# 2.4.1 Feb 18, 2020
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# checkout latest
|
||||
git fetch
|
||||
git checkout gh-pages
|
||||
git pull
|
||||
git checkout --orphan gh-pages-temp
|
||||
|
||||
# cleanup
|
||||
rm -rf node_modules
|
||||
rm -rf coverage
|
||||
rm -rf packages
|
||||
rm -rf test
|
||||
|
||||
# add
|
||||
git add -A
|
||||
git commit -am "refresh history"
|
||||
|
||||
# danger, force new
|
||||
git branch -D gh-pages
|
||||
git branch -m gh-pages
|
||||
git push -f origin gh-pages
|
||||
+1
-1
@@ -9,5 +9,5 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "2.5.1"
|
||||
"version": "2.6.2"
|
||||
}
|
||||
|
||||
+7
-8
@@ -1,14 +1,13 @@
|
||||
{
|
||||
"version": "2.5.1",
|
||||
"repository": "https://github.com/polkadot-js/common",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "Apache-2",
|
||||
"private": true,
|
||||
"engines": {
|
||||
"yarn": "^1.10.1"
|
||||
},
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
],
|
||||
"resolutions": {
|
||||
"typescript": "^3.8.2"
|
||||
"typescript": "^3.8.3"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-ts",
|
||||
@@ -20,8 +19,8 @@
|
||||
"test:one": "polkadot-dev-run-test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.8.4",
|
||||
"@polkadot/dev": "^0.41.2",
|
||||
"@polkadot/ts": "^0.2.3"
|
||||
"@babel/core": "^7.8.7",
|
||||
"@polkadot/dev": "^0.50.30",
|
||||
"@polkadot/ts": "^0.3.7"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/keyring",
|
||||
"version": "2.5.1",
|
||||
"version": "2.6.2",
|
||||
"description": "Keyring management",
|
||||
"main": "index.js",
|
||||
"publishConfig": {
|
||||
@@ -27,8 +27,8 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.8.4",
|
||||
"@polkadot/util": "2.5.1",
|
||||
"@polkadot/util-crypto": "2.5.1"
|
||||
"@babel/runtime": "^7.8.7",
|
||||
"@polkadot/util": "2.6.2",
|
||||
"@polkadot/util-crypto": "2.6.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { decodeAddress, encodeAddress, setSS58Format } from '@polkadot/util-cryp
|
||||
import Keyring from './keyring';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
detectPackage(require('./package.json'), __dirname);
|
||||
detectPackage(require('./package.json'), typeof __dirname !== 'undefined' && __dirname);
|
||||
|
||||
export default Keyring;
|
||||
|
||||
|
||||
@@ -14,19 +14,7 @@ type DecodeResult = PairInfo & {
|
||||
secretKey: Uint8Array;
|
||||
};
|
||||
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(encrypted, 'No encrypted data available to decode');
|
||||
|
||||
const encoded = passphrase
|
||||
? (naclDecrypt(
|
||||
encrypted.subarray(NONCE_LENGTH),
|
||||
encrypted.subarray(0, NONCE_LENGTH),
|
||||
u8aFixLength(stringToU8a(passphrase), 256, true)
|
||||
) as Uint8Array)
|
||||
: encrypted;
|
||||
|
||||
assert(encoded, 'Unable to unencrypt using the supplied passphrase');
|
||||
|
||||
function decodePkcs8 (encoded: Uint8Array): DecodeResult {
|
||||
const header = encoded.subarray(0, PKCS8_HEADER.length);
|
||||
|
||||
assert(header.toString() === PKCS8_HEADER.toString(), 'Invalid Pkcs8 header found in body');
|
||||
@@ -52,3 +40,19 @@ export default function decode (passphrase?: string, encrypted?: Uint8Array | nu
|
||||
secretKey
|
||||
};
|
||||
}
|
||||
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(encrypted, 'No encrypted data available to decode');
|
||||
|
||||
const encoded = passphrase
|
||||
? naclDecrypt(
|
||||
encrypted.subarray(NONCE_LENGTH),
|
||||
encrypted.subarray(0, NONCE_LENGTH),
|
||||
u8aFixLength(stringToU8a(passphrase), 256, true)
|
||||
)
|
||||
: encrypted;
|
||||
|
||||
assert(encoded, 'Unable to unencrypt using the supplied passphrase');
|
||||
|
||||
return decodePkcs8(encoded);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-crypto",
|
||||
"version": "2.5.1",
|
||||
"version": "2.6.2",
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,23 +25,23 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.8.4",
|
||||
"@polkadot/util": "2.5.1",
|
||||
"@polkadot/wasm-crypto": "^1.0.1",
|
||||
"base-x": "^3.0.7",
|
||||
"@babel/runtime": "^7.8.7",
|
||||
"@polkadot/util": "2.6.2",
|
||||
"@polkadot/wasm-crypto": "^1.2.1",
|
||||
"base-x": "^3.0.8",
|
||||
"bip39": "^3.0.2",
|
||||
"blakejs": "^1.1.0",
|
||||
"bn.js": "^5.1.1",
|
||||
"bs58": "^4.0.1",
|
||||
"elliptic": "^6.5.2",
|
||||
"js-sha3": "^0.8.0",
|
||||
"pbkdf2": "^3.0.9",
|
||||
"pbkdf2": "^3.0.17",
|
||||
"tweetnacl": "^1.0.3",
|
||||
"xxhashjs": "^0.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"@types/bs58": "^4.0.0",
|
||||
"@types/bs58": "^4.0.1",
|
||||
"@types/elliptic": "^6.4.12",
|
||||
"@types/pbkdf2": "^3.0.0",
|
||||
"@types/xxhashjs": "^0.2.1"
|
||||
|
||||
@@ -6,8 +6,8 @@ import { Prefix } from './types';
|
||||
|
||||
import bs58 from 'bs58';
|
||||
|
||||
import checkChecksum from './checkChecksum';
|
||||
import defaults from './defaults';
|
||||
import sshash from './sshash';
|
||||
|
||||
export default function check (address: string, prefix: Prefix): [boolean, string | null] {
|
||||
const decoded = bs58.decode(address);
|
||||
@@ -18,18 +18,7 @@ export default function check (address: string, prefix: Prefix): [boolean, strin
|
||||
return [false, 'Invalid decoded address length'];
|
||||
}
|
||||
|
||||
const isPublicKey = decoded.length === 35;
|
||||
const [isValid] = checkChecksum(decoded);
|
||||
|
||||
// non-publicKeys has 1 byte checksums, else default to 2
|
||||
const endPos = decoded.length - (isPublicKey ? 2 : 1);
|
||||
|
||||
// calculate the hash and do the checksum byte checks
|
||||
const hash = sshash(decoded.subarray(0, endPos));
|
||||
const checks = isPublicKey
|
||||
? decoded[decoded.length - 2] === hash[0] && decoded[decoded.length - 1] === hash[1]
|
||||
: decoded[decoded.length - 1] === hash[0];
|
||||
|
||||
return !checks
|
||||
? [false, 'Invalid decoded address checksum']
|
||||
: [true, null];
|
||||
return [isValid, isValid ? null : 'Invalid decoded address checksum'];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import sshash from './sshash';
|
||||
|
||||
export default function checkChecksum (decoded: Uint8Array): [boolean, number] {
|
||||
const isPublicKey = decoded.length === 35;
|
||||
|
||||
// non-publicKeys has 1 byte checksums, else default to 2
|
||||
const length = decoded.length - (isPublicKey ? 2 : 1);
|
||||
|
||||
// calculate the hash and do the checksum byte checks
|
||||
const hash = sshash(decoded.subarray(0, length));
|
||||
|
||||
// see if the hash actually matches
|
||||
const isValid = isPublicKey
|
||||
? decoded[decoded.length - 2] === hash[0] && decoded[decoded.length - 1] === hash[1]
|
||||
: decoded[decoded.length - 1] === hash[0];
|
||||
|
||||
return [isValid, length];
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import { Prefix } from './types';
|
||||
import bs58 from 'bs58';
|
||||
import { assert, bufferToU8a, isHex, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import checkChecksum from './checkChecksum';
|
||||
import defaults from './defaults';
|
||||
import sshash from './sshash';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export default function decode (encoded: string | Uint8Array, ignoreChecksum?: boolean, ss58Format: Prefix = 99): Uint8Array {
|
||||
@@ -30,18 +30,9 @@ export default function decode (encoded: string | Uint8Array, ignoreChecksum?: b
|
||||
// console.log(`WARN: Expected ${prefix}, found ${decoded[0]}`);
|
||||
// }
|
||||
|
||||
const isPublicKey = decoded.length === 35;
|
||||
const [isValid, endPos] = checkChecksum(decoded);
|
||||
|
||||
// non-publicKeys has 1 byte checksums, else default to 2
|
||||
const endPos = decoded.length - (isPublicKey ? 2 : 1);
|
||||
|
||||
// calculate the hash and do the checksum byte checks
|
||||
const hash = sshash(decoded.subarray(0, endPos));
|
||||
const checks = isPublicKey
|
||||
? decoded[decoded.length - 2] === hash[0] && decoded[decoded.length - 1] === hash[1]
|
||||
: decoded[decoded.length - 1] === hash[0];
|
||||
|
||||
assert(ignoreChecksum || checks, error('Invalid decoded address checksum'));
|
||||
assert(ignoreChecksum || isValid, error('Invalid decoded address checksum'));
|
||||
|
||||
return decoded.slice(1, endPos);
|
||||
}
|
||||
|
||||
@@ -3,12 +3,14 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import checkAddress from './check';
|
||||
import checkAddressChecksum from './checkChecksum';
|
||||
import decodeAddress from './decode';
|
||||
import encodeAddress from './encode';
|
||||
import setSS58Format from './setSS58Format';
|
||||
|
||||
export {
|
||||
checkAddress,
|
||||
checkAddressChecksum,
|
||||
decodeAddress,
|
||||
encodeAddress,
|
||||
setSS58Format
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
// polyfills
|
||||
import './polyfill';
|
||||
|
||||
import wasmCrypto from '@polkadot/wasm-crypto';
|
||||
// out of order and early in the chain, this also kicks off initialization on require
|
||||
export * from './init';
|
||||
|
||||
// all internal exports
|
||||
export * from './address';
|
||||
export * from './blake2';
|
||||
export * from './keccak';
|
||||
@@ -16,20 +19,5 @@ export * from './random';
|
||||
export * from './schnorrkel';
|
||||
export * from './secp256k1';
|
||||
export * from './sha512';
|
||||
export * from './signature';
|
||||
export * from './xxhash';
|
||||
|
||||
export function cryptoWaitReady (): Promise<boolean> {
|
||||
return wasmCrypto
|
||||
.waitReady()
|
||||
.then((): boolean => true)
|
||||
.catch((error): boolean => {
|
||||
console.error('Unable to initialize @polkadot/util-crypto', error);
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
// start init process immediately
|
||||
cryptoWaitReady().catch((): void => {
|
||||
// shouldn't happen, logged above
|
||||
});
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import wasmCrypto from '@polkadot/wasm-crypto';
|
||||
|
||||
export function cryptoIsReady (): boolean {
|
||||
return wasmCrypto.isReady();
|
||||
}
|
||||
|
||||
export function cryptoWaitReady (): Promise<boolean> {
|
||||
return wasmCrypto
|
||||
.waitReady()
|
||||
.then((): boolean => true)
|
||||
.catch((error): boolean => {
|
||||
console.error('Unable to initialize @polkadot/util-crypto', error);
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
// start init process immediately
|
||||
cryptoWaitReady().catch((): void => {
|
||||
// shouldn't happen, logged above
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
/**
|
||||
* @summary Utilities for working with signatures
|
||||
*/
|
||||
|
||||
export { default as signatureVerify } from './verify';
|
||||
@@ -0,0 +1,92 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { hexToU8a, stringToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { decodeAddress } from '../address';
|
||||
import { signatureVerify } from '.';
|
||||
|
||||
const ADDR_ED = 'DxN4uvzwPzJLtn17yew6jEffPhXQfdKHTp2brufb98vGbPN';
|
||||
const ADDR_SR = 'EK1bFgKm2FsghcttHT7TB7rNyXApFgs9fCbijMGQNyFGBQm';
|
||||
const MESSAGE = 'hello world';
|
||||
const SIG_ED = '0x299d3bf4c8bb51af732f8067b3a3015c0862a5ff34721749d8ed6577ea2708365d1c5f76bd519009971e41156f12c70abc2533837ceb3bad9a05a99ab923de06';
|
||||
const SIG_SR = '0xca01419b5a17219f7b78335658cab3b126db523a5df7be4bfc2bef76c2eb3b1dcf4ca86eb877d0a6cf6df12db5995c51d13b00e005d053b892bd09c594434288';
|
||||
const MUL_ED = u8aToHex(u8aConcat(new Uint8Array([0]), hexToU8a(SIG_ED)));
|
||||
const MUL_SR = u8aToHex(u8aConcat(new Uint8Array([1]), hexToU8a(SIG_SR)));
|
||||
|
||||
describe('signatureVerify', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('throws on invalid signature length', (): void => {
|
||||
expect(
|
||||
() => signatureVerify(MESSAGE, new Uint8Array(32), ADDR_ED)
|
||||
).toThrow('Invalid signature length, expected 64 or 65 bytes, found 32');
|
||||
});
|
||||
|
||||
describe('verifyDetect', (): void => {
|
||||
it('verifies an ed25519 signature', (): void => {
|
||||
expect(signatureVerify(MESSAGE, SIG_ED, ADDR_ED)).toEqual({
|
||||
crypto: 'ed25519',
|
||||
isValid: true
|
||||
});
|
||||
});
|
||||
|
||||
it('verifies an sr25519 signature', (): void => {
|
||||
expect(signatureVerify(MESSAGE, SIG_SR, ADDR_SR)).toEqual({
|
||||
crypto: 'sr25519',
|
||||
isValid: true
|
||||
});
|
||||
});
|
||||
|
||||
it('allows various inputs', (): void => {
|
||||
expect(signatureVerify(stringToU8a(MESSAGE), hexToU8a(SIG_ED), decodeAddress(ADDR_ED))).toEqual({
|
||||
crypto: 'ed25519',
|
||||
isValid: true
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on an invalid signature', (): void => {
|
||||
expect(signatureVerify(MESSAGE, SIG_SR, ADDR_ED)).toEqual({
|
||||
crypto: 'none',
|
||||
isValid: false
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('verifyMultisig', (): void => {
|
||||
it('throws with invalid multisig indicator', (): void => {
|
||||
const u8aSig = hexToU8a(MUL_ED);
|
||||
|
||||
u8aSig[0] = 69;
|
||||
|
||||
expect(
|
||||
() => signatureVerify(MESSAGE, u8aSig, ADDR_ED)
|
||||
).toThrow('Unknown crypto type, expected signature prefix of 0 or 1, found 69');
|
||||
});
|
||||
|
||||
it('verifies an ed25519 signature', (): void => {
|
||||
expect(signatureVerify(MESSAGE, MUL_ED, ADDR_ED)).toEqual({
|
||||
crypto: 'ed25519',
|
||||
isValid: true
|
||||
});
|
||||
});
|
||||
|
||||
it('verifies an sr25519 signature', (): void => {
|
||||
expect(signatureVerify(MESSAGE, MUL_SR, ADDR_SR)).toEqual({
|
||||
crypto: 'sr25519',
|
||||
isValid: true
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on an invalid signature', (): void => {
|
||||
expect(signatureVerify(MESSAGE, MUL_SR, ADDR_ED)).toEqual({
|
||||
crypto: 'sr25519',
|
||||
isValid: false
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { KeypairType, VerifyResult } from '../types';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import addressDecode from '../address/decode';
|
||||
import naclVerify from '../nacl/verify';
|
||||
import schnorrkelVerify from '../schnorrkel/verify';
|
||||
|
||||
const VERIFIERS: [KeypairType, (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string) => boolean][] = [
|
||||
['ed25519', naclVerify],
|
||||
['sr25519', schnorrkelVerify]
|
||||
];
|
||||
|
||||
function verifyDetect (result: VerifyResult, message: Uint8Array | string, signature: Uint8Array, publicKey: Uint8Array): VerifyResult {
|
||||
result.isValid = VERIFIERS.some(([crypto, verify]): boolean => {
|
||||
try {
|
||||
if (verify(message, signature, publicKey)) {
|
||||
result.crypto = crypto;
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
// do nothing, result.isValid still set to false
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function verifyMultisig (result: VerifyResult, message: Uint8Array | string, signature: Uint8Array, publicKey: Uint8Array): VerifyResult {
|
||||
assert([0, 1].includes(signature[0]), `Unknown crypto type, expected signature prefix of 0 or 1, found ${signature[0]}`);
|
||||
|
||||
const isEd25519 = signature[0] === 0;
|
||||
|
||||
result.crypto = isEd25519
|
||||
? 'ed25519'
|
||||
: 'sr25519';
|
||||
|
||||
try {
|
||||
result.isValid = isEd25519
|
||||
? naclVerify(message, signature.subarray(1), publicKey)
|
||||
: schnorrkelVerify(message, signature.subarray(1), publicKey);
|
||||
} catch (error) {
|
||||
// ignore, result.isValid still set to false
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export default function signatureVerify (message: Uint8Array | string, signature: Uint8Array | string, addressOrPublicKey: Uint8Array | string): VerifyResult {
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
assert([64, 65].includes(signatureU8a.length), `Invalid signature length, expected 64 or 65 bytes, found ${signatureU8a.length}`);
|
||||
|
||||
const result: VerifyResult = { crypto: 'none', isValid: false };
|
||||
const publicKey = addressDecode(addressOrPublicKey);
|
||||
|
||||
return signatureU8a.length === 65
|
||||
? verifyMultisig(result, message, signatureU8a, publicKey)
|
||||
: verifyDetect(result, message, signatureU8a, publicKey);
|
||||
}
|
||||
@@ -13,3 +13,8 @@ export interface Seedpair {
|
||||
}
|
||||
|
||||
export type KeypairType = 'ed25519' | 'sr25519';
|
||||
|
||||
export interface VerifyResult {
|
||||
crypto: 'none' | KeypairType;
|
||||
isValid: boolean;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "2.5.1",
|
||||
"version": "2.6.2",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -25,7 +25,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.8.4",
|
||||
"@babel/runtime": "^7.8.7",
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"bn.js": "^5.1.1",
|
||||
"camelcase": "^5.3.1",
|
||||
|
||||
@@ -13,9 +13,9 @@ import hexToBn from '../hex/toBn';
|
||||
|
||||
/**
|
||||
* @name bnToBn
|
||||
* @summary Creates a BN value from a BN.js bignumber or number input.
|
||||
* @summary Creates a BN value from a BN, BigInt, string (base 10 or hex) or number input.
|
||||
* @description
|
||||
* `null` inputs returns a `0x0` result, BN values returns the value, numnbers returns a BN representation.
|
||||
* `null` inputs returns a `0x0` result, BN values returns the value, numbers returns a BN representation.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
|
||||
@@ -26,7 +26,7 @@ type PjsGlobal = NodeJS.Global & PjsChecks;
|
||||
type PjsWindow = Window & PjsChecks;
|
||||
|
||||
function expandPath (path?: string): string {
|
||||
return (!path || path.length < 5) ? '<unknown path>' : path;
|
||||
return (!path || path.length < 5) ? '<unknown>' : path;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -47,7 +47,7 @@ function flattenVersions (_all: (VersionPath | string)[]): string {
|
||||
* @name detectPackage
|
||||
* @summary Checks that a specific package is only imported once
|
||||
*/
|
||||
export default function detectPackage ({ name, version }: PackageJson, path?: string): void {
|
||||
export default function detectPackage ({ name, version }: PackageJson, path?: string | false): void {
|
||||
const _global = typeof window !== 'undefined'
|
||||
? window as PjsWindow
|
||||
: global as PjsGlobal;
|
||||
@@ -58,7 +58,7 @@ export default function detectPackage ({ name, version }: PackageJson, path?: st
|
||||
|
||||
assert(name.startsWith('@polkadot'), `Invalid package descriptor ${name}`);
|
||||
|
||||
_global.__polkadotjs[name] = [...(_global.__polkadotjs[name] || []), { path, version }];
|
||||
_global.__polkadotjs[name] = [...(_global.__polkadotjs[name] || []), { path: path || '', version }];
|
||||
|
||||
if (_global.__polkadotjs[name].length !== 1) {
|
||||
const versions = flattenVersions(_global.__polkadotjs[name]);
|
||||
|
||||
@@ -25,19 +25,11 @@ export default function hexFixLength (value: string, bitLength = -1, withPadding
|
||||
const strLength = Math.ceil(bitLength / 4);
|
||||
const hexLength = strLength + 2;
|
||||
|
||||
if (bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength)) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value)
|
||||
);
|
||||
}
|
||||
|
||||
if (value.length > hexLength) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value).slice(-1 * strLength)
|
||||
);
|
||||
}
|
||||
|
||||
return hexAddPrefix(
|
||||
`${'0'.repeat(strLength)}${hexStripPrefix(value)}`.slice(-1 * strLength)
|
||||
(bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength))
|
||||
? hexStripPrefix(value)
|
||||
: (value.length > hexLength)
|
||||
? hexStripPrefix(value).slice(-1 * strLength)
|
||||
: `${'0'.repeat(strLength)}${hexStripPrefix(value)}`.slice(-1 * strLength)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,9 +19,7 @@ import hexToBn from './toBn';
|
||||
* ```
|
||||
*/
|
||||
export default function hexToNumber (value?: string | null): number {
|
||||
if (!value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return hexToBn(value).toNumber();
|
||||
return value
|
||||
? hexToBn(value).toNumber()
|
||||
: NaN;
|
||||
}
|
||||
|
||||
@@ -26,9 +26,5 @@ export default function numberToHex (value?: number | null, bitLength = -1): str
|
||||
return '0x';
|
||||
}
|
||||
|
||||
return hexFixLength(
|
||||
(value || 0).toString(16),
|
||||
bitLength,
|
||||
true
|
||||
);
|
||||
return hexFixLength(value.toString(16), bitLength, true);
|
||||
}
|
||||
|
||||
@@ -17,9 +17,7 @@
|
||||
* ```
|
||||
*/
|
||||
export default function stringLowerFirst (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return value.charAt(0).toLowerCase() + value.slice(1);
|
||||
return value
|
||||
? value.charAt(0).toLowerCase() + value.slice(1)
|
||||
: '';
|
||||
}
|
||||
|
||||
@@ -37,9 +37,7 @@ try {
|
||||
* ```
|
||||
*/
|
||||
export default function stringToU8a (value?: string): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
return encoder.encode(value);
|
||||
return value
|
||||
? encoder.encode(value)
|
||||
: new Uint8Array([]);
|
||||
}
|
||||
|
||||
@@ -17,9 +17,7 @@
|
||||
* ```
|
||||
*/
|
||||
export default function stringUpperFirst (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return value.charAt(0).toUpperCase() + value.slice(1);
|
||||
return value
|
||||
? value.charAt(0).toUpperCase() + value.slice(1)
|
||||
: '';
|
||||
}
|
||||
|
||||
@@ -23,9 +23,7 @@ export default function u8aFixLength (value: Uint8Array, bitLength = -1, atStart
|
||||
|
||||
if (bitLength === -1 || value.length === byteLength) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value.length > byteLength) {
|
||||
} else if (value.length > byteLength) {
|
||||
return value.subarray(0, byteLength);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,7 @@ import { ToBnOptions } from '../types';
|
||||
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
* ```
|
||||
*/
|
||||
export default function u8aToBn (
|
||||
value: Uint8Array,
|
||||
options: ToBnOptions | boolean = { isLe: true, isNegative: false }
|
||||
): BN {
|
||||
export default function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = { isLe: true, isNegative: false }): BN {
|
||||
return hexToBn(
|
||||
u8aToHex(value),
|
||||
options
|
||||
|
||||
@@ -17,9 +17,7 @@
|
||||
* ```
|
||||
*/
|
||||
export default function u8aToBuffer (value?: Uint8Array | null): Buffer {
|
||||
if (!value) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return Buffer.from(value);
|
||||
return !value
|
||||
? Buffer.from([])
|
||||
: Buffer.from(value);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function u8aToHex (value?: Uint8Array | null, bitLength = -1, isP
|
||||
? '0x'
|
||||
: '';
|
||||
|
||||
if (!value || !value.length) {
|
||||
if (!value?.length) {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,9 +33,7 @@ try {
|
||||
* ```
|
||||
*/
|
||||
export default function u8aToString (value?: Uint8Array | null): string {
|
||||
if (!value || !value.length) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return decoder.decode(value);
|
||||
return !value?.length
|
||||
? ''
|
||||
: decoder.decode(value);
|
||||
}
|
||||
|
||||
@@ -27,21 +27,15 @@ import stringToU8a from '../string/toU8a';
|
||||
export default function u8aToU8a (value?: number[] | Buffer | Uint8Array | string | null): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
if (isBuffer(value)) {
|
||||
} else if (isBuffer(value)) {
|
||||
return bufferToU8a(value);
|
||||
}
|
||||
|
||||
if (isString(value)) {
|
||||
} else if (isString(value)) {
|
||||
return isHex(value)
|
||||
? hexToU8a(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return Uint8Array.from(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
return Array.isArray(value)
|
||||
? Uint8Array.from(value)
|
||||
: value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user