Compare commits

..
61 Commits
Author SHA1 Message Date
Github Actions 21e9b9a033 [CI Skip] release/stable 3.0.1
skip-checks: true
2020-07-27 06:21:35 +00:00
Jaco Greeff db61ea3077 3.0 release (#643) 2020-07-27 08:17:28 +02:00
Github Actions d4a3d2392e [CI Skip] release/beta 3.0.0-beta.4
skip-checks: true
2020-07-27 06:09:08 +00:00
Jaco Greeff 88ecda706d 3.0 (#642)
* 3.0

* Bump dev
2020-07-27 08:05:04 +02:00
Github Actions 184fb1f12e [CI Skip] release/beta 3.0.0-beta.3
skip-checks: true
2020-07-22 15:45:42 +00:00
Reto Trinkler 91340577df Adding 4 as address type of ss58 for Katal Chain (#639) 2020-07-22 17:41:49 +02:00
Github Actions 36729678e2 [CI Skip] release/beta 3.0.0-beta.2
skip-checks: true
2020-07-21 07:06:23 +00:00
Jaco Greeff 1c6b4bfc34 Dev bump 2020-07-21 09:02:25 +02:00
Github Actions bbfd175579 [CI Skip] release/beta 3.0.0-beta.1
skip-checks: true
2020-07-21 06:21:56 +00:00
Jaco Greeff 0a6bd41444 Keyring JSON with kdf (#637)
* Keyring JSON with pbkdf2

* pbkdf2 from wasm

* Encode/decode defaults adjusted

* v2 decoding test

* Recode refreshes defaults

* Use scrypt for kdf

* test conversions

* Fix params

* Cleanup decoding pass

* Swap encoding to base64

* Update for base64 encoding

* 3.0.0
2020-07-21 08:17:46 +02:00
Github Actions ab21274c36 [CI Skip] release/beta 2.19.0-beta.2
skip-checks: true
2020-07-17 12:30:08 +00:00
Jaco Greeff d4e6ad5553 Bump dev 2020-07-17 14:26:11 +02:00
Github Actions 49dbd757df [CI Skip] release/beta 2.19.0-beta.1
skip-checks: true
2020-07-17 12:07:35 +00:00
Jaco Greeff 20e82cc45b Dev bump 2020-07-17 14:03:36 +02:00
Github Actions 6a7fa80c55 [CI Skip] release/beta 2.19.0-beta.0
skip-checks: true
2020-07-17 06:39:13 +00:00
Jaco Greeff 12e34cd3e8 Bump deps (#635) 2020-07-17 08:35:10 +02:00
Github Actions 0a598fdf92 [CI Skip] release/stable 2.18.1
skip-checks: true
2020-07-13 08:51:33 +00:00
Jaco Greeff 659b896ee7 2.18 (#634) 2020-07-13 10:47:53 +02:00
Github Actions 7cda007a30 [CI Skip] release/beta 2.18.0-beta.3
skip-checks: true
2020-07-09 07:19:51 +00:00
Jaco Greeff 2288c4da8a Base64 utils (#632) 2020-07-09 09:16:24 +02:00
Github Actions fbfc21be1c [CI Skip] release/beta 2.18.0-beta.2
skip-checks: true
2020-07-08 13:39:52 +00:00
Jaco Greeff 787beb2aea Add isUtf8 (#631) 2020-07-08 15:35:43 +02:00
Github Actions 6234e16c37 [CI Skip] release/beta 2.18.0-beta.1
skip-checks: true
2020-07-08 11:58:35 +00:00
Jaco Greeff e9cbc0a0d9 Bump deps (#630) 2020-07-08 13:54:57 +02:00
Github Actions 166eeb8852 [CI Skip] release/beta 2.18.0-beta.0
skip-checks: true
2020-07-08 09:18:38 +00:00
Jaco Greeff b321f92161 Add isAscii (#629) 2020-07-08 11:14:55 +02:00
Github Actions cf7d9d9ff5 [CI Skip] release/stable 2.17.1
skip-checks: true
2020-07-06 04:41:13 +00:00
Jaco Greeff fb5b3c1663 2.17 (#628) 2020-07-06 06:37:16 +02:00
Github Actions 46bbc45145 [CI Skip] release/beta 2.17.0-beta.3
skip-checks: true
2020-07-04 08:12:18 +00:00
Jaco Greeff dc8b4a6447 Add encode{Derived, Multi}Address (#627)
* Add encode{Derived, Multi}Address

* Tests for encode*

* CHANGELOG
2020-07-04 10:08:40 +02:00
Github Actions 8ec38eca4a [CI Skip] release/beta 2.17.0-beta.2
skip-checks: true
2020-07-02 05:36:03 +00:00
Jaco Greeff 24b533689c Handle 0 on numberToU8a (#626)
* Handle 0 on numberToU8a

* Remove old test (replaced)

* base58 CHANGELOG
2020-07-02 07:32:25 +02:00
Github Actions 5421e06fc9 [CI Skip] release/beta 2.17.0-beta.1
skip-checks: true
2020-07-02 05:29:40 +00:00
Jaco Greeff 7115efd94f Simplify base58 dependency tree (#624) 2020-07-02 07:26:04 +02:00
Github Actions 92f88222c4 [CI Skip] release/beta 2.17.0-beta.0
skip-checks: true
2020-07-02 05:12:02 +00:00
Jaco Greeff bc7aba71a6 Bump deps (#625) 2020-07-02 07:08:40 +02:00
Github Actions 1d8b8b3204 [CI Skip] release/stable 2.16.1
skip-checks: true
2020-06-29 05:19:13 +00:00
Jaco Greeff 4a2e090ef8 2.16 (#623) 2020-06-29 07:15:21 +02:00
Github Actions 8cf8d5b7e8 [CI Skip] release/beta 2.16.0-beta.0
skip-checks: true
2020-06-26 12:49:47 +00:00
Jaco Greeff 8d355ff6fc Local base58 alphabet check for better errors (#622)
* Local base58 alphabet check for better errors

* Rewrite yarn.lock

* Align error return values

* Split base58{Encode, Decode}

* base58Encode input types

* Function names, some docs
2020-06-26 14:46:19 +02:00
Github Actions 72456bb2bd [CI Skip] release/stable 2.15.1
skip-checks: true
2020-06-22 09:33:14 +00:00
Jaco Greeff 87f3fd2827 2.15 (#621) 2020-06-22 11:29:33 +02:00
Github Actions 7254aab68d [CI Skip] release/beta 2.15.0-beta.0
skip-checks: true
2020-06-19 15:39:36 +00:00
Jaco Greeff 95d7160ad0 Test for 0 prefix (#620)
* Test for 0 prefix

* Add test for keyring encodeAddress (& fix)

* Test other branch of ?: check
2020-06-19 17:35:48 +02:00
Github Actions 611b1be481 [CI Skip] release/stable 2.14.1
skip-checks: true
2020-06-16 06:02:06 +00:00
Jaco Greeff 73b35fc188 2.14 (#618) 2020-06-16 07:58:33 +02:00
Github Actions 6c30d5b9fd [CI Skip] release/beta 2.14.0-beta.0
skip-checks: true
2020-06-08 15:58:49 +00:00
Keith Ingram 56d151fe66 Add BN consts and timeToString (#610)
* Add BN consts and timeToString

* docs

* lint

* fixes

* lint

* lint

* add tests, ms

* fix

* test
2020-06-09 03:55:21 +12:00
Github Actions cd20fe6c8b [CI Skip] release/stable 2.13.1
skip-checks: true
2020-06-08 14:03:31 +00:00
Jaco Greeff 28f8ca73aa 2.13 (#616) 2020-06-08 15:59:47 +02:00
Github Actions f4a8bfc0c0 [CI Skip] release/beta 2.13.0-beta.5
skip-checks: true
2020-06-08 12:33:10 +00:00
Jaco Greeff 94b058d6a3 Align bnToHex signature with bnToU8a (#615) 2020-06-08 14:29:40 +02:00
Github Actions 3d7ddb85a2 [CI Skip] release/beta 2.13.0-beta.4
skip-checks: true
2020-06-08 11:35:08 +00:00
Jaco Greeff f7a2bb895f Fix blake2 U8a JS with hex inputs (#614)
* Fix blake2 U8a JS with hex inputs

* Apply suggestions from code review
2020-06-08 13:31:10 +02:00
Github Actions c1bfcc00b6 [CI Skip] release/beta 2.13.0-beta.3
skip-checks: true
2020-06-06 08:08:42 +00:00
Jaco Greeff b16d8e7c26 Bump deps (#612) 2020-06-06 10:05:02 +02:00
Github Actions 2a4b363fcd [CI Skip] release/beta 2.13.0-beta.2
skip-checks: true
2020-06-05 20:17:13 +00:00
Jaco Greeff de689b8e6f Bump deps (#611) 2020-06-05 22:13:55 +02:00
Github Actions 2e72ab920b [CI Skip] release/beta 2.13.0-beta.1
skip-checks: true
2020-06-02 09:44:29 +00:00
Jaco Greeff 565e6baecd Linting 2020-06-02 11:40:25 +02:00
Jaco Greeff 1e15203b6c Reset to beta 2020-06-02 11:24:23 +02:00
73 changed files with 3178 additions and 2056 deletions
+37
View File
@@ -1,5 +1,42 @@
# CHANGELOG
## 3.0.1 Jul 27, 2020
- Add Katal ss58Format (Thanks to https://github.com/retotrinkler)
- Adjust JSON pair output with scrypt kdf (version 3 output)
## 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
- Adjust keyring to no override 0-prefix (Polkadot live) settings with keychain default
## 2.14.1 Jun 16, 2020
- Add `extractTime` utility conversion (Thanks to https://github.com/kwingram25)
## 2.13.1 Jun 8, 2020
- Fix JS blake2 fallback (non-wasm) to correctly deal with hex inputs
- Align `bnToHex` signature with `bnToU8a` (with old/new style)
- Allow `bnTo{Hex,U8a}` to take any value with `.toBn()` signatures
## 2.12.2 Jun 1, 2020
- Fix `isFunction` signature to assert `Function`
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "2.12.2"
"version": "3.0.1"
}
+5 -5
View File
@@ -21,10 +21,10 @@
"test:one": "polkadot-dev-run-test"
},
"devDependencies": {
"@babel/core": "^7.9.6",
"@polkadot/dev": "^0.55.6",
"@polkadot/ts": "^0.3.25",
"@types/jest": "^25.2.3"
"@babel/core": "^7.10.5",
"@polkadot/dev": "^0.55.28",
"@polkadot/ts": "^0.3.29",
"@types/jest": "^26.0.7"
},
"version": "2.12.2"
"version": "3.0.1"
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/keyring",
"version": "2.12.2",
"version": "3.0.1",
"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.9.6",
"@polkadot/util": "2.12.2",
"@polkadot/util-crypto": "2.12.2"
"@babel/runtime": "^7.10.5",
"@polkadot/util": "3.0.1",
"@polkadot/util-crypto": "3.0.1"
}
}
+89
View File
@@ -212,4 +212,93 @@ describe('keypair', (): void => {
expect(pair.verify(MESSAGE, signature)).toBe(true);
});
});
describe('util', (): void => {
let keyring: Keyring;
beforeEach((): void => {
keyring = new Keyring({ ss58Format: 42 });
});
it('can re-encode an address to Polkadot live', (): void => {
expect(
keyring.encodeAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 0)
).toEqual('15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5');
});
it('can re-encode an address to keyring default', (): void => {
expect(
keyring.encodeAddress('15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5')
).toEqual('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
});
});
describe('version 2 JSON', (): void => {
const PAIR = '{"address":"5CczAE5AmGrZ93MeVhha3Ywam7j9dKB7cArnH7gtrXcMFJvu","encoded":"0xee8f236e2ac3217ce689692a4afc612220dc77fddaed0482f8f95136a7c3e034cccfbc495410a6e9b2439904974ed1d207abeca536ff6985ceb78edeeb3dc343e561c184c488101af8811d1331430b4ccf0e96ef507132e5132964e8564232e7100d973c5bee7b231dd0c8ad5273f3501515a422c8d7ed9d20a73c0ed17c98ee4588e54844bb73052dcad81f7a1094613d63c162fec7446c88b1fae70e","encoding":{"content":["pkcs8","sr25519"],"type":"xsalsa20-poly1305","version":"2"},"meta":{"genesisHash":"0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e","name":"json v2","tags":[],"whenCreated":1595243159596}}';
const PASS2 = 'versionTwo';
const PASS3 = 'versionThree';
let keyring: Keyring;
beforeEach((): void => {
keyring = new Keyring({ ss58Format: 42 });
});
it('can decode from a version 2 JSON file', (): void => {
const pair = keyring.addFromJson(JSON.parse(PAIR));
pair.decodePkcs8(PASS2);
const json = pair.toJson(PASS3);
expect(pair.isLocked).toBe(false);
expect(pair.address).toBe('5CczAE5AmGrZ93MeVhha3Ywam7j9dKB7cArnH7gtrXcMFJvu');
expect(json.encoding).toEqual({
content: ['pkcs8', 'sr25519'],
type: ['scrypt', 'xsalsa20-poly1305'],
version: '3'
});
pair.decodePkcs8(PASS3);
expect(pair.address).toEqual('5CczAE5AmGrZ93MeVhha3Ywam7j9dKB7cArnH7gtrXcMFJvu');
});
});
describe('version 3 JSON (hex)', (): void => {
const PAIR = '{"address":"FLiSDPCcJ6auZUGXALLj6jpahcP6adVFDBUQznPXUQ7yoqH","encoded":"0xcd238963070cc4d6806053ee1ac500c7add9c28732bb5d434a332f84a91d9be0008000000100000008000000cf630a1113941b350ddd06697e50399183162e5e9a0e893eafc7f5f4893a223dca5055706b9925b56fdb4304192143843da718e11717daf89cf4f4781f94fb443f61432f782d54280af9eec90bd3069c3cc2d957a42b7c18dc2e9497f623735518e0e49b58f8e4db2c09da3a45dbb935659d015fc94b946cba75b606a6ff7f4e823f6b049e2e6892026b49de02d6dbbd64646fe0933f537d9ea53a70be","encoding":{"content":["pkcs8","sr25519"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{"genesisHash":"0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe","name":"version3","tags":[],"whenCreated":1595277797639}}';
const PASS3 = 'version3';
let keyring: Keyring;
beforeEach((): void => {
keyring = new Keyring({ ss58Format: 2 });
});
it('can decode from a version 3 JSON file', (): void => {
const pair = keyring.addFromJson(JSON.parse(PAIR));
pair.decodePkcs8(PASS3);
expect(pair.isLocked).toBe(false);
expect(pair.address).toBe('FLiSDPCcJ6auZUGXALLj6jpahcP6adVFDBUQznPXUQ7yoqH');
});
});
describe('version 3 JSON (base64)', (): void => {
const PAIR = '{"address":"FLiSDPCcJ6auZUGXALLj6jpahcP6adVFDBUQznPXUQ7yoqH","encoded":"ILjSgYaGvq1zaCz/kx+aqfLaHBjLXz0Qsmr6RnkOVU4AgAAAAQAAAAgAAAB5R2hm5kgXyc0NQYFxvMU4zCdjB+ugs/ibEooqCvuudbaeKn3Ee47NkCqU1ecOJV+eeaVn4W4dRvIpj5kGmQOGsewR+MiQ/B0G9NFh7JXV0qcPlk2QMNW1/mbJrTO4miqL448BSkP7ZOhUV6HFUpMt3B9HwjiRLN8RORcFp0ID/Azs4Jl/xOpXNzbgQGIffWgCIKTxN9N1ku6tdlG4","encoding":{"content":["pkcs8","sr25519"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{"genesisHash":"0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe","name":"version3","tags":[],"whenCreated":1595277797639,"whenEdited":1595278378596}}';
const PASS3 = 'version3';
let keyring: Keyring;
beforeEach((): void => {
keyring = new Keyring({ ss58Format: 2 });
});
it('can decode from a version 3 JSON file', (): void => {
const pair = keyring.addFromJson(JSON.parse(PAIR));
pair.decodePkcs8(PASS3);
expect(pair.isLocked).toBe(false);
expect(pair.address).toBe('FLiSDPCcJ6auZUGXALLj6jpahcP6adVFDBUQznPXUQ7yoqH');
});
});
});
+12 -9
View File
@@ -3,10 +3,10 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType, Keypair } from '@polkadot/util-crypto/types';
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringOptions } from './types';
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta, KeyringOptions } from './types';
import { assert, hexToU8a, isHex, stringToU8a } from '@polkadot/util';
import { decodeAddress, encodeAddress, keyExtractSuri, keyFromPath, naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, secp256k1KeypairFromSeed as secp256k1FromSeed, mnemonicToMiniSecret } from '@polkadot/util-crypto';
import { assert, hexToU8a, isHex, isUndefined, stringToU8a } from '@polkadot/util';
import { base64Decode, decodeAddress, encodeAddress, keyExtractSuri, keyFromPath, naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, secp256k1KeypairFromSeed as secp256k1FromSeed, mnemonicToMiniSecret } from '@polkadot/util-crypto';
import { DEV_PHRASE } from './defaults';
import createPair from './pair';
@@ -84,10 +84,10 @@ export default class Keyring implements KeyringInstance {
* of an account backup), and then generates a keyring pair from them that it passes to
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
*/
public addFromAddress (address: string | Uint8Array, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null, type: KeypairType = this.type, ignoreChecksum?: boolean): KeyringPair {
public addFromAddress (address: string | Uint8Array, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null, type: KeypairType = this.type, ignoreChecksum?: boolean, encType?: KeyringPair$JsonEncodingTypes[]): KeyringPair {
const publicKey = this.decodeAddress(address, ignoreChecksum);
return this.addPair(createPair({ toSS58: this.encodeAddress, type }, { publicKey, secretKey: new Uint8Array() }, meta, encoded));
return this.addPair(createPair({ toSS58: this.encodeAddress, type }, { publicKey, secretKey: new Uint8Array() }, meta, encoded, encType));
}
/**
@@ -97,12 +97,15 @@ export default class Keyring implements KeyringInstance {
* of an account backup), and then generates a keyring pair from it that it passes to
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
*/
public addFromJson ({ address, encoded, encoding: { content, version }, meta }: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair {
const type = version === '0' || !Array.isArray(content)
public addFromJson ({ address, encoded, encoding: { content, type, version }, meta }: KeyringPair$Json, ignoreChecksum?: boolean): KeyringPair {
const cryptoType = version === '0' || !Array.isArray(content)
? this.type
: content[1];
const encType = !Array.isArray(type)
? [type]
: type;
return this.addFromAddress(address, meta, hexToU8a(encoded), type, ignoreChecksum);
return this.addFromAddress(address, meta, isHex(encoded) ? hexToU8a(encoded) : base64Decode(encoded), cryptoType, ignoreChecksum, encType);
}
/**
@@ -192,7 +195,7 @@ export default class Keyring implements KeyringInstance {
* @description Encodes the input into an ss58 representation
*/
public encodeAddress = (address: Uint8Array | string, ss58Format?: number): string => {
return encodeAddress(address, ss58Format || this.#ss58);
return encodeAddress(address, isUndefined(ss58Format) ? this.#ss58 : ss58Format);
}
/**
+26 -10
View File
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert, stringToU8a, u8aFixLength } from '@polkadot/util';
import { naclDecrypt } from '@polkadot/util-crypto';
import { KeyringPair$JsonEncodingTypes } from '../types';
import { PairInfo } from './types';
import { NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH } from './defaults';
import { assert, stringToU8a, u8aFixLength } from '@polkadot/util';
import { naclDecrypt, scryptEncode, scryptFromU8a } from '@polkadot/util-crypto';
import { NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH, SCRYPT_LENGTH } from './defaults';
const SEED_OFFSET = PKCS8_HEADER.length;
@@ -41,18 +43,32 @@ function decodePkcs8 (encoded: Uint8Array): DecodeResult {
};
}
export default function decode (passphrase?: string, encrypted?: Uint8Array | null): DecodeResult {
export default function decode (passphrase?: string, encrypted?: Uint8Array | null, encType: KeyringPair$JsonEncodingTypes[] = ['scrypt', 'xsalsa20-poly1305']): DecodeResult {
assert(encrypted, 'No encrypted data available to decode');
assert(passphrase || !encType.includes('xsalsa20-poly1305'), 'Password required to decode encypted data');
const encoded = passphrase
? naclDecrypt(
let encoded: Uint8Array | null = encrypted;
if (passphrase) {
let password: Uint8Array;
if (encType.includes('scrypt')) {
const { params, salt } = scryptFromU8a(encrypted);
password = scryptEncode(passphrase, salt, params).password;
encrypted = encrypted.subarray(SCRYPT_LENGTH);
} else {
password = stringToU8a(passphrase);
}
encoded = naclDecrypt(
encrypted.subarray(NONCE_LENGTH),
encrypted.subarray(0, NONCE_LENGTH),
u8aFixLength(stringToU8a(passphrase), 256, true)
)
: encrypted;
u8aFixLength(password, 256, true)
);
}
assert(encoded, 'Unable to unencrypt using the supplied passphrase');
assert(encoded, 'Unable to decode using the supplied passphrase');
return decodePkcs8(encoded);
}
+5 -1
View File
@@ -6,14 +6,18 @@ const NONCE_LENGTH = 24;
const PKCS8_DIVIDER = new Uint8Array([161, 35, 3, 33, 0]);
const PKCS8_HEADER = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
const PUB_LENGTH = 32;
const SALT_LENGTH = 32;
const SEC_LENGTH = 64;
const SEED_LENGTH = 32;
const SCRYPT_LENGTH = SALT_LENGTH + (3 * 4);
export {
NONCE_LENGTH,
PKCS8_DIVIDER,
PKCS8_HEADER,
PUB_LENGTH,
SALT_LENGTH,
SEC_LENGTH,
SEED_LENGTH
SEED_LENGTH,
SCRYPT_LENGTH
};
+2 -2
View File
@@ -3,10 +3,10 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import testingPairs from '../testingPairs';
import { PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH } from './defaults';
import { PKCS8_DIVIDER, PKCS8_HEADER, NONCE_LENGTH, PUB_LENGTH, SCRYPT_LENGTH, SEC_LENGTH } from './defaults';
const PKCS8_LENGTH = PKCS8_DIVIDER.length + PKCS8_HEADER.length + PUB_LENGTH + SEC_LENGTH;
const ENCODED_LENGTH = 157;
const ENCODED_LENGTH = 16 + PKCS8_LENGTH + NONCE_LENGTH + SCRYPT_LENGTH;
const keyring = testingPairs({ type: 'ed25519' }, false);
+5 -4
View File
@@ -4,8 +4,8 @@
import { PairInfo } from './types';
import { stringToU8a, u8aConcat, u8aFixLength, assert } from '@polkadot/util';
import { naclEncrypt } from '@polkadot/util-crypto';
import { u8aConcat, assert } from '@polkadot/util';
import { naclEncrypt, scryptEncode, scryptToU8a } from '@polkadot/util-crypto';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
@@ -23,7 +23,8 @@ export default function encode ({ publicKey, secretKey }: PairInfo, passphrase?:
return encoded;
}
const { encrypted, nonce } = naclEncrypt(encoded, u8aFixLength(stringToU8a(passphrase), 256, true));
const { params, password, salt } = scryptEncode(passphrase);
const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
return u8aConcat(nonce, encrypted);
return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
}
+13 -6
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Keypair, KeypairType } from '@polkadot/util-crypto/types';
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } from '../types';
import { KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta, SignOptions } from '../types';
import { PairInfo } from './types';
import { assert, u8aConcat } from '@polkadot/util';
@@ -112,7 +112,14 @@ function isLocked (secretKey?: Uint8Array): secretKey is undefined {
* an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
* property that indicates whether the public key value of the `encoded` property is encoded or not.
*/
export default function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null): KeyringPair {
export default function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null = null, encTypes?: KeyringPair$JsonEncodingTypes[]): KeyringPair {
const recode = (passphrase?: string): Uint8Array => {
encoded = encode({ publicKey, secretKey }, passphrase); // re-encode, latest version
encTypes = undefined; // swap to defaults, latest version follows
return encoded;
};
return {
get address (): string {
return toSS58(getAddress(type, publicKey));
@@ -130,8 +137,8 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
return type;
},
// eslint-disable-next-line sort-keys
decodePkcs8: (passphrase?: string, _encoded?: Uint8Array | null): void => {
const decoded = decode(passphrase, _encoded || encoded);
decodePkcs8: (passphrase?: string, userEncoded?: Uint8Array | null): void => {
const decoded = decode(passphrase, userEncoded || encoded, encTypes);
if (decoded.secretKey.length === 64) {
publicKey = decoded.publicKey;
@@ -152,7 +159,7 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
return createPair({ toSS58, type }, derived, meta, null);
},
encodePkcs8: (passphrase?: string): Uint8Array =>
encode({ publicKey, secretKey }, passphrase),
recode(passphrase),
lock: (): void => {
secretKey = new Uint8Array(0);
},
@@ -165,7 +172,7 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
return sign(type, message, { publicKey, secretKey }, options);
},
toJson: (passphrase?: string): KeyringPair$Json =>
toJson(type, { address: toSS58(getAddress(type, publicKey)), meta }, encode({ publicKey, secretKey }, passphrase), !!passphrase),
toJson(type, { address: toSS58(getAddress(type, publicKey)), meta }, recode(passphrase), !!passphrase),
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
verify(type, message, signature, publicKey)
};
+6 -6
View File
@@ -12,11 +12,11 @@ describe('toJson', (): void => {
keyring.alice.toJson()
).toMatchObject({
address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua',
encoded: '0x3053020101300506032b657004220420416c696365202020202020202020202020202020202020202020202020202020d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4fa123032100d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f',
encoded: 'MFMCAQEwBQYDK2VwBCIEIEFsaWNlICAgICAgICAgICAgICAgICAgICAgICAgICAg0XKnTNpMhlkSwyugqApXrmmrrkEOXMtZ3uhOL0Qy20+hIwMhANFyp0zaTIZZEsMroKgKV65pq65BDlzLWd7oTi9EMttP',
encoding: {
content: ['pkcs8', 'ed25519'],
type: 'none',
version: '2'
type: ['none'],
version: '3'
},
meta: {
isTesting: true,
@@ -28,13 +28,13 @@ describe('toJson', (): void => {
it('creates an encoded output with passphrase', (): void => {
const json = keyring.alice.toJson('testing');
expect(json.encoded).toHaveLength(316);
expect(json.encoded).toHaveLength(268);
expect(json).toMatchObject({
address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua',
encoding: {
content: ['pkcs8', 'ed25519'],
type: 'xsalsa20-poly1305',
version: '2'
type: ['scrypt', 'xsalsa20-poly1305'],
version: '3'
}
});
});
+8 -5
View File
@@ -5,23 +5,26 @@
import { KeypairType } from '@polkadot/util-crypto/types';
import { KeyringPair$Json, KeyringPair$Meta } from '../types';
import { u8aToHex } from '@polkadot/util';
import { base64Encode } from '@polkadot/util-crypto';
interface PairStateJson {
address: string;
meta: KeyringPair$Meta;
}
// version 2 - nonce, encoded (previous)
// version 3 - salt, nonce, encoded
export default function toJson (type: KeypairType, { address, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
return {
address,
encoded: u8aToHex(encoded),
encoded: base64Encode(encoded),
encoding: {
content: ['pkcs8', type],
type: isEncrypted
? 'xsalsa20-poly1305'
: 'none',
version: '2'
? ['scrypt', 'xsalsa20-poly1305']
: ['none'],
version: '3'
},
meta
};
+4 -2
View File
@@ -12,11 +12,13 @@ export interface KeyringOptions {
export type KeyringPair$Meta = Record<string, unknown>;
export type KeyringPair$JsonVersion = '0' | '1' | '2';
export type KeyringPair$JsonVersion = '0' | '1' | '2' | '3';
export type KeyringPair$JsonEncodingTypes = 'none' | 'scrypt' | 'xsalsa20-poly1305';
export interface KeyringPair$JsonEncoding {
content: ['pkcs8', KeypairType];
type: 'xsalsa20-poly1305' | 'none';
type: KeyringPair$JsonEncodingTypes | KeyringPair$JsonEncodingTypes[];
version: KeyringPair$JsonVersion;
}
+8 -8
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-crypto",
"version": "2.12.2",
"version": "3.0.1",
"description": "A collection of useful crypto utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -25,25 +25,25 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.9.6",
"@polkadot/util": "2.12.2",
"@babel/runtime": "^7.10.5",
"@polkadot/util": "3.0.1",
"@polkadot/wasm-crypto": "^1.2.1",
"base-x": "^3.0.8",
"bip39": "^3.0.2",
"blakejs": "^1.1.0",
"bn.js": "^5.1.2",
"bs58": "^4.0.1",
"elliptic": "^6.5.2",
"elliptic": "^6.5.3",
"js-sha3": "^0.8.0",
"pbkdf2": "^3.0.17",
"pbkdf2": "^3.1.1",
"scryptsy": "^2.1.0",
"tweetnacl": "^1.0.3",
"xxhashjs": "^0.2.2"
},
"devDependencies": {
"@types/bn.js": "^4.11.6",
"@types/bs58": "^4.0.1",
"@types/elliptic": "^6.4.12",
"@types/pbkdf2": "^3.0.0",
"@types/pbkdf2": "^3.1.0",
"@types/scryptsy": "^2.0.0",
"@types/xxhashjs": "^0.2.2"
}
}
@@ -17,6 +17,12 @@ describe('checkAddress', (): void => {
).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 => {
expect(
checkAddress('5EnxxUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG', 2)
+14 -3
View File
@@ -4,13 +4,24 @@
import { Prefix } from './types';
import bs58 from 'bs58';
import base58Decode from '../base58/decode';
import checkChecksum from './checkChecksum';
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] {
const decoded = bs58.decode(address);
let decoded;
try {
decoded = base58Decode(address);
} catch (error) {
return [false, (error as Error).message];
}
if (decoded[0] !== prefix) {
return [false, `Prefix mismatch, expected ${prefix}, found ${decoded[0]}`];
@@ -94,4 +94,10 @@ describe('decode', (): void => {
(): Uint8Array => decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU')
).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/);
});
});
+12 -6
View File
@@ -6,9 +6,9 @@ import { Prefix } from './types';
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6
import bs58 from 'bs58';
import { assert, bufferToU8a, isHex, isU8a, u8aToU8a } from '@polkadot/util';
import { assert, isHex, isU8a, u8aToU8a } from '@polkadot/util';
import base58Decode from '../base58/decode';
import checkChecksum from './checkChecksum';
import defaults from './defaults';
@@ -18,11 +18,17 @@ export default function decode (encoded: string | Uint8Array, ignoreChecksum?: b
return u8aToU8a(encoded);
}
const decoded = bufferToU8a(bs58.decode(encoded));
const error = (message: string) => `Decoding ${encoded as string}: ${message}`;
const wrapError = (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.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
// if (decoded[0] !== prefix) {
@@ -31,7 +37,7 @@ export default function decode (encoded: string | Uint8Array, ignoreChecksum?: b
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);
}
@@ -21,6 +21,12 @@ describe('encode', (): void => {
).toEqual('7sL6eNJj5ZGV5cn3hhV2deRUsivXfBfMH76wCALCqWj1EKzv');
});
it('can re-encode an address to Polkadot live', (): void => {
expect(
encode('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 0)
).toEqual('15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5');
});
it('fails when non-valid publicKey provided', (): void => {
expect(
(): string => encode(
+4 -6
View File
@@ -6,9 +6,9 @@ import { Prefix } from './types';
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
import bs58 from 'bs58';
import { assert, u8aConcat, u8aToBuffer } from '@polkadot/util';
import { assert, u8aConcat } from '@polkadot/util';
import base58Encode from '../base58/encode';
import decode from './decode';
import defaults from './defaults';
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 hash = sshash(input);
return bs58.encode(
u8aToBuffer(
u8aConcat(input, hash.subarray(0, isPublicKey ? 2 : 1))
)
return base58Encode(
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);
}
+6 -2
View File
@@ -5,9 +5,11 @@
import checkAddress from './check';
import checkAddressChecksum from './checkChecksum';
import createKeyMulti from './keyMulti';
import createKeySub from './keySub';
import createKeyDerived from './keyDerived';
import decodeAddress from './decode';
import encodeAddress from './encode';
import encodeDerivedAddress from './encodeDerived';
import encodeMultiAddress from './encodeMulti';
import addressEq from './eq';
import setSS58Format from './setSS58Format';
import sortAddresses from './sort';
@@ -16,10 +18,12 @@ export {
addressEq,
checkAddress,
checkAddressChecksum,
createKeyDerived,
createKeyMulti,
createKeySub,
decodeAddress,
encodeAddress,
encodeDerivedAddress,
encodeMultiAddress,
setSS58Format,
sortAddresses
};
@@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// 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 => {
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(
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 => {
expect(
createKeySub('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 1)
createKeyDerived('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 1)
).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])
);
@@ -10,7 +10,7 @@ import decodeAddress from './decode';
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(
u8aConcat(
PREFIX,
+2
View File
@@ -7,6 +7,8 @@ export const allowedSS58 = [
0, 1,
// Kusama
2, 3,
// Katal Chain
4,
// Kulupu
16, 17,
// Dothereum
+15
View File
@@ -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
};
+20
View File
@@ -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));
}
+19
View File
@@ -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))
);
}
+11
View File
@@ -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 Приветствую ми 你好')
);
});
});
+19
View File
@@ -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==');
});
});
+15
View File
@@ -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');
}
+11
View File
@@ -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;
}
@@ -43,4 +43,18 @@ describe('blake2AsU8a', (): void => {
hexToU8a('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923')
);
});
it('has equivalent Wasm/Js outputs on hex inputs', (): void => {
const a = blake2AsU8a('0x123456', 256, null, false);
const b = blake2AsU8a('0x123456', 256, null, true);
expect(a).toEqual(b);
});
it('has equivalent Wasm/Js outputs with key inputs', (): void => {
const a = blake2AsU8a('0x123456', 256, new Uint8Array([1, 2]), false);
const b = blake2AsU8a('0x123456', 256, new Uint8Array([1, 2]), true);
expect(a).toEqual(b);
});
});
+3 -3
View File
@@ -20,10 +20,10 @@ import { blake2b, isReady } from '@polkadot/wasm-crypto';
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa53, 0xf98, 0x1c, 0x4d, 0x0d]
* ```
*/
export default function blake2AsU8a (data: Uint8Array | string, bitLength = 256, key: Uint8Array | null = null): Uint8Array {
export default function blake2AsU8a (data: Uint8Array | string, bitLength = 256, key: Uint8Array | null = null, onlyJs = false): Uint8Array {
const byteLength = Math.ceil(bitLength / 8);
return isReady()
return isReady() && !onlyJs
? blake2b(u8aToU8a(data), u8aToU8a(key), byteLength)
: blakejs.blake2b(data, key, byteLength);
: blakejs.blake2b(u8aToU8a(data), key, byteLength);
}
+4
View File
@@ -10,13 +10,17 @@ export * from './init';
// all internal exports
export * from './address';
export * from './base58';
export * from './base64';
export * from './blake2';
export * from './keccak';
export * from './key';
export * from './mnemonic';
export * from './nacl';
export * from './pbkdf2';
export * from './random';
export * from './schnorrkel';
export * from './scrypt';
export * from './secp256k1';
export * from './sha512';
export * from './signature';
+24
View File
@@ -0,0 +1,24 @@
// 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 { pbkdf2Sync } from 'pbkdf2';
import { bufferToU8a, u8aToBuffer, u8aToU8a } from '@polkadot/util';
import { isReady, pbkdf2 } from '@polkadot/wasm-crypto';
import randomAsU8a from '../random/asU8a';
interface Result {
password: Uint8Array;
rounds: number;
salt: Uint8Array;
}
export default function pbkdf2Encode (passphrase?: Uint8Array | string, salt = randomAsU8a(), rounds = 2048): Result {
const u8a = u8aToU8a(passphrase);
const password = isReady()
? pbkdf2(u8a, salt, rounds)
: bufferToU8a(pbkdf2Sync(u8aToBuffer(u8a), u8aToBuffer(salt), rounds, 64));
return { password, rounds, salt };
}
+5
View File
@@ -0,0 +1,5 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as pbkdf2Encode } from './encode';
@@ -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.
const DEFAULT_PARAMS = {
N: 1 << 15,
p: 1,
r: 8
};
export { DEFAULT_PARAMS };
+25
View File
@@ -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 { Params } from './types';
import scrypt from 'scryptsy';
import { bufferToU8a, u8aToBuffer, u8aToU8a } from '@polkadot/util';
import randomAsU8a from '../random/asU8a';
import { DEFAULT_PARAMS } from './defaults';
interface Result {
params: Params,
password: Uint8Array;
salt: Uint8Array;
}
export default function scryptEncode (passphrase?: Uint8Array | string, salt = randomAsU8a(), params = DEFAULT_PARAMS): Result {
const password = bufferToU8a(
scrypt(u8aToBuffer(u8aToU8a(passphrase)), u8aToBuffer(salt), params.N, params.r, params.p, 64)
);
return { params, password, salt };
}
@@ -0,0 +1,29 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Params } from './types';
import { u8aToBn, assert } from '@polkadot/util';
import { DEFAULT_PARAMS } from './defaults';
interface Result {
params: Params,
salt: Uint8Array;
}
export default function scryptFromU8a (data: Uint8Array): Result {
const salt = data.subarray(0, 32);
const N = u8aToBn(data.subarray(32 + 0, 32 + 4), { isLe: true }).toNumber();
const p = u8aToBn(data.subarray(32 + 4, 32 + 8), { isLe: true }).toNumber();
const r = u8aToBn(data.subarray(32 + 8, 32 + 12), { isLe: true }).toNumber();
// FIXME At this moment we assume these to be fixed params, this is not a great idea since we lose flexibility
// and updates for greater security. However we need some protection against carefully-crafted params that can
// eat up CPU since these are user inputs. So we need to get very clever here, but atm we only allow the defaults
// and if no match, bail out
assert(N === DEFAULT_PARAMS.N && p === DEFAULT_PARAMS.p && r === DEFAULT_PARAMS.r, 'Invalid injected scrypt params found');
return { params: { N, p, r }, salt };
}
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as scryptEncode } from './encode';
export { default as scryptFromU8a } from './fromU8a';
export { default as scryptToU8a } from './toU8a';
+16
View File
@@ -0,0 +1,16 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Params } from './types';
import { bnToU8a, u8aConcat } from '@polkadot/util';
export default function scryptToU8a (salt: Uint8Array, { N, p, r }: Params): Uint8Array {
return u8aConcat(
salt,
bnToU8a(N, { bitLength: 32, isLe: true }),
bnToU8a(p, { bitLength: 32, isLe: true }),
bnToU8a(r, { bitLength: 32, isLe: true })
);
}
+9
View File
@@ -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.
export interface Params {
N: number;
p: number;
r: number;
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "2.12.2",
"version": "3.0.1",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -25,11 +25,11 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
"dependencies": {
"@babel/runtime": "^7.9.6",
"@babel/runtime": "^7.10.5",
"@types/bn.js": "^4.11.6",
"bn.js": "^5.1.2",
"camelcase": "^5.3.1",
"chalk": "^4.0.0",
"chalk": "^4.1.0",
"ip-regex": "^4.1.0"
}
}
+38
View File
@@ -0,0 +1,38 @@
// 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 { BN_ZERO, BN_ONE, BN_TEN, BN_HUNDRED, BN_THOUSAND } from '.';
import BN from 'bn.js';
describe('consts', (): void => {
it('BN_ZERO equals 0', (): void => {
expect(
BN_ZERO
).toEqual(new BN(0));
});
it('BN_ONE equals 1', (): void => {
expect(
BN_ONE
).toEqual(new BN(1));
});
it('BN_TEN equals 10', (): void => {
expect(
BN_TEN
).toEqual(new BN(10));
});
it('BN_HUNDRED equals 100', (): void => {
expect(
BN_HUNDRED
).toEqual(new BN(100));
});
it('BN_THOUSAND equals 1000', (): void => {
expect(
BN_THOUSAND
).toEqual(new BN(1000));
});
});
+43
View File
@@ -0,0 +1,43 @@
// 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 BN from 'bn.js';
/**
* @name BN_ZERO
* @summary BN constant for 0.
*/
const BN_ZERO: BN = new BN(0);
/**
* @name BN_ONE
* @summary BN constant for 1.
*/
const BN_ONE: BN = new BN(1);
/**
* @name BN_TEN
* @summary BN constant for 10.
*/
const BN_TEN: BN = new BN(10);
/**
* @name BN_HUNDRED
* @summary BN constant for 100.
*/
const BN_HUNDRED: BN = new BN(100);
/**
* @name BN_THOUSAND
* @summary BN constant for 1000.
*/
const BN_THOUSAND: BN = new BN(1000);
export {
BN_ZERO,
BN_ONE,
BN_TEN,
BN_HUNDRED,
BN_THOUSAND
};
+1
View File
@@ -6,6 +6,7 @@
* @summary Utility methods to convert to and from `BN` objects
*/
export * from './consts';
export { default as bnFromHex } from './fromHex';
export { default as bnMax } from './max';
export { default as bnMin } from './min';
+6
View File
@@ -25,6 +25,12 @@ describe('bnToHex', (): void => {
).toBe('0x0080');
});
it('converts BN values to a prefixed hex representation (bitLength + le)', (): void => {
expect(
bnToHex(new BN(128), 16, true)
).toBe('0x8000');
});
it('converts BN values to a prefixed hex representation (LE)', (): void => {
expect(
bnToHex(new BN(128), { bitLength: 16, isLe: true })
+8 -3
View File
@@ -2,11 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ToBn, ToBnOptions } from '../types';
import BN from 'bn.js';
import isNumber from '../is/number';
import bnToU8a from './toU8a';
import { ToBnOptions } from '../types';
import { u8aToHex } from '../u8a';
const ZERO_STR = '0x00';
@@ -30,7 +31,9 @@ interface Options extends ToBnOptions {
* bnToHex(new BN(0x123456)); // => '0x123456'
* ```
*/
export default function bnToHex (value?: BN | BigInt | number | null, options: number | Options = { bitLength: -1, isLe: false, isNegative: false }): string {
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, options?: Options): string;
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, bitLength?: number, isLe?: boolean): string;
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, arg1: number | Options = { bitLength: -1, isLe: false, isNegative: false }, arg2?: boolean): string {
if (!value) {
return ZERO_STR;
}
@@ -39,8 +42,10 @@ export default function bnToHex (value?: BN | BigInt | number | null, options: n
isLe: false,
isNegative: false,
// Backwards-compatibility
...(isNumber(options) ? { bitLength: options } : options)
...(isNumber(arg1) ? { bitLength: arg1, isLe: arg2 } : arg1)
};
return u8aToHex(bnToU8a(value, _options));
}
export default bnToHex;
+5 -4
View File
@@ -2,11 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ToBn, ToBnOptions } from '../types';
import BN from 'bn.js';
import isNumber from '../is/number';
import bnToBn from './toBn';
import { ToBnOptions } from '../types';
interface Options extends ToBnOptions {
bitLength?: number;
@@ -26,9 +27,9 @@ interface Options extends ToBnOptions {
* bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
* ```
*/
function bnToU8a (value: BN | BigInt | number | null, options?: Options): Uint8Array;
function bnToU8a (value: BN | BigInt | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
function bnToU8a (value: BN | BigInt | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array {
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, options?: Options): Uint8Array;
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array {
const _options: Options = {
bitLength: -1,
isLe: true,
+20
View File
@@ -0,0 +1,20 @@
// 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 { extractTime } from '.';
describe('extractTime', (): void => {
const milliseconds = 1e9 + 123;
it('extracts time components correctly', (): void => {
expect(extractTime(milliseconds))
.toEqual({
days: 11,
hours: 13,
milliseconds: 123,
minutes: 46,
seconds: 40
});
});
});
+73
View File
@@ -0,0 +1,73 @@
// 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 { Time } from './types';
const HRS = 60 * 60;
const DAY = HRS * 24;
/**
* @name addTime
* @summary Add together two Time arrays
*/
function addTime (a: Time, b: Time): Time {
return {
days: a.days + b.days,
hours: a.hours + b.hours,
milliseconds: a.milliseconds + b.milliseconds,
minutes: a.minutes + b.minutes,
seconds: a.seconds + b.seconds
};
}
const ZERO = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
/**
* @name extractTime
* @summary Convert a quantity of seconds to Time array representing accumulated {days, minutes, hours, seconds, milliseconds}
* @example
* <BR>
*
* ```javascript
* import { extractTime } from '@polkadot/util';
*
* const { days, minutes, hours, seconds, milliseconds } = extractTime(6000); // 0, 0, 10, 0, 0
* ```
*/
export default function extractTime (milliseconds?: number): Time {
if (!milliseconds) {
return ZERO;
} else if (milliseconds < 1000) {
return { ...ZERO, milliseconds };
}
const secs = milliseconds / 1000;
if (secs < 60) {
const seconds = Math.floor(secs);
return addTime({ ...ZERO, seconds }, extractTime(milliseconds - (seconds * 1000)));
}
const mins = secs / 60;
if (mins < 60) {
const minutes = Math.floor(mins);
return addTime({ ...ZERO, minutes }, extractTime(milliseconds - (minutes * 60 * 1000)));
}
const hrs = mins / 60;
if (hrs < 24) {
const hours = Math.floor(hrs);
return addTime({ ...ZERO, hours }, extractTime(milliseconds - (hours * HRS * 1000)));
}
const days = Math.floor(hrs / 24);
return addTime({ ...ZERO, days }, extractTime(milliseconds - (days * DAY * 1000)));
}
+1
View File
@@ -10,6 +10,7 @@ import './polyfill';
export { default as assert } from './assert';
export { default as detectPackage } from './detectPackage';
export { default as extractTime } from './extractTime';
export { default as logger } from './logger';
export { default as promisify } from './promisify';
+31
View File
@@ -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);
});
});
+24
View File
@@ -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)));
}
+2
View File
@@ -5,6 +5,7 @@
/**
* @summary Type checking utilities
*/
export { default as isAscii } from './ascii';
export { default as isBigInt } from './bigInt';
export { default as isBn } from './bn';
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 isU8a } from './u8a';
export { default as isUndefined } from './undefined';
export { default as isUtf8 } from './utf8';
+41
View File
@@ -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);
});
});
+208
View File
@@ -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;
}
+12 -6
View File
@@ -5,12 +5,6 @@
import { numberToU8a } from '.';
describe('numberToU8a', (): void => {
it('converts 0 to empty', (): void => {
expect(
numberToU8a(0)
).toEqual(new Uint8Array());
});
it('converts undefined to empty', (): void => {
expect(
numberToU8a()
@@ -29,6 +23,18 @@ describe('numberToU8a', (): void => {
).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 => {
expect(
numberToU8a(0x3456)
+3 -1
View File
@@ -3,6 +3,8 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import hexToU8a from '../hex/toU8a';
import isNull from '../is/null';
import isUndefined from '../is/undefined';
import numberToHex from './toHex';
/**
@@ -20,7 +22,7 @@ import numberToHex from './toHex';
* ```
*/
export default function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
if (!value || isNaN(value)) {
if (isUndefined(value) || isNull(value) || isNaN(value)) {
return new Uint8Array();
}
@@ -26,7 +26,7 @@ describe('TextDecoder', (): void => {
});
it('polyfills with no exceptions (with TextDecoder)', (): void => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
(global as any).TextDecoder = require('util').TextDecoder;
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -26,7 +26,7 @@ describe('TextEncoder', (): void => {
});
it('polyfills with no exceptions (with TextEncoder)', (): void => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
(global as any).TextEncoder = require('util').TextEncoder;
// eslint-disable-next-line @typescript-eslint/no-var-requires
+8
View File
@@ -40,3 +40,11 @@ export type BnList = {
0: BN;
1: BN;
} & BN[];
export interface Time {
days: number;
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
}
+1954 -1949
View File
File diff suppressed because it is too large Load Diff