Cryptography

The Lisk elements cryptography module provides all the cryptographic functionality necessary when interacting with the Lisk ecosystem.

Installation

How to add the Lisk client as a dependency of your project:

npm install @liskhq/lisk-cryptography@2.5.0

Upgrade

How to perform an upgrade:

npm update @liskhq/lisk-cryptography

Usage

The lisk-cryptography package can be used with both the server-side and client-side code.

In case the lisk-cryptography is used server-side, it is beneficial to speed up the application by using the sodium-native library.

To perform this, expose NACL_FAST=enable as the environment variable with the following command:

export NACL_FAST=enable

To switch back to the default library TweetNaCl.js, which is slower but can also be executed on the client-side, set it to disable as shown below:

export NACL_FAST=disable

Alternatively, it can be unset completely with the following command:

unset NACL_FAST

Constants

Cryptography specific constants are available via the cryptography key as can be seen below:

Usage

import * as cryptography from '@liskhq/lisk-cryptography';

cryptography.constants.SIGNED_MESSAGE_PREFIX; (1)
1 Prefix for signed messages.

Methods for converting between formats

Methods for converting between different formats.

bufferToHex

This converts a buffer or byte array to a hex string.

Syntax

bufferToHex(buffer)

Parameters

buffer: The buffer to convert into hex string format.

Return value

string: The hex string representation of the buffer.

Examples

import * as cryptography from '@liskhq/lisk-cryptography';

const buffer = Buffer.from([0xab, 0xcd, 0x12, 0x34]);
cryptography.bufferToHex(buffer); // 'abcd1234'

getAddressFromPublicKey

This converts a public key into a Lisk address.

Syntax

getAddressFromPublicKey(publicKey)

Parameters

publicKey: This is the public key as string to convert.

Return value

string: This is the Lisk address for the public key.

Examples

const publicKey = '968ba2fa993ea9dc27ed740da0daf49eddd740dbd7cb1cb4fc5db3a20baf341b';
cryptography.getAddressFromPublicKey(publicKey); // '12668885769632475474L'

hexToBuffer

Converts a hexadecimal value string to a buffer.

Syntax

hexToBuffer(hexString)

Parameters

hexString: The string to convert to a buffer.

Return value

buffer: The created buffer.

Examples

const hex = 'abcd1234';
cryptography.hexToBuffer(hex); // <Buffer ab cd 12 34>

intToBuffer

Converts an integer value to a buffer.

Syntax

intToBuffer(intString, byteLength, endianness)

Parameters

  • intString: The integer to convert to a buffer. Can be of type string or number.

  • byteLength: Size of the buffer.

  • endianness: A string value that refers to the order of bytes in the buffer. Default value is big.

Return value

buffer: The created buffer.

Examples

const int = '12345';
cryptography.intToBuffer(int); // <Buffer 30 39>

parseEncryptedPassphrase

This parses an encrypted passphrase string as an object.

Syntax

parseEncryptedPassphrase(encryptedPassphrase)

Parameters

encryptedPassphrase: The stringified encrypted passphrase to parse.

Return value

object: The parsed encrypted passphrase.

Examples

const encryptedPassphrase = 'iterations=1000000&salt=bce40d3176e31998ec435ffc2993b280&cipherText=99bb7eff6755ecfe1dfa0368328c2d10589d7b85a23f75043497d7bdf7f14fb84e8caee1f9bc4b9543ba320e7f10801b0ff2065427d55c3139cf15e3b626b54f73b72a5b993323a6d60ec4aa407472ae&iv=51bcc76bbd0ab97b2292e305&tag=12e8fcfe7ad735fa9957baa48442e205&version=1';
cryptography.parseEncryptedPassphrase(encryptedPassphrase);
/* {
    iterations: 1000000,
    salt: 'bce40d3176e31998ec435ffc2993b280',
    cipherText: '99bb7eff6755ecfe1dfa0368328c2d10589d7b85a23f75043497d7bdf7f14fb84e8caee1f9bc4b9543ba320e7f10801b0ff2065427d55c3139cf15e3b626b54f73b72a5b993323a6d60ec4aa407472ae',
    iv: '51bcc76bbd0ab97b2292e305',
    tag: '12e8fcfe7ad735fa9957baa48442e205',
    version: '1',
} */

