'use strict'
var config = require('../config/config.js');
var CryptoJS = require("crypto-js");
require('dotenv').config()
function toJson(code_, obj_, errMsg_) {
    var code = code_
    var data = obj_
    var errMsg = errMsg_
    let jopt = { code, data, errMsg };
    return JSON.stringify(jopt)
}

/**
 * 
 * @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
    console.log('encrypted=', encrypted);
    console.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);
    console.log('decrypted==', de_pk);
    return de_pk
}


const CHAIN_NAME = {
    eth: 'eth',
    bsc_testnet: 'bsc testnet',
    bsc_mainnet: 'bsc'
}

const CHAIN_ID = {
    eth: '0x1',
    bsc_testnet: '0x61',
    bsc_mainnet: '0x38'
}

function getChainName(key) {
    return CHAIN_NAME[key];
}

function getChainId(key) {
    return CHAIN_ID[key];
}

module.exports = {
    toJson,
    decryptPrivityKey,
    getChainName,
    getChainId,
}