UNPKG

@iota/checksum

Version:

Add, remove and validate checksums

140 lines 5.49 kB
"use strict"; /** @module checksum */ exports.__esModule = true; var converter_1 = require("@iota/converter"); var kerl_1 = require("@iota/kerl"); var errors_1 = require("../../errors"); var guards_1 = require("../../guards"); exports.errors = { INVALID_ADDRESS: errors_1.INVALID_ADDRESS, INVALID_CHECKSUM: errors_1.INVALID_CHECKSUM, INVALID_TRYTES: errors_1.INVALID_TRYTES, INVALID_CHECKSUM_LENGTH: 'Invalid checksum length' }; var HASH_TRYTES_LENGTH = 81; var ADDRESS_CHECKSUM_TRYTES_LENGTH = 9; var ADDRESS_WITH_CHECKSUM_TRYTES_LENGTH = HASH_TRYTES_LENGTH + ADDRESS_CHECKSUM_TRYTES_LENGTH; var MIN_CHECKSUM_TRYTES_LENGTH = 3; /** * 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. */ function addChecksum(input, checksumLength, isAddress) { if (checksumLength === void 0) { checksumLength = ADDRESS_CHECKSUM_TRYTES_LENGTH; } if (isAddress === void 0) { isAddress = true; } if (!guards_1.isTrytes(input)) { throw new Error(exports.errors.INVALID_TRYTES); } if (isAddress && input.length !== HASH_TRYTES_LENGTH) { if (input.length === ADDRESS_WITH_CHECKSUM_TRYTES_LENGTH) { return input; } throw new Error(exports.errors.INVALID_ADDRESS); } if (!Number.isInteger(checksumLength) || checksumLength < MIN_CHECKSUM_TRYTES_LENGTH || (isAddress && checksumLength !== ADDRESS_CHECKSUM_TRYTES_LENGTH)) { throw new Error(exports.errors.INVALID_CHECKSUM_LENGTH); } var paddedInputTrytes = input; while (paddedInputTrytes.length % HASH_TRYTES_LENGTH !== 0) { paddedInputTrytes += '9'; } var inputTrits = converter_1.trits(paddedInputTrytes); var checksumTrits = new Int8Array(kerl_1["default"].HASH_LENGTH); var kerl = new kerl_1["default"](); kerl.initialize(); kerl.absorb(inputTrits, 0, inputTrits.length); kerl.squeeze(checksumTrits, 0, kerl_1["default"].HASH_LENGTH); return input.concat(converter_1.trytes(checksumTrits.slice(243 - checksumLength * 3, 243))); } exports.addChecksum = addChecksum; /** * 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. */ function removeChecksum(input) { if (!guards_1.isTrytes(input, HASH_TRYTES_LENGTH) && !guards_1.isTrytes(input, ADDRESS_WITH_CHECKSUM_TRYTES_LENGTH)) { throw new Error(exports.errors.INVALID_ADDRESS); } return input.slice(0, HASH_TRYTES_LENGTH); } exports.removeChecksum = removeChecksum; /** * 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. */ exports.isValidChecksum = function (addressWithChecksum) { return addressWithChecksum === addChecksum(removeChecksum(addressWithChecksum)); }; //# sourceMappingURL=index.js.map