stringifyEncryptedPassphrase

This converts an encrypted passphrase object to a string for convenient storage.

Syntax

stringifyEncryptedPassphrase(encryptedPassphrase)

Parameters

encryptedPassphrase: The encrypted passphrase object to convert into a string.

Return value

string: The encrypted passphrase as a string.

Examples

const encryptedPassphrase = cryptography.encryptPassphraseWithPassword(
    'robust swift grocery peasant forget share enable convince deputy road keep cheap',
    'some secure password'
);
cryptography.stringifyEncryptedPassphrase(encryptedPassphrase); // 'iterations=1000000&salt=bce40d3176e31998ec435ffc2993b280&cipherText=99bb7eff6755ecfe1dfa0368328c2d10589d7b85a23f75043497d7bdf7f14fb84e8caee1f9bc4b9543ba320e7f10801b0ff2065427d55c3139cf15e3b626b54f73b72a5b993323a6d60ec4aa407472ae&iv=51bcc76bbd0ab97b2292e305&tag=12e8fcfe7ad735fa9957baa48442e205&version=1'

stringToBuffer

This converts a string value into a Buffer.

Syntax

stringToBuffer(string)

Parameters

string: A string value.

Return value

Buffer: The buffer representation of the string value.

Examples

const someString = "foobar";
cryptography.stringToBuffer(someString); // <Buffer 66 6f 6f 62 61 72>

Methods for encrypting and decrypting

decryptMessageWithPassphrase

This decrypts a message that has been encrypted for a given public key, using the corresponding passphrase as shown below:

Syntax

decryptMessageWithPassphrase(cipherHex, nonce, passphrase, senderPublicKey)

Parameters

cipherHex: The hex string representation of the encrypted message.

nonce: The hex string representation of the nonce used during encryption.

passphrase: The passphrase to be used in decryption.

senderPublicKey: The public key of the message sender, (this is used to ensure the message was signed by the correct person).

Return value

string: The decrypted message.

Examples

const decryptedMessage = cryptography.decryptMessageWithPassphrase(
    '7bef28e1ddb34902d2e006a36062805e597924c9885c142444bafb',
    '5c29c9df3f041529a5f9ba07c444a86cbafbfd21413ec3a7',
    'robust swift grocery peasant forget share enable convince deputy road keep cheap',
    '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f'
); // 'Hello Lisk!'

decryptPassphraseWithPassword

This decrypts a passphrase that has been encrypted using a password.

Syntax

decryptPassphraseWithPassword(encryptedPassphraseObject, password)

Parameters

  • encryptedPassphraseObject: The output of encryptPassphraseWithPassword. Contains iterations, cipherText, iv, salt, tag, and version.

  • password: The password to be used in decryption.

Return value

string: The decrypted passphrase.

Examples

const encryptedPassphrase = {
    iterations: 1000000,
    salt: 'bce40d3176e31998ec435ffc2993b280',
    cipherText: '99bb7eff6755ecfe1dfa0368328c2d10589d7b85a23f75043497d7bdf7f14fb84e8caee1f9bc4b9543ba320e7f10801b0ff2065427d55c3139cf15e3b626b54f73b72a5b993323a6d60ec4aa407472ae',
    iv: '51bcc76bbd0ab97b2292e305',
    tag: '12e8fcfe7ad735fa9957baa48442e205',
    version: '1',
};
const decryptedPassphrase = cryptography.decryptPassphraseWithPassword(
    encryptedPassphrase,
    'some secure password'
); // 'robust swift grocery peasant forget share enable convince deputy road keep cheap'

encryptMessageWithPassphrase

This encrypts a message under a recipient’s public key, using a passphrase to create a signature.

Syntax

encryptMessageWithPassphrase(message, passphrase, recipientPublicKey)

