'use strict' var CryptoJS = require("crypto-js"); require('dotenv').config() const logger = require('./logger') var moment = require('moment'); const chinaTime = require('china-time'); var { cryppt_config } = require('../config/config.js') function toJson(code_, obj_, errMsg_) { var code = code_ var data = obj_ var errMsg = errMsg_ let jopt = { code, data, errMsg }; return JSON.stringify(jopt) } function decryptJson(message, key) { } /** * * @param {*} crypt_key 密文 * @param {*} message 待解密的内容 U2FsdGVkX18zHfDE3lO2yVxk2lMCEiwjrzYo1ZbHqfriFapZWXqLhczs9J0cN9EnvQR64GLRzYDgzMuAr9U1C0LZjA5mV4cSAZeBOrY8KNY09+BhTDetb2/43bIwiFR6 */ function decryptPrivityKey(message) { // var encrypted = 'Zc4v3uJgPY+BVg/IrDLA4aIk9ko/UrgjAtBTLqymdMAvmqdp1GttRjkBgNNNTyxA+3ThbIGlM0INeeHGicYVbLea4ymkty8gd30rZXQ4yYQ='; //python is base64 ECB // var key = '0IPmaec1o9FQCqGN'//key used in Python // var iv = 'Zh4A7bOY2ksp9oIn' var encrypted = message; //python is base64 ECB var key = process.env.DENET_CRYPT_KEY //key used in Python var iv = process.env.DENET_CRYPT_IV if (!key || !iv) { logger.error('decryptPrivityKey key or iv is empty?'); return null; } logger.log('encrypted=', encrypted); // logger.log('decryptPrivityKey key= iv=', key, iv); key = CryptoJS.enc.Utf8.parse(key); // var decrypted = CryptoJS.AES.decrypt(encrypted, key, {mode:CryptoJS.mode.ECB}); iv = CryptoJS.enc.Utf8.parse(iv) var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv, mode: CryptoJS.mode.CBC }); var de_pk = decrypted.toString(CryptoJS.enc.Utf8); // logger.log('decrypted==', de_pk); return de_pk } // 密钥 const SECRET_KEY = CryptoJS.enc.Utf8.parse(cryppt_config.WITHDRAW_KEY); // 密钥偏移量 const SECRET_IV = CryptoJS.enc.Utf8.parse("68F37CFC40C330D9FAAC0A16D49C8AD5"); /** * 加密方法 * @param data * @returns {string} */ function encrypt(data) { if (typeof data === "object") { try { data = JSON.stringify(data); } catch (error) { console.log("encrypt error:", error); } } const dataHex = CryptoJS.enc.Utf8.parse(data); const encrypted = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString(); } /** * 解密方法 * @param data * @returns {string} */ function decrypt(data) { // const key = CryptoJS.enc.Hex.parse(SECRET_KEY); const decrypt = CryptoJS.AES.decrypt(data, SECRET_KEY, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return CryptoJS.enc.Utf8.stringify(decrypt).toString(); } const CHAIN_NAME = { eth: 'eth', bsc_testnet: 'bsc testnet', bsc_mainnet: 'bsc' } const CHAIN_ID = { eth: '0x1', bsc_testnet: '0x61', bsc_mainnet: '0x38' } const CHAIN_ID_NAME = { 97: 'bsc_testnet', 56: 'bsc_mainnet', } function getChainName(key) { return CHAIN_NAME[key]; } function getChainIdToName(key) { return CHAIN_ID_NAME[key]; } function getChainId(key) { return CHAIN_ID[key]; } function sleep(time) { return new Promise((resolve) => setTimeout(resolve, time)); } function getTimestamp() { return new Date().getTime() } function getCurrentDate() { return chinaTime('YYYY-MM-DD HH:mm:ss'); } function getTimestampToDate(tm) { var now = new Date(tm), y = now.getFullYear(), m = now.getMonth() + 1, d = now.getDate(); return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + 'T' + now.toTimeString().substr(0, 8)+'.000Z'; } /** * @description 科学计数法转为string * @param {string, number} param */ function scientificNotationToString(param) { let strParam = String(param) let flag = /e/.test(strParam) if (!flag) return param // 指数符号 true: 正,false: 负 let sysbol = true if (/e-/.test(strParam)) { sysbol = false } // 指数 let index = Number(strParam.match(/\d+$/)[0]) // 基数 let basis = strParam.match(/^[\d\.]+/)[0].replace(/\./, '') if (sysbol) { return basis.padEnd(index + 1, 0) } else { return basis.padStart(index + basis.length, 0).replace(/^0/, '0.') } } module.exports = { toJson, decryptPrivityKey, getChainName, getChainId, CHAIN_NAME, CHAIN_ID, sleep, encrypt, decrypt, getTimestamp, getChainIdToName, getCurrentDate, scientificNotationToString, getTimestampToDate, }