Use micro-base for base{32, 58, 64} (#1209)

This commit is contained in:
Jaco
2021-11-19 11:27:47 +02:00
committed by GitHub
parent 222bb74a81
commit 4d77bf3c5b
15 changed files with 89 additions and 87 deletions
+12 -2
View File
@@ -1,6 +1,16 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// https://github.com/multiformats/js-multibase/blob/master/src/constants.js
import { utils } from 'micro-base';
export const BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567';
export const BITS_PER_CHAR = 5;
// We define our own chain, the default base32 has padding
export const base32 = utils.chain(
utils.radix2(5),
utils.alphabet(BASE32_ALPHABET),
{
decode: (input: string) => input.split(''),
encode: (input: string[]) => input.join('')
}
);
+5 -40
View File
@@ -1,39 +1,9 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// 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 { base32 } from './bs32';
import { base32Validate } from './validate';
// Build the character lookup table:
const LOOKUP = BASE32_ALPHABET.split('').reduce<Record<string, number>>((lookup, a, i) => {
lookup[a] = i;
return lookup;
}, {});
/** @internal */
function decode (output: Uint8Array, input: string, offset: number): [Uint8Array, number, number] {
let bits = 0;
let buffer = 0;
let written = 0;
for (let i = offset; i < input.length; i++) {
buffer = (buffer << BITS_PER_CHAR) | LOOKUP[input[i]];
bits += BITS_PER_CHAR;
if (bits >= 8) {
bits -= 8;
output[written++] = 0xff & (buffer >> bits);
}
}
return [output, bits, buffer];
}
/**
* @name base32Decode
* @summary Delookup a base32 value.
@@ -43,14 +13,9 @@ function decode (output: Uint8Array, input: string, offset: number): [Uint8Array
export function base32Decode (value: string, ipfsCompat = false): Uint8Array {
base32Validate(value, ipfsCompat);
const offset = (ipfsCompat ? 1 : 0);
const [output, bits, buffer] = decode(
new Uint8Array(((value.length - offset) * BITS_PER_CHAR / 8) | 0),
value,
offset
return base32.decode(
ipfsCompat
? value.substr(1)
: value
);
assert(!(bits >= BITS_PER_CHAR || 0xff & (buffer << (8 - bits))), 'Unexpected end of data');
return output;
}
@@ -11,6 +11,12 @@ describe('base32Encode', (): void => {
).toEqual('irswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb');
});
it('encodes to a base32 (ipfs-compat)', (): void => {
expect(
base32Encode('Decentralize everything!!', true)
).toEqual('birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb');
});
it('encodes a base58 to a base32', (): void => {
expect(
base32Encode(
+2 -23
View File
@@ -1,15 +1,11 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// adapted from https://github.com/multiformats/js-multibase/blob/424709195b46ffb1d6f2f69a7707598ebe751e5e/src/rfc4648.js
import type { U8aLike } from '@polkadot/util/types';
import { u8aToU8a } from '@polkadot/util';
import { BASE32_ALPHABET, BITS_PER_CHAR } from './bs32';
const MASK = (1 << BITS_PER_CHAR) - 1;
import { base32 } from './bs32';
/**
* @name base32Encode
@@ -18,24 +14,7 @@ const MASK = (1 << BITS_PER_CHAR) - 1;
* From the provided input, create the base32 and return the result as a string.
*/
export function base32Encode (value: U8aLike, 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))];
}
const out = base32.encode(u8aToU8a(value));
return ipfsCompat
? `b${out}`