Parameters

message: The plaintext message to encrypt.

passphrase: The passphrase used to sign the encryption and ensure message integrity.

recipientPublicKey: The public key to be used in encryption.

Return value

object: The result of encryption. This contains the nonce and encryptedMessage, both in hex string format.

Examples

const encryptedMessage = cryptography.encryptMessageWithPassphrase(
    'Hello Lisk!',
    'robust swift grocery peasant forget share enable convince deputy road keep cheap',
    '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f'
);
/* {
    encryptedMessage: '7bef28e1ddb34902d2e006a36062805e597924c9885c142444bafb',
    nonce: '5c29c9df3f041529a5f9ba07c444a86cbafbfd21413ec3a7',
} */

encryptPassphraseWithPassword

This encrypts a passphrase under a password for secure storage.

Syntax

encryptPassphraseWithPassword(passphrase, password, [iterations])

Parameters

passphrase: The passphrase in plaintext to encrypt.

password: The password to be used in encryption.

iterations: The number of iterations to use when deriving a key from the password using PBKDF2. (Default if not provided is 1,000,000.)

Return value

object: The result of encryption. This contains the iterations, cipherText, iv, salt, tag and version.

Examples

const encryptedPassphrase = cryptography.encryptPassphraseWithPassword(
    'robust swift grocery peasant forget share enable convince deputy road keep cheap',
    'some secure password',
);
/* {
    iterations: 1000000,
    salt: 'bce40d3176e31998ec435ffc2993b280',
    cipherText: '99bb7eff6755ecfe1dfa0368328c2d10589d7b85a23f75043497d7bdf7f14fb84e8caee1f9bc4b9543ba320e7f10801b0ff2065427d55c3139cf15e3b626b54f73b72a5b993323a6d60ec4aa407472ae',
    iv: '51bcc76bbd0ab97b2292e305',
    tag: '12e8fcfe7ad735fa9957baa48442e205',
    version: '1',
} */

Methods for hashing

getNetworkIdentifier

Syntax

getNetworkIdentifier(genesisBlockPayloadHash, communityIdentifier)

Parameters

  • genesisBlockPayloadHash: The payload hash of the genesis block.

  • communityIdentifier: The community identifier as string.

Return value

string: The ID of the corresponding network as hash string.

Examples

const networkIdentifier = getNetworkIdentifier(
    "23ce0366ef0a14a91e5fd4b1591fc880ffbef9d988ff8bebf8f3666b0c09597d",
    "Lisk",
); // '7158c297294a540bc9ac6e474529c3da38d03ece056e3fa2d98141e6ec54132d'

hash

Hashes an input using the SHA256 algorithm.

Syntax

hash(data, [format])

Parameters

  • data: The data to hash provided as a buffer, or a string.

  • format: The format of the input data if provided as a string. Must be one of hex or utf8.

Return value

buffer: The result of hashing.

Examples

cryptography.hash(Buffer.from([0xab, 0xcd, 0x12, 0x34])); // <Buffer 77 79 07 d5 4b 6a 45 02 bd 65 4c b4 ae 81 c5 f7 27 01 3b 5e 3b 93 cd 8b 53 d7 21 34 42 69 d3 b0>
cryptography.hash('abcd1234', 'hex'); // <Buffer 77 79 07 d5 4b 6a 45 02 bd 65 4c b4 ae 81 c5 f7 27 01 3b 5e 3b 93 cd 8b 53 d7 21 34 42 69 d3 b0>
cryptography.hash('abcd1234', 'utf8'); // <Buffer e9 ce e7 1a b9 32 fd e8 63 33 8d 08 be 4d e9 df e3 9e a0 49 bd af b3 42 ce 65 9e c5 45 0b 69 ae>

hashOnion

Syntax

hashOnion(seed, count, distance)

Parameters

  • seed: The initial seed to generate the hash onion from. The seed is a random 16 bytes number, that can be generated by the function generateHashOnionSeed().

  • count: The amount of layers that the hash onion will have. Default value: 1000000.

  • distance: The distance between checkpoints in the hash onion. Default value: 1000.

