diff --git a/CHANGELOG.md b/CHANGELOG.md index a124856eb..4f81e2d37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## 3.1.0-beta.x + +- Use `scrypt` from WASM as available +- Add `base32{Encode, Decode}` utilities +- Support IPFS compatibility flag in base58 encoding/decoding +- Update to `@polkadot/wasm-crypto` 1.3.1 + ## 3.0.1 Jul 27, 2020 - Add Katal ss58Format (Thanks to https://github.com/retotrinkler) diff --git a/packages/util-crypto/src/base32/bs32.ts b/packages/util-crypto/src/base32/bs32.ts new file mode 100644 index 000000000..7c0942459 --- /dev/null +++ b/packages/util-crypto/src/base32/bs32.ts @@ -0,0 +1,12 @@ +// 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. + +// https://github.com/multiformats/js-multibase/blob/master/src/constants.js +const BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567'; +const BITS_PER_CHAR = 5; + +export { + BASE32_ALPHABET, + BITS_PER_CHAR +}; diff --git a/packages/util-crypto/src/base32/decode.spec.ts b/packages/util-crypto/src/base32/decode.spec.ts new file mode 100644 index 000000000..62b3d2b0b --- /dev/null +++ b/packages/util-crypto/src/base32/decode.spec.ts @@ -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 { u8aToString } from '@polkadot/util'; + +import { base32Decode } from './'; + +describe('base32Decode', (): void => { + it('decodes a base32', (): void => { + expect( + u8aToString( + base32Decode('irswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb') + ) + ).toEqual('Decentralize everything!!'); + }); + + it('decodes a base32 (ipfsCompat)', (): void => { + expect( + u8aToString( + base32Decode('birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb', true) + ) + ).toEqual('Decentralize everything!!'); + }); +}); diff --git a/packages/util-crypto/src/base32/decode.ts b/packages/util-crypto/src/base32/decode.ts new file mode 100644 index 000000000..5ad5e9015 --- /dev/null +++ b/packages/util-crypto/src/base32/decode.ts @@ -0,0 +1,54 @@ +// 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. + +// adapted from https://github.com/multiformats/js-multibase/blob/424709195b46ffb1d6f2f69a7707598ebe751e5e/src/rfc4648.js + +import { assert } from '@polkadot/util'; + +import { BASE32_ALPHABET, BITS_PER_CHAR } from './bs32'; +import validate from './validate'; + +// Build the character lookup table: +const LOOKUP = BASE32_ALPHABET + .split('') + .reduce((lookup: Record, char: string, index: number): Record => { + lookup[char] = index; + + return lookup; + }, {}); + +/** + * @name base32Decode + * @summary Delookup a base32 value. + * @description + * From the provided input, decode the base32 and return the result as an `Uint8Array`. + */ +export default function base32Decode (value: string, ipfsCompat = false): Uint8Array { + if (ipfsCompat) { + assert(value[0] === 'b', "Expected IPFS base32 identifier 'b' at string start"); + + value = value.substr(1); + } + + validate(value); + + const out = new Uint8Array((value.length * BITS_PER_CHAR / 8) | 0); + let bits = 0; + let buffer = 0; + let written = 0; + + for (let i = 0; i < value.length; ++i) { + buffer = (buffer << BITS_PER_CHAR) | LOOKUP[value[i]]; + bits += BITS_PER_CHAR; + + if (bits >= 8) { + bits -= 8; + out[written++] = 0xff & (buffer >> bits); + } + } + + assert(!(bits >= BITS_PER_CHAR || 0xff & (buffer << (8 - bits))), 'Unexpected end of data'); + + return out; +} diff --git a/packages/util-crypto/src/base32/encode.spec.ts b/packages/util-crypto/src/base32/encode.spec.ts new file mode 100644 index 000000000..c129cba31 --- /dev/null +++ b/packages/util-crypto/src/base32/encode.spec.ts @@ -0,0 +1,23 @@ +// 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 { base58Decode } from '../base58'; +import { base32Encode } from './'; + +describe('base32Encode', (): void => { + it('encodes to a base32', (): void => { + expect( + base32Encode('Decentralize everything!!') + ).toEqual('irswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb'); + }); + + it('encodes a base58 to a base32', (): void => { + expect( + base32Encode( + base58Decode('zb2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV', true), + true + ) + ).toEqual('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy'); + }); +}); diff --git a/packages/util-crypto/src/base32/encode.ts b/packages/util-crypto/src/base32/encode.ts new file mode 100644 index 000000000..4040a26ad --- /dev/null +++ b/packages/util-crypto/src/base32/encode.ts @@ -0,0 +1,42 @@ +// 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. + +// adapted from https://github.com/multiformats/js-multibase/blob/424709195b46ffb1d6f2f69a7707598ebe751e5e/src/rfc4648.js + +import { u8aToU8a } from '@polkadot/util'; + +import { BASE32_ALPHABET, BITS_PER_CHAR } from './bs32'; + +const MASK = (1 << BITS_PER_CHAR) - 1; + +/** + * @name base32Encode + * @summary Creates a base32 value. + * @description + * From the provided input, create the base32 and return the result as a string. + */ +export default function base32Encode (value: Uint8Array | string | Buffer | number[], ipfsCompat = false): string { + const u8a = u8aToU8a(value); + let out = ''; + let bits = 0; + let buffer = 0; + + for (let i = 0; i < u8a.length; ++i) { + buffer = (buffer << 8) | u8a[i]; + bits += 8; + + while (bits > BITS_PER_CHAR) { + bits -= BITS_PER_CHAR; + out += BASE32_ALPHABET[MASK & (buffer >> bits)]; + } + } + + if (bits) { + out += BASE32_ALPHABET[MASK & (buffer << (BITS_PER_CHAR - bits))]; + } + + return ipfsCompat + ? `b${out}` + : out; +} diff --git a/packages/util-crypto/src/base32/index.ts b/packages/util-crypto/src/base32/index.ts new file mode 100644 index 000000000..01a852c4e --- /dev/null +++ b/packages/util-crypto/src/base32/index.ts @@ -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 base32 values + */ + +export { default as base32Decode } from './decode'; +export { default as base32Encode } from './encode'; +export { default as base32Validate } from './validate'; diff --git a/packages/util-crypto/src/base32/validate.ts b/packages/util-crypto/src/base32/validate.ts new file mode 100644 index 000000000..89eb48385 --- /dev/null +++ b/packages/util-crypto/src/base32/validate.ts @@ -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 { validateChars } from '../base58/validate'; +import { BASE32_ALPHABET } from './bs32'; + +/** + * @name base32Validate + * @summary Validates a base32 value. + * @description + * Validates the the supplied value is valid base32 + */ +export default function base32Validate (value: string): true { + return validateChars('base32', BASE32_ALPHABET, value); +} diff --git a/packages/util-crypto/src/base58/decode.ts b/packages/util-crypto/src/base58/decode.ts index a8b9d538f..0e01dbccb 100644 --- a/packages/util-crypto/src/base58/decode.ts +++ b/packages/util-crypto/src/base58/decode.ts @@ -2,7 +2,7 @@ // 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 { assert, bufferToU8a } from '@polkadot/util'; import { bs58 } from './bs58'; import validate from './validate'; @@ -13,7 +13,13 @@ import validate from './validate'; * @description * From the provided input, decode the base58 and return the result as an `Uint8Array`. */ -export default function base58Decode (value: string): Uint8Array { +export default function base58Decode (value: string, ipfsCompat = false): Uint8Array { + if (ipfsCompat) { + assert(value[0] === 'z', "Expected IPFS base32 identifier 'z' at string start"); + + value = value.substr(1); + } + validate(value); return bufferToU8a(bs58.decode(value)); diff --git a/packages/util-crypto/src/base58/encode.spec.ts b/packages/util-crypto/src/base58/encode.spec.ts new file mode 100644 index 000000000..923e1ccab --- /dev/null +++ b/packages/util-crypto/src/base58/encode.spec.ts @@ -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 { base32Decode } from '../base32'; +import { base58Encode } from './'; + +describe('base58Encode', (): void => { + it('encodes a base32 to a base38', (): void => { + expect( + base58Encode( + base32Decode('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true), + true + ) + ).toEqual('zb2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV'); + }); +}); diff --git a/packages/util-crypto/src/base58/encode.ts b/packages/util-crypto/src/base58/encode.ts index 5f1df9457..2d6d5be01 100644 --- a/packages/util-crypto/src/base58/encode.ts +++ b/packages/util-crypto/src/base58/encode.ts @@ -12,8 +12,12 @@ import { bs58 } from './bs58'; * @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( +export default function base58Encode (value: Uint8Array | string | Buffer | number[], ipfsCompat = false): string { + const out = bs58.encode( u8aToBuffer(u8aToU8a(value)) ); + + return ipfsCompat + ? `z${out}` + : out; } diff --git a/packages/util-crypto/src/base58/validate.ts b/packages/util-crypto/src/base58/validate.ts index a88f1046f..532b995fc 100644 --- a/packages/util-crypto/src/base58/validate.ts +++ b/packages/util-crypto/src/base58/validate.ts @@ -4,6 +4,16 @@ import { BASE58_ALPHABET } from './bs58'; +export function validateChars (type: string, alphabet: string, value: string): true { + for (let i = 0; i < value.length; i++) { + if (!alphabet.includes(value[i])) { + throw new TypeError(`Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`); + } + } + + return true; +} + /** * @name base58Validate * @summary Validates a base58 value. @@ -11,11 +21,5 @@ import { BASE58_ALPHABET } from './bs58'; * 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; + return validateChars('base58', BASE58_ALPHABET, value); }