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}`
+1 -3
View File
@@ -1,9 +1,7 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import baseX from 'base-x';
export { base58 } from 'micro-base';
// https://github.com/cryptocoinjs/base-x#alphabets
export const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
export const bs58 = baseX(BASE58_ALPHABET);
@@ -0,0 +1,23 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { base32Decode } from '../base32';
import { base58Decode } from './';
describe('base58Encode', (): void => {
it('encodes a base58 to a base32', (): void => {
expect(
base58Decode('b2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV')
).toEqual(
base32Decode('afkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
);
});
it('encodes a base58 to a base32 (ipfs-compat)', (): void => {
expect(
base58Decode('zb2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV', true)
).toEqual(
base32Decode('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
);
});
});
+6 -4
View File
@@ -1,9 +1,7 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { bufferToU8a } from '@polkadot/util';
import { bs58 } from './bs58';
import { base58 } from './bs58';
import { base58Validate } from './validate';
/**
@@ -15,5 +13,9 @@ import { base58Validate } from './validate';
export function base58Decode (value: string, ipfsCompat?: boolean): Uint8Array {
base58Validate(value, ipfsCompat);
return bufferToU8a(bs58.decode(value.substr(ipfsCompat ? 1 : 0)));
return base58.decode(
ipfsCompat
? value.substr(1)
: value
);
}
@@ -5,7 +5,15 @@ import { base32Decode } from '../base32';
import { base58Encode } from './';
describe('base58Encode', (): void => {
it('encodes a base32 to a base38', (): void => {
it('encodes a base32 to a base58', (): void => {
expect(
base58Encode(
base32Decode('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true)
)
).toEqual('b2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV');
});
it('encodes a base32 to a base58 (ipfs-compat)', (): void => {
expect(
base58Encode(
base32Decode('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', true),
+2 -2
View File
@@ -5,7 +5,7 @@ import type { U8aLike } from '@polkadot/util/types';
import { u8aToU8a } from '@polkadot/util';
import { bs58 } from './bs58';
import { base58 } from './bs58';
/**
* @name base58Encode
@@ -14,7 +14,7 @@ import { bs58 } from './bs58';
* From the provided input, create the base58 and return the result as a string.
*/
export function base58Encode (value: U8aLike, ipfsCompat?: boolean): string {
const out = bs58.encode(u8aToU8a(value));
const out = base58.encode(u8aToU8a(value));
return ipfsCompat
? `z${out}`
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { base64 } from 'micro-base';
export const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
+2 -3
View File
@@ -1,8 +1,7 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import base64 from 'base64-js';
import { base64 } from './bs64';
import { base64Validate } from './validate';
/**
@@ -14,5 +13,5 @@ import { base64Validate } from './validate';
export function base64Decode (value: string): Uint8Array {
base64Validate(value);
return base64.toByteArray(value);
return base64.decode(value);
}
+3 -3
View File
@@ -3,10 +3,10 @@
import type { U8aLike } from '@polkadot/util/types';
import base64 from 'base64-js';
import { u8aToU8a } from '@polkadot/util';
import { base64 } from './bs64';
/**
* @name base64Encode
* @summary Creates a base64 value.
@@ -14,5 +14,5 @@ import { u8aToU8a } from '@polkadot/util';
* From the provided input, create the base64 and return the result as a string.
*/
export function base64Encode (value: U8aLike): string {
return base64.fromByteArray(u8aToU8a(value));
return base64.encode(u8aToU8a(value));
}