Skip to main content

Utils

This is a utility namespace mostly containing functions for managing multiformat type conversions.

Utils can be imported separately from the client:

const Utils = require("@eluvio/elv-client-js/src/Utils)

or

import Utils from "@eluvio/elv-client-js/src/Utils"

It can be accessed from ElvClient and FrameClient as client.utils

AddressToHash​

AddressToHash(
  address,
  key
) → string

Convert contract address to multiformat hash

Returns: Hash of contract address

Parameters

NameTypeRequiredDescription
addressstring✓

Address of contract

keyboolean✓

Whether or not the first param is a public key. Defaults to address type

AddressToLibraryId​

AddressToLibraryId(address) → string

Convert contract address to content library ID

Returns: Content library ID from contract address

Parameters

NameTypeRequiredDescription
addressstring✓

Address of contract

AddressToNodeId​

AddressToNodeId(address) → string

Convert contract address to node ID

Returns: Node ID from contract address

Parameters

NameTypeRequiredDescription
addressstring✓

Address of contract

AddressToObjectId​

AddressToObjectId(address) → string

Convert contract address to content object ID

Returns: Content object ID from contract address

Parameters

NameTypeRequiredDescription
addressstring✓

Address of contract

AddressToSpaceId​

AddressToSpaceId(address) → string

Convert contract address to content space ID

Returns: Content space ID from contract address

Parameters

NameTypeRequiredDescription
addressstring✓

Address of contract

DecodeAuthorizationToken​

DecodeAuthorizationToken(token) → Object

Decode the given fabric authorization token

Returns: Token Info: {qspace_id, qlib_id*, addr, tx_id*, afgh_pk*, signature}

Parameters

NameTypeRequiredDescription
tokenstring✓

The authorization token to decode

DecodeSignedToken​

DecodeSignedToken(token) → Object

Decode the specified signed token into its component parts

Returns: Components of the signed token

Parameters

NameTypeRequiredDescription
tokenstring✓

The token to decode

DecodeVersionHash​

DecodeVersionHash(versionHash) → Object

Decode the specified version hash into its component parts

Returns: Components of the version hash.

Parameters

NameTypeRequiredDescription
versionHash✓

DecodeWriteToken​

DecodeWriteToken(writeToken) → Object

Decode the specified write token into its component parts

Returns: Components of the write token.

Parameters

NameTypeRequiredDescription
writeToken✓

EqualAddress​

EqualAddress(
  firstAddress,
  secondAddress
) → boolean

Compare two addresses to determine if they are the same, regardless of format/capitalization

Returns: Whether or not the addresses match

Parameters

NameTypeRequiredDescription
firstAddress✓
secondAddress✓

EqualHash​

EqualHash(
  firstHash,
  secondHash
) → boolean

Compare two IDs to determine if the hashes are the same by comparing the contract address they resolve to

Returns: Whether or not the hashes of the IDs match

Parameters

NameTypeRequiredDescription
firstHash✓
secondHash✓

EtherToWei​

EtherToWei(ether) → BigNumber

Convert ether to wei

Returns: Given value in wei

Parameters

NameTypeRequiredDescription
ethernumber | string | BigNumber✓

Ether value to convert to wei

FormatAddress​

FormatAddress(address) → string

Convert address to normalized form - lower case with "0x" prefix

Returns: Formatted address

Parameters

NameTypeRequiredDescription
addressstring✓

Address to format

FormatSignature​

FormatSignature(sig) → string

Formats a signature into multi-sig

Returns: Multi-sig string representation of signature

Parameters

NameTypeRequiredDescription
sigstring✓

Hex representation of signature

HashToAddress​

HashToAddress(
  hash,
  key
) → string

Convert any content fabric ID to the corresponding contract address

Returns: Contract address of item

Parameters

NameTypeRequiredDescription
hashstring✓

Hash to convert to address

keyboolean✓

Whether or not the first param is a key. Defaults to address type

IsCloneable​

IsCloneable(value) → boolean

Determine if the given value is cloneable - Data passed in messages must be cloneable

Returns: Whether or not the value is cloneable

Parameters

NameTypeRequiredDescription
value*✓

Value to check

MakeClonable​

MakeClonable(value) → *

Make the given value cloneable if it is not already.

Note: this will remove or transform any attributes of the object that are not cloneable (e.g. functions)

Transformations:

  • Buffer: Converted to ArrayBuffer
  • Error: Converted to string (error.message)

Returns: Cloneable value

Parameters

NameTypeRequiredDescription
value*✓

Value to check

PublicKeyToAddress​

PublicKeyToAddress(key) → string

Converts the given string to a public address

Returns: the public address

Parameters

NameTypeRequiredDescription
key✓

Public key to convert to a public address

ResizeImage​

ResizeImage(
  imageUrl,
  height // optional
) → string

Resize the image file or link URL to the specified maximum height. Can also be used to remove max height parameter(s) from a url if height is not specified.

Returns: The modified URL with the height parameter

Parameters

NameTypeRequiredDescription
imageUrl✓

Url to an image file or link in the Fabric

heightnumber

The maximum height for the image to be scaled to.

ResponseToFormat​

ResponseToFormat(
  format,
  response,
  debug, // optional
  logFn
) → *

Interprets an http response body obtained from an http call as a requested format and returns result of converting/formatting.

Returns: Result of converting response body into the requested format

Parameters

NameTypeRequiredDescription
formatstring✓

The format to use when interpreting response body (e.g. "json", "text" et. al.)

responsePromise✓

An http response from node-fetch

debugboolean

Whether or not to log a debug statement containing the body (ignored for formats other than "json" and "text")

logFnfunction✓

Log function to use if debug is truthy

ResponseToJson​

ResponseToJson(
  response,
  debug, // optional
  logFn
) → *

Interprets an http response body obtained from an http call as JSON and returns result of parsing it.

Returns: Result of parsing response body as JSON

Parameters

NameTypeRequiredDescription
responsePromise✓

An http response from node-fetch

debugboolean

Whether or not to log the body

logFnfunction✓

Log function to use if debug is truthy

ToBigNumber​

ToBigNumber(value) → BigNumber

Convert number or string to BigNumber

Returns: Given value as a BigNumber

Parameters

NameTypeRequiredDescription
valuestring | number✓

Value to convert to BigNumber

ToBytes32​

ToBytes32(string) → string

Convert the specified string to a bytes32 string

Returns: The given string in bytes32 format

Parameters

NameTypeRequiredDescription
stringstring✓

String to format as a bytes32 string

ValidAddress​

ValidAddress(address) → boolean

Determine whether the address is valid

Returns: Whether or not the address is valid

Parameters

NameTypeRequiredDescription
addressstring✓

Address to validate

ValidHash​

ValidHash(hash) → boolean

Determine whether the hash is valid

Returns: Whether or not the hash is valid

Parameters

NameTypeRequiredDescription
hashstring✓

Hash to validate

WeiToEther​

WeiToEther(wei) → BigNumber

Convert wei to ether

Returns: Given value in ether

Parameters

NameTypeRequiredDescription
weistring | BigNumber✓

Wei value to convert to ether