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
address,
key
) → string
Convert contract address to multiformat hash
Returns: Hash of contract address
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
address | string | ✓ | Address of contract |
key | boolean | ✓ | Whether or not the first param is a public key. Defaults to address type |
AddressToLibraryId
Convert contract address to content library ID
Returns: Content library ID from contract address
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
address | string | ✓ | Address of contract |
AddressToNodeId
Convert contract address to node ID
Returns: Node ID from contract address
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
address | string | ✓ | Address of contract |
AddressToObjectId
Convert contract address to content object ID
Returns: Content object ID from contract address
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
address | string | ✓ | Address of contract |
AddressToSpaceId
Convert contract address to content space ID
Returns: Content space ID from contract address
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
address | string | ✓ | Address of contract |
DecodeAuthorizationToken
Decode the given fabric authorization token
Returns: Token Info: {qspace_id, qlib_id*, addr, tx_id*, afgh_pk*, signature}
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
token | string | ✓ | The authorization token to decode |
DecodeSignedToken
Decode the specified signed token into its component parts
Returns: Components of the signed token
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
token | string | ✓ | The token to decode |
DecodeVersionHash
Decode the specified version hash into its component parts
Returns: Components of the version hash.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
versionHash | ✓ |
DecodeWriteToken
Decode the specified write token into its component parts
Returns: Components of the write token.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
writeToken | ✓ |
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
| Name | Type | Required | Description |
|---|---|---|---|
firstAddress | ✓ | ||
secondAddress | ✓ |
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
| Name | Type | Required | Description |
|---|---|---|---|
firstHash | ✓ | ||
secondHash | ✓ |
EtherToWei
Convert ether to wei
Returns: Given value in wei
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
ether | number | string | BigNumber | ✓ | Ether value to convert to wei |
FormatAddress
Convert address to normalized form - lower case with "0x" prefix
Returns: Formatted address
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
address | string | ✓ | Address to format |
FormatSignature
Formats a signature into multi-sig
Returns: Multi-sig string representation of signature
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
sig | string | ✓ | Hex representation of signature |
HashToAddress
hash,
key
) → string
Convert any content fabric ID to the corresponding contract address
Returns: Contract address of item
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
hash | string | ✓ | Hash to convert to address |
key | boolean | ✓ | Whether or not the first param is a key. Defaults to address type |
IsCloneable
Determine if the given value is cloneable - Data passed in messages must be cloneable
Returns: Whether or not the value is cloneable
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
value | * | ✓ | Value to check |
MakeClonable
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
| Name | Type | Required | Description |
|---|---|---|---|
value | * | ✓ | Value to check |
PublicKeyToAddress
Converts the given string to a public address
Returns: the public address
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
key | ✓ | Public key to convert to a public address |
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
| Name | Type | Required | Description |
|---|---|---|---|
imageUrl | ✓ | Url to an image file or link in the Fabric | |
height | number | The maximum height for the image to be scaled to. |
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
| Name | Type | Required | Description |
|---|---|---|---|
format | string | ✓ | The format to use when interpreting response body (e.g. "json", "text" et. al.) |
response | Promise | ✓ | An http response from node-fetch |
debug | boolean | Whether or not to log a debug statement containing the body (ignored for formats other than "json" and "text") | |
logFn | function | ✓ | Log function to use if debug is truthy |
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
| Name | Type | Required | Description |
|---|---|---|---|
response | Promise | ✓ | An http response from node-fetch |
debug | boolean | Whether or not to log the body | |
logFn | function | ✓ | Log function to use if debug is truthy |
ToBigNumber
Convert number or string to BigNumber
Returns: Given value as a BigNumber
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
value | string | number | ✓ | Value to convert to BigNumber |
ToBytes32
Convert the specified string to a bytes32 string
Returns: The given string in bytes32 format
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
string | string | ✓ | String to format as a bytes32 string |
ValidAddress
Determine whether the address is valid
Returns: Whether or not the address is valid
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
address | string | ✓ | Address to validate |
ValidHash
Determine whether the hash is valid
Returns: Whether or not the hash is valid
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
hash | string | ✓ | Hash to validate |
WeiToEther
Convert wei to ether
Returns: Given value in ether
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
wei | string | BigNumber | ✓ | Wei value to convert to ether |