Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a598fdf92 | ||
|
|
659b896ee7 | ||
|
|
7cda007a30 | ||
|
|
2288c4da8a | ||
|
|
fbfc21be1c | ||
|
|
787beb2aea | ||
|
|
6234e16c37 | ||
|
|
e9cbc0a0d9 | ||
|
|
166eeb8852 | ||
|
|
b321f92161 | ||
|
|
cf7d9d9ff5 | ||
|
|
fb5b3c1663 | ||
|
|
46bbc45145 | ||
|
|
dc8b4a6447 | ||
|
|
8ec38eca4a | ||
|
|
24b533689c | ||
|
|
5421e06fc9 | ||
|
|
7115efd94f | ||
|
|
92f88222c4 | ||
|
|
bc7aba71a6 | ||
|
|
1d8b8b3204 | ||
|
|
4a2e090ef8 | ||
|
|
8cf8d5b7e8 | ||
|
|
8d355ff6fc |
@@ -1,5 +1,23 @@
|
|||||||
# CHANGELOG
|
# CHANGELOG
|
||||||
|
|
||||||
|
## 2.18.1 Jul 13, 2020
|
||||||
|
|
||||||
|
- Add `base64{Decode, Encode, Validate}` as crypto utils
|
||||||
|
- Extract `base58Validate` from base58 decode checks
|
||||||
|
- Add `isAscii` to allow detection of printable ASCII sequences (including tab, newline)
|
||||||
|
- Add `isUtf8` to allow detection of valid Utf8 sequences
|
||||||
|
|
||||||
|
## 2.17.1 Jul 6, 2020
|
||||||
|
|
||||||
|
- Add `encode{Derived, Multi}Address` to encode derived/multi addresses
|
||||||
|
- Correctly handle 0 inputs in `numberToU8a`
|
||||||
|
- Simplify and flatten base58 dependencies
|
||||||
|
|
||||||
|
## 2.16.1 Jun 29, 2020
|
||||||
|
|
||||||
|
- Adjust `addressCheck` and `encodeAddress` function to check for valid base58 alphabets (better error reporting)
|
||||||
|
- Rename `createKeySub` to `createKeyDerived` based on changed in Substrate (sub keys now named derived)
|
||||||
|
|
||||||
## 2.15.1 Jun 22, 2020
|
## 2.15.1 Jun 22, 2020
|
||||||
|
|
||||||
- Adjust keyring to no override 0-prefix (Polkadot live) settings with keychain default
|
- Adjust keyring to no override 0-prefix (Polkadot live) settings with keychain default
|
||||||
|
|||||||
+1
-1
@@ -9,5 +9,5 @@
|
|||||||
"packages": [
|
"packages": [
|
||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
],
|
||||||
"version": "2.15.1"
|
"version": "2.18.1"
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -21,10 +21,10 @@
|
|||||||
"test:one": "polkadot-dev-run-test"
|
"test:one": "polkadot-dev-run-test"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.10.3",
|
"@babel/core": "^7.10.4",
|
||||||
"@polkadot/dev": "^0.55.11",
|
"@polkadot/dev": "^0.55.21",
|
||||||
"@polkadot/ts": "^0.3.26",
|
"@polkadot/ts": "^0.3.27",
|
||||||
"@types/jest": "^26.0.0"
|
"@types/jest": "^26.0.4"
|
||||||
},
|
},
|
||||||
"version": "2.15.1"
|
"version": "2.18.1"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@polkadot/keyring",
|
"name": "@polkadot/keyring",
|
||||||
"version": "2.15.1",
|
"version": "2.18.1",
|
||||||
"description": "Keyring management",
|
"description": "Keyring management",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
@@ -27,8 +27,8 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/runtime": "^7.10.3",
|
"@babel/runtime": "^7.10.4",
|
||||||
"@polkadot/util": "2.15.1",
|
"@polkadot/util": "2.18.1",
|
||||||
"@polkadot/util-crypto": "2.15.1"
|
"@polkadot/util-crypto": "2.18.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@polkadot/util-crypto",
|
"name": "@polkadot/util-crypto",
|
||||||
"version": "2.15.1",
|
"version": "2.18.1",
|
||||||
"description": "A collection of useful crypto utilities for @polkadot",
|
"description": "A collection of useful crypto utilities for @polkadot",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@@ -25,14 +25,13 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/runtime": "^7.10.3",
|
"@babel/runtime": "^7.10.4",
|
||||||
"@polkadot/util": "2.15.1",
|
"@polkadot/util": "2.18.1",
|
||||||
"@polkadot/wasm-crypto": "^1.2.1",
|
"@polkadot/wasm-crypto": "^1.2.1",
|
||||||
"base-x": "^3.0.8",
|
"base-x": "^3.0.8",
|
||||||
"bip39": "^3.0.2",
|
"bip39": "^3.0.2",
|
||||||
"blakejs": "^1.1.0",
|
"blakejs": "^1.1.0",
|
||||||
"bn.js": "^5.1.2",
|
"bn.js": "^5.1.2",
|
||||||
"bs58": "^4.0.1",
|
|
||||||
"elliptic": "^6.5.3",
|
"elliptic": "^6.5.3",
|
||||||
"js-sha3": "^0.8.0",
|
"js-sha3": "^0.8.0",
|
||||||
"pbkdf2": "^3.1.1",
|
"pbkdf2": "^3.1.1",
|
||||||
@@ -41,7 +40,6 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bn.js": "^4.11.6",
|
"@types/bn.js": "^4.11.6",
|
||||||
"@types/bs58": "^4.0.1",
|
|
||||||
"@types/elliptic": "^6.4.12",
|
"@types/elliptic": "^6.4.12",
|
||||||
"@types/pbkdf2": "^3.0.0",
|
"@types/pbkdf2": "^3.0.0",
|
||||||
"@types/xxhashjs": "^0.2.2"
|
"@types/xxhashjs": "^0.2.2"
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ describe('checkAddress', (): void => {
|
|||||||
).toEqual([true, null]);
|
).toEqual([true, null]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('fails when an invalid base58 character is supplied', (): void => {
|
||||||
|
expect(
|
||||||
|
checkAddress('5EnxIUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG', 2)
|
||||||
|
).toEqual([false, 'Invalid base58 character "I" (0x49) at index 4']);
|
||||||
|
});
|
||||||
|
|
||||||
it('fails with invalid prefix when checking Substrate against Kusama prefix', (): void => {
|
it('fails with invalid prefix when checking Substrate against Kusama prefix', (): void => {
|
||||||
expect(
|
expect(
|
||||||
checkAddress('5EnxxUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG', 2)
|
checkAddress('5EnxxUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG', 2)
|
||||||
|
|||||||
@@ -4,13 +4,24 @@
|
|||||||
|
|
||||||
import { Prefix } from './types';
|
import { Prefix } from './types';
|
||||||
|
|
||||||
import bs58 from 'bs58';
|
import base58Decode from '../base58/decode';
|
||||||
|
|
||||||
import checkChecksum from './checkChecksum';
|
import checkChecksum from './checkChecksum';
|
||||||
import defaults from './defaults';
|
import defaults from './defaults';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name checkAddress
|
||||||
|
* @summary Validates an ss58 address.
|
||||||
|
* @description
|
||||||
|
* From the provided input, validate that the address is a valid input.
|
||||||
|
*/
|
||||||
export default function check (address: string, prefix: Prefix): [boolean, string | null] {
|
export default function check (address: string, prefix: Prefix): [boolean, string | null] {
|
||||||
const decoded = bs58.decode(address);
|
let decoded;
|
||||||
|
|
||||||
|
try {
|
||||||
|
decoded = base58Decode(address);
|
||||||
|
} catch (error) {
|
||||||
|
return [false, (error as Error).message];
|
||||||
|
}
|
||||||
|
|
||||||
if (decoded[0] !== prefix) {
|
if (decoded[0] !== prefix) {
|
||||||
return [false, `Prefix mismatch, expected ${prefix}, found ${decoded[0]}`];
|
return [false, `Prefix mismatch, expected ${prefix}, found ${decoded[0]}`];
|
||||||
|
|||||||
@@ -94,4 +94,10 @@ describe('decode', (): void => {
|
|||||||
(): Uint8Array => decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU')
|
(): Uint8Array => decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU')
|
||||||
).toThrow(/address checksum/);
|
).toThrow(/address checksum/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('fails when invalid base58 encoded address is found', (): void => {
|
||||||
|
expect(
|
||||||
|
() => u8aToHex(decode('F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29'))
|
||||||
|
).toThrow(/Decoding F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29: Invalid base58 character "I" \(0x49\) at index 4/);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import { Prefix } from './types';
|
|||||||
|
|
||||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6
|
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6
|
||||||
|
|
||||||
import bs58 from 'bs58';
|
import { assert, isHex, isU8a, u8aToU8a } from '@polkadot/util';
|
||||||
import { assert, bufferToU8a, isHex, isU8a, u8aToU8a } from '@polkadot/util';
|
|
||||||
|
|
||||||
|
import base58Decode from '../base58/decode';
|
||||||
import checkChecksum from './checkChecksum';
|
import checkChecksum from './checkChecksum';
|
||||||
import defaults from './defaults';
|
import defaults from './defaults';
|
||||||
|
|
||||||
@@ -18,11 +18,17 @@ export default function decode (encoded: string | Uint8Array, ignoreChecksum?: b
|
|||||||
return u8aToU8a(encoded);
|
return u8aToU8a(encoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
const decoded = bufferToU8a(bs58.decode(encoded));
|
const wrapError = (message: string) => `Decoding ${encoded as string}: ${message}`;
|
||||||
const error = (message: string) => `Decoding ${encoded as string}: ${message}`;
|
let decoded;
|
||||||
|
|
||||||
|
try {
|
||||||
|
decoded = base58Decode(encoded);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(wrapError((error as Error).message));
|
||||||
|
}
|
||||||
|
|
||||||
// assert(defaults.allowedPrefix.includes(decoded[0] as Prefix), error('Invalid decoded address prefix'));
|
// assert(defaults.allowedPrefix.includes(decoded[0] as Prefix), error('Invalid decoded address prefix'));
|
||||||
assert(defaults.allowedEncodedLengths.includes(decoded.length), error('Invalid decoded address length'));
|
assert(defaults.allowedEncodedLengths.includes(decoded.length), wrapError('Invalid decoded address length'));
|
||||||
|
|
||||||
// TODO Unless it is an "use everywhere" prefix, throw an error
|
// TODO Unless it is an "use everywhere" prefix, throw an error
|
||||||
// if (decoded[0] !== prefix) {
|
// if (decoded[0] !== prefix) {
|
||||||
@@ -31,7 +37,7 @@ export default function decode (encoded: string | Uint8Array, ignoreChecksum?: b
|
|||||||
|
|
||||||
const [isValid, endPos] = checkChecksum(decoded);
|
const [isValid, endPos] = checkChecksum(decoded);
|
||||||
|
|
||||||
assert(ignoreChecksum || isValid, error('Invalid decoded address checksum'));
|
assert(ignoreChecksum || isValid, wrapError('Invalid decoded address checksum'));
|
||||||
|
|
||||||
return decoded.slice(1, endPos);
|
return decoded.slice(1, endPos);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import { Prefix } from './types';
|
|||||||
|
|
||||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
|
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
|
||||||
|
|
||||||
import bs58 from 'bs58';
|
import { assert, u8aConcat } from '@polkadot/util';
|
||||||
import { assert, u8aConcat, u8aToBuffer } from '@polkadot/util';
|
|
||||||
|
|
||||||
|
import base58Encode from '../base58/encode';
|
||||||
import decode from './decode';
|
import decode from './decode';
|
||||||
import defaults from './defaults';
|
import defaults from './defaults';
|
||||||
import sshash from './sshash';
|
import sshash from './sshash';
|
||||||
@@ -23,9 +23,7 @@ export default function encode (_key: Uint8Array | string, ss58Format: Prefix =
|
|||||||
const input = u8aConcat(new Uint8Array([ss58Format]), key);
|
const input = u8aConcat(new Uint8Array([ss58Format]), key);
|
||||||
const hash = sshash(input);
|
const hash = sshash(input);
|
||||||
|
|
||||||
return bs58.encode(
|
return base58Encode(
|
||||||
u8aToBuffer(
|
u8aConcat(input, hash.subarray(0, isPublicKey ? 2 : 1))
|
||||||
u8aConcat(input, hash.subarray(0, isPublicKey ? 2 : 1))
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// 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 encodeDerivedAddress from './encodeDerived';
|
||||||
|
|
||||||
|
describe('encodeDerivedAddress', (): void => {
|
||||||
|
it('creates a valid known derived address', (): void => {
|
||||||
|
expect(
|
||||||
|
encodeDerivedAddress('5GvUh7fGKsdBEh5XpypkfkGuf7j3vXLxH9BdxjxnJNVXRYi1', 0)
|
||||||
|
).toEqual('5E5XxqPxm7QbEs6twYfp3tyjXidn4kqRrNPH4o6JK9JSLUeD');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// 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 { Prefix } from './types';
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
|
|
||||||
|
import decode from './decode';
|
||||||
|
import encode from './encode';
|
||||||
|
import keyDerived from './keyDerived';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name encodeDerivedAddress
|
||||||
|
* @summary Creates a derived address.
|
||||||
|
* @description
|
||||||
|
* Creates a Substrate derived address based on the input address/publicKey and the index supplied.
|
||||||
|
*/
|
||||||
|
export default function encodeDerivedAddress (who: Uint8Array | string, index: BigInt | BN | number, ss58Format?: Prefix): string {
|
||||||
|
return encode(keyDerived(decode(who), index), ss58Format);
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// 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 encodeMultiAddress from './encodeMulti';
|
||||||
|
|
||||||
|
describe('encodeMultiAddress', (): void => {
|
||||||
|
it('creates a valid known multi address', (): void => {
|
||||||
|
expect(
|
||||||
|
encodeMultiAddress([
|
||||||
|
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
|
||||||
|
'5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
|
||||||
|
'5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'
|
||||||
|
], 2)
|
||||||
|
).toEqual('5DjYJStmdZ2rcqXbXGX7TW85JsrW6uG4y9MUcLq2BoPMpRA7');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// 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 { Prefix } from './types';
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
|
|
||||||
|
import encode from './encode';
|
||||||
|
import keyMulti from './keyMulti';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name encodeMultiAddress
|
||||||
|
* @summary Creates a multisig address.
|
||||||
|
* @description
|
||||||
|
* Creates a Substrate multisig address based on the input address and the required threshold.
|
||||||
|
*/
|
||||||
|
export default function encodeMultiAddress (who: (Uint8Array | string)[], threshold: BigInt | BN | number, ss58Format?: Prefix): string {
|
||||||
|
return encode(keyMulti(who, threshold), ss58Format);
|
||||||
|
}
|
||||||
@@ -5,9 +5,11 @@
|
|||||||
import checkAddress from './check';
|
import checkAddress from './check';
|
||||||
import checkAddressChecksum from './checkChecksum';
|
import checkAddressChecksum from './checkChecksum';
|
||||||
import createKeyMulti from './keyMulti';
|
import createKeyMulti from './keyMulti';
|
||||||
import createKeySub from './keySub';
|
import createKeyDerived from './keyDerived';
|
||||||
import decodeAddress from './decode';
|
import decodeAddress from './decode';
|
||||||
import encodeAddress from './encode';
|
import encodeAddress from './encode';
|
||||||
|
import encodeDerivedAddress from './encodeDerived';
|
||||||
|
import encodeMultiAddress from './encodeMulti';
|
||||||
import addressEq from './eq';
|
import addressEq from './eq';
|
||||||
import setSS58Format from './setSS58Format';
|
import setSS58Format from './setSS58Format';
|
||||||
import sortAddresses from './sort';
|
import sortAddresses from './sort';
|
||||||
@@ -16,10 +18,12 @@ export {
|
|||||||
addressEq,
|
addressEq,
|
||||||
checkAddress,
|
checkAddress,
|
||||||
checkAddressChecksum,
|
checkAddressChecksum,
|
||||||
|
createKeyDerived,
|
||||||
createKeyMulti,
|
createKeyMulti,
|
||||||
createKeySub,
|
|
||||||
decodeAddress,
|
decodeAddress,
|
||||||
encodeAddress,
|
encodeAddress,
|
||||||
|
encodeDerivedAddress,
|
||||||
|
encodeMultiAddress,
|
||||||
setSS58Format,
|
setSS58Format,
|
||||||
sortAddresses
|
sortAddresses
|
||||||
};
|
};
|
||||||
|
|||||||
+4
-4
@@ -2,12 +2,12 @@
|
|||||||
// This software may be modified and distributed under the terms
|
// This software may be modified and distributed under the terms
|
||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import createKeySub from './keySub';
|
import createKeyDerived from './keyDerived';
|
||||||
|
|
||||||
describe('createKeySub', (): void => {
|
describe('createKeyDerived', (): void => {
|
||||||
it('matches sub accounts with Rust', (): void => {
|
it('matches sub accounts with Rust', (): void => {
|
||||||
expect(
|
expect(
|
||||||
createKeySub(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]), 0)
|
createKeyDerived(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]), 0)
|
||||||
).toEqual(
|
).toEqual(
|
||||||
new Uint8Array([234, 236, 28, 96, 177, 168, 152, 193, 71, 179, 226, 102, 179, 155, 188, 240, 90, 182, 21, 175, 47, 47, 250, 179, 178, 0, 81, 222, 70, 56, 52, 234])
|
new Uint8Array([234, 236, 28, 96, 177, 168, 152, 193, 71, 179, 226, 102, 179, 155, 188, 240, 90, 182, 21, 175, 47, 47, 250, 179, 178, 0, 81, 222, 70, 56, 52, 234])
|
||||||
);
|
);
|
||||||
@@ -15,7 +15,7 @@ describe('createKeySub', (): void => {
|
|||||||
|
|
||||||
it('creates a valid subkey', (): void => {
|
it('creates a valid subkey', (): void => {
|
||||||
expect(
|
expect(
|
||||||
createKeySub('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 1)
|
createKeyDerived('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 1)
|
||||||
).toEqual(
|
).toEqual(
|
||||||
new Uint8Array([248, 19, 86, 209, 254, 89, 84, 48, 54, 128, 166, 239, 153, 212, 143, 34, 191, 60, 210, 50, 39, 77, 122, 71, 29, 60, 247, 198, 95, 101, 246, 83])
|
new Uint8Array([248, 19, 86, 209, 254, 89, 84, 48, 54, 128, 166, 239, 153, 212, 143, 34, 191, 60, 210, 50, 39, 77, 122, 71, 29, 60, 247, 198, 95, 101, 246, 83])
|
||||||
);
|
);
|
||||||
+1
-1
@@ -10,7 +10,7 @@ import decodeAddress from './decode';
|
|||||||
|
|
||||||
const PREFIX = stringToU8a('modlpy/utilisuba');
|
const PREFIX = stringToU8a('modlpy/utilisuba');
|
||||||
|
|
||||||
export default function createKeySub (who: Uint8Array | string, index: BigInt | BN | number): Uint8Array {
|
export default function createKeyDerived (who: Uint8Array | string, index: BigInt | BN | number): Uint8Array {
|
||||||
return blake2AsU8a(
|
return blake2AsU8a(
|
||||||
u8aConcat(
|
u8aConcat(
|
||||||
PREFIX,
|
PREFIX,
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// 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 baseX from 'base-x';
|
||||||
|
|
||||||
|
// https://github.com/cryptocoinjs/base-x#alphabets
|
||||||
|
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
||||||
|
|
||||||
|
const bs58 = baseX(BASE58_ALPHABET);
|
||||||
|
|
||||||
|
export {
|
||||||
|
BASE58_ALPHABET,
|
||||||
|
bs58
|
||||||
|
};
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// 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 { bufferToU8a } from '@polkadot/util';
|
||||||
|
|
||||||
|
import { bs58 } from './bs58';
|
||||||
|
import validate from './validate';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name base58Decode
|
||||||
|
* @summary Decodes a base58 value.
|
||||||
|
* @description
|
||||||
|
* From the provided input, decode the base58 and return the result as an `Uint8Array`.
|
||||||
|
*/
|
||||||
|
export default function base58Decode (value: string): Uint8Array {
|
||||||
|
validate(value);
|
||||||
|
|
||||||
|
return bufferToU8a(bs58.decode(value));
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// 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 { u8aToBuffer, u8aToU8a } from '@polkadot/util';
|
||||||
|
|
||||||
|
import { bs58 } from './bs58';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name base58Encode
|
||||||
|
* @summary Creates a base58 value.
|
||||||
|
* @description
|
||||||
|
* From the provided input, create the base58 and return the result as a string.
|
||||||
|
*/
|
||||||
|
export default function base58Encode (value: Uint8Array | string | Buffer | number[]): string {
|
||||||
|
return bs58.encode(
|
||||||
|
u8aToBuffer(u8aToU8a(value))
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// 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 Encode and decode base58 values
|
||||||
|
*/
|
||||||
|
|
||||||
|
export { default as base58Decode } from './decode';
|
||||||
|
export { default as base58Encode } from './encode';
|
||||||
|
export { default as base58Validate } from './validate';
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// 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 { BASE58_ALPHABET } from './bs58';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name base58Validate
|
||||||
|
* @summary Validates a base58 value.
|
||||||
|
* @description
|
||||||
|
* Validates the the supplied value is valid base58
|
||||||
|
*/
|
||||||
|
export default function base58Validate (value: string): true {
|
||||||
|
for (let i = 0; i < value.length; i++) {
|
||||||
|
if (!BASE58_ALPHABET.includes(value[i])) {
|
||||||
|
throw new TypeError(`Invalid base58 character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// 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 { stringToU8a } from '@polkadot/util';
|
||||||
|
|
||||||
|
import { base64Decode } from '.';
|
||||||
|
|
||||||
|
describe('base64Decode', (): void => {
|
||||||
|
it('decodes a mixed base64 utf8 string', (): void => {
|
||||||
|
expect(
|
||||||
|
base64Decode('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
|
||||||
|
).toEqual(
|
||||||
|
stringToU8a('hello world Приветствую ми 你好')
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// 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 { bufferToU8a } from '@polkadot/util';
|
||||||
|
|
||||||
|
import validate from './validate';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name base64Decode
|
||||||
|
* @summary Decodes a base64 value.
|
||||||
|
* @description
|
||||||
|
* From the provided input, decode the base64 and return the result as an `Uint8Array`.
|
||||||
|
*/
|
||||||
|
export default function base64Decode (value: string): Uint8Array {
|
||||||
|
validate(value);
|
||||||
|
|
||||||
|
return bufferToU8a(Buffer.from(value, 'base64'));
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// 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 { base64Encode } from '.';
|
||||||
|
|
||||||
|
describe('base64Encode', (): void => {
|
||||||
|
it('encodes a mixed base64 utf8 string', (): void => {
|
||||||
|
expect(
|
||||||
|
base64Encode('hello world Приветствую ми 你好')
|
||||||
|
).toEqual('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// 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 { u8aToBuffer, u8aToU8a } from '@polkadot/util';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name base64Encode
|
||||||
|
* @summary Creates a base64 value.
|
||||||
|
* @description
|
||||||
|
* From the provided input, create the base64 and return the result as a string.
|
||||||
|
*/
|
||||||
|
export default function base58Encode (value: Uint8Array | string | Buffer | number[]): string {
|
||||||
|
return u8aToBuffer(u8aToU8a(value)).toString('base64');
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// 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 Encode and decode base64 values
|
||||||
|
*/
|
||||||
|
|
||||||
|
export { default as base64Decode } from './decode';
|
||||||
|
export { default as base64Encode } from './encode';
|
||||||
|
export { default as base64Validate } from './validate';
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// 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 { base64Validate } from '.';
|
||||||
|
|
||||||
|
describe('base64Validate', (): void => {
|
||||||
|
it('decodes a mixed base64 utf8 string', (): void => {
|
||||||
|
expect(
|
||||||
|
() => base64Validate('aGVsbG8gd29ybGQg0J/RgNC40LLQtd^GC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
|
||||||
|
).toThrow(/Invalid base64 encoding/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name base64Validate
|
||||||
|
* @summary Validates a base64 value.
|
||||||
|
* @description
|
||||||
|
* Validates the the supplied value is valid base64
|
||||||
|
*/
|
||||||
|
export default function base64Validate (value: string): true {
|
||||||
|
if (!(/^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value))) {
|
||||||
|
throw new TypeError('Invalid base64 encoding');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -10,6 +10,8 @@ export * from './init';
|
|||||||
|
|
||||||
// all internal exports
|
// all internal exports
|
||||||
export * from './address';
|
export * from './address';
|
||||||
|
export * from './base58';
|
||||||
|
export * from './base64';
|
||||||
export * from './blake2';
|
export * from './blake2';
|
||||||
export * from './keccak';
|
export * from './keccak';
|
||||||
export * from './key';
|
export * from './key';
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@polkadot/util",
|
"name": "@polkadot/util",
|
||||||
"version": "2.15.1",
|
"version": "2.18.1",
|
||||||
"description": "A collection of useful utilities for @polkadot",
|
"description": "A collection of useful utilities for @polkadot",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
|
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/runtime": "^7.10.3",
|
"@babel/runtime": "^7.10.4",
|
||||||
"@types/bn.js": "^4.11.6",
|
"@types/bn.js": "^4.11.6",
|
||||||
"bn.js": "^5.1.2",
|
"bn.js": "^5.1.2",
|
||||||
"camelcase": "^5.3.1",
|
"camelcase": "^5.3.1",
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import { isAscii } from '.';
|
||||||
|
|
||||||
|
describe('isAscii', (): void => {
|
||||||
|
it('returns true for an ASCII string', (): void => {
|
||||||
|
expect(isAscii('Hello\tWorld!\n\rTesting')).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true for ASCII bytes', (): void => {
|
||||||
|
expect(isAscii(new Uint8Array([0x31, 0x32, 0x20, 10]))).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true for empty string inputs', (): void => {
|
||||||
|
expect(isAscii('')).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true for empty U8a inputs', (): void => {
|
||||||
|
expect(isAscii(new Uint8Array())).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false for empty undefined inputs', (): void => {
|
||||||
|
expect(isAscii()).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false for non-printable characters', (): void => {
|
||||||
|
expect(isAscii(new Uint8Array([5]))).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import u8aToU8a from '../u8a/toU8a';
|
||||||
|
import isString from './string';
|
||||||
|
|
||||||
|
const FORMAT = [9, 10, 13];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name isAscii
|
||||||
|
* @summary Tests if the input is printable ASCII
|
||||||
|
* @description
|
||||||
|
* Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters
|
||||||
|
*/
|
||||||
|
export default function isAscii (value?: number[] | Buffer | Uint8Array | string | null): boolean {
|
||||||
|
if (!value) {
|
||||||
|
return isString(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const u8a = u8aToU8a(value);
|
||||||
|
|
||||||
|
return !u8a.some((byte) => (byte >= 127) || (byte < 32 && !FORMAT.includes(byte)));
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
/**
|
/**
|
||||||
* @summary Type checking utilities
|
* @summary Type checking utilities
|
||||||
*/
|
*/
|
||||||
|
export { default as isAscii } from './ascii';
|
||||||
export { default as isBigInt } from './bigInt';
|
export { default as isBigInt } from './bigInt';
|
||||||
export { default as isBn } from './bn';
|
export { default as isBn } from './bn';
|
||||||
export { default as isBuffer } from './buffer';
|
export { default as isBuffer } from './buffer';
|
||||||
@@ -25,3 +26,4 @@ export { default as isTestChain } from './testChain';
|
|||||||
export { default as isToBn } from './toBn';
|
export { default as isToBn } from './toBn';
|
||||||
export { default as isU8a } from './u8a';
|
export { default as isU8a } from './u8a';
|
||||||
export { default as isUndefined } from './undefined';
|
export { default as isUndefined } from './undefined';
|
||||||
|
export { default as isUtf8 } from './utf8';
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
import { isUtf8 } from '.';
|
||||||
|
|
||||||
|
describe('isUtf8', (): void => {
|
||||||
|
it('returns true for an ASCII string', (): void => {
|
||||||
|
expect(isUtf8('Hello\tWorld!\n\rTesting')).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true for ASCII bytes', (): void => {
|
||||||
|
expect(isUtf8(new Uint8Array([0x31, 0x32, 0x20, 10]))).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true for empty string inputs', (): void => {
|
||||||
|
expect(isUtf8('')).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true for empty U8a inputs', (): void => {
|
||||||
|
expect(isUtf8(new Uint8Array())).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false for empty undefined inputs', (): void => {
|
||||||
|
expect(isUtf8()).toEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it is valid for ru', (): void => {
|
||||||
|
expect(isUtf8('Приветствую, ми')).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is valid for zh', (): void => {
|
||||||
|
expect(isUtf8('你好')).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is invalid for something random', (): void => {
|
||||||
|
expect(isUtf8('0x7f07b1f87709608bee603bbc79a0dfc29cd315c1351a83aa31adf7458d7d3003')).toEqual(false);
|
||||||
|
expect(isUtf8('0xc9ec51351beec59b52db06b3dd4685004013035df080358c319553ced597ca05')).toEqual(false);
|
||||||
|
expect(isUtf8('0xd12cef06e90a05b63737d542aaee1389ff9f5c25e461b362180f025c3b2d5635')).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,208 @@
|
|||||||
|
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||||
|
// This software may be modified and distributed under the terms
|
||||||
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
|
// Adapted from https://github.com/JulienPalard/is_utf8/blob/master/is_utf8.c
|
||||||
|
|
||||||
|
import u8aToU8a from '../u8a/toU8a';
|
||||||
|
import isString from './string';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name isUtf8
|
||||||
|
* @summary Tests if the input is valid Utf8
|
||||||
|
* @description
|
||||||
|
* Checks to see if the input string or Uint8Array is valid Utf8
|
||||||
|
*/
|
||||||
|
export default function isUtf8 (value?: number[] | Buffer | Uint8Array | string | null): boolean {
|
||||||
|
if (!value) {
|
||||||
|
return isString(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const u8a = u8aToU8a(value);
|
||||||
|
const len = u8a.length;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
while (i < len) {
|
||||||
|
if (u8a[i] <= 0x7F) /* 00..7F */ {
|
||||||
|
i += 1;
|
||||||
|
} else if (u8a[i] >= 0xC2 && u8a[i] <= 0xDF) /* C2..DF 80..BF */ {
|
||||||
|
if (i + 1 < len) /* Expect a 2nd byte */ {
|
||||||
|
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||||
|
// *message = "After a first byte between C2 and DF, expecting a 2nd byte between 80 and BF";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte between C2 and DF, expecting a 2nd byte.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 2;
|
||||||
|
} else if (u8a[i] === 0xE0) /* E0 A0..BF 80..BF */ {
|
||||||
|
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||||
|
if (u8a[i + 1] < 0xA0 || u8a[i + 1] > 0xBF) {
|
||||||
|
// *message = "After a first byte of E0, expecting a 2nd byte between A0 and BF.";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||||
|
// *message = "After a first byte of E0, expecting a 3nd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 3;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte of E0, expecting two following bytes.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
} else if (u8a[i] >= 0xE1 && u8a[i] <= 0xEC) /* E1..EC 80..BF 80..BF */ {
|
||||||
|
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||||
|
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||||
|
// *message = "After a first byte between E1 and EC, expecting the 2nd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||||
|
// *message = "After a first byte between E1 and EC, expecting the 3rd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 3;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte between E1 and EC, expecting two following bytes.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
} else if (u8a[i] === 0xED) /* ED 80..9F 80..BF */ {
|
||||||
|
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||||
|
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0x9F) {
|
||||||
|
// *message = "After a first byte of ED, expecting 2nd byte between 80 and 9F.";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||||
|
// *message = "After a first byte of ED, expecting 3rd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 3;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte of ED, expecting two following bytes.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
} else if (u8a[i] >= 0xEE && u8a[i] <= 0xEF) /* EE..EF 80..BF 80..BF */ {
|
||||||
|
if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
|
||||||
|
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||||
|
// *message = "After a first byte between EE and EF, expecting 2nd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||||
|
// *message = "After a first byte between EE and EF, expecting 3rd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 3;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte between EE and EF, two following bytes.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
} else if (u8a[i] === 0xF0) /* F0 90..BF 80..BF 80..BF */ {
|
||||||
|
if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
|
||||||
|
if (u8a[i + 1] < 0x90 || u8a[i + 1] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F0, expecting 2nd byte between 90 and BF.";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F0, expecting 3rd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 3;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F0, expecting 4th byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 4;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte of F0, expecting three following bytes.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 4;
|
||||||
|
} else if (u8a[i] >= 0xF1 && u8a[i] <= 0xF3) /* F1..F3 80..BF 80..BF 80..BF */ {
|
||||||
|
if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
|
||||||
|
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F1, F2, or F3, expecting a 2nd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F1, F2, or F3, expecting a 3rd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 3;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F1, F2, or F3, expecting a 4th byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 4;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte of F1, F2, or F3, expecting three following bytes.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 4;
|
||||||
|
} else if (u8a[i] === 0xF4) /* F4 80..8F 80..BF 80..BF */ {
|
||||||
|
if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
|
||||||
|
if (u8a[i + 1] < 0x80 || u8a[i + 1] > 0x8F) {
|
||||||
|
// *message = "After a first byte of F4, expecting 2nd byte between 80 and 8F.";
|
||||||
|
// *faulty_bytes = 2;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 2] < 0x80 || u8a[i + 2] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F4, expecting 3rd byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 3;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8a[i + 3] < 0x80 || u8a[i + 3] > 0xBF) {
|
||||||
|
// *message = "After a first byte of F4, expecting 4th byte between 80 and BF.";
|
||||||
|
// *faulty_bytes = 4;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// *message = "After a first byte of F4, expecting three following bytes.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 4;
|
||||||
|
} else {
|
||||||
|
// *message = "Expecting bytes in the following ranges: 00..7F C2..F4.";
|
||||||
|
// *faulty_bytes = 1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -5,12 +5,6 @@
|
|||||||
import { numberToU8a } from '.';
|
import { numberToU8a } from '.';
|
||||||
|
|
||||||
describe('numberToU8a', (): void => {
|
describe('numberToU8a', (): void => {
|
||||||
it('converts 0 to empty', (): void => {
|
|
||||||
expect(
|
|
||||||
numberToU8a(0)
|
|
||||||
).toEqual(new Uint8Array());
|
|
||||||
});
|
|
||||||
|
|
||||||
it('converts undefined to empty', (): void => {
|
it('converts undefined to empty', (): void => {
|
||||||
expect(
|
expect(
|
||||||
numberToU8a()
|
numberToU8a()
|
||||||
@@ -29,6 +23,18 @@ describe('numberToU8a', (): void => {
|
|||||||
).toEqual(new Uint8Array());
|
).toEqual(new Uint8Array());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('converts 0 to u8a', (): void => {
|
||||||
|
expect(
|
||||||
|
numberToU8a(0)
|
||||||
|
).toEqual(new Uint8Array([0]));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('converts 0 to u8a (with length)', (): void => {
|
||||||
|
expect(
|
||||||
|
numberToU8a(0, 16)
|
||||||
|
).toEqual(new Uint8Array([0, 0]));
|
||||||
|
});
|
||||||
|
|
||||||
it('converts values to the u8a', (): void => {
|
it('converts values to the u8a', (): void => {
|
||||||
expect(
|
expect(
|
||||||
numberToU8a(0x3456)
|
numberToU8a(0x3456)
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import hexToU8a from '../hex/toU8a';
|
import hexToU8a from '../hex/toU8a';
|
||||||
|
import isNull from '../is/null';
|
||||||
|
import isUndefined from '../is/undefined';
|
||||||
import numberToHex from './toHex';
|
import numberToHex from './toHex';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,7 +22,7 @@ import numberToHex from './toHex';
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export default function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
|
export default function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
|
||||||
if (!value || isNaN(value)) {
|
if (isUndefined(value) || isNull(value) || isNaN(value)) {
|
||||||
return new Uint8Array();
|
return new Uint8Array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user