Compare commits

...
4 Commits
Author SHA1 Message Date
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
19 changed files with 546 additions and 459 deletions
+5
View File
@@ -1,5 +1,10 @@
# CHANGELOG
## 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
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "2.15.1"
"version": "2.16.1"
}
+3 -3
View File
@@ -22,9 +22,9 @@
},
"devDependencies": {
"@babel/core": "^7.10.3",
"@polkadot/dev": "^0.55.11",
"@polkadot/dev": "^0.55.13",
"@polkadot/ts": "^0.3.26",
"@types/jest": "^26.0.0"
"@types/jest": "^26.0.3"
},
"version": "2.15.1"
"version": "2.16.1"
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/keyring",
"version": "2.15.1",
"version": "2.16.1",
"description": "Keyring management",
"main": "index.js",
"publishConfig": {
@@ -28,7 +28,7 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
"dependencies": {
"@babel/runtime": "^7.10.3",
"@polkadot/util": "2.15.1",
"@polkadot/util-crypto": "2.15.1"
"@polkadot/util": "2.16.1",
"@polkadot/util-crypto": "2.16.1"
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-crypto",
"version": "2.15.1",
"version": "2.16.1",
"description": "A collection of useful crypto utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -26,7 +26,7 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.10.3",
"@polkadot/util": "2.15.1",
"@polkadot/util": "2.16.1",
"@polkadot/wasm-crypto": "^1.2.1",
"base-x": "^3.0.8",
"bip39": "^3.0.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);
}
+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))
);
}
+2 -2
View File
@@ -5,7 +5,7 @@
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 addressEq from './eq';
@@ -16,8 +16,8 @@ export {
addressEq,
checkAddress,
checkAddressChecksum,
createKeyDerived,
createKeyMulti,
createKeySub,
decodeAddress,
encodeAddress,
setSS58Format,
@@ -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 => {
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,
+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 bs58 from 'bs58';
import { bufferToU8a } from '@polkadot/util';
// copied from bs58
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
/**
* @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 {
for (let i = 0; i < value.length; i++) {
if (!BASE58_ALPHABET.includes(value[i])) {
throw new Error(`Invalid base58 character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
}
}
return bufferToU8a(bs58.decode(value));
}
+18
View File
@@ -0,0 +1,18 @@
// 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 bs58 from 'bs58';
import { u8aToBuffer, u8aToU8a } from '@polkadot/util';
/**
* @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))
);
}
+10
View File
@@ -0,0 +1,10 @@
// 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';
+1
View File
@@ -10,6 +10,7 @@ export * from './init';
// all internal exports
export * from './address';
export * from './base58';
export * from './blake2';
export * from './keccak';
export * from './key';
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "2.15.1",
"version": "2.16.1",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"keywords": [
+429 -428
View File
File diff suppressed because it is too large Load Diff