utils/key-utils.js

Utility encryption related functions for KeyManager module.
Author:
  • James Aman (j.aman@topl.me)
  • Raul Aragonez (r.aragonez@topl.me)
Source:

Methods

(inner) create(params) → {Object}

Generate random numbers for private key, initialization vector, and salt (for key derivation).
Source:
Parameters:
Name Type Description
params Object Encryption options.
Name Type Description
keyBytes string Private key size in bytes.
ivBytes string Initialization vector size in bytes.
Returns:
Type:
Object
Keys, IV and salt.

(inner) decrypt(ciphertext, key, iv, algoopt) → {Buffer}

Symmetric privateKey + secretKey decryption using secret (derived) key.
Source:
Parameters:
Name Type Attributes Description
ciphertext Buffer | string Data to be decrypted.
key Buffer | string derived key.
iv Buffer | string Initialization vector.
algo string <optional>
Encryption algorithm (default: constants.cipher).
Returns:
Type:
Buffer
Decrypted data.

(inner) deriveKey(password, salt, kdfParamsopt) → {Buffer}

Derive secret key from password with key derivation function.
Source:
Parameters:
Name Type Attributes Description
password String | Buffer User-supplied password.
salt String | Buffer Randomly generated salt.
kdfParams Object <optional>
key-derivation parameters
Returns:
Type:
Buffer
Secret key derived from password.

(inner) dump(password, keyObject, options) → {Object}

Export private key to keystore secret-storage format.
Source:
Parameters:
Name Type Description
password string | Buffer User-supplied password.
keyObject Object Object containing the raw public / private keypair
options Buffer encryption algorithm to be used
Returns:
Type:
Object
keyStorage for use with exportToFile

(inner) encrypt(plaintext, key, iv, algoopt) → {Buffer}

Symmetric privateKey + secretKey encryption using secret (derived) key.
Source:
Parameters:
Name Type Attributes Description
plaintext Buffer | string Data to be encrypted.
key Buffer | string Secret key.
iv Buffer | string Initialization vector.
algo string <optional>
Encryption algorithm (default: constants.cipher).
Returns:
Type:
Buffer
Encrypted data.

(inner) generateKeystoreFilename(publicKey) → {string}

Generate filename for a keystore file.
Source:
Parameters:
Name Type Description
publicKey String Topl address.
Returns:
Type:
string
Keystore filename.

(inner) getMAC(derivedKey, ciphertext) → {string}

Calculate message authentication code from secret (derived) key and encrypted text. The MAC is the keccak-256 hash of the byte array formed by concatenating the second 16 bytes of the derived key with the ciphertext key's contents.
Source:
Parameters:
Name Type Description
derivedKey Buffer | string Secret key derived from password.
ciphertext Buffer | string Text encrypted with secret key.
Returns:
Type:
string
Base58-encoded MAC.

(inner) isCipherAvailable(cipher) → {boolean}

Check if the selected cipher is available.
Source:
Parameters:
Name Type Description
cipher string Encryption algorithm.
Returns:
Type:
boolean
If available true, otherwise false.

(inner) keysEncodedFormat(keysBuffer) → {Array}

Parse KeysBuffer and split into [secretKey, publicKey]
Source:
Parameters:
Name Type Description
keysBuffer Buffer Buffer containing both keys
Returns:
Type:
Array
Array with format [sk, pk]

(inner) marshal(derivedKey, keyObject, salt, iv, algo, network) → {Object}

Assemble key data object in secret-storage format.
Source:
Parameters:
Name Type Description
derivedKey Buffer Password-derived secret key.
keyObject Object Object containing the raw public / private keypair
salt Buffer Randomly generated salt.
iv Buffer Initialization vector.
algo Buffer encryption algorithm to be used
network String network prefix as string i.e local/private/toplnet
Returns:
Type:
Object
key data object in secret-storage format

(inner) recover(password, keyStorage, kdfParamsopt) → {Buffer}

Recover plaintext private key from secret-storage key object.
Source:
Parameters:
Name Type Attributes Description
password string | Buffer User-supplied password.
keyStorage Object Keystore object.
kdfParams Object <optional>
key-derivation parameters
Returns:
Type:
Buffer
Plaintext private key.

(inner) str2buf(str, encopt) → {Buffer}

Convert a string to a Buffer with optional Node builtin encoding specified. If encoding is not specified, Base58 encoding will be assumed, if the input is valid.
Source:
Parameters:
Name Type Attributes Description
str string String to be converted.
enc string <optional>
Encoding of the input string (optional).
Returns:
Type:
Buffer
Buffer (bytearray) containing the input data.