Utils

Source utils.ts

Utils contain helper functions which are used across the whole project


promisify

utils.promisify(funThis, functionName, ...args);

run given function from this, use function(error, result) {…} callback for promise resolve/reject

Parameters

  1. funThis - any: the functions ‘this’ object
  2. functionName - string: name of the contract function to call
  3. ...args - any: any addtional parameters that should be passed to the called function

Returns

Promise resolves to any: the result from the function(error, result) {…} callback.

Example

runtime.utils
  .promisify(fs, 'readFile', 'somefile.txt')
  .then(content => console.log('file content: ' + content))

obfuscate

utils.obfuscate(text);

obfuscates strings by replacing each character but the last two with ‘x’

Parameters

  1. text - string: text to obfuscate

Returns

string: obfuscated text

Example

const obfuscated = runtime.utils.obfuscate('sample text');
// returns 'sample texx'

getSmartAgentAuthHeaders

utils.getSmartAgentAuthHeaders(runtime[, message]);

create auth header data to authenticate with active account or underlying account of active identity against a smart agent server

Parameters

  1. runtime - Runtime: an initialized runtime
  2. message - string (optional): message to sign, defaults to current timestamp
  3. identity - string (optional): identity address that is controlled by the underlying account of the runtime, uses activeIdentity as default

Returns

Promise resolves to string: auth header value as string

Example

const authData = await getSmartAgentAuthHeaders(runtime);
console.log(authData);
// Output:
// EvanAuth 0x001De828935e8c7e4cb56Fe610495cAe63fb2612,EvanIdentity 0x0d87204C3957D73b68AE28d0AF961d3c72403906,EvanMessage 1566569193297,EvanSignedMessage 0x4ce5c94b3fb77e6fbd7dcbbedc564058d841c849020f11514b7e525776b033eb6cb54f480b604ae7dccb9858eb116267cfe547fab52679730b5e33ac975dbbab1b