UNPKG

967 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { base58Decode } from "../base58/index.js";
4import { checkAddressChecksum } from "./checksum.js";
5import { defaults } from "./defaults.js";
6/**
7 * @name checkAddress
8 * @summary Validates an ss58 address.
9 * @description
10 * From the provided input, validate that the address is a valid input.
11 */
12
13export function checkAddress(address, prefix) {
14 let decoded;
15
16 try {
17 decoded = base58Decode(address);
18 } catch (error) {
19 return [false, error.message];
20 }
21
22 const [isValid,,, ss58Decoded] = checkAddressChecksum(decoded);
23
24 if (ss58Decoded !== prefix) {
25 return [false, `Prefix mismatch, expected ${prefix}, found ${ss58Decoded}`];
26 } else if (!defaults.allowedEncodedLengths.includes(decoded.length)) {
27 return [false, 'Invalid decoded address length'];
28 }
29
30 return [isValid, isValid ? null : 'Invalid decoded address checksum'];
31}
\No newline at end of file