isAddress/validateAddress functions (#946)
This commit is contained in:
@@ -4,8 +4,10 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Add `isAddress` and `validateAddress` functions (the latter will throw if invalid)
|
||||
- Check `@polkadot/hw-ledger` against `@polkadot/networks` for discrepancies (test-only)
|
||||
|
||||
|
||||
## 6.1.1 Apr 11, 2021
|
||||
|
||||
Contributed:
|
||||
|
||||
@@ -10,7 +10,9 @@ import { base58Decode } from '../base58/decode';
|
||||
import { checkAddressChecksum } from './checksum';
|
||||
import { defaults } from './defaults';
|
||||
|
||||
export function decodeAddress (encoded: string | Uint8Array, ignoreChecksum?: boolean, ss58Format: Prefix = -1): Uint8Array {
|
||||
export function decodeAddress (encoded?: string | Uint8Array | null, ignoreChecksum?: boolean, ss58Format: Prefix = -1): Uint8Array {
|
||||
assert(encoded, 'Invalid empty address passed');
|
||||
|
||||
if (isU8a(encoded) || isHex(encoded)) {
|
||||
return u8aToU8a(encoded);
|
||||
}
|
||||
|
||||
@@ -13,5 +13,7 @@ export { encodeDerivedAddress } from './encodeDerived';
|
||||
export { encodeMultiAddress } from './encodeMulti';
|
||||
export { evmToAddress } from './evmToAddress';
|
||||
export { addressEq } from './eq';
|
||||
export { isAddress } from './is';
|
||||
export { setSS58Format } from './setSS58Format';
|
||||
export { sortAddresses } from './sort';
|
||||
export { validateAddress } from './validate';
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Prefix } from './types';
|
||||
|
||||
import { validateAddress } from './validate';
|
||||
|
||||
export function isAddress (address?: string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): address is string {
|
||||
try {
|
||||
return validateAddress(address, ignoreChecksum, ss58Format);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Prefix } from './types';
|
||||
|
||||
import { decodeAddress } from './decode';
|
||||
|
||||
export function validateAddress (encoded?: string | null, ignoreChecksum?: boolean, ss58Format?: Prefix): encoded is string {
|
||||
return !!decodeAddress(encoded, ignoreChecksum, ss58Format);
|
||||
}
|
||||
@@ -11,6 +11,6 @@ export function testValidator (validate: (value?: string | null, ipfsCompat?: bo
|
||||
}
|
||||
}
|
||||
|
||||
export function isBase32 (value?: string | null, ipfsCompat?: boolean): boolean {
|
||||
export function isBase32 (value?: string | null, ipfsCompat?: boolean): value is string {
|
||||
return testValidator(base32Validate, value, ipfsCompat);
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ const BASE_CONFIG = { alphabet: BASE32_ALPHABET, ipfsChar: 'b', type: 'base32' }
|
||||
* @name base32Validate
|
||||
* @summary Validates a base32 value.
|
||||
* @description
|
||||
* Validates the the supplied value is valid base32
|
||||
* Validates that the supplied value is valid base32
|
||||
*/
|
||||
export function base32Validate (value?: string | null, ipfsCompat?: boolean): true {
|
||||
export function base32Validate (value?: string | null, ipfsCompat?: boolean): value is string {
|
||||
return validateChars(BASE_CONFIG, value, ipfsCompat);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@
|
||||
import { testValidator } from '../base32/is';
|
||||
import { base58Validate } from './validate';
|
||||
|
||||
export function isBase58 (value?: string | null, ipfsCompat?: boolean): boolean {
|
||||
export function isBase58 (value?: string | null, ipfsCompat?: boolean): value is string {
|
||||
return testValidator(base58Validate, value, ipfsCompat);
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?
|
||||
* @name base58Validate
|
||||
* @summary Validates a base58 value.
|
||||
* @description
|
||||
* Validates the the supplied value is valid base58
|
||||
* Validates that the supplied value is valid base58
|
||||
*/
|
||||
export function base58Validate (value?: string | null, ipfsCompat?: boolean): true {
|
||||
export function base58Validate (value?: string | null, ipfsCompat?: boolean): value is string {
|
||||
return validateChars(BASE_CONFIG, value, ipfsCompat);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { base64Validate } from './validate';
|
||||
|
||||
export function isBase64 (value?: string | null): boolean {
|
||||
export function isBase64 (value?: string | null): value is string {
|
||||
try {
|
||||
return base64Validate(value);
|
||||
} catch (error) {
|
||||
|
||||
@@ -7,9 +7,9 @@ import { assert } from '@polkadot/util';
|
||||
* @name base64Validate
|
||||
* @summary Validates a base64 value.
|
||||
* @description
|
||||
* Validates the the supplied value is valid base64
|
||||
* Validates that the supplied value is valid base64
|
||||
*/
|
||||
export function base64Validate (value?: string | null): true {
|
||||
export function base64Validate (value?: string | null): value is string {
|
||||
assert(value, 'Expected non-null, non-empty base64 input');
|
||||
assert(/^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value), 'Invalid base64 encoding');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user