Skip to content

Commit

Permalink
Allow empty string as valid base32/base58/base64 decoder input (#1788)
Browse files Browse the repository at this point in the history
* Allow empty string as valid base32/base58/base64 decoder input

* Remove empty failure

* Validation with optional validation flag

* Empty validation
  • Loading branch information
jacogr authored Mar 31, 2023
1 parent edb6240 commit b794d22
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Contributed:

Changes:

- Allow empty string as valid base32/base58/base64 decoder input (yields empty)
- Add additional/missing `/*#__PURE__*/` annotations
- Upgrade dependencies to latest stable versions

Expand Down
8 changes: 8 additions & 0 deletions packages/util-crypto/src/base32/decode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import { u8aToString } from '@polkadot/util';
import { base32Decode } from './index.js';

describe('base32Decode', (): void => {
it('decodes an empty string)', (): void => {
expect(
u8aToString(
base32Decode('')
)
).toEqual('');
});

it('decodes a base32', (): void => {
expect(
u8aToString(
Expand Down
8 changes: 3 additions & 5 deletions packages/util-crypto/src/base32/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ export function createIs (validate: ValidateFn): ValidateFn {
/** @internal */
export function createValidate ({ chars, ipfs, type }: Config): ValidateFn {
return (value?: unknown, ipfsCompat?: boolean): value is string => {
if (!value || typeof value !== 'string') {
throw new Error(`Expected non-null, non-empty ${type} string input`);
}

if (ipfs && ipfsCompat && value[0] !== ipfs) {
if (typeof value !== 'string') {
throw new Error(`Expected ${type} string input`);
} else if (ipfs && ipfsCompat && value[0] !== ipfs) {
throw new Error(`Expected ipfs-compatible ${type} to start with '${ipfs}'`);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/util-crypto/src/base32/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ describe('base32Validate', (): void => {
).toEqual(true);
});

it('fails on empty', (): void => {
it('does not fail on empty', (): void => {
expect(
() => base32Validate('')
).toThrow(/Expected/);
base32Validate('')
).toEqual(true);
});

it('fails on non-string', (): void => {
Expand Down
6 changes: 6 additions & 0 deletions packages/util-crypto/src/base58/decode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { base32Decode } from '../base32/index.js';
import { base58Decode } from './index.js';

describe('base58Encode', (): void => {
it('decodes an empty string)', (): void => {
expect(
base58Decode('')
).toEqual(new Uint8Array());
});

it('encodes a base58 to a base32', (): void => {
expect(
base58Decode('b2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV')
Expand Down
8 changes: 8 additions & 0 deletions packages/util-crypto/src/base64/decode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import { stringToU8a } from '@polkadot/util';
import { base64Decode } from './index.js';

describe('base64Decode', (): void => {
it('decodes an empty string)', (): void => {
expect(
base64Decode('')
).toEqual(
stringToU8a('')
);
});

it('decodes a mixed base64 utf8 string (1)', (): void => {
expect(
base64Decode('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==')
Expand Down

0 comments on commit b794d22

Please sign in to comment.