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¶
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¶
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¶
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¶
str
-string
: String to modify.
Returns¶
string
: The string with “0x”.
isString¶
utils.isString(str)
Checks if the input value is a string.
Parameters¶
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¶
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¶
data
-string
: Message to sign.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¶
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¶
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¶
n
-number
: Number of transactions to generate.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¶
blockNum
-number
: Block in which this transaction will be included.sender
-string
: Address of the sender.recipient
-string
: Address of the recipient.numTransfers
-number
: Number of transfers to generate.
Returns¶
UnsignedTransaction
: An UnsignedTransaction object.