@iota/checksum
Add, remove and validate checksums.Installation
Install using npm:npm install @iota/checksum
or using yarn:
yarn add @iota/checksum
API Reference
* [.addChecksum(input, [checksumLength], [isAddress])](#module_checksum.addChecksum)
* [.removeChecksum(input)](#module_checksum.removeChecksum)
* [.isValidChecksum(addressWithChecksum)](#module_checksum.isValidChecksum)
checksum.addChecksum(input, checksumLength, isAddress)
Summary: Generates a checksum and appends it to the given trytes.Throws:
errors.INVALID\_ADDRESS
: Make sure that the given address is 90 trytes long.errors.INVALID\_TRYTES
: Make sure that theinput
argument contains only tryteserrors.INVALID\_CHECKSUM\_LENGTH
: Make sure that thechecksumLength
argument is a number greater than or equal to 3. If theisAddress
argument is set to true, make sure that thechecksumLength
argument is 9.
| Param | Type | Default | Description | | --- | --- | --- | --- | | input |
string
| | 81 trytes to which to append the checksum |
| checksumLength | number
| 9
| Length of the checksum to generate |
| isAddress | boolean
| true
| Whether the input is an address |This method takes 81 trytes, which could be an address or a seed, generates the checksum 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 thegetNewAddress()
method.Returns:
string
- The original trytes with an appended checksum.Example
let addressWithChecksum = Checksum.addChecksum('ADDRESS...');
checksum.removeChecksum(input)
Summary: Removes the checksum from the given address.Throws:
errors.INVALID\_ADDRESS
: Make sure that the given address is 90 trytes long.
| Param | Type | Description | | --- | --- | --- | | input |
string
| Address from which to remove the checksum |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 thegetNewAddress()
method.
To add a checksum to an address, use the addChecksum()
method.Returns:
string
- The original address without the appended checksum.Example
let addressWithoutChecksum = Checksum.removeChecksum('ADDRESS...');
checksum.isValidChecksum(addressWithChecksum)
Summary: Validates the checksum of an address.Throws:
errors.INVALID\_ADDRESS
: Make sure that the given address is 90 trytes long.
| Param | Type | Description | | --- | --- | --- | | addressWithChecksum |
string
| Address with a checksum |This method takes an address of 90 trytes, and checks if the checksum is valid.
Related methods
To generate an address, use thegetNewAddress()
method.
To add a checksum to an address, use the addChecksum()
method.Returns:
boolean
- Whether the checksum is valid.Example
let valid = Checksum.isValidChecksum('ADDRESS...');