Return value

array: A list of the checkpoints of the hash onion. This should be inserted in the config of a node, before enabling forging for a delegate. See the enable forging guide for more information.

Examples

cryptography.hashOnion(seed);
/*
[
  <Buffer a0 e7 59 11 e5 a3 35 ef 1c 6b 77 05 76 fc b4 b9>,
  <Buffer aa 3d ce 47 9e d5 5f 33 6a ea e3 8c be 03 b7 b4>,
  <Buffer ee db b0 3b e5 74 fa 84 35 52 5b d7 ad ad 18 59>,
  <Buffer 9c a7 a2 cd 7d 7e 92 fb 2a b2 37 c5 d0 38 ee 9c>,
  <Buffer ee a3 b2 57 21 b3 dc 50 b6 9f 98 02 39 ae 88 c1>,
  <Buffer 9e a9 aa 0e 12 2e 73 04 6c d9 4d 1b fc 13 82 24>,
  <Buffer a5 cf ce 8d 96 77 9e d6 72 06 52 53 42 30 03 5f>,
  <Buffer 54 b5 ed bf d9 23 c7 74 e4 fe 90 27 fc c2 73 03>,
  <Buffer 17 80 73 53 1f 23 b5 96 88 86 ac 49 f5 94 f6 89>,
  <Buffer 00 20 a0 38 4f c1 f9 b0 6d c0 7f c1 20 4d 44 44>,
  <Buffer 67 6f 76 a0 bd 77 b0 14 e1 73 2f ad 73 cf 4a d6>,
  <Buffer b5 54 cd 16 0e 44 f7 c5 00 12 62 45 d0 22 ec 84>,
  <Buffer e3 b0 69 84 b6 02 d0 49 8c ee 0d 79 30 6d 1a 0d>,
  <Buffer dd 7b ce ec 1b e8 ee a7 91 ab 80 87 cb 24 48 cb>,
  <Buffer 40 93 01 27 43 be 15 ec bb 43 72 78 9f dd ac 18>,
  <Buffer c2 1c 7a b5 04 92 60 08 91 57 aa 39 a6 a0 09 a9>,
  <Buffer ee 2e 02 1c 1d 2e 90 a6 73 9b 97 e4 a6 b6 d0 c8>,
  <Buffer 8f 01 2e 94 b0 6a 36 ec c1 19 13 af d3 a6 5b 50>,
  <Buffer 64 d2 38 52 97 60 ab e8 14 71 1d ea bb 6b 90 b8>,
  <Buffer 0f 14 76 d1 49 d4 a5 dc c8 74 c1 ba 98 a6 5a d5>,
  <Buffer 4c 50 df 93 ed 92 a9 3d f3 53 ca 1d 66 22 44 a5>,
  <Buffer 28 4f 35 c3 24 17 b2 51 27 9f 17 86 8c 72 df 2d>,
  <Buffer ad c1 ef b1 4b 83 7b 01 10 71 0d 7c cf 66 33 3e>,
  <Buffer cd 79 f7 e7 e2 11 2b ff d4 a6 1f 71 12 69 2a c2>,
  <Buffer 09 53 26 3b c0 7d 7b 99 e9 95 3e 1f 59 4c ae ae>,
  <Buffer d5 12 5f 20 59 c9 32 86 27 2c a7 62 e4 1d fb 78>,
  <Buffer 55 ae bf a8 70 73 87 20 14 6e 95 eb 0f f0 8c d0>,
  <Buffer 80 b0 24 f5 10 17 2c e7 f2 62 68 ec a2 4a 85 18>,
  <Buffer e5 2b 36 ff 7d 54 ca 91 8c 95 bd db 14 32 69 c1>,
  <Buffer c3 e1 a0 af 41 42 b5 79 75 f5 9b 1b eb 01 ec 20>,
  <Buffer 3d ab 48 77 b6 ab 2b 96 fa 02 30 d7 6b ed 4e 6f>,
  <Buffer f7 53 49 fc 8b 4e 61 5c a6 49 0c bc 04 52 b5 33>,
  <Buffer 90 98 78 a5 c6 48 ff 67 b3 3c 7f 36 33 3f 92 60>,
  <Buffer 3c 19 51 a7 78 02 65 e3 a9 79 b4 35 6d b3 89 57>,
  <Buffer e3 c4 44 a9 07 f8 87 ca 89 c1 af bb 27 6a de 68>,
  <Buffer bf 0e 1e 26 95 ff eb c1 c7 96 ab 28 68 fc d0 7c>,
  <Buffer 53 34 a0 ed 38 71 77 cb 19 60 0c c4 53 6e 6d ad>,
  <Buffer dd 32 04 04 04 42 ed 6b d6 9b e2 a2 e8 84 db 37>,
  <Buffer 08 c2 7b 5b 76 5a 35 dd a0 bb 1b 85 6e 11 ef e6>,
  <Buffer e6 47 02 6a 45 11 33 67 9f 7d ef 67 ff 48 dd 35>,
  <Buffer 20 27 31 6e 9c f5 56 df 07 71 7b e1 18 66 de b0>,
  <Buffer 3e fb e9 43 91 64 a2 e6 97 f6 7e 55 f2 d9 35 fa>,
  <Buffer 0b 7c 56 3c f3 0d a7 00 80 fa 98 58 f3 ac 65 7a>,
  <Buffer e3 82 ff b9 0e d3 33 37 0c 63 fd 75 a8 d4 91 99>,
  <Buffer f9 65 41 5f 55 a1 b9 7a 92 67 d9 f7 a3 c8 7e 22>,
  <Buffer 1e 53 20 b7 b9 3f ef 14 42 b7 43 07 21 04 de 67>,
  <Buffer 24 b6 b4 18 28 b7 7c 18 28 6e 52 bb 8a 5d 07 47>,
  <Buffer 75 e4 e7 18 f5 c7 12 65 ea 04 2b 75 e7 7e ec 95>,
  <Buffer 52 8d bd b9 86 7c 38 a7 ae 12 cf a5 6b 99 c0 3e>,
  <Buffer 0a 3f 85 13 2f 22 a6 6d f6 b1 0d 55 c5 f2 d6 57>,
  <Buffer 47 c5 92 ed a6 b8 87 f9 75 15 ca df 58 cf 7a 0a>,
  <Buffer 78 6c 8e 8b f6 0e e4 12 b0 a2 f6 e0 de 4f c7 df>,
  <Buffer df f1 1c a8 70 a9 72 c3 34 0b a9 b5 53 92 bb 0a>,
  <Buffer 21 73 86 44 1b cc 89 ac 5d 55 c2 32 e2 b4 9f f2>,
  <Buffer 71 7f f4 15 ee 19 b7 be 74 b5 8e d8 2c c6 3e 84>,
  <Buffer fd b3 63 84 5c e7 05 a2 c5 12 85 e4 a6 d7 60 f7>,
  <Buffer fb a2 3a 25 31 bc 39 eb 5b fe 89 28 57 63 d7 ba>,
  <Buffer c7 ad c8 da 25 a6 8b d1 b8 10 37 15 b8 a3 50 91>,
  <Buffer 6d e2 e4 cc d1 22 92 29 00 ce 7d 0e 7c 1f f8 f9>,
  <Buffer 95 3d cf d7 2e e7 32 68 23 07 ea 7a 29 2b 36 49>,
  <Buffer bb d5 b3 9b 00 d8 32 71 65 5b 54 7e 4d 84 d6 e2>,
  <Buffer ec 6c 6b 9a 11 79 10 79 e2 1a 5f 6b b5 b9 46 4d>,
  <Buffer ed e5 e6 fa 97 e5 78 79 aa 50 86 24 15 34 ec 54>,
  <Buffer 58 33 3b 56 c8 99 91 83 58 af d3 02 de 29 35 21>,
  <Buffer 6d 2e 6c d6 a3 d3 be 47 fe 6b 9f a5 ee a5 9b 6d>,
  <Buffer 1b 3f 77 9a 0e 64 89 c7 f0 e7 e4 51 67 15 63 6a>,
  <Buffer f1 3d 16 2a e2 ad 66 20 d7 c4 f6 af 28 33 70 eb>,
  <Buffer ad 62 11 90 b3 00 39 b1 0e 01 d5 e7 2c 20 ac 95>,
  <Buffer c1 1a f8 37 7c 6b af 06 32 ce a5 7e 4d 39 30 97>,
  <Buffer 93 41 97 81 5c bf e9 7b 12 7d e8 19 7f 00 95 d6>,
  <Buffer 12 39 33 59 c1 85 cc 86 4b bd 3e ca 32 b9 9c 1a>,
  <Buffer 4e ba 98 ae fc 43 f5 d4 87 ec b5 cc 80 d3 49 8b>,
  <Buffer e1 e1 52 cf b5 4e 19 21 37 27 87 44 19 af 6c e3>,
  <Buffer 63 14 2f 0f a0 34 26 81 98 aa ae fb 33 f5 4a be>,
  <Buffer bc d8 93 41 24 b0 23 98 11 4c c8 bd 90 63 73 69>,
  <Buffer 92 93 29 79 d8 11 70 18 7a 01 e5 b4 61 54 ec 53>,
  <Buffer 2d 3e 94 c0 2d b1 f6 f9 17 fc c6 ff 36 b0 f7 9c>,
  <Buffer 80 ec 1e 28 75 5d 9e 2b bf e8 52 25 6f bf 29 0d>,
  <Buffer 83 74 b0 22 61 4a 81 91 d1 e0 96 22 44 50 7a 21>,
  <Buffer f0 d8 41 ff 87 f3 aa 60 18 a9 0a 60 45 bc 12 d2>,
  <Buffer 9e d5 c8 a1 e3 e2 03 22 bf d2 0d 73 f3 91 04 06>,
  <Buffer 5e 4c 73 00 7c 03 60 dd 76 d5 fd f4 71 95 fe 67>,
  <Buffer d0 b1 f9 bf 07 f1 ac a1 74 ad 1e 27 c3 35 c6 11>,
  <Buffer 2f a8 67 de 54 8b af 71 02 96 8d 5d 92 5c d6 33>,
  <Buffer dd c0 0c 30 e8 09 a1 bd 64 78 e6 15 84 d5 29 4c>,
  <Buffer 7a ae 2c 59 47 95 86 0b d5 2c 75 a6 06 a7 58 49>,
  <Buffer 8c 92 87 e4 70 15 ea d7 e3 96 16 bd 0e 84 9f 97>,
  <Buffer b2 7d 9f 31 b0 af c3 3a 83 38 ac b5 46 ac fc d9>,
  <Buffer c4 d5 1b de 4a b0 87 e2 f2 66 8a 6c 19 aa 85 07>,
  <Buffer f4 2f 6e d7 06 11 b5 c1 62 d7 0d fd d3 67 e6 f2>,
  <Buffer 3c 4e 9e 7d d2 74 55 f3 2e 8b ad 2a de e2 07 ca>,
  <Buffer b1 c0 d3 ec fc 1c 76 32 8c bf ae 32 22 a3 13 2f>,
  <Buffer 77 df c2 dd e1 c3 50 15 34 b1 67 4c b8 2f fc 3f>,
  <Buffer c2 c8 7b df 76 a7 c0 9b 1d e9 1c ae 30 eb c0 51>,
  <Buffer 66 73 e9 4a 4a e4 3b 69 92 50 36 64 e0 d9 dd be>,
  <Buffer 66 f0 37 40 7c fd c7 dc 3d af ca 0a 8d 89 e5 05>,
  <Buffer f2 13 0a 29 47 f0 2c 0d 32 b8 75 1c 94 73 31 b7>,
  <Buffer e8 a7 47 57 b9 79 66 d8 68 a8 b1 6e aa 9a d0 8f>,
  <Buffer 3d 1c 52 4d a1 cf 9d d6 89 d0 1c 47 4e 36 e9 68>,
  <Buffer 64 ba 9a 37 11 f1 f0 1f 51 a5 90 da e9 fa 44 39>,
  ... 901 more items
]
*/

