First commit
This commit is contained in:
		
							
								
								
									
										13
									
								
								lib/logger.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								lib/logger.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| 'use strict' | ||||
|  | ||||
| // Log | ||||
| const logMessage = (source, msg) => { | ||||
|   console.log(new Date(), `[${source}]`, msg) | ||||
| } | ||||
|  | ||||
| // Error | ||||
| const errorMessage = (source, msg) => { | ||||
|   console.error(new Date(), `[${source}]`, msg) | ||||
| } | ||||
|  | ||||
| module.exports = { logMessage, errorMessage } | ||||
							
								
								
									
										84
									
								
								lib/tron.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								lib/tron.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,84 @@ | ||||
| 'use strict' | ||||
|  | ||||
| // Environment | ||||
| require('dotenv').config() | ||||
|  | ||||
| // Libraries | ||||
| const path = require('path') | ||||
| const fs = require('fs') | ||||
| const { decryptString } = require('./utils') | ||||
|  | ||||
| // Connect to Tron network | ||||
| const TronWeb = require('tronweb') | ||||
|  | ||||
| // Set Tron connection | ||||
| const API_URL = | ||||
|   process.env.DEBUG === '1' | ||||
|     ? process.env.TRON_NILE_API | ||||
|     : process.env.DEBUG === '2' | ||||
|     ? process.env.TRON_LOCAL_API | ||||
|     : process.env.TRON_MAINNET_API | ||||
| const HttpProvider = TronWeb.providers.HttpProvider | ||||
| const fullNode = new HttpProvider(API_URL) | ||||
| const solidityNode = new HttpProvider(API_URL) | ||||
| const eventServer = API_URL | ||||
| const tronWeb = new TronWeb(fullNode, solidityNode, eventServer) | ||||
| const feeLimit = parseInt(process.env.FEE_LIMIT) | ||||
|  | ||||
| // Set contract data | ||||
| const contractPath = path.join( | ||||
|   __dirname, | ||||
|   `../build/contracts/${process.env.CONTRACT_NAME}.json` | ||||
| ) | ||||
| const contractData = JSON.parse(fs.readFileSync(contractPath)) | ||||
| const contract = tronWeb.contract( | ||||
|   contractData.abi, | ||||
|   process.env.CONTRACT_ADDRESS | ||||
| ) | ||||
|  | ||||
| // TRX to SUN conversion | ||||
| function trxToSun(trx) { | ||||
|   const sun = +(+trx * 1e6).toFixed(0) | ||||
|   return sun | ||||
| } | ||||
|  | ||||
| // SUN to TRX conversion | ||||
| function sunToTrx(sun) { | ||||
|   const trx = +(+sun / 1e6).toFixed(6) | ||||
|   return trx | ||||
| } | ||||
|  | ||||
| // Address to base58 conversion | ||||
| function addressToBase58(address) { | ||||
|   return tronWeb.address.fromHex(address) | ||||
| } | ||||
|  | ||||
| // Set default private key | ||||
| function setDefaultPrivateKey(pk) { | ||||
|   // Decrypt private key | ||||
|   const decPK = | ||||
|     pk || | ||||
|     decryptString(process.env.PRIVATE_KEY_MAINNET, process.env.SEED_PASSWORD) | ||||
|   // Set private key in TronWeb | ||||
|   tronWeb.setPrivateKey(decPK.substring(2)) | ||||
|  | ||||
|   return decPK | ||||
| } | ||||
|  | ||||
| // Get TRX balance | ||||
| const getTrxBalance = async (address) => { | ||||
|   const balance = sunToTrx(await tronWeb.trx.getBalance(address)) | ||||
|   return balance | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   tronWeb, | ||||
|   setDefaultPrivateKey, | ||||
|   feeLimit, | ||||
|   contractData, | ||||
|   contract, | ||||
|   trxToSun, | ||||
|   sunToTrx, | ||||
|   addressToBase58, | ||||
|   getTrxBalance | ||||
| } | ||||
							
								
								
									
										73
									
								
								lib/utils.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								lib/utils.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| 'use strict' | ||||
|  | ||||
| // Libraries | ||||
| const { customAlphabet } = require('nanoid') | ||||
| const StringCrypto = require('string-crypto') | ||||
| const path = require('path') | ||||
|  | ||||
| // NanoId with custom alphabet | ||||
| const newId = (len) => { | ||||
|   const nanoid = customAlphabet( | ||||
|     'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', | ||||
|     len || 21 | ||||
|   ) | ||||
|   return nanoid() | ||||
| } | ||||
|  | ||||
| // Remove all _id | ||||
| const removeFieldByName = (o, fieldName) => { | ||||
|   delete o[fieldName] | ||||
|   for (let v of Object.values(o)) | ||||
|     if (v instanceof Object) removeFieldByName(v, fieldName) | ||||
| } | ||||
|  | ||||
| // Clean object from unnecesary fields | ||||
| const fieldsToRemove = ['_id'] | ||||
| const cleanObject = (o) => { | ||||
|   const obj = JSON.parse(JSON.stringify(o)) | ||||
|   fieldsToRemove.forEach((f) => { | ||||
|     removeFieldByName(obj, f) | ||||
|   }) | ||||
|   return obj | ||||
| } | ||||
|  | ||||
| // Securize sensible data | ||||
| const cryptoOptions = { | ||||
|   salt: process.env.CRYPTO_SALT, | ||||
|   iterations: parseInt(process.env.CRYPTO_ITERATIONS), | ||||
|   digest: process.env.CRYPTO_DIGEST // one of: 'blake2b512' | 'blake2s256' | 'md4' | 'md5' | 'md5-sha1' | 'mdc2' | 'ripemd160' | 'sha1' | 'sha224' | 'sha256' | 'sha3-224' | 'sha3-256' | 'sha3-384' | 'sha3-512' | 'sha384' | 'sha512' | 'sha512-224' | 'sha512-256' | 'sm3' | 'whirlpool'; | ||||
| } | ||||
| const stringCypto = new StringCrypto(cryptoOptions) | ||||
| const encryptString = (value, password) => { | ||||
|   return stringCypto.encryptString(value, password) | ||||
| } | ||||
|  | ||||
| const decryptString = (value, password) => { | ||||
|   return stringCypto.decryptString(value, password) | ||||
| } | ||||
|  | ||||
| // Split array into pieces | ||||
| const chunk = (arr, size) => | ||||
|   arr.reduce( | ||||
|     (acc, _, i) => (i % size ? acc : [...acc, arr.slice(i, i + size)]), | ||||
|     [] | ||||
|   ) | ||||
|  | ||||
| // Sleep | ||||
| const sleep = (segundos) => { | ||||
|   return new Promise((resolve) => setTimeout(resolve, segundos * 1000)) | ||||
| } | ||||
|  | ||||
| // Get file name | ||||
| const getFileName = (f) => path.basename(f, '.js') | ||||
|  | ||||
| module.exports = { | ||||
|   newId, | ||||
|   removeFieldByName, | ||||
|   cleanObject, | ||||
|   encryptString, | ||||
|   decryptString, | ||||
|   chunk, | ||||
|   sleep, | ||||
|   getFileName | ||||
| } | ||||
		Reference in New Issue
	
	Block a user