UNPKG

@iota/checksum

Version:

Add, remove and validate checksums

94 lines (93 loc) 3.47 kB
/** @module checksum */ import { Trytes } from '../../types'; export declare const errors: { INVALID_ADDRESS: string; INVALID_CHECKSUM: string; INVALID_TRYTES: string; INVALID_CHECKSUM_LENGTH: string; }; /** * This method takes 81 trytes, which could be an address or a seed, generates the [checksum](https://docs.iota.org/docs/getting-started/0.1/clients/checksums) and appends it to the trytes. * * To generate a checksum that is less than 9 trytes long, make sure to set the `isAddress` argument to false. * * ## Related methods * * To generate an address, use the [`getNewAddress()`]{@link #module_core.getNewAddress} method. * * @method addChecksum * * @summary Generates a checksum and appends it to the given trytes. * * @memberof module:checksum * * @param {string} input - 81 trytes to which to append the checksum * * @param {number} [checksumLength=9] - Length of the checksum to generate * * @param {boolean} [isAddress=true] - Whether the input is an address * * @example * ```js * let addressWithChecksum = Checksum.addChecksum('ADDRESS...'); * ``` * * @returns {string} The original trytes with an appended checksum. * * @throws {errors.INVALID_ADDRESS}: Make sure that the given address is 90 trytes long. * @throws {errors.INVALID_TRYTES}: Make sure that the `input` argument contains only [trytes](https://docs.iota.org/docs/getting-started/0.1/introduction/ternary) * @throws {errors.INVALID_CHECKSUM_LENGTH}: Make sure that the `checksumLength` argument is a number greater than or equal to 3. If the `isAddress` argument is set to true, make sure that the `checksumLength` argument is 9. */ export declare function addChecksum(input: Trytes, checksumLength?: number, isAddress?: boolean): Trytes; /** * This method takes an address of 90 trytes, and removes the last 9 trytes to return the address without a checksum. * * ## Related methods * * To generate an address, use the [`getNewAddress()`]{@link #module_core.getNewAddress} method. * To add a checksum to an address, use the [`addChecksum()`]{@link #module_checksum.addChecksum} method. * * @method removeChecksum * * @summary Removes the checksum from the given address. * * @memberof module:checksum * * @param {string} input - Address from which to remove the checksum * * @example * ```js * let addressWithoutChecksum = Checksum.removeChecksum('ADDRESS...'); * ``` * * @returns {string} The original address without the appended checksum. * * @throws {errors.INVALID_ADDRESS}: Make sure that the given address is 90 trytes long. */ export declare function removeChecksum(input: Trytes): Trytes; /** * This method takes an address of 90 trytes, and checks if the checksum is valid. * * ## Related methods * * To generate an address, use the [`getNewAddress()`]{@link #module_core.getNewAddress} method. * To add a checksum to an address, use the [`addChecksum()`]{@link #module_checksum.addChecksum} method. * * @method isValidChecksum * * @summary Validates the checksum of an address. * * @memberof module:checksum * * @param {string} addressWithChecksum - Address with a checksum * * @example * ```js * let valid = Checksum.isValidChecksum('ADDRESS...'); * ``` * * @returns {boolean} Whether the checksum is valid. * * @throws {errors.INVALID_ADDRESS}: Make sure that the given address is 90 trytes long. */ export declare const isValidChecksum: (addressWithChecksum: string) => boolean;