42 lines
1.6 KiB
TypeScript
42 lines
1.6 KiB
TypeScript
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Prefix } from './types';
|
|
|
|
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6
|
|
import { assert, isHex, isU8a, u8aToU8a } from '@polkadot/util';
|
|
|
|
import { base58Decode } from '../base58/decode';
|
|
import { checkAddressChecksum } from './checksum';
|
|
import { defaults } from './defaults';
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
export function decodeAddress (encoded: string | Uint8Array, ignoreChecksum?: boolean, ss58Format: Prefix = -1): Uint8Array {
|
|
if (isU8a(encoded) || isHex(encoded)) {
|
|
return u8aToU8a(encoded);
|
|
}
|
|
|
|
const wrapError = (message: string) => `Decoding ${encoded as string}: ${message}`;
|
|
let decoded;
|
|
|
|
try {
|
|
decoded = base58Decode(encoded);
|
|
} catch (error) {
|
|
throw new Error(wrapError((error as Error).message));
|
|
}
|
|
|
|
// assert(defaults.allowedPrefix.includes(decoded[0] as Prefix), error('Invalid decoded address prefix'));
|
|
assert(defaults.allowedEncodedLengths.includes(decoded.length), wrapError('Invalid decoded address length'));
|
|
|
|
// TODO Unless it is an "use everywhere" prefix, throw an error
|
|
// if (ss58Format !== -1 && (decoded[0] !== ss58Format)) {
|
|
// console.log(`WARN: Expected ${ss58Format}, found ${decoded[0]}`);
|
|
// }
|
|
|
|
const [isValid, endPos] = checkAddressChecksum(decoded);
|
|
|
|
assert(ignoreChecksum || isValid, wrapError('Invalid decoded address checksum'));
|
|
|
|
return decoded.slice(1, endPos);
|
|
}
|