generateHashOnionSeed

Syntax

generateHashOnionSeed()

Parameters

None.

Return value

hash: A random 16 bytes number. To be used as a seed parameter in the hashOnion function.

Examples

cryptography.generateHashOnionSeed();
/*
<Buffer ce a9 ab 82 f5 ea f0 9c 5e 75 d2 01 e8 21 de 13>
*/

Methods for managing keys

getAddressAndPublicKeyFromPassphrase

This returns an object containing the address and public key for a provided passphrase.

Syntax

getAddressAndPublicKeyFromPassphrase(passphrase)

Parameters

passphrase: The secret passphrase to process.

Return value

object: This contains an address as a string, and the publicKey as a hex string.

Examples

cryptography.getAddressAndPublicKeyFromPassphrase(
    'robust swift grocery peasant forget share enable convince deputy road keep cheap'
);
/* {
    address: '8273455169423958419L',
    publicKey: '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f',
} */

getAddressFromPassphrase

This returns the Lisk address for a provided passphrase.

Syntax

getAddressFromPassphrase(passphrase)

Parameters

passphrase: The secret passphrase to process.

Return value

string: The address associated with the provided passphrase.

Examples

cryptography.getAddressFromPassphrase(
    'robust swift grocery peasant forget share enable convince deputy road keep cheap'
); //'8273455169423958419L'

getKeys

An alias for getPrivateAndPublicKeyFromPassphrase.

getPrivateAndPublicKeyBytesFromPassphrase

This returns an object containing the private and public keys as Uint8Arrays for a provided passphrase.

Syntax

getPrivateAndPublicKeyBytesFromPassphrase(passphrase)

Parameters

passphrase: The secret passphrase to process.

Return value

object: This contains the privateKey and publicKey as Uint8Arrays.

Examples

cryptography.getPrivateAndPublicKeyBytesFromPassphrase(
    'robust swift grocery peasant forget share enable convince deputy road keep cheap'
);
/* {
    privateKey: [Uint8Array],
    publicKey: [Uint8Array],
} */

getPrivateAndPublicKeyFromPassphrase

This returns an object containing the private and public keys as hex strings for a provided passphrase.

Syntax

getPrivateAndPublicKeyFromPassphrase(passphrase)

Parameters

passphrase: The secret passphrase to process.

Return value

object: This contains the privateKey and publicKey as hex strings.

Examples

cryptography.getPrivateAndPublicKeyFromPassphrase(
    'robust swift grocery peasant forget share enable convince deputy road keep cheap'
);
/* {
    privateKey: 'b092a6664e9eed658ff50fe796ee695b9fe5617e311e9e8a34eb340eb5b831549d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f',
    publicKey: '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f',
} */

Methods for signing and verifying

printSignedMessage

This outputs a string representation of a signed message object which is suitable for printing.

Syntax

printSignedMessage(signedMessageObject)

Parameters

  • signedMessageObject: The result of calling signMessageWithPassphrase or signMessageWithTwoPassphrases.

Return value

string: The string representation of the signed message object.

Examples

const stringToPrint = cryptography.printSignedMessage({
    message: 'Hello Lisk!',
    publicKey: '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f',
    signature: '125febe625b2d62381ff836c020de0b00297f7d2493fe6404bc6109fd70a55348555b7a66a35ac657d338d7fe329efd203da1602f4c88cc21934605676558401',
});
console.log(stringToPrint);
//-----BEGIN LISK SIGNED MESSAGE-----
//-----MESSAGE-----
//Hello Lisk!
//-----PUBLIC KEY-----
//9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f
//-----SIGNATURE-----
//125febe625b2d62381ff836c020de0b00297f7d2493fe6404bc6109fd70a55348555b7a66a35ac657d338d7fe329efd203da1602f4c88cc21934605676558401
//-----END LISK SIGNED MESSAGE-----

signAndPrintMessage

This signs a message with one or two passphrases and outputs a string representation which is suitable for printing.

Syntax

signAndPrintMessage(message, passphrase, [secondPassphrase])

Parameters

message: The string message to sign.

passphrase: The secret passphrase required to sign the message.

secondPassphrase: The optional second secret passphrase to sign the message.

Return value

string: The string representation of the signed message object.

Examples

const stringToPrint = cryptography.signAndPrintMessage('Hello Lisk!',  'robust swift grocery peasant forget share enable convince deputy road keep cheap');
console.log(stringToPrint);
// -----BEGIN LISK SIGNED MESSAGE-----
//-----MESSAGE-----
//Hello Lisk!
//-----PUBLIC KEY-----
//9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f
//-----SIGNATURE-----
//125febe625b2d62381ff836c020de0b00297f7d2493fe6404bc6109fd70a55348555b7a66a35ac657d338d7fe329efd203da1602f4c88cc21934605676558401
//-----END LISK SIGNED MESSAGE-----

signMessageWithPassphrase

Signs a message with a passphrase.

Syntax

signMessageWithPassphrase(message, passphrase)

Parameters

message: The string message to sign.

passphrase: The secret passphrase as string used to sign the message.

Return value

object: This contains the message, and publicKey corresponding to the passphrase and signature as a hex string.

Examples

cryptography.signMessageWithPassphrase('Hello Lisk!',  'robust swift grocery peasant forget share enable convince deputy road keep cheap');
/* {
    message: 'Hello Lisk!',
    publicKey: '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f',
    signature: '125febe625b2d62381ff836c020de0b00297f7d2493fe6404bc6109fd70a55348555b7a66a35ac657d338d7fe329efd203da1602f4c88cc21934605676558401',
} */

verifyMessageWithPublicKey

This verifies that a signature for a given message matches the provided public key.

Syntax

verifyMessageWithPublicKey(signedMessageObject)

Parameters

signedMessageObject: The result of calling signMessageWithPassphrase.

Return value

boolean: Returns true if the signature is valid, and false if not.

Examples

cryptography.verifyMessageWithPublicKey({
    message: 'Hello Lisk!',
    publicKey: '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f',
    signature: '125febe625b2d62381ff836c020de0b00297f7d2493fe6404bc6109fd70a55348555b7a66a35ac657d338d7fe329efd203da1602f4c88cc21934605676558401',
}); // true

verifyMessageWithTwoPublicKeys

This verifies that a signature and second signature for a given message match the provided public keys.

Syntax

verifyMessageWithTwoPublicKeys(signedMessageObject)

Parameters

signedMessageObject: The result of calling signMessageWithTwoPassphrases.

Return value

boolean: Returns true if the signatures are valid, and false if not.

Examples

cryptography.verifyMessageWithTwoPublicKeys({
    message: 'Hello Lisk!',
    publicKey: '9d3058175acab969f41ad9b86f7a2926c74258670fe56b37c429c01fca9f2f0f',
    secondPublicKey: '141b16ac8d5bd150f16b1caa08f689057ca4c4434445e56661831f4e671b7c0a',
    signature: '125febe625b2d62381ff836c020de0b00297f7d2493fe6404bc6109fd70a55348555b7a66a35ac657d338d7fe329efd203da1602f4c88cc21934605676558401',
    secondSignature: '97196d262823166ec9ae5145238479effe00204e763d43cc9539cc711277a6652e8266aace3622f9e8a08cd5de08115c06db15fee71a44a98172cfab58f91c01',
}); // true

signDataWithPrivateKey

Signs data with a private key.

Syntax

signDataWithPrivateKey(data,privatekey)

Parameters

data: The data to be signed, e.g. a transaction object as Buffer. privatekey: The private key if a user account as Buffer.

Return value

Buffer: Returns the signed data.