Utilities

plasma-utils.utils provides miscellaneous utilities often used when interacting with plasma chains.

const utils = require('plasma-utils').utils

int32ToHex

utils.int32ToHex(x)

Converts a 32 byte integer to a hex string.

Parameters

  1. x - number: A 32 byte integer.

Returns

string: The integer represented as a hex string.


sleep

utils.sleep(ms)

Sleeps for a number of milliseconds.

Parameters

  1. ms - number: Number of milliseconds to sleep.

Returns

Promise: A promise that resolves after the given number of ms.


remove0x

utils.remove0x(str)

Removes “0x” from the start of a string, if it’s present.

Parameters

  1. str - string: String to modify.

Returns

string: The string without a leading “0x”.


add0x

utils.add0x(x)

Adds “0x” to the start of a string, if not already present.

Parameters

  1. str - string: String to modify.

Returns

string: The string with “0x”.


isString

utils.isString(str)

Checks if the input value is a string.

Parameters

  1. str - any: The thing that might be a string.

Returns

boolean: true if the input is a string, false otherwise.


getRandomElement

utils.getRandomElement(arr)

Returns a random element from an array.

Parameters

  1. arr - Array: An array.

Returns

any: A random element from that array.


getRandomAccount

utils.getRandomAccount()

Returns a random Ethereum account.

Returns

any: The Ethereum account.


sign

utils.sign(data, key)

Signs a message with a private key.

Parameters

  1. data - string: Message to sign.
  2. key - string: Private key to sign with.

Returns

string: The signature.


signatureToString

utils.signatureToString(signature)

Converts a signature with v,r,s Buffers to a single hex string.

Parameters

  1. signature - Object: A signature object.

Returns

string: The signature as a hex string.


stringToSignature

utils.stringToSignature(signature)

Converts a hex string signature into an object with v,r,s Buffers.

Parameters

  1. signature - string: A signature string.

Returns

Object: A signature object with v,r,s as Buffers.


getSequentialTxs

utils.getSequentialTxs(n, blockNum)

Generates sequential transactions in the same block. Usually used for testing with mass amounts of transactions.

Parameters

  1. n - number: Number of transactions to generate.
  2. blockNum - number: Block in which the transactions will be included.

Returns

Array<SignedTransaction>: An array of SignedTransaction objects.


getRandomTx

utils.getRandomTx(blockNum, sender, recipient, numTransfers)

Generates a random transaction. Usually used for testing.

Parameters

  1. blockNum - number: Block in which this transaction will be included.
  2. sender - string: Address of the sender.
  3. recipient - string: Address of the recipient.
  4. numTransfers - number: Number of transfers to generate.

Returns

UnsignedTransaction: An UnsignedTransaction object.