UNPKG

1.26 kBJavaScriptView Raw
1/* helper functions
2 */
3const hash = require("hash.js")
4const TRACING = false //enable when debugging to see detailed outputs
5
6// log data
7function log(message, data) {
8 if (TRACING == true) {
9 console.log(message, data)
10 }
11}
12
13// encrypt the values of a json object
14function encryptParams(params) {
15 var hash = require("hash.js")
16 let encryptedParams = {}
17 Object.keys(params).map(key => {
18 encryptedParams[key] = hash.sha256().update(params[key]).digest('hex')
19 })
20 return encryptedParams
21}
22
23// get balance of an account for a given token symbol
24function getTokenAmount(tokenAmount, tokenSymbol = "CPU") {
25 try {
26 if (typeof tokenAmount === "number") {
27 const amount = parseFloat(tokenAmount).toFixed(4)
28 return amount.toString() + " " + tokenSymbol
29 } else if (typeof tokenAmount === "string") {
30 if (tokenAmount.split(" ")[1] === tokenSymbol) {
31 return tokenAmount
32 } else {
33 return parseFloat(tokenAmount).toFixed(4).toString() + " " + tokenSymbol
34 }
35 } else {
36 throw err
37 }
38 } catch (err) {
39 console.info(err)
40 }
41}
42
43module.exports = {
44 log,
45 encryptParams,
46 getTokenAmount
47}
\No newline